forked from coda/packs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.ts
405 lines (385 loc) · 13.9 KB
/
builder.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
import type {Authentication} from './types';
import type {AuthenticationDef} from './types';
import {AuthenticationType} from './types';
import type {BasicPackDefinition} from './types';
import {ConnectionRequirement} from './api_types';
import type {DynamicSyncTableOptions} from './api';
import type {Format} from './types';
import type {Formula} from './api';
import type {FormulaDefinition} from './api';
import type {ObjectSchema} from './schema';
import type {ObjectSchemaDefinition} from './schema';
import type {PackVersionDefinition} from './types';
import type {ParamDefs} from './api_types';
import type {Schema} from './schema';
import type {SyncTable} from './api';
import type {SyncTableOptions} from './api';
import type {SystemAuthentication} from './types';
import type {SystemAuthenticationDef} from './types';
import type {ValueType} from './schema';
import {isDynamicSyncTable} from './api';
import {makeDynamicSyncTable} from './api';
import {makeFormula} from './api';
import {makeSyncTable} from './api';
import {maybeRewriteConnectionForFormula} from './api';
import {setEndpointDefHelper} from './helpers/migration';
import {wrapMetadataFunction} from './api';
/**
* Creates a new skeleton pack definition that can be added to.
*
* @example
* ```
* export const pack = newPack();
* pack.addFormula({resultType: ValueType.String, name: 'MyFormula', ...});
* pack.addSyncTable('MyTable', ...);
* pack.setUserAuthentication({type: AuthenticationType.HeaderBearerToken});
* ```
*/
export function newPack(definition?: Partial<PackVersionDefinition>): PackDefinitionBuilder {
return new PackDefinitionBuilder(definition);
}
/**
* A class that assists in constructing a pack definition. Use {@link newPack} to create one.
*/
export class PackDefinitionBuilder implements BasicPackDefinition {
/**
* See {@link PackVersionDefinition.formulas}.
*/
formulas: Formula[];
/**
* See {@link PackVersionDefinition.formats}.
*/
formats: Format[];
/**
* See {@link PackVersionDefinition.syncTables}.
*/
syncTables: SyncTable[];
/**
* See {@link PackVersionDefinition.networkDomains}.
*/
networkDomains: string[];
/**
* See {@link PackVersionDefinition.defaultAuthentication}.
*/
defaultAuthentication?: Authentication;
/**
* See {@link PackVersionDefinition.systemConnectionAuthentication}.
*/
systemConnectionAuthentication?: SystemAuthentication;
/**
* See {@link PackVersionDefinition.version}.
*/
version?: string;
/** @deprecated */
formulaNamespace?: string;
private _defaultConnectionRequirement: ConnectionRequirement | undefined;
/**
* Constructs a {@link PackDefinitionBuilder}. However, `coda.newPack()` should be used instead
* rather than constructing a builder directly.
*/
constructor(definition?: Partial<PackVersionDefinition>) {
const {
formulas,
formats,
syncTables,
networkDomains,
defaultAuthentication,
systemConnectionAuthentication,
version,
formulaNamespace,
} = definition || {};
this.formulas = formulas || [];
this.formats = formats || [];
this.syncTables = syncTables || [];
this.networkDomains = networkDomains || [];
this.defaultAuthentication = defaultAuthentication;
this.systemConnectionAuthentication = systemConnectionAuthentication;
this.version = version;
this.formulaNamespace = formulaNamespace || 'Deprecated';
}
/**
* Adds a formula definition to this pack.
*
* In the web editor, the `/Formula` shortcut will insert a snippet of a skeleton formula.
*
* @example
* ```
* pack.addFormula({
* resultType: ValueType.String,
* name: 'MyFormula',
* description: 'My description.',
* parameters: [
* makeParameter({
* type: ParameterType.String,
* name: 'myParam',
* description: 'My param description.',
* }),
* ],
* execute: async ([param]) => {
* return `Hello ${param}`;
* },
* });
* ```
*/
addFormula<ParamDefsT extends ParamDefs, ResultT extends ValueType, SchemaT extends Schema>(
definition: {resultType: ResultT} & FormulaDefinition<ParamDefsT, ResultT, SchemaT>,
): this {
const formula = makeFormula<ParamDefsT, ResultT, SchemaT>({
...definition,
connectionRequirement: definition.connectionRequirement || this._defaultConnectionRequirement,
});
this.formulas.push(formula as any); // WTF
return this;
}
/**
* Adds a sync table definition to this pack.
*
* In the web editor, the `/SyncTable` shortcut will insert a snippet of a skeleton sync table.
*
* @example
* ```
* pack.addSyncTable({
* name: 'MySyncTable',
* identityName: 'EntityName',
* schema: coda.makeObjectSchema({
* ...
* }),
* formula: {
* ...
* },
* });
* ```
*/
addSyncTable<K extends string, L extends string, ParamDefsT extends ParamDefs, SchemaT extends ObjectSchema<K, L>>({
name,
description,
identityName,
schema,
formula,
connectionRequirement,
dynamicOptions = {},
}: SyncTableOptions<K, L, ParamDefsT, SchemaT>): this {
const connectionRequirementToUse = connectionRequirement || this._defaultConnectionRequirement;
const syncTable = makeSyncTable({
name,
description,
identityName,
schema,
formula,
connectionRequirement: connectionRequirementToUse,
dynamicOptions,
});
this.syncTables.push(syncTable);
return this;
}
/**
* Adds a dynamic sync table definition to this pack.
*
* In the web editor, the `/DynamicSyncTable` shortcut will insert a snippet of a skeleton sync table.
*
* @example
* ```
* pack.addDynamicSyncTable({
* name: "MySyncTable",
* getName: async funciton (context) => {
* const response = await context.fetcher.fetch({method: "GET", url: context.sync.dynamicUrl});
* return response.body.name;
* },
* getName: async function (context) => {
* const response = await context.fetcher.fetch({method: "GET", url: context.sync.dynamicUrl});
* return response.body.browserLink;
* },
* ...
* });
* ```
*/
addDynamicSyncTable<
K extends string,
L extends string,
ParamDefsT extends ParamDefs,
SchemaT extends ObjectSchemaDefinition<K, L>,
>(definition: DynamicSyncTableOptions<K, L, ParamDefsT, SchemaT>): this {
const dynamicSyncTable = makeDynamicSyncTable({
...definition,
connectionRequirement: definition.connectionRequirement || this._defaultConnectionRequirement,
});
this.syncTables.push(dynamicSyncTable);
return this;
}
/**
* Adds a column format definition to this pack.
*
* In the web editor, the `/ColumnFormat` shortcut will insert a snippet of a skeleton format.
*
* @example
* ```
* pack.addColumnFormat({
* name: 'MyColumn',
* formulaName: 'MyFormula',
* });
* ```
*/
addColumnFormat(format: Format): this {
this.formats.push(format);
return this;
}
/**
* Sets this pack to use authentication for individual users, using the
* authentication method is the given definition.
*
* Each user will need to register an account in order to use this pack.
*
* In the web editor, the `/UserAuthentication` shortcut will insert a snippet of a skeleton
* authentication definition.
*
* By default, this will set a default connection (account) requirement, making a user account
* required to invoke all formulas in this pack unless you specify differently on a particular
* formula. To change the default, you can pass a `defaultConnectionRequirement` option into
* this method.
*
* @example
* ```
* pack.setUserAuthentication({
* type: AuthenticationType.HeaderBearerToken,
* });
* ```
*/
setUserAuthentication(authDef: AuthenticationDef & {defaultConnectionRequirement?: ConnectionRequirement}): this {
const {defaultConnectionRequirement = ConnectionRequirement.Required, ...authentication} = authDef;
if (authentication.type === AuthenticationType.None || authentication.type === AuthenticationType.Various) {
this.defaultAuthentication = authentication;
} else {
const {
getConnectionName: getConnectionNameDef,
getConnectionUserId: getConnectionUserIdDef,
postSetup: postSetupDef,
...rest
} = authentication;
const getConnectionName = wrapMetadataFunction(getConnectionNameDef);
const getConnectionUserId = wrapMetadataFunction(getConnectionUserIdDef);
const postSetup = postSetupDef?.map(step => {
return {...step, getOptions: wrapMetadataFunction(setEndpointDefHelper(step).getOptions)};
});
this.defaultAuthentication = {...rest, getConnectionName, getConnectionUserId, postSetup} as Authentication;
}
if (authentication.type !== AuthenticationType.None) {
this._setDefaultConnectionRequirement(defaultConnectionRequirement);
}
return this;
}
/**
* Sets this pack to use authentication provided by you as the maker of this pack.
*
* You will need to register credentials to use with this pack. When users use the
* pack, their requests will be authenticated with those system credentials, they need
* not register their own account.
*
* In the web editor, the `/SystemAuthentication` shortcut will insert a snippet of a skeleton
* authentication definition.
*
* @example
* ```
* pack.setSystemAuthentication({
* type: AuthenticationType.HeaderBearerToken,
* });
* ```
*/
setSystemAuthentication(systemAuthentication: SystemAuthenticationDef): this {
const {
getConnectionName: getConnectionNameDef,
getConnectionUserId: getConnectionUserIdDef,
postSetup: postSetupDef,
...rest
} = systemAuthentication;
const getConnectionName = wrapMetadataFunction(getConnectionNameDef);
const getConnectionUserId = wrapMetadataFunction(getConnectionUserIdDef);
const postSetup = postSetupDef?.map(step => {
return {...step, getOptions: wrapMetadataFunction(setEndpointDefHelper(step).getOptions)};
});
this.systemConnectionAuthentication = {
...rest,
getConnectionName,
getConnectionUserId,
postSetup,
} as SystemAuthentication;
return this;
}
/**
* Adds the domain that this pack makes HTTP requests to.
* For example, if your pack makes HTTP requests to "api.example.com",
* use "example.com" as your network domain.
*
* If your pack make HTTP requests, it must declare a network domain,
* for security purposes. Coda enforces that your pack cannot make requests to
* any undeclared domains.
*
* You are allowed one network domain per pack by default. If your pack needs
* to connect to multiple domains, contact Coda Support for approval.
*
* @example
* ```
* pack.addNetworkDomain('example.com');
* ```
*/
addNetworkDomain(...domain: string[]): this {
this.networkDomains.push(...domain);
return this;
}
/**
* Sets the semantic version of this pack version, e.g. `'1.2.3'`.
*
* This is optional, and you only need to provide a version if you are manually doing
* semantic versioning, or using the CLI. If using the web editor, you can omit this
* and the web editor will automatically provide an appropriate semantic version
* each time you build a version.
*
* @example
* ```
* pack.setVersion('1.2.3');
* ```
*/
setVersion(version: string): this {
this.version = version;
return this;
}
private _setDefaultConnectionRequirement(connectionRequirement: ConnectionRequirement): this {
this._defaultConnectionRequirement = connectionRequirement;
// Rewrite any formulas or sync tables that were already defined, in case the maker sets the default
// after the fact.
this.formulas = this.formulas.map(formula => {
return formula.connectionRequirement ? formula : maybeRewriteConnectionForFormula(formula, connectionRequirement);
});
this.syncTables = this.syncTables.map(syncTable => {
if (syncTable.getter.connectionRequirement) {
return syncTable;
} else if (isDynamicSyncTable(syncTable)) {
return {
...syncTable,
getter: maybeRewriteConnectionForFormula(syncTable.getter, connectionRequirement),
// These 4 are metadata formulas, so they use ConnectionRequirement.Required
// by default if you don't specify a connection requirement (a legacy behavior
// that is confusing and perhaps undesirable now that we have better builders).
// We don't know if the maker set Required explicitly or if was just the default,
// so we don't know if we should overwrite the connection requirement. For lack
// of a better option, we'll override it here regardless. This ensure that these
// dynamic sync table metadata formulas have the same connetion requirement as the
// sync table itself, which seems desirable basically 100% of the time and should
// always work, but it does give rise to confusing behavior that calling
// setDefaultConnectionRequirement() can wipe away an explicit connection
// requirement override set on one of these 4 metadata formulas.
getName: maybeRewriteConnectionForFormula(syncTable.getName, connectionRequirement),
getDisplayUrl: maybeRewriteConnectionForFormula(syncTable.getDisplayUrl, connectionRequirement),
getSchema: maybeRewriteConnectionForFormula(syncTable.getSchema, connectionRequirement),
listDynamicUrls: maybeRewriteConnectionForFormula(syncTable.listDynamicUrls, connectionRequirement),
};
} else {
return {
...syncTable,
getter: maybeRewriteConnectionForFormula(syncTable.getter, connectionRequirement),
getSchema: maybeRewriteConnectionForFormula(syncTable.getSchema, connectionRequirement),
};
}
});
return this;
}
}