diff --git a/src/infrastructure/api/apis/local-server/apis.ts b/src/infrastructure/api/apis/local-server/apis.ts new file mode 100644 index 0000000..68dd90a --- /dev/null +++ b/src/infrastructure/api/apis/local-server/apis.ts @@ -0,0 +1,36 @@ +import type { + ErrorResponse, + InternalCallParams, + ResponseNecessary, + SuccessResponse, +} from '../../domain'; +import type { TestRequest, TestResponse } from './schemas'; + +type GetApisProps = { + callWithToken: ( + p: InternalCallParams & { token: string } + ) => Promise; + callWithoutToken: ( + p: InternalCallParams & { token?: never } + ) => Promise; + callWithOptionalToken: ( + p: InternalCallParams & { token?: string } + ) => Promise; +}; + +type Api = (_: { + body: never; + token: string; + params: never; + query: never; +}) => Promise<{ status: number; data: unknown }>; + +export const getLocalServerApis = ({ callWithoutToken }: GetApisProps) => + ({ + 'GET /test': ({ body }: { body: TestRequest }) => + callWithoutToken>({ + method: 'GET', + path: 'test', + body, + }), + }) satisfies Record; diff --git a/src/infrastructure/api/apis/local-server/index.ts b/src/infrastructure/api/apis/local-server/index.ts new file mode 100644 index 0000000..b14c9ba --- /dev/null +++ b/src/infrastructure/api/apis/local-server/index.ts @@ -0,0 +1 @@ +export { getLocalServerApis } from './apis'; diff --git a/src/infrastructure/api/apis/local-server/schemas.ts b/src/infrastructure/api/apis/local-server/schemas.ts new file mode 100644 index 0000000..f17b97b --- /dev/null +++ b/src/infrastructure/api/apis/local-server/schemas.ts @@ -0,0 +1,7 @@ +export type TestRequest = { + test: number; +}; + +export type TestResponse = { + message: string; +}; diff --git a/src/infrastructure/api/client.ts b/src/infrastructure/api/client.ts new file mode 100644 index 0000000..3d5f739 --- /dev/null +++ b/src/infrastructure/api/client.ts @@ -0,0 +1,55 @@ +import { getLocalServerApis } from './apis/local-server'; +import type { + ErrorResponse, + ExternalCallParams, + InternalCallParams, + ResponseNecessary, +} from './domain'; + +type ImplApiProps = { + externalCall(_: ExternalCallParams): Promise; +}; + +export const implApi = ({ externalCall }: ImplApiProps) => { + const internalCall = async (content: { + method: string; + path: string; + body?: Record; + token?: string; + }) => { + const response = await externalCall({ + method: content.method, + path: content.path, + body: content.body, + headers: { + 'content-type': 'application/json;charset=UTF-8', + ...(content.token !== undefined + ? { Authorization: `Bearer ${content.token}` } + : {}), + }, + credentials: 'include', + }); + + return response as R; + }; + + const callWithToken = ( + p: InternalCallParams & { token: string } + ) => internalCall(p); + + const callWithoutToken = ( + p: InternalCallParams & { token?: never } + ) => internalCall(p); + + const callWithOptionalToken = ( + p: InternalCallParams & { token?: string } + ) => internalCall(p); + + return getLocalServerApis({ + callWithToken, + callWithoutToken, + callWithOptionalToken, + }); +}; + +export type Apis = ReturnType; diff --git a/src/infrastructure/api/domain.ts b/src/infrastructure/api/domain.ts new file mode 100644 index 0000000..bc152e5 --- /dev/null +++ b/src/infrastructure/api/domain.ts @@ -0,0 +1,36 @@ +export type ResponseNecessary = { + status: number; + data: unknown; +}; + +// TODO: 200 이외의 코드가 내려왔을 때 대응 필요 +export type SuccessResponse = { + status: Status; + data: T; +}; + +export type ErrorResponse< + Status extends number = 400 | 401 | 403 | 404 | 409 | 500, +> = { + status: Status; + data: { + timestamp: string; + message: string; + code: string; + }; +}; + +export type ExternalCallParams = { + method: string; + path: string; + body?: Record; + headers?: Record; + credentials?: string; +}; + +export type InternalCallParams = { + method: string; + path: string; + body?: Record; + token?: string; +}; diff --git a/src/infrastructure/api/index.ts b/src/infrastructure/api/index.ts new file mode 100644 index 0000000..933b46b --- /dev/null +++ b/src/infrastructure/api/index.ts @@ -0,0 +1,2 @@ +export { type Apis, implApi } from './client'; +export type { ExternalCallParams } from './domain';