@kubb/oas 3.2.0 → 3.3.1

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.js CHANGED
@@ -1,5 +1,12 @@
1
- export { Oas, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired } from './chunk-X2CIM2ZZ.js';
1
+ import { matchesMimeType } from 'oas/utils';
2
2
  export { findSchemaDefinition, matchesMimeType } from 'oas/utils';
3
+ import { isRef } from 'oas/types';
4
+ import { isPlainObject } from 'remeda';
5
+ import { loadConfig, bundle } from '@redocly/openapi-core';
6
+ import OASNormalize from 'oas-normalize';
7
+ import swagger2openapi from 'swagger2openapi';
8
+ import BaseOas from 'oas';
9
+ import jsonpointer from 'jsonpointer';
3
10
 
4
11
  // src/types.ts
5
12
  var HttpMethods = {
@@ -12,7 +19,243 @@ var HttpMethods = {
12
19
  OPTIONS: "options",
13
20
  TRACE: "trace"
14
21
  };
22
+ var Oas = class extends BaseOas {
23
+ #options = {};
24
+ document = void 0;
25
+ constructor({ oas, user }, options = {}) {
26
+ if (typeof oas === "string") {
27
+ oas = JSON.parse(oas);
28
+ }
29
+ super(oas, user);
30
+ this.document = oas;
31
+ this.#options = options;
32
+ }
33
+ get($ref) {
34
+ const origRef = $ref;
35
+ $ref = $ref.trim();
36
+ if ($ref === "") {
37
+ return false;
38
+ }
39
+ if ($ref.startsWith("#")) {
40
+ $ref = globalThis.decodeURIComponent($ref.substring(1));
41
+ } else {
42
+ return null;
43
+ }
44
+ const current = jsonpointer.get(this.api, $ref);
45
+ if (!current) {
46
+ throw new Error(`Could not find a definition for ${origRef}.`);
47
+ }
48
+ return current;
49
+ }
50
+ set($ref, value) {
51
+ $ref = $ref.trim();
52
+ if ($ref === "") {
53
+ return false;
54
+ }
55
+ if ($ref.startsWith("#")) {
56
+ $ref = globalThis.decodeURIComponent($ref.substring(1));
57
+ jsonpointer.set(this.api, $ref, value);
58
+ }
59
+ }
60
+ resolveDiscriminators() {
61
+ const schemas = this.api.components?.schemas || {};
62
+ Object.entries(schemas).forEach(([key, schemaObject]) => {
63
+ if ("discriminator" in schemaObject) {
64
+ const { mapping = {}, propertyName } = schemaObject.discriminator || {};
65
+ Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {
66
+ if (mappingValue) {
67
+ const childSchema = this.get(mappingValue);
68
+ const property = childSchema.properties?.[propertyName];
69
+ if (property) {
70
+ childSchema.properties[propertyName] = {
71
+ ...childSchema.properties[propertyName],
72
+ enum: [...property?.enum?.filter((value) => value !== mappingKey) ?? [], mappingKey]
73
+ };
74
+ childSchema.required = [...childSchema.required ?? [], propertyName];
75
+ this.set(mappingValue, childSchema);
76
+ }
77
+ }
78
+ });
79
+ }
80
+ });
81
+ }
82
+ dereferenceWithRef(schema) {
83
+ if (isReference(schema)) {
84
+ return {
85
+ ...this.get(schema.$ref),
86
+ $ref: schema.$ref
87
+ };
88
+ }
89
+ return schema;
90
+ }
91
+ /**
92
+ * Oas does not have a getResponseBody(contentType)
93
+ */
94
+ #getResponseBodyFactory(responseBody) {
95
+ function hasResponseBody(res = responseBody) {
96
+ return !!res;
97
+ }
98
+ return (contentType) => {
99
+ if (!hasResponseBody(responseBody)) {
100
+ return false;
101
+ }
102
+ if (isReference(responseBody)) {
103
+ return false;
104
+ }
105
+ if (!responseBody.content) {
106
+ return false;
107
+ }
108
+ if (contentType) {
109
+ if (!(contentType in responseBody.content)) {
110
+ return false;
111
+ }
112
+ return responseBody.content[contentType];
113
+ }
114
+ let availablecontentType = void 0;
115
+ const contentTypes = Object.keys(responseBody.content);
116
+ contentTypes.forEach((mt) => {
117
+ if (!availablecontentType && matchesMimeType.json(mt)) {
118
+ availablecontentType = mt;
119
+ }
120
+ });
121
+ if (!availablecontentType) {
122
+ contentTypes.forEach((mt) => {
123
+ if (!availablecontentType) {
124
+ availablecontentType = mt;
125
+ }
126
+ });
127
+ }
128
+ if (availablecontentType) {
129
+ return [availablecontentType, responseBody.content[availablecontentType], ...responseBody.description ? [responseBody.description] : []];
130
+ }
131
+ return false;
132
+ };
133
+ }
134
+ getResponseSchema(operation, statusCode) {
135
+ if (operation.schema.responses) {
136
+ Object.keys(operation.schema.responses).forEach((key) => {
137
+ const schema2 = operation.schema.responses[key];
138
+ const $ref = isReference(schema2) ? schema2.$ref : void 0;
139
+ if (schema2 && $ref) {
140
+ operation.schema.responses[key] = this.get($ref);
141
+ }
142
+ });
143
+ }
144
+ const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode));
145
+ const { contentType } = this.#options;
146
+ const responseBody = getResponseBody(contentType);
147
+ if (responseBody === false) {
148
+ return {};
149
+ }
150
+ const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema;
151
+ if (!schema) {
152
+ return {};
153
+ }
154
+ return this.dereferenceWithRef(schema);
155
+ }
156
+ getRequestSchema(operation) {
157
+ const { contentType } = this.#options;
158
+ if (operation.schema.requestBody) {
159
+ operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody);
160
+ }
161
+ const requestBody = operation.getRequestBody(contentType);
162
+ if (requestBody === false) {
163
+ return void 0;
164
+ }
165
+ const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema;
166
+ if (!schema) {
167
+ return void 0;
168
+ }
169
+ return this.dereferenceWithRef(schema);
170
+ }
171
+ getParametersSchema(operation, inKey) {
172
+ const { contentType = operation.getContentType() } = this.#options;
173
+ const params = operation.getParameters().map((schema) => {
174
+ return this.dereferenceWithRef(schema);
175
+ }).filter((v) => v.in === inKey);
176
+ if (!params.length) {
177
+ return null;
178
+ }
179
+ return params.reduce(
180
+ (schema, pathParameters) => {
181
+ const property = pathParameters.content?.[contentType]?.schema ?? pathParameters.schema;
182
+ const required = [...schema.required || [], pathParameters.required ? pathParameters.name : void 0].filter(Boolean);
183
+ return {
184
+ ...schema,
185
+ description: schema.description,
186
+ deprecated: schema.deprecated,
187
+ example: schema.example,
188
+ required,
189
+ properties: {
190
+ ...schema.properties,
191
+ [pathParameters.name]: {
192
+ description: pathParameters.description,
193
+ ...property
194
+ }
195
+ }
196
+ };
197
+ },
198
+ { type: "object", required: [], properties: {} }
199
+ );
200
+ }
201
+ async valdiate() {
202
+ const oasNormalize = new OASNormalize(this.api, {
203
+ enablePaths: true,
204
+ colorizeErrors: true
205
+ });
206
+ await oasNormalize.validate({
207
+ convertToLatest: true,
208
+ parser: {
209
+ validate: {
210
+ colorizeErrors: true
211
+ }
212
+ }
213
+ });
214
+ }
215
+ };
216
+
217
+ // src/utils.ts
218
+ function isOpenApiV2Document(doc) {
219
+ return doc && isPlainObject(doc) && !("openapi" in doc);
220
+ }
221
+ function isOpenApiV3_1Document(doc) {
222
+ return doc && isPlainObject(doc) && "openapi" in doc && doc.openapi.startsWith("3.1");
223
+ }
224
+ function isParameterObject(obj) {
225
+ return obj && "in" in obj;
226
+ }
227
+ function isReference(obj) {
228
+ return !!obj && isRef(obj);
229
+ }
230
+ function isRequired(schema) {
231
+ if (!schema) {
232
+ return false;
233
+ }
234
+ return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required;
235
+ }
236
+ function isOptional(schema) {
237
+ return !isRequired(schema);
238
+ }
239
+ async function parse(pathOrApi, oasClass = Oas) {
240
+ if (typeof pathOrApi === "string") {
241
+ const config = await loadConfig();
242
+ const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi });
243
+ return parse(bundleResults.bundle.parsed);
244
+ }
245
+ const oasNormalize = new OASNormalize(pathOrApi, {
246
+ enablePaths: true,
247
+ colorizeErrors: true
248
+ });
249
+ const document = await oasNormalize.load();
250
+ if (isOpenApiV2Document(document)) {
251
+ const { openapi } = await swagger2openapi.convertObj(document, {
252
+ anchors: true
253
+ });
254
+ return new oasClass({ oas: openapi });
255
+ }
256
+ return new oasClass({ oas: document });
257
+ }
15
258
 
