Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/infrastructure/api/apis/local-server/apis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type {
ErrorResponse,
InternalCallParams,
ResponseNecessary,
SuccessResponse,
} from '../../domain';
import type { TestRequest, TestResponse } from './schemas';

type GetApisProps = {
callWithToken: <R extends ResponseNecessary>(
p: InternalCallParams & { token: string }
) => Promise<R | ErrorResponse>;
callWithoutToken: <R extends ResponseNecessary>(
p: InternalCallParams & { token?: never }
) => Promise<R | ErrorResponse>;
callWithOptionalToken: <R extends ResponseNecessary>(
p: InternalCallParams & { token?: string }
) => Promise<R | ErrorResponse>;
};

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<SuccessResponse<TestResponse>>({
method: 'GET',
path: 'test',
body,
}),
}) satisfies Record<string, Api>;
1 change: 1 addition & 0 deletions src/infrastructure/api/apis/local-server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getLocalServerApis } from './apis';
7 changes: 7 additions & 0 deletions src/infrastructure/api/apis/local-server/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type TestRequest = {
test: number;
};

export type TestResponse = {
message: string;
};
55 changes: 55 additions & 0 deletions src/infrastructure/api/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getLocalServerApis } from './apis/local-server';
import type {
ErrorResponse,
ExternalCallParams,
InternalCallParams,
ResponseNecessary,
} from './domain';

type ImplApiProps = {
externalCall(_: ExternalCallParams): Promise<ResponseNecessary>;
};

export const implApi = ({ externalCall }: ImplApiProps) => {
const internalCall = async <R extends ResponseNecessary>(content: {
method: string;
path: string;
body?: Record<string, unknown>;
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 = <R extends ResponseNecessary>(
p: InternalCallParams & { token: string }
) => internalCall<R | ErrorResponse>(p);

const callWithoutToken = <R extends ResponseNecessary>(
p: InternalCallParams & { token?: never }
) => internalCall<R | ErrorResponse>(p);

const callWithOptionalToken = <R extends ResponseNecessary>(
p: InternalCallParams & { token?: string }
) => internalCall<R | ErrorResponse>(p);

return getLocalServerApis({
callWithToken,
callWithoutToken,
callWithOptionalToken,
});
};

export type Apis = ReturnType<typeof getLocalServerApis>;
36 changes: 36 additions & 0 deletions src/infrastructure/api/domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export type ResponseNecessary = {
status: number;
data: unknown;
};

// TODO: 200 이외의 코드가 내려왔을 때 대응 필요
export type SuccessResponse<T, Status extends number = 200> = {
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<string, unknown>;
headers?: Record<string, string>;
credentials?: string;
};

export type InternalCallParams = {
method: string;
path: string;
body?: Record<string, unknown>;
token?: string;
};
2 changes: 2 additions & 0 deletions src/infrastructure/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { type Apis, implApi } from './client';
export type { ExternalCallParams } from './domain';
Loading