Skip to content

BlackGlory/extra-request

Repository files navigation

extra-request

Utilities for Request.

Install

npm install --save extra-request
# or
yarn add extra-request

Usage

import { post } from 'extra-request'
import { url, json } from 'extra-request/transformers'

const req = post(
  url('http://example.com')
, json({ hello: 'world' })
)
const res = await fetch(req)

API

interface IRequestOptions {
  url?: URL
  headers?: Headers
  payload?:
  | BodyInit // WHATWG
  | NodeJS.ReadableStream // node-fetch
  signal?: AbortSignal
  keepalive?: boolean
  redirect?: RequestRedirect
}

type IRequestOptionsTransformer = (options: IRequestOptions) => RequestOptions

get

function get(...transformers: Array<IRequestOptionsTransformer | Falsy>): Request

head

function head(...transformers: Array<IRequestOptionsTransformer | Falsy>: Request

post

function post(...transformers: Array<IRequestOptionsTransformer | Falsy>): Request

put

function put(...transformers: Array<IRequestOptionsTransformer | Falsy>): Request

patch

function patch(...transformers: Array<IRequestOptionsTransformer | Falsy>): Request

del

function del(...transformers: Array<IRequestOptionsTrransformer | Falsy>): Request

pipeRequestOptionsTransformers

function pipeRequestOptionsTransformers(
  ...transformers: Array<IRequestOptionsTransformer | Falsy>
): IRequestOptions

Transformers

url

function url(...urls: NonEmptyArray<string | URL>): IRequestOptionsTransformer

text

function text(payload: string): IRequestOptionsTransformer

json

function json<T extends JSONValue | JSONSerializable<any>>(
  payload: T
): IRequestOptionsTransformer

csv

function csv<T extends object>(payload: T[]): IRequestOptionsTransformer

signal

function signal(signal: AbortSignal): IRequestOptionsTransformer

header

function header(name: string, value: string): IRequestOptionsTransformer

appendHeader

function appendHeader(name: string, value: string): IRequestOptionsTransformer

headers

function headers<T extends Record<string, string>>(
  headers: T
): IRequestOptionsTransformer

host

function host(host: string): IRequestOptionsTransformer

port

function port(port: number): IRequestOptionsTransformer

pathname

function pathname(pathname: string): IRequestOptionsTransformer

appendPathname

function appendPathname(pathname: string): IRequestOptionsTransformer

search

function search(search: string): IRequestOptionsTransformer

searchParam

function searchParam(
  name: string
, value: string | number
): IRequestOptionsTransformer

appendSearchParam

function appendSearchParam(
  name: string
, value: string | number
): IRequestOptionsTransformer

searchParams

function searchParams<T extends Record<string, string | number>>(
  searchParams: T
): IRequestOptionsTransformer

formDataField

function formDataField(
  name: string
, value: string | string[] | Blob
): IRequestOptionsTransformer

basicAuth

function basicAuth(username: string, password: string): IRequestOptionsTransformer

bearerAuth

function bearerAuth(token: string): IRequestOptionsTransformer

keepalive

function keepalive(val: boolean = true): IRequestOptionsTransformer

redirect

function redirect(val: RequestRedirect): IRequestOptionsTransformer

body

function body(val: BodyInit | NodeJS.ReadableStream): IRequestOptionsTransformer