npm.io
11.2.0 • Published 4 months ago

extra-request

Licence
MIT
Version
11.2.0
Deps
6
Size
49 kB
Vulns
0
Weekly
0

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
  signal?: AbortSignal
  keepalive?: boolean
  redirect?: RequestRedirect
  cache?: RequestCache
  priority?: RequestPriority
}

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
request
function request(
  method: 'GET' | 'HEAD' | 'PUT' | 'POST' | 'PATCH' | 'DELETE'
, ...transformers: Array<IRequestOptionsTransformer | 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<Field extends string>(
  header: NonEmptyArray<Field>
, data: Array<Record<Field, number | string>>
): 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
hostname
function hostname(hostname: string): IRequestOptionsTransformer
port
function port(port: number): IRequestOptionsTransformer
pathname
function pathname(pathname: string): IRequestOptionsTransformer
appendPathname
function appendPathname(pathname: string): IRequestOptionsTransformer
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 | undefined>>(
  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(keepalive: boolean = true): IRequestOptionsTransformer
redirect
function redirect(redirect: RequestRedirect): IRequestOptionsTransformer
body
function body(body: BodyInit): IRequestOptionsTransformer
cache
function cache(cache: RequestCache): IRequestOptionsTransformer
priority
function priority(priority: RequestPriority): IRequestOptionsTransformer

Keywords