-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-routes.ts
557 lines (489 loc) · 15.1 KB
/
generate-routes.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
import { readFile, writeFile } from 'node:fs/promises'
import { dirname, posix, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { openapi } from '@seamapi/types/connect'
import { camelCase, kebabCase, pascalCase, snakeCase } from 'change-case'
import { deleteAsync } from 'del'
import { ESLint } from 'eslint'
import { format, resolveConfig } from 'prettier'
const rootPathParts = ['src', 'lib', 'seam', 'connect']
const routeOutputPathParts = ['routes']
const rootPath = resolve(
dirname(fileURLToPath(import.meta.url)),
...rootPathParts,
)
const rootClassPath = resolve(rootPath, 'seam-http.ts')
const routeOutputPath = resolve(rootPath, ...routeOutputPathParts)
async function main(): Promise<void> {
const routes = createRoutes()
const routeNames = await Promise.all(routes.map(writeRoute))
const routeIndexName = await writeRoutesIndex(routes)
const outputPathParts = [...rootPathParts, ...routeOutputPathParts]
await deleteAsync([
posix.join(...outputPathParts, '*'),
`!${posix.join(...outputPathParts, routeIndexName)}`,
...routeNames.map((name) => `!${posix.join(...outputPathParts, name)}`),
])
}
const openapiResponseKeyProp = 'x-fern-sdk-return-value'
const routePaths = [
'/access_codes',
'/access_codes/simulate',
'/access_codes/unmanaged',
'/acs',
'/acs/access_groups',
'/acs/credential_pools',
'/acs/credential_provisioning_automations',
'/acs/credentials',
'/acs/entrances',
'/acs/systems',
'/acs/users',
'/acs/users/unmanaged',
'/acs/access_groups/unmanaged',
'/acs/credentials/unmanaged',
'/acs/encoders',
'/acs/encoders/simulate',
'/action_attempts',
'/client_sessions',
'/connect_webviews',
'/connected_accounts',
'/devices',
'/devices/unmanaged',
'/devices/simulate',
'/events',
'/locks',
'/networks',
'/noise_sensors',
'/noise_sensors/noise_thresholds',
'/noise_sensors/simulate',
'/phones',
'/phones/simulate',
'/thermostats',
'/thermostats/schedules',
'/user_identities',
'/user_identities/enrollment_automations',
'/webhooks',
'/workspaces',
] as const
const routePathSubresources: Partial<
Record<(typeof routePaths)[number], string[]>
> = {
'/access_codes': ['unmanaged', 'simulate'],
'/acs': [
'access_groups',
'credential_pools',
'credential_provisioning_automations',
'credentials',
'entrances',
'systems',
'users',
],
'/acs/users': ['unmanaged'],
'/acs/access_groups': ['unmanaged'],
'/acs/credentials': ['unmanaged'],
'/acs/encoders': ['simulate'],
'/phones': ['simulate'],
'/devices': ['unmanaged', 'simulate'],
'/noise_sensors': ['noise_thresholds', 'simulate'],
'/thermostats': ['schedules'],
'/user_identities': ['enrollment_automations'],
}
interface Route {
namespace: string
endpoints: Endpoint[]
subresources: string[]
}
interface Endpoint {
name: string
path: string
namespace: string
resource: string | null
method: Method
requestFormat: 'params' | 'body'
isRequestParamOptional: boolean
}
type Method = 'GET' | 'POST'
interface ClassMeta {
constructors: string
}
const createRoutes = (): Route[] => {
const paths = Object.keys(openapi.paths)
const unmatchedEndpointPaths = paths.filter(
(path) =>
!routePaths.some((routePath) => isEndpointUnderRoute(path, routePath)),
)
if (unmatchedEndpointPaths.length > 0) {
throw new Error(
`The following endpoints will not be generated (add them to routePaths in generate-routes.ts):\n\n${unmatchedEndpointPaths.join(
'\n',
)}\n`,
)
}
return routePaths.map(createRoute)
}
const createRoute = (routePath: (typeof routePaths)[number]): Route => {
const endpointPaths = Object.keys(openapi.paths).filter((path) =>
isEndpointUnderRoute(path, routePath),
)
const namespace = routePath.split('/').join('_').slice(1)
return {
namespace,
subresources: routePathSubresources[routePath] ?? [],
endpoints: endpointPaths.map((endpointPath) =>
createEndpoint(namespace, routePath, endpointPath),
),
}
}
const createEndpoint = (
namespace: string,
routePath: string,
endpointPath: string,
): Endpoint => {
if (!isOpenapiPath(endpointPath)) {
throw new Error(`Did not find ${endpointPath} in OpenAPI spec`)
}
const spec = openapi.paths[endpointPath]
const method = deriveSemanticMethod(Object.keys(spec))
const name = endpointPath.split(routePath)[1]?.slice(1)
if (name == null) {
throw new Error(`Could not parse name from ${endpointPath}`)
}
return {
name,
namespace,
path: endpointPath,
method,
resource: deriveResource(endpointPath, method),
requestFormat: ['GET', 'DELETE'].includes(method) ? 'params' : 'body',
// UPSTREAM: This could be derived from the OpenAPI spec, however some endpoints require at least one param,
// and in the spec this currently looks as if params are optional.
isRequestParamOptional: true,
}
}
const deriveResource = (
endpointPath: string,
method: Method,
): string | null => {
if (isOpenapiPath(endpointPath)) {
const spec = openapi.paths[endpointPath]
const methodKey = method.toLowerCase()
if (methodKey === 'post' && 'post' in spec) {
const postSpec = spec.post
const openapiEndpointResource =
openapiResponseKeyProp in postSpec
? postSpec[openapiResponseKeyProp]
: null
return openapiEndpointResource
}
if (methodKey === 'get' && 'get' in spec) {
const response = spec.get.responses[200]
if (!('content' in response)) {
throw new Error(`Missing resource for ${method} ${endpointPath}`)
}
return deriveResourceFromSchemaForGetRequest(
response.content['application/json']?.schema?.properties ?? {},
)
}
}
throw new Error(`Could not derive resource for ${method} ${endpointPath}`)
}
const deriveResourceFromSchemaForGetRequest = (
properties: object,
): string | null =>
Object.keys(properties).filter((key) => key !== 'ok')[0] ?? null
const deriveSemanticMethod = (methods: string[]): Method => {
// UPSTREAM: This should return GET before POST.
// Blocked on https://github.com/seamapi/nextlove/issues/117
// and https://github.com/seamapi/javascript-http/issues/43
if (methods.includes('post')) return 'POST'
if (methods.includes('get')) return 'GET'
throw new Error(`Could not find valid method in ${methods.join(', ')}`)
}
const isOpenapiPath = (key: string): key is keyof typeof openapi.paths =>
key in openapi.paths
const isEndpointUnderRoute = (
endpointPath: string,
routePath: string,
): boolean =>
endpointPath.startsWith(routePath) &&
endpointPath.split('/').length - 1 === routePath.split('/').length
const renderRoute = (route: Route, { constructors }: ClassMeta): string => `
/*
* Automatically generated by generate-routes.ts.
* Do not edit this file or add other files to this directory.
*/
${renderImports(route)}
${renderClass(route, { constructors })}
${renderExports(route)}
`
const renderImports = ({ namespace, subresources }: Route): string =>
`
import type {
RouteRequestBody,
RouteRequestParams,
RouteResponse,
} from '@seamapi/types/connect'
import type { SetNonNullable } from 'lib/types.js'
import {
getAuthHeadersForClientSessionToken,
warnOnInsecureuserIdentifierKey,
} from 'lib/seam/connect/auth.js'
import {
type Client,
type ClientOptions,
createClient,
} from 'lib/seam/connect/client.js'
import {
isSeamHttpOptionsWithApiKey,
isSeamHttpOptionsWithClient,
isSeamHttpOptionsWithClientSessionToken,
isSeamHttpOptionsWithConsoleSessionToken,
isSeamHttpOptionsWithPersonalAccessToken,
type SeamHttpFromPublishableKeyOptions,
SeamHttpInvalidOptionsError,
type SeamHttpOptions,
type SeamHttpOptionsWithApiKey,
type SeamHttpOptionsWithClient,
type SeamHttpOptionsWithClientSessionToken,
type SeamHttpOptionsWithConsoleSessionToken,
type SeamHttpOptionsWithPersonalAccessToken,
type SeamHttpRequestOptions,
} from 'lib/seam/connect/options.js'
import {
limitToSeamHttpRequestOptions,
parseOptions
} from 'lib/seam/connect/parse-options.js'
import {
resolveActionAttempt,
} from 'lib/seam/connect/resolve-action-attempt.js'
import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js'
${
namespace === 'client_sessions'
? ''
: "import { SeamHttpClientSessions } from './client-sessions.js'"
}
${
namespace === 'action_attempts'
? ''
: "import { SeamHttpActionAttempts } from './action-attempts.js'"
}
${subresources
.map((subresource) => renderSubresourceImport(subresource, namespace))
.join('\n')}
`
const renderSubresourceImport = (
subresource: string,
namespace: string,
): string => `
import {
SeamHttp${pascalCase(namespace)}${pascalCase(subresource)}
} from './${kebabCase(namespace)}-${kebabCase(subresource)}.js'
`
const renderClass = (
{ namespace, endpoints, subresources }: Route,
{ constructors }: ClassMeta,
): string =>
`
export class SeamHttp${pascalCase(namespace)} {
client: Client
readonly defaults: Required<SeamHttpRequestOptions>
${constructors
.replaceAll(': SeamHttp ', `: SeamHttp${pascalCase(namespace)} `)
.replaceAll('<SeamHttp>', `<SeamHttp${pascalCase(namespace)}>`)
.replaceAll(
'SeamHttp.fromClientSessionToken',
`SeamHttp${pascalCase(namespace)}.fromClientSessionToken`,
)
.replaceAll('new SeamHttp(', `new SeamHttp${pascalCase(namespace)}(`)}
${subresources
.map((subresource) => renderSubresourceMethod(subresource, namespace))
.join('\n')}
${endpoints.map(renderClassMethod).join('\n')}
}
`
const renderClassMethod = ({
name,
requestFormat,
method,
namespace,
resource,
path,
isRequestParamOptional,
}: Endpoint): string => `
${camelCase(name)}(
${requestFormat}${isRequestParamOptional ? '?' : ''}: ${renderRequestType({
name,
namespace,
})},
${renderClassMethodOptions({ resource })}
): SeamHttpRequest<${
resource === null
? 'void, undefined'
: `${renderResponseType({ name, namespace })}, '${resource}'`
}> {
return new SeamHttpRequest(this, {
path: '${path}',
method: '${snakeCase(method)}', ${
requestFormat === 'params' ? 'params,' : ''
} ${requestFormat === 'body' ? 'body,' : ''}
responseKey: ${resource === null ? 'undefined' : `'${resource}'`},
${resource === 'action_attempt' ? 'options' : ''}
})
}
`
const renderClassMethodOptions = ({
resource,
}: Pick<Endpoint, 'resource'>): string => {
if (resource === 'action_attempt') {
return `options: ${renderClassMethodOptionsTypeDef({
resource,
})} = {},`
}
return ''
}
const renderClassMethodOptionsType = ({
name,
namespace,
}: Pick<Endpoint, 'name' | 'namespace'>): string =>
[pascalCase(namespace), pascalCase(name), 'Options'].join('')
const renderClassMethodOptionsTypeDef = ({
resource,
}: Pick<Endpoint, 'resource'>): string => {
if (resource === 'action_attempt') {
return "Pick<SeamHttpRequestOptions, 'waitForActionAttempt'>"
}
return 'never'
}
const renderSubresourceMethod = (
subresource: string,
namespace: string,
): string => `
get ${camelCase(subresource)} (): SeamHttp${pascalCase(
namespace,
)}${pascalCase(subresource)} {
return SeamHttp${pascalCase(namespace)}${pascalCase(
subresource,
)}.fromClient(this.client, this.defaults)
}
`
const renderExports = (route: Route): string =>
route.endpoints.map(renderEndpointExports).join('\n')
const renderEndpointExports = ({
name,
path,
namespace,
resource,
requestFormat,
}: Endpoint): string => `
export type ${renderRequestType({
name,
namespace,
})} = RouteRequest${pascalCase(requestFormat)}<'${path}'>
export type ${renderResponseType({ name, namespace })}= SetNonNullable<
Required<RouteResponse<'${path}'>>
>
export type ${renderClassMethodOptionsType({
name,
namespace,
})} = ${renderClassMethodOptionsTypeDef({ resource })}
`
const renderRequestType = ({
name,
namespace,
}: Pick<Endpoint, 'name' | 'namespace'>): string =>
[
pascalCase(namespace),
pascalCase(name),
pascalCase(requestFormatToRequestType(name, namespace)),
].join('')
// UPSTREAM: This function is a workaround, as the request type should always match the request format.
// Blocked on https://github.com/seamapi/nextlove/issues/117
// and https://github.com/seamapi/javascript-http/issues/43
const requestFormatToRequestType = (
name: string,
namespace: string,
): 'params' | 'body' => {
if (namespace.includes('simulate')) return 'body'
if (['get', 'list', 'view'].includes(name)) return 'params'
if (['delete'].includes(name)) return 'params'
if (name.includes('revoke')) return 'params'
if (name.includes('remove')) return 'params'
if (name.includes('deactivate')) return 'params'
if (name.startsWith('list')) return 'params'
return 'body'
}
const renderResponseType = ({
name,
namespace,
}: Pick<Endpoint, 'name' | 'namespace'>): string =>
[pascalCase(namespace), pascalCase(name), 'Response'].join('')
const getClassConstructors = (data: string): string => {
const lines = data.split('\n')
const startIdx = lines.findIndex((line) =>
line.trim().startsWith('constructor('),
)
if (startIdx === -1) {
throw new Error('Could not find start of class constructors')
}
const endIdx = lines.findIndex((line) => line.trim().startsWith('get '))
if (endIdx === -1) {
throw new Error('Could not find end of class constructors')
}
return lines.slice(startIdx, endIdx).join('\n')
}
const writeRoute = async (route: Route): Promise<string> => {
const rootClass = await readFile(rootClassPath)
const constructors = getClassConstructors(rootClass.toString())
const name = `${kebabCase(route.namespace)}.ts`
await write(renderRoute(route, { constructors }), routeOutputPath, name)
return name
}
const writeRoutesIndex = async (routes: Route[]): Promise<string> => {
const exports = routes.map(
(route) => `export * from './${kebabCase(route.namespace)}.js'`,
)
const name = 'index.ts'
await write(exports.join('\n'), routeOutputPath, name)
return name
}
const write = async (data: string, ...path: string[]): Promise<void> => {
const filePath = resolve(...path)
await writeFile(
filePath,
'// Generated empty file to allow ESLint parsing by filename',
)
const fixedOutput = await eslintFixOutput(data, filePath)
const prettyOutput = await prettierOutput(fixedOutput, filePath)
await writeFile(filePath, prettyOutput)
}
const prettierOutput = async (
data: string,
filepath: string,
): Promise<string> => {
const config = await resolveConfig(filepath)
if (config == null) {
throw new Error('Failed to resolve Prettier config')
}
return await format(data, { ...config, filepath })
}
const eslintFixOutput = async (
data: string,
filePath: string,
): Promise<string> => {
const eslint = new ESLint({ fix: true })
const [linted] = await eslint.lintText(data, { filePath })
if (linted == null) {
throw new Error('ESLint returned empty results')
}
if (linted.fatalErrorCount > 0) {
throw new Error(
`ESLint returned fatal errors:\n${JSON.stringify(
linted.messages,
null,
2,
)}`,
)
}
return linted.output ?? linted.source ?? data
}
await main()