@kubb/oas 3.10.2 → 3.10.4
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 +36 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -3
- package/dist/index.d.ts +31 -3
- package/dist/index.js +36 -31
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/Oas.ts +37 -36
- package/src/index.ts +1 -1
- package/src/utils.ts +29 -1
package/dist/index.cjs
CHANGED
|
@@ -55,6 +55,10 @@ var Oas = class extends BaseOas__default.default {
|
|
|
55
55
|
}
|
|
56
56
|
return current;
|
|
57
57
|
}
|
|
58
|
+
getKey($ref) {
|
|
59
|
+
const key = $ref.split("/").pop();
|
|
60
|
+
return key === "" ? void 0 : key;
|
|
61
|
+
}
|
|
58
62
|
set($ref, value) {
|
|
59
63
|
$ref = $ref.trim();
|
|
60
64
|
if ($ref === "") {
|
|
@@ -65,39 +69,37 @@ var Oas = class extends BaseOas__default.default {
|
|
|
65
69
|
jsonpointer__default.default.set(this.api, $ref, value);
|
|
66
70
|
}
|
|
67
71
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
getDiscriminator(schema) {
|
|
73
|
+
if (!isDiscriminator(schema)) {
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
const mapping = schema.discriminator.mapping || {};
|
|
77
|
+
if (schema.oneOf) {
|
|
78
|
+
schema.oneOf.forEach((schema2) => {
|
|
79
|
+
if (isReference(schema2)) {
|
|
80
|
+
const key = this.getKey(schema2.$ref);
|
|
81
|
+
if (key && !Object.values(mapping).includes(schema2.$ref)) {
|
|
82
|
+
mapping[key] = schema2.$ref;
|
|
83
|
+
}
|
|
75
84
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
if (!childSchema.properties) {
|
|
85
|
-
childSchema.properties = {};
|
|
86
|
-
}
|
|
87
|
-
const property = childSchema.properties[propertyName];
|
|
88
|
-
if (childSchema.properties) {
|
|
89
|
-
childSchema.properties[propertyName] = {
|
|
90
|
-
...childSchema.properties ? childSchema.properties[propertyName] : {},
|
|
91
|
-
enum: [...property?.enum?.filter((value) => value !== mappingKey) ?? [], mappingKey]
|
|
92
|
-
};
|
|
93
|
-
childSchema.required = [...childSchema.required ?? [], propertyName];
|
|
94
|
-
this.set(mappingValue, childSchema);
|
|
95
|
-
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (schema.anyOf) {
|
|
88
|
+
schema.anyOf.forEach((schema2) => {
|
|
89
|
+
if (isReference(schema2)) {
|
|
90
|
+
const key = this.getKey(schema2.$ref);
|
|
91
|
+
if (key && !Object.values(mapping).includes(schema2.$ref)) {
|
|
92
|
+
mapping[key] = schema2.$ref;
|
|
96
93
|
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
...schema.discriminator,
|
|
99
|
+
mapping
|
|
100
|
+
};
|
|
100
101
|
}
|
|
102
|
+
// TODO add better typing
|
|
101
103
|
dereferenceWithRef(schema) {
|
|
102
104
|
if (isReference(schema)) {
|
|
103
105
|
return {
|
|
@@ -251,6 +253,9 @@ function isNullable(schema) {
|
|
|
251
253
|
function isReference(obj) {
|
|
252
254
|
return !!obj && types.isRef(obj);
|
|
253
255
|
}
|
|
256
|
+
function isDiscriminator(obj) {
|
|
257
|
+
return !!obj && obj?.["discriminator"] && typeof obj.discriminator !== "string";
|
|
258
|
+
}
|
|
254
259
|
function isRequired(schema) {
|
|
255
260
|
if (!schema) {
|
|
256
261
|
return false;
|
|
@@ -290,6 +295,7 @@ Object.defineProperty(exports, "matchesMimeType", {
|
|
|
290
295
|
});
|
|
291
296
|
exports.HttpMethods = HttpMethods;
|
|
292
297
|
exports.Oas = Oas;
|
|
298
|
+
exports.isDiscriminator = isDiscriminator;
|
|
293
299
|
exports.isNullable = isNullable;
|
|
294
300
|
exports.isOpenApiV3_1Document = isOpenApiV3_1Document;
|
|
295
301
|
exports.isOptional = isOptional;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["BaseOas","jsonpointer","matchesMimeType","schema","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;ACPa,IAAA,GAAA,GAAN,cAAwCA,wBAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAEjB,YAAY,EAAE,GAAA,EAAK,MAA2D,EAAA,OAAA,GAAmB,EAAI,EAAA;AACnG,IAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AAC3B,MAAM,GAAA,GAAA,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA;AAGtB,IAAA,KAAA,CAAM,KAAoB,IAAI,CAAA;AAE9B,IAAA,IAAA,CAAK,QAAW,GAAA,GAAA;AAChB,IAAA,IAAA,CAAK,QAAW,GAAA,OAAA;AAAA;AAClB,EAEA,IAAI,IAAc,EAAA;AAChB,IAAA,MAAM,OAAU,GAAA,IAAA;AAChB,IAAA,IAAA,GAAO,KAAK,IAAK,EAAA;AACjB,IAAA,IAAI,SAAS,EAAI,EAA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,CAAA,MAAA,CAAO,MAAM,CAAA;AAAA;AAG1C,EAAM,MAAA,YAAA,GAAe,IAAIJ,6BAAAA,CAAa,SAAW,EAAA;AAAA,IAC/C,WAAa,EAAA,IAAA;AAAA,IACb,cAAgB,EAAA;AAAA,GACjB,CAAA;AACD,EAAM,MAAA,QAAA,GAAY,MAAM,YAAA,CAAa,IAAK,EAAA;AAE1C,EAAI,IAAA,mBAAA,CAAoB,QAAQ,CAAG,EAAA;AACjC,IAAA,MAAM,EAAE,OAAQ,EAAA,GAAI,MAAMK,gCAAA,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.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 BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport { matchesMimeType } from 'oas/utils'\n\nimport jsonpointer from 'jsonpointer'\n\nimport { isReference } from './utils.ts'\n\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport type { OasTypes, OpenAPIV3 } from './index.ts'\nimport type { contentType } from './types.ts'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\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 this.#options = 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 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 resolveDiscriminators(): void {\n const schemas = (this.api.components?.schemas || {}) as Record<string, OasTypes.SchemaObject>\n\n Object.entries(schemas).forEach(([_key, schemaObject]) => {\n if ('discriminator' in schemaObject && typeof schemaObject.discriminator !== 'string') {\n const { mapping = {}, propertyName } = (schemaObject.discriminator || {}) as OpenAPIV3.DiscriminatorObject\n\n if (!schemaObject.properties?.[propertyName]) {\n schemaObject.properties = {}\n }\n\n const enums: string[] = (schemaObject.properties[propertyName] as NonNullable<OpenAPIV3.SchemaObject>).enum || []\n\n schemaObject.properties[propertyName] = {\n ...schemaObject.properties[propertyName],\n enum: [...Object.keys(mapping), ...enums],\n }\n\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 }\n\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 = 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 await 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\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\nexport function isReference(obj?: unknown): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\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"],"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;ACNa,IAAA,GAAA,GAAN,cAAwCA,wBAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAEjB,YAAY,EAAE,GAAA,EAAK,MAA2D,EAAA,OAAA,GAAmB,EAAI,EAAA;AACnG,IAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AAC3B,MAAM,GAAA,GAAA,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA;AAGtB,IAAA,KAAA,CAAM,KAAoB,IAAI,CAAA;AAE9B,IAAA,IAAA,CAAK,QAAW,GAAA,GAAA;AAChB,IAAA,IAAA,CAAK,QAAW,GAAA,OAAA;AAAA;AAClB,EAEA,IAAI,IAAc,EAAA;AAChB,IAAA,MAAM,OAAU,GAAA,IAAA;AAChB,IAAA,IAAA,GAAO,KAAK,IAAK,EAAA;AACjB,IAAA,IAAI,SAAS,EAAI,EAAA;AACf,MAAO,OAAA,KAAA;AAAA;AAET,IAAI,IAAA,IAAA,CAAK,UAAW,CAAA,GAAG,CAAG,EAAA;AACxB,MAAA,IAA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oB,QAAQ,CAAG,EAAA;AACjC,IAAA,MAAM,EAAE,OAAQ,EAAA,GAAI,MAAMK,gCAAA,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.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 BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport { matchesMimeType } from 'oas/utils'\n\nimport jsonpointer from 'jsonpointer'\n\nimport { isDiscriminator, isReference } from './utils.ts'\n\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport type { OpenAPIV3 } from 'openapi-types'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\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 this.#options = 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 = schema.discriminator.mapping || {}\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\n if (key && !Object.values(mapping).includes(schema.$ref)) {\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\n if (key && !Object.values(mapping).includes(schema.$ref)) {\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 = 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 await 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"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -40,8 +40,9 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
40
40
|
user?: User;
|
|
41
41
|
}, options?: Options);
|
|
42
42
|
get($ref: string): any;
|
|
43
|
+
getKey($ref: string): string | undefined;
|
|
43
44
|
set($ref: string, value: unknown): false | undefined;
|
|
44
|
-
|
|
45
|
+
getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined;
|
|
45
46
|
dereferenceWithRef(schema?: unknown): any;
|
|
46
47
|
getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
|
|
47
48
|
getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
|
|
@@ -51,12 +52,39 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
51
52
|
|
|
52
53
|
declare function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document;
|
|
53
54
|
declare function isParameterObject(obj: ParameterObject | SchemaObject$1): obj is ParameterObject;
|
|
55
|
+
/**
|
|
56
|
+
* Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.
|
|
57
|
+
*
|
|
58
|
+
* @param schema - The schema object to check.
|
|
59
|
+
* @returns `true` if the schema is marked as nullable; otherwise, `false`.
|
|
60
|
+
*/
|
|
54
61
|
declare function isNullable(schema?: SchemaObject$1 & {
|
|
55
62
|
'x-nullable'?: boolean;
|
|
56
63
|
}): boolean;
|
|
57
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Determines if the given object is an OpenAPI ReferenceObject.
|
|
66
|
+
*
|
|
67
|
+
* @returns True if {@link obj} is a ReferenceObject; otherwise, false.
|
|
68
|
+
*/
|
|
69
|
+
declare function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
|
|
70
|
+
/**
|
|
71
|
+
* Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.
|
|
72
|
+
*
|
|
73
|
+
* @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.
|
|
74
|
+
*/
|
|
75
|
+
declare function isDiscriminator(obj?: any): obj is SchemaObject$1 & {
|
|
76
|
+
discriminator: OpenAPIV3.DiscriminatorObject;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Determines whether a schema is required.
|
|
80
|
+
*
|
|
81
|
+
* Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.
|
|
82
|
+
*
|
|
83
|
+
* @param schema - The schema object to check.
|
|
84
|
+
* @returns True if the schema is required; otherwise, false.
|
|
85
|
+
*/
|
|
58
86
|
declare function isRequired(schema?: SchemaObject$1): boolean;
|
|
59
87
|
declare function isOptional(schema?: SchemaObject$1): boolean;
|
|
60
88
|
declare function parse(pathOrApi: string | OASDocument, oasClass?: typeof Oas): Promise<Oas>;
|
|
61
89
|
|
|
62
|
-
export { HttpMethods, Oas, type SchemaObject, type contentType, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
|
90
|
+
export { HttpMethods, Oas, type SchemaObject, type contentType, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
package/dist/index.d.ts
CHANGED
|
@@ -40,8 +40,9 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
40
40
|
user?: User;
|
|
41
41
|
}, options?: Options);
|
|
42
42
|
get($ref: string): any;
|
|
43
|
+
getKey($ref: string): string | undefined;
|
|
43
44
|
set($ref: string, value: unknown): false | undefined;
|
|
44
|
-
|
|
45
|
+
getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined;
|
|
45
46
|
dereferenceWithRef(schema?: unknown): any;
|
|
46
47
|
getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
|
|
47
48
|
getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
|
|
@@ -51,12 +52,39 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
51
52
|
|
|
52
53
|
declare function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document;
|
|
53
54
|
declare function isParameterObject(obj: ParameterObject | SchemaObject$1): obj is ParameterObject;
|
|
55
|
+
/**
|
|
56
|
+
* Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.
|
|
57
|
+
*
|
|
58
|
+
* @param schema - The schema object to check.
|
|
59
|
+
* @returns `true` if the schema is marked as nullable; otherwise, `false`.
|
|
60
|
+
*/
|
|
54
61
|
declare function isNullable(schema?: SchemaObject$1 & {
|
|
55
62
|
'x-nullable'?: boolean;
|
|
56
63
|
}): boolean;
|
|
57
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Determines if the given object is an OpenAPI ReferenceObject.
|
|
66
|
+
*
|
|
67
|
+
* @returns True if {@link obj} is a ReferenceObject; otherwise, false.
|
|
68
|
+
*/
|
|
69
|
+
declare function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
|
|
70
|
+
/**
|
|
71
|
+
* Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.
|
|
72
|
+
*
|
|
73
|
+
* @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.
|
|
74
|
+
*/
|
|
75
|
+
declare function isDiscriminator(obj?: any): obj is SchemaObject$1 & {
|
|
76
|
+
discriminator: OpenAPIV3.DiscriminatorObject;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Determines whether a schema is required.
|
|
80
|
+
*
|
|
81
|
+
* Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.
|
|
82
|
+
*
|
|
83
|
+
* @param schema - The schema object to check.
|
|
84
|
+
* @returns True if the schema is required; otherwise, false.
|
|
85
|
+
*/
|
|
58
86
|
declare function isRequired(schema?: SchemaObject$1): boolean;
|
|
59
87
|
declare function isOptional(schema?: SchemaObject$1): boolean;
|
|
60
88
|
declare function parse(pathOrApi: string | OASDocument, oasClass?: typeof Oas): Promise<Oas>;
|
|
61
89
|
|
|
62
|
-
export { HttpMethods, Oas, type SchemaObject, type contentType, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
|
90
|
+
export { HttpMethods, Oas, type SchemaObject, type contentType, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
package/dist/index.js
CHANGED
|
@@ -47,6 +47,10 @@ var Oas = class extends BaseOas {
|
|
|
47
47
|
}
|
|
48
48
|
return current;
|
|
49
49
|
}
|
|
50
|
+
getKey($ref) {
|
|
51
|
+
const key = $ref.split("/").pop();
|
|
52
|
+
return key === "" ? void 0 : key;
|
|
53
|
+
}
|
|
50
54
|
set($ref, value) {
|
|
51
55
|
$ref = $ref.trim();
|
|
52
56
|
if ($ref === "") {
|
|
@@ -57,39 +61,37 @@ var Oas = class extends BaseOas {
|
|
|
57
61
|
jsonpointer.set(this.api, $ref, value);
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
getDiscriminator(schema) {
|
|
65
|
+
if (!isDiscriminator(schema)) {
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
const mapping = schema.discriminator.mapping || {};
|
|
69
|
+
if (schema.oneOf) {
|
|
70
|
+
schema.oneOf.forEach((schema2) => {
|
|
71
|
+
if (isReference(schema2)) {
|
|
72
|
+
const key = this.getKey(schema2.$ref);
|
|
73
|
+
if (key && !Object.values(mapping).includes(schema2.$ref)) {
|
|
74
|
+
mapping[key] = schema2.$ref;
|
|
75
|
+
}
|
|
67
76
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
if (!childSchema.properties) {
|
|
77
|
-
childSchema.properties = {};
|
|
78
|
-
}
|
|
79
|
-
const property = childSchema.properties[propertyName];
|
|
80
|
-
if (childSchema.properties) {
|
|
81
|
-
childSchema.properties[propertyName] = {
|
|
82
|
-
...childSchema.properties ? childSchema.properties[propertyName] : {},
|
|
83
|
-
enum: [...property?.enum?.filter((value) => value !== mappingKey) ?? [], mappingKey]
|
|
84
|
-
};
|
|
85
|
-
childSchema.required = [...childSchema.required ?? [], propertyName];
|
|
86
|
-
this.set(mappingValue, childSchema);
|
|
87
|
-
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (schema.anyOf) {
|
|
80
|
+
schema.anyOf.forEach((schema2) => {
|
|
81
|
+
if (isReference(schema2)) {
|
|
82
|
+
const key = this.getKey(schema2.$ref);
|
|
83
|
+
if (key && !Object.values(mapping).includes(schema2.$ref)) {
|
|
84
|
+
mapping[key] = schema2.$ref;
|
|
88
85
|
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
...schema.discriminator,
|
|
91
|
+
mapping
|
|
92
|
+
};
|
|
92
93
|
}
|
|
94
|
+
// TODO add better typing
|
|
93
95
|
dereferenceWithRef(schema) {
|
|
94
96
|
if (isReference(schema)) {
|
|
95
97
|
return {
|
|
@@ -243,6 +245,9 @@ function isNullable(schema) {
|
|
|
243
245
|
function isReference(obj) {
|
|
244
246
|
return !!obj && isRef(obj);
|
|
245
247
|
}
|
|
248
|
+
function isDiscriminator(obj) {
|
|
249
|
+
return !!obj && obj?.["discriminator"] && typeof obj.discriminator !== "string";
|
|
250
|
+
}
|
|
246
251
|
function isRequired(schema) {
|
|
247
252
|
if (!schema) {
|
|
248
253
|
return false;
|
|
@@ -272,6 +277,6 @@ async function parse(pathOrApi, oasClass = Oas) {
|
|
|
272
277
|
return new oasClass({ oas: document });
|
|
273
278
|
}
|
|
274
279
|
|
|
275
|
-
export { HttpMethods, Oas, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
|
280
|
+
export { HttpMethods, Oas, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
|
|
276
281
|
//# sourceMappingURL=index.js.map
|
|
277
282
|
//# 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,EAAA,QAAA;AAAA,EACR,IAAM,EAAA,MAAA;AAAA,EACN,OAAS,EAAA,SAAA;AAAA,EACT,KAAO,EAAA;AACT;ACPa,IAAA,GAAA,GAAN,cAAwC,OAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAEjB,YAAY,EAAE,GAAA,EAAK,MAA2D,EAAA,OAAA,GAAmB,EAAI,EAAA;AACnG,IAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AAC3B,MAAM,GAAA,GAAA,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA;AAGtB,IAAA,KAAA,CAAM,KAAoB,IAAI,CAAA;AAE9B,IAAA,IAAA,CAAK,QAAW,GAAA,GAAA;AAChB,IAAA,IAAA,CAAK,QAAW,GAAA,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AAA,GAAgB,MAAM,MAAO,CAAA,EAAE,KAAK,SAAW,EAAA,MAAA,EAAQ,IAAM,EAAA,SAAA,EAAW,CAAA;AAE9E,IAAO,OAAA,KAAA,CAAM,aAAc,CAAA,MAAA,CAAO,MAAM,CAAA;AAAA;AAG1C,EAAM,MAAA,YAAA,GAAe,IAAIC,YAAAA,CAAa,SAAW,EAAA;AAAA,IAC/C,WAAa,EAAA,IAAA;AAAA,IACb,cAAgB,EAAA;AAAA,GACjB,CAAA;AACD,EAAM,MAAA,QAAA,GAAY,MAAM,YAAA,CAAa,IAAK,EAAA;AAE1C,EAAI,IAAA,mBAAA,CAAoB,QAAQ,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 BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport { matchesMimeType } from 'oas/utils'\n\nimport jsonpointer from 'jsonpointer'\n\nimport { isReference } from './utils.ts'\n\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport type { OasTypes, OpenAPIV3 } from './index.ts'\nimport type { contentType } from './types.ts'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\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 this.#options = 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 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 resolveDiscriminators(): void {\n const schemas = (this.api.components?.schemas || {}) as Record<string, OasTypes.SchemaObject>\n\n Object.entries(schemas).forEach(([_key, schemaObject]) => {\n if ('discriminator' in schemaObject && typeof schemaObject.discriminator !== 'string') {\n const { mapping = {}, propertyName } = (schemaObject.discriminator || {}) as OpenAPIV3.DiscriminatorObject\n\n if (!schemaObject.properties?.[propertyName]) {\n schemaObject.properties = {}\n }\n\n const enums: string[] = (schemaObject.properties[propertyName] as NonNullable<OpenAPIV3.SchemaObject>).enum || []\n\n schemaObject.properties[propertyName] = {\n ...schemaObject.properties[propertyName],\n enum: [...Object.keys(mapping), ...enums],\n }\n\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 }\n\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 = 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 await 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\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\nexport function isReference(obj?: unknown): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\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;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;ACNa,IAAA,GAAA,GAAN,cAAwC,OAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAEjB,YAAY,EAAE,GAAA,EAAK,MAA2D,EAAA,OAAA,GAAmB,EAAI,EAAA;AACnG,IAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AAC3B,MAAM,GAAA,GAAA,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA;AAGtB,IAAA,KAAA,CAAM,KAAoB,IAAI,CAAA;AAE9B,IAAA,IAAA,CAAK,QAAW,GAAA,GAAA;AAChB,IAAA,IAAA,CAAK,QAAW,GAAA,OAAA;AAAA;AAClB,EAEA,IAAI,IAAc,EAAA;AAChB,IAAA,MAAM,OAAU,GAAA,IAAA;AAChB,IAAA,IAAA,GAAO,KAAK,IAAK,EAAA;AACjB,IAAA,IAAI,SAAS,EAAI,EAAA;AACf,MAAO,OAAA,KAAA;AAAA;AAET,IAAI,IAAA,IAA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;AAE1C,EAAI,IAAA,mBAAA,CAAoB,QAAQ,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 BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport { matchesMimeType } from 'oas/utils'\n\nimport jsonpointer from 'jsonpointer'\n\nimport { isDiscriminator, isReference } from './utils.ts'\n\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport type { OpenAPIV3 } from 'openapi-types'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\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 this.#options = 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 = schema.discriminator.mapping || {}\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\n if (key && !Object.values(mapping).includes(schema.$ref)) {\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\n if (key && !Object.values(mapping).includes(schema.$ref)) {\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 = 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 await 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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/oas",
|
|
3
|
-
"version": "3.10.
|
|
3
|
+
"version": "3.10.4",
|
|
4
4
|
"description": "Oas helpers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
"expect-type": "^1.2.1",
|
|
66
66
|
"tsup": "^8.4.0",
|
|
67
67
|
"typescript": "^5.8.3",
|
|
68
|
-
"@kubb/config-ts": "3.10.
|
|
69
|
-
"@kubb/config-tsup": "3.10.
|
|
68
|
+
"@kubb/config-ts": "3.10.4",
|
|
69
|
+
"@kubb/config-tsup": "3.10.4"
|
|
70
70
|
},
|
|
71
71
|
"engines": {
|
|
72
72
|
"node": ">=20"
|
package/src/Oas.ts
CHANGED
|
@@ -4,12 +4,13 @@ import { matchesMimeType } from 'oas/utils'
|
|
|
4
4
|
|
|
5
5
|
import jsonpointer from 'jsonpointer'
|
|
6
6
|
|
|
7
|
-
import { isReference } from './utils.ts'
|
|
7
|
+
import { isDiscriminator, isReference } from './utils.ts'
|
|
8
8
|
|
|
9
9
|
import type { Operation } from 'oas/operation'
|
|
10
10
|
import type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'
|
|
11
|
-
import type { OasTypes
|
|
11
|
+
import type { OasTypes } from './index.ts'
|
|
12
12
|
import type { contentType } from './types.ts'
|
|
13
|
+
import type { OpenAPIV3 } from 'openapi-types'
|
|
13
14
|
|
|
14
15
|
type Options = {
|
|
15
16
|
contentType?: contentType
|
|
@@ -49,6 +50,10 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
49
50
|
return current
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
getKey($ref: string) {
|
|
54
|
+
const key = $ref.split('/').pop()
|
|
55
|
+
return key === '' ? undefined : key
|
|
56
|
+
}
|
|
52
57
|
set($ref: string, value: unknown) {
|
|
53
58
|
$ref = $ref.trim()
|
|
54
59
|
if ($ref === '') {
|
|
@@ -61,49 +66,45 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
61
66
|
}
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if ('discriminator' in schemaObject && typeof schemaObject.discriminator !== 'string') {
|
|
69
|
-
const { mapping = {}, propertyName } = (schemaObject.discriminator || {}) as OpenAPIV3.DiscriminatorObject
|
|
69
|
+
getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {
|
|
70
|
+
if (!isDiscriminator(schema)) {
|
|
71
|
+
return undefined
|
|
72
|
+
}
|
|
70
73
|
|
|
71
|
-
|
|
72
|
-
schemaObject.properties = {}
|
|
73
|
-
}
|
|
74
|
+
const mapping = schema.discriminator.mapping || {}
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
// loop over oneOf and add default mapping when none is defined
|
|
77
|
+
if (schema.oneOf) {
|
|
78
|
+
schema.oneOf.forEach((schema) => {
|
|
79
|
+
if (isReference(schema)) {
|
|
80
|
+
const key = this.getKey(schema.$ref)
|
|
76
81
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
82
|
+
if (key && !Object.values(mapping).includes(schema.$ref)) {
|
|
83
|
+
mapping[key] = schema.$ref
|
|
84
|
+
}
|
|
80
85
|
}
|
|
86
|
+
})
|
|
87
|
+
}
|
|
81
88
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
childSchema.properties = {}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const property = childSchema.properties[propertyName] as SchemaObject
|
|
90
|
-
|
|
91
|
-
if (childSchema.properties) {
|
|
92
|
-
childSchema.properties[propertyName] = {
|
|
93
|
-
...(childSchema.properties ? childSchema.properties[propertyName] : {}),
|
|
94
|
-
enum: [...(property?.enum?.filter((value) => value !== mappingKey) ?? []), mappingKey],
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
childSchema.required = [...(childSchema.required ?? []), propertyName]
|
|
89
|
+
if (schema.anyOf) {
|
|
90
|
+
schema.anyOf.forEach((schema) => {
|
|
91
|
+
if (isReference(schema)) {
|
|
92
|
+
const key = this.getKey(schema.$ref)
|
|
98
93
|
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
if (key && !Object.values(mapping).includes(schema.$ref)) {
|
|
95
|
+
mapping[key] = schema.$ref
|
|
101
96
|
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
...schema.discriminator,
|
|
103
|
+
mapping,
|
|
104
|
+
}
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
+
// TODO add better typing
|
|
107
108
|
dereferenceWithRef(schema?: unknown) {
|
|
108
109
|
if (isReference(schema)) {
|
|
109
110
|
return {
|
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, isNullable, isReference, isParameterObject, isOpenApiV3_1Document, parse } from './utils.ts'
|
|
3
|
+
export { isRequired, isOptional, isDiscriminator, isNullable, isReference, isParameterObject, isOpenApiV3_1Document, parse } from './utils.ts'
|
|
4
4
|
export { Oas } from './Oas.ts'
|
|
5
5
|
export type { Infer, Model, RequestParams, Response } from './infer/index.ts'
|
package/src/utils.ts
CHANGED
|
@@ -28,14 +28,42 @@ export function isParameterObject(obj: ParameterObject | SchemaObject): obj is P
|
|
|
28
28
|
return obj && 'in' in obj
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.
|
|
33
|
+
*
|
|
34
|
+
* @param schema - The schema object to check.
|
|
35
|
+
* @returns `true` if the schema is marked as nullable; otherwise, `false`.
|
|
36
|
+
*/
|
|
31
37
|
export function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {
|
|
32
38
|
return schema?.nullable ?? schema?.['x-nullable'] ?? false
|
|
33
39
|
}
|
|
34
40
|
|
|
35
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Determines if the given object is an OpenAPI ReferenceObject.
|
|
43
|
+
*
|
|
44
|
+
* @returns True if {@link obj} is a ReferenceObject; otherwise, false.
|
|
45
|
+
*/
|
|
46
|
+
export function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {
|
|
36
47
|
return !!obj && isRef(obj)
|
|
37
48
|
}
|
|
38
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.
|
|
52
|
+
*
|
|
53
|
+
* @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.
|
|
54
|
+
*/
|
|
55
|
+
export function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {
|
|
56
|
+
return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Determines whether a schema is required.
|
|
61
|
+
*
|
|
62
|
+
* Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.
|
|
63
|
+
*
|
|
64
|
+
* @param schema - The schema object to check.
|
|
65
|
+
* @returns True if the schema is required; otherwise, false.
|
|
66
|
+
*/
|
|
39
67
|
export function isRequired(schema?: SchemaObject): boolean {
|
|
40
68
|
if (!schema) {
|
|
41
69
|
return false
|