Skip to content

Commit

Permalink
refactor: return response in json format (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
arahiko-ayami authored Feb 5, 2024
1 parent 74e2c25 commit 83ef482
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@middy/core": "^4.5.5",
"@middy/http-error-handler": "^4.5.5",
"@middy/http-json-body-parser": "^4.5.5",
"@middy/http-response-serializer": "^5.2.3",
"@middy/util": "^5.2.3",
"@middy/validator": "^4.5.5",
"class-transformer": "^0.5.1",
Expand Down
6 changes: 0 additions & 6 deletions packages/functions/src/middleware/index.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/functions/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './middleware';
export * from './util';
28 changes: 28 additions & 0 deletions packages/functions/src/shared/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import middy from '@middy/core';
import httpErrorHandler from '@middy/http-error-handler';
import jsonBodyParser from '@middy/http-json-body-parser';
import httpResponseSerializer from '@middy/http-response-serializer';

export const middleware = () =>
middy()
.use(jsonBodyParser())
.use(httpErrorHandler())
.use(
httpResponseSerializer({
serializers: [
{
regex: /^application\/xml$/,
serializer: ({ body }) => `<message>${body}</message>`,
},
{
regex: /^application\/json$/,
serializer: ({ body }) => JSON.stringify(body),
},
{
regex: /^text\/plain$/,
serializer: ({ body }) => body,
},
],
defaultContentType: 'application/json',
}),
);
6 changes: 6 additions & 0 deletions packages/functions/src/shared/util/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface HttpResponse {
statusCode: number;
body?: string | object;
headers?: { [key: string]: string };
error?: Error | string | object;
}
1 change: 1 addition & 0 deletions packages/functions/src/shared/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './http';
9 changes: 5 additions & 4 deletions packages/functions/src/student/studentDetails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { middleware } from '../middleware';
import { APIGatewayProxyEvent } from 'aws-lambda';
import { middleware } from '../shared/middleware';
import { plainToInstance } from 'class-transformer';
import { IsString } from 'class-validator';
import {
Expand All @@ -9,14 +9,15 @@ import {
} from '@kma-score-serverless/core/index';
import { UnexpectedError } from '@kma-score-serverless/core/shared';
import { container } from '@kma-score-serverless/core/container';
import { HttpResponse } from 'src/shared/util';

class StudentPathParameters {
@IsString()
id!: string;
}

export const handler = middleware().handler(
async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
async (event: APIGatewayProxyEvent): Promise<HttpResponse> => {
const pathParameters = event.pathParameters as StudentPathParameters | null;

const query = plainToInstance(StudentDetailsQuery, {
Expand All @@ -29,7 +30,7 @@ export const handler = middleware().handler(
if (res.isOk) {
return {
statusCode: 200,
body: JSON.stringify(res.value),
body: res.value,
};
}

Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83ef482

Please sign in to comment.