-
-
Notifications
You must be signed in to change notification settings - Fork 7
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): support mapping and filtering operations #57
base: main
Are you sure you want to change the base?
Changes from all commits
14abf27
e1674fb
ef6531c
b3b644a
c49d3c7
c642e1f
b73aa7b
e6a83f2
89f2e80
e3d2391
45df894
ef583e7
898c443
a214ae2
053d58b
f31ae1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { normalizeSwaggerPath } from './normalize-swagger-path.js' | ||
|
||
it('replaces swagger path parameters with colons', () => { | ||
expect(normalizeSwaggerPath('/user/{userId}')).toEqual('/user/:userId') | ||
expect( | ||
normalizeSwaggerPath('https://{subdomain}.example.com/{resource}/recent'), | ||
).toEqual('https://:subdomain.example.com/:resource/recent') | ||
}) | ||
|
||
it('returns otherwise normal URL as-is', () => { | ||
expect(normalizeSwaggerPath('/user/abc-123')).toEqual('/user/abc-123') | ||
expect( | ||
normalizeSwaggerPath('https://finance.example.com/reports/recent'), | ||
).toEqual('https://finance.example.com/reports/recent') | ||
}) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// @vitest-environment happy-dom | ||
import { fromOpenApi } from '../../src/open-api/from-open-api.js' | ||
import { createOpenApiSpec } from '../support/create-open-api-spec.js' | ||
import { InspectedHandler, inspectHandlers } from '../support/inspect.js' | ||
|
||
it('creates handlers based on provided filter', async () => { | ||
const openApiSpec = createOpenApiSpec({ | ||
paths: { | ||
'/numbers': { | ||
get: { | ||
responses: { | ||
200: { | ||
description: 'Numbers response', | ||
content: { | ||
'application/json': { | ||
example: [1, 2, 3], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
put: { | ||
responses: { | ||
200: { | ||
description: 'Numbers response', | ||
content: { | ||
'application/json': { | ||
example: [1, 2, 3], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
'/orders': { | ||
get: { | ||
responses: { | ||
200: { | ||
description: 'Orders response', | ||
content: { | ||
'application/json': { | ||
example: [{ id: 1 }, { id: 2 }, { id: 3 }], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const handlers = await fromOpenApi( | ||
openApiSpec, | ||
({ path, method, operation }) => { | ||
return path === '/numbers' && method === 'get' ? operation : undefined | ||
}, | ||
) | ||
|
||
const inspectedHandlers = await inspectHandlers(handlers) | ||
expect(inspectHandlers.length).toBe(1) | ||
expect(inspectedHandlers).toEqual<InspectedHandler[]>([ | ||
{ | ||
handler: { | ||
method: 'GET', | ||
path: 'http://localhost/numbers', | ||
}, | ||
response: { | ||
status: 200, | ||
statusText: 'OK', | ||
headers: expect.arrayContaining([['content-type', 'application/json']]), | ||
body: JSON.stringify([1, 2, 3]), | ||
}, | ||
}, | ||
]) | ||
}) | ||
|
||
it('creates handler with modified response', async () => { | ||
const openApiSpec = createOpenApiSpec({ | ||
paths: { | ||
'/numbers': { | ||
description: 'Get numbers', | ||
get: { | ||
responses: { | ||
200: { | ||
description: 'Numbers response', | ||
content: { | ||
'application/json': { | ||
example: [1, 2, 3], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
put: { | ||
responses: { | ||
200: { | ||
description: 'Numbers response', | ||
content: { | ||
'application/json': { | ||
example: [1, 2, 3], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
'/orders': { | ||
get: { | ||
responses: { | ||
200: { | ||
description: 'Orders response', | ||
content: { | ||
'application/json': { | ||
example: [{ id: 1 }, { id: 2 }, { id: 3 }], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const handlers = await fromOpenApi( | ||
openApiSpec, | ||
({ path, method, operation }) => { | ||
return path === '/numbers' && method === 'get' | ||
? { | ||
...operation, | ||
responses: { | ||
200: { | ||
description: 'Get numbers response', | ||
content: { 'application/json': { example: [10] } }, | ||
}, | ||
}, | ||
} | ||
: operation | ||
}, | ||
) | ||
|
||
expect(await inspectHandlers(handlers)).toEqual<InspectedHandler[]>([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to add the request test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm it will fail, no? Since I added an extra expect for that to check the handlers length anyway: e3d2391 |
||
{ | ||
handler: { | ||
method: 'GET', | ||
path: 'http://localhost/numbers', | ||
}, | ||
response: { | ||
status: 200, | ||
statusText: 'OK', | ||
headers: expect.arrayContaining([['content-type', 'application/json']]), | ||
body: JSON.stringify([10]), | ||
}, | ||
}, | ||
{ | ||
handler: { | ||
method: 'PUT', | ||
path: 'http://localhost/numbers', | ||
}, | ||
response: { | ||
status: 200, | ||
statusText: 'OK', | ||
headers: expect.arrayContaining([['content-type', 'application/json']]), | ||
body: JSON.stringify([1, 2, 3]), | ||
}, | ||
}, | ||
{ | ||
handler: { | ||
method: 'GET', | ||
path: 'http://localhost/orders', | ||
}, | ||
response: { | ||
status: 200, | ||
statusText: 'OK', | ||
headers: expect.arrayContaining([['content-type', 'application/json']]), | ||
body: JSON.stringify([{ id: 1 }, { id: 2 }, { id: 3 }]), | ||
}, | ||
}, | ||
]) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add another test case for mapping the operation. It may look like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great test! 45df894
I modified response, since operation itself does not contain
path
and that one we are not able to modify in this mapping function