2.0.1 • Published 2 years ago

@angius/http v2.0.1

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

HTTP

A thin but type-safe wrapper on top of fetch()

Installation

yarn add @angius/http
npm i @angius/http
pnpm add @angius/http

Usage

Javascript

const result = Http.get(url);
if (result.isSuccess) {
	console.log(`Success! The value is ${result.getValue()}`);
} else {
	console.error(`Error! ${result.error}`)
}

Typescript

const result = Http.get<{ name: string }>(url);
if (result.isSuccess) {
	console.log(`Success! The name is ${result.getValue().name}`);
} else {
	console.error(`Error! ${result.error}`)
}

HTTP API

The usual

  • T describes the desired type of response
  • config is an optional parameter that gets spread directly into fetch() config should you need to tap into that directly
  • Every method returns a Promise<Result<T>> with a few exceptions mentioned below

GET

  • This method doesn't support sending the body, so the payload parameter isn't here
<T>(url: string, headers?: object, config?: object) => Promise<Result<T>>;

POST

<T>(url: string, payload: object, headers?: object, config?: object) => Promise<Result<T>>;

PATCH

<T>(url: string, payload: object, headers?: object, config?: object) => Promise<Result<T>>;

PUT

<T>(url: string, payload: object, headers?: object, config?: object) => Promise<Result<T>>;

HEAD

  • This method doesn't support returning body, so the generic parameter isn't here
  • This method doesn't support sending the body, so the payload parameter isn't here
(url: string, headers?: object, config?: object) => Promise<Result<void>>;

DELETE

<T>(url: string, payload?: object, headers?: object, config?: object) => Promise<Result<T>>;

CONNECT

  • This method doesn't support sending the body, so the payload parameter isn't here
<T>(url: string, headers?: object, config?: object) => Promise<Result<T>>;

OPTIONS

  • This method doesn't support sending the body, so the payload parameter isn't here
<T>(url: string, headers?: object, config?: object) => Promise<Result<T>>;

TRACE

  • This method doesn't support returning body, so the generic parameter isn't here
  • This method doesn't support sending the body, so the payload parameter isn't here
(url: string, headers?: object, config?: object) => Promise<Result<void>>;

The Unusual

REQUEST

If you ever find yourself in need of something more, the Http.request() function is here for you. Every other function is simply a result of currying to this very method, so treat it as a base of sorts. It has two notable differences:

  • method parameter takes Method enum value or a string. Any HTTP method is valid
  • dataGetter parameter takes a lambda that gets later called on the Response from fetch(). The default is async (res) => await res.json() but you can use it to get any other type of data you need.
<TResponse>({
				url,
				method,
				payload = null,
				headers = null,
				config = null,
				dataGetter = null
			}: RequestOptions) => Promise<Result<TResponse>>;
interface RequestOptions {
	url: string;
	method: Method | string;
	payload?: object;
	headers?: object;
	config?: object;
	dataGetter?: DataGetter;
}
type DataGetter = (res?: Response) => Promise<any>;

Writing your own request function

Just curry it, like this library itself does:

const yeet = <T>(
      url: string,
      payload?: object,
      headers?: object,
      config?: object
) =>
    Http.request<T>({
        url: url,
        method: 'YEET',
        payload: { ...payload, foo: 'bar' },
        headers: headers,
        config: config,
        dataGetter: async (res: Response) => 
            res.headers.has('i-like-text')
                ? await res.text()
                : await res.json()
    });
3.0.0-rc1.1

2 years ago

3.0.0-rc2.1

2 years ago

3.0.0-rc1.2

2 years ago

3.0.0-rc2

2 years ago

3.0.0-rc1

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago