https://github.com/doasync/fry
Tiny declarative HTTP client based on the browser Fetch API
https://github.com/doasync/fry
Last synced: 12 months ago
JSON representation
Tiny declarative HTTP client based on the browser Fetch API
- Host: GitHub
- URL: https://github.com/doasync/fry
- Owner: doasync
- License: mit
- Created: 2020-05-28T22:45:06.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T21:09:14.000Z (about 3 years ago)
- Last Synced: 2025-03-20T16:16:33.503Z (12 months ago)
- Language: TypeScript
- Size: 1.49 MB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[![NPM Version][npm-image]][npm-url] ![NPM Downloads][downloads-image] [![GitHub issues][issues-image]][issues-url]
[npm-image]: https://img.shields.io/npm/v/fry.svg
[npm-url]: https://www.npmjs.com/package/fry
[downloads-image]: https://img.shields.io/npm/dw/fry.svg
[deps-image]: https://david-dm.org/doasync/fry.svg
[issues-image]: https://img.shields.io/github/issues/doasync/fry.svg
[issues-url]: https://github.com/doasync/fry/issues
# Benefits over plain `fetch`
- Extended API
- Treats non-2xx status codes as errors
- Handles JSON
- Custom defaults
- Easy interceptors
- Transform function
- And more...
> `fry` is written in TypeScript
---
## Installation
```bash
npm install fry
```
## Description
fry is `fetch` on steroids :)
There are two exports in the package: `request` and `createRequest`
`createRequest` is used to create a `request` with predefined config
The `request` accepts the same params/options as `fetch` as well as additional ones: baseUrl, url, data, params, fn, silent
```md
url - the only required param,
baseUrl - will be prepended to url,
data - object for json body,
params - query as an object,
silent - do not fall on http errors,
fn - preparation of the result (if necessary)
```
`fn` parameters:
```md
request - object of the request,
response - object of the response,
jsonData - parsed json,
config - what was passed to the request
```
There are also basic inteceptors:
```md
onBeforeRequest - change config,
onRequestError - return a new response (or rethrow an error),
onBeforeResponse - do something before response is handled,
onResponseError - return a new response or handle errors,
```
## Usage
```typescript
import { createRequest } from 'fry'; // import { request } from "fry";
const request = createRequest({
baseUrl: 'https://app.io/api',
redirect: 'error',
});
export const checkUser = id =>
request({
url: 'user',
method: 'post',
data: { userId: id }, // json
fn: ({ jsonData: [user], config }) =>
Boolean(config.data.userId === user.id && user.exists === true), // return boolean
});
// No need for async / await
export const submitTransaction = tx =>
request({
url: 'transactions',
method: 'post',
data: { base64 },
fn: ({ config, request, response, jsonData, resource, init, baseConfig }) =>
tx, // returns tx
});
export const activate = accountId =>
request({
url: `accounts/${accountId}`,
method: 'post',
}); // returns jsonData if no `fn`
const getAccount = accountId => request(`accounts/${accountId}`); // 'get' method is default
// Using TypeScript
const fetchCountry = async countryId =>
request(`api/countries/${countryId}`) as Promise;
const fetchLocations = async () =>
request({
url: 'api/locations/countries/',
fn: ({ jsonData }) => convertCountriesToLocations(jsonData as Country[]),
});
```
### Types
Config
```ts
export type Config = {
url?: Url;
baseUrl?: BaseUrl;
params?: Params;
data?: Json;
fn?: Fn;
silent?: boolean;
onBeforeRequest?: (config: Config) => Config | Promise>;
onRequestError?: (meta: {
config: Config;
request: Request;
error: unknown;
}) => Response | Promise;
onBeforeResponse?: (meta: {
config: Config;
response: Response;
request: Request;
}) => Response | Promise;
onResponseError?: (meta: {
config: Config;
response: Response;
request: Request;
error: Error;
}) => Response | Promise;
} & RequestInit;
```
Other
```ts
export type Json = JsonPrimitive | JsonObject | JsonArray;
export type JsonPrimitive = string | number | boolean | null;
export type JsonObject = { [key: string]: Json };
export type JsonArray = Json[];
export type ObjectString = { [key: string]: string };
export type Params = string[][] | ObjectString | string | URLSearchParams;
export type Url = string;
export type BaseUrl = string;
export type Fn = (meta: {
config: ConfigFn;
request: Request;
response: Response;
jsonData?: unknown;
}) => R;
```
### Repository
---
GitHub ★: https://github.com/doasync/fry