3.0.4 • Published 8 months ago

@sapphire/fetch v3.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Sapphire Logo

@sapphire/fetch

Tiny wrapper around node-fetch for improved TypeScript and data type support

GitHub codecov npm bundle size npm Depfu

Table of Contents

Description

node-fetch is already a great library for making API calls, but because it focuses solely on bringing the Fetch API to Node.js, it doesn't provide specific error messages and handling for different return types (JSON, Buffer, plain text, etc). This is where @sapphire/fetch comes in. The syntax is more restrictive than that of node-fetch, but that makes it consistent and easier to use in TypeScript.

Features

  • Written in TypeScript
  • Fully tested
  • Exported const enum for the common return data types.
  • Throws distinctive errors when the API returns a "not ok" status code to make them easier to understand.
  • Enforces casting the return type when requesting JSON data, to ensure your return data is strictly typed.

Installation

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm install @sapphire/fetch

Node-fetch types

TypeScript users should also add @types/node-fetch:

npm install --save-dev @types/node-fetch

Alternatively, you can enable the skipLibChecks TypeScript compiler option

Usage

Note: While this section uses import, it maps 1:1 with CommonJS' require syntax. For example, import { fetch } from '@sapphire/fetch' is the same as const { fetch } = require('@sapphire/fetch').

Note: fetch can also be imported as a default import: import fetch from '@sapphire/fetch'.

GETting JSON data

// Import the fetch function
import { fetch, FetchResultTypes } from '@sapphire/fetch';

interface JsonPlaceholderResponse {
	userId: number;
	id: number;
	title: string;
	completed: boolean;
}

// Fetch the data. No need to call `.json()` after making the request!
const data = await fetch<JsonPlaceholderResponse>('https://jsonplaceholder.typicode.com/todos/1', FetchResultTypes.JSON);

// Do something with the data
console.log(data.userId);

GETting Buffer data (images, etc.)

// Import the fetch function
import { fetch, FetchResultTypes } from '@sapphire/fetch';

// Fetch the data. No need to call `.buffer()` after making the request!
const sapphireLogo = await fetch('https://github.com/sapphiredev.png', FetchResultTypes.Buffer);

// sapphireLogo is the `Buffer` of the image
console.log(sapphireLogo);

POSTing JSON data

// Import the fetch function
import { fetch, FetchResultTypes, FetchMethods } from '@sapphire/fetch';

// Fetch the data. No need to call `.json()` after making the request!
const responseData = await fetch(
	'https://jsonplaceholder.typicode.com/todos',
	{
		method: FetchMethods.Post,
		headers: {
			'Content-Type': 'application/json'
		},
		body: JSON.stringify({
			name: 'John Doe'
		})
	},
	FetchResultTypes.JSON
);

// Do something with the response data
console.log(responseData);

API Documentation

For the full API documentation please refer to the TypeDoc generated documentation.

Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate WithAddress
Open CollectiveClick Here
Ko-fiClick Here
PatreonClick Here
PayPalClick Here

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

3.0.4

8 months ago

3.0.4-next.63feeab8

10 months ago

3.0.4-next.855b323f

10 months ago

3.0.4-next.6100b95e

10 months ago

3.0.4-next.8810833c

10 months ago

3.0.4-next.4a5bf954

10 months ago

3.0.4-next.f3243e67

10 months ago

3.0.4-next.5c8eaf07

10 months ago

3.0.4-next.b77ee28f

10 months ago

3.0.3-next.1e73db92

11 months ago

3.0.3-next.bedc937e

11 months ago

3.0.3-next.ef2a0072

11 months ago

3.0.3-next.99f8c9a5

11 months ago

3.0.3-next.38151673

11 months ago

3.0.4-next.98befbf8

10 months ago

3.0.3-next.db356eb0

11 months ago

3.0.3-next.1c613f60

11 months ago

3.0.3-next.b2a9e60c

11 months ago

3.0.3-next.6d981b24

12 months ago

3.0.3-next.c367691b

11 months ago

3.0.3-next.2e9590be

11 months ago

3.0.3-next.f63a9a11

10 months ago

3.0.3-next.479de108

10 months ago

3.0.3-next.c6f8a90f

10 months ago

3.0.3-next.0645081b

11 months ago

3.0.3-next.68ee882a

11 months ago

3.0.3-next.997614c6

11 months ago

3.0.3-next.c589ab8b

12 months ago

3.0.4-next.aded2f12

10 months ago

3.0.3-next.269734b3

11 months ago

3.0.3-next.0cc95647

11 months ago

3.0.3-next.8bfb0191

10 months ago

3.0.3-next.7c2bed20

11 months ago

3.0.4-next.4a46e518

10 months ago

3.0.3-next.6f32caca

11 months ago

3.0.3-next.3cfc4fd3

11 months ago

3.0.3

10 months ago

3.0.3-next.11863ecd

11 months ago

3.0.3-next.8aee4b20

11 months ago

3.0.4-next.9b2e7d8e

10 months ago

3.0.4-next.b180557f

10 months ago

3.0.3-next.ec73e32c

12 months ago

3.0.3-next.655d8715

11 months ago

3.0.3-next.77c7f288

12 months ago

3.0.3-next.bcef5359

11 months ago

3.0.4-next.1ac67e71

10 months ago

3.0.2

1 year ago

3.0.1

2 years ago

3.0.0

2 years ago

2.4.2

2 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.0.4

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago