|
| 1 | +import { |
| 2 | + Accepts, |
| 3 | + BufferBody, |
| 4 | + Controller, |
| 5 | + Dto, |
| 6 | + File, |
| 7 | + findProjectRoot, |
| 8 | + Get, |
| 9 | + Header, |
| 10 | + Host, |
| 11 | + IP, |
| 12 | + Param, |
| 13 | + Post, |
| 14 | + Query, |
| 15 | + Req, |
| 16 | + Res, |
| 17 | + Response, |
| 18 | + StreamableFile, |
| 19 | + UseGuards, |
| 20 | + UserAgent, |
| 21 | + Validate, |
| 22 | +} from '@intentjs/core'; |
| 23 | +import { CustomGuard } from '../guards/custom'; |
| 24 | +import { Request, UploadedFile } from '@intentjs/hyper-express'; |
| 25 | +import { createReadStream } from 'fs'; |
| 26 | +import { join } from 'path'; |
| 27 | +import { LoginDto } from 'app/validators/auth'; |
| 28 | + |
| 29 | +@Controller('/icon') |
| 30 | +@UseGuards(CustomGuard) |
| 31 | +export class IntentController { |
| 32 | + public service: any; |
| 33 | + |
| 34 | + constructor() { |
| 35 | + this.service = null; |
| 36 | + } |
| 37 | + |
| 38 | + @Get('/:name') |
| 39 | + @UseGuards(CustomGuard) |
| 40 | + async getHello( |
| 41 | + // @Req() req: Request, |
| 42 | + // @Param('name') name: string, |
| 43 | + // @Query() query: Record<string, any>, |
| 44 | + // @Query('b') bQuery: string, |
| 45 | + // @Param() pathParams: string, |
| 46 | + // @Host() hostname: string, |
| 47 | + // @IP() ips: string, |
| 48 | + // @Accepts() accepts: string, |
| 49 | + // @BufferBody() bufferBody: Promise<Buffer>, |
| 50 | + // @UserAgent() userAgent: string, |
| 51 | + // @Header() headers: Record<string, any>, |
| 52 | + @Res() res: Response, |
| 53 | + ) { |
| 54 | + // console.log( |
| 55 | + // 'query ==> ', |
| 56 | + // query, |
| 57 | + // 'bQuyery ==> ', |
| 58 | + // bQuery, |
| 59 | + // 'name ===> ', |
| 60 | + // name, |
| 61 | + // bufferBody, |
| 62 | + // pathParams, |
| 63 | + // 'hostname===> ', |
| 64 | + // hostname, |
| 65 | + // 'accepts ===> ', |
| 66 | + // accepts, |
| 67 | + // 'ips ===> ', |
| 68 | + // ips, |
| 69 | + // 'inside get method', |
| 70 | + // 'user agent ===> ', |
| 71 | + // userAgent, |
| 72 | + // ); |
| 73 | + // console.log('all headers ===> ', headers); |
| 74 | + // throw new Error('hello there'); |
| 75 | + // return { hello: 'world' }; |
| 76 | + // const readStream = createReadStream( |
| 77 | + // join(findProjectRoot(), 'storage/uploads/sample-image.jpg'), |
| 78 | + // ); |
| 79 | + // return new StreamableFile(readStream, { type: 'image/jpeg' }); |
| 80 | + } |
| 81 | + |
| 82 | + @Get('/plain-with-query-param') |
| 83 | + @UseGuards(CustomGuard) |
| 84 | + async plainWithQueryParam(@Req() req: Request) { |
| 85 | + console.log(req); |
| 86 | + return { hello: 'world' }; |
| 87 | + } |
| 88 | + |
| 89 | + @Get('/:id') |
| 90 | + @UseGuards(CustomGuard) |
| 91 | + async plainWithPathParam(@Req() req: Request) { |
| 92 | + console.log(req); |
| 93 | + return { hello: 'world' }; |
| 94 | + } |
| 95 | + |
| 96 | + @Post('/json') |
| 97 | + @Validate(LoginDto) |
| 98 | + async postJson( |
| 99 | + @Req() req: Request, |
| 100 | + @Dto() dto: LoginDto, |
| 101 | + @Param('name') name: string, |
| 102 | + @Query() query: Record<string, any>, |
| 103 | + @Query('b') bQuery: string, |
| 104 | + @Param() pathParams: string, |
| 105 | + @Host() hostname: string, |
| 106 | + @IP() ips: string, |
| 107 | + @Accepts() accepts: string, |
| 108 | + @BufferBody() bufferBody: Promise<Buffer>, |
| 109 | + @UserAgent() userAgent: string, |
| 110 | + @Header() headers: Record<string, any>, |
| 111 | + @File('file') file: UploadedFile, |
| 112 | + @Res() res: Response, |
| 113 | + ) { |
| 114 | + // console.log( |
| 115 | + // 'query ==> ', |
| 116 | + // query, |
| 117 | + // 'bQuyery ==> ', |
| 118 | + // bQuery, |
| 119 | + // 'name ===> ', |
| 120 | + // name, |
| 121 | + // bufferBody, |
| 122 | + // pathParams, |
| 123 | + // 'hostname===> ', |
| 124 | + // hostname, |
| 125 | + // 'accepts ===> ', |
| 126 | + // accepts, |
| 127 | + // 'ips ===> ', |
| 128 | + // ips, |
| 129 | + // 'inside get method', |
| 130 | + // 'user agent ===> ', |
| 131 | + // userAgent, |
| 132 | + // ); |
| 133 | + |
| 134 | + // console.log('all headers ===> ', headers); |
| 135 | + console.log('uploaded files ==> ', req.dto(), dto); |
| 136 | + |
| 137 | + const readStream = createReadStream( |
| 138 | + join(findProjectRoot(), 'storage/uploads/sample-image.jpg'), |
| 139 | + ); |
| 140 | + |
| 141 | + return new StreamableFile(readStream, { type: 'image/jpeg' }); |
| 142 | + |
| 143 | + return { hello: 'world from POST /json' }; |
| 144 | + } |
| 145 | + |
| 146 | + @Post('/multipart') |
| 147 | + @UseGuards(CustomGuard) |
| 148 | + async postHello(@Req() req: Request) { |
| 149 | + return { hello: 'world' }; |
| 150 | + } |
| 151 | + |
| 152 | + @Post('/form-data') |
| 153 | + @UseGuards(CustomGuard) |
| 154 | + async postFormData(@Req() req: Request) { |
| 155 | + return { hello: 'world' }; |
| 156 | + } |
| 157 | + |
| 158 | + @Post('/binary') |
| 159 | + @UseGuards(CustomGuard) |
| 160 | + async postBinary(@Req() req: Request) { |
| 161 | + return { hello: 'world' }; |
| 162 | + } |
| 163 | +} |
0 commit comments