# @elara-services/fetch

> The package was made smaller and reworked some stuff within the package. The main change is `.text()`, `.json()` not being async anymore.

Latest version **2.0.3** (published 2025-03-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install @elara-services/fetch
pnpm add @elara-services/fetch
yarn add @elara-services/fetch
bun add @elara-services/fetch
```

## Health

**Score 40/100 (D)** — status: stable.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2025-03-04 |
| First published | 2022-01-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 215.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | SUPERCHIEFYT |
| Maintainers | elara-services |
| Keywords | http, https, request, fetch, url, lightweight |

## Links

- npm: https://www.npmjs.com/package/@elara-services/fetch
- Repository: https://github.com/elara-bots/npm.git#main
- Homepage: https://github.com/elara-bots/npm/tree/main#readme
- Issues: https://github.com/elara-bots/npm/issues
- npm.io page: https://npm.io/package/@elara-services/fetch

## Dependencies (1)

- [form-data](https://npm.io/package/form-data.md) ^4.0.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 2.0.3 (latest) — 2025-03-04
- 2.0.2 — 2023-08-15
- 2.0.1 — 2023-07-12
- 2.0.0 — 2023-07-10
- 1.0.1 — 2022-01-29
- 1.0.0 — 2022-01-10

## README

## This is a rework of [centra](https://npmjs.com/package/centra)
The package was made smaller and reworked some stuff within the package. 
The main change is `<res>.text()`, `<res>.json()` not being async anymore. 

----


## The core lightweight HTTP client for Node
-------
## Install

```shell
npm i @elara-services/fetch
```

## Getting Started

First, require the library.

```js
const { fetch } = require("@elara-services/fetch")
```

Then let's make a request in an async function!

```js
	const res = await fetch("https://example.com")
		.send();
	console.log(res.json())
```

## More advanced usage

### Send data in a JSON body

```js
	fetch("https://example.com/nonexistentJSONAPI", "POST")
	.body({
		name: "Jim"
	}, "json")
	.send()
	.then((res) => {
		/*...*/
	})
```

### Send data in a form body

```js
	fetch("https://example.com/nonexistentJSONAPI", "POST")
	.body({
		name: "Kim"
	}, "form")
	.send()
	.then((res) => {
		/*...*/
	})
```

### Set query string parameters

One at a time:

```js
	fetch("https://example.com/user")
	.query("id", "9101467")
	.send()
	.then((res) => {
		/*...*/
	})
```

Many at a time:

```js
	fetch("https://example.com/user")
	.query({
		id: "9101467",
		name: "Bob"
	})
	.send()
	.then((res) => {
		/*...*/
	})
```

### Set a request timeout

```js
	fetch("https://example.com")
	.timeout(2000)
	.send()
	.then((res) => {
		// Success!
	})
	.catch((err) => {
		// Has the request timed out?
	})
```

### Stream a request's response

In this example, the [stream](https://nodejs.org/api/stream.html) is piped to a file:

```js
	// require the fs module beforehand
	fetch("https://example.com/image.png")
	.stream()
	.send()
	.then((stream) => stream.pipe(fs.createWriteStream(path.join(__dirname, "logo.png"))))
```

### Switch paths on the fly

```js
	fetch("https://example.me/test")
	.path("/hello")
	.send()
	// This will make a request to https://example.com/test/hello
```

### Specify request headers

One at a time:

```js
	fetch("https://example.com")
	.header("Content-Type", "application/json")
	.send()
```

Many at a time:

```js
	fetch("https://example.com")
	.header({
		"Content-Type": "application/json",
		"X-Connecting-With": "elara-bots/fetch"
	})
	.send()
```

### Modify core HTTP request options

See [http.request](https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_http_request_url_options_callback)'s options for more information about core HTTP request options.
Let's change our localAddress as an example.

```js
	fetch("https://example.com")
	.option("localAddress", "127.0.0.2")
	.send()
```

### Accept compressed responses

```js
	fetch("https://example.com")
	.compress()
	.send()
	// This will cause elara-bots/fetch to accept compressed content from the server. (gzip and deflate are currently supported)
```

---
_Source: https://npm.io/package/@elara-services/fetch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
