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 intofetch()
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 takesMethod
enum value or a string. Any HTTP method is valid -
dataGetter
parameter takes a lambda that gets later called on theResponse
fromfetch()
. The default isasync (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()
});