16
- export { HttpMethods };
259
+ export { HttpMethods, Oas, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, parse };
17
260
  //# sourceMappingURL=index.js.map
18
261
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts"],"names":[],"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","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"]}
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,E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AAY,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) {\n const { mapping = {}, propertyName } = (schemaObject.discriminator || {}) as OpenAPIV3.DiscriminatorObject\n\n Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {\n if (mappingValue) {\n const childSchema = this.get(mappingValue)\n const property = childSchema.properties?.[propertyName] as SchemaObject\n\n if (property) {\n childSchema.properties[propertyName] = {\n ...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 ...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 convertToLatest: true,\n parser: {\n validate: {\n colorizeErrors: true,\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 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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/oas",
3
- "version": "3.2.0",
3
+ "version": "3.3.1",
4
4
  "description": "Oas helpers",
5
5
  "keywords": [
6
6
  "typescript",
@@ -23,11 +23,6 @@
23
23
  "require": "./dist/index.cjs",
24
24
  "default": "./dist/index.cjs"
25
25
  },
26
- "./parser": {
27
- "import": "./dist/parser.js",
28
- "require": "./dist/parser.cjs",
29
- "default": "./dist/parser.cjs"
30
- },
31
26
  "./infer": {
32
27
  "import": "./dist/infer.js",
33
28
  "require": "./dist/infer.cjs",
@@ -41,9 +36,6 @@
41
36
  "types": "./dist/index.d.cts",
42
37
  "typesVersions": {
43
38
  "*": {
44
- "parser": [
45
- "./dist/parser.d.ts"
46
- ],
47
39
  "infer": [
48
40
  "./dist/infer.d.ts"
49
41
  ]
@@ -70,11 +62,11 @@
70
62
  "devDependencies": {
71
63
  "@stoplight/yaml": "^4.3.0",
72
64
  "@types/swagger2openapi": "^7.0.4",
73
- "expect-type": "^0.20.0",
65
+ "expect-type": "^1.1.0",
74
66
  "tsup": "^8.3.5",
75
67
  "typescript": "^5.7.2",
76
- "@kubb/config-ts": "3.2.0",
77
- "@kubb/config-tsup": "3.2.0"
68
+ "@kubb/config-ts": "3.3.1",
69
+ "@kubb/config-tsup": "3.3.1"
78
70
  },
79
71
  "engines": {
80
72
  "node": ">=20"
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, isReference, isParameterObject, isOpenApiV3_1Document } from './utils.ts'
3
+ export { isRequired, isOptional, 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
@@ -1,8 +1,13 @@
1
1
  import { isRef, isSchema } from 'oas/types'
2
2
  import { isPlainObject } from 'remeda'
3
3
 
4
+ import { bundle, loadConfig } from '@redocly/openapi-core'
5
+ import OASNormalize from 'oas-normalize'
6
+ import type { OASDocument } from 'oas/types'
4
7
  import type { ParameterObject, SchemaObject } from 'oas/types'
5
- import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
8
+ import type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
9
+ import swagger2openapi from 'swagger2openapi'
10
+ import { Oas } from './Oas.ts'
6
11
 
7
12
  export function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {
8
13
  return doc && isPlainObject(doc) && !('openapi' in doc)
@@ -38,3 +43,29 @@ export function isRequired(schema?: SchemaObject): boolean {
38
43
  export function isOptional(schema?: SchemaObject): boolean {
39
44
  return !isRequired(schema)
40
45
  }
46
+
47
+ export async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oas = Oas): Promise<Oas> {
48
+ if (typeof pathOrApi === 'string') {
49
+ // resolve external refs
50
+ const config = await loadConfig()
51
+ const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })
52
+
53
+ return parse(bundleResults.bundle.parsed)
54
+ }
55
+
56
+ const oasNormalize = new OASNormalize(pathOrApi, {
57
+ enablePaths: true,
58
+ colorizeErrors: true,
59
+ })
60
+ const document = (await oasNormalize.load()) as OpenAPI.Document
61
+
62
+ if (isOpenApiV2Document(document)) {
63
+ const { openapi } = await swagger2openapi.convertObj(document, {
64
+ anchors: true,
65
+ })
66
+
67
+ return new oasClass({ oas: openapi as OASDocument })
68
+ }
69
+
70
+ return new oasClass({ oas: document })
71
+ }
@@ -1,42 +0,0 @@
1
- import BaseOas from 'oas';
2
- import { Operation } from 'oas/operation';
3
- import * as OasTypes from 'oas/types';
4
- import { OASDocument, User, SchemaObject as SchemaObject$1 } from 'oas/types';
5
-
6
- type contentType = 'application/json' | (string & {});
7
- type SchemaObject = OasTypes.SchemaObject & {
8
- 'x-nullable'?: boolean;
9
- $ref?: string;
10
- };
11
- declare const HttpMethods: {
12
- GET: "get";
13
- POST: "post";
14
- PUT: "put";
15
- PATCH: "patch";
16
- DELETE: "delete";
17
- HEAD: "head";
18
- OPTIONS: "options";
19
- TRACE: "trace";
20
- };
21
-
22
- type Options = {
23
- contentType?: contentType;
24
- };
25
- declare class Oas<const TOAS = unknown> extends BaseOas {
26
- #private;
27
- document: TOAS;
28
- constructor({ oas, user }: {
29
- oas: TOAS | OASDocument | string;
30
- user?: User;
31
- }, options?: Options);
32
- get($ref: string): any;
33
- set($ref: string, value: unknown): false | undefined;
34
- resolveDiscriminators(): void;
35
- dereferenceWithRef(schema?: unknown): any;
36
- getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
37
- getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
38
- getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject$1 | null;
39
- valdiate(): Promise<void>;
40
- }
41
-
42
- export { HttpMethods as H, Oas as O, type SchemaObject as S, type contentType as c };
@@ -1,42 +0,0 @@
1
- import BaseOas from 'oas';
2
- import { Operation } from 'oas/operation';
3
- import * as OasTypes from 'oas/types';
4
- import { OASDocument, User, SchemaObject as SchemaObject$1 } from 'oas/types';
5
-
6
- type contentType = 'application/json' | (string & {});
7
- type SchemaObject = OasTypes.SchemaObject & {
8
- 'x-nullable'?: boolean;
9
- $ref?: string;
10
- };
11
- declare const HttpMethods: {
12
- GET: "get";
13
- POST: "post";
14
- PUT: "put";
15
- PATCH: "patch";
16
- DELETE: "delete";
17
- HEAD: "head";
18
- OPTIONS: "options";
19
- TRACE: "trace";
20
- };
21
-
22
- type Options = {
23
- contentType?: contentType;
24
- };
25
- declare class Oas<const TOAS = unknown> extends BaseOas {
26
- #private;
27
- document: TOAS;
28
- constructor({ oas, user }: {
29
- oas: TOAS | OASDocument | string;
30
- user?: User;
31
- }, options?: Options);
32
- get($ref: string): any;
33
- set($ref: string, value: unknown): false | undefined;
34
- resolveDiscriminators(): void;
35
- dereferenceWithRef(schema?: unknown): any;
36
- getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
37
- getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
38
- getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject$1 | null;
39
- valdiate(): Promise<void>;
40
- }
41
-
42
- export { HttpMethods as H, Oas as O, type SchemaObject as S, type contentType as c };