@dbos-inc/dbos-sdk 0.8.32-preview → 0.8.35-preview

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. package/dist/src/cloud-cli/cli.js +2 -2
  2. package/dist/src/cloud-cli/cli.js.map +1 -1
  3. package/dist/src/cloud-cli/userdb.d.ts +1 -1
  4. package/dist/src/cloud-cli/userdb.d.ts.map +1 -1
  5. package/dist/src/cloud-cli/userdb.js +82 -2
  6. package/dist/src/cloud-cli/userdb.js.map +1 -1
  7. package/dist/src/system_database.d.ts +3 -0
  8. package/dist/src/system_database.d.ts.map +1 -1
  9. package/dist/src/telemetry/logs.js +5 -1
  10. package/dist/src/telemetry/logs.js.map +1 -1
  11. package/dist/src/telemetry/traces.d.ts +2 -0
  12. package/dist/src/telemetry/traces.d.ts.map +1 -1
  13. package/dist/src/telemetry/traces.js +8 -0
  14. package/dist/src/telemetry/traces.js.map +1 -1
  15. package/package.json +1 -1
  16. package/dist/src/cloud-cli/applications/configure.d.ts +0 -2
  17. package/dist/src/cloud-cli/applications/configure.d.ts.map +0 -1
  18. package/dist/src/cloud-cli/applications/configure.js +0 -64
  19. package/dist/src/cloud-cli/applications/configure.js.map +0 -1
  20. package/dist/src/dbos-runtime/TypeParser.d.ts +0 -33
  21. package/dist/src/dbos-runtime/TypeParser.d.ts.map +0 -1
  22. package/dist/src/dbos-runtime/TypeParser.js +0 -104
  23. package/dist/src/dbos-runtime/TypeParser.js.map +0 -1
  24. package/dist/src/dbos-runtime/openApi.d.ts +0 -35
  25. package/dist/src/dbos-runtime/openApi.d.ts.map +0 -1
  26. package/dist/src/dbos-runtime/openApi.js +0 -595
  27. package/dist/src/dbos-runtime/openApi.js.map +0 -1
  28. package/dist/src/dbos-runtime/tsDiagUtil.d.ts +0 -16
  29. package/dist/src/dbos-runtime/tsDiagUtil.d.ts.map +0 -1
  30. package/dist/src/dbos-runtime/tsDiagUtil.js +0 -72
  31. package/dist/src/dbos-runtime/tsDiagUtil.js.map +0 -1
  32. package/dist/src/provenance/provenance_daemon.d.ts +0 -34
  33. package/dist/src/provenance/provenance_daemon.d.ts.map +0 -1
  34. package/dist/src/provenance/provenance_daemon.js +0 -86
  35. package/dist/src/provenance/provenance_daemon.js.map +0 -1
  36. package/dist/src/telemetry/signals.d.ts +0 -20
  37. package/dist/src/telemetry/signals.d.ts.map +0 -1
  38. package/dist/src/telemetry/signals.js +0 -11
  39. package/dist/src/telemetry/signals.js.map +0 -1
