EP12. API Response Format
API Response Format์ ์ค๊ณํ๊ณ ๊ทธ ์ด์ ๋ฅผ ํ์ตํฉ๋๋ค.
1. ํ ์๋น์ค API Response ํ์ด๋ณด๊ธฐ
์ ๋ช
์๋น์ค๋ค์ API Response๋ฅผ ์ดํด๋ณด๋ฉฐ ์ ๊ทธ๋ ๊ฒ ๊ตฌํํ์์ง ๊ณ ๋ฏผํด๋ด
์๋ค.
1.
๋ค์ด๋ฒ ์นํฐ
{
"code": 20002,
"message": "์ฑ๊ณต",
"result": {
"appVersion": "2.19.0",
"titleId": "793887"
}
}
JSON
๋ณต์ฌ
2.
์ฟ ํก
{
"rCode": "RET0000",
"rMessage": "SUCCESS",
"rData": true
}
JSON
๋ณต์ฌ
3.
ํ์ด์ฝ
{
"code": 0,
"message": "success",
"result": {
"popupList": [],
"floatingPopup": null
}
}
JSON
๋ณต์ฌ
4.
๋ฐฐ๋ฌ์๋ฏผ์กฑ
{
"status": "SUCCESS",
"message": "์ฑ๊ณต",
"serverDatetime": "2024-06-18 22:44:12",
"data": {}
}
JSON
๋ณต์ฌ
๋ค๋ฅธ ์๋น์ค ์ฑ์ธ๋ฐ Response Format์ด ๋น์ทํ ์ด์ ์ ๋ํด์ ๊ณ ๋ฏผํด๋ด
์๋ค.
2. ๊ณตํต Response Format ๊ตฌํ
functions/api ๋๋ ํ ๋ฆฌ ํ์์ lib/response ๋๋ ํ ๋ฆฌ๋ฅผ ์ถ๊ฐํฉ๋๋ค.
functions/api/lib/response/responseCode.ts
export enum ResponseCode {
SUCCESS = 10000,
INVALID_ARGUMENTS = 40000,
SERVER_ERROR = 50000,
// ์๋น์ค์ ํ์ํ ์ค๋ฅ๋ฅผ ์ถ๊ฐํด์ ์ฌ์ฉํฉ๋๋ค.
}
TypeScript
๋ณต์ฌ
โข
์๋น์ค์์ ๋ฐ์ํ ์ ์๋ ์ค๋ฅ๋ฅผ ์ธ๋ถํํ์ฌ Code๋ก ํํํฉ๋๋ค.
functions/api/lib/response/responseFormat.ts
import { ResponseCode } from "./responseCode.ts";
// ResponseFormat ์ธํฐํ์ด์ค๋ API ์๋ต์ ํ์์ ์ ์ํฉ๋๋ค.
// ์ ๋ค๋ฆญ ํ์
T๋ JSON ๊ฐ์ฒด ๋๋ null์ด์ด์ผ ํฉ๋๋ค.
// deno-lint-ignore no-explicit-any๋ Record<string, any> ์ฌ์ฉ ์ lint ์ค๋ฅ๋ฅผ ๋ฌด์ํ๋๋ก ํฉ๋๋ค.
// deno-lint-ignore no-explicit-any
export interface ResponseFormat<T extends Record<string, any> | null> {
// ์๋ต ์ฝ๋
code: ResponseCode;
// ์๋ต ๋ฉ์์ง
message: string;
// ์๋ต ๊ฒฐ๊ณผ, JSON ๊ฐ์ฒด ๋๋ null
result: T;
}
// createResponse ํจ์๋ ResponseFormat ํ์
์ ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
// ์ ๋ค๋ฆญ ํ์
T๋ JSON ๊ฐ์ฒด ๋๋ null์ด์ด์ผ ํฉ๋๋ค.
// deno-lint-ignore no-explicit-any๋ Record<string, any> ์ฌ์ฉ ์ lint ์ค๋ฅ๋ฅผ ๋ฌด์ํ๋๋ก ํฉ๋๋ค.
// deno-lint-ignore no-explicit-any
export function createResponse<T extends Record<string, any> | null>(
// ์๋ต ์ฝ๋
code: ResponseCode,
// ์๋ต ๋ฉ์์ง
message: string,
// ์๋ต ๊ฒฐ๊ณผ, JSON ๊ฐ์ฒด ๋๋ null
result: T,
): ResponseFormat<T> {
// ResponseFormat ๊ฐ์ฒด๋ฅผ ๋ฐํ
return { code, message, result };
}
TypeScript
๋ณต์ฌ
โข
code: ์์ฒญ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ Code๊ฐ
โข
message: ์์ฒญ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ์์ง
โข
result: ์์ฒญ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ
3. ๊ณตํต Response Format ์ ์ฉ
supabase/functions/api/controllers/userController.ts
+import { ResponseCode } from "./../lib/response/responseCode.ts";
import { Context } from "https://deno.land/x/hono/mod.ts";
import { UserService } from "../services/userService.ts";
+import { createResponse } from "../lib/response/responseFormat.ts";
// UserController ํด๋์ค๋ FCM ํ ํฐ์ ๊ด๋ฆฌํ๊ธฐ ์ํ ์ปจํธ๋กค๋ฌ์
๋๋ค.
export class UserController {
@@ -17,7 +19,13 @@ export class UserController {
// fcmToken์ด ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฉ์์ง๋ฅผ ๋ฐํํฉ๋๋ค.
if (!fcmToken) {
- return c.json("Missing userID or fcmToken");
+ return c.json(
+ createResponse(
+ ResponseCode.INVALID_ARGUMENTS,
+ "Missing userID or fcmToken",
+ null,
+ ),
+ );
}
// UserService๋ฅผ ํตํด FCM ํ ํฐ์ ์ถ๊ฐํฉ๋๋ค.
@@ -25,7 +33,17 @@ export class UserController {
// ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ์ ์ ํ ๋ฉ์์ง๋ฅผ ๋ฐํํฉ๋๋ค.
return c.json(
- result ? "FCM token added successfully" : "Failed to add FCM token",
+ result
+ ? createResponse(
+ ResponseCode.SUCCESS,
+ "FCM token added successfully",
+ null,
+ )
+ : createResponse(
+ ResponseCode.SERVER_ERROR,
+ "Failed to add FCM token",
+ null,
+ ),
);
}
@@ -35,7 +53,9 @@ export class UserController {
const tokens = await this.userService.getFCMTokensByUserID("mock_user_id");
// ํ ํฐ ๋ชฉ๋ก์ JSON ํ์์ผ๋ก ๋ฐํํฉ๋๋ค.
- return c.json(tokens);
+ return c.json(
+ createResponse(ResponseCode.SUCCESS, "Success", { fcmTokens: tokens }),
+ );
}
// deleteFCMTokenV1 ๋ฉ์๋๋ FCM ํ ํฐ์ ์ญ์ ํ๋ API ์๋ํฌ์ธํธ์
๋๋ค.
@@ -45,7 +65,13 @@ export class UserController {
// fcmToken์ด ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฉ์์ง๋ฅผ ๋ฐํํฉ๋๋ค.
if (!fcmToken) {
- return c.json("Missing userID or fcmToken");
+ return c.json(
+ createResponse(
+ ResponseCode.INVALID_ARGUMENTS,
+ "Missing userID or fcmToken",
+ null,
+ ),
+ );
}
// UserService๋ฅผ ํตํด FCM ํ ํฐ์ ์ญ์ ํฉ๋๋ค.
@@ -56,7 +82,17 @@ export class UserController {
// ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ์ ์ ํ ๋ฉ์์ง๋ฅผ ๋ฐํํฉ๋๋ค.
return c.json(
- result ? "FCM token deleted successfully" : "Failed to delete FCM token",
+ result
+ ? createResponse(
+ ResponseCode.SUCCESS,
+ "FCM token deleted successfully",
+ null,
+ )
+ : createResponse(
+ ResponseCode.SERVER_ERROR,
+ "Failed to delete FCM token",
+ null,
+ ),
);
}
}
Diff
๋ณต์ฌ
โข
API Response๋ฅผ ์์ฑํ๋ ๋ถ๋ถ์์ createResponseํจ์๋ฅผ ์ด์ฉํด ๊ณตํต ํฌ๋งท์ผ๋ก ๋ณ๊ฒฝ
4. ๋ณ๊ฒฝ๋ API Response ํ์ธ
Postman์ ํ์ฉํด ๋ณ๊ฒฝ๋ API Response๋ฅผ ํ์ธํฉ๋๋ค.
5. Public ๋ฐฐํฌ
bash deploy.sh
TypeScript
๋ณต์ฌ
public ๋ฐฐํฌ ํ public API endpoint ํ
์คํธ๋ ์งํํฉ๋๋ค.