{"id":15809382,"url":"https://github.com/zordius/yfetch","last_synced_at":"2026-01-20T19:32:10.366Z","repository":{"id":19993826,"uuid":"88341281","full_name":"zordius/yfetch","owner":"zordius","description":"Yet another fetch. A guide for \"How to make fetch more beautiful?\".","archived":false,"fork":false,"pushed_at":"2023-01-05T16:20:54.000Z","size":680,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-02T18:40:23.413Z","etag":null,"topics":["fetch","fetch-api","json"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zordius.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-15T10:55:21.000Z","updated_at":"2024-08-23T08:04:45.000Z","dependencies_parsed_at":"2023-01-13T20:43:04.906Z","dependency_job_id":null,"html_url":"https://github.com/zordius/yfetch","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/zordius/yfetch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fyfetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fyfetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fyfetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fyfetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zordius","download_url":"https://codeload.github.com/zordius/yfetch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zordius%2Fyfetch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28610608,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T18:56:40.769Z","status":"ssl_error","status_checked_at":"2026-01-20T18:54:26.653Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fetch","fetch-api","json"],"created_at":"2024-10-05T03:22:25.079Z","updated_at":"2026-01-20T19:32:10.338Z","avatar_url":"https://github.com/zordius.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"yfetch\n======\nYet another fetch. A guide for \"How to make fetch more beautiful?\".\n\n[![npm version](https://img.shields.io/npm/v/yfetch.svg)](https://www.npmjs.org/package/yfetch) [![Build Status](https://travis-ci.org/zordius/yfetch.svg?branch=master)](https://travis-ci.org/zordius/yfetch) [![Test Coverage](https://codeclimate.com/github/zordius/yfetch/badges/coverage.svg)](https://codeclimate.com/github/zordius/yfetch) [![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)\n\n[fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) is a new API based on [Promise](https://developers.google.com/web/fundamentals/getting-started/primers/promises), it help you to prevent callback hell.\n\nI like fetch, so here is a set of use cases and example codes to know how to use yfetch or make your fetch coding style better.\n\n**Core Features**:\n* A set of transform functions to help you to deal with fetch request or response.\n* support JSONP at client side.\n* Accept same options just like fetch with some extensions:\n  * `opts.base` : base url\n  * `opts.query` : will be appended into url automatically\n  * `opts.url` : pass into fetch() as 1st param with processed opts.base and opts.query\n  * `opts.json` : auto json headers in request, then json parse the response.body\n  * `opts.ignoreJsonError` : ignore JSON parse error, and the respose.body will be a string\n  * `opts.error` : rejects when the response http code be included in opts.error array\n  * `response.body` : auto resolved as String or JSON\n  * `response.parsed` : true when opts.json successed\n  * `response.headers` : auto transformed as Object from Header\n  * `response.fetchArgs` : the arguments of the fetch call\n* [debug](https://www.npmjs.com/package/debug) , export DEBUG=... to show debug log:\n  * `yfetch:start`: show url and fetch arguments just before the request starts\n  * `yfetch:raw` : show url, response size, status, headers, raw\n  * `yfetch:result` : show url, response status, body as text or JSON\n  * `yfetch:error` : show url, response status, body as text or JSON, error\n\nInstall\n-------\n\n```\nnpm install yfetch --save\n```\n\nYou will need these polyfills for older browsers or other environments:\n* [Promise](https://www.npmjs.com/search?q=promise%20polyfill\u0026page=1\u0026ranking=popularity) : [browser support](http://caniuse.com/#feat=promises)\n* [fetch](https://www.npmjs.com/search?q=fetch%20polyfill\u0026page=1\u0026ranking=popularity) : [browser support](http://caniuse.com/#feat=fetch)\n\nUsage\n-----\n\n```javascript\nimport yfetch from 'yfetch';\n\n// Same as fetch('https://some.where/test?page=10\u0026size=5', {})\nyfetch({\n  url: '/test',\n  base: 'https://some.where',\n  query: {\n    page: 10,\n    size: 5\n  }\n}).then((ret) =\u003e console.log(ret));\n\n/*\n{\n  url: 'https://some.where/test?page=10\u0026size=5',\n  headers: { ... },\n  status: 200,\n  statusText: 'OK',\n  ok: true,\n  body: '...',                                  // yfetch transformed text or JSON\n  size: 1234,\n  fetchArgs: [                                  // yfetch exntends attribute,\n    'https://some.where/test?page=10\u0026size=5',   // original fetch arguments stores here\n    {}\n  ]\n}\n*/\n```\n\n**JSONP**\n\nYou will need \u003ca href=\"https://www.npmjs.com/package/fetch-jsonp\"\u003efetch-jsonp\u003c/a\u003e for JSONP feature. When you set { jsonp: true } in yfetch arguments and global.fetchJsonp() exists, the request will made by jsonp.\n\n```javascript\n\nimport yfetch from 'yfetch';\n\nyfetch({\n  url: 'http://another.host.com/jsonp',\n  jsonp: true,                          // Required for jsonp\n  // Allow fetch-jsonp options https://github.com/camsong/fetch-jsonp\n  jsonpCallback: 'custom_callback_param_name',            // Optional\n  jsonpCallbackFunction: 'custom_callback_function_name', // Optional\n}).then((ret) =\u003e console.log(ret.body));\n```\n\nTo ensure global.fetchJsonp() ready at client side, you can add these:\n\n```javascript\nimport fetchJsonp from 'fetch-jsonp'\n\nif (global.window) {\n  global.fetchJsonp = fetchJsonp\n}\n```\n\nWhy I need yfetch?\n------------------\n\nCheck these daily use cases, you may use yfetch to make things simple, or just do the same task with more code.\n\n**Get Response body**\n\u003ctable\u003e\n \u003ctr\u003e\n  \u003cth width=\"50%\"\u003ewithout yfetch\u003c/th\u003e\u003cth width=\"50%\"\u003ewith yfetch\u003c/th\u003e\n \u003c/tr\u003e\n \u003ctr\u003e\n  \u003ctd valign=\"top\"\u003e\n\n```javascript\nfetch(url, opts)\n.then((response) =\u003e response.body.text())\n.then (body =\u003e {\n    // body, but response dropped\n});\n```\n\n  \u003c/td\u003e\n  \u003ctd valign=\"top\"\u003e\n\n```javascript\nyfetch({ url, ...opts })\n.then(response =\u003e {\n    // response.body\n});\n```\n\n  \u003c/td\u003e\n \u003c/tr\u003e\n\u003c/table\u003e\n\n**Get Response as JSON**\n\u003ctable\u003e\n \u003ctr\u003e\n  \u003cth width=\"50%\"\u003ewithout yfetch\u003c/th\u003e\u003cth width=\"50%\"\u003ewith yfetch\u003c/th\u003e\n \u003c/tr\u003e\n \u003ctr\u003e\n  \u003ctd valign=\"top\"\u003e\n\n```javascript\nfetch(url, { headers: { Accept: 'application/json' }, ...opts })\n.then((response) =\u003e response.body.json())\n.then (body =\u003e {\n    // body as JSON, but response dropped\n});\n```\n\n  \u003c/td\u003e\n  \u003ctd valign=\"top\"\u003e\n\n```javascript\nyfetch({ url, json: true, ...opts })\n.then(response =\u003e {\n    // response.body as JSON\n});\n```\n\n  \u003c/td\u003e\n \u003c/tr\u003e\n\u003c/table\u003e\n\n**Debug**\n\u003ctable\u003e\n \u003ctr\u003e\n  \u003cth width=\"50%\"\u003ewithout yfetch\u003c/th\u003e\u003cth width=\"50%\"\u003ewith yfetch\u003c/th\u003e\n \u003c/tr\u003e\n \u003ctr\u003e\n  \u003ctd valign=\"top\"\u003e\n\n```javascript\n// ES6 arrow function to return the promise\nmyfetch = (url, opts) =\u003e fetch(url, opts)\n.then((response) =\u003e response.body.json())\n.then (\nbody =\u003e console.log('success', body, url, opts),\nerror =\u003e console.log('error', error, url, opts)\n);\n\n// always use the wrapped version\nmyfetch(url, opts);\n```\n\n  \u003c/td\u003e\n  \u003ctd valign=\"top\"\u003e\n\n```javascript\n// debug in your code....deprecated\nyfetch({ url, ...opts })\n.then(\nresp =\u003e console.log('success', resp.body, resp.fetchArgs),\nerror =\u003e console.log('error', error, error.fetchArgs)\n);\n\n// BETTER: export DEBUG=yfetch:* then\n// just do yfetch without changing your code\nyfetch({url, json: true, ...opts})\n```\n\n  \u003c/td\u003e\n \u003c/tr\u003e\n\u003c/table\u003e\n\n**Conclusion**\n\nYou always need a wrapped version of fetch for debugging and response handling, you can just use yfetch, or do it your own. If you do not use yfetch, we still encourage you to use ES6 coding style to make your fetch more beautiful. In simple words, yfetch is:\n\n```javascript\nconst yfetch = (opts = {}) =\u003e {\n    const fetchArgs = transformFetchOptions(opts);\n    return fetch(...fetchArgs)\n    .then(transformForContext(fetchArgs))\n    .then(transformFetchResult)\n    .catch(transformFetchError(fetchArgs));\n}\n```\n\nYou can build your own `transformFetchOptions`, `transformForContext`, `transformFetchResult` and `transformFetchError` , or just enjoy yfetch and reuse the exported yfetch transform functions. Review [yfetch source code](src.js) , then make your decision.\n\nTransform functions\n-------------------\n**transformFetchOptions(opts)**\n\nDeal with `opts.base`, `opts.url` and `opts.query`.\n\n```javascript\nimport { transformFetchOptions } from 'yfetch';\n\ntransformFetchOptions({}));     // ['', {}]\ntransformFetchOptions({ base: 'pre_' }));   // ['pre_', {}]\ntransformFetchOptions({ url: 'test' }));    // ['test', {}]\ntransformFetchOptions({ base: 'pre_', url: 'test' }));      // ['pre_test', {}]\ntransformFetchOptions({ url: 'test', query: { foo: 'bar' } }));      // ['test?foo=bar', {}]\n\ntransformFetchOptions({ url: 'ya', json: true }));\n// ['ya', {\n//   json: true,\n//   headers: {\n//     Accept: 'application/json',\n//     'Content-Type': 'application/json' } }]\n```\n\n**transformFetchResult(response)**\n\nThe response must contains .fetchArgs property. Deal with `opts.json` and `opts.error`.\n\n```javascript\nimport { transformFetchResult } from 'yfetch';\n\ntransformFetchResult({body, fetchArgs: [url, {json: true}}]);       // will JSON.parse(body)\ntransformFetchResult({code: 404, fetchArgs: [url, {error: [404, 500]}}]);   // will throw\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzordius%2Fyfetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzordius%2Fyfetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzordius%2Fyfetch/lists"}