-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[typescript-nestjs-server] #22928 improve request parameter handling #22960
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
Open
aryobenholzner
wants to merge
9
commits into
OpenAPITools:master
Choose a base branch
from
aryobenholzner:bugfix/22928-parameter-handling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39b0eaf
[typescript-nestjs-server] #22928 exclude inline union strings from g…
aryobenholzner 1fae8c2
[typescript-nestjs-server] #22928 add optional type hints
aryobenholzner 617a8f6
[typescript-nestjs-server] #22928 add/improve support for various par…
aryobenholzner cd3b9a1
[typescript-nestjs-server] #22928 add docs, fix indentations and test…
aryobenholzner f3b4491
[typescript-nestjs-server] #22928 correctly parse numeric parameters,…
aryobenholzner bb85c06
[typescript-nestjs-server] #22928 lowercase header access, check each…
aryobenholzner 57ec23f
[typescript-nestjs-server] #22928 allow optional parameters for numbe…
aryobenholzner 702fa69
Merge branch 'OpenAPITools:master' into bugfix/22928-parameter-handling
aryobenholzner 0f46899
[typescript-nestjs-server] #22928 updated README, additional PR feedback
aryobenholzner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,6 @@ | ||
| generatorName: typescript-nestjs-server | ||
| outputDir: samples/server/petstore/typescript-nestjs-server/builds/parameters | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_0/parameter-test-spec.yaml | ||
| templateDir: modules/openapi-generator/src/main/resources/typescript-nestjs-server | ||
| additionalProperties: | ||
| "useSingleRequestParameter" : true |
This file contains hidden or 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 hidden or 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 hidden or 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,6 +2,8 @@ | |
|
|
||
| Usage: The generated output is intended to be its own module, that can be imported into your NestJS App Module. You do not need to change generated files, just import the module and implement the API | ||
|
|
||
| Currently, only Express is supported. | ||
|
Member
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. was this already the case before this change? |
||
|
|
||
| Example usage (with the openapi sample `petstore.yaml`): | ||
|
|
||
| 1. Invoke openapi-generator | ||
|
|
@@ -28,7 +30,7 @@ Example usage (with the openapi sample `petstore.yaml`): | |
|
|
||
| ... | ||
| ``` | ||
| 3. Import the generated `ApiModule` with `ApiModule.forRoot` and provide a instance of `ApiImplementations` with a reference to your implementation | ||
| 3. Import the generated `ApiModule` with `ApiModule.forRoot` and provide an instance of `ApiImplementations` with a reference to your implementation | ||
| `app.module.ts` | ||
| ```typescript | ||
| import { Module } from "@nestjs/common"; | ||
|
|
@@ -45,12 +47,35 @@ Example usage (with the openapi sample `petstore.yaml`): | |
|
|
||
| @Module({ | ||
| imports: [ | ||
| ApiModule.forRoot(apiImplementations), | ||
| ApiModule.forRoot({ | ||
| apiImplementations: apiImplementations, | ||
| providers: [ | ||
| // additional providers for services injected into apiImplementations | ||
| ] | ||
| }), | ||
| ], | ||
| controllers: [], | ||
| providers: [], | ||
| }) | ||
| export class AppModule {} | ||
| ``` | ||
|
|
||
| You now can regenerate the API module as often as you want without overriding your implementation. | ||
| You now can regenerate the API module as often as you want without overriding your implementation. | ||
|
|
||
| ## Using Cookie parameters | ||
|
|
||
| In order for cookie parameters to work, the the framework specific cookie middleware must be enabled. | ||
|
|
||
| For Express, the `cookie-parser` middleware must be installed and enabled. | ||
|
Member
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. please link the package/npm page here |
||
|
|
||
| ``` | ||
| npm install cookie-parser | ||
| ``` | ||
|
|
||
| in `main.ts` | ||
|
|
||
| ``` | ||
| import * as cookieParser from 'cookie-parser'; | ||
|
|
||
| app.use(cookieParser()); | ||
| ``` | ||
This file contains hidden or 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 hidden or 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
24 changes: 24 additions & 0 deletions
24
.../openapi-generator/src/main/resources/typescript-nestjs-server/cookies-decorator.mustache
This file contains hidden or 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,24 @@ | ||
| import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
|
||
| /** | ||
| * A decorator function for retrieving cookies from the request object in an HTTP context. | ||
| * | ||
| * This decorator only works, if the framework specific cookie middleware is installed and enabled. | ||
| * - For Express, you need to use the `cookie-parser` middleware. | ||
| * - For Fastify, you need to use the `@fastify/cookie` plugin. | ||
| * | ||
| * Consult https://docs.nestjs.com/techniques/cookies for further information | ||
| * | ||
| * Usage: | ||
| * ``` | ||
| * @Get() | ||
| * findAll(@Cookies('name') name: string) {} | ||
| * ``` | ||
| */ | ||
| export const Cookies = createParamDecorator((cookieName: string, ctx: ExecutionContext) => { | ||
| const request = ctx.switchToHttp().getRequest(); | ||
| if (!cookieName) { | ||
| return { ...request.cookies, ...request.signedCookies }; | ||
| } | ||
| return request.cookies?.[cookieName] ?? request.signedCookies?.[cookieName]; | ||
| }); |
2 changes: 2 additions & 0 deletions
2
modules/openapi-generator/src/main/resources/typescript-nestjs-server/decorators.mustache
This file contains hidden or 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 @@ | ||
| export * from './cookies-decorator'; | ||
| export * from './headers-decorator'; |
16 changes: 16 additions & 0 deletions
16
.../openapi-generator/src/main/resources/typescript-nestjs-server/headers-decorator.mustache
This file contains hidden or 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,16 @@ | ||
| import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
|
||
| /** | ||
| * A decorator function for retrieving headers from the request object in an HTTP context. | ||
| * Workaround for enabling PipeTransformers on Headers (see https://github.com/nestjs/nest/issues/356) | ||
| * | ||
| * Usage: | ||
| * ``` | ||
| * @Get() | ||
| * findAll(@Headers('name') name: string) {} | ||
| * ``` | ||
| */ | ||
| export const Headers = createParamDecorator((headerName: string, ctx: ExecutionContext) => { | ||
| const request = ctx.switchToHttp().getRequest(); | ||
| return headerName ? request.headers?.[headerName.toLowerCase()] : request.headers; | ||
| }); |
1 change: 1 addition & 0 deletions
1
modules/openapi-generator/src/main/resources/typescript-nestjs-server/paramPipe.mustache
This file contains hidden or 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 @@ | ||
| {{#defaultValue}}, new DefaultValuePipe({{{defaultValue}}}){{/defaultValue}}{{#isNumber}}, new {{#isFloat}}ParseFloatPipe({{/isFloat}}{{^isFloat}}ParseIntPipe({{/isFloat}}{{^isRequired}}{optional: true}{{/isRequired}}{{#isNullable}}{optional: true}{{/isNullable}}){{/isNumber}} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.