deno.land / x / fetch_goody@v5.0.1

deno-fetch-goody

This library offers a fetch wrapper that can:

  • automatically add Content-Type header
  • directly use objects for body as json (see below)
  • send form, formData, qs (query string) easily from objects
  • set a validator globally or per request, to reject when validator throws.
  • accept a timeout option and abort when timeout is reached
  • add Accept header with value application/json, text/plain, */* if not already set by you

Version v5.0.0+ is the recommended version now (abort controller is used now). please don't use v4 of fetch goody anymore.

Deno v1.11+ is required.

usage

you can import wrapFetch from mod.ts file.

export { wrapFetch } from 'https://deno.land/x/fetch_goody@v5.0.0/mod.ts';

wrapFetch

// this simple
const wrappedfetch = wrapFetch();

Or

// you can also pass your own wrapped fetch function, allowing for wrapping fetch multiple times
const wrappedfetch = wrapFetch({ fetch: yourFetch });

using the new wrappedfetch

// v3.0.0 : for sending a multipart/form-data body now you should use `formData`.
const resp1 = await wrappedfetch("url",{
  form: {
    'foo': 'bar'
  }
}); // sets method to POST by default and converts object to application/x-www-form-urlencoded.



// or 

const resp2 = await wrappedfetch("url",{
  body: {
    'foo': 'bar'
  }
}); // is sent as json and corresponding header is set
// also if method is not defined for this, it will be set as POST

// other features:

// adding query string

const resp3 = await wrappedfetch("url",{
  qs: {
    'foo': 'bar'
  }
}); // results to url being sent to be "url?foo=bar"

// adding a response validator where you can throw errors

const resp4 = await wrappedfetch("url",{
  validator(response: Response, init: ExtendedRequestInit) {
    if (response.status > 200) {
      throw new Error('yada');
    }
  }
});

test

fetch wrapper tests require network access to emulate server.

run with deno test --allow-net

fetch_goody
https://deno.land/x/fetch_goody
GitHub Stars
1

Version Info

Tagged at
5 months ago

External Dependencies