0
0

More than 1 year has passed since last update.

NestJSをAPI Gateway + AWS Lambdaで構成する(その4)- ControllersコントローラーでDTO (Data Transfer Object)

Last updated at Posted at 2023-07-27

Controllers(コントローラー)のRequest payloads

公式

ドキュメント原文

But first (if you use TypeScript), we need to determine the DTO (Data Transfer Object) schema. A DTO is an object that defines how the data will be sent over the network. We could determine the DTO schema by using TypeScript interfaces, or by simple classes. 

Interestingly, we recommend using classes here. Why?
Classes are part of the JavaScript ES6 standard, and therefore they are preserved as real entities in the compiled JavaScript. 
On the other hand, since TypeScript interfaces are removed during the transpilation, Nest can't refer to them at runtime. 
This is important because features such as Pipes enable additional possibilities when they have access to the metatype of the variable at runtime.

DeepLより

DTOスキーマを決定するには、TypeScriptのインターフェイスを使うか、単純なクラスを使う方法がある。興味深いことに、ここではクラスを使うことを推奨する。
なぜか?クラスはJavaScript ES6標準の一部であるため、コンパイルされたJavaScriptの中で実際のエンティティとして保存される。
一方、TypeScriptのインターフェイスはトランスパイル時に削除されるため、Nestは実行時にインターフェイスを参照できない。Pipesのような機能は、実行時に変数のメタタイプにアクセスすることで、さらなる可能性を可能にするため、これは重要である。

DTOを作成する

  • DTOはクライアントからのリクエストを受ける箱のようなもの、リクエストされた内容とクラスのフィールドを結びつけるのはWebフレームワーク側でやってくれる
  • aws-node-typescript-nest/src/cats/create-cat.dto.ts
create-cat.dto.ts
export class CreateCatDto {
  name: string;
  age: number;
  breed: string;
}

create用のREST APIを追加する

  • aws-node-typescript-nest/src/cats/cats.controller.ts
cats.controller.ts
  @Post()
  create(@Req() request: Request, @Body() createCatDto: CreateCatDto) {

    console.log({ createCatDto });
    console.log(createCatDto.name + "," + createCatDto.age);
    return 'This action adds a new cat';
  }

リクエストを送る

  • Windowsのコマンドプロンプトの場合、postでデータを送る
curl -X POST --data-urlencode "name=太郎" -d "age=30" http://localhost:3000/dev/cats/

  • API側のログ
ANY /dev/cats (λ: main)
{ createCatDto: { name: '太郎', age: '30' } }
太郎,30
(λ: main) RequestId: 55b8c4eb-8a99-42eb-9219-f393bd95ce4a  Duration: 4.82 ms  Billed Duration: 5 ms

  • パターン1)windowsコマンドプロンプトでは json と その bodyで送るとエラーになる
curl -X POST -H "Content-Type: application/json" -d  "{ "name" : "佐藤" , "age" : "11"}" http://localhost:3000/dev/cats/
  • パターン1)エラー
curl -X POST -H "Content-Type: application/json" -d  "{ "name" : "佐藤" , "age" : "11"}" http://localhost:3000/dev/cats/
{"statusCode":400,"error":"Bad Request","message":"Unexpected token n in JSON at position 2"}

  • パターン2)
curl -X POST -H "Content-Type: application/json" -d  '{ "name" : "佐藤" , "age" : "11"}' http://localhost:3000/dev/cats/
  • パターン2)エラー
C:\Users\shigeo.sugimoto>curl -X POST -H "Content-Type: application/json" -d  '{ "name" : "佐藤" , "age" : "11"}' http://localhost:3000/dev/cats/
curl: (6) Could not resolve host: name
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: xn--qqq934k
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: age
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched close brace/bracket in URL position 3:
11}'
  ^
  • postmanからは json bodyで送って成功する

image.png

{
    "name":"名前",
    "age":"10"
}
  • postman実行
  • API側のログ
ANY /dev/cats (λ: main)
{ createCatDto: { name: '名前', age: '10' } }
名前,10
(λ: main) RequestId: 33f7d2b6-e418-46e8-a0c6-1a3a90c89e2e  Duration: 6.16 ms  Billed Duration: 7 ms
  • Windowsコマンドプロンプトでは json と その bodyで送るときにバックスラッシュ「\」でエスケープする
    • 'を"へ
    • "の前にエスケープのバックスラッシュ\を記述
curl -X POST -H "Content-Type: application/json" -d  "{\"name\" : \"佐藤\" , \"age\" : \"11\"}" http://localhost:3000/dev/cats/
  • API側のログ
{ createCatDto: { name: '佐藤', age: '11' } }
佐藤,11
(λ: main) RequestId: a606e154-8436-4033-8478-543f8af31ce2  Duration: 275.65 ms  Billed Duration: 276 ms

Route parameters, URIでパラメータ

  • http://localhost:3000/dev/cats/1 のように 1が意味のあるID等になる場合
Routes with parameters should be declared after any static paths. This prevents the parameterized paths from intercepting traffic destined for the static paths.
  • aws-node-typescript-nest/src/cats/cats.controller.ts
cats.controller.ts
  @Get(':id')
  findOne(@Param() params: any): string {
    console.log(params.id);
    return `This action returns a #${params.id} cat`;
  }

さらに単純に

cats.controller.ts
  @Get(':id')
  findOne(@Param('id') id: string): string {
    console.log({ id });
    return `This action returns a #${id} cat`;
  }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0