-
-
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
base: master
Are you sure you want to change the base?
Changes from all commits
39b0eaf
1fae8c2
617a8f6
cd3b9a1
f3b4491
bb85c06
57ec23f
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 | ||||
|---|---|---|---|---|---|---|
| @@ -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((data: string, ctx: ExecutionContext) => { | ||||||
aryobenholzner marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
|
||||||
| const request = ctx.switchToHttp().getRequest(); | ||||||
| if (!data) { | ||||||
| return { ...request.cookies, ...request.signedCookies }; | ||||||
| } | ||||||
| return request.cookies?.[data] ?? request.signedCookies?.[data]; | ||||||
| }); | ||||||
| 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((data: string, ctx: ExecutionContext) => { | ||||||
|
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.
Suggested change
|
||||||
| const request = ctx.switchToHttp().getRequest(); | ||||||
|
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 fix the indentation here |
||||||
| return data ? request.headers?.[data.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}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { Body, Controller, Delete, Get, Post, Put, Param, Query, Req } from '@nestjs/common'; | ||
| import { Body, Controller, DefaultValuePipe, Delete, Get, Post, Put, Param, ParseIntPipe, ParseFloatPipe, Query, Req } from '@nestjs/common'; | ||
| import { Observable } from 'rxjs'; | ||
| import { Cookies, Headers } from '../decorators'; | ||
| import { PetApi } from '../api'; | ||
| import { ApiResponse, Pet, } from '../models'; | ||
|
|
||
|
|
@@ -13,7 +14,7 @@ export class PetApiController { | |
| } | ||
|
|
||
| @Delete('/pet/:petId') | ||
| deletePet(@Param('petId') petId: number, apiKey: string, @Req() request: Request): void | Promise<void> | Observable<void> { | ||
| deletePet(@Param('petId') petId: number, @Headers('api_key') apiKey: string | undefined, @Req() request: Request): void | Promise<void> | Observable<void> { | ||
| return this.petApi.deletePet(petId, apiKey, request); | ||
| } | ||
|
|
||
|
|
@@ -38,12 +39,12 @@ export class PetApiController { | |
| } | ||
|
|
||
| @Post('/pet/:petId') | ||
| updatePetWithForm(@Param('petId') petId: number, name: string, status: string, @Req() request: Request): void | Promise<void> | Observable<void> { | ||
| updatePetWithForm(@Param('petId') petId: number, name: string | undefined, status: string | undefined, @Req() request: Request): void | Promise<void> | Observable<void> { | ||
| return this.petApi.updatePetWithForm(petId, name, status, request); | ||
| } | ||
|
|
||
| @Post('/pet/:petId/uploadImage') | ||
| uploadFile(@Param('petId') petId: number, additionalMetadata: string, file: Blob, @Req() request: Request): ApiResponse | Promise<ApiResponse> | Observable<ApiResponse> { | ||
| uploadFile(@Param('petId') petId: number, additionalMetadata: string | undefined, file: Blob | undefined, @Req() request: Request): ApiResponse | Promise<ApiResponse> | Observable<ApiResponse> { | ||
|
Contributor
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. P2: Upload endpoint defines a Prompt for AI agents |
||
| return this.petApi.uploadFile(petId, additionalMetadata, file, request); | ||
| } | ||
|
|
||
|
|
||
| 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((data: string, ctx: ExecutionContext) => { | ||
| const request = ctx.switchToHttp().getRequest(); | ||
| if (!data) { | ||
| return { ...request.cookies, ...request.signedCookies }; | ||
| } | ||
| return request.cookies?.[data] ?? request.signedCookies?.[data]; | ||
| }); |
| 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((data: string, ctx: ExecutionContext) => { | ||
| const request = ctx.switchToHttp().getRequest(); | ||
| return data ? request.headers?.[data.toLowerCase()] : request.headers; | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.