-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathapi.ts
More file actions
176 lines (159 loc) · 4.96 KB
/
api.ts
File metadata and controls
176 lines (159 loc) · 4.96 KB
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
import { makeField } from './field'
import { makeFieldLocale } from './field-locale'
import createWindow from './window'
import createEntry from './entry'
import createAsset from './asset'
import createSpace from './space'
import createDialogs from './dialogs'
import createEditor from './editor'
import createNavigator from './navigator'
import createApp from './app'
import createAgent from './agent'
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'
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_ASSET_SIDEBAR]: [makeSharedAPI, makeAssetAPI, 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],
[locations.LOCATION_AGENT]: [makeSharedAPI, makeAgentAPI, makeWindowAPI],
}
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,
release,
uiLanguageLocale,
} = 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, release) 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,
release,
uiLanguageLocale,
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) => makeField(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: makeFieldLocale(channel, field),
}
}
function makeDialogAPI(channel: Channel) {
return {
close: (data: any) => channel.send('closeDialog', data),
}
}
function makeAssetAPI(channel: Channel, { asset }: ConnectMessage) {
return {
asset: createAsset(channel, asset),
}
}
function makeAppAPI(channel: Channel) {
const app = createApp(channel)
return {
app,
}
}
function makeAgentAPI(channel: Channel, { agent }: ConnectMessage) {
return {
agent: createAgent(channel, agent),
}
}