@kubb/oas 3.0.0-alpha.9 → 3.0.0-beta.10
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/README.md +14 -5
- package/dist/{Oas-BEe7KZDj.d.cts → Oas-Br0yKhGF.d.cts} +3 -0
- package/dist/{Oas-BEe7KZDj.d.ts → Oas-Br0yKhGF.d.ts} +3 -0
- package/dist/chunk-4ZPTFFHL.cjs +241 -0
- package/dist/chunk-4ZPTFFHL.cjs.map +1 -0
- package/dist/{chunk-LVZACNUG.js → chunk-X2CIM2ZZ.js} +63 -65
- package/dist/chunk-X2CIM2ZZ.js.map +1 -0
- package/dist/index.cjs +37 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -432
- package/dist/index.d.ts +7 -432
- package/dist/index.js +4 -21
- package/dist/index.js.map +1 -1
- package/dist/infer.cjs +4 -0
- package/dist/infer.cjs.map +1 -0
- package/dist/infer.d.cts +380 -0
- package/dist/infer.d.ts +380 -0
- package/dist/infer.js +3 -0
- package/dist/infer.js.map +1 -0
- package/dist/parser.cjs +18 -18
- package/dist/parser.cjs.map +1 -1
- package/dist/parser.d.cts +3 -44
- package/dist/parser.d.ts +3 -44
- package/dist/parser.js +11 -18
- package/dist/parser.js.map +1 -1
- package/package.json +19 -12
- package/src/Oas.ts +66 -5
- package/src/index.ts +0 -2
- package/src/parser/index.ts +5 -51
- package/src/utils.ts +1 -49
- package/dist/chunk-LVZACNUG.js.map +0 -1
- package/dist/chunk-WZOC4WVK.cjs +0 -229
- package/dist/chunk-WZOC4WVK.cjs.map +0 -1
- package/src/typings.d.ts +0 -51
package/README.md
CHANGED
|
@@ -13,12 +13,8 @@
|
|
|
13
13
|
[![Coverage][coverage-src]][coverage-href]
|
|
14
14
|
[![License][license-src]][license-href]
|
|
15
15
|
|
|
16
|
-
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
17
|
-
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
18
|
-
</p>
|
|
19
|
-
|
|
20
16
|
<h4>
|
|
21
|
-
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/
|
|
17
|
+
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/main//examples/typescript" target="_blank">View Demo</a>
|
|
22
18
|
<span> · </span>
|
|
23
19
|
<a href="https://kubb.dev/" target="_blank">Documentation</a>
|
|
24
20
|
<span> · </span>
|
|
@@ -27,6 +23,19 @@
|
|
|
27
23
|
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Request Feature</a>
|
|
28
24
|
</h4>
|
|
29
25
|
</div>
|
|
26
|
+
## Supporting Kubb
|
|
27
|
+
|
|
28
|
+
Kubb uses an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:
|
|
29
|
+
|
|
30
|
+
- [Become a Sponsor on GitHub](https://github.com/sponsors/stijnvanhulle)
|
|
31
|
+
|
|
32
|
+
<p align="center">
|
|
33
|
+
<a href="https://github.com/sponsors/stijnvanhulle">
|
|
34
|
+
<img src="https://raw.githubusercontent.com/stijnvanhulle/sponsors/main/sponsors.svg" alt="My sponsors" />
|
|
35
|
+
</a>
|
|
36
|
+
</p>
|
|
37
|
+
|
|
38
|
+
|
|
30
39
|
<!-- Badges -->
|
|
31
40
|
|
|
32
41
|
[npm-version-src]: https://img.shields.io/npm/v/@kubb/oas?flat&colorA=18181B&colorB=f58517
|
|
@@ -29,6 +29,9 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
29
29
|
oas: TOAS | OASDocument | string;
|
|
30
30
|
user?: User;
|
|
31
31
|
}, options?: Options);
|
|
32
|
+
get($ref: string): any;
|
|
33
|
+
set($ref: string, value: unknown): false | undefined;
|
|
34
|
+
resolveDiscriminators(): void;
|
|
32
35
|
dereferenceWithRef(schema?: unknown): any;
|
|
33
36
|
getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
|
|
34
37
|
getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
|
|
@@ -29,6 +29,9 @@ declare class Oas<const TOAS = unknown> extends BaseOas {
|
|
|
29
29
|
oas: TOAS | OASDocument | string;
|
|
30
30
|
user?: User;
|
|
31
31
|
}, options?: Options);
|
|
32
|
+
get($ref: string): any;
|
|
33
|
+
set($ref: string, value: unknown): false | undefined;
|
|
34
|
+
resolveDiscriminators(): void;
|
|
32
35
|
dereferenceWithRef(schema?: unknown): any;
|
|
33
36
|
getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
|
|
34
37
|
getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var types = require('oas/types');
|
|
4
|
+
var remeda = require('remeda');
|
|
5
|
+
var BaseOas = require('oas');
|
|
6
|
+
var OASNormalize = require('oas-normalize');
|
|
7
|
+
var utils = require('oas/utils');
|
|
8
|
+
var jsonpointer = require('jsonpointer');
|
|
9
|
+
|
|
10
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var BaseOas__default = /*#__PURE__*/_interopDefault(BaseOas);
|
|
13
|
+
var OASNormalize__default = /*#__PURE__*/_interopDefault(OASNormalize);
|
|
14
|
+
var jsonpointer__default = /*#__PURE__*/_interopDefault(jsonpointer);
|
|
15
|
+
|
|
16
|
+
// src/utils.ts
|
|
17
|
+
function isOpenApiV2Document(doc) {
|
|
18
|
+
return doc && remeda.isPlainObject(doc) && !("openapi" in doc);
|
|
19
|
+
}
|
|
20
|
+
function isOpenApiV3_1Document(doc) {
|
|
21
|
+
return doc && remeda.isPlainObject(doc) && "openapi" in doc && doc.openapi.startsWith("3.1");
|
|
22
|
+
}
|
|
23
|
+
function isParameterObject(obj) {
|
|
24
|
+
return obj && "in" in obj;
|
|
25
|
+
}
|
|
26
|
+
function isReference(obj) {
|
|
27
|
+
return !!obj && types.isRef(obj);
|
|
28
|
+
}
|
|
29
|
+
function isRequired(schema) {
|
|
30
|
+
if (!schema) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required;
|
|
34
|
+
}
|
|
35
|
+
function isOptional(schema) {
|
|
36
|
+
return !isRequired(schema);
|
|
37
|
+
}
|
|
38
|
+
var Oas = class extends BaseOas__default.default {
|
|
39
|
+
#options = {};
|
|
40
|
+
document = void 0;
|
|
41
|
+
constructor({ oas, user }, options = {}) {
|
|
42
|
+
if (typeof oas === "string") {
|
|
43
|
+
oas = JSON.parse(oas);
|
|
44
|
+
}
|
|
45
|
+
super(oas, user);
|
|
46
|
+
this.document = oas;
|
|
47
|
+
this.#options = options;
|
|
48
|
+
}
|
|
49
|
+
get($ref) {
|
|
50
|
+
const origRef = $ref;
|
|
51
|
+
$ref = $ref.trim();
|
|
52
|
+
if ($ref === "") {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
if ($ref.startsWith("#")) {
|
|
56
|
+
$ref = globalThis.decodeURIComponent($ref.substring(1));
|
|
57
|
+
} else {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const current = jsonpointer__default.default.get(this.api, $ref);
|
|
61
|
+
if (!current) {
|
|
62
|
+
throw new Error(`Could not find a definition for ${origRef}.`);
|
|
63
|
+
}
|
|
64
|
+
return current;
|
|
65
|
+
}
|
|
66
|
+
set($ref, value) {
|
|
67
|
+
$ref = $ref.trim();
|
|
68
|
+
if ($ref === "") {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if ($ref.startsWith("#")) {
|
|
72
|
+
$ref = globalThis.decodeURIComponent($ref.substring(1));
|
|
73
|
+
jsonpointer__default.default.set(this.api, $ref, value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
resolveDiscriminators() {
|
|
77
|
+
const schemas = this.api.components?.schemas || {};
|
|
78
|
+
Object.entries(schemas).forEach(([key, schemaObject]) => {
|
|
79
|
+
if ("discriminator" in schemaObject) {
|
|
80
|
+
const { mapping = {}, propertyName } = schemaObject.discriminator || {};
|
|
81
|
+
Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {
|
|
82
|
+
if (mappingValue) {
|
|
83
|
+
const childSchema = this.get(mappingValue);
|
|
84
|
+
const property = childSchema.properties?.[propertyName];
|
|
85
|
+
if (property) {
|
|
86
|
+
childSchema.properties[propertyName] = {
|
|
87
|
+
...childSchema.properties[propertyName],
|
|
88
|
+
enum: [...property?.enum?.filter((value) => value !== mappingKey) ?? [], mappingKey]
|
|
89
|
+
};
|
|
90
|
+
childSchema.required = [...childSchema.required ?? [], propertyName];
|
|
91
|
+
this.set(mappingValue, childSchema);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
dereferenceWithRef(schema) {
|
|
99
|
+
if (isReference(schema)) {
|
|
100
|
+
return {
|
|
101
|
+
...this.get(schema.$ref),
|
|
102
|
+
$ref: schema.$ref
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return schema;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Oas does not have a getResponseBody(contentType)
|
|
109
|
+
*/
|
|
110
|
+
#getResponseBodyFactory(responseBody) {
|
|
111
|
+
function hasResponseBody(res = responseBody) {
|
|
112
|
+
return !!res;
|
|
113
|
+
}
|
|
114
|
+
return (contentType) => {
|
|
115
|
+
if (!hasResponseBody(responseBody)) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (isReference(responseBody)) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
if (!responseBody.content) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (contentType) {
|
|
125
|
+
if (!(contentType in responseBody.content)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
return responseBody.content[contentType];
|
|
129
|
+
}
|
|
130
|
+
let availablecontentType = void 0;
|
|
131
|
+
const contentTypes = Object.keys(responseBody.content);
|
|
132
|
+
contentTypes.forEach((mt) => {
|
|
133
|
+
if (!availablecontentType && utils.matchesMimeType.json(mt)) {
|
|
134
|
+
availablecontentType = mt;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
if (!availablecontentType) {
|
|
138
|
+
contentTypes.forEach((mt) => {
|
|
139
|
+
if (!availablecontentType) {
|
|
140
|
+
availablecontentType = mt;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
if (availablecontentType) {
|
|
145
|
+
return [availablecontentType, responseBody.content[availablecontentType], ...responseBody.description ? [responseBody.description] : []];
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
getResponseSchema(operation, statusCode) {
|
|
151
|
+
if (operation.schema.responses) {
|
|
152
|
+
Object.keys(operation.schema.responses).forEach((key) => {
|
|
153
|
+
const schema2 = operation.schema.responses[key];
|
|
154
|
+
const $ref = isReference(schema2) ? schema2.$ref : void 0;
|
|
155
|
+
if (schema2 && $ref) {
|
|
156
|
+
operation.schema.responses[key] = this.get($ref);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode));
|
|
161
|
+
const { contentType } = this.#options;
|
|
162
|
+
const responseBody = getResponseBody(contentType);
|
|
163
|
+
if (responseBody === false) {
|
|
164
|
+
return {};
|
|
165
|
+
}
|
|
166
|
+
const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema;
|
|
167
|
+
if (!schema) {
|
|
168
|
+
return {};
|
|
169
|
+
}
|
|
170
|
+
return this.dereferenceWithRef(schema);
|
|
171
|
+
}
|
|
172
|
+
getRequestSchema(operation) {
|
|
173
|
+
const { contentType } = this.#options;
|
|
174
|
+
if (operation.schema.requestBody) {
|
|
175
|
+
operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody);
|
|
176
|
+
}
|
|
177
|
+
const requestBody = operation.getRequestBody(contentType);
|
|
178
|
+
if (requestBody === false) {
|
|
179
|
+
return void 0;
|
|
180
|
+
}
|
|
181
|
+
const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema;
|
|
182
|
+
if (!schema) {
|
|
183
|
+
return void 0;
|
|
184
|
+
}
|
|
185
|
+
return this.dereferenceWithRef(schema);
|
|
186
|
+
}
|
|
187
|
+
getParametersSchema(operation, inKey) {
|
|
188
|
+
const { contentType = operation.getContentType() } = this.#options;
|
|
189
|
+
const params = operation.getParameters().map((schema) => {
|
|
190
|
+
return this.dereferenceWithRef(schema);
|
|
191
|
+
}).filter((v) => v.in === inKey);
|
|
192
|
+
if (!params.length) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
return params.reduce(
|
|
196
|
+
(schema, pathParameters) => {
|
|
197
|
+
const property = pathParameters.content?.[contentType]?.schema ?? pathParameters.schema;
|
|
198
|
+
const required = [...schema.required || [], pathParameters.required ? pathParameters.name : void 0].filter(Boolean);
|
|
199
|
+
return {
|
|
200
|
+
...schema,
|
|
201
|
+
description: schema.description,
|
|
202
|
+
deprecated: schema.deprecated,
|
|
203
|
+
example: schema.example,
|
|
204
|
+
required,
|
|
205
|
+
properties: {
|
|
206
|
+
...schema.properties,
|
|
207
|
+
[pathParameters.name]: {
|
|
208
|
+
description: pathParameters.description,
|
|
209
|
+
...property
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
},
|
|
214
|
+
{ type: "object", required: [], properties: {} }
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
async valdiate() {
|
|
218
|
+
const oasNormalize = new OASNormalize__default.default(this.api, {
|
|
219
|
+
enablePaths: true,
|
|
220
|
+
colorizeErrors: true
|
|
221
|
+
});
|
|
222
|
+
await oasNormalize.validate({
|
|
223
|
+
convertToLatest: true,
|
|
224
|
+
parser: {
|
|
225
|
+
validate: {
|
|
226
|
+
colorizeErrors: true
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
exports.Oas = Oas;
|
|
234
|
+
exports.isOpenApiV2Document = isOpenApiV2Document;
|
|
235
|
+
exports.isOpenApiV3_1Document = isOpenApiV3_1Document;
|
|
236
|
+
exports.isOptional = isOptional;
|
|
237
|
+
exports.isParameterObject = isParameterObject;
|
|
238
|
+
exports.isReference = isReference;
|
|
239
|
+
exports.isRequired = isRequired;
|
|
240
|
+
//# sourceMappingURL=chunk-4ZPTFFHL.cjs.map
|
|
241
|
+
//# sourceMappingURL=chunk-4ZPTFFHL.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/Oas.ts"],"names":["isPlainObject","isRef","BaseOas","jsonpointer","matchesMimeType","schema","OASNormalize"],"mappings":";;;;;;;;;;;;;;;;AAMO,SAAS,oBAAoB,GAAqC,EAAA;AACvE,EAAA,OAAO,GAAO,IAAAA,oBAAA,CAAc,GAAG,CAAA,IAAK,EAAE,SAAa,IAAA,GAAA,CAAA;AACrD;AAKO,SAAS,sBAAsB,GAAuC,EAAA;AAC3E,EAAO,OAAA,GAAA,IAAOA,qBAAoC,GAAG,CAAA,IAAK,aAAa,GAAO,IAAA,GAAA,CAAI,OAAQ,CAAA,UAAA,CAAW,KAAK,CAAA;AAC5G;AAMO,SAAS,kBAAkB,GAA6D,EAAA;AAC7F,EAAA,OAAO,OAAO,IAAQ,IAAA,GAAA;AACxB;AAEO,SAAS,YAAY,GAA+E,EAAA;AACzG,EAAA,OAAO,CAAC,CAAC,GAAO,IAAAC,WAAA,CAAM,GAAG,CAAA;AAC3B;AAEO,SAAS,WAAW,MAAgC,EAAA;AACzD,EAAA,IAAI,CAAC,MAAQ,EAAA;AACX,IAAO,OAAA,KAAA;AAAA;AAGT,EAAA,OAAO,KAAM,CA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EAAA;AAAA,KACjB,CAAA;AAED,IAAA,MAAM,aAAa,QAAS,CAAA;AAAA,MAC1B,eAAiB,EAAA,IAAA;AAAA,MACjB,MAAQ,EAAA;AAAA,QACN,QAAU,EAAA;AAAA,UACR,cAAgB,EAAA;AAAA;AAClB;AACF,KACD,CAAA;AAAA;AAEL","file":"chunk-4ZPTFFHL.cjs","sourcesContent":["import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\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","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"]}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import { isRef } from 'oas/types';
|
|
2
|
+
import { isPlainObject } from 'remeda';
|
|
3
|
+
import BaseOas from 'oas';
|
|
4
|
+
import OASNormalize from 'oas-normalize';
|
|
5
|
+
import { matchesMimeType } from 'oas/utils';
|
|
6
|
+
import jsonpointer from 'jsonpointer';
|
|
7
|
+
|
|
1
8
|
// src/utils.ts
|
|
2
|
-
import { isRef, isSchema } from "oas/types";
|
|
3
|
-
import openapiFormat from "openapi-format";
|
|
4
|
-
import { isPlainObject } from "remeda";
|
|
5
9
|
function isOpenApiV2Document(doc) {
|
|
6
10
|
return doc && isPlainObject(doc) && !("openapi" in doc);
|
|
7
11
|
}
|
|
@@ -23,52 +27,6 @@ function isRequired(schema) {
|
|
|
23
27
|
function isOptional(schema) {
|
|
24
28
|
return !isRequired(schema);
|
|
25
29
|
}
|
|
26
|
-
async function filterAndSort(data, options = {}) {
|
|
27
|
-
const mergedOptions = {
|
|
28
|
-
sort: options.sort ?? true,
|
|
29
|
-
["no-sort"]: options["no-sort"] ?? false,
|
|
30
|
-
sortSet: {
|
|
31
|
-
root: ["openapi", "info", "servers", "paths", "components", "tags", "x-tagGroups", "externalDocs"],
|
|
32
|
-
get: ["operationId", "summary", "description", "parameters", "requestBody", "responses"],
|
|
33
|
-
post: ["operationId", "summary", "description", "parameters", "requestBody", "responses"],
|
|
34
|
-
put: ["operationId", "summary", "description", "parameters", "requestBody", "responses"],
|
|
35
|
-
patch: ["operationId", "summary", "description", "parameters", "requestBody", "responses"],
|
|
36
|
-
delete: ["operationId", "summary", "description", "parameters", "requestBody", "responses"],
|
|
37
|
-
parameters: ["name", "in", "description", "required", "schema"],
|
|
38
|
-
requestBody: ["description", "required", "content"],
|
|
39
|
-
responses: ["description", "headers", "content", "links"],
|
|
40
|
-
content: [],
|
|
41
|
-
components: ["parameters", "schemas"],
|
|
42
|
-
schema: ["description", "type", "items", "properties", "format", "example", "default"],
|
|
43
|
-
schemas: ["description", "type", "items", "properties", "format", "example", "default"],
|
|
44
|
-
properties: ["description", "type", "items", "format", "example", "default", "enum"],
|
|
45
|
-
...options.sortSet
|
|
46
|
-
},
|
|
47
|
-
sortComponentsSet: {
|
|
48
|
-
...options.sortComponentsSet
|
|
49
|
-
},
|
|
50
|
-
filterSet: {
|
|
51
|
-
inverseMethods: ["get", "put", "post", "delete", "patch", "head", "options", "trace", "parameters"],
|
|
52
|
-
unusedComponents: options.filterSet ? ["requestBodies", "schemas", "parameters", "responses"] : [],
|
|
53
|
-
...options.filterSet
|
|
54
|
-
},
|
|
55
|
-
casingSet: {
|
|
56
|
-
...options.casingSet
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
const restFilter = await openapiFormat.openapiFilter(data, mergedOptions);
|
|
60
|
-
data = restFilter.data;
|
|
61
|
-
const resFormat = await openapiFormat.openapiSort(data, mergedOptions);
|
|
62
|
-
data = resFormat.data;
|
|
63
|
-
const resChangeCase = await openapiFormat.openapiChangeCase(data, mergedOptions);
|
|
64
|
-
data = resChangeCase.data;
|
|
65
|
-
return data;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// src/Oas.ts
|
|
69
|
-
import BaseOas from "oas";
|
|
70
|
-
import OASNormalize from "oas-normalize";
|
|
71
|
-
import { findSchemaDefinition, matchesMimeType } from "oas/utils";
|
|
72
30
|
var Oas = class extends BaseOas {
|
|
73
31
|
#options = {};
|
|
74
32
|
document = void 0;
|
|
@@ -80,10 +38,59 @@ var Oas = class extends BaseOas {
|
|
|
80
38
|
this.document = oas;
|
|
81
39
|
this.#options = options;
|
|
82
40
|
}
|
|
41
|
+
get($ref) {
|
|
42
|
+
const origRef = $ref;
|
|
43
|
+
$ref = $ref.trim();
|
|
44
|
+
if ($ref === "") {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if ($ref.startsWith("#")) {
|
|
48
|
+
$ref = globalThis.decodeURIComponent($ref.substring(1));
|
|
49
|
+
} else {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const current = jsonpointer.get(this.api, $ref);
|
|
53
|
+
if (!current) {
|
|
54
|
+
throw new Error(`Could not find a definition for ${origRef}.`);
|
|
55
|
+
}
|
|
56
|
+
return current;
|
|
57
|
+
}
|
|
58
|
+
set($ref, value) {
|
|
59
|
+
$ref = $ref.trim();
|
|
60
|
+
if ($ref === "") {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if ($ref.startsWith("#")) {
|
|
64
|
+
$ref = globalThis.decodeURIComponent($ref.substring(1));
|
|
65
|
+
jsonpointer.set(this.api, $ref, value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
resolveDiscriminators() {
|
|
69
|
+
const schemas = this.api.components?.schemas || {};
|
|
70
|
+
Object.entries(schemas).forEach(([key, schemaObject]) => {
|
|
71
|
+
if ("discriminator" in schemaObject) {
|
|
72
|
+
const { mapping = {}, propertyName } = schemaObject.discriminator || {};
|
|
73
|
+
Object.entries(mapping).forEach(([mappingKey, mappingValue]) => {
|
|
74
|
+
if (mappingValue) {
|
|
75
|
+
const childSchema = this.get(mappingValue);
|
|
76
|
+
const property = childSchema.properties?.[propertyName];
|
|
77
|
+
if (property) {
|
|
78
|
+
childSchema.properties[propertyName] = {
|
|
79
|
+
...childSchema.properties[propertyName],
|
|
80
|
+
enum: [...property?.enum?.filter((value) => value !== mappingKey) ?? [], mappingKey]
|
|
81
|
+
};
|
|
82
|
+
childSchema.required = [...childSchema.required ?? [], propertyName];
|
|
83
|
+
this.set(mappingValue, childSchema);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
83
90
|
dereferenceWithRef(schema) {
|
|
84
91
|
if (isReference(schema)) {
|
|
85
92
|
return {
|
|
86
|
-
...
|
|
93
|
+
...this.get(schema.$ref),
|
|
87
94
|
$ref: schema.$ref
|
|
88
95
|
};
|
|
89
96
|
}
|
|
@@ -138,7 +145,7 @@ var Oas = class extends BaseOas {
|
|
|
138
145
|
const schema2 = operation.schema.responses[key];
|
|
139
146
|
const $ref = isReference(schema2) ? schema2.$ref : void 0;
|
|
140
147
|
if (schema2 && $ref) {
|
|
141
|
-
operation.schema.responses[key] =
|
|
148
|
+
operation.schema.responses[key] = this.get($ref);
|
|
142
149
|
}
|
|
143
150
|
});
|
|
144
151
|
}
|
|
@@ -205,25 +212,16 @@ var Oas = class extends BaseOas {
|
|
|
205
212
|
colorizeErrors: true
|
|
206
213
|
});
|
|
207
214
|
await oasNormalize.validate({
|
|
215
|
+
convertToLatest: true,
|
|
208
216
|
parser: {
|
|
209
217
|
validate: {
|
|
210
|
-
colorizeErrors: true
|
|
211
|
-
schema: false,
|
|
212
|
-
spec: false
|
|
218
|
+
colorizeErrors: true
|
|
213
219
|
}
|
|
214
220
|
}
|
|
215
221
|
});
|
|
216
222
|
}
|
|
217
223
|
};
|
|
218
224
|
|
|
219
|
-
export {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
isParameterObject,
|
|
223
|
-
isReference,
|
|
224
|
-
isRequired,
|
|
225
|
-
isOptional,
|
|
226
|
-
filterAndSort,
|
|
227
|
-
Oas
|
|
228
|
-
};
|
|
229
|
-
//# sourceMappingURL=chunk-LVZACNUG.js.map
|
|
225
|
+
export { Oas, isOpenApiV2Document, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired };
|
|
226
|
+
//# sourceMappingURL=chunk-X2CIM2ZZ.js.map
|
|
227
|
+
//# sourceMappingURL=chunk-X2CIM2ZZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/Oas.ts"],"names":["schema"],"mappings":";;;;;;;;AAMO,SAAS,oBAAoB,GAAqC,EAAA;AACvE,EAAA,OAAO,GAAO,IAAA,aAAA,CAAc,GAAG,CAAA,IAAK,EAAE,SAAa,IAAA,GAAA,CAAA;AACrD;AAKO,SAAS,sBAAsB,GAAuC,EAAA;AAC3E,EAAO,OAAA,GAAA,IAAO,cAAoC,GAAG,CAAA,IAAK,aAAa,GAAO,IAAA,GAAA,CAAI,OAAQ,CAAA,UAAA,CAAW,KAAK,CAAA;AAC5G;AAMO,SAAS,kBAAkB,GAA6D,EAAA;AAC7F,EAAA,OAAO,OAAO,IAAQ,IAAA,GAAA;AACxB;AAEO,SAAS,YAAY,GAA+E,EAAA;AACzG,EAAA,OAAO,CAAC,CAAC,GAAO,IAAA,KAAA,CAAM,GA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W,GAAA;AACf,IAAA,MAAM,YAAe,GAAA,IAAI,YAAa,CAAA,IAAA,CAAK,GAAK,EAAA;AAAA,MAC9C,WAAa,EAAA,IAAA;AAAA,MACb,cAAgB,EAAA;AAAA,KACjB,CAAA;AAED,IAAA,MAAM,aAAa,QAAS,CAAA;AAAA,MAC1B,eAAiB,EAAA,IAAA;AAAA,MACjB,MAAQ,EAAA;AAAA,QACN,QAAU,EAAA;AAAA,UACR,cAAgB,EAAA;AAAA;AAClB;AACF,KACD,CAAA;AAAA;AAEL","file":"chunk-X2CIM2ZZ.js","sourcesContent":["import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\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","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"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var _chunkWZOC4WVKcjs = require('./chunk-WZOC4WVK.cjs');
|
|
3
|
+
var chunk4ZPTFFHL_cjs = require('./chunk-4ZPTFFHL.cjs');
|
|
4
|
+
var utils = require('oas/utils');
|
|
9
5
|
|
|
10
6
|
// src/types.ts
|
|
11
7
|
var HttpMethods = {
|
|
@@ -19,17 +15,38 @@ var HttpMethods = {
|
|
|
19
15
|
TRACE: "trace"
|
|
20
16
|
};
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
18
|
+
Object.defineProperty(exports, "Oas", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () { return chunk4ZPTFFHL_cjs.Oas; }
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(exports, "isOpenApiV3_1Document", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () { return chunk4ZPTFFHL_cjs.isOpenApiV3_1Document; }
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(exports, "isOptional", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () { return chunk4ZPTFFHL_cjs.isOptional; }
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, "isParameterObject", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () { return chunk4ZPTFFHL_cjs.isParameterObject; }
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(exports, "isReference", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: function () { return chunk4ZPTFFHL_cjs.isReference; }
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(exports, "isRequired", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () { return chunk4ZPTFFHL_cjs.isRequired; }
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, "findSchemaDefinition", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () { return utils.findSchemaDefinition; }
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(exports, "matchesMimeType", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
get: function () { return utils.matchesMimeType; }
|
|
49
|
+
});
|
|
50
|
+
exports.HttpMethods = HttpMethods;
|
|
51
|
+
//# sourceMappingURL=index.cjs.map
|
|
35
52
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
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.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"]}
|