Skip to main content
Deno 2 is finally here 🎉️
Learn more

deno-fetch-goody

This library offers a fetch wrapper that automatically adds Content-Type header. also it can accepts object as body now to send as json.

Also adds Accept header with value application/json, text/plain, */* if not already set by you.

usage

you can import wrapFetch from mod.ts file.

export { wrapFetch } from 'https://deno.land/x/fetch_goody@v1.3.3/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({ fetchFn: yourFetch });

using the new wrappedfetch

const resp1 = await wrappedfetch("url",{
  form: {
    'foo': 'bar'
  }
}); // sets method to POST by default and converts object to FormData.

// 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

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

test

fetch wrapper tests require network access to emulate server.

run with deno test --allow-net