-
Notifications
You must be signed in to change notification settings - Fork 31
/
api.ts
148 lines (134 loc) · 4.37 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
import Field from './field'
import FieldLocale from './field-locale'
import createWindow from './window'
import createEntry from './entry'
import createSpace from './space'
import createDialogs from './dialogs'
import createEditor from './editor'
import createNavigator from './navigator'
import createApp from './app'
import locations from './locations'
import {
EntryFieldInfo,
NavigatorAPI,
ConnectMessage,
JSONPatchItem,
BaseAppSDK,
KnownAppSDK,
} from './types'
import { Channel } from './channel'
import { createAdapter } from './cmaAdapter'
import { createCMAClient } from './cma'
import { KeyValueMap } from 'contentful-management/types'
const DEFAULT_API_PRODUCERS = [
makeSharedAPI,
makeEntryAPI,
makeFieldAPI,
makeEditorAPI,
makeWindowAPI,
]
type ProducerFunc = (
channel: Channel,
data: ConnectMessage,
currentGlobal: typeof globalThis,
) => any
const LOCATION_TO_API_PRODUCERS: { [location: string]: ProducerFunc[] } = {
[locations.LOCATION_ENTRY_FIELD]: DEFAULT_API_PRODUCERS,
[locations.LOCATION_ENTRY_FIELD_SIDEBAR]: DEFAULT_API_PRODUCERS,
[locations.LOCATION_ENTRY_SIDEBAR]: [makeSharedAPI, makeEntryAPI, makeEditorAPI, makeWindowAPI],
[locations.LOCATION_ENTRY_EDITOR]: [makeSharedAPI, makeEntryAPI, makeEditorAPI],
[locations.LOCATION_DIALOG]: [makeSharedAPI, makeDialogAPI, makeWindowAPI],
[locations.LOCATION_PAGE]: [makeSharedAPI],
[locations.LOCATION_HOME]: [makeSharedAPI],
[locations.LOCATION_APP_CONFIG]: [makeSharedAPI, makeAppAPI],
}
export default function createAPI(
channel: Channel,
data: ConnectMessage,
currentGlobal: typeof globalThis,
): KnownAppSDK {
const producers = LOCATION_TO_API_PRODUCERS[data.location as string] || DEFAULT_API_PRODUCERS
return producers.reduce((api, produce) => {
return { ...api, ...produce(channel, data, currentGlobal) }
}, {}) as any
}
function makeSharedAPI(
channel: Channel,
data: ConnectMessage,
): BaseAppSDK<KeyValueMap, KeyValueMap, never> {
const { user, parameters, locales, ids, initialContentTypes, hostnames } = data
const currentLocation = data.location || locations.LOCATION_ENTRY_FIELD
return {
cma: createCMAClient(ids, channel),
cmaAdapter: createAdapter(channel),
location: {
is: (tested: string) => currentLocation === tested,
},
user,
parameters,
locales: {
available: locales.available,
default: locales.default,
names: locales.names,
fallbacks: locales.fallbacks,
optional: locales.optional,
direction: locales.direction,
},
space: createSpace(channel, initialContentTypes),
dialogs: createDialogs(channel, ids),
// Typecast because promises returned by navigator methods aren't typed
navigator: createNavigator(channel, ids) as NavigatorAPI,
notifier: {
success: (message: string) => channel.send('notify', { type: 'success', message }),
error: (message: string) => channel.send('notify', { type: 'error', message }),
warning: (message: string) => channel.send('notify', { type: 'warning', message }),
},
ids,
hostnames,
access: {
can: (action: string, entity: any, patch?: JSONPatchItem[]) =>
channel.call('checkAccess', action, entity, patch) as Promise<boolean>,
canEditAppConfig: () => channel.call('checkAppConfigAccess') as Promise<boolean>,
},
}
}
function makeWindowAPI(channel: Channel, _data: any, currentGlobal: typeof globalThis) {
return {
window: createWindow(currentGlobal, channel),
}
}
function makeEditorAPI(channel: Channel, data: any) {
const { editorInterface, editor } = data
return {
editor: createEditor(channel, editorInterface, editor),
}
}
function makeEntryAPI(
channel: Channel,
{ locales, contentType, entry, fieldInfo }: ConnectMessage,
) {
const createEntryField = (info: EntryFieldInfo) => new Field(channel, info, locales.default)
return {
contentType,
entry: createEntry(channel, entry, fieldInfo, createEntryField),
}
}
function makeFieldAPI(channel: Channel, { field }: ConnectMessage) {
if (!field) {
throw new Error('FieldAPI called for location without "field" property defined.')
}
return {
field: new FieldLocale(channel, field),
}
}
function makeDialogAPI(channel: Channel) {
return {
close: (data: any) => channel.send('closeDialog', data),
}
}
function makeAppAPI(channel: Channel) {
const app = createApp(channel)
return {
app,
}
}