|
| 1 | +import { Body, Deprecated, File, FormField, Patch, Post, Query, Route, UploadedFile, UploadedFiles } from '@tsoa/runtime'; |
| 2 | +import { ModelService } from '../services/modelService'; |
| 3 | +import { GenericRequest, TestClassModel, TestModel, TupleTestModel } from '../testModel31'; |
| 4 | + |
| 5 | +@Route('PostTest') |
| 6 | +export class PostTestController { |
| 7 | + private statusCode?: number = undefined; |
| 8 | + |
| 9 | + public setStatus(statusCode: number) { |
| 10 | + this.statusCode = statusCode; |
| 11 | + } |
| 12 | + |
| 13 | + public getStatus() { |
| 14 | + return this.statusCode; |
| 15 | + } |
| 16 | + |
| 17 | + public getHeaders() { |
| 18 | + return []; |
| 19 | + } |
| 20 | + |
| 21 | + @Post() |
| 22 | + public async postModel(@Body() model: TestModel): Promise<TestModel> { |
| 23 | + return model; |
| 24 | + } |
| 25 | + |
| 26 | + @Post('Object') |
| 27 | + public async postObject(@Body() body: { obj: { [key: string]: string } }): Promise<{ [key: string]: string }> { |
| 28 | + return body.obj; |
| 29 | + } |
| 30 | + |
| 31 | + @Post('MultiType') |
| 32 | + public async postMultiType(@Body() input: number | string): Promise<number | string> { |
| 33 | + return input; |
| 34 | + } |
| 35 | + |
| 36 | + @Patch() |
| 37 | + public async updateModel(@Body() model: TestModel): Promise<TestModel> { |
| 38 | + return new ModelService().getModel(); |
| 39 | + } |
| 40 | + |
| 41 | + @Post('WithDifferentReturnCode') |
| 42 | + public async postWithDifferentReturnCode(@Body() model: TestModel): Promise<TestModel> { |
| 43 | + this.setStatus(201); |
| 44 | + return model; |
| 45 | + } |
| 46 | + |
| 47 | + @Post('WithClassModel') |
| 48 | + public async postClassModel(@Body() model: TestClassModel): Promise<TestClassModel> { |
| 49 | + const augmentedModel = new TestClassModel('test', 'test2', 'test3', 'test4', 'test5'); |
| 50 | + augmentedModel.id = 700; |
| 51 | + |
| 52 | + return augmentedModel; |
| 53 | + } |
| 54 | + |
| 55 | + @Post('File') |
| 56 | + public async postWithFile(@UploadedFile('someFile') aFile: File): Promise<File> { |
| 57 | + return aFile; |
| 58 | + } |
| 59 | + |
| 60 | + @Post('FileOptional') |
| 61 | + public async postWithOptionalFile(@UploadedFile('optionalFile') optionalFile?: File): Promise<string> { |
| 62 | + return optionalFile?.originalname ?? 'no file'; |
| 63 | + } |
| 64 | + |
| 65 | + @Post('FileWithoutName') |
| 66 | + public async postWithFileWithoutName(@UploadedFile() aFile: File): Promise<File> { |
| 67 | + return aFile; |
| 68 | + } |
| 69 | + |
| 70 | + @Post('ManyFilesAndFormFields') |
| 71 | + public async postWithFiles(@UploadedFiles('someFiles') files: File[], @FormField('a') a: string, @FormField('c') c: string): Promise<File[]> { |
| 72 | + return files; |
| 73 | + } |
| 74 | + |
| 75 | + @Post('ManyFilesInDifferentFields') |
| 76 | + public async postWithDifferentFields(@UploadedFile('file_a') fileA: File, @UploadedFile('file_b') fileB: File): Promise<File[]> { |
| 77 | + return [fileA, fileB]; |
| 78 | + } |
| 79 | + |
| 80 | + @Post('ManyFilesInDifferentArrayFields') |
| 81 | + public async postWithDifferentArrayFields(@UploadedFiles('files_a') filesA: File[], @UploadedFile('file_b') fileB: File, @UploadedFiles('files_c') filesC: File[]): Promise<File[][]> { |
| 82 | + return [filesA, [fileB], filesC]; |
| 83 | + } |
| 84 | + |
| 85 | + @Post('MixedFormDataWithFilesContainsOptionalFile') |
| 86 | + public async mixedFormDataWithFile( |
| 87 | + @FormField('username') username: string, |
| 88 | + @UploadedFile('avatar') avatar: File, |
| 89 | + @UploadedFile('optionalAvatar') optionalAvatar?: File, |
| 90 | + ): Promise<{ username: string; avatar: File; optionalAvatar?: File }> { |
| 91 | + return { username, avatar, optionalAvatar }; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * |
| 96 | + * @param aFile File description of multipart |
| 97 | + * @param a FormField description of multipart |
| 98 | + * @param c |
| 99 | + */ |
| 100 | + @Post('DescriptionOfFileAndFormFields') |
| 101 | + public async postWithFileAndParams(@UploadedFile('file') aFile: File, @FormField('a') a: string, @FormField('c') c: string): Promise<File> { |
| 102 | + return aFile; |
| 103 | + } |
| 104 | + |
| 105 | + @Post('DeprecatedFormField') |
| 106 | + public async postWithDeprecatedParam(@FormField('a') a: string, @FormField('dontUse') @Deprecated() dontUse?: string): Promise<TestModel> { |
| 107 | + return new ModelService().getModel(); |
| 108 | + } |
| 109 | + |
| 110 | + @Post('Location') |
| 111 | + public async postModelAtLocation(): Promise<TestModel> { |
| 112 | + return new ModelService().getModel(); |
| 113 | + } |
| 114 | + |
| 115 | + @Post('Multi') |
| 116 | + public async postWithMultiReturn(): Promise<TestModel[]> { |
| 117 | + const model = new ModelService().getModel(); |
| 118 | + |
| 119 | + return [model, model]; |
| 120 | + } |
| 121 | + |
| 122 | + @Post('WithId/{id}') |
| 123 | + public async postWithId(id: number): Promise<TestModel> { |
| 124 | + return new ModelService().getModel(); |
| 125 | + } |
| 126 | + |
| 127 | + @Post('WithBodyAndQueryParams') |
| 128 | + public async postWithBodyAndQueryParams(@Body() model: TestModel, @Query() query: string): Promise<TestModel> { |
| 129 | + return new ModelService().getModel(); |
| 130 | + } |
| 131 | + |
| 132 | + @Post('GenericBody') |
| 133 | + public async getGenericRequest(@Body() genericReq: GenericRequest<TestModel>): Promise<TestModel> { |
| 134 | + return genericReq.value; |
| 135 | + } |
| 136 | + |
| 137 | + @Post('TupleTest') |
| 138 | + public async postTuple(@Body() model: TupleTestModel): Promise<TupleTestModel> { |
| 139 | + return model; |
| 140 | + } |
| 141 | +} |
0 commit comments