@kubb/oas 2.13.2 → 2.13.3
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/{Oas-DBH7VR4S.d.cts → Oas-BEe7KZDj.d.cts} +4 -6
- package/dist/{Oas-DBH7VR4S.d.ts → Oas-BEe7KZDj.d.ts} +4 -6
- package/dist/{chunk-U2H7VGLT.js → chunk-2SLQJGDM.js} +37 -36
- package/dist/chunk-2SLQJGDM.js.map +1 -0
- package/dist/{chunk-WMBNWD46.cjs → chunk-EPQ73VUM.cjs} +37 -36
- package/dist/chunk-EPQ73VUM.cjs.map +1 -0
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/parser.cjs +4 -4
- package/dist/parser.d.cts +1 -1
- package/dist/parser.d.ts +1 -1
- package/dist/parser.js +1 -1
- package/package.json +2 -2
- package/src/Oas.ts +39 -37
- package/src/types.ts +1 -1
- package/dist/chunk-U2H7VGLT.js.map +0 -1
- package/dist/chunk-WMBNWD46.cjs.map +0 -1
|
@@ -3,7 +3,7 @@ import { Operation } from 'oas/operation';
|
|
|
3
3
|
import * as OasTypes from 'oas/types';
|
|
4
4
|
import { OASDocument, User, SchemaObject as SchemaObject$1 } from 'oas/types';
|
|
5
5
|
|
|
6
|
-
type
|
|
6
|
+
type contentType = 'application/json' | (string & {});
|
|
7
7
|
type SchemaObject = OasTypes.SchemaObject & {
|
|
8
8
|
'x-nullable'?: boolean;
|
|
9
9
|
$ref?: string;
|
|
@@ -20,7 +20,7 @@ declare const HttpMethods: {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
type Options = {
|
|
23
|
-
|
|
23
|
+
contentType?: contentType;
|
|
24
24
|
};
|
|
25
25
|
declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
26
26
|
#private;
|
|
@@ -29,13 +29,11 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
29
29
|
oas: TOAS | OASDocument | string;
|
|
30
30
|
user?: User;
|
|
31
31
|
}, options?: Options);
|
|
32
|
-
|
|
33
|
-
withRef?: boolean;
|
|
34
|
-
}): any;
|
|
32
|
+
dereferenceWithRef(schema?: unknown): any;
|
|
35
33
|
getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
|
|
36
34
|
getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
|
|
37
35
|
getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject$1 | null;
|
|
38
36
|
valdiate(): Promise<void>;
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
export { HttpMethods as H,
|
|
39
|
+
export { HttpMethods as H, Oas as O, type SchemaObject as S, type contentType as c };
|
|
@@ -3,7 +3,7 @@ import { Operation } from 'oas/operation';
|
|
|
3
3
|
import * as OasTypes from 'oas/types';
|
|
4
4
|
import { OASDocument, User, SchemaObject as SchemaObject$1 } from 'oas/types';
|
|
5
5
|
|
|
6
|
-
type
|
|
6
|
+
type contentType = 'application/json' | (string & {});
|
|
7
7
|
type SchemaObject = OasTypes.SchemaObject & {
|
|
8
8
|
'x-nullable'?: boolean;
|
|
9
9
|
$ref?: string;
|
|
@@ -20,7 +20,7 @@ declare const HttpMethods: {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
type Options = {
|
|
23
|
-
|
|
23
|
+
contentType?: contentType;
|
|
24
24
|
};
|
|
25
25
|
declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
26
26
|
#private;
|
|
@@ -29,13 +29,11 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
29
29
|
oas: TOAS | OASDocument | string;
|
|
30
30
|
user?: User;
|
|
31
31
|
}, options?: Options);
|
|
32
|
-
|
|
33
|
-
withRef?: boolean;
|
|
34
|
-
}): any;
|
|
32
|
+
dereferenceWithRef(schema?: unknown): any;
|
|
35
33
|
getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
|
|
36
34
|
getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
|
|
37
35
|
getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject$1 | null;
|
|
38
36
|
valdiate(): Promise<void>;
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
export { HttpMethods as H,
|
|
39
|
+
export { HttpMethods as H, Oas as O, type SchemaObject as S, type contentType as c };
|
|
@@ -55,7 +55,7 @@ var Oas = class extends BaseOas {
|
|
|
55
55
|
}
|
|
56
56
|
super(oas, user);
|
|
57
57
|
/**
|
|
58
|
-
* Oas does not have a getResponseBody(
|
|
58
|
+
* Oas does not have a getResponseBody(contentType)
|
|
59
59
|
*/
|
|
60
60
|
__privateAdd(this, _getResponseBodyFactory);
|
|
61
61
|
__privateAdd(this, _options, {});
|
|
@@ -63,27 +63,28 @@ var Oas = class extends BaseOas {
|
|
|
63
63
|
this.document = oas;
|
|
64
64
|
__privateSet(this, _options, options);
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
dereferenceWithRef(schema) {
|
|
67
67
|
if (isReference(schema)) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
return findSchemaDefinition(schema?.$ref, this.api);
|
|
68
|
+
return {
|
|
69
|
+
...findSchemaDefinition(schema.$ref, this.api),
|
|
70
|
+
$ref: schema.$ref
|
|
71
|
+
};
|
|
75
72
|
}
|
|
76
73
|
return schema;
|
|
77
74
|
}
|
|
78
75
|
getResponseSchema(operation, statusCode) {
|
|
79
76
|
if (operation.schema.responses) {
|
|
80
77
|
Object.keys(operation.schema.responses).forEach((key) => {
|
|
81
|
-
|
|
78
|
+
const schema2 = operation.schema.responses[key];
|
|
79
|
+
const $ref = isReference(schema2) ? schema2.$ref : void 0;
|
|
80
|
+
if (schema2 && $ref) {
|
|
81
|
+
operation.schema.responses[key] = findSchemaDefinition($ref, this.api);
|
|
82
|
+
}
|
|
82
83
|
});
|
|
83
84
|
}
|
|
84
85
|
const getResponseBody = __privateMethod(this, _getResponseBodyFactory, getResponseBodyFactory_fn).call(this, operation.getResponseByStatusCode(statusCode));
|
|
85
|
-
const {
|
|
86
|
-
const responseBody = getResponseBody(
|
|
86
|
+
const { contentType } = __privateGet(this, _options);
|
|
87
|
+
const responseBody = getResponseBody(contentType);
|
|
87
88
|
if (responseBody === false) {
|
|
88
89
|
return {};
|
|
89
90
|
}
|
|
@@ -91,14 +92,14 @@ var Oas = class extends BaseOas {
|
|
|
91
92
|
if (!schema) {
|
|
92
93
|
return {};
|
|
93
94
|
}
|
|
94
|
-
return this.
|
|
95
|
+
return this.dereferenceWithRef(schema);
|
|
95
96
|
}
|
|
96
97
|
getRequestSchema(operation) {
|
|
97
|
-
const {
|
|
98
|
+
const { contentType } = __privateGet(this, _options);
|
|
98
99
|
if (operation.schema.requestBody) {
|
|
99
|
-
operation.schema.requestBody = this.
|
|
100
|
+
operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody);
|
|
100
101
|
}
|
|
101
|
-
const requestBody = operation.getRequestBody(
|
|
102
|
+
const requestBody = operation.getRequestBody(contentType);
|
|
102
103
|
if (requestBody === false) {
|
|
103
104
|
return void 0;
|
|
104
105
|
}
|
|
@@ -106,19 +107,19 @@ var Oas = class extends BaseOas {
|
|
|
106
107
|
if (!schema) {
|
|
107
108
|
return void 0;
|
|
108
109
|
}
|
|
109
|
-
return this.
|
|
110
|
+
return this.dereferenceWithRef(schema);
|
|
110
111
|
}
|
|
111
112
|
getParametersSchema(operation, inKey) {
|
|
112
|
-
const {
|
|
113
|
+
const { contentType = operation.getContentType() } = __privateGet(this, _options);
|
|
113
114
|
const params = operation.getParameters().map((schema) => {
|
|
114
|
-
return this.
|
|
115
|
+
return this.dereferenceWithRef(schema);
|
|
115
116
|
}).filter((v) => v.in === inKey);
|
|
116
117
|
if (!params.length) {
|
|
117
118
|
return null;
|
|
118
119
|
}
|
|
119
120
|
return params.reduce(
|
|
120
121
|
(schema, pathParameters) => {
|
|
121
|
-
const property = pathParameters.content?.[
|
|
122
|
+
const property = pathParameters.content?.[contentType]?.schema ?? pathParameters.schema;
|
|
122
123
|
const required = [...schema.required || [], pathParameters.required ? pathParameters.name : void 0].filter(Boolean);
|
|
123
124
|
return {
|
|
124
125
|
...schema,
|
|
@@ -152,7 +153,7 @@ getResponseBodyFactory_fn = function(responseBody) {
|
|
|
152
153
|
function hasResponseBody(res = responseBody) {
|
|
153
154
|
return !!res;
|
|
154
155
|
}
|
|
155
|
-
return (
|
|
156
|
+
return (contentType) => {
|
|
156
157
|
if (!hasResponseBody(responseBody)) {
|
|
157
158
|
return false;
|
|
158
159
|
}
|
|
@@ -162,28 +163,28 @@ getResponseBodyFactory_fn = function(responseBody) {
|
|
|
162
163
|
if (!responseBody.content) {
|
|
163
164
|
return false;
|
|
164
165
|
}
|
|
165
|
-
if (
|
|
166
|
-
if (!(
|
|
166
|
+
if (contentType) {
|
|
167
|
+
if (!(contentType in responseBody.content)) {
|
|
167
168
|
return false;
|
|
168
169
|
}
|
|
169
|
-
return responseBody.content[
|
|
170
|
+
return responseBody.content[contentType];
|
|
170
171
|
}
|
|
171
|
-
let
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
if (!
|
|
175
|
-
|
|
172
|
+
let availablecontentType = void 0;
|
|
173
|
+
const contentTypes = Object.keys(responseBody.content);
|
|
174
|
+
contentTypes.forEach((mt) => {
|
|
175
|
+
if (!availablecontentType && matchesMimeType.json(mt)) {
|
|
176
|
+
availablecontentType = mt;
|
|
176
177
|
}
|
|
177
178
|
});
|
|
178
|
-
if (!
|
|
179
|
-
|
|
180
|
-
if (!
|
|
181
|
-
|
|
179
|
+
if (!availablecontentType) {
|
|
180
|
+
contentTypes.forEach((mt) => {
|
|
181
|
+
if (!availablecontentType) {
|
|
182
|
+
availablecontentType = mt;
|
|
182
183
|
}
|
|
183
184
|
});
|
|
184
185
|
}
|
|
185
|
-
if (
|
|
186
|
-
return [
|
|
186
|
+
if (availablecontentType) {
|
|
187
|
+
return [availablecontentType, responseBody.content[availablecontentType], ...responseBody.description ? [responseBody.description] : []];
|
|
187
188
|
}
|
|
188
189
|
return false;
|
|
189
190
|
};
|
|
@@ -197,4 +198,4 @@ export {
|
|
|
197
198
|
isRequired,
|
|
198
199
|
Oas
|
|
199
200
|
};
|
|
200
|
-
//# sourceMappingURL=chunk-
|
|
201
|
+
//# sourceMappingURL=chunk-2SLQJGDM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/Oas.ts"],"sourcesContent":["import type { ParameterObject, SchemaObject } from 'oas/types'\nimport { isRef, isSchema } from 'oas/types'\nimport { isObject } from 'remeda'\n\nimport type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isObject(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","import BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { findSchemaDefinition, matchesMimeType } from 'oas/utils'\nimport type { contentType } from './types.ts'\nimport { isReference } from './utils.ts'\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 dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...findSchemaDefinition(schema.$ref, this.api),\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] = findSchemaDefinition($ref, this.api)\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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,aAAO;AAAA,IACT;AAEA,QAAI,aAAa;AACf,UAAI,EAAE,eAAe,aAAa,UAAU;AAC1C,eAAO;AAAA,MACT;AAEA,aAAO,aAAa,QAAQ,WAAW;AAAA,IACzC;AAIA,QAAI,uBAA2C;AAC/C,UAAM,eAAe,OAAO,KAAK,aAAa,OAAO;AACrD,iBAAa,QAAQ,CAAC,OAAe;AACnC,UAAI,CAAC,wBAAwB,gBAAgB,KAAK,EAAE,GAAG;AACrD,+BAAuB;AAAA,MACzB;AAAA,IACF,CAAC;AAED,QAAI,CAAC,sBAAsB;AACzB,mBAAa,QAAQ,CAAC,OAAe;AACnC,YAAI,CAAC,sBAAsB;AACzB,iCAAuB;AAAA,QACzB;AAAA,MACF,CAAC;AAAA,IACH;AAEA,QAAI,sBAAsB;AACxB,aAAO,CAAC,sBAAsB,aAAa,QAAQ,oBAAoB,GAAI,GAAI,aAAa,cAAc,CAAC,aAAa,WAAW,IAAI,CAAC,CAAE;AAAA,IAC5I;AAEA,WAAO;AAAA,EACT;AACF;","names":["schema"]}
|
|
@@ -55,7 +55,7 @@ var Oas = class extends _oas2.default {
|
|
|
55
55
|
}
|
|
56
56
|
super(oas, user);
|
|
57
57
|
/**
|
|
58
|
-
* Oas does not have a getResponseBody(
|
|
58
|
+
* Oas does not have a getResponseBody(contentType)
|
|
59
59
|
*/
|
|
60
60
|
__privateAdd(this, _getResponseBodyFactory);
|
|
61
61
|
__privateAdd(this, _options, {});
|
|
@@ -63,27 +63,28 @@ var Oas = class extends _oas2.default {
|
|
|
63
63
|
this.document = oas;
|
|
64
64
|
__privateSet(this, _options, options);
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
dereferenceWithRef(schema) {
|
|
67
67
|
if (isReference(schema)) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
return _utils.findSchemaDefinition.call(void 0, _optionalChain([schema, 'optionalAccess', _4 => _4.$ref]), this.api);
|
|
68
|
+
return {
|
|
69
|
+
..._utils.findSchemaDefinition.call(void 0, schema.$ref, this.api),
|
|
70
|
+
$ref: schema.$ref
|
|
71
|
+
};
|
|
75
72
|
}
|
|
76
73
|
return schema;
|
|
77
74
|
}
|
|
78
75
|
getResponseSchema(operation, statusCode) {
|
|
79
76
|
if (operation.schema.responses) {
|
|
80
77
|
Object.keys(operation.schema.responses).forEach((key) => {
|
|
81
|
-
|
|
78
|
+
const schema2 = operation.schema.responses[key];
|
|
79
|
+
const $ref = isReference(schema2) ? schema2.$ref : void 0;
|
|
80
|
+
if (schema2 && $ref) {
|
|
81
|
+
operation.schema.responses[key] = _utils.findSchemaDefinition.call(void 0, $ref, this.api);
|
|
82
|
+
}
|
|
82
83
|
});
|
|
83
84
|
}
|
|
84
85
|
const getResponseBody = __privateMethod(this, _getResponseBodyFactory, getResponseBodyFactory_fn).call(this, operation.getResponseByStatusCode(statusCode));
|
|
85
|
-
const {
|
|
86
|
-
const responseBody = getResponseBody(
|
|
86
|
+
const { contentType } = __privateGet(this, _options);
|
|
87
|
+
const responseBody = getResponseBody(contentType);
|
|
87
88
|
if (responseBody === false) {
|
|
88
89
|
return {};
|
|
89
90
|
}
|
|
@@ -91,14 +92,14 @@ var Oas = class extends _oas2.default {
|
|
|
91
92
|
if (!schema) {
|
|
92
93
|
return {};
|
|
93
94
|
}
|
|
94
|
-
return this.
|
|
95
|
+
return this.dereferenceWithRef(schema);
|
|
95
96
|
}
|
|
96
97
|
getRequestSchema(operation) {
|
|
97
|
-
const {
|
|
98
|
+
const { contentType } = __privateGet(this, _options);
|
|
98
99
|
if (operation.schema.requestBody) {
|
|
99
|
-
operation.schema.requestBody = this.
|
|
100
|
+
operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody);
|
|
100
101
|
}
|
|
101
|
-
const requestBody = operation.getRequestBody(
|
|
102
|
+
const requestBody = operation.getRequestBody(contentType);
|
|
102
103
|
if (requestBody === false) {
|
|
103
104
|
return void 0;
|
|
104
105
|
}
|
|
@@ -106,19 +107,19 @@ var Oas = class extends _oas2.default {
|
|
|
106
107
|
if (!schema) {
|
|
107
108
|
return void 0;
|
|
108
109
|
}
|
|
109
|
-
return this.
|
|
110
|
+
return this.dereferenceWithRef(schema);
|
|
110
111
|
}
|
|
111
112
|
getParametersSchema(operation, inKey) {
|
|
112
|
-
const {
|
|
113
|
+
const { contentType = operation.getContentType() } = __privateGet(this, _options);
|
|
113
114
|
const params = operation.getParameters().map((schema) => {
|
|
114
|
-
return this.
|
|
115
|
+
return this.dereferenceWithRef(schema);
|
|
115
116
|
}).filter((v) => v.in === inKey);
|
|
116
117
|
if (!params.length) {
|
|
117
118
|
return null;
|
|
118
119
|
}
|
|
119
120
|
return params.reduce(
|
|
120
121
|
(schema, pathParameters) => {
|
|
121
|
-
const property = _nullishCoalesce(_optionalChain([pathParameters, 'access',
|
|
122
|
+
const property = _nullishCoalesce(_optionalChain([pathParameters, 'access', _3 => _3.content, 'optionalAccess', _4 => _4[contentType], 'optionalAccess', _5 => _5.schema]), () => ( pathParameters.schema));
|
|
122
123
|
const required = [...schema.required || [], pathParameters.required ? pathParameters.name : void 0].filter(Boolean);
|
|
123
124
|
return {
|
|
124
125
|
...schema,
|
|
@@ -152,7 +153,7 @@ getResponseBodyFactory_fn = function(responseBody) {
|
|
|
152
153
|
function hasResponseBody(res = responseBody) {
|
|
153
154
|
return !!res;
|
|
154
155
|
}
|
|
155
|
-
return (
|
|
156
|
+
return (contentType) => {
|
|
156
157
|
if (!hasResponseBody(responseBody)) {
|
|
157
158
|
return false;
|
|
158
159
|
}
|
|
@@ -162,28 +163,28 @@ getResponseBodyFactory_fn = function(responseBody) {
|
|
|
162
163
|
if (!responseBody.content) {
|
|
163
164
|
return false;
|
|
164
165
|
}
|
|
165
|
-
if (
|
|
166
|
-
if (!(
|
|
166
|
+
if (contentType) {
|
|
167
|
+
if (!(contentType in responseBody.content)) {
|
|
167
168
|
return false;
|
|
168
169
|
}
|
|
169
|
-
return responseBody.content[
|
|
170
|
+
return responseBody.content[contentType];
|
|
170
171
|
}
|
|
171
|
-
let
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
if (!
|
|
175
|
-
|
|
172
|
+
let availablecontentType = void 0;
|
|
173
|
+
const contentTypes = Object.keys(responseBody.content);
|
|
174
|
+
contentTypes.forEach((mt) => {
|
|
175
|
+
if (!availablecontentType && _utils.matchesMimeType.json(mt)) {
|
|
176
|
+
availablecontentType = mt;
|
|
176
177
|
}
|
|
177
178
|
});
|
|
178
|
-
if (!
|
|
179
|
-
|
|
180
|
-
if (!
|
|
181
|
-
|
|
179
|
+
if (!availablecontentType) {
|
|
180
|
+
contentTypes.forEach((mt) => {
|
|
181
|
+
if (!availablecontentType) {
|
|
182
|
+
availablecontentType = mt;
|
|
182
183
|
}
|
|
183
184
|
});
|
|
184
185
|
}
|
|
185
|
-
if (
|
|
186
|
-
return [
|
|
186
|
+
if (availablecontentType) {
|
|
187
|
+
return [availablecontentType, responseBody.content[availablecontentType], ...responseBody.description ? [responseBody.description] : []];
|
|
187
188
|
}
|
|
188
189
|
return false;
|
|
189
190
|
};
|
|
@@ -197,4 +198,4 @@ getResponseBodyFactory_fn = function(responseBody) {
|
|
|
197
198
|
|
|
198
199
|
|
|
199
200
|
exports.isOpenApiV2Document = isOpenApiV2Document; exports.isOpenApiV3_1Document = isOpenApiV3_1Document; exports.isParameterObject = isParameterObject; exports.isReference = isReference; exports.isRequired = isRequired; exports.Oas = Oas;
|
|
200
|
-
//# sourceMappingURL=chunk-
|
|
201
|
+
//# sourceMappingURL=chunk-EPQ73VUM.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/Oas.ts"],"names":["schema"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AACA,SAAS,OAAO,gBAAgB;AAChC,SAAS,gBAAgB;AAIlB,SAAS,oBAAoB,KAAqC;AACvE,SAAO,OAAO,SAAS,GAAG,KAAK,EAAE,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AAI,CAAC,wBAAwB,gBAAgB,KAAK,EAAE,GAAG;AACrD,+BAAuB;AAAA,MACzB;AAAA,IACF,CAAC;AAED,QAAI,CAAC,sBAAsB;AACzB,mBAAa,QAAQ,CAAC,OAAe;AACnC,YAAI,CAAC,sBAAsB;AACzB,iCAAuB;AAAA,QACzB;AAAA,MACF,CAAC;AAAA,IACH;AAEA,QAAI,sBAAsB;AACxB,aAAO,CAAC,sBAAsB,aAAa,QAAQ,oBAAoB,GAAI,GAAI,aAAa,cAAc,CAAC,aAAa,WAAW,IAAI,CAAC,CAAE;AAAA,IAC5I;AAEA,WAAO;AAAA,EACT;AACF","sourcesContent":["import type { ParameterObject, SchemaObject } from 'oas/types'\nimport { isRef, isSchema } from 'oas/types'\nimport { isObject } from 'remeda'\n\nimport type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isObject(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","import BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { findSchemaDefinition, matchesMimeType } from 'oas/utils'\nimport type { contentType } from './types.ts'\nimport { isReference } from './utils.ts'\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 dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...findSchemaDefinition(schema.$ref, this.api),\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] = findSchemaDefinition($ref, this.api)\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 }\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunkEPQ73VUMcjs = require('./chunk-EPQ73VUM.cjs');
|
|
8
8
|
|
|
9
9
|
// src/types.ts
|
|
10
10
|
var HttpMethods = {
|
|
@@ -29,5 +29,5 @@ var _utils = require('oas/utils');
|
|
|
29
29
|
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
exports.HttpMethods = HttpMethods; exports.Oas =
|
|
32
|
+
exports.HttpMethods = HttpMethods; exports.Oas = _chunkEPQ73VUMcjs.Oas; exports.findSchemaDefinition = _utils.findSchemaDefinition; exports.isOpenApiV3_1Document = _chunkEPQ73VUMcjs.isOpenApiV3_1Document; exports.isParameterObject = _chunkEPQ73VUMcjs.isParameterObject; exports.isReference = _chunkEPQ73VUMcjs.isReference; exports.isRequired = _chunkEPQ73VUMcjs.isRequired; exports.matchesMimeType = _utils.matchesMimeType;
|
|
33
33
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts","../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAeO,IAAM,cAAc;AAAA,EACzB,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AAAA,EACL,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,SAAS;AAAA,EACT,OAAO;AACT;;;ACvBA,SAAS,sBAAsB,uBAAuB","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
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAeO,IAAM,cAAc;AAAA,EACzB,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AAAA,EACL,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,SAAS;AAAA,EACT,OAAO;AACT;;;ACvBA,SAAS,sBAAsB,uBAAuB","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","export * from './types.ts'\nexport { findSchemaDefinition, matchesMimeType } from 'oas/utils'\nexport { isRequired, isReference, isParameterObject, isOpenApiV3_1Document } from './utils.ts'\nexport { Oas } from './Oas.ts'\nexport type { Infer, Model, RequestParams, Response } from './infer/index.ts'\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { H as HttpMethods,
|
|
1
|
+
export { H as HttpMethods, O as Oas, S as SchemaObject, c as contentType } from './Oas-BEe7KZDj.cjs';
|
|
2
2
|
export { findSchemaDefinition, matchesMimeType } from 'oas/utils';
|
|
3
3
|
import * as OasTypes from 'oas/types';
|
|
4
4
|
import { ParameterObject, SchemaObject } from 'oas/types';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { H as HttpMethods,
|
|
1
|
+
export { H as HttpMethods, O as Oas, S as SchemaObject, c as contentType } from './Oas-BEe7KZDj.js';
|
|
2
2
|
export { findSchemaDefinition, matchesMimeType } from 'oas/utils';
|
|
3
3
|
import * as OasTypes from 'oas/types';
|
|
4
4
|
import { ParameterObject, SchemaObject } from 'oas/types';
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts","../src/index.ts"],"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
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/index.ts"],"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","export * from './types.ts'\nexport { findSchemaDefinition, matchesMimeType } from 'oas/utils'\nexport { isRequired, isReference, isParameterObject, isOpenApiV3_1Document } from './utils.ts'\nexport { Oas } from './Oas.ts'\nexport type { Infer, Model, RequestParams, Response } from './infer/index.ts'\n"],"mappings":";;;;;;;;;AAeO,IAAM,cAAc;AAAA,EACzB,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AAAA,EACL,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,SAAS;AAAA,EACT,OAAO;AACT;;;ACvBA,SAAS,sBAAsB,uBAAuB;","names":[]}
|
package/dist/parser.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var _chunkEPQ73VUMcjs = require('./chunk-EPQ73VUM.cjs');
|
|
5
5
|
|
|
6
6
|
// src/parser/index.ts
|
|
7
7
|
var _oasnormalize = require('oas-normalize'); var _oasnormalize2 = _interopRequireDefault(_oasnormalize);
|
|
@@ -12,11 +12,11 @@ async function parse(pathOrApi) {
|
|
|
12
12
|
colorizeErrors: true
|
|
13
13
|
});
|
|
14
14
|
const document = await oasNormalize.load();
|
|
15
|
-
if (
|
|
15
|
+
if (_chunkEPQ73VUMcjs.isOpenApiV2Document.call(void 0, document)) {
|
|
16
16
|
const { openapi: oas } = await _swagger2openapi2.default.convertObj(document, { anchors: true });
|
|
17
|
-
return new (0,
|
|
17
|
+
return new (0, _chunkEPQ73VUMcjs.Oas)({ oas });
|
|
18
18
|
}
|
|
19
|
-
return new (0,
|
|
19
|
+
return new (0, _chunkEPQ73VUMcjs.Oas)({ oas: document });
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
|
package/dist/parser.d.cts
CHANGED
package/dist/parser.d.ts
CHANGED
package/dist/parser.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/oas",
|
|
3
|
-
"version": "2.13.
|
|
3
|
+
"version": "2.13.3",
|
|
4
4
|
"description": "Oas helpers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -62,8 +62,8 @@
|
|
|
62
62
|
"expect-type": "^0.19.0",
|
|
63
63
|
"tsup": "^8.0.2",
|
|
64
64
|
"typescript": "^5.4.5",
|
|
65
|
-
"@kubb/ts-config": "0.1.0",
|
|
66
65
|
"@kubb/biome-config": "0.1.0",
|
|
66
|
+
"@kubb/ts-config": "0.1.0",
|
|
67
67
|
"@kubb/tsup-config": "1.1.8"
|
|
68
68
|
},
|
|
69
69
|
"engines": {
|
package/src/Oas.ts
CHANGED
|
@@ -3,10 +3,10 @@ import OASNormalize from 'oas-normalize'
|
|
|
3
3
|
import type { Operation } from 'oas/operation'
|
|
4
4
|
import type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'
|
|
5
5
|
import { findSchemaDefinition, matchesMimeType } from 'oas/utils'
|
|
6
|
-
import type {
|
|
6
|
+
import type { contentType } from './types.ts'
|
|
7
7
|
import { isReference } from './utils.ts'
|
|
8
8
|
type Options = {
|
|
9
|
-
|
|
9
|
+
contentType?: contentType
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export class Oas<const TOAS = unknown> extends BaseOas {
|
|
@@ -24,29 +24,26 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
24
24
|
this.#options = options
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
dereferenceWithRef(schema?: unknown) {
|
|
28
28
|
if (isReference(schema)) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
$ref: schema.$ref,
|
|
33
|
-
}
|
|
29
|
+
return {
|
|
30
|
+
...findSchemaDefinition(schema.$ref, this.api),
|
|
31
|
+
$ref: schema.$ref,
|
|
34
32
|
}
|
|
35
|
-
return findSchemaDefinition(schema?.$ref, this.api)
|
|
36
33
|
}
|
|
37
34
|
|
|
38
35
|
return schema
|
|
39
36
|
}
|
|
40
37
|
|
|
41
38
|
/**
|
|
42
|
-
* Oas does not have a getResponseBody(
|
|
39
|
+
* Oas does not have a getResponseBody(contentType)
|
|
43
40
|
*/
|
|
44
|
-
#getResponseBodyFactory(responseBody: boolean | ResponseObject): (
|
|
41
|
+
#getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {
|
|
45
42
|
function hasResponseBody(res = responseBody): res is ResponseObject {
|
|
46
43
|
return !!res
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
return (
|
|
46
|
+
return (contentType) => {
|
|
50
47
|
if (!hasResponseBody(responseBody)) {
|
|
51
48
|
return false
|
|
52
49
|
}
|
|
@@ -61,34 +58,34 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
61
58
|
return false
|
|
62
59
|
}
|
|
63
60
|
|
|
64
|
-
if (
|
|
65
|
-
if (!(
|
|
61
|
+
if (contentType) {
|
|
62
|
+
if (!(contentType in responseBody.content)) {
|
|
66
63
|
return false
|
|
67
64
|
}
|
|
68
65
|
|
|
69
|
-
return responseBody.content[
|
|
66
|
+
return responseBody.content[contentType]!
|
|
70
67
|
}
|
|
71
68
|
|
|
72
69
|
// Since no media type was supplied we need to find either the first JSON-like media type that
|
|
73
70
|
// we've got, or the first available of anything else if no JSON-like media types are present.
|
|
74
|
-
let
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
if (!
|
|
78
|
-
|
|
71
|
+
let availablecontentType: string | undefined = undefined
|
|
72
|
+
const contentTypes = Object.keys(responseBody.content)
|
|
73
|
+
contentTypes.forEach((mt: string) => {
|
|
74
|
+
if (!availablecontentType && matchesMimeType.json(mt)) {
|
|
75
|
+
availablecontentType = mt
|
|
79
76
|
}
|
|
80
77
|
})
|
|
81
78
|
|
|
82
|
-
if (!
|
|
83
|
-
|
|
84
|
-
if (!
|
|
85
|
-
|
|
79
|
+
if (!availablecontentType) {
|
|
80
|
+
contentTypes.forEach((mt: string) => {
|
|
81
|
+
if (!availablecontentType) {
|
|
82
|
+
availablecontentType = mt
|
|
86
83
|
}
|
|
87
84
|
})
|
|
88
85
|
}
|
|
89
86
|
|
|
90
|
-
if (
|
|
91
|
-
return [
|
|
87
|
+
if (availablecontentType) {
|
|
88
|
+
return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]
|
|
92
89
|
}
|
|
93
90
|
|
|
94
91
|
return false
|
|
@@ -98,14 +95,19 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
98
95
|
getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {
|
|
99
96
|
if (operation.schema.responses) {
|
|
100
97
|
Object.keys(operation.schema.responses).forEach((key) => {
|
|
101
|
-
|
|
98
|
+
const schema = operation.schema.responses![key]
|
|
99
|
+
const $ref = isReference(schema) ? schema.$ref : undefined
|
|
100
|
+
|
|
101
|
+
if (schema && $ref) {
|
|
102
|
+
operation.schema.responses![key] = findSchemaDefinition($ref, this.api)
|
|
103
|
+
}
|
|
102
104
|
})
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))
|
|
106
108
|
|
|
107
|
-
const {
|
|
108
|
-
const responseBody = getResponseBody(
|
|
109
|
+
const { contentType } = this.#options
|
|
110
|
+
const responseBody = getResponseBody(contentType)
|
|
109
111
|
|
|
110
112
|
if (responseBody === false) {
|
|
111
113
|
// return empty object because response will always be defined(request does not need a body)
|
|
@@ -120,17 +122,17 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
120
122
|
return {}
|
|
121
123
|
}
|
|
122
124
|
|
|
123
|
-
return this.
|
|
125
|
+
return this.dereferenceWithRef(schema)
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
getRequestSchema(operation: Operation): SchemaObject | undefined {
|
|
127
|
-
const {
|
|
129
|
+
const { contentType } = this.#options
|
|
128
130
|
|
|
129
131
|
if (operation.schema.requestBody) {
|
|
130
|
-
operation.schema.requestBody = this.
|
|
132
|
+
operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)
|
|
131
133
|
}
|
|
132
134
|
|
|
133
|
-
const requestBody = operation.getRequestBody(
|
|
135
|
+
const requestBody = operation.getRequestBody(contentType)
|
|
134
136
|
|
|
135
137
|
if (requestBody === false) {
|
|
136
138
|
return undefined
|
|
@@ -142,15 +144,15 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
142
144
|
return undefined
|
|
143
145
|
}
|
|
144
146
|
|
|
145
|
-
return this.
|
|
147
|
+
return this.dereferenceWithRef(schema)
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {
|
|
149
|
-
const {
|
|
151
|
+
const { contentType = operation.getContentType() } = this.#options
|
|
150
152
|
const params = operation
|
|
151
153
|
.getParameters()
|
|
152
154
|
.map((schema) => {
|
|
153
|
-
return this.
|
|
155
|
+
return this.dereferenceWithRef(schema)
|
|
154
156
|
})
|
|
155
157
|
.filter((v) => v.in === inKey)
|
|
156
158
|
|
|
@@ -160,7 +162,7 @@ export class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
160
162
|
|
|
161
163
|
return params.reduce(
|
|
162
164
|
(schema, pathParameters) => {
|
|
163
|
-
const property = pathParameters.content?.[
|
|
165
|
+
const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)
|
|
164
166
|
const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)
|
|
165
167
|
|
|
166
168
|
return {
|
package/src/types.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type { HttpMethods as HttpMethod } from 'oas/types'
|
|
|
6
6
|
export type * as OasTypes from 'oas/types'
|
|
7
7
|
export type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
|
|
8
8
|
|
|
9
|
-
export type
|
|
9
|
+
export type contentType = 'application/json' | (string & {})
|
|
10
10
|
|
|
11
11
|
export type SchemaObject = OasTypes.SchemaObject & {
|
|
12
12
|
'x-nullable'?: boolean
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts","../src/Oas.ts"],"sourcesContent":["import type { ParameterObject, SchemaObject } from 'oas/types'\nimport { isRef, isSchema } from 'oas/types'\nimport { isObject } from 'remeda'\n\nimport type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isObject(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","import BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { findSchemaDefinition, matchesMimeType } from 'oas/utils'\nimport type { MediaType } from './types.ts'\nimport { isReference } from './utils.ts'\ntype Options = {\n mediaType?: MediaType\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 dereference(schema?: unknown, { withRef = false }: { withRef?: boolean } = {}) {\n if (isReference(schema)) {\n if (withRef) {\n return {\n ...findSchemaDefinition(schema?.$ref, this.api),\n $ref: schema.$ref,\n }\n }\n return findSchemaDefinition(schema?.$ref, this.api)\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(mediaType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (mediaType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (mediaType) => {\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 (mediaType) {\n if (!(mediaType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[mediaType]!\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 availableMediaType: string | undefined = undefined\n const mediaTypes = Object.keys(responseBody.content)\n mediaTypes.forEach((mt: string) => {\n if (!availableMediaType && matchesMimeType.json(mt)) {\n availableMediaType = mt\n }\n })\n\n if (!availableMediaType) {\n mediaTypes.forEach((mt: string) => {\n if (!availableMediaType) {\n availableMediaType = mt\n }\n })\n }\n\n if (availableMediaType) {\n return [availableMediaType, responseBody.content[availableMediaType]!, ...(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 operation.schema.responses![key] = this.dereference(operation.schema.responses![key])\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { mediaType } = this.#options\n const responseBody = getResponseBody(mediaType)\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.dereference(schema, { withRef: true })\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { mediaType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereference(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(mediaType)\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.dereference(schema, { withRef: true })\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { mediaType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereference(schema, { withRef: true })\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?.[mediaType]?.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 }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AACA,SAAS,OAAO,gBAAgB;AAChC,SAAS,gBAAgB;AAIlB,SAAS,oBAAoB,KAAqC;AACvE,SAAO,OAAO,SAAS,GAAG,KAAK,EAAE,aAAa;AAChD;AAKO,SAAS,sBAAsB,KAAuC;AAC3E,SAAO,OAAO,SAAS,GAAG,KAAK,aAAa,OAAO,IAAI,QAAQ,WAAW,KAAK;AACjF;AAMO,SAAS,kBAAkB,KAA6D;AAC7F,SAAO,OAAO,QAAQ;AACxB;A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AED,UAAM,aAAa,SAAS;AAAA,EAC9B;AACF;AApLE;AA+BA;AAAA,4BAAuB,SAAC,cAAkI;AACxJ,WAAS,gBAAgB,MAAM,cAAqC;AAClE,WAAO,CAAC,CAAC;AAAA,EACX;AAEA,SAAO,CAAC,cAAc;AACpB,QAAI,CAAC,gBAAgB,YAAY,GAAG;AAClC,aAAO;AAAA,IACT;AAEA,QAAI,YAAY,YAAY,GAAG;AAG7B,aAAO;AAAA,IACT;AAEA,QAAI,CAAC,aAAa,SAAS;AACzB,aAAO;AAAA,IACT;AAEA,QAAI,WAAW;AACb,UAAI,EAAE,aAAa,aAAa,UAAU;AACxC,eAAO;AAAA,MACT;AAEA,aAAO,aAAa,QAAQ,SAAS;AAAA,IACvC;AAIA,QAAI,qBAAyC;AAC7C,UAAM,aAAa,OAAO,KAAK,aAAa,OAAO;AACnD,eAAW,QAAQ,CAAC,OAAe;AACjC,UAAI,CAAC,sBAAsB,gBAAgB,KAAK,EAAE,GAAG;AACnD,6BAAqB;AAAA,MACvB;AAAA,IACF,CAAC;AAED,QAAI,CAAC,oBAAoB;AACvB,iBAAW,QAAQ,CAAC,OAAe;AACjC,YAAI,CAAC,oBAAoB;AACvB,+BAAqB;AAAA,QACvB;AAAA,MACF,CAAC;AAAA,IACH;AAEA,QAAI,oBAAoB;AACtB,aAAO,CAAC,oBAAoB,aAAa,QAAQ,kBAAkB,GAAI,GAAI,aAAa,cAAc,CAAC,aAAa,WAAW,IAAI,CAAC,CAAE;AAAA,IACxI;AAEA,WAAO;AAAA,EACT;AACF","sourcesContent":["import type { ParameterObject, SchemaObject } from 'oas/types'\nimport { isRef, isSchema } from 'oas/types'\nimport { isObject } from 'remeda'\n\nimport type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isObject(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","import BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { findSchemaDefinition, matchesMimeType } from 'oas/utils'\nimport type { MediaType } from './types.ts'\nimport { isReference } from './utils.ts'\ntype Options = {\n mediaType?: MediaType\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 dereference(schema?: unknown, { withRef = false }: { withRef?: boolean } = {}) {\n if (isReference(schema)) {\n if (withRef) {\n return {\n ...findSchemaDefinition(schema?.$ref, this.api),\n $ref: schema.$ref,\n }\n }\n return findSchemaDefinition(schema?.$ref, this.api)\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(mediaType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (mediaType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (mediaType) => {\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 (mediaType) {\n if (!(mediaType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[mediaType]!\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 availableMediaType: string | undefined = undefined\n const mediaTypes = Object.keys(responseBody.content)\n mediaTypes.forEach((mt: string) => {\n if (!availableMediaType && matchesMimeType.json(mt)) {\n availableMediaType = mt\n }\n })\n\n if (!availableMediaType) {\n mediaTypes.forEach((mt: string) => {\n if (!availableMediaType) {\n availableMediaType = mt\n }\n })\n }\n\n if (availableMediaType) {\n return [availableMediaType, responseBody.content[availableMediaType]!, ...(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 operation.schema.responses![key] = this.dereference(operation.schema.responses![key])\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { mediaType } = this.#options\n const responseBody = getResponseBody(mediaType)\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.dereference(schema, { withRef: true })\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { mediaType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereference(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(mediaType)\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.dereference(schema, { withRef: true })\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { mediaType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereference(schema, { withRef: true })\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?.[mediaType]?.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 }\n}\n"]}
|