{"id":19936377,"url":"https://github.com/jedirandy/semantic-fetch","last_synced_at":"2025-07-02T10:32:24.615Z","repository":{"id":57356566,"uuid":"61400403","full_name":"jedirandy/semantic-fetch","owner":"jedirandy","description":":tophat: A HTTP library with enhanced semantics based on Fetch","archived":false,"fork":false,"pushed_at":"2016-07-11T22:20:48.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-03T07:13:59.357Z","etag":null,"topics":["es6","fetch","http-client","javascript"],"latest_commit_sha":null,"homepage":"","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/jedirandy.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":"2016-06-17T20:27:11.000Z","updated_at":"2016-06-18T09:32:27.000Z","dependencies_parsed_at":"2022-09-05T04:21:31.552Z","dependency_job_id":null,"html_url":"https://github.com/jedirandy/semantic-fetch","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/jedirandy/semantic-fetch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedirandy%2Fsemantic-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedirandy%2Fsemantic-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedirandy%2Fsemantic-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedirandy%2Fsemantic-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jedirandy","download_url":"https://codeload.github.com/jedirandy/semantic-fetch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedirandy%2Fsemantic-fetch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263120930,"owners_count":23416901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["es6","fetch","http-client","javascript"],"created_at":"2024-11-12T23:25:47.082Z","updated_at":"2025-07-02T10:32:24.584Z","avatar_url":"https://github.com/jedirandy.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Semantic Fetch\n:tophat: A HTTP library with redefined semantics based on Fetch\n\n[![Build Status](https://travis-ci.org/jedirandy/semantic-fetch.svg?branch=master)](https://travis-ci.org/jedirandy/semantic-fetch)\n[![dependencies](https://david-dm.org/jedirandy/semantic-fetch.svg)](https://david-dm.org/jedirandy/semantic-fetch)\n[![npm module](https://badge.fury.io/js/semantic-fetch.svg)](https://www.npmjs.org/package/semantic-fetch)\n[![codecov.io](https://codecov.io/github/jedirandy/semantic-fetch/coverage.svg?branch=master)](https://codecov.io/github/jedirandy/semantic-fetch?branch=master)\n\n\nThe Fetch API is awesome, but it might need some tweaks when it comes to error handling and body resolving, this library provides an alternative fetcher with the following semantics:\n\n1. success means a response with status \u003c 400\n2. failure results from a response with status \u003e= 400, or a network error (status defined as 0)\n3. response comes with a body already resolved\n\n## Install\n```\nnpm install semantic-fetch\n```\n\nIt supports ES6 modules, AMD, CommonJS or a global variable as SemanticFetch\n## Example\n```javascript\n// use fetch polyfill of your choice\n// import 'isomorphic-fetch'\nimport 'whatwg-fetch'\nimport { createFetch } from 'semantic-fetch'\nconst fetcher = createFetch(fetch)\n\nfetcher('/api', { method: 'GET' })\n    .then(res =\u003e {\n        // response body is available\n        console.log(`${res.status} ${res.body}`)\n    })\n    .catch(res =\u003e {\n        if (res.status === 0)\n            console.log('network error')\n        else\n            console.log(`failed due to ${res.body}`)\n    })\n```\n\n## API\n\n### `createFetch(fetch, bodyResolvers)`\n\nCreates a fetch function\n#### Arguments\nthis fetch creator takes 2 arguments:\n\n * `fetch` (function)\n\n   the Fetch function, you can inject a fetch implementation of your choice\n\n * `bodyResolvers` array of (function: (Response, Resolve callback, Next callback) =\u003e Unit) [Optional]\n\n   an array of functions that take the response and two callbacks as arguments, then either call resolve callback to resolve the body content, e.g. ```resolve(res.json())```, or pass to the next resolver by calling ```next()```\n\n   defaultBodyResolvers is used if the arg is not provided, which can be imported as ```{ defaultBodyResolver }```, providing text and json resolvers\n\n#### Returns\nthe enhanced fetch function that takes the same arguments as `fetch`\n\n## License\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedirandy%2Fsemantic-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjedirandy%2Fsemantic-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedirandy%2Fsemantic-fetch/lists"}