Skip to content

aws-api-responsebuilder - API Gateway レスポンスビルダー

aws-api-responsebuilder は、API Gateway + Lambda のレスポンス構築に Builder パターンを適用した TypeScript ライブラリです。CORS ヘッダーの自動付与と JSON.stringify を共通化し、各ハンドラのレスポンス実装を統一します。jsResponseBuilder .setStatusCode(400) .setBody({ message: 'Invalid request', code: 'invalid_request' }) console.log(ResponseBuilder.getResponse()) // { // statusCode: 400, // headers: { 'Access-Control-Allow-Origin': '*', ... }, // body: '{"message":"Invalid request",...}' // }

owned_oss

Architecture

API Gateway と Lambda を組み合わせた開発で、全ハンドラに同じ CORS ヘッダーのコピーペーストと JSON.stringify の手動実装が繰り返されていました。レスポンス構築を 1 箇所に集約することで、CORS ポリシー変更時の修正漏れや body の未シリアライズによるバグを防ぐために開発しました。

TypeScript ジェネリクスでボディとカスタムヘッダーの型を指定できる ResponseFactory.init<BodyType, HeadersType>() API を提供しています。

Visit site / repository
Hidetaka Okamoto profile photo

Hidetaka Okamoto

Developer Experience Engineer

Developer Experience Engineer. A developer specialized in serverless application development on AWS and Cloudflare. Former Stripe Developer Advocate / AWS Samurai 2017. Skilled in creating content and presentations that introduce service usage and best practices. You can follow me on Twitter at @hidetaka_dev