@@ -1,35 +0,0 @@
1
- import ts from 'typescript';
2
- import { DecoratorInfo, MethodInfo, ClassInfo, ParameterInfo } from './TypeParser';
3
- import { APITypes, ArgSources } from '../httpServer/handler';
4
- import { Schema } from 'ts-json-schema-generator';
5
- import { OpenAPIV3 as OpenApi3 } from 'openapi-types';
6
- interface HttpEndpointInfo {
7
- verb: APITypes;
8
- path: string;
9
- }
10
- export declare class OpenApiGenerator {
11
- #private;
12
- readonly program: ts.Program;
13
- get diags(): readonly ts.Diagnostic[];
14
- constructor(program: ts.Program);
15
- generate(classes: readonly ClassInfo[], title: string, version: string): OpenApi3.Document | undefined;
16
- generatePath(method: MethodInfo, { verb, path }: HttpEndpointInfo, defaultRoles: readonly string[], securityScheme: string | undefined): [string, OpenApi3.PathItemObject] | undefined;
17
- getMethodReturnType(node: ts.MethodDeclaration): ts.Type | undefined;
18
- generateResponse(method: MethodInfo): [string, OpenApi3.ResponseObject] | undefined;
19
- generateParameters(sourcedParams: [ParameterInfo, ArgSources][]): OpenApi3.ParameterObject[];
20
- generateParameter(parameter: ParameterInfo, argSource: ArgSources): OpenApi3.ParameterObject | undefined;
21
- getLocation(parameter: ParameterInfo, argSource: ArgSources): "query" | "path" | undefined;
22
- generateRequestBody(sourcedParams: [ParameterInfo, ArgSources][]): OpenApi3.RequestBodyObject | undefined;
23
- generateSchema(node?: ts.Node): OpenApi3.SchemaObject | OpenApi3.ReferenceObject | undefined;
24
- getDBOSDecorator(decorated: MethodInfo | ParameterInfo | ClassInfo, name: string): DecoratorInfo | undefined;
25
- getHttpInfo(method: MethodInfo): HttpEndpointInfo | undefined;
26
- getParamSource(parameter: ParameterInfo, verb: APITypes): ArgSources.BODY | ArgSources.QUERY | ArgSources.URL | undefined;
27
- getParamName(parameter: ParameterInfo): string | undefined;
28
- mapSchema(schema: Schema): OpenApi3.SchemaObject | OpenApi3.ReferenceObject | undefined;
29
- parseStringLiteralArray(node: ts.Expression | undefined): string[] | undefined;
30
- parseSecurityScheme(arg?: ts.Expression): SecurityScheme | undefined;
31
- }
32
- type SecurityScheme = Exclude<OpenApi3.SecuritySchemeObject, OpenApi3.OAuth2SecurityScheme>;
33
- export declare function generateOpenApi(entrypoint: string): Promise<OpenApi3.Document | undefined>;
34
- export {};
35
- //# sourceMappingURL=openApi.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"openApi.d.ts","sourceRoot":"","sources":["../../../src/dbos-runtime/openApi.ts"],"names":[],"mappings":"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"}
@@ -1,595 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.generateOpenApi = exports.OpenApiGenerator = void 0;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const TypeParser_1 = require("./TypeParser");
9
- const handler_1 = require("../httpServer/handler");
10
- const ts_json_schema_generator_1 = require("ts-json-schema-generator");
11
- const node_path_1 = __importDefault(require("node:path"));
12
- const promises_1 = __importDefault(require("node:fs/promises"));
13
- const tsDiagUtil_1 = require("./tsDiagUtil");
14
- function isValid(value) { return value !== undefined; }
15
- // ts-json-schema-generator does not support BigInt, so OpenApiGenerator needs a custom type, formatter and parser for it
16
- class BigIntType extends ts_json_schema_generator_1.PrimitiveType {
17
- getId() { return "integer"; }
18
- }
19
- class BigIntKeywordParser {
20
- supportsNode(node) {
21
- return node.kind === typescript_1.default.SyntaxKind.BigIntKeyword;
22
- }
23
- createType(_node, _context, _reference) {
24
- return new BigIntType();
25
- }
26
- }
27
- class BigIntTypeFormatter {
28
- supportsType(type) {
29
- return type instanceof BigIntType;
30
- }
31
- getDefinition(_type) {
32
- // Note, JSON Schema integer type is not constrained to a specific size, so it is a valid type for BigInt
33
- return { type: "integer", description: "bigint" };
34
- }
35
- getChildren(_type) {
36
- return [];
37
- }
38
- }
39
- // type guard functions for ts.Type/TypeNode
40
- function isNodeWithTypeArguments(node) {
41
- return "typeArguments" in node;
42
- }
43
- function isObjectType(node) {
44
- return !!(node.flags & typescript_1.default.TypeFlags.Object);
45
- }
46
- function isTypeReference(node) {
47
- return isObjectType(node) && !!(node.objectFlags & typescript_1.default.ObjectFlags.Reference);
48
- }
49
- // workflow UUID header parameter info which is added to every generated endpoint
50
- const workflowUuidParamName = "dbosWorkflowUUID";
51
- const workflowUuidRef = { $ref: `#/components/parameters/${workflowUuidParamName}` };
52
- const workflowUuidParam = [workflowUuidParamName, {
53
- name: 'dbos-workflowuuid',
54
- in: 'header',
55
- required: false,
56
- description: "Caller specified [workflow idempotency key](https://docs.dbos.dev/tutorials/idempotency-tutorial#setting-idempotency-keys)",
57
- schema: { type: 'string' },
58
- }];
59
- class OpenApiGenerator {
60
- program;
61
- #checker;
62
- #schemaGenerator;
63
- #schemaMap = new Map();
64
- #securitySchemeMap = new Map();
65
- #diags = new tsDiagUtil_1.DiagnosticsCollector();
66
- get diags() { return this.#diags.diags; }
67
- constructor(program) {
68
- this.program = program;
69
- this.#checker = program.getTypeChecker();
70
- const config = {
71
- discriminatorType: 'open-api',
72
- encodeRefs: false
73
- };
74
- const parser = (0, ts_json_schema_generator_1.createParser)(program, config, aug => aug.addNodeParser(new BigIntKeywordParser()));
75
- const formatter = (0, ts_json_schema_generator_1.createFormatter)(config, (fmt) => fmt.addTypeFormatter(new BigIntTypeFormatter()));
76
- this.#schemaGenerator = new ts_json_schema_generator_1.SchemaGenerator(program, parser, formatter, {});
77
- }
78
- generate(classes, title, version) {
79
- const paths = new Array();
80
- classes.forEach((cls, index) => {
81
- // if the class name is not specified, manufacture a name using the class index as the prefix
82
- // JS class identifiers cannot start with a digit but OpenApi security scheme names can
83
- const securitySchemeName = cls.name ? `${cls.name}Auth` : `${index}ClassAuth`;
84
- const securitySchemeDecorator = this.getDBOSDecorator(cls, 'OpenApiSecurityScheme');
85
- const securityScheme = this.parseSecurityScheme(securitySchemeDecorator?.args[0]);
86
- const defaultRoles = this.parseStringLiteralArray(this.getDBOSDecorator(cls, 'DefaultRequiredRole')?.args[0]) ?? [];
87
- if (securityScheme) {
88
- this.#securitySchemeMap.set(securitySchemeName, securityScheme);
89
- }
90
- for (const method of cls.methods) {
91
- const http = this.getHttpInfo(method);
92
- if (!http)
93
- continue;
94
- const path = this.generatePath(method, http, defaultRoles, securityScheme ? securitySchemeName : undefined);
95
- if (path)
96
- paths.push(path);
97
- }
98
- });
99
- const openApi = {
100
- openapi: "3.0.3",
101
- info: { title, version },
102
- paths: Object.fromEntries(paths),
103
- components: {
104
- parameters: Object.fromEntries([workflowUuidParam]),
105
- schemas: Object.fromEntries(this.#schemaMap),
106
- securitySchemes: Object.fromEntries(this.#securitySchemeMap)
107
- }
108
- };
109
- return (0, tsDiagUtil_1.diagResult)(openApi, this.diags);
110
- }
111
- generatePath(method, { verb, path }, defaultRoles, securityScheme) {
112
- const sourcedParams = method.parameters
113
- // The first parameter of a handle method must be an DBOSContext, which is not exposed via the API
114
- .slice(1)
115
- .map(p => [p, this.getParamSource(p, verb)]);
116
- const parameters = this.generateParameters(sourcedParams);
117
- // add optional parameter for workflow UUID header
118
- parameters.push(workflowUuidRef);
119
- const requestBody = this.generateRequestBody(sourcedParams);
120
- const response = this.generateResponse(method);
121
- if (!response)
122
- return;
123
- // if there is a security scheme specified, create a security requirement for it
124
- // unless the method has no required roles
125
- const security = new Array();
126
- if (securityScheme) {
127
- const roles = this.parseStringLiteralArray(this.getDBOSDecorator(method, 'RequiredRole')?.args[0]) ?? defaultRoles;
128
- if (roles.length > 0) {
129
- security.push({ [securityScheme]: [] });
130
- }
131
- }
132
- const operation = {
133
- operationId: method.name,
134
- responses: Object.fromEntries([response]),
135
- parameters,
136
- requestBody,
137
- security: security.length > 0 ? security : undefined,
138
- };
139
- // validate all path parameters have matching parameters with URL ArgSource
140
- const pathParams = path.split('/')
141
- .filter(p => p.startsWith(':'))
142
- .map(p => p.substring(1));
143
- for (const pathParam of pathParams) {
144
- const param = sourcedParams.find(([parameter, _]) => parameter.name === pathParam);
145
- if (!param) {
146
- this.#diags.raise(`Missing path parameter ${pathParam} for ${method.name}`, method.node);
147
- return;
148
- }
149
- if (param[1] !== handler_1.ArgSources.URL) {
150
- this.#diags.raise(`Path parameter ${pathParam} must be a URL parameter: ${method.name}`, param[0].node);
151
- return;
152
- }
153
- }
154
- // OpenAPI indicates path parameters with curly braces, but DBOS uses colons
155
- const $path = path.split('/')
156
- .map(p => p.startsWith(':') ? `{${p.substring(1)}}` : p)
157
- .join('/');
158
- switch (verb) {
159
- case handler_1.APITypes.GET: return [$path, { get: operation }];
160
- case handler_1.APITypes.POST: return [$path, { post: operation }];
161
- }
162
- }
163
- getMethodReturnType(node) {
164
- // if the method has a declared return type, try to use that
165
- if (node.type) {
166
- if (!isNodeWithTypeArguments(node.type)) {
167
- this.#diags.warn(`Unexpected type node kind ${typescript_1.default.SyntaxKind[node.type.kind]}`, node.type);
168
- }
169
- else {
170
- // return type must be a Promise<T>, so take the first type argument
171
- const typeArg = node.type.typeArguments?.[0];
172
- if (typeArg)
173
- return this.#checker.getTypeFromTypeNode(typeArg);
174
- const printer = typescript_1.default.createPrinter();
175
- const text = printer.printNode(typescript_1.default.EmitHint.Unspecified, node.type, node.getSourceFile());
176
- this.#diags.warn(`Unexpected return type without at least one type argument ${text}`, node.type);
177
- }
178
- }
179
- // if the return type is inferred or the Promise type argument could not be determined,
180
- // infer the return type via the type checker
181
- const signature = this.#checker.getSignatureFromDeclaration(node);
182
- const returnType = signature?.getReturnType();
183
- if (!returnType || !isTypeReference(returnType))
184
- return undefined;
185
- // again, return type must be a Promise<T>, so take the first type argument
186
- return returnType.typeArguments?.[0];
187
- }
188
- generateResponse(method) {
189
- const returnType = this.getMethodReturnType(method.node);
190
- if (!returnType) {
191
- this.#diags.raise(`Could not determine return type of ${method.name}`, method.node);
192
- return;
193
- }
194
- if (returnType.flags & typescript_1.default.TypeFlags.VoidLike) {
195
- return ["204", { description: "No Content" }];
196
- }
197
- const typeNode = this.#checker.typeToTypeNode(returnType, undefined, undefined);
198
- if (!typeNode) {
199
- this.#diags.warn(`Could not determine return type node of ${method.name}`, method.node);
200
- }
201
- return ["200", {
202
- description: "Ok",
203
- content: {
204
- "application/json": {
205
- schema: this.generateSchema(typeNode)
206
- }
207
- }
208
- }];
209
- }
210
- generateParameters(sourcedParams) {
211
- if (sourcedParams.length === 0)
212
- return [];
213
- return sourcedParams
214
- // QUERY and URL parameters are specified in the Operation.parameters field
215
- .filter(([_, source]) => source === handler_1.ArgSources.QUERY || source === handler_1.ArgSources.URL)
216
- .map(([parameter, argSource]) => this.generateParameter(parameter, argSource))
217
- .filter(isValid);
218
- }
219
- generateParameter(parameter, argSource) {
220
- if (argSource === handler_1.ArgSources.BODY) {
221
- this.#diags.raise(`BODY parameters must be specified in the Operation.requestBody field: ${parameter.name}`, parameter.node);
222
- return;
223
- }
224
- if (argSource === handler_1.ArgSources.URL && !parameter.required) {
225
- this.#diags.raise(`URL parameters must be required: ${parameter.name}`, parameter.node);
226
- return;
227
- }
228
- const schema = this.generateSchema(parameter.node);
229
- const name = this.getParamName(parameter);
230
- const location = this.getLocation(parameter, argSource);
231
- if (!name || !location)
232
- return;
233
- return {
234
- name,
235
- in: location,
236
- required: parameter.required,
237
- schema
238
- };
239
- }
240
- getLocation(parameter, argSource) {
241
- switch (argSource) {
242
- case handler_1.ArgSources.QUERY: return 'query';
243
- case handler_1.ArgSources.URL: return 'path';
244
- default:
245
- this.#diags.raise(`Unsupported Parameter ArgSource: ${argSource}`, parameter.node);
246
- return;
247
- }
248
- }
249
- generateRequestBody(sourcedParams) {
250
- // BODY parameters are specified in the Operation.requestBody field
251
- const parameters = sourcedParams
252
- .filter(([_, source]) => source === handler_1.ArgSources.BODY)
253
- .map(([parameter, _]) => [this.getParamName(parameter), parameter]);
254
- if (parameters.length === 0)
255
- return undefined;
256
- const properties = parameters.map(([name, parameter]) => [name, this.generateSchema(parameter.node)]);
257
- const schema = {
258
- type: 'object',
259
- properties: Object.fromEntries(properties),
260
- required: parameters.filter(([_, parameter]) => parameter.required).map(([name, _]) => name),
261
- };
262
- return {
263
- required: true,
264
- content: {
265
- "application/json": { schema }
266
- }
267
- };
268
- }
269
- generateSchema(node) {
270
- if (!node)
271
- return { description: "Undefined Node" };
272
- const schema = this.#schemaGenerator.createSchemaFromNodes([node]);
273
- if (!schema.$ref)
274
- return { description: "No $ref" };
275
- const slashIndex = schema.$ref.lastIndexOf('/');
276
- if (slashIndex === -1)
277
- return { description: `Invalid $ref ${schema.$ref}` };
278
- const name = schema.$ref.substring(slashIndex + 1);
279
- const defs = new Map(Object.entries(schema.definitions ?? {}));
280
- if (defs.size === 0)
281
- return { description: "No definitions" };
282
- const def = defs.get(name);
283
- if (!def)
284
- return { description: `No definition ${name}` };
285
- if (typeof def === 'boolean')
286
- return { description: `Definition ${name} is a boolean` };
287
- if (defs.size > 1) {
288
- defs.delete(name);
289
- for (const [$name, $def] of defs) {
290
- if (typeof $def === 'boolean')
291
- continue;
292
- const $schema = this.mapSchema($def);
293
- if ($schema)
294
- this.#schemaMap.set($name, $schema);
295
- }
296
- }
297
- return this.mapSchema(def);
298
- }
299
- getDBOSDecorator(decorated, name) {
300
- const filtered = decorated.decorators.filter(d => d.module === '@dbos-inc/dbos-sdk' && d.name === name);
301
- if (filtered.length === 0)
302
- return undefined;
303
- if (filtered.length > 1) {
304
- this.#diags.raise(`Multiple ${JSON.stringify(name)} decorators found on ${decorated.name ?? "<unknown>"}`, decorated.node);
305
- }
306
- return filtered[0];
307
- }
308
- getHttpInfo(method) {
309
- const getApiDecorator = this.getDBOSDecorator(method, 'GetApi');
310
- const postApiDecorator = this.getDBOSDecorator(method, 'PostApi');
311
- if (getApiDecorator && postApiDecorator) {
312
- this.#diags.raise(`Method ${method.name} has both GetApi and PostApi decorators`);
313
- return;
314
- }
315
- if (!getApiDecorator && !postApiDecorator)
316
- return undefined;
317
- const verb = getApiDecorator ? handler_1.APITypes.GET : handler_1.APITypes.POST;
318
- const arg = getApiDecorator ? getApiDecorator.args[0] : postApiDecorator?.args[0];
319
- if (!arg) {
320
- this.#diags.raise(`Missing path argument for ${verb}Api decorator`, method.node);
321
- return;
322
- }
323
- if (!typescript_1.default.isStringLiteral(arg)) {
324
- this.#diags.raise(`Unexpected path argument type: ${typescript_1.default.SyntaxKind[arg.kind]}`, method.node);
325
- return;
326
- }
327
- return { verb, path: arg.text };
328
- }
329
- getParamSource(parameter, verb) {
330
- const argSource = this.getDBOSDecorator(parameter, 'ArgSource');
331
- if (!argSource)
332
- return getDefaultArgSource(verb);
333
- if (!typescript_1.default.isPropertyAccessExpression(argSource.args[0])) {
334
- this.#diags.raise(`Unexpected ArgSource argument type: ${typescript_1.default.SyntaxKind[argSource.args[0].kind]}`, parameter.node);
335
- return;
336
- }
337
- switch (argSource.args[0].name.text) {
338
- case "BODY": return handler_1.ArgSources.BODY;
339
- case "QUERY": return handler_1.ArgSources.QUERY;
340
- case "URL": return handler_1.ArgSources.URL;
341
- case "DEFAULT": return getDefaultArgSource(verb);
342
- default:
343
- this.#diags.raise(`Unexpected ArgSource argument: ${argSource.args[0].name.text}`, parameter.node);
344
- return;
345
- }
346
- function getDefaultArgSource(verb) {
347
- switch (verb) {
348
- case handler_1.APITypes.GET: return handler_1.ArgSources.QUERY;
349
- case handler_1.APITypes.POST: return handler_1.ArgSources.BODY;
350
- }
351
- }
352
- }
353
- getParamName(parameter) {
354
- const argName = this.getDBOSDecorator(parameter, 'ArgName');
355
- if (!argName)
356
- return parameter.name;
357
- const nameParam = argName.args[0];
358
- if (nameParam && typescript_1.default.isStringLiteral(nameParam))
359
- return nameParam.text;
360
- this.#diags.raise(`Unexpected ArgName argument type: ${typescript_1.default.SyntaxKind[nameParam.kind]}`, parameter.node);
361
- }
362
- mapSchema(schema) {
363
- if (Array.isArray(schema.type)) {
364
- this.#diags.raise(`OpenApi 3.0.x doesn't support type arrays: ${JSON.stringify(schema.type)}`);
365
- return;
366
- }
367
- if (schema.$ref) {
368
- const $ref = schema.$ref.replace("#/definitions/", "#/components/schemas/");
369
- return { $ref };
370
- }
371
- const [maximum, exclusiveMaximum] = getMaxes();
372
- const [minimum, exclusiveMinimum] = getMins();
373
- const base = {
374
- title: schema.title,
375
- multipleOf: schema.multipleOf,
376
- maximum,
377
- exclusiveMaximum,
378
- minimum,
379
- exclusiveMinimum,
380
- maxLength: schema.maxLength,
381
- minLength: schema.minLength,
382
- pattern: schema.pattern,
383
- maxItems: schema.maxItems,
384
- minItems: schema.minItems,
385
- uniqueItems: schema.uniqueItems,
386
- maxProperties: schema.maxProperties,
387
- minProperties: schema.minProperties,
388
- required: schema.required,
389
- enum: schema.enum,
390
- description: schema.description,
391
- format: schema.format,
392
- default: schema.default,
393
- // OpenApi 3.0.x doesn't support boolean schema types, so filter those out when mapping these fields
394
- allOf: schema.allOf?.filter(isSchema).map(s => this.mapSchema(s)).filter(isValid),
395
- oneOf: schema.oneOf?.filter(isSchema).map(s => this.mapSchema(s)).filter(isValid),
396
- anyOf: schema.anyOf?.filter(isSchema).map(s => this.mapSchema(s)).filter(isValid),
397
- not: schema.not
398
- ? isSchema(schema.not) ? this.mapSchema(schema.not) : undefined
399
- : undefined,
400
- properties: schema.properties
401
- ? Object.fromEntries(Object.entries(schema.properties)
402
- .filter((entry) => isSchema(entry[1]))
403
- .map(([name, prop]) => [name, this.mapSchema(prop)])
404
- .filter((entry) => isValid(entry[1])))
405
- : undefined,
406
- additionalProperties: typeof schema.additionalProperties === 'object'
407
- ? this.mapSchema(schema.additionalProperties)
408
- : schema.additionalProperties,
409
- };
410
- if (schema.type === 'array') {
411
- if (schema.items === undefined) {
412
- this.#diags.raise(`Array schema has no items`);
413
- return;
414
- }
415
- if (Array.isArray(schema.items)) {
416
- this.#diags.raise(`OpenApi 3.0.x doesn't support array items arrays: ${JSON.stringify(schema.items)}`);
417
- return;
418
- }
419
- if (typeof schema.items === 'boolean') {
420
- this.#diags.raise(`OpenApi 3.0.x doesn't support array items booleans: ${JSON.stringify(schema.items)}`);
421
- return;
422
- }
423
- return {
424
- type: schema.type,
425
- items: this.mapSchema(schema.items),
426
- ...base,
427
- };
428
- }
429
- return {
430
- // OpenApi 3.0.x doesn't support null type value, so map null type to undefined and add nullable: true property
431
- type: schema.type === 'null' ? undefined : schema.type,
432
- ...base,
433
- nullable: schema.type === 'null' ? true : undefined,
434
- };
435
- function isSchema(schema) {
436
- return typeof schema === "object";
437
- }
438
- // Convert JSON Schema Draft 7 (used by ts-json-schema-generator) max/min and exclusive max/min values to JSON Schema Draft 5 (used by OpenAPI).
439
- // In Draft 5, exclusive max/min values are booleans
440
- // In Draft 7, exclusive max/min values are numbers
441
- function getMaxes() {
442
- const { maximum: max, exclusiveMaximum: xMax } = schema;
443
- if (max) {
444
- if (xMax) {
445
- return (xMax < max) ? [xMax, true] : [max, false];
446
- }
447
- else {
448
- return [max, false];
449
- }
450
- }
451
- else {
452
- return xMax ? [xMax, true] : [undefined, undefined];
453
- }
454
- }
455
- function getMins() {
456
- const { minimum: min, exclusiveMinimum: xMin } = schema;
457
- if (min) {
458
- if (xMin) {
459
- return (xMin > min) ? [xMin, true] : [min, false];
460
- }
461
- else {
462
- return [min, false];
463
- }
464
- }
465
- else {
466
- return xMin ? [xMin, true] : [undefined, undefined];
467
- }
468
- }
469
- }
470
- parseStringLiteralArray(node) {
471
- if (!node)
472
- return undefined;
473
- if (!typescript_1.default.isArrayLiteralExpression(node)) {
474
- this.#diags.raise(`Expected Array Literal node, received: ${typescript_1.default.SyntaxKind[node.kind]}`, node);
475
- return;
476
- }
477
- const values = new Array();
478
- for (const exp of node.elements) {
479
- if (typescript_1.default.isStringLiteral(exp)) {
480
- values.push(exp.getText());
481
- }
482
- else {
483
- this.#diags.raise(`Expected String Literal node, received: ${typescript_1.default.SyntaxKind[exp.kind]}`, exp);
484
- }
485
- }
486
- return values;
487
- }
488
- parseSecurityScheme(arg) {
489
- if (!arg)
490
- return undefined;
491
- if (!typescript_1.default.isObjectLiteralExpression(arg)) {
492
- this.#diags.raise(`Expected Object Literal node, received: ${typescript_1.default.SyntaxKind[arg.kind]}`, arg);
493
- return undefined;
494
- }
495
- const map = new Map();
496
- for (const prop of arg.properties) {
497
- if (!typescript_1.default.isPropertyAssignment(prop)) {
498
- this.#diags.raise(`Expected Property Assignment node, received: ${typescript_1.default.SyntaxKind[prop.kind]}`, prop);
499
- continue;
500
- }
501
- if (!typescript_1.default.isStringLiteral(prop.initializer)) {
502
- this.#diags.raise(`Expected String Literal node, received: ${typescript_1.default.SyntaxKind[prop.initializer.kind]}`, prop.initializer);
503
- continue;
504
- }
505
- map.set(prop.name.getText(), prop.initializer.text);
506
- }
507
- const type = map.get("type");
508
- const description = map.get("description");
509
- if (!type) {
510
- this.#diags.raise(`missing type property`, arg);
511
- return;
512
- }
513
- if (type === 'http') {
514
- const scheme = map.get("scheme");
515
- if (!scheme) {
516
- this.#diags.raise(`missing scheme property`, arg);
517
- return;
518
- }
519
- const bearerFormat = map.get("bearerFormat");
520
- return { type, scheme, bearerFormat, description };
521
- }
522
- if (type === 'apiKey') {
523
- const name = map.get("name");
524
- if (!name) {
525
- this.#diags.raise(`missing name property`, arg);
526
- return;
527
- }
528
- const $in = map.get("id");
529
- if (!$in) {
530
- this.#diags.raise(`missing in property`, arg);
531
- return;
532
- }
533
- return { type, name, in: $in, description };
534
- }
535
- if (type === "openIdConnect") {
536
- const openIdConnectUrl = map.get("openIdConnectUrl");
537
- if (!openIdConnectUrl) {
538
- this.#diags.raise(`missing openIdConnectUrl property`, arg);
539
- return;
540
- }
541
- return { type, openIdConnectUrl, description };
542
- }
543
- if (type === 'oauth2') {
544
- this.#diags.raise(`OAuth2 Security Scheme not supported`, arg);
545
- }
546
- else {
547
- this.#diags.raise(`unrecognized Security Scheme type ${type}`, arg);
548
- }
549
- return;
550
- }
551
- }
552
- exports.OpenApiGenerator = OpenApiGenerator;
553
- async function findPackageInfo(entrypoint) {
554
- let dirname = node_path_1.default.dirname(entrypoint);
555
- while (dirname !== '/') {
556
- try {
557
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
558
- const packageJson = JSON.parse(await promises_1.default.readFile(node_path_1.default.join(dirname, 'package.json'), { encoding: 'utf-8' }));
559
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
560
- const name = packageJson.name ?? "unknown";
561
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
562
- const version = packageJson.version;
563
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
564
- const isPrivate = packageJson.private ?? false;
565
- return {
566
- name,
567
- version: version
568
- ? version
569
- : isPrivate ? "private" : "unknown"
570
- };
571
- }
572
- catch (error) {
573
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
574
- if (error.code !== 'ENOENT')
575
- throw error;
576
- }
577
- dirname = node_path_1.default.dirname(dirname);
578
- }
579
- return { name: "unknown", version: "unknown" };
580
- }
581
- async function generateOpenApi(entrypoint) {
582
- const program = typescript_1.default.createProgram([entrypoint], {});
583
- const parser = new TypeParser_1.TypeParser(program);
584
- const classes = parser.parse();
585
- (0, tsDiagUtil_1.logDiagnostics)(parser.diags);
586
- if (!classes || classes.length === 0)
587
- return undefined;
588
- const { name, version } = await findPackageInfo(entrypoint);
589
- const generator = new OpenApiGenerator(program);
590
- const openapi = generator.generate(classes, name, version);
591
- (0, tsDiagUtil_1.logDiagnostics)(generator.diags);
592
- return openapi;
593
- }
594
- exports.generateOpenApi = generateOpenApi;
595
- //# sourceMappingURL=openApi.js.map