/
api.ts
1242 lines (1156 loc) · 27.4 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
EditorNotification,
NotificationLevel,
announceNotification,
} from './ui/parts/notifications';
import {FeatureManagerSettings} from '../utility/featureManager';
import {FieldConfig} from '@blinkk/selective-edit';
import {IncludeExcludeFilterConfig} from '../utility/filter';
import {LiveEditorLabels} from './template';
import bent from 'bent';
/**
* Interface for the live editor api.
*
* This defines how the editor works with the underlying data. The api
* is responsible for all file or network operations needed to make the
* editor function.
*/
export interface LiveEditorApiComponent {
/**
* Verify that the authentication for services that require auth.
*
* @returns True if the authentication is complete.
*/
checkAuth(): boolean;
/**
* Verify that the basic information is available for the editor.
*
* If information needed to start editing files are missing the
* onboarding UI will be shown.
*
* @returns Onboarding status to determine if the app needs to
* onboard users to prompt for more information.
*/
checkOnboarding(): Promise<OnboardingInfo>;
/**
* Clean any current authentication and log out the user of any
* accounts.
*/
clearAuth(): Promise<void>;
/**
* Copy a file.
*
* @param path Full path for the original file.
* @param path Full path for the new file.
*/
copyFile(originalPath: string, path: string): Promise<FileData>;
/**
* Create a new file from scratch.
*
* @param path Full path for the new file.
*/
createFile(path: string): Promise<FileData>;
/**
* Create a new workspace based off an existing workspace.
*
* @param base Workspace to base new workspace from.
* @param workspace New workspace name.
*/
createWorkspace(
base: WorkspaceData,
workspace: string
): Promise<WorkspaceData>;
/**
* Delete an existing file.
*
* @param path Full path for the file being deleted.
*/
deleteFile(file: FileData): Promise<EmptyData>;
/**
* Retrieve information about the account requirements for the
* project.
*/
getAuthentication(): Promise<AuthenticationData>;
/**
* Retrieve the devices used for previews.
*/
getDevices(): Promise<Array<DeviceData>>;
/**
* Retrieve the file information.
*
* This is a complete loading of the file information and
* configuration for use in rendering the editor for the
* file.
*/
getFile(file: FileData): Promise<EditorFileData>;
/**
* Retrieve the files that can be edited in the editor.
*/
getFiles(): Promise<Array<FileData>>;
/**
* Retrieve the url to preview the file.
*
* When retrieving a list of files it is often slow to
* retrieve the serving url for all the files. If the
* preview url is missing, the API will be called to
* the serving url on a per file basis.
*/
getFileUrl(file: FileData): Promise<FileData>;
/**
* Retrieve configuration for the preview server.
*/
getPreviewConfig(
settings: EditorPreviewSettings,
workspace: WorkspaceData
): Promise<PreviewSettings>;
/**
* Retrieve information about the project.
*/
getProject(): Promise<ProjectData>;
/**
* Retrieve information about the active workspace.
*/
getWorkspace(): Promise<WorkspaceData>;
/**
* Retrieve information about available workspaces.
*/
getWorkspaces(): Promise<Array<WorkspaceData>>;
/**
* Load the workspace.
*
* This may redirect to a different URL.
* (ex: workspaces may be domain based.)
*/
loadWorkspace(workspace: WorkspaceData): Promise<WorkspaceData>;
/**
* Project type specific apis.
*/
projectTypes: ApiProjectTypes;
/**
* Start the publish process.
*
* Begins the publish process. Some publish processes may take time and cannot
* be completed right away. This api begins the process of publishing and
* gives a status response on the new publish.
*/
publish(
workspace: WorkspaceData,
data?: Record<string, any>
): Promise<PublishResult>;
/**
* Save the updated file data.
*
* @param file File data to be saved.
* @param isRawEdit Is the edit to the raw file data?
*/
saveFile(file: EditorFileData, isRawEdit: boolean): Promise<EditorFileData>;
/**
* Update onboarding information when it is changed in the UI.
*
* If information needed to start editing files are missing the
* onboarding UI will be shown. After updating the API decides
* if the onboarding is complete.
*
* @returns Onboarding status to determine if the app needs to
* onboard users to prompt for more information.
*/
updateOnboarding(info: OnboardingInfo): Promise<OnboardingInfo>;
/**
* Upload a file.
*
* Uses a File object to provide a blob file that should be uploaded
* or saved appropriately. Often for media like images or videos.
*/
uploadFile(file: File, options?: MediaOptions): Promise<MediaFileData>;
}
/**
* Information about the account requirements.
*/
export interface AuthenticationData {
/**
* Are there accounts in use?
*/
usesAccounts: boolean;
}
export interface ApiProjectTypes {
amagaki: AmagakiProjectTypeApi;
grow: GrowProjectTypeApi;
}
export interface AmagakiProjectTypeApi {
/**
* Retrieve the partials for the project for the partials field.
*/
getPartials(): Promise<Record<string, PartialData>>;
}
export interface GrowProjectTypeApi {
/**
* Retrieve the partials for the project for the partials field.
*/
getPartials(): Promise<Record<string, PartialData>>;
/**
* Retrieve the available strings used in the `!g.string` yaml constructor.
*
* Returns a mapping of strings podpath to the contents of the strings file.
*
* ```json
* {
* "/content/strings/example.yaml": {
* "foo": "bar"
* }
* }
* ```
*/
getStrings(): Promise<Record<string, any>>;
}
/**
* Interface for the structure of the editor settings file.
*
* Settings in a project's `editor.yaml` should follow this interface.
*/
export interface EditorFileSettings {
/**
* Title of the site to display in the editor.
*/
title?: string;
/**
* Devices to use in the editor preview.
*/
devices?: Array<DeviceData>;
/**
* Editor experiment flags and settings.
*
* Used to control editor.dev experiments for the project.
*/
experiments?: Record<string, boolean | FeatureManagerSettings>;
/**
* Editor feature flags and settings.
*
* Used to control editor.dev features for the project.
*/
features?: Record<string, boolean | FeatureManagerSettings>;
/**
* Media configuration for the project.
*
* This controls options around how the media is handled in the project.
* Including custom providers for media upload.
*/
media?: ProjectMediaConfig;
/**
* Settings for previewing the files.
*/
preview?: EditorPreviewSettings;
/**
* Configuration for the site display in the editor.
*/
site?: SiteData;
/**
* Users or groups approved access to the editor.
*/
users?: Array<UserData>;
/**
* Settings for customizing the editor UI.
*/
ui?: EditorUiSettings;
}
export interface EditorPreviewSettings {
/**
* Base url for the preview iframe to use.
*
* All preview urls will use the base url for things like the preview
* iframe, non-remote media urls, etc.
*/
baseUrl: string;
/**
* Url for preview server configuration for the site.
*
* If no route url is provided the editor will use the `baseUrl` and
* search for `${baseUrl}/preview.json`.
*
* Example `preview.json` format (Uses PreviewFileSettings interface):
*
* ```json
* {
* "routes": {
* "/content/pages/index.yaml": {
* "en": {
* "path": "/",
* "title": "Coolest site evar!",
* }
* }
* }
* }
* ```
*/
configUrl?: string;
}
export interface EditorUiSettings {
/**
* Labels for customizing the editor UI.
*/
labels?: LiveEditorLabels;
}
/**
* Interface for the structure of the preview server config.
*
* The editor reads the preview server's configuration to determine
* how to preview content.
*
* The url of the preview server configuration is defined in the
* `EditorFileSettings`.
*/
export interface PreviewSettings {
/**
* Default locale used by the preview server.
*/
defaultLocale: string;
/**
* Route information for the preview server
*/
routes: PreviewRoutesData;
}
/**
* Interface for the structure of the preview routes file.
*
* The editor reads the preview server's routes file to determine
* how to serve a file's preview.
*/
export interface PreviewRoutesData {
/**
* Mapping of path string to the localized or meta data for
* the path contents.
*/
[path: string]: PreviewRoutesLocaleData | PreviewRoutesMetaData;
}
/**
* Interface for the preview server's route localized data.
*
* File contents
*/
export interface PreviewRoutesLocaleData {
/**
* Mapping of a locale to the meta data about the path contents.
*/
[locale: string]: PreviewRoutesMetaData;
}
/**
* Interface for the preview server's route meta data.
*
* Every file in the routes provides information for the editor to use
* in previewing a servable path.
*/
export interface PreviewRoutesMetaData {
/**
* Title for the route resource.
*
* For example, the title of a page if the path is a document.
*/
title?: string;
/**
* Serving path for the servable file.
*
* Should be a relative path from the preview server's `baseUrl` defined
* in the `editor.yaml` (ex: `/images/something.png`)
* or a fully qualified url for serving the file (ex: `https://...`).
*/
path: string;
}
/**
* Device information used for previews.
*/
export interface DeviceData {
/**
* Can the device preview be rotated?
*/
canRotate?: boolean;
/**
* Height of the device view.
*/
height?: number;
/**
* Is the device the default for the device view?
*/
isDefault?: boolean;
/**
* Label for the device.
*/
label: string;
/**
* Width of the device view.
*/
width: number;
}
/**
* Full file information for rendering the file editor.
*/
export interface EditorFileData {
/**
* File contents.
*
* For example, the html in an html file, or the markdown body
* in a markdown file.
*/
content?: string;
/**
* File data.
*
* For example, the frontmatter for a markdown file or the contents
* of a yaml file.
*/
data?: any;
/**
* Raw file data.
*
* For example, the frontmatter for a markdown file or the contents
* of a yaml file. This is the unprocessed data string that can be edited
* in the 'Raw' content form.
*/
dataRaw?: string;
/**
* Editor configuration for the file.
*
* If not provided the editor will attempt to guess the fields to use.
*/
editor?: EditorFileConfig;
/**
* File information.
*/
file: FileData;
/**
* File repository history.
*/
history?: Array<RepoCommit>;
/**
* Sha of the file being edited.
*
* Used by the api to verify that there are no new changes to the file
* since the edit started to avoid overwriting external changes.
*/
sha?: string;
/**
* URL for viewing the file in the preview iframe.
*
* If no url is provided the preview will be hidden.
*/
url?: string;
/**
* URLs for viewing the file in different environments.
*/
urls?: Array<UrlConfig>;
}
/**
* Empty response from the api.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface EmptyData {}
/**
* File information.
*/
export interface FileData {
/**
* Complete path for the file.
*/
path: string;
/**
* URL for serving the file.
*
* Used for showing previews of different files.
* For example, image or video previews.
*
* For performance reasons, file data does not need to include url
* information as it may require more time to properly retrieve the
* url for a file which can slow down file list retrieval.
*
* `undefined` url is used to denote that the url was not retrieved.
* `null` url is used to denote that there is no url for the file.
*
* The editor will attempt to use the `getFileUrl()` api method when
* the url is `undefined` and the url is needed. If the value is `null`
* the editor assumes that there is no url for the file and does not
* make a request to the `getFileUrl()` method.
*/
url?: string | null;
}
export interface PartialData {
/**
* Partial identifier.
*
* This is the same value used in the partial definition of partials
* in content files.
*/
partial: string;
/**
* Configuration for how the editor should present the partial in the
* editor.
*/
editor?: PartialEditorConfig;
}
/**
* Configuration for rendering the file editor.
*/
export interface PartialEditorConfig {
/**
* Partial label.
*/
label?: string;
/**
* Partial description.
*/
description?: string;
/**
* Field configurations for the editor.
*/
fields: Array<FieldConfig>;
/**
* Is the partial hidden?
*
* Partials can opt to not show the partial in the listing of partials.
* This is helpful for partials that are part of the design such as
* header and footer partials.
*/
isHidden?: boolean;
/**
* Preview field key.
*
* When showing a preview of the partial, use this field key to determine
* the value to show for the preview.
*/
previewField?: string;
/**
* Preview field keys.
*
* When showing a preview of the partial, use these field keys to determine
* the value to show for the preview.
*/
previewFields?: Array<string>;
/**
* When displaying lists of paritals the priority will affect the
* sort order of the partials.
*
* Default priority is 1000
*/
priority?: number;
}
/**
* Overall project information.
*/
export interface ProjectData {
/**
* Url for an avatar to use when showing a project.
*/
avatarUrl?: string;
/**
* Project title
*/
title: string;
/**
* Editor experiment flags and settings.
*
* Experiments can be defined in the `editor.yaml` file for the project.
* The API can override experiment flags if the API does not support
* specific experiments of the editor.
*/
experiments?: Record<string, boolean | FeatureManagerSettings>;
/**
* Editor feature flags and settings.
*
* Features can be defined in the `editor.yaml` file for the project.
* The API can override feature flags if the API does not support
* specific features of the editor.
*/
features?: Record<string, boolean | FeatureManagerSettings>;
/**
* The editor will use important links for the project in the UI.
*/
links?: ProjectLinksData;
/**
* Media configuration for the project.
*
* This controls options around how the media is handled in the project.
*/
media?: ProjectMediaConfig;
/**
* Publish configuration for the project.
*
* This controls if the UI allows for publishing and what information
* to collect for providing to the `publish` method on the api.
*/
publish?: ProjectPublishConfig;
/**
* Settings for previewing the project files.
*/
preview?: EditorPreviewSettings;
/**
* Configuration for the site display.
*/
site?: SiteData;
/**
* Information about the source of the project.
*
* This information provides information for the editor on where the
* project data is coming from (ex: local project or a service).
*/
source?: SourceData;
/**
* Project type for the editor to use.
*/
type?: ProjectTypes | string;
/**
* Settings for customizing the editor UI.
*/
ui?: EditorUiSettings;
/**
* Users or groups approved access to the editor.
*/
users?: Array<UserData>;
}
/**
* Links for the project that can be used in the UI to direct the user.
*/
export interface ProjectLinksData {
/**
* The UI can display a breadcrumb style heirarchy links for the project.
* This allows for easier navigation between projects on a given service.
*/
breadcrumbs?: Array<LinkData>;
}
/**
* Information about a specific link.
*/
export interface LinkData {
/**
* Label for how the link is displayed.
*/
label?: string;
/**
* URL for the link.
*/
url: string;
}
/**
* Result from pinging the api.
*/
export interface PingResult {
/**
* Status of the api.
*/
status: PingStatus | string;
}
/**
* Result from starting the publish process.
*/
export interface PublishResult {
/**
* Status of the publish process.
*/
status: PublishStatus | string;
/**
* Updated workspace data.
*
* When a publish process is complete the workflow for the publish
* process can either keep the same workspace open, or remove it.
* In the case that the workspace is removed, the api can direct the
* editor to load a different workspace instead.
*/
workspace?: WorkspaceData;
/**
* URLs for viewing the publish state.
*/
urls?: Array<UrlConfig>;
}
/**
* Site information.
*/
export interface SiteData {
/**
* Site files configuration.
*/
files?: SiteFilesConfig;
}
/**
* Information on the source of the project data.
*/
export interface SourceData {
/**
* Source identifier.
*/
source: ProjectSource | string;
/**
* Label shown to the user for identifying the source.
*/
label: string;
/**
* Identifier for the project.
*
* For example, in a github project this would be the
* '<organization>/<project>' (ex: blinkk/amagaki-starter)
*/
identifier: string;
}
/**
* User information.
*/
export interface UserData {
/**
* Name of the user.
*/
name: string;
/**
* Email representing the user.
*/
email: string;
/**
* Is the user data a group?
*/
isGroup?: boolean;
}
/**
* Workspace information.
*/
export interface WorkspaceData {
/**
* Full branch information from the workspace.
*/
branch: RepoBranch;
/**
* Short name for the workspace used in labels and lists.
*/
name: string;
/**
* Workspace specific publishing configuration.
*/
publish?: WorkspacePublishConfig;
}
/**
* When there are errors with an api call the promise should
* be rejected with an ApiError argument.
*
* ```typescript
* createWorkspace(base: WorkspaceData, workspace: string): Promise<null> {
* return new Promise<null>((resolve, reject) => {
* // Successful api calls resolve() when done.
* // Failure should reject() the promise with an ApiError argument.
* reject({message: 'Houston we have a problem'} as ApiError);
* });
* }
* ```
*/
export interface ApiError extends EditorNotification {
/**
* Additional meta information that can be used for a full report
* or debugging of the error.
*/
details?: any;
/**
* Code for what error is being returned.
*/
errorCode?: ApiErrorCode | string;
}
export enum ApiErrorCode {
FileNotFound = 'FileNotFound',
WorkspaceNotFound = 'WorkspaceNotFound',
}
/**
* Catch and announce an api error.
*
* @param error Error from api.
*/
export function catchError(
error: ApiError | bent.StatusError,
callback?: (error: ApiError) => void
) {
const handler = callback || announceNotification;
// Check for bent status error for failed api call.
if ((error as bent.StatusError).json) {
(error as bent.StatusError).json().then(value => {
const apiError = value as ApiError;
apiError.level = NotificationLevel.Error;
handler(apiError);
});
return;
}
error = error as ApiError;
error.level = NotificationLevel.Error;
handler(error);
}
/**
* Auxillary interfaces used in the api data.
*/
/**
* Configuration for rendering the file editor.
*/
export interface EditorFileConfig {
/**
* Field configurations for the editor.
*/
fields: Array<FieldConfig>;
}
/**
* Configuration for how media works in the editor UI.
*/
export interface ProjectMediaConfig {
/**
* Remote configuration for uploading media to a remote provider.
*
* This is used for things such as uploading to a CDN or
* optimization service.
*/
remote?: RemoteMediaOptions;
/**
* Local media configuration for uploading media using the
* connector api.
*/
options?: MediaOptions;
}
/**
* Configuration for how publishing works in the editor UI.
*/
export interface ProjectPublishConfig {
/**
* Field information for collecting information for the publish process.
*
* If there are field configurations provided the UI will prompt the user
* for the information and pass it on to the `publish` api call.
*/
fields?: Array<FieldConfig>;
}
/**
* Configuration for how site files are displayed.
*/
export interface SiteFilesConfig {
/**
* Filter settings for how the site files are filtered.
*
* By default the site files filters:
* - Only `.yaml`, `.md`, and `.html` files.
* - Ignores files and directories starting with `_` and `.`.
*/
filter?: IncludeExcludeFilterConfig;
}
/**
* Remote media providers supported in the editor.
*/
export enum RemoteMediaProviders {
GCS = 'GCS',
}
/**
* Project types supported in the editor.
*/
export enum ProjectTypes {
Amagaki = 'Amagaki',
Grow = 'Grow',
}
/**
* Status for the api ping.
*/
export enum PingStatus {
/**
* Api is available.
*/
Ok = 'Ok',
}
/**
* Status for the publish process.
*/
export enum PublishStatus {
/**
* There are no publish processes allowed.
*
* Some workspaces may not allow for publishing.
* For example the `main` branch has no where to be published.
*/
NotAllowed = 'NotAllowed',
/**
* There are no active publish processes.
*/
NotStarted = 'NotStarted',
/**
* There are no changes to publish.
*
* For example, the main branch and the current branch are on the same
* commit and there is nothing to publish.
*/
NoChanges = 'NoChanges',
/**
* There is an active publish in process.
*/
Pending = 'Pending',
/**
* The publish process has completed.
*/
Complete = 'Complete',
/**
* There was a problem during the publish process.
*/
Failure = 'Failure',
}
/**
* Repository author information.
*/
export interface RepoAuthor {
/**
* Name of the author.
*/
name: string;
/**
* Email address of the author.
*/
email: string;
}
/**
* Repository branch information.
*/
export interface RepoBranch {
/**
* Commit most recent commit.
*/
commit: RepoCommit;
/**
* Full branch name.
*/
name: string;
/**
* Url for viewing the branch in an external system.
*/
url?: string;
}
/**
* Repository commit information.
*/
export interface RepoCommit {
/**
* Author of the last commit.
*/
author?: RepoAuthor;
/**
* Commit hash of the last commit.
*/
hash: string;
/**
* Full commit message.
*/
message?: string;
/**
* Summary of the commit.
*/