Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fromOpenApi): add browser support #51

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@
"vitest": "^1.6.0"
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.0.2",
"@stoplight/json": "^3.21.0",
"@types/har-format": "^1.2.7",
"@yellow-ticket/seed-json-schema": "^0.1.6",
"openapi-types": "^7.2.3",
"outvariant": "^1.2.1"
"outvariant": "^1.2.1",
"yaml": "^2.4.5"
},
"pnpm": {
"overrides": {
"ws@>=8.0.0 <8.17.1": ">=8.17.1",
"braces@<3.0.3": ">=3.0.3"
}
}
}
}
140 changes: 61 additions & 79 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/open-api/from-open-api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { RequestHandler, HttpHandler, http } from 'msw'
import type { OpenAPIV3, OpenAPIV2, OpenAPI } from 'openapi-types'
import SwaggerParser from '@apidevtools/swagger-parser'
import { parse } from 'yaml'
import { normalizeSwaggerUrl } from './utils/normalize-swagger-url.js'
import { getServers } from './utils/get-servers.js'
import { isAbsoluteUrl, joinPaths } from './utils/url.js'
import { createResponseResolver } from './utils/open-api-utils.js'
import { dereference } from './utils/dereference.js'

type SupportedHttpMethods = keyof typeof http
const supportedHttpMethods = Object.keys(
Expand All @@ -21,7 +22,11 @@ const supportedHttpMethods = Object.keys(
export async function fromOpenApi(
document: string | OpenAPI.Document | OpenAPIV3.Document | OpenAPIV2.Document,
weyert marked this conversation as resolved.
Show resolved Hide resolved
): Promise<Array<RequestHandler>> {
const specification = await SwaggerParser.dereference(document)
if (typeof document === 'string') {
// Supports both JSON and YAML strings.
document = parse(document)
}
const specification = await dereference(document)
const requestHandlers: Array<RequestHandler> = []

if (typeof specification.paths === 'undefined') {
Expand Down
60 changes: 60 additions & 0 deletions src/open-api/utils/dereference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { dereference } from './dereference.js'

it('dereferences', async () => {
const dereferenced = await dereference({
foo: {
$ref: '#/components/schemas/User',
},
components: {
schemas: {
User: {
type: 'object',
properties: {
age: {
type: 'string',
},
name: {
$ref: '#/components/schemas/Name',
},
},
},
Name: {
type: 'string',
},
},
},
})
expect(dereferenced).toMatchInlineSnapshot(`
{
"components": {
"schemas": {
"Name": {
"type": "string",
},
"User": {
"properties": {
"age": {
"type": "string",
},
"name": {
"type": "string",
},
},
"type": "object",
},
},
},
"foo": {
"properties": {
"age": {
"type": "string",
},
"name": {
"type": "string",
},
},
"type": "object",
},
}
`)
})
Loading
Loading