-
-
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
Changes from all commits
39b0eaf
1fae8c2
617a8f6
cd3b9a1
f3b4491
bb85c06
57ec23f
702fa69
0f46899
b6d0a1a
387123d
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,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 |
| 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?
Contributor
Author
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. Fastify compability should not be any better or worse with this PR. Although some basic stuff might already work, I have never tested Fastify support. |
||
|
|
||
| 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 framework specific cookie middleware must be enabled. | ||
|
|
||
| For Express, the [cookie-parser](https://www.npmjs.com/package/cookie-parser) middleware must be installed and enabled. | ||
|
|
||
| ``` | ||
| npm install cookie-parser | ||
| ``` | ||
|
|
||
| in `main.ts` | ||
|
|
||
| ``` | ||
| import * as cookieParser from 'cookie-parser'; | ||
|
|
||
| app.use(cookieParser()); | ||
| ``` | ||
| 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]; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './cookies-decorator'; | ||
| export * from './headers-decorator'; |
| 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; | ||
| }); |
| 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}} |
Uh oh!
There was an error while loading. Please reload this page.