Define some types before start:
interface MyResponseType {
hello: 'world';
}
interface MyRequestType {
id: number;
}
give an adapter:
import axios from 'axios';
import request from 'request-with';
const url = request.by(request.adapter.axios(axios));
then create request:
const myRequest = url<Promise<MyResponseType>>('/api/xxx')
.with(request.body<MyRequestType>())
.with(request.method.preset('POST'));
const response = await myRequest({ id: 123 });
const myRequest = url<Promise<MyResponseType>>('/api/xxx')
.with(request.method.preset('GET'))
.with(request.query<[date: string]>('date'));
const response = await myRequest('2021-04-13');
const myRequest = url<Promise<MyResponseType>>()
.with(request.template('/api/:id'))
.with(request.headers.preset({ 'X-My-Header': 'Hello' }));
const response = await myRequest({ id: 456 });
const myRequest = url<Promise<MyResponseType>>().with(
request.mix({ date: request.query<[date: string]>('date'), body: request.body<MyRequestType>() }),
);
const response = await myRequest({ body: { id: 456 }, date: '2021-04-13' });