1
1
import type { ResponseResolver } from 'msw'
2
- import { OpenAPIV3 } from 'openapi-types'
2
+ import { OpenAPI , OpenAPIV3 , OpenAPIV3_1 } from 'openapi-types'
3
3
import { seedSchema } from '@yellow-ticket/seed-json-schema'
4
4
import { toString } from './to-string.js'
5
5
import { STATUS_CODES } from './status-codes.js'
6
6
7
7
export function createResponseResolver (
8
- operation : OpenAPIV3 . OperationObject ,
8
+ operation : OpenAPI . Operation ,
9
9
) : ResponseResolver {
10
10
return ( { request } ) => {
11
11
const { responses } = operation
@@ -25,7 +25,7 @@ export function createResponseResolver(
25
25
} )
26
26
}
27
27
28
- let responseObject : OpenAPIV3 . ResponseObject
28
+ let responseObject : OpenAPIV3 . ResponseObject | OpenAPIV3_1 . ResponseObject
29
29
30
30
const url = new URL ( request . url )
31
31
const explicitResponseStatus = url . searchParams . get ( 'response' )
@@ -45,8 +45,12 @@ export function createResponseResolver(
45
45
responseObject = responseByStatus
46
46
} else {
47
47
const fallbackResponse =
48
- ( responses [ '200' ] as OpenAPIV3 . ResponseObject ) ||
49
- ( responses . default as OpenAPIV3 . ResponseObject )
48
+ ( responses [ '200' ] as
49
+ | OpenAPIV3 . ResponseObject
50
+ | OpenAPIV3_1 . ResponseObject ) ||
51
+ ( responses . default as
52
+ | OpenAPIV3 . ResponseObject
53
+ | OpenAPIV3_1 . ResponseObject )
50
54
51
55
if ( ! fallbackResponse ) {
52
56
return new Response ( 'Not Implemented' , {
@@ -73,7 +77,7 @@ export function createResponseResolver(
73
77
*/
74
78
export function toHeaders (
75
79
request : Request ,
76
- responseObject : OpenAPIV3 . ResponseObject ,
80
+ responseObject : OpenAPIV3 . ResponseObject | OpenAPIV3_1 . ResponseObject ,
77
81
) : Headers | undefined {
78
82
const { content } = responseObject
79
83
if ( ! content ) {
@@ -123,9 +127,9 @@ export function toHeaders(
123
127
const headers = new Headers ( )
124
128
125
129
for ( const [ headerName , headerObject ] of Object . entries ( responseHeaders ) ) {
126
- const headerSchema = ( headerObject as OpenAPIV3 . HeaderObject ) . schema as
127
- | OpenAPIV3 . SchemaObject
128
- | undefined
130
+ const headerSchema = (
131
+ headerObject as OpenAPIV3 . HeaderObject | OpenAPIV3_1 . HeaderObject
132
+ ) . schema as OpenAPIV3 . SchemaObject | OpenAPIV3_1 . SchemaObject | undefined
129
133
130
134
if ( ! headerSchema ) {
131
135
continue
@@ -152,7 +156,7 @@ export function toHeaders(
152
156
*/
153
157
export function toBody (
154
158
request : Request ,
155
- responseObject : OpenAPIV3 . ResponseObject ,
159
+ responseObject : OpenAPIV3 . ResponseObject | OpenAPIV3_1 . ResponseObject ,
156
160
) : RequestInit [ 'body' ] {
157
161
const { content } = responseObject
158
162
@@ -163,7 +167,10 @@ export function toBody(
163
167
// See what "Content-Type" the request accepts.
164
168
const acceptedContentTypes = getAcceptedContentTypes ( request . headers )
165
169
166
- let mediaTypeObject : OpenAPIV3 . MediaTypeObject | undefined
170
+ let mediaTypeObject :
171
+ | OpenAPIV3 . MediaTypeObject
172
+ | OpenAPIV3_1 . MediaTypeObject
173
+ | undefined
167
174
const responseContentTypes = Object . keys ( content )
168
175
169
176
// Lookup the first response content type that satisfies
@@ -213,16 +220,17 @@ export function toBody(
213
220
if ( exampleName ) {
214
221
const exampleByName = mediaTypeObject . examples [ exampleName ] as
215
222
| OpenAPIV3 . ExampleObject
223
+ | OpenAPIV3_1 . ExampleObject
216
224
| undefined
217
225
return exampleByName
218
226
? exampleByName . value
219
227
: `Cannot find example by name "${ exampleName } "`
220
228
}
221
229
222
230
// Otherwise, use the first example.
223
- const firstExample = Object . values (
224
- mediaTypeObject . examples ,
225
- ) [ 0 ] as OpenAPIV3 . ExampleObject
231
+ const firstExample = Object . values ( mediaTypeObject . examples ) [ 0 ] as
232
+ | OpenAPIV3 . ExampleObject
233
+ | OpenAPIV3_1 . ExampleObject
226
234
227
235
if ( typeof firstExample . value === 'object' ) {
228
236
return JSON . stringify ( firstExample . value )
@@ -236,8 +244,9 @@ export function toBody(
236
244
* @note `example` is always nested under `schema`.
237
245
* `examples` is a sibling to `schema`.
238
246
*/
239
- const schemaExample = ( mediaTypeObject . schema as OpenAPIV3 . SchemaObject )
240
- ?. example
247
+ const schemaExample = (
248
+ mediaTypeObject . schema as OpenAPIV3 . SchemaObject | OpenAPIV3_1 . SchemaObject
249
+ ) ?. example
241
250
242
251
if ( schemaExample ) {
243
252
if ( typeof schemaExample === 'object' ) {
0 commit comments