1.0.18 • Published 6 years ago

rest-fetcher v1.0.18

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

rest-fetcher

npm version npm downloads

A simple REST API fetcher module.

Install

$ npm install rest-fetcher --save

Features

  • Lightweight REST API module.
  • Support both browser and node.js runtime environment (based on isomorphic-fetch).
  • Built-in client-side timeout.
  • Error handling.

Usage

import { Fetcher } from 'rest-fetcher';
const config = {baseUrl: 'http://<your_server_url>'};
const fetcher = new Fetcher(config);


// For GET requests
// args are optional key-value paris for query parameters.
const response = await fetcher.get('/some/api/path', [ { p1: 'k1' }, { p2: 'k2' } ]);

// For POST requests
 response = await fetcher.post('/some/api/path', body);

 // For PUT requests
 response = await fetcher.put('/some/api/path', body);

 // For PATCH requests
 response = await fetcher.patch('/some/api/path', body);

 // For DELETE requests
 response = await fetcher.delete('/some/api/path', body);


 // Fetcher module has a built-in client-side timeout (set to 1 minute by default)
 // You can set your own default by sending a second parameter to the constructor:
 const config = {baseUrl: 'http://<your_server_url>', defaultTimeout: 12000};
 const fetcher = new Fetcher(config);

 // Furthermore, you can overwrite the default timeout on a specific API call:
 response = await fetcher.post('/some/api/path', body, 300000);

Headers Support

import { Fetcher } from 'rest-fetcher';

// You can assign default headers (optional) that will be sent upon each request
const config = {
        baseUrl: 'http://<your_server_url>',
        defaultHeaders: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }
      };

const fetcher = new Fetcher(config);


// Or send headers for a specific request (in this case those headers will be added to the default).
const response = await fetcher.get('/some/api/path', body, headers);

Credentials Mode Support

import { Fetcher } from 'rest-fetcher';

// You can set credentials mode (optional) that will be sent upon each request
const config = {
        baseUrl: 'http://<your_server_url>',
        credentialsMode: "include" // | "same-origin" | "omit"
      };

const fetcher = new Fetcher(config);
1.0.18

6 years ago

1.0.17

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago