{"id":20972030,"url":"https://github.com/fabulator/rest-api-handler","last_synced_at":"2025-05-14T11:34:03.881Z","repository":{"id":28907262,"uuid":"119052014","full_name":"fabulator/rest-api-handler","owner":"fabulator","description":"Handler for REST APIs","archived":false,"fork":false,"pushed_at":"2025-05-12T07:40:56.000Z","size":3524,"stargazers_count":1,"open_issues_count":9,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-12T08:55:25.135Z","etag":null,"topics":["api","fetch","rest","rest-api"],"latest_commit_sha":null,"homepage":"https://doc.esdoc.org/github.com/fabulator/rest-api-handler/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fabulator.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-01-26T13:01:42.000Z","updated_at":"2025-03-11T22:57:20.000Z","dependencies_parsed_at":"2024-10-27T19:43:08.184Z","dependency_job_id":"99f72b17-2ece-4427-b8bd-34329bfee714","html_url":"https://github.com/fabulator/rest-api-handler","commit_stats":{"total_commits":238,"total_committers":6,"mean_commits":"39.666666666666664","dds":0.5252100840336135,"last_synced_commit":"ccf80cfbb199550b377b2f4b190bbd800d0c0fc9"},"previous_names":[],"tags_count":66,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabulator%2Frest-api-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabulator%2Frest-api-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabulator%2Frest-api-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabulator%2Frest-api-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabulator","download_url":"https://codeload.github.com/fabulator/rest-api-handler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253708811,"owners_count":21951058,"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":["api","fetch","rest","rest-api"],"created_at":"2024-11-19T04:06:19.298Z","updated_at":"2025-05-14T11:34:03.869Z","avatar_url":"https://github.com/fabulator.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# REST API Handler\n\n[![npm version](https://badge.fury.io/js/rest-api-handler.svg)](https://badge.fury.io/js/rest-api-handler)\n[![renovate-app](https://img.shields.io/badge/renovate-app-blue.svg)](https://renovateapp.com/) \n[![Known Vulnerabilities](https://snyk.io/test/github/fabulator/rest-api-handler/badge.svg)](https://snyk.io/test/github/fabulator/rest-api-handler)\n[![codecov](https://codecov.io/gh/fabulator/rest-api-handler/branch/master/graph/badge.svg)](https://codecov.io/gh/fabulator/rest-api-handler) \n[![travis](https://travis-ci.org/fabulator/rest-api-handler.svg?branch=master)](https://travis-ci.org/fabulator/rest-api-handler)\n\nThis library will help you with requests to REST APIs. It uses [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) which isn't supported by node and some [older browsers](https://caniuse.com/#feat=fetch) and Node. Remember to include polyfill if need it.\n\n## Basic request\n\nInstall npm library:\n\n```node\nnpm install rest-api-handler --save\n```\n\nFirst initiate Api class and then you can send HTTP requests. As first parameter use base url.\n\n```javascript\nimport { Api } from 'rest-api-handler';\n\nconst api = new Api('https://api.blockcypher.com');\n\napi.get('v1/btc/main').then((response) =\u003e {\n    console.log(response); // same response as in Fetch API\n});\n\n// or you can request full url\n\napi.get('https://api.blockcypher.com/v1/btc/main');\n\n```\n\nIn default configuration, response is same as in Fetch API. That why you can define your own processors that will parse responses or use default one provided by this library.\n\n```javascript\nimport { Api, defaultResponseProcessor, DefaultApiException } from 'rest-api-handler';\n\nconst api = new Api('https://api.blockcypher.com', [\n    new DefaultResponseProcessor(DefaultApiException),\n]);\n\napi.get('v1/btc/main').then((response) =\u003e {\n    console.log(response.code); // 200\n    console.log(response.data); // parsed JSON\n    console.log(response.source); // original Response\n});\n\n```\n\nHere is how to create your own response processors and use them in the chain:\n\n```javascript\nconst api = new Api('//some.api.com', [\n    new DefaultResponseProcessor(DefaultApiException),\n    onlyDataProcessor,\n]);\n\nfunction onlyDataProcessor(response) {\n    return Promise.resolve(response.data);\n}\n\napi.get('endpoint').then((response) =\u003e {\n    console.log(response); // parsed JSON\n});\n\n```\n\n## Methods\n\nThere are default methods for GET, POST, PUT and DELETE. But you can send any HTTP method using request.\n\n```javascript\napi.get('v1/btc/main'); // GET https://api.blockcypher.com/v1/btc/main\napi.get('v1/btc/main', { a: 'b' }); // GET https://api.blockcypher.com/v1/btc/main?a=b\n\napi.post('method', { a: 'b' });\napi.put('method', { a: 'b' });\napi.delete('method');\n\n// you can create your own requests\n// use can use other parameters from Fetch API - https://developer.mozilla.org/en-US/docs/Web/API/Request\napi.request('endpoint', 'PUT', {\n    body: 'Simple string request',\n});\n```\n\n## Data encoding\n\nBy default, data for POST and PUT are encoded as JSON. You can also encode them as FormData. This can be used for images or files uploading.\n\n```javascript\nimport { Api } from 'rest-api-handler';\nconst api = new Api('//some.api.com');\n\napi.post('file-upload', {\n    file: fileObject,\n}, Api.FORMATS.FORM_DATA);\n\n```\n\n## Authentication\n\nYou can authorize to API by using default headers or set them after.\n\n```javascript\nconst api = new Api('//some.api.com', [], {\n    Authorization: 'Bearer XYZ',\n    'Content-Type': 'application/json',\n});\n\n// this will replace original default value\napi.addDefaultHeader('Authorization', 'Bearer ABC');\n\n// this will delete authorization\napi.removeDefaultHeader('Authorization');\n```\n\nYou can also set custom headers for every request:\n\n```javascript\napi.request('endpoint', 'GET', {}, {\n    'Authorization': 'Bearer XYZ',\n})\n```\n\n## Node\n\nTo use it as node library, just import Fetch polyfill:\n\n```javascript\nrequire('cross-fetch/polyfill');\nconst FormData = require('form-data');\n\nglobal.FormData = FormData;\n\nconst { Api, DefaultResponseProcessor, DefaultApiException } = require('rest-api-handler');\n\nconst api = new Api('https://api.blockcypher.com', [ new DefaultResponseProcessor(DefaultApiException), ]);\n\napi.get('v1/btc/main').then((response) =\u003e {\n    console.log(response.data);\n});\n```\n\n## Exception handling\n\nWhen you catch exception from fetch it might be tricky. It can be response from api or some javascript syntax error. You can create exception throwing. Use example or your own methods:\n\n```javascript\nimport { Api, DefaultResponseProcessor, DefaultApiException } from 'rest-api-handler';\n\nconst api = new Api(apiUrl, [\n    new DefaultResponseProcessor(DefaultApiException),\n]);\n\napi\n    .get('some-namespace')\n    .catch((exception) =\u003e {\n        if (exception instanceof DefaultApiException) {\n            console.log('Api throwed some exception. Do something.');\n        };\n        console.log('There was some other exception');\n        throw exception;\n    });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabulator%2Frest-api-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabulator%2Frest-api-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabulator%2Frest-api-handler/lists"}