0.3.1 • Published 3 years ago

request-with v0.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Request With

Usage

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' });
0.3.0

3 years ago

0.2.0

3 years ago

0.3.1

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago