0.0.3 • Published 1 year ago
@kapustlo/http v0.0.3
HTTP
HTTP class client based on Axios
Installation
npm install @kapustlo/http
Usage
import HTTP from '@kapustlo/http';
const client = new HTTP('https://jsonplaceholder.typicode.com');
type Todo = {
userId: number;
id: number;
title: string;
completed: boolean;
}
const response = await client.get<Todo>('/todos/1');
const data = client.getData<Todo>(response);
console.log(data);
/*
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
*/
`
API
import type {
AxiosRequestconfig,
AxiosResponse
} from 'axios';
type HTTPMethodName =
| 'get' | 'GET'
| 'delete' | 'DELETE'
| 'head' | 'HEAD'
| 'options' | 'OPTIONS'
| 'post' | 'POST'
| 'put' | 'PUT'
| 'patch' | 'PATCH'
| 'purge' | 'PURGE'
| 'link' | 'LINK'
| 'unlink' | 'UNLINK';
export type RequestConfig = AxiosRequestConfig;
export type Response<T=any> = AxiosResponse<T>;
export type JSONData = ReturnType<typeof JSON.parse>;
export type RequestData = JSONData | FormData;
export type QueryParams = {[key: string]: number | string | Array<string | number>};
export type HTTPMethod<T=any> = (path: string, query?: QueryParams, config?: RequestConfig) => Promise<Response<T>>;
export type Headers = {
[key: string]: string
};
interface HTTPInterface {
baseURL?: string;
headers: Headers;
mergePaths(...args: Array<string | number>): string;
request(path: string, method: HTTPMethodName, query?: QueryParams, config?: RequestConfig): Promise<Response>;
getData<T>(response: Response<T>): T;
get: HTTPMethod;
post: HTTPMethod;
delete: HTTPMethod;
patch: HTTPMethod;
}
License
MIT