@kubb/oas 0.0.0-canary-20240422211850

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Stijn Van Hulle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ <div align="center">
2
+
3
+ <!-- <img src="assets/logo.png" alt="logo" width="200" height="auto" /> -->
4
+ <h1>@kubb/oas</h1>
5
+
6
+ <p>
7
+ Oas helpers
8
+ </p>
9
+ <img src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/banner.png" alt="logo" height="auto" />
10
+
11
+ [![npm version][npm-version-src]][npm-version-href]
12
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
13
+ [![Coverage][coverage-src]][coverage-href]
14
+ [![License][license-src]][license-href]
15
+
16
+ <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
17
+ <!-- ALL-CONTRIBUTORS-BADGE:END -->
18
+ </p>
19
+
20
+ <h4>
21
+ <a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/alpha/examples/typescript" target="_blank">View Demo</a>
22
+ <span> · </span>
23
+ <a href="https://kubb.dev/" target="_blank">Documentation</a>
24
+ <span> · </span>
25
+ <a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
26
+ <span> · </span>
27
+ <a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Request Feature</a>
28
+ </h4>
29
+ </div>
30
+ <!-- Badges -->
31
+
32
+ [npm-version-src]: https://img.shields.io/npm/v/@kubb/oas?flat&colorA=18181B&colorB=f58517
33
+ [npm-version-href]: https://npmjs.com/package/@kubb/oas
34
+ [npm-downloads-src]: https://img.shields.io/npm/dm/@kubb/oas?flat&colorA=18181B&colorB=f58517
35
+ [npm-downloads-href]: https://npmjs.com/package/@kubb/oas
36
+ [license-src]: https://img.shields.io/github/license/kubb-labs/kubb.svg?flat&colorA=18181B&colorB=f58517
37
+ [license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
38
+ [build-src]: https://img.shields.io/github/actions/workflow/status/kubb-labs/kubb/ci.yaml?style=flat&colorA=18181B&colorB=f58517
39
+ [build-href]: https://www.npmjs.com/package/@kubb/oas
40
+ [minified-src]: https://img.shields.io/bundlephobia/min/@kubb/oas?style=flat&colorA=18181B&colorB=f58517
41
+ [minified-href]: https://www.npmjs.com/package/@kubb/oas
42
+ [coverage-src]: https://img.shields.io/codecov/c/github/kubb-labs/kubb?style=flat&colorA=18181B&colorB=f58517
43
+ [coverage-href]: https://www.npmjs.com/package/@kubb/oas
@@ -0,0 +1,41 @@
1
+ import BaseOas from 'oas';
2
+ import { Operation } from 'oas/operation';
3
+ import * as OasTypes from 'oas/types';
4
+ import { OASDocument, User, SchemaObject as SchemaObject$1 } from 'oas/types';
5
+
6
+ type MediaType = 'application/json' | (string & {});
7
+ type SchemaObject = OasTypes.SchemaObject & {
8
+ 'x-nullable'?: boolean;
9
+ $ref?: string;
10
+ };
11
+ declare const HttpMethods: {
12
+ GET: "get";
13
+ POST: "post";
14
+ PUT: "put";
15
+ PATCH: "patch";
16
+ DELETE: "delete";
17
+ HEAD: "head";
18
+ OPTIONS: "options";
19
+ TRACE: "trace";
20
+ };
21
+
22
+ type Options = {
23
+ mediaType?: MediaType;
24
+ };
25
+ declare class Oas<const TOAS = unknown> extends BaseOas {
26
+ #private;
27
+ document: TOAS;
28
+ constructor({ oas, user }: {
29
+ oas: TOAS | OASDocument | string;
30
+ user?: User;
31
+ }, options?: Options);
32
+ dereference(schema?: unknown, { withRef }?: {
33
+ withRef?: boolean;
34
+ }): any;
35
+ getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
36
+ getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
37
+ getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject$1 | null;
38
+ valdiate(): Promise<void>;
39
+ }
40
+
41
+ export { HttpMethods as H, type MediaType as M, Oas as O, type SchemaObject as S };
@@ -0,0 +1,41 @@
1
+ import BaseOas from 'oas';
2
+ import { Operation } from 'oas/operation';
3
+ import * as OasTypes from 'oas/types';
4
+ import { OASDocument, User, SchemaObject as SchemaObject$1 } from 'oas/types';
5
+
6
+ type MediaType = 'application/json' | (string & {});
7
+ type SchemaObject = OasTypes.SchemaObject & {
8
+ 'x-nullable'?: boolean;
9
+ $ref?: string;
10
+ };
11
+ declare const HttpMethods: {
12
+ GET: "get";
13
+ POST: "post";
14
+ PUT: "put";
15
+ PATCH: "patch";
16
+ DELETE: "delete";
17
+ HEAD: "head";
18
+ OPTIONS: "options";
19
+ TRACE: "trace";
20
+ };
21
+
22
+ type Options = {
23
+ mediaType?: MediaType;
24
+ };
25
+ declare class Oas<const TOAS = unknown> extends BaseOas {
26
+ #private;
27
+ document: TOAS;
28
+ constructor({ oas, user }: {
29
+ oas: TOAS | OASDocument | string;
30
+ user?: User;
31
+ }, options?: Options);
32
+ dereference(schema?: unknown, { withRef }?: {
33
+ withRef?: boolean;
34
+ }): any;
35
+ getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject$1;
36
+ getRequestSchema(operation: Operation): SchemaObject$1 | undefined;
37
+ getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject$1 | null;
38
+ valdiate(): Promise<void>;
39
+ }
40
+
41
+ export { HttpMethods as H, type MediaType as M, Oas as O, type SchemaObject as S };
@@ -0,0 +1,200 @@
1
+ var __accessCheck = (obj, member, msg) => {
2
+ if (!member.has(obj))
3
+ throw TypeError("Cannot " + msg);
4
+ };
5
+ var __privateGet = (obj, member, getter) => {
6
+ __accessCheck(obj, member, "read from private field");
7
+ return getter ? getter.call(obj) : member.get(obj);
8
+ };
9
+ var __privateAdd = (obj, member, value) => {
10
+ if (member.has(obj))
11
+ throw TypeError("Cannot add the same private member more than once");
12
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
+ };
14
+ var __privateSet = (obj, member, value, setter) => {
15
+ __accessCheck(obj, member, "write to private field");
16
+ setter ? setter.call(obj, value) : member.set(obj, value);
17
+ return value;
18
+ };
19
+ var __privateMethod = (obj, member, method) => {
20
+ __accessCheck(obj, member, "access private method");
21
+ return method;
22
+ };
23
+
24
+ // src/utils.ts
25
+ import { isRef, isSchema } from "oas/types";
26
+ import { isObject } from "remeda";
27
+ function isOpenApiV2Document(doc) {
28
+ return doc && isObject(doc) && !("openapi" in doc);
29
+ }
30
+ function isOpenApiV3_1Document(doc) {
31
+ return doc && isObject(doc) && "openapi" in doc && doc.openapi.startsWith("3.1");
32
+ }
33
+ function isParameterObject(obj) {
34
+ return obj && "in" in obj;
35
+ }
36
+ function isReference(obj) {
37
+ return !!obj && isRef(obj);
38
+ }
39
+ function isRequired(schema) {
40
+ if (!schema) {
41
+ return false;
42
+ }
43
+ return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required;
44
+ }
45
+
46
+ // src/Oas.ts
47
+ import BaseOas from "oas";
48
+ import OASNormalize from "oas-normalize";
49
+ import { findSchemaDefinition, matchesMimeType } from "oas/utils";
50
+ var _options, _getResponseBodyFactory, getResponseBodyFactory_fn;
51
+ var Oas = class extends BaseOas {
52
+ constructor({ oas, user }, options = {}) {
53
+ if (typeof oas === "string") {
54
+ oas = JSON.parse(oas);
55
+ }
56
+ super(oas, user);
57
+ /**
58
+ * Oas does not have a getResponseBody(mediaType)
59
+ */
60
+ __privateAdd(this, _getResponseBodyFactory);
61
+ __privateAdd(this, _options, {});
62
+ this.document = void 0;
63
+ this.document = oas;
64
+ __privateSet(this, _options, options);
65
+ }
66
+ dereference(schema, { withRef = false } = {}) {
67
+ if (isReference(schema)) {
68
+ if (withRef) {
69
+ return {
70
+ ...findSchemaDefinition(schema?.$ref, this.api),
71
+ $ref: schema.$ref
72
+ };
73
+ }
74
+ return findSchemaDefinition(schema?.$ref, this.api);
75
+ }
76
+ return schema;
77
+ }
78
+ getResponseSchema(operation, statusCode) {
79
+ if (operation.schema.responses) {
80
+ Object.keys(operation.schema.responses).forEach((key) => {
81
+ operation.schema.responses[key] = this.dereference(operation.schema.responses[key]);
82
+ });
83
+ }
84
+ const getResponseBody = __privateMethod(this, _getResponseBodyFactory, getResponseBodyFactory_fn).call(this, operation.getResponseByStatusCode(statusCode));
85
+ const { mediaType } = __privateGet(this, _options);
86
+ const responseBody = getResponseBody(mediaType);
87
+ if (responseBody === false) {
88
+ return {};
89
+ }
90
+ const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema;
91
+ if (!schema) {
92
+ return {};
93
+ }
94
+ return this.dereference(schema, { withRef: true });
95
+ }
96
+ getRequestSchema(operation) {
97
+ const { mediaType } = __privateGet(this, _options);
98
+ if (operation.schema.requestBody) {
99
+ operation.schema.requestBody = this.dereference(operation.schema.requestBody);
100
+ }
101
+ const requestBody = operation.getRequestBody(mediaType);
102
+ if (requestBody === false) {
103
+ return void 0;
104
+ }
105
+ const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema;
106
+ if (!schema) {
107
+ return void 0;
108
+ }
109
+ return this.dereference(schema, { withRef: true });
110
+ }
111
+ getParametersSchema(operation, inKey) {
112
+ const { mediaType = operation.getContentType() } = __privateGet(this, _options);
113
+ const params = operation.getParameters().map((schema) => {
114
+ return this.dereference(schema, { withRef: true });
115
+ }).filter((v) => v.in === inKey);
116
+ if (!params.length) {
117
+ return null;
118
+ }
119
+ return params.reduce(
120
+ (schema, pathParameters) => {
121
+ const property = pathParameters.content?.[mediaType]?.schema ?? pathParameters.schema;
122
+ const required = [...schema.required || [], pathParameters.required ? pathParameters.name : void 0].filter(Boolean);
123
+ return {
124
+ ...schema,
125
+ description: schema.description,
126
+ deprecated: schema.deprecated,
127
+ example: schema.example,
128
+ required,
129
+ properties: {
130
+ ...schema.properties,
131
+ [pathParameters.name]: {
132
+ description: pathParameters.description,
133
+ ...property
134
+ }
135
+ }
136
+ };
137
+ },
138
+ { type: "object", required: [], properties: {} }
139
+ );
140
+ }
141
+ async valdiate() {
142
+ const oasNormalize = new OASNormalize(this.api, {
143
+ enablePaths: true,
144
+ colorizeErrors: true
145
+ });
146
+ await oasNormalize.validate();
147
+ }
148
+ };
149
+ _options = new WeakMap();
150
+ _getResponseBodyFactory = new WeakSet();
151
+ getResponseBodyFactory_fn = function(responseBody) {
152
+ function hasResponseBody(res = responseBody) {
153
+ return !!res;
154
+ }
155
+ return (mediaType) => {
156
+ if (!hasResponseBody(responseBody)) {
157
+ return false;
158
+ }
159
+ if (isReference(responseBody)) {
160
+ return false;
161
+ }
162
+ if (!responseBody.content) {
163
+ return false;
164
+ }
165
+ if (mediaType) {
166
+ if (!(mediaType in responseBody.content)) {
167
+ return false;
168
+ }
169
+ return responseBody.content[mediaType];
170
+ }
171
+ let availableMediaType = void 0;
172
+ const mediaTypes = Object.keys(responseBody.content);
173
+ mediaTypes.forEach((mt) => {
174
+ if (!availableMediaType && matchesMimeType.json(mt)) {
175
+ availableMediaType = mt;
176
+ }
177
+ });
178
+ if (!availableMediaType) {
179
+ mediaTypes.forEach((mt) => {
180
+ if (!availableMediaType) {
181
+ availableMediaType = mt;
182
+ }
183
+ });
184
+ }
185
+ if (availableMediaType) {
186
+ return [availableMediaType, responseBody.content[availableMediaType], ...responseBody.description ? [responseBody.description] : []];
187
+ }
188
+ return false;
189
+ };
190
+ };
191
+
192
+ export {
193
+ isOpenApiV2Document,
194
+ isOpenApiV3_1Document,
195
+ isParameterObject,
196
+ isReference,
197
+ isRequired,
198
+ Oas
199
+ };
200
+ //# sourceMappingURL=chunk-U2H7VGLT.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 { 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;AAEO,SAAS,YAAY,KAA+E;AACzG,SAAO,CAAC,CAAC,OAAO,MAAM,GAAG;AAC3B;AAEO,SAAS,WAAW,QAAgC;AACzD,MAAI,CAAC,QAAQ;AACX,WAAO;AAAA,EACT;AAEA,SAAO,MAAM,QAAQ,OAAO,QAAQ,IAAI,CAAC,CAAC,OAAO,UAAU,SAAS,CAAC,CAAC,OAAO;AAC/E;;;ACnCA,OAAO,aAAa;AACpB,OAAO,kBAAkB;AAGzB,SAAS,sBAAsB,uBAAuB;AAJtD;AAWO,IAAM,MAAN,cAAwC,QAAQ;AAAA,EAIrD,YAAY,EAAE,KAAK,KAAK,GAAsD,UAAmB,CAAC,GAAG;AACnG,QAAI,OAAO,QAAQ,UAAU;AAC3B,YAAM,KAAK,MAAM,GAAG;AAAA,IACtB;AAEA,UAAM,KAAoB,IAAI;AAuBhC;AAAA;AAAA;AAAA;AA/BA,iCAAoB,CAAC;AACrB,oBAAiB;AASf,SAAK,WAAW;AAChB,uBAAK,UAAW;AAAA,EAClB;AAAA,EAEA,YAAY,QAAkB,EAAE,UAAU,MAAM,IAA2B,CAAC,GAAG;AAC7E,QAAI,YAAY,MAAM,GAAG;AACvB,UAAI,SA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B,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;","names":[]}
@@ -0,0 +1,200 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var __accessCheck = (obj, member, msg) => {
2
+ if (!member.has(obj))
3
+ throw TypeError("Cannot " + msg);
4
+ };
5
+ var __privateGet = (obj, member, getter) => {
6
+ __accessCheck(obj, member, "read from private field");
7
+ return getter ? getter.call(obj) : member.get(obj);
8
+ };
9
+ var __privateAdd = (obj, member, value) => {
10
+ if (member.has(obj))
11
+ throw TypeError("Cannot add the same private member more than once");
12
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
+ };
14
+ var __privateSet = (obj, member, value, setter) => {
15
+ __accessCheck(obj, member, "write to private field");
16
+ setter ? setter.call(obj, value) : member.set(obj, value);
17
+ return value;
18
+ };
19
+ var __privateMethod = (obj, member, method) => {
20
+ __accessCheck(obj, member, "access private method");
21
+ return method;
22
+ };
23
+
24
+ // src/utils.ts
25
+ var _types = require('oas/types');
26
+ var _remeda = require('remeda');
27
+ function isOpenApiV2Document(doc) {
28
+ return doc && _remeda.isObject.call(void 0, doc) && !("openapi" in doc);
29
+ }
30
+ function isOpenApiV3_1Document(doc) {
31
+ return doc && _remeda.isObject.call(void 0, doc) && "openapi" in doc && doc.openapi.startsWith("3.1");
32
+ }
33
+ function isParameterObject(obj) {
34
+ return obj && "in" in obj;
35
+ }
36
+ function isReference(obj) {
37
+ return !!obj && _types.isRef.call(void 0, obj);
38
+ }
39
+ function isRequired(schema) {
40
+ if (!schema) {
41
+ return false;
42
+ }
43
+ return Array.isArray(schema.required) ? !!_optionalChain([schema, 'access', _ => _.required, 'optionalAccess', _2 => _2.length]) : !!schema.required;
44
+ }
45
+
46
+ // src/Oas.ts
47
+ var _oas = require('oas'); var _oas2 = _interopRequireDefault(_oas);
48
+ var _oasnormalize = require('oas-normalize'); var _oasnormalize2 = _interopRequireDefault(_oasnormalize);
49
+ var _utils = require('oas/utils');
50
+ var _options, _getResponseBodyFactory, getResponseBodyFactory_fn;
51
+ var Oas = class extends _oas2.default {
52
+ constructor({ oas, user }, options = {}) {
53
+ if (typeof oas === "string") {
54
+ oas = JSON.parse(oas);
55
+ }
56
+ super(oas, user);
57
+ /**
58
+ * Oas does not have a getResponseBody(mediaType)
59
+ */
60
+ __privateAdd(this, _getResponseBodyFactory);
61
+ __privateAdd(this, _options, {});
62
+ this.document = void 0;
63
+ this.document = oas;
64
+ __privateSet(this, _options, options);
65
+ }
66
+ dereference(schema, { withRef = false } = {}) {
67
+ if (isReference(schema)) {
68
+ if (withRef) {
69
+ return {
70
+ ..._utils.findSchemaDefinition.call(void 0, _optionalChain([schema, 'optionalAccess', _3 => _3.$ref]), this.api),
71
+ $ref: schema.$ref
72
+ };
73
+ }
74
+ return _utils.findSchemaDefinition.call(void 0, _optionalChain([schema, 'optionalAccess', _4 => _4.$ref]), this.api);
75
+ }
76
+ return schema;
77
+ }
78
+ getResponseSchema(operation, statusCode) {
79
+ if (operation.schema.responses) {
80
+ Object.keys(operation.schema.responses).forEach((key) => {
81
+ operation.schema.responses[key] = this.dereference(operation.schema.responses[key]);
82
+ });
83
+ }
84
+ const getResponseBody = __privateMethod(this, _getResponseBodyFactory, getResponseBodyFactory_fn).call(this, operation.getResponseByStatusCode(statusCode));
85
+ const { mediaType } = __privateGet(this, _options);
86
+ const responseBody = getResponseBody(mediaType);
87
+ if (responseBody === false) {
88
+ return {};
89
+ }
90
+ const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema;
91
+ if (!schema) {
92
+ return {};
93
+ }
94
+ return this.dereference(schema, { withRef: true });
95
+ }
96
+ getRequestSchema(operation) {
97
+ const { mediaType } = __privateGet(this, _options);
98
+ if (operation.schema.requestBody) {
99
+ operation.schema.requestBody = this.dereference(operation.schema.requestBody);
100
+ }
101
+ const requestBody = operation.getRequestBody(mediaType);
102
+ if (requestBody === false) {
103
+ return void 0;
104
+ }
105
+ const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema;
106
+ if (!schema) {
107
+ return void 0;
108
+ }
109
+ return this.dereference(schema, { withRef: true });
110
+ }
111
+ getParametersSchema(operation, inKey) {
112
+ const { mediaType = operation.getContentType() } = __privateGet(this, _options);
113
+ const params = operation.getParameters().map((schema) => {
114
+ return this.dereference(schema, { withRef: true });
115
+ }).filter((v) => v.in === inKey);
116
+ if (!params.length) {
117
+ return null;
118
+ }
119
+ return params.reduce(
120
+ (schema, pathParameters) => {
121
+ const property = _nullishCoalesce(_optionalChain([pathParameters, 'access', _5 => _5.content, 'optionalAccess', _6 => _6[mediaType], 'optionalAccess', _7 => _7.schema]), () => ( pathParameters.schema));
122
+ const required = [...schema.required || [], pathParameters.required ? pathParameters.name : void 0].filter(Boolean);
123
+ return {
124
+ ...schema,
125
+ description: schema.description,
126
+ deprecated: schema.deprecated,
127
+ example: schema.example,
128
+ required,
129
+ properties: {
130
+ ...schema.properties,
131
+ [pathParameters.name]: {
132
+ description: pathParameters.description,
133
+ ...property
134
+ }
135
+ }
136
+ };
137
+ },
138
+ { type: "object", required: [], properties: {} }
139
+ );
140
+ }
141
+ async valdiate() {
142
+ const oasNormalize = new (0, _oasnormalize2.default)(this.api, {
143
+ enablePaths: true,
144
+ colorizeErrors: true
145
+ });
146
+ await oasNormalize.validate();
147
+ }
148
+ };
149
+ _options = new WeakMap();
150
+ _getResponseBodyFactory = new WeakSet();
151
+ getResponseBodyFactory_fn = function(responseBody) {
152
+ function hasResponseBody(res = responseBody) {
153
+ return !!res;
154
+ }
155
+ return (mediaType) => {
156
+ if (!hasResponseBody(responseBody)) {
157
+ return false;
158
+ }
159
+ if (isReference(responseBody)) {
160
+ return false;
161
+ }
162
+ if (!responseBody.content) {
163
+ return false;
164
+ }
165
+ if (mediaType) {
166
+ if (!(mediaType in responseBody.content)) {
167
+ return false;
168
+ }
169
+ return responseBody.content[mediaType];
170
+ }
171
+ let availableMediaType = void 0;
172
+ const mediaTypes = Object.keys(responseBody.content);
173
+ mediaTypes.forEach((mt) => {
174
+ if (!availableMediaType && _utils.matchesMimeType.json(mt)) {
175
+ availableMediaType = mt;
176
+ }
177
+ });
178
+ if (!availableMediaType) {
179
+ mediaTypes.forEach((mt) => {
180
+ if (!availableMediaType) {
181
+ availableMediaType = mt;
182
+ }
183
+ });
184
+ }
185
+ if (availableMediaType) {
186
+ return [availableMediaType, responseBody.content[availableMediaType], ...responseBody.description ? [responseBody.description] : []];
187
+ }
188
+ return false;
189
+ };
190
+ };
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+ exports.isOpenApiV2Document = isOpenApiV2Document; exports.isOpenApiV3_1Document = isOpenApiV3_1Document; exports.isParameterObject = isParameterObject; exports.isReference = isReference; exports.isRequired = isRequired; exports.Oas = Oas;
200
+ //# sourceMappingURL=chunk-WMBNWD46.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts","../src/Oas.ts"],"names":[],"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;AAEO,SAAS,YAAY,KAA+E;AACzG,SAAO,CAAC,CAAC,OAAO,MAAM,GAAG;AAC3B;AAEO,SAAS,WAAW,QAAgC;AACzD,MAAI,CAAC,QAAQ;AACX,WAAO;AAAA,EACT;AAEA,SAAO,MAAM,QAAQ,OAAO,QAAQ,IAAI,CAAC,CAAC,OAAO,UAAU,SAAS,CAAC,CAAC,OAAO;AAC/E;;;ACnCA,OAAO,aAAa;AACpB,OAAO,kBAAkB;AAGzB,SAAS,sBAAsB,uBAAuB;AAJtD;AAWO,IAAM,MAAN,cAAwC,QAAQ;AAAA,EAIrD,YAAY,EAAE,KAAK,KAAK,GAAsD,UAAmB,CAAC,GAAG;AACnG,QAAI,OAAO,QAAQ,UAAU;AAC3B,YAAM,KAAK,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KAAK,KAAK;AAAA,MAC9C,aAAa;AAAA,MACb,gBAAgB;AAAA,IAClB,CAAC;AAED,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"]}
package/dist/index.cjs ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+
6
+
7
+ var _chunkWMBNWD46cjs = require('./chunk-WMBNWD46.cjs');
8
+
9
+ // src/types.ts
10
+ var HttpMethods = {
11
+ GET: "get",
12
+ POST: "post",
13
+ PUT: "put",
14
+ PATCH: "patch",
15
+ DELETE: "delete",
16
+ HEAD: "head",
17
+ OPTIONS: "options",
18
+ TRACE: "trace"
19
+ };
20
+
21
+ // src/index.ts
22
+ var _utils = require('oas/utils');
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+ exports.HttpMethods = HttpMethods; exports.Oas = _chunkWMBNWD46cjs.Oas; exports.findSchemaDefinition = _utils.findSchemaDefinition; exports.isOpenApiV3_1Document = _chunkWMBNWD46cjs.isOpenApiV3_1Document; exports.isParameterObject = _chunkWMBNWD46cjs.isParameterObject; exports.isReference = _chunkWMBNWD46cjs.isReference; exports.isRequired = _chunkWMBNWD46cjs.isRequired; exports.matchesMimeType = _utils.matchesMimeType;
33
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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 MediaType = '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"]}