Skip to content

Commit

Permalink
optional headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpilott committed Jul 25, 2020
1 parent 21caa92 commit ed81fbe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ Handy sapper fetch wrapper
- `params` **[object][56]** object to convert to query string (optional, default `{}`)
- `preload` **[boolean][58]** instruction to use sappers preload fetch (optional, default `false`)
- `api` **[boolean][58]** toggle to opt out of the prepended API_URL env var (optional, default `true`)
- `head` **([array][59] \| [boolean][58])** include headers in return (optional, default `false`)

Returns **[object][56]** fetch data in json format

**Meta**

- **version**: 1.1.2
- **version**: 1.2.0

### push

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neuekit/utilities",
"version": "1.5.7",
"version": "1.6.0",
"license": "MIT",
"description": "Tree-shakeable JavaScript utilities, add yours today!",
"main": "dist/bundle.js",
Expand All @@ -22,6 +22,7 @@
"qss": "^2.0.3"
},
"devDependencies": {
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.9.6",
"@rollup/plugin-babel": "^5.0.0",
"@rollup/plugin-commonjs": "^11.1.0",
Expand Down
15 changes: 10 additions & 5 deletions src/svelte/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ import { encode } from 'qss';
/**
* Handy sapper fetch wrapper
* @memberof Svelte
* @version 1.1.3
* @version 1.2.0
* @param {string} endpoint api url
* @param {object} [params={}] object to convert to query string
* @param {boolean} [preload=false] instruction to use sappers preload fetch
* @param {boolean} api toggle to opt out of the prepended API_URL env var
* @param {array|boolean} head include headers in return
* @returns {object} fetch data in json format
*/

export default async function (
endpoint,
params = {},
preload = false,
api = true
api = true,
head = false
) {
const base = (api && process.env.API_URL) || '';
const url = base + endpoint + encode(params, '?');
const req = await (preload ? preload.fetch(url) : fetch(url));
const res = await req.json();
const res = await (preload ? preload.fetch(url) : fetch(url));
const json = await res.json();
const headers =
head &&
head.reduce((o, key) => ({ ...o, [key]: res.headers.get(key) }), {});

return res;
return head ? { headers, json } : json;
}

0 comments on commit ed81fbe

Please sign in to comment.