-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from asteasolutions/custom-openapi-components
Custom OpenAPI components, response headers, z.discriminatedUnion & prettier
- Loading branch information
Showing
20 changed files
with
443 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/node_modules | ||
.node-version | ||
.nvm | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/dist | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"semi": true, | ||
"singleQuote": true, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { OpenAPIGenerator } from '../src/openapi-generator'; | ||
import { OpenAPIRegistry } from '../src/openapi-registry'; | ||
import { z } from 'zod'; | ||
import { extendZodWithOpenApi } from '../src/zod-extensions'; | ||
|
||
extendZodWithOpenApi(z); | ||
|
||
const testDocConfig = { | ||
openapi: '3.0.0', | ||
info: { | ||
version: '1.0.0', | ||
title: 'Swagger Petstore', | ||
description: 'A sample API', | ||
termsOfService: 'http://swagger.io/terms/', | ||
license: { | ||
name: 'Apache 2.0', | ||
url: 'https://www.apache.org/licenses/LICENSE-2.0.html', | ||
}, | ||
}, | ||
servers: [{ url: 'v1' }], | ||
}; | ||
|
||
describe('Custom components', () => { | ||
it('can register and generate security schemes', () => { | ||
const registry = new OpenAPIRegistry(); | ||
|
||
const bearerAuth = registry.registerComponent( | ||
'securitySchemes', | ||
'bearerAuth', | ||
{ | ||
type: 'http', | ||
scheme: 'bearer', | ||
bearerFormat: 'JWT', | ||
} | ||
); | ||
|
||
registry.registerPath({ | ||
path: '/units', | ||
method: 'get', | ||
security: [{ [bearerAuth.name]: [] }], | ||
responses: { | ||
200: { | ||
mediaType: 'application/json', | ||
schema: z.string().openapi({ description: 'Sample response' }), | ||
}, | ||
}, | ||
}); | ||
|
||
const builder = new OpenAPIGenerator(registry.definitions); | ||
const document = builder.generateDocument(testDocConfig); | ||
|
||
expect(document.paths['/units'].get.security).toEqual([{ bearerAuth: [] }]); | ||
|
||
expect(document.components!.securitySchemes).toEqual({ | ||
bearerAuth: { | ||
bearerFormat: 'JWT', | ||
scheme: 'bearer', | ||
type: 'http', | ||
}, | ||
}); | ||
}); | ||
|
||
it('can register and generate headers', () => { | ||
const registry = new OpenAPIRegistry(); | ||
|
||
const apiKeyHeader = registry.registerComponent('headers', 'api-key', { | ||
example: '1234', | ||
required: true, | ||
description: 'The API Key you were given in the developer portal', | ||
}); | ||
|
||
registry.registerPath({ | ||
path: '/units', | ||
method: 'get', | ||
responses: { | ||
200: { | ||
mediaType: 'application/json', | ||
headers: { 'x-api-key': apiKeyHeader.ref }, | ||
schema: z.string().openapi({ description: 'Sample response' }), | ||
}, | ||
}, | ||
}); | ||
|
||
const builder = new OpenAPIGenerator(registry.definitions); | ||
const document = builder.generateDocument(testDocConfig); | ||
|
||
expect(document.paths['/units'].get.responses['200'].headers).toEqual({ | ||
'x-api-key': { $ref: '#/components/headers/api-key' }, | ||
}); | ||
|
||
expect(document.components!.headers).toEqual({ | ||
'api-key': { | ||
example: '1234', | ||
required: true, | ||
description: 'The API Key you were given in the developer portal', | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.