@kubb/oas 4.5.1 → 4.5.2

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/infer.cjs CHANGED
@@ -0,0 +1 @@
1
+ const require_index = require('./index.cjs');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/oas",
3
- "version": "4.5.1",
3
+ "version": "4.5.2",
4
4
  "description": "OpenAPI Specification (OAS) utilities and helpers for Kubb, providing parsing, normalization, and manipulation of OpenAPI/Swagger schemas.",
5
5
  "keywords": [
6
6
  "openapi",
@@ -56,7 +56,7 @@
56
56
  }
57
57
  ],
58
58
  "dependencies": {
59
- "@redocly/openapi-core": "^2.10.0",
59
+ "@redocly/openapi-core": "^2.11.0",
60
60
  "hotscript": "^1.0.13",
61
61
  "json-schema-to-ts": "^3.1.1",
62
62
  "jsonpointer": "^5.0.1",
package/src/index.ts CHANGED
@@ -2,4 +2,15 @@ export { findSchemaDefinition, matchesMimeType } from 'oas/utils'
2
2
  export type { Infer, Model, RequestParams, Response } from './infer/index.ts'
3
3
  export { Oas } from './Oas.ts'
4
4
  export * from './types.ts'
5
- export { isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, merge, parse } from './utils.ts'
5
+ export {
6
+ isDiscriminator,
7
+ isNullable,
8
+ isOpenApiV3_1Document,
9
+ isOptional,
10
+ isParameterObject,
11
+ isReference,
12
+ isRequired,
13
+ merge,
14
+ parse,
15
+ parseFromConfig,
16
+ } from './utils.ts'
package/src/utils.spec.ts CHANGED
@@ -1,4 +1,7 @@
1
- import { merge, parse } from './utils.ts'
1
+ import path from 'node:path'
2
+ import type { Config } from '@kubb/core'
3
+ import yaml from '@stoplight/yaml'
4
+ import { merge, parse, parseFromConfig } from './utils.ts'
2
5
 
3
6
  describe('utils', () => {
4
7
  test('merge of 2 oas documents', async () => {
@@ -63,3 +66,117 @@ components:
63
66
  expect(oas.api?.info.title).toBe('Swagger Petstore')
64
67
  })
65
68
  })
69
+
70
+ describe('parseFromConfig', () => {
71
+ const petStoreV3 = path.resolve(__dirname, '../mocks/petStore.yaml')
72
+ const petStoreV2 = path.resolve(__dirname, '../mocks/petStoreV2.json')
73
+
74
+ const yamlPetStoreString = `
75
+ openapi: 3.0.0
76
+ info:
77
+ title: Swagger Petstore
78
+ version: 1.0.0
79
+ paths:
80
+ /users/{userId}:
81
+ get:
82
+ tags:
83
+ - Users
84
+ summary: Get public user details
85
+ operationId: getUser
86
+ parameters:
87
+ - $ref: "#/components/parameters/userId"
88
+ responses:
89
+ '200':
90
+ description: Successful response
91
+ content:
92
+ application/json:
93
+ schema:
94
+ type: object
95
+ properties:
96
+ message:
97
+ type: string
98
+ example: User details retrieved successfully
99
+ user:
100
+ type: object
101
+ properties:
102
+ userId:
103
+ type: string
104
+ example: 1234343434343
105
+ components:
106
+ parameters:
107
+ userId:
108
+ name: userId
109
+ in: path
110
+ description: Executes the action in the context of the specified user.
111
+ required: true
112
+ schema:
113
+ type: string
114
+ example: 1234343434343
115
+ `
116
+
117
+ const petStoreObject = yaml.parse(yamlPetStoreString)
118
+
119
+ test('check if oas and title is defined based on a Swagger(v3) file', async () => {
120
+ const oas = await parse(petStoreV3)
121
+
122
+ expect(oas).toBeDefined()
123
+ expect(oas.api?.info.title).toBe('Swagger Petstore - OpenAPI 3.0')
124
+ })
125
+
126
+ test('check if oas and title is defined based on a Swagger(v2) file', async () => {
127
+ const oas = await parse(petStoreV2)
128
+
129
+ expect(oas).toBeDefined()
130
+ expect(oas.api?.info.title).toBe('Swagger Petstore')
131
+ })
132
+
133
+ test('check if oas and title is defined based on a Swagger(v3) JSON import', async () => {
134
+ const data = await import(petStoreV2)
135
+
136
+ const oas = await parseFromConfig({
137
+ root: process.cwd(),
138
+ input: {
139
+ data,
140
+ },
141
+ } as Config)
142
+
143
+ expect(oas).toBeDefined()
144
+ expect(oas.api?.info.title).toBe('Swagger Petstore')
145
+ })
146
+
147
+ test('check if oas and title is defined based on a Swagger(v3) JSON string', async () => {
148
+ const oas = await parseFromConfig({
149
+ root: process.cwd(),
150
+ input: {
151
+ data: JSON.stringify(petStoreObject),
152
+ },
153
+ } as Config)
154
+
155
+ expect(oas).toBeDefined()
156
+ expect(oas.api?.info.title).toBe('Swagger Petstore')
157
+ })
158
+
159
+ test('check if oas and title is defined based on a Swagger(v3) JSON object', async () => {
160
+ const oas = await parseFromConfig({
161
+ root: process.cwd(),
162
+ input: {
163
+ data: petStoreObject,
164
+ },
165
+ } as Config)
166
+
167
+ expect(oas).toBeDefined()
168
+ expect(oas.api?.info.title).toBe('Swagger Petstore')
169
+ })
170
+
171
+ test('check if oas and title is defined based on a Swagger(v3) YAML', async () => {
172
+ const oas = await parseFromConfig({
173
+ root: process.cwd(),
174
+ input: {
175
+ data: yamlPetStoreString,
176
+ },
177
+ } as Config)
178
+
179
+ expect(oas).toBeDefined()
180
+ expect(oas.api?.info.title).toBe('Swagger Petstore')
181
+ })
182
+ })
package/src/utils.ts CHANGED
@@ -1,3 +1,8 @@
1
+ import path from 'node:path'
2
+ import type { Config } from '@kubb/core'
3
+ import { URLPath } from '@kubb/core/utils'
4
+ import yaml from '@stoplight/yaml'
5
+ import type * as OasTypes from 'oas/types'
1
6
  import type { OASDocument, ParameterObject, SchemaObject } from 'oas/types'
2
7
  import { isRef, isSchema } from 'oas/types'
3
8
  import OASNormalize from 'oas-normalize'
@@ -130,3 +135,34 @@ export async function merge(pathOrApi: Array<string | OASDocument>, { oasClass =
130
135
 
131
136
  return parse(merged, { oasClass })
132
137
  }
138
+
139
+ export function parseFromConfig(config: Config, oasClass: typeof Oas = Oas): Promise<Oas> {
140
+ if ('data' in config.input) {
141
+ if (typeof config.input.data === 'object') {
142
+ const api: OasTypes.OASDocument = JSON.parse(JSON.stringify(config.input.data)) as OasTypes.OASDocument
143
+ return parse(api, { oasClass })
144
+ }
145
+
146
+ // data is a string - try YAML first, then fall back to passing to parse()
147
+ try {
148
+ const api: string = yaml.parse(config.input.data as string)
149
+ return parse(api, { oasClass })
150
+ } catch (_e) {
151
+ // YAML parse failed, let parse() handle it (supports JSON strings and more)
152
+ return parse(config.input.data as string, { oasClass })
153
+ }
154
+ }
155
+
156
+ if (Array.isArray(config.input)) {
157
+ return merge(
158
+ config.input.map((input) => path.resolve(config.root, input.path)),
159
+ { oasClass },
160
+ )
161
+ }
162
+
163
+ if (new URLPath(config.input.path).isURL) {
164
+ return parse(config.input.path, { oasClass })
165
+ }
166
+
167
+ return parse(path.resolve(config.root, config.input.path), { oasClass })
168
+ }