@kubb/oas 3.14.3 → 3.15.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/dist/index.cjs +27 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.ts +9 -2
- package/dist/index.js +28 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/__snapshots__/utils.spec.ts.snap +46 -0
- package/src/index.ts +1 -1
- package/src/utils.spec.ts +65 -0
- package/src/utils.ts +34 -4
package/dist/index.cjs
CHANGED
|
@@ -301,14 +301,14 @@ function isRequired(schema) {
|
|
|
301
301
|
function isOptional(schema) {
|
|
302
302
|
return !isRequired(schema);
|
|
303
303
|
}
|
|
304
|
-
async function parse(pathOrApi, oasClass = Oas) {
|
|
305
|
-
if (typeof pathOrApi === "string") {
|
|
304
|
+
async function parse(pathOrApi, { oasClass = Oas, canBundle = true, enablePaths = true } = {}) {
|
|
305
|
+
if (typeof pathOrApi === "string" && canBundle) {
|
|
306
306
|
const config = await openapiCore.loadConfig();
|
|
307
307
|
const bundleResults = await openapiCore.bundle({ ref: pathOrApi, config, base: pathOrApi });
|
|
308
308
|
return parse(bundleResults.bundle.parsed);
|
|
309
309
|
}
|
|
310
310
|
const oasNormalize = new OASNormalize__default.default(pathOrApi, {
|
|
311
|
-
enablePaths
|
|
311
|
+
enablePaths,
|
|
312
312
|
colorizeErrors: true
|
|
313
313
|
});
|
|
314
314
|
const document = await oasNormalize.load();
|
|
@@ -320,6 +320,29 @@ async function parse(pathOrApi, oasClass = Oas) {
|
|
|
320
320
|
}
|
|
321
321
|
return new oasClass({ oas: document });
|
|
322
322
|
}
|
|
323
|
+
async function merge(pathOrApi, { oasClass = Oas } = {}) {
|
|
324
|
+
const instances = await Promise.all(pathOrApi.map((p) => parse(p, { oasClass, enablePaths: false, canBundle: false })));
|
|
325
|
+
if (instances.length === 0) {
|
|
326
|
+
throw new Error("No OAS instances provided for merging.");
|
|
327
|
+
}
|
|
328
|
+
const merged = instances.reduce(
|
|
329
|
+
(acc, current) => {
|
|
330
|
+
return remeda.mergeDeep(acc, current.document);
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
openapi: "3.0.0",
|
|
334
|
+
info: {
|
|
335
|
+
title: "Merged API",
|
|
336
|
+
version: "1.0.0"
|
|
337
|
+
},
|
|
338
|
+
paths: {},
|
|
339
|
+
components: {
|
|
340
|
+
schemas: {}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
return parse(merged, { oasClass });
|
|
345
|
+
}
|
|
323
346
|
|
|
324
347
|
Object.defineProperty(exports, "findSchemaDefinition", {
|
|
325
348
|
enumerable: true,
|
|
@@ -338,6 +361,7 @@ exports.isOptional = isOptional;
|
|
|
338
361
|
exports.isParameterObject = isParameterObject;
|
|
339
362
|
exports.isReference = isReference;
|
|
340
363
|
exports.isRequired = isRequired;
|
|
364
|
+
exports.merge = merge;
|
|
341
365
|
exports.parse = parse;
|
|
342
366
|
//# sourceMappingURL=index.cjs.map
|
|
343
367
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["BaseOas","jsonpointer","schema","matchesMimeType","OASNormalize","isPlainObject","isRef","loadConfig","bundle","swagger2openapi"],"mappings":";;;;;;;;;;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;AAAA,EACzB,GAAK,EAAA,KAAA;AAAA,EACL,IAAM,EAAA,MAAA;AAAA,EACN,GAAK,EAAA,KAAA;AAAA,EACL,KAAO,EAAA,OAAA;AAAA,EACP,MAAQ,EAAA,QAAA;AAAA,EACR,IAAM,EAAA,MAAA;AAAA,EACN,OAAS,EAAA,SAAA;AAAA,EACT,KAAO,EAAA;AACT;ACRa,IAAA,GAAA,GAAN,cAAwCA,wBAAQ,CAAA;AAAA,EACrD,QA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V,CAAA;AAED,IAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,SAAwB,CAAA;AAAA;AAGrD,EAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,UAAU,CAAA;AACvC","file":"index.cjs","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import jsonpointer from 'jsonpointer'\nimport BaseOas from 'oas'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { matchesMimeType } from 'oas/utils'\nimport OASNormalize from 'oas-normalize'\nimport type { OpenAPIV3 } from 'openapi-types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport { isDiscriminator, isReference } from './utils.ts'\n\ntype Options = {\n contentType?: contentType\n discriminator?: 'strict' | 'inherit'\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {\n discriminator: 'strict',\n }\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n }\n\n setOptions(options: Options) {\n this.#options = options\n }\n\n get options(): Options {\n return this.#options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const { mapping = {}, propertyName } = schema.discriminator\n\n if (this.#options.discriminator === 'inherit') {\n Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {\n if (mappingValue) {\n const childSchema = this.get(mappingValue)\n if (!childSchema.properties) {\n childSchema.properties = {}\n }\n\n const property = childSchema.properties[propertyName] as SchemaObject\n\n if (childSchema.properties) {\n childSchema.properties[propertyName] = {\n ...(childSchema.properties ? childSchema.properties[propertyName] : {}),\n enum: [...(property?.enum?.filter((value) => value !== mappingKey) ?? []), mappingKey],\n }\n\n childSchema.required = [...(childSchema.required ?? []), propertyName]\n\n this.set(mappingValue, childSchema)\n }\n }\n })\n }\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n return oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oas = Oas): Promise<Oas> {\n if (typeof pathOrApi === 'string') {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths: true,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["BaseOas","jsonpointer","schema","matchesMimeType","OASNormalize","isPlainObject","isRef","loadConfig","bundle","swagger2openapi","mergeDeep"],"mappings":";;;;;;;;;;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;AAAA,EACzB,GAAK,EAAA,KAAA;AAAA,EACL,IAAM,EAA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,CAAA,MAAA,EAAQ,EAAE,QAAA,EAAU,CAAA;AACnC","file":"index.cjs","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import jsonpointer from 'jsonpointer'\nimport BaseOas from 'oas'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { matchesMimeType } from 'oas/utils'\nimport OASNormalize from 'oas-normalize'\nimport type { OpenAPIV3 } from 'openapi-types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport { isDiscriminator, isReference } from './utils.ts'\n\ntype Options = {\n contentType?: contentType\n discriminator?: 'strict' | 'inherit'\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {\n discriminator: 'strict',\n }\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n }\n\n setOptions(options: Options) {\n this.#options = options\n }\n\n get options(): Options {\n return this.#options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const { mapping = {}, propertyName } = schema.discriminator\n\n if (this.#options.discriminator === 'inherit') {\n Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {\n if (mappingValue) {\n const childSchema = this.get(mappingValue)\n if (!childSchema.properties) {\n childSchema.properties = {}\n }\n\n const property = childSchema.properties[propertyName] as SchemaObject\n\n if (childSchema.properties) {\n childSchema.properties[propertyName] = {\n ...(childSchema.properties ? childSchema.properties[propertyName] : {}),\n enum: [...(property?.enum?.filter((value) => value !== mappingKey) ?? []), mappingKey],\n }\n\n childSchema.required = [...(childSchema.required ?? []), propertyName]\n\n this.set(mappingValue, childSchema)\n }\n }\n })\n }\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n return oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject, mergeDeep } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(\n pathOrApi: string | OASDocument,\n { oasClass = Oas, canBundle = true, enablePaths = true }: { oasClass?: typeof Oas; canBundle?: boolean; enablePaths?: boolean } = {},\n): Promise<Oas> {\n if (typeof pathOrApi === 'string' && canBundle) {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n\nexport async function merge(pathOrApi: Array<string | OASDocument>, { oasClass = Oas }: { oasClass?: typeof Oas } = {}): Promise<Oas> {\n const instances = await Promise.all(pathOrApi.map((p) => parse(p, { oasClass, enablePaths: false, canBundle: false })))\n\n if (instances.length === 0) {\n throw new Error('No OAS instances provided for merging.')\n }\n\n const merged = instances.reduce(\n (acc, current) => {\n return mergeDeep(acc, current.document as OASDocument)\n },\n {\n openapi: '3.0.0',\n info: {\n title: 'Merged API',\n version: '1.0.0',\n },\n paths: {},\n components: {\n schemas: {},\n },\n } as any,\n )\n\n return parse(merged, { oasClass })\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -89,6 +89,13 @@ declare function isDiscriminator(obj?: any): obj is SchemaObject$1 & {
|
|
|
89
89
|
*/
|
|
90
90
|
declare function isRequired(schema?: SchemaObject$1): boolean;
|
|
91
91
|
declare function isOptional(schema?: SchemaObject$1): boolean;
|
|
92
|
-
declare function parse(pathOrApi: string | OASDocument, oasClass
|
|
92
|
+
declare function parse(pathOrApi: string | OASDocument, { oasClass, canBundle, enablePaths }?: {
|
|
93
|
+
oasClass?: typeof Oas;
|
|
94
|
+
canBundle?: boolean;
|
|
95
|
+
enablePaths?: boolean;
|
|
96
|
+
}): Promise<Oas>;
|
|
97
|
+
declare function merge(pathOrApi: Array<string | OASDocument>, { oasClass }?: {
|
|
98
|
+
oasClass?: typeof Oas;
|
|
99
|
+
}): Promise<Oas>;
|
|
93
100
|
|
|
94
|
-
export { HttpMethods, Oas, type SchemaObject, type contentType, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
|
101
|
+
export { HttpMethods, Oas, type SchemaObject, type contentType, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, merge, parse };
|
package/dist/index.d.ts
CHANGED
|
@@ -89,6 +89,13 @@ declare function isDiscriminator(obj?: any): obj is SchemaObject$1 & {
|
|
|
89
89
|
*/
|
|
90
90
|
declare function isRequired(schema?: SchemaObject$1): boolean;
|
|
91
91
|
declare function isOptional(schema?: SchemaObject$1): boolean;
|
|
92
|
-
declare function parse(pathOrApi: string | OASDocument, oasClass
|
|
92
|
+
declare function parse(pathOrApi: string | OASDocument, { oasClass, canBundle, enablePaths }?: {
|
|
93
|
+
oasClass?: typeof Oas;
|
|
94
|
+
canBundle?: boolean;
|
|
95
|
+
enablePaths?: boolean;
|
|
96
|
+
}): Promise<Oas>;
|
|
97
|
+
declare function merge(pathOrApi: Array<string | OASDocument>, { oasClass }?: {
|
|
98
|
+
oasClass?: typeof Oas;
|
|
99
|
+
}): Promise<Oas>;
|
|
93
100
|
|
|
94
|
-
export { HttpMethods, Oas, type SchemaObject, type contentType, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
|
101
|
+
export { HttpMethods, Oas, type SchemaObject, type contentType, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, merge, parse };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { matchesMimeType } from 'oas/utils';
|
|
2
2
|
export { findSchemaDefinition, matchesMimeType } from 'oas/utils';
|
|
3
3
|
import { isRef } from 'oas/types';
|
|
4
|
-
import { isPlainObject } from 'remeda';
|
|
4
|
+
import { isPlainObject, mergeDeep } from 'remeda';
|
|
5
5
|
import { loadConfig, bundle } from '@redocly/openapi-core';
|
|
6
6
|
import OASNormalize from 'oas-normalize';
|
|
7
7
|
import swagger2openapi from 'swagger2openapi';
|
|
@@ -293,14 +293,14 @@ function isRequired(schema) {
|
|
|
293
293
|
function isOptional(schema) {
|
|
294
294
|
return !isRequired(schema);
|
|
295
295
|
}
|
|
296
|
-
async function parse(pathOrApi, oasClass = Oas) {
|
|
297
|
-
if (typeof pathOrApi === "string") {
|
|
296
|
+
async function parse(pathOrApi, { oasClass = Oas, canBundle = true, enablePaths = true } = {}) {
|
|
297
|
+
if (typeof pathOrApi === "string" && canBundle) {
|
|
298
298
|
const config = await loadConfig();
|
|
299
299
|
const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi });
|
|
300
300
|
return parse(bundleResults.bundle.parsed);
|
|
301
301
|
}
|
|
302
302
|
const oasNormalize = new OASNormalize(pathOrApi, {
|
|
303
|
-
enablePaths
|
|
303
|
+
enablePaths,
|
|
304
304
|
colorizeErrors: true
|
|
305
305
|
});
|
|
306
306
|
const document = await oasNormalize.load();
|
|
@@ -312,7 +312,30 @@ async function parse(pathOrApi, oasClass = Oas) {
|
|
|
312
312
|
}
|
|
313
313
|
return new oasClass({ oas: document });
|
|
314
314
|
}
|
|
315
|
+
async function merge(pathOrApi, { oasClass = Oas } = {}) {
|
|
316
|
+
const instances = await Promise.all(pathOrApi.map((p) => parse(p, { oasClass, enablePaths: false, canBundle: false })));
|
|
317
|
+
if (instances.length === 0) {
|
|
318
|
+
throw new Error("No OAS instances provided for merging.");
|
|
319
|
+
}
|
|
320
|
+
const merged = instances.reduce(
|
|
321
|
+
(acc, current) => {
|
|
322
|
+
return mergeDeep(acc, current.document);
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
openapi: "3.0.0",
|
|
326
|
+
info: {
|
|
327
|
+
title: "Merged API",
|
|
328
|
+
version: "1.0.0"
|
|
329
|
+
},
|
|
330
|
+
paths: {},
|
|
331
|
+
components: {
|
|
332
|
+
schemas: {}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
);
|
|
336
|
+
return parse(merged, { oasClass });
|
|
337
|
+
}
|
|
315
338
|
|
|
316
|
-
export { HttpMethods, Oas, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
|
339
|
+
export { HttpMethods, Oas, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, merge, parse };
|
|
317
340
|
//# sourceMappingURL=index.js.map
|
|
318
341
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["schema","OASNormalize"],"mappings":";;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;AAAA,EACzB,GAAK,EAAA,KAAA;AAAA,EACL,IAAM,EAAA,MAAA;AAAA,EACN,GAAK,EAAA,KAAA;AAAA,EACL,KAAO,EAAA,OAAA;AAAA,EACP,MAAQ,E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Q,CAAG,EAAA;AACjC,IAAA,MAAM,EAAE,OAAQ,EAAA,GAAI,MAAM,eAAA,CAAgB,WAAW,QAAU,EAAA;AAAA,MAC7D,OAAS,EAAA;AAAA,KACV,CAAA;AAED,IAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,SAAwB,CAAA;AAAA;AAGrD,EAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,UAAU,CAAA;AACvC","file":"index.js","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import jsonpointer from 'jsonpointer'\nimport BaseOas from 'oas'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { matchesMimeType } from 'oas/utils'\nimport OASNormalize from 'oas-normalize'\nimport type { OpenAPIV3 } from 'openapi-types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport { isDiscriminator, isReference } from './utils.ts'\n\ntype Options = {\n contentType?: contentType\n discriminator?: 'strict' | 'inherit'\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {\n discriminator: 'strict',\n }\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n }\n\n setOptions(options: Options) {\n this.#options = options\n }\n\n get options(): Options {\n return this.#options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const { mapping = {}, propertyName } = schema.discriminator\n\n if (this.#options.discriminator === 'inherit') {\n Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {\n if (mappingValue) {\n const childSchema = this.get(mappingValue)\n if (!childSchema.properties) {\n childSchema.properties = {}\n }\n\n const property = childSchema.properties[propertyName] as SchemaObject\n\n if (childSchema.properties) {\n childSchema.properties[propertyName] = {\n ...(childSchema.properties ? childSchema.properties[propertyName] : {}),\n enum: [...(property?.enum?.filter((value) => value !== mappingKey) ?? []), mappingKey],\n }\n\n childSchema.required = [...(childSchema.required ?? []), propertyName]\n\n this.set(mappingValue, childSchema)\n }\n }\n })\n }\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n return oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oas = Oas): Promise<Oas> {\n if (typeof pathOrApi === 'string') {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths: true,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["schema","OASNormalize"],"mappings":";;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;A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A,EAAA,OAAO,KAAM,CAAA,MAAA,EAAQ,EAAE,QAAA,EAAU,CAAA;AACnC","file":"index.js","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import jsonpointer from 'jsonpointer'\nimport BaseOas from 'oas'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { matchesMimeType } from 'oas/utils'\nimport OASNormalize from 'oas-normalize'\nimport type { OpenAPIV3 } from 'openapi-types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport { isDiscriminator, isReference } from './utils.ts'\n\ntype Options = {\n contentType?: contentType\n discriminator?: 'strict' | 'inherit'\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {\n discriminator: 'strict',\n }\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n }\n\n setOptions(options: Options) {\n this.#options = options\n }\n\n get options(): Options {\n return this.#options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const { mapping = {}, propertyName } = schema.discriminator\n\n if (this.#options.discriminator === 'inherit') {\n Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {\n if (mappingValue) {\n const childSchema = this.get(mappingValue)\n if (!childSchema.properties) {\n childSchema.properties = {}\n }\n\n const property = childSchema.properties[propertyName] as SchemaObject\n\n if (childSchema.properties) {\n childSchema.properties[propertyName] = {\n ...(childSchema.properties ? childSchema.properties[propertyName] : {}),\n enum: [...(property?.enum?.filter((value) => value !== mappingKey) ?? []), mappingKey],\n }\n\n childSchema.required = [...(childSchema.required ?? []), propertyName]\n\n this.set(mappingValue, childSchema)\n }\n }\n })\n }\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n return oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject, mergeDeep } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(\n pathOrApi: string | OASDocument,\n { oasClass = Oas, canBundle = true, enablePaths = true }: { oasClass?: typeof Oas; canBundle?: boolean; enablePaths?: boolean } = {},\n): Promise<Oas> {\n if (typeof pathOrApi === 'string' && canBundle) {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n\nexport async function merge(pathOrApi: Array<string | OASDocument>, { oasClass = Oas }: { oasClass?: typeof Oas } = {}): Promise<Oas> {\n const instances = await Promise.all(pathOrApi.map((p) => parse(p, { oasClass, enablePaths: false, canBundle: false })))\n\n if (instances.length === 0) {\n throw new Error('No OAS instances provided for merging.')\n }\n\n const merged = instances.reduce(\n (acc, current) => {\n return mergeDeep(acc, current.document as OASDocument)\n },\n {\n openapi: '3.0.0',\n info: {\n title: 'Merged API',\n version: '1.0.0',\n },\n paths: {},\n components: {\n schemas: {},\n },\n } as any,\n )\n\n return parse(merged, { oasClass })\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/oas",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.15.0",
|
|
4
4
|
"description": "OpenAPI Specification (OAS) utilities and helpers for Kubb, providing parsing, normalization, and manipulation of OpenAPI/Swagger schemas.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openapi",
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
"@types/swagger2openapi": "^7.0.4",
|
|
69
69
|
"tsup": "^8.5.0",
|
|
70
70
|
"typescript": "^5.8.3",
|
|
71
|
-
"@kubb/config-ts": "3.
|
|
72
|
-
"@kubb/config-tsup": "3.
|
|
71
|
+
"@kubb/config-ts": "3.15.0",
|
|
72
|
+
"@kubb/config-tsup": "3.15.0"
|
|
73
73
|
},
|
|
74
74
|
"engines": {
|
|
75
75
|
"node": ">=20"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`utils > merge of 2 oas documents 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"components": {
|
|
6
|
+
"schemas": {
|
|
7
|
+
"Point": {
|
|
8
|
+
"properties": {
|
|
9
|
+
"x": {
|
|
10
|
+
"type": "number",
|
|
11
|
+
},
|
|
12
|
+
"y": {
|
|
13
|
+
"type": "number",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
"required": [
|
|
17
|
+
"x",
|
|
18
|
+
"y",
|
|
19
|
+
],
|
|
20
|
+
"type": "object",
|
|
21
|
+
},
|
|
22
|
+
"Square": {
|
|
23
|
+
"properties": {
|
|
24
|
+
"size": {
|
|
25
|
+
"type": "number",
|
|
26
|
+
},
|
|
27
|
+
"topLeft": {
|
|
28
|
+
"$ref": "#/components/schemas/Point",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
"required": [
|
|
32
|
+
"topLeft",
|
|
33
|
+
"size",
|
|
34
|
+
],
|
|
35
|
+
"type": "object",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
"info": {
|
|
40
|
+
"title": "Shapes",
|
|
41
|
+
"version": "1.0.0",
|
|
42
|
+
},
|
|
43
|
+
"openapi": "3.0.0",
|
|
44
|
+
"paths": {},
|
|
45
|
+
}
|
|
46
|
+
`;
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from './types.ts'
|
|
2
2
|
export { findSchemaDefinition, matchesMimeType } from 'oas/utils'
|
|
3
|
-
export { isRequired, isOptional, isDiscriminator, isNullable, isReference, isParameterObject, isOpenApiV3_1Document, parse } from './utils.ts'
|
|
3
|
+
export { isRequired, isOptional, isDiscriminator, isNullable, isReference, isParameterObject, isOpenApiV3_1Document, merge, parse } from './utils.ts'
|
|
4
4
|
export { Oas } from './Oas.ts'
|
|
5
5
|
export type { Infer, Model, RequestParams, Response } from './infer/index.ts'
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { merge, parse } from './utils.ts'
|
|
2
|
+
|
|
3
|
+
describe('utils', () => {
|
|
4
|
+
test('merge of 2 oas documents', async () => {
|
|
5
|
+
const documents = [
|
|
6
|
+
`openapi: 3.0.0
|
|
7
|
+
info:
|
|
8
|
+
title: Swagger Petstore
|
|
9
|
+
version: 1.0.0
|
|
10
|
+
components:
|
|
11
|
+
schemas:
|
|
12
|
+
Point:
|
|
13
|
+
type: object
|
|
14
|
+
properties:
|
|
15
|
+
x:
|
|
16
|
+
type: number
|
|
17
|
+
y:
|
|
18
|
+
type: number
|
|
19
|
+
required: [x, y]
|
|
20
|
+
`,
|
|
21
|
+
`openapi: 3.0.0
|
|
22
|
+
info:
|
|
23
|
+
title: Shapes
|
|
24
|
+
version: 1.0.0
|
|
25
|
+
paths: {}
|
|
26
|
+
components:
|
|
27
|
+
schemas:
|
|
28
|
+
Square:
|
|
29
|
+
type: object
|
|
30
|
+
properties:
|
|
31
|
+
topLeft:
|
|
32
|
+
$ref: '#/components/schemas/Point'
|
|
33
|
+
size:
|
|
34
|
+
type: number
|
|
35
|
+
required: [topLeft, size]`,
|
|
36
|
+
]
|
|
37
|
+
const oas = await merge(documents)
|
|
38
|
+
|
|
39
|
+
expect(oas).toBeDefined()
|
|
40
|
+
expect(oas.document).toMatchSnapshot()
|
|
41
|
+
expect(oas.api?.info.title).toBe('Shapes')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('parse a simple oas document', async () => {
|
|
45
|
+
const oas = await parse(
|
|
46
|
+
`openapi: 3.0.0
|
|
47
|
+
info:
|
|
48
|
+
title: Swagger Petstore
|
|
49
|
+
version: 1.0.0
|
|
50
|
+
components:
|
|
51
|
+
schemas:
|
|
52
|
+
Point:
|
|
53
|
+
type: object
|
|
54
|
+
properties:
|
|
55
|
+
x:
|
|
56
|
+
type: number
|
|
57
|
+
y:
|
|
58
|
+
type: number
|
|
59
|
+
required: [x, y]
|
|
60
|
+
`,
|
|
61
|
+
{ canBundle: false },
|
|
62
|
+
)
|
|
63
|
+
expect(oas.api?.info.title).toBe('Swagger Petstore')
|
|
64
|
+
})
|
|
65
|
+
})
|
package/src/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isRef, isSchema } from 'oas/types'
|
|
2
|
-
import { isPlainObject } from 'remeda'
|
|
2
|
+
import { isPlainObject, mergeDeep } from 'remeda'
|
|
3
3
|
|
|
4
4
|
import { bundle, loadConfig } from '@redocly/openapi-core'
|
|
5
5
|
import OASNormalize from 'oas-normalize'
|
|
@@ -76,8 +76,11 @@ export function isOptional(schema?: SchemaObject): boolean {
|
|
|
76
76
|
return !isRequired(schema)
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
export async function parse(
|
|
80
|
-
|
|
79
|
+
export async function parse(
|
|
80
|
+
pathOrApi: string | OASDocument,
|
|
81
|
+
{ oasClass = Oas, canBundle = true, enablePaths = true }: { oasClass?: typeof Oas; canBundle?: boolean; enablePaths?: boolean } = {},
|
|
82
|
+
): Promise<Oas> {
|
|
83
|
+
if (typeof pathOrApi === 'string' && canBundle) {
|
|
81
84
|
// resolve external refs
|
|
82
85
|
const config = await loadConfig()
|
|
83
86
|
const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })
|
|
@@ -86,7 +89,7 @@ export async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oa
|
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
const oasNormalize = new OASNormalize(pathOrApi, {
|
|
89
|
-
enablePaths
|
|
92
|
+
enablePaths,
|
|
90
93
|
colorizeErrors: true,
|
|
91
94
|
})
|
|
92
95
|
const document = (await oasNormalize.load()) as OpenAPI.Document
|
|
@@ -101,3 +104,30 @@ export async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oa
|
|
|
101
104
|
|
|
102
105
|
return new oasClass({ oas: document })
|
|
103
106
|
}
|
|
107
|
+
|
|
108
|
+
export async function merge(pathOrApi: Array<string | OASDocument>, { oasClass = Oas }: { oasClass?: typeof Oas } = {}): Promise<Oas> {
|
|
109
|
+
const instances = await Promise.all(pathOrApi.map((p) => parse(p, { oasClass, enablePaths: false, canBundle: false })))
|
|
110
|
+
|
|
111
|
+
if (instances.length === 0) {
|
|
112
|
+
throw new Error('No OAS instances provided for merging.')
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const merged = instances.reduce(
|
|
116
|
+
(acc, current) => {
|
|
117
|
+
return mergeDeep(acc, current.document as OASDocument)
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
openapi: '3.0.0',
|
|
121
|
+
info: {
|
|
122
|
+
title: 'Merged API',
|
|
123
|
+
version: '1.0.0',
|
|
124
|
+
},
|
|
125
|
+
paths: {},
|
|
126
|
+
components: {
|
|
127
|
+
schemas: {},
|
|
128
|
+
},
|
|
129
|
+
} as any,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
return parse(merged, { oasClass })
|
|
133
|
+
}
|