@ankhorage/data-sources 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/openapi/index.d.ts +89 -0
- package/dist/openapi/index.d.ts.map +1 -0
- package/dist/openapi/index.js +330 -0
- package/dist/openapi/index.js.map +1 -0
- package/dist/rest/index.d.ts +44 -0
- package/dist/rest/index.d.ts.map +1 -0
- package/dist/rest/index.js +158 -0
- package/dist/rest/index.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +3 -0
- package/src/openapi/index.test.ts +296 -0
- package/src/openapi/index.ts +576 -0
- package/src/rest/index.test.ts +237 -0
- package/src/rest/index.ts +243 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CredentialRef,
|
|
3
|
+
DataContractValue,
|
|
4
|
+
DataEndpointConfig,
|
|
5
|
+
DataOperationConfig,
|
|
6
|
+
DataOperationIntent,
|
|
7
|
+
DataOperationParameter,
|
|
8
|
+
DataOperationParameterLocation,
|
|
9
|
+
DataOperationRequest,
|
|
10
|
+
DataOperationResponse,
|
|
11
|
+
DataSchema,
|
|
12
|
+
DataSchemaPrimitiveType,
|
|
13
|
+
DataSchemaRegistry,
|
|
14
|
+
DataSourceDiagnostic,
|
|
15
|
+
DataSourceDiagnosticResult,
|
|
16
|
+
EndpointId,
|
|
17
|
+
OpenApiDataSourceConfig,
|
|
18
|
+
OperationId,
|
|
19
|
+
} from '@ankhorage/contracts/data';
|
|
20
|
+
|
|
21
|
+
const OPENAPI_HTTP_METHODS = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'] as const;
|
|
22
|
+
const DATA_SCHEMA_PRIMITIVES = [
|
|
23
|
+
'array',
|
|
24
|
+
'boolean',
|
|
25
|
+
'integer',
|
|
26
|
+
'null',
|
|
27
|
+
'number',
|
|
28
|
+
'object',
|
|
29
|
+
'string',
|
|
30
|
+
] as const satisfies readonly DataSchemaPrimitiveType[];
|
|
31
|
+
|
|
32
|
+
export type OpenApiHttpMethod = (typeof OPENAPI_HTTP_METHODS)[number];
|
|
33
|
+
|
|
34
|
+
export interface OpenApiServerObject {
|
|
35
|
+
readonly url: string;
|
|
36
|
+
readonly description?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface OpenApiSchemaObject {
|
|
40
|
+
readonly $ref?: string;
|
|
41
|
+
readonly type?: string | readonly string[];
|
|
42
|
+
readonly title?: string;
|
|
43
|
+
readonly description?: string;
|
|
44
|
+
readonly enum?: readonly DataContractValue[];
|
|
45
|
+
readonly const?: DataContractValue;
|
|
46
|
+
readonly default?: DataContractValue;
|
|
47
|
+
readonly format?: string;
|
|
48
|
+
readonly nullable?: boolean;
|
|
49
|
+
readonly required?: readonly string[];
|
|
50
|
+
readonly properties?: Readonly<Record<string, OpenApiSchemaObject>>;
|
|
51
|
+
readonly additionalProperties?: boolean | OpenApiSchemaObject;
|
|
52
|
+
readonly items?: OpenApiSchemaObject;
|
|
53
|
+
readonly allOf?: readonly OpenApiSchemaObject[];
|
|
54
|
+
readonly anyOf?: readonly OpenApiSchemaObject[];
|
|
55
|
+
readonly oneOf?: readonly OpenApiSchemaObject[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface OpenApiMediaTypeObject {
|
|
59
|
+
readonly schema?: OpenApiSchemaObject;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface OpenApiRequestBodyObject {
|
|
63
|
+
readonly description?: string;
|
|
64
|
+
readonly required?: boolean;
|
|
65
|
+
readonly content?: Readonly<Record<string, OpenApiMediaTypeObject>>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface OpenApiResponseObject {
|
|
69
|
+
readonly description?: string;
|
|
70
|
+
readonly content?: Readonly<Record<string, OpenApiMediaTypeObject>>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface OpenApiParameterObject {
|
|
74
|
+
readonly name: string;
|
|
75
|
+
readonly in: string;
|
|
76
|
+
readonly required?: boolean;
|
|
77
|
+
readonly description?: string;
|
|
78
|
+
readonly schema?: OpenApiSchemaObject;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface OpenApiOperationObject {
|
|
82
|
+
readonly operationId?: string;
|
|
83
|
+
readonly summary?: string;
|
|
84
|
+
readonly description?: string;
|
|
85
|
+
readonly deprecated?: boolean;
|
|
86
|
+
readonly parameters?: readonly OpenApiParameterObject[];
|
|
87
|
+
readonly requestBody?: OpenApiRequestBodyObject;
|
|
88
|
+
readonly responses?: Readonly<Record<string, OpenApiResponseObject>>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type OpenApiPathItemObject = Partial<Record<OpenApiHttpMethod, OpenApiOperationObject>> & {
|
|
92
|
+
readonly parameters?: readonly OpenApiParameterObject[];
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export interface OpenApiComponentsObject {
|
|
96
|
+
readonly schemas?: Readonly<Record<string, OpenApiSchemaObject>>;
|
|
97
|
+
readonly securitySchemes?: Readonly<Record<string, DataContractValue>>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface OpenApiDocumentObject {
|
|
101
|
+
readonly openapi?: string;
|
|
102
|
+
readonly info?: {
|
|
103
|
+
readonly title?: string;
|
|
104
|
+
readonly version?: string;
|
|
105
|
+
readonly description?: string;
|
|
106
|
+
};
|
|
107
|
+
readonly servers?: readonly OpenApiServerObject[];
|
|
108
|
+
readonly paths?: Readonly<Record<string, OpenApiPathItemObject>>;
|
|
109
|
+
readonly components?: OpenApiComponentsObject;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface OpenApiImportInput {
|
|
113
|
+
readonly id: string;
|
|
114
|
+
readonly document: OpenApiDocumentObject;
|
|
115
|
+
readonly baseUrl?: string;
|
|
116
|
+
readonly credential?: CredentialRef;
|
|
117
|
+
readonly documentId?: string;
|
|
118
|
+
readonly documentUrl?: string;
|
|
119
|
+
readonly name?: string;
|
|
120
|
+
readonly description?: string;
|
|
121
|
+
readonly metadata?: DataContractValue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type OpenApiImportResult = DataSourceDiagnosticResult<OpenApiDataSourceConfig>;
|
|
125
|
+
|
|
126
|
+
export function importOpenApiDocument(input: OpenApiImportInput): OpenApiImportResult {
|
|
127
|
+
const diagnostics: DataSourceDiagnostic[] = [];
|
|
128
|
+
const baseUrl = resolveOpenApiBaseUrl(input, diagnostics);
|
|
129
|
+
const schemas = normalizeOpenApiSchemas(
|
|
130
|
+
input.document.components?.schemas,
|
|
131
|
+
input.id,
|
|
132
|
+
diagnostics,
|
|
133
|
+
);
|
|
134
|
+
const endpoints = normalizeOpenApiEndpoints(input, baseUrl, diagnostics);
|
|
135
|
+
const hasErrors = diagnostics.some((diagnostic) => diagnostic.severity === 'error');
|
|
136
|
+
|
|
137
|
+
if (hasErrors) {
|
|
138
|
+
return { ok: false, diagnostics };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
ok: true,
|
|
143
|
+
data: {
|
|
144
|
+
id: input.id,
|
|
145
|
+
kind: 'openapi',
|
|
146
|
+
name: input.name ?? input.document.info?.title,
|
|
147
|
+
description: input.description ?? input.document.info?.description,
|
|
148
|
+
baseUrl,
|
|
149
|
+
credential: input.credential,
|
|
150
|
+
endpoints,
|
|
151
|
+
schemas,
|
|
152
|
+
import: {
|
|
153
|
+
url: input.documentUrl,
|
|
154
|
+
documentId: input.documentId,
|
|
155
|
+
version: input.document.info?.version,
|
|
156
|
+
},
|
|
157
|
+
metadata: input.metadata,
|
|
158
|
+
},
|
|
159
|
+
diagnostics,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function normalizeOpenApiOperationId(
|
|
164
|
+
method: OpenApiHttpMethod,
|
|
165
|
+
path: string,
|
|
166
|
+
operationId?: string,
|
|
167
|
+
): OperationId {
|
|
168
|
+
const rawId = operationId ?? `${method}-${path}`;
|
|
169
|
+
const normalized = rawId
|
|
170
|
+
.replace(/\{([^}]+)\}/g, '$1')
|
|
171
|
+
.replace(/[^A-Za-z0-9]+/g, '-')
|
|
172
|
+
.replace(/^-+|-+$/g, '')
|
|
173
|
+
.toLowerCase();
|
|
174
|
+
|
|
175
|
+
return normalized.length > 0 ? normalized : `${method}-root`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function normalizeOpenApiEndpointId(path: string): EndpointId {
|
|
179
|
+
const normalized = path
|
|
180
|
+
.replace(/\{([^}]+)\}/g, '$1')
|
|
181
|
+
.replace(/[^A-Za-z0-9]+/g, '-')
|
|
182
|
+
.replace(/^-+|-+$/g, '')
|
|
183
|
+
.toLowerCase();
|
|
184
|
+
|
|
185
|
+
return normalized.length > 0 ? normalized : 'root';
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function normalizeOpenApiSchema(schema: OpenApiSchemaObject): DataSchema {
|
|
189
|
+
const additionalProperties = normalizeAdditionalProperties(schema.additionalProperties);
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
ref: normalizeOpenApiSchemaRef(schema.$ref),
|
|
193
|
+
type: normalizeSchemaType(schema.type),
|
|
194
|
+
title: schema.title,
|
|
195
|
+
description: schema.description,
|
|
196
|
+
enum: schema.enum,
|
|
197
|
+
const: schema.const,
|
|
198
|
+
default: schema.default,
|
|
199
|
+
format: schema.format,
|
|
200
|
+
nullable: schema.nullable,
|
|
201
|
+
required: schema.required,
|
|
202
|
+
properties: normalizeSchemaRecord(schema.properties),
|
|
203
|
+
additionalProperties,
|
|
204
|
+
items: schema.items === undefined ? undefined : normalizeOpenApiSchema(schema.items),
|
|
205
|
+
allOf: normalizeSchemaList(schema.allOf),
|
|
206
|
+
anyOf: normalizeSchemaList(schema.anyOf),
|
|
207
|
+
oneOf: normalizeSchemaList(schema.oneOf),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function resolveOpenApiBaseUrl(
|
|
212
|
+
input: OpenApiImportInput,
|
|
213
|
+
diagnostics: DataSourceDiagnostic[],
|
|
214
|
+
): string | undefined {
|
|
215
|
+
if (input.baseUrl !== undefined) return input.baseUrl;
|
|
216
|
+
|
|
217
|
+
const servers = input.document.servers ?? [];
|
|
218
|
+
|
|
219
|
+
if (servers.length === 0) {
|
|
220
|
+
diagnostics.push({
|
|
221
|
+
code: 'ambiguous-server',
|
|
222
|
+
dataSourceId: input.id,
|
|
223
|
+
message:
|
|
224
|
+
'OpenAPI document does not define a server URL and no baseUrl override was provided.',
|
|
225
|
+
path: 'servers',
|
|
226
|
+
severity: 'warning',
|
|
227
|
+
});
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (servers.length > 1) {
|
|
232
|
+
diagnostics.push({
|
|
233
|
+
code: 'ambiguous-server',
|
|
234
|
+
dataSourceId: input.id,
|
|
235
|
+
message: 'OpenAPI document defines multiple servers. The first server URL was selected.',
|
|
236
|
+
path: 'servers',
|
|
237
|
+
severity: 'warning',
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return servers[0]?.url;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function normalizeOpenApiSchemas(
|
|
245
|
+
schemas: Readonly<Record<string, OpenApiSchemaObject>> | undefined,
|
|
246
|
+
dataSourceId: string,
|
|
247
|
+
diagnostics: DataSourceDiagnostic[],
|
|
248
|
+
): DataSchemaRegistry | undefined {
|
|
249
|
+
if (schemas === undefined) return undefined;
|
|
250
|
+
|
|
251
|
+
const registry: Record<string, DataSchema> = {};
|
|
252
|
+
|
|
253
|
+
for (const [schemaId, schema] of Object.entries(schemas)) {
|
|
254
|
+
registry[schemaId] = normalizeOpenApiSchema(schema);
|
|
255
|
+
collectUnsupportedSchemaDiagnostics(
|
|
256
|
+
schema,
|
|
257
|
+
dataSourceId,
|
|
258
|
+
`components.schemas.${schemaId}`,
|
|
259
|
+
diagnostics,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return registry;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function normalizeOpenApiEndpoints(
|
|
267
|
+
input: OpenApiImportInput,
|
|
268
|
+
baseUrl: string | undefined,
|
|
269
|
+
diagnostics: DataSourceDiagnostic[],
|
|
270
|
+
): Record<EndpointId, DataEndpointConfig> {
|
|
271
|
+
const { paths } = input.document;
|
|
272
|
+
|
|
273
|
+
if (paths === undefined || Object.keys(paths).length === 0) {
|
|
274
|
+
diagnostics.push({
|
|
275
|
+
code: 'invalid-config',
|
|
276
|
+
dataSourceId: input.id,
|
|
277
|
+
message: 'OpenAPI document must define at least one path.',
|
|
278
|
+
path: 'paths',
|
|
279
|
+
severity: 'error',
|
|
280
|
+
});
|
|
281
|
+
return {};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const endpoints: Record<EndpointId, DataEndpointConfig> = {};
|
|
285
|
+
const operationIds = new Set<OperationId>();
|
|
286
|
+
|
|
287
|
+
for (const [path, pathItem] of Object.entries(paths)) {
|
|
288
|
+
const endpointId = normalizeOpenApiEndpointId(path);
|
|
289
|
+
const operations: Record<OperationId, DataOperationConfig> = {};
|
|
290
|
+
|
|
291
|
+
for (const method of OPENAPI_HTTP_METHODS) {
|
|
292
|
+
const operation = pathItem[method];
|
|
293
|
+
if (operation === undefined) continue;
|
|
294
|
+
|
|
295
|
+
const operationId = resolveUniqueOperationId(
|
|
296
|
+
input.id,
|
|
297
|
+
endpointId,
|
|
298
|
+
method,
|
|
299
|
+
path,
|
|
300
|
+
operation.operationId,
|
|
301
|
+
operationIds,
|
|
302
|
+
diagnostics,
|
|
303
|
+
);
|
|
304
|
+
const parameters = normalizeOpenApiParameters(
|
|
305
|
+
[...(pathItem.parameters ?? []), ...(operation.parameters ?? [])],
|
|
306
|
+
input.id,
|
|
307
|
+
endpointId,
|
|
308
|
+
operationId,
|
|
309
|
+
diagnostics,
|
|
310
|
+
);
|
|
311
|
+
const request = normalizeOpenApiRequest(operation.requestBody, parameters);
|
|
312
|
+
const response = normalizeOpenApiResponse(operation.responses);
|
|
313
|
+
|
|
314
|
+
operations[operationId] = {
|
|
315
|
+
id: operationId,
|
|
316
|
+
endpointId,
|
|
317
|
+
name: operation.summary,
|
|
318
|
+
description: operation.description,
|
|
319
|
+
protocol: 'http',
|
|
320
|
+
intent: mapOpenApiMethodToIntent(method),
|
|
321
|
+
method: method.toUpperCase(),
|
|
322
|
+
path,
|
|
323
|
+
request,
|
|
324
|
+
response,
|
|
325
|
+
metadata: {
|
|
326
|
+
deprecated: operation.deprecated ?? false,
|
|
327
|
+
source: 'openapi',
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
endpoints[endpointId] = {
|
|
333
|
+
id: endpointId,
|
|
334
|
+
kind: 'http',
|
|
335
|
+
baseUrl,
|
|
336
|
+
path,
|
|
337
|
+
credential: input.credential,
|
|
338
|
+
operations,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return endpoints;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function resolveUniqueOperationId(
|
|
346
|
+
dataSourceId: string,
|
|
347
|
+
endpointId: EndpointId,
|
|
348
|
+
method: OpenApiHttpMethod,
|
|
349
|
+
path: string,
|
|
350
|
+
operationId: string | undefined,
|
|
351
|
+
operationIds: Set<OperationId>,
|
|
352
|
+
diagnostics: DataSourceDiagnostic[],
|
|
353
|
+
): OperationId {
|
|
354
|
+
const normalized = normalizeOpenApiOperationId(method, path, operationId);
|
|
355
|
+
|
|
356
|
+
if (!operationIds.has(normalized)) {
|
|
357
|
+
operationIds.add(normalized);
|
|
358
|
+
return normalized;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
let suffix = 2;
|
|
362
|
+
let uniqueId: OperationId = `${normalized}-${suffix}`;
|
|
363
|
+
|
|
364
|
+
while (operationIds.has(uniqueId)) {
|
|
365
|
+
suffix += 1;
|
|
366
|
+
uniqueId = `${normalized}-${suffix}`;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
operationIds.add(uniqueId);
|
|
370
|
+
diagnostics.push({
|
|
371
|
+
code: 'duplicate-operation-id',
|
|
372
|
+
dataSourceId,
|
|
373
|
+
endpointId,
|
|
374
|
+
operationId: uniqueId,
|
|
375
|
+
message: `OpenAPI operation id '${normalized}' is duplicated. Normalized to '${uniqueId}'.`,
|
|
376
|
+
path: `paths.${path}.${method}.operationId`,
|
|
377
|
+
severity: 'warning',
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
return uniqueId;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function normalizeOpenApiParameters(
|
|
384
|
+
parameters: readonly OpenApiParameterObject[],
|
|
385
|
+
dataSourceId: string,
|
|
386
|
+
endpointId: EndpointId,
|
|
387
|
+
operationId: OperationId,
|
|
388
|
+
diagnostics: DataSourceDiagnostic[],
|
|
389
|
+
): readonly DataOperationParameter[] | undefined {
|
|
390
|
+
if (parameters.length === 0) return undefined;
|
|
391
|
+
|
|
392
|
+
const normalized: DataOperationParameter[] = [];
|
|
393
|
+
|
|
394
|
+
for (const parameter of parameters) {
|
|
395
|
+
const location = normalizeParameterLocation(parameter.in);
|
|
396
|
+
|
|
397
|
+
if (location === undefined) {
|
|
398
|
+
diagnostics.push({
|
|
399
|
+
code: 'invalid-config',
|
|
400
|
+
dataSourceId,
|
|
401
|
+
endpointId,
|
|
402
|
+
operationId,
|
|
403
|
+
message: `OpenAPI parameter '${parameter.name}' uses unsupported location '${parameter.in}'.`,
|
|
404
|
+
path: `paths.${endpointId}.parameters.${parameter.name}`,
|
|
405
|
+
severity: 'warning',
|
|
406
|
+
});
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
normalized.push({
|
|
411
|
+
name: parameter.name,
|
|
412
|
+
location,
|
|
413
|
+
required: parameter.required,
|
|
414
|
+
description: parameter.description,
|
|
415
|
+
schema: parameter.schema === undefined ? undefined : normalizeOpenApiSchema(parameter.schema),
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return normalized;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function normalizeParameterLocation(location: string): DataOperationParameterLocation | undefined {
|
|
423
|
+
if (
|
|
424
|
+
location === 'cookie' ||
|
|
425
|
+
location === 'header' ||
|
|
426
|
+
location === 'path' ||
|
|
427
|
+
location === 'query'
|
|
428
|
+
) {
|
|
429
|
+
return location;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return undefined;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function normalizeOpenApiRequest(
|
|
436
|
+
requestBody: OpenApiRequestBodyObject | undefined,
|
|
437
|
+
parameters: readonly DataOperationParameter[] | undefined,
|
|
438
|
+
): DataOperationRequest | undefined {
|
|
439
|
+
const content = firstContentEntry(requestBody?.content);
|
|
440
|
+
|
|
441
|
+
if (requestBody === undefined && parameters === undefined) return undefined;
|
|
442
|
+
|
|
443
|
+
return {
|
|
444
|
+
contentType: content?.contentType,
|
|
445
|
+
parameters,
|
|
446
|
+
schema: content?.schema === undefined ? undefined : normalizeOpenApiSchema(content.schema),
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function normalizeOpenApiResponse(
|
|
451
|
+
responses: Readonly<Record<string, OpenApiResponseObject>> | undefined,
|
|
452
|
+
): DataOperationResponse | undefined {
|
|
453
|
+
if (responses === undefined) return undefined;
|
|
454
|
+
|
|
455
|
+
const status = chooseResponseStatus(Object.keys(responses));
|
|
456
|
+
if (status === undefined) return undefined;
|
|
457
|
+
|
|
458
|
+
const response = responses[status];
|
|
459
|
+
const content = firstContentEntry(response?.content);
|
|
460
|
+
|
|
461
|
+
return {
|
|
462
|
+
status,
|
|
463
|
+
contentType: content?.contentType,
|
|
464
|
+
description: response?.description,
|
|
465
|
+
schema: content?.schema === undefined ? undefined : normalizeOpenApiSchema(content.schema),
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function firstContentEntry(
|
|
470
|
+
content: Readonly<Record<string, OpenApiMediaTypeObject>> | undefined,
|
|
471
|
+
): { readonly contentType: string; readonly schema?: OpenApiSchemaObject } | undefined {
|
|
472
|
+
if (content === undefined) return undefined;
|
|
473
|
+
|
|
474
|
+
const [entry] = Object.entries(content);
|
|
475
|
+
if (entry === undefined) return undefined;
|
|
476
|
+
|
|
477
|
+
const [contentType, media] = entry;
|
|
478
|
+
return { contentType, schema: media.schema };
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function chooseResponseStatus(statuses: readonly string[]): string | undefined {
|
|
482
|
+
return statuses.find((status) => status.startsWith('2')) ?? statuses[0];
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function mapOpenApiMethodToIntent(method: OpenApiHttpMethod): DataOperationIntent {
|
|
486
|
+
if (method === 'get' || method === 'head') return 'read';
|
|
487
|
+
if (method === 'post') return 'create';
|
|
488
|
+
if (method === 'delete') return 'delete';
|
|
489
|
+
if (method === 'patch' || method === 'put') return 'update';
|
|
490
|
+
return 'action';
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function normalizeOpenApiSchemaRef(ref: string | undefined): { readonly id: string } | undefined {
|
|
494
|
+
if (ref === undefined) return undefined;
|
|
495
|
+
|
|
496
|
+
const prefix = '#/components/schemas/';
|
|
497
|
+
if (ref.startsWith(prefix)) return { id: ref.slice(prefix.length) };
|
|
498
|
+
|
|
499
|
+
return { id: ref };
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function normalizeSchemaRecord(
|
|
503
|
+
record: Readonly<Record<string, OpenApiSchemaObject>> | undefined,
|
|
504
|
+
): Readonly<Record<string, DataSchema>> | undefined {
|
|
505
|
+
if (record === undefined) return undefined;
|
|
506
|
+
|
|
507
|
+
const normalized: Record<string, DataSchema> = {};
|
|
508
|
+
|
|
509
|
+
for (const [key, schema] of Object.entries(record)) {
|
|
510
|
+
normalized[key] = normalizeOpenApiSchema(schema);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
return normalized;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function normalizeSchemaList(
|
|
517
|
+
list: readonly OpenApiSchemaObject[] | undefined,
|
|
518
|
+
): readonly DataSchema[] | undefined {
|
|
519
|
+
return list?.map((schema) => normalizeOpenApiSchema(schema));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function normalizeAdditionalProperties(
|
|
523
|
+
additionalProperties: boolean | OpenApiSchemaObject | undefined,
|
|
524
|
+
): boolean | DataSchema | undefined {
|
|
525
|
+
if (additionalProperties === undefined || typeof additionalProperties === 'boolean') {
|
|
526
|
+
return additionalProperties;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return normalizeOpenApiSchema(additionalProperties);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function normalizeSchemaType(
|
|
533
|
+
type: string | readonly string[] | undefined,
|
|
534
|
+
): DataSchemaPrimitiveType | readonly DataSchemaPrimitiveType[] | undefined {
|
|
535
|
+
if (type === undefined) return undefined;
|
|
536
|
+
|
|
537
|
+
if (typeof type === 'string') {
|
|
538
|
+
return isDataSchemaPrimitive(type) ? type : undefined;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return type.filter(isDataSchemaPrimitive);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function isDataSchemaPrimitive(type: string): type is DataSchemaPrimitiveType {
|
|
545
|
+
return DATA_SCHEMA_PRIMITIVES.some((primitive) => primitive === type);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function collectUnsupportedSchemaDiagnostics(
|
|
549
|
+
schema: OpenApiSchemaObject,
|
|
550
|
+
dataSourceId: string,
|
|
551
|
+
path: string,
|
|
552
|
+
diagnostics: DataSourceDiagnostic[],
|
|
553
|
+
): void {
|
|
554
|
+
if (typeof schema.type === 'string' && !isDataSchemaPrimitive(schema.type)) {
|
|
555
|
+
diagnostics.push({
|
|
556
|
+
code: 'unsupported-schema',
|
|
557
|
+
dataSourceId,
|
|
558
|
+
message: `OpenAPI schema type '${schema.type}' is not supported by the normalized schema model.`,
|
|
559
|
+
path: `${path}.type`,
|
|
560
|
+
severity: 'warning',
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
for (const [propertyName, propertySchema] of Object.entries(schema.properties ?? {})) {
|
|
565
|
+
collectUnsupportedSchemaDiagnostics(
|
|
566
|
+
propertySchema,
|
|
567
|
+
dataSourceId,
|
|
568
|
+
`${path}.properties.${propertyName}`,
|
|
569
|
+
diagnostics,
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (schema.items !== undefined) {
|
|
574
|
+
collectUnsupportedSchemaDiagnostics(schema.items, dataSourceId, `${path}.items`, diagnostics);
|
|
575
|
+
}
|
|
576
|
+
}
|