axios request wrapper
yarn add api-request-client
ornpm install api-request-client
import { APIRequestClient, ResponseError } from 'api-request-client';
interface User {
name: string;
}
const createUser = async () => {
const requestClient = new APIRequestClient({
url: 'users',
baseURL: 'http://localhost:8080',
});
try {
const response = await requestClient
.setMethod('POST')
.setData({
name: 'jiseung',
})
.send<User>();
console.log(response.name);
} catch (err) {
if (err instanceof ResponseError) {
if (err.errorResponse.status === 400) {
alert('bad request');
}
}
}
};
import { APIRequestClient, ResponseError } from 'api-request-client';
const form: FormData = new FormData();
form.append('test', '1');
const response = await request
.setMethod('POST')
.setBodyType('form')
.setData(form)
.send<Params>();