Skip to content

HyunsDev/endpoint-client

Repository files navigation

endpoint-client

타입 안전한 API 엔드포인트 추상화

MIT License MIT License


install

npm install endpoint-client
yarn add endpoint-client

Usage

엔드포인트 정의

import { Endpoint } from "endpoint-client";

export type GetEndpointParameter = {
    pathItem: string;
    bodyItem: string;
    queryItem: string;
};

export type GetEndpointResponse = {
    name: string;
};

export const GetEndpoint: Endpoint<GetEndpointParameter, GetEndpointResponse> =
    {
        method: "GET",
        path: (e) => `/endpoint/${e.pathItem}`,
        bodyParams: ["bodyItem"],
        pathParams: ["pathItem"],
        queryParams: ["queryItem"],
    };

클라이언트 정의

import { EndpointClient } from "endpoint-client";
import { GetEndpoint } from "...";

export class Client extends EndpointClient {
    readonly endpoint = {
        get: this.endpointBuilder(GetEndpoint),
    };
}

export const client = new Client({
    baseUrl: "https://api.example/com",
});

엔드포인트 사용

...
    const res = await client.endpoint.get({
        pathItem: 'a',
        bodyItem: 'b',
        queryItem: 'c',
    });
    console.log(res);
...

About

API 요청을 함수로 추상화하기 위한 클라이언트 라이브러리

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published