{"id":16995076,"url":"https://github.com/jalik/js-rest-client","last_synced_at":"2025-10-27T23:16:44.558Z","repository":{"id":39257504,"uuid":"257777777","full_name":"jalik/js-rest-client","owner":"jalik","description":"Simple and efficient REST Client.","archived":false,"fork":false,"pushed_at":"2024-02-26T20:02:28.000Z","size":1947,"stargazers_count":0,"open_issues_count":12,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-29T06:22:10.099Z","etag":null,"topics":["api","client","rest","sdk"],"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/jalik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2020-04-22T03:09:22.000Z","updated_at":"2024-02-26T20:02:32.000Z","dependencies_parsed_at":"2024-10-14T03:47:32.138Z","dependency_job_id":"cc060836-ad15-45dd-a068-c39f4f083187","html_url":"https://github.com/jalik/js-rest-client","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":"0.040000000000000036","last_synced_commit":"a2082bd289b4758579a99d35c8d57c041e9aded8"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-rest-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jalik","download_url":"https://codeload.github.com/jalik/js-rest-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247385339,"owners_count":20930590,"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","client","rest","sdk"],"created_at":"2024-10-14T03:47:30.334Z","updated_at":"2025-10-27T23:16:39.523Z","avatar_url":"https://github.com/jalik.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jalik/rest-client\n\n![GitHub package.json version](https://img.shields.io/github/package-json/v/jalik/js-rest-client.svg)\n[![Build Status](https://travis-ci.com/jalik/js-rest-client.svg?branch=master)](https://travis-ci.com/jalik/js-rest-client)\n![GitHub](https://img.shields.io/github/license/jalik/js-rest-client.svg)\n![GitHub last commit](https://img.shields.io/github/last-commit/jalik/js-rest-client.svg)\n[![GitHub issues](https://img.shields.io/github/issues/jalik/js-rest-client.svg)](https://github.com/jalik/js-rest-client/issues)\n![npm](https://img.shields.io/npm/dt/@jalik/rest-client.svg)\n\n**WARNING: DEPRECATED PACKAGE**  \nI've made a mistake while naming this package, so I decided to create a new one with a different name and lightly different goal, using TypeScript.  \nHave a look at [@jalik/fetch-client](https://github.com/jalik/js-fetch-client) to use an HTTP client based on fetch which runs in the Browser and NodeJS, with global config, shortcut methods, transformations capabilities and more.\n\n## Why using this ?\n\nWe often repeat our self when creating a REST client, however there are some little things that could be avoided on each request, like concatenating base URL with path, passing credentials, serializing data...\n\nWith this lib, you can build a REST client quickly and almost effortlessly.\nIt can also be used to create an REST SDK, so your users would not have to craft the requests entirely.\n\n## Creating a REST client\n\nLet's see how to create a REST client.\n\n```js\nimport RestClient from '@jalik/rest-client';\n\nconst client = new RestClient({\n  // The base URL of all requests.\n  baseUrl: 'https://rest.coinapi.io/v1',\n});\n```\n\n## Sending requests\n\n### Fetch\n\nFetch is the standard and simplest way to execute HTTP requests in web browsers using `Promise`.\nThis lib is using `fetch()` under the hood (via the `whatwg-fetch` polyfill package), so if you know how to use `fetch()`, you already know how to craft requests with this REST client.\n\nSee how to use fetch on https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch.\n\n```js\nconst body = { name: 'Karl' };\n\nclient.fetch('/users', { \n  method: 'POST',\n  body: JSON.stringify(body),\n  headers: {\n    'content-type': 'application/json'\n  }\n})\n  .then((resp) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\nThis method is generic, so to make code more concise and readable, you can use any of the next shortcut methods, which correspond to an HTTP verb.\n\n**Note about JSON serialization**\n\nThe `fetch()` method which is called by all the methods below (delete, get, patch...) will automatically serialize the request body, if `body` is an object and the header `content-type` is `application/json`.\n\n### DELETE\n\n```js\nclient.delete('/users/1')\n  .then((resp) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\n### GET\n\n```js\nclient.get('/users', {\n  headers: { accept: 'application/json' }\n})\n  .then((resp) =\u003e resp.json())\n  .then((json) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\n### HEAD\n\n```js\nclient.head('/users', {\n  headers: { accept: 'application/json' }\n})\n  .then((resp) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\n### OPTIONS\n\n```js\nclient.options('/users', {\n  headers: { accept: 'application/json' }\n})\n  .then((resp) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\n### PATCH\n\n```js\nconst body = { name: 'Karl' };\n\nclient.patch('/users/1', body, {\n  headers: { 'content-type': 'application/json' }\n})\n  .then((resp) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\n### POST\n\n```js\nconst body = { name: 'Karl' };\n\nclient.post('/users', body, {\n  headers: { 'content-type': 'application/json' }\n})\n  .then((resp) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\n### PUT\n\n```js\nconst body = { name: 'Karl' };\n\nclient.put('/users', body, {\n  headers: { 'content-type': 'application/json' }\n})\n  .then((resp) =\u003e {\n    // handle response\n  })\n  .catch((error) =\u003e {\n    // handle error\n  });\n```\n\n## Managing headers\n\nHeaders can be set when creating the `RestClient` instance.\nFor example the code below will send the `accept` header with all requests (while being overridable on each request).\n\n```js\nconst client = new RestClient({\n  baseUrl: 'https://rest.coinapi.io/v1',\n  headers: {\n    accept: 'application/json',\n  }\n});\n```\n\nHeaders can be get or set at anytime and anywhere in your code.\n\n```js\nclient.setHeader('accept', '*');\nclient.getHeader('accept');\n```\n\nNote that all header names are lower cased automatically in `getHeader()` and `setHeader()` to make it more bug proof, so the following code is referring to the same header.\n\n```js\n// Define 'accept' header equivalents\nclient.setHeader('Accept', '*');\nclient.setHeader('ACCEPT', '*');\nclient.setHeader('accept', '*');\n\n// Return 'accept' header equivalents\nclient.getHeader('Accept');\nclient.getHeader('ACCEPT');\nclient.getHeader('accept');\n```\n\nIf you need to override or remove specific headers for a request it's still possible by passing these headers in the request options. The next example works with all methods (delete, patch, post...).\n\n```js\n// This will replace the `accept` header only for this request.\nclient.get('/users', {\n  headers: { accept: 'application/xml' }\n});\n\n// This will not send current credentials for this request.\nclient.post('/auth', { user: 'jalik', pass: 'yun8amginIr#' }, {\n  headers: { authorization: '' }\n});\n```\n\nFinally, if you need to loop through all defined headers.\n\n```js\nclient.getHeaderNames().forEach((name) =\u003e {\n  const value = client.getHeader(name);\n  // do something with value...\n});\n```\n\n## Authentication\n\nGenerally, authenticating against an API is done by providing a header, which could be the classic `authorization` header or a custom access token header. Below is a list of examples showing some of them.\n\n```js\n// Basic authentication\nclient.setHeader('authorization', 'Basic dGVzdDoxMjPCow==');\n\n// Bearer authentication\nclient.setHeader('authorization', 'Bearer mF_9.B5f-4.1JqM');\n\n// Custom access token (api key)\nclient.setHeader('x-api-key', 'Jec5omtyacuzUcCanQuibPyFrajecEif');\n```\n\nNote that there is no mechanism to support token as query parameter because it is a bad practice and not secure (https://tools.ietf.org/html/rfc6750#section-2.3). However, this can still be achieved by passing the access token on each request manually.\n\n## Changelog\n\nHistory of releases is in the [changelog](./CHANGELOG.md).\n\n## License\n\nThe code is released under the [MIT License](http://www.opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalik%2Fjs-rest-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjalik%2Fjs-rest-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalik%2Fjs-rest-client/lists"}