@aspida/node-fetch
TypeScript icon, indicating that this package has built-in type declarations

1.14.0 • Public • Published

@aspida/node-fetch


aspida


node-fetch client for aspida.


Getting Started

Installation

  • Using npm:

    $ npm install aspida node-fetch @aspida/node-fetch
    $ npm install @types/node-fetch --save-dev
  • Using Yarn:

    $ yarn add aspida node-fetch @aspida/node-fetch
    $ yarn add @types/node-fetch --dev

Make HTTP request from application

src/index.ts

import fetch, { Response } from "node-fetch";
import aspida, { HTTPError } from "@aspida/node-fetch";
import api from "../api/$api";

const fetchConfig = {
  baseURL: "https://example.com/api",
  throwHttpErrors: true, // throw an error on 4xx/5xx, default is false
};

const client = api(aspida(fetch, fetchConfig));
(async () => {
  const userId = 0;
  const limit = 10;

  await client.v1.users.post({ name: "mario" });

  const res = await client.v1.users.get({ query: { limit } });
  console.log(res);
  // req -> GET: https://example.com/api/v1/users/?limit=10
  // res -> { status: 200, data: [{ id: 0, name: "mario" }], headers: {...} }

  try {
    const user = await client.v1.users._userId(userId).$get();
    console.log(user);
    // req -> GET: https://example.com/api/v1/users/0
    // res -> { id: 0, name: "mario" }
  } catch (e) {
    if (e instanceof HTTPError) {
      console.log(e.response instanceof Response); // true
    } else {
      console.log(e.message);
    }
  }
})();

Serialize GET parameters manually

src/index.ts

import fetch from "node-fetch";
import aspida, { HTTPError } from "@aspida/fetch";
import qs from "qs";
import api from "../api/$api";

const fetchConfig = {
  paramsSerializer: params => qs.stringify(params),
};

const client = api(aspida(fetch, fetchConfig));
(async () => {
  const users = await client.v1.users.$get({
    // config: { paramsSerializer: (params) => qs.stringify(params) },
    query: { ids: [1, 2, 3] },
  });
  console.log(users);
  // req -> GET: /v1/users/?ids%5B0%5D=1&ids%5B1%5D=2&ids%5B2%5D=3
  // decoded ->             ids[0]=1    &ids[1]=2    &ids[2]=3
  // res -> [{ id: 1, name: "taro1" }, { id: 2, name: "taro2" }, { id: 3, name: "taro3" }]
})();

License

@aspida/node-fetch is licensed under a MIT License.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.14.0596latest

Version History

VersionDownloads (Last 7 Days)Published
1.14.0596
1.13.31
1.13.20
1.13.10
1.13.01
1.12.0413
1.11.01
1.10.31
1.10.21
1.10.11
1.10.01
1.9.11
1.9.01
1.8.11
1.8.00
1.7.11
1.7.03
1.6.31
1.6.21
1.6.11
1.6.01
1.5.01
1.4.10
1.4.01
1.3.01
1.2.11
1.2.01
1.1.10
1.1.01
1.0.01
0.9.21
0.9.10
0.9.01
0.8.41
0.8.31
0.8.20
0.8.11
0.8.00
0.7.20
0.7.11
0.7.01
0.6.41
0.6.30
0.6.20
0.6.11
0.6.00
0.5.11
0.5.01
0.4.01
0.3.01
0.2.30
0.2.21
0.2.11
0.2.00
0.1.20
0.1.10
0.1.01

Package Sidebar

Install

npm i @aspida/node-fetch

Weekly Downloads

1,050

Version

1.14.0

License

MIT

Unpacked Size

16.3 kB

Total Files

11

Last publish

Collaborators

  • m-mitsuhide
  • solufa