{"id":19580066,"url":"https://github.com/surovv/luch","last_synced_at":"2025-04-27T08:32:08.862Z","repository":{"id":57290831,"uuid":"86914341","full_name":"surovv/luch","owner":"surovv","description":"Light and flexible promise-based http client for browser and node.js","archived":false,"fork":false,"pushed_at":"2018-12-24T09:31:01.000Z","size":191,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T19:17:58.053Z","etag":null,"topics":["fetch","fetch-api","fetcher","http-client","isomorphic","isomorphic-fetch","isomorphic-javascript","javascript","js","promise","promise-api","promise-library","promise-wrapper","request","requests","wrapper"],"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/surovv.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-01T13:12:37.000Z","updated_at":"2018-12-24T09:31:03.000Z","dependencies_parsed_at":"2022-09-07T14:53:11.865Z","dependency_job_id":null,"html_url":"https://github.com/surovv/luch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surovv%2Fluch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surovv%2Fluch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surovv%2Fluch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surovv%2Fluch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/surovv","download_url":"https://codeload.github.com/surovv/luch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251112546,"owners_count":21538162,"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":["fetch","fetch-api","fetcher","http-client","isomorphic","isomorphic-fetch","isomorphic-javascript","javascript","js","promise","promise-api","promise-library","promise-wrapper","request","requests","wrapper"],"created_at":"2024-11-11T07:21:02.629Z","updated_at":"2025-04-27T08:32:06.308Z","avatar_url":"https://github.com/surovv.png","language":"JavaScript","readme":"Light and flexible promise-based http client built on top of isomorphic-fetch with usefull extra functionality\n\n# luch\n[![Code Climate](https://codeclimate.com/github/surovv/luch/badges/gpa.svg)](https://codeclimate.com/github/surovv/luch) [![bitHound Overall Score](https://www.bithound.io/github/surovv/luch/badges/score.svg)](https://www.bithound.io/github/surovv/luch)\n\nThe `luch`  is light and flexible modular http client built on top of [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch \"isomorphic-fetch\"), which build on [GitHub's WHATWG Fetch polyfill](https://github.com/github/fetch).\n\nThe `luch` could be used for both environments as for browser and for node.js, it provides possibility to make requests in easy laconic way with flexible configuration. Also `luch` uses isomorphic and standardized base under the hood because it built on top of isomorphic-fetch.\n\n## Warnings\nAccording to origin [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch \"isomorphic-fetch\") docs\n\u003e- You must bring your own ES6 Promise compatible polyfill, I suggest [es6-promise](https://github.com/jakearchibald/es6-promise).\n\n\u003eFor [ease-of-maintenance and backward-compatibility reasons][why polyfill], this library will always be a polyfill. As a \"safe\" alternative, which does not modify the global, consider [fetch-ponyfill][].\n\n\n[why polyfill]: https://github.com/matthew-andrews/isomorphic-fetch/issues/31#issuecomment-149668361\n[fetch-ponyfill]: https://github.com/qubyte/fetch-ponyfill\n\n## Installation\n\n####  NPM\n\n```sh\nnpm install --save luch es6-promise\n```\n\n## luch API\n\n##### luch(url [, options])\n```js\nrequire('es6-promise').polyfill();\nconst {luch} = require('luch');\n// or es6\nimport {luch} from 'luch';\n\nconst someHeaders = new Headers();\n\nconst someOptions = {\n  method: 'GET',\n  headers: someHeaders,\n  mode: 'cors',\n  cache: 'default',\n};\n\nluch('api.web.com', someOptions)\n  .then(response =\u003e response.json())\n  .then(data =\u003e succFn(data))\n  .catch(err =\u003e errFn(err))\n```\n\n### Request method aliases\n\nFor convenience aliases have been provided for all popular request methods.\n\n##### luch.get(url [, params, config])\n##### luch.head(url [, params, config])\n##### luch.options(url [, params, config])\n##### luch.delete(url [, params, config])\n##### luch.post(url [, data, config]])\n##### luch.put(url [, data, config]])\n##### luch.patch(url [, data, config]])\n\n###### NOTE\nBy default **get head options delete** methods stringify *params* to query-string and **post put patch** pack *data* argument to body as json.\n\n```js\nluch.get('http://cool.api.com/resource', {limit: 5, offset: 0})\n  .then(response =\u003e handleResponse(response))\n  .catch(err =\u003e handleErr(err));\n\nluch.post('http://cool.api.com/things', {name: 'luch', howCoolItIs: 'very cool'});\n\n\nconst optns = {\n  headers: new Headers(),\n  cache: 'default',\n};\n\nconst vals = {\n  name: 'dog',\n  howCoolItIs: 'WUF WUF',\n};\n\nluch.put('http://cool.api.com/things', vals, optns);\n```\n\n## luchFor\n\n**luchFor** is method for defining customized luch-like clients with common configuration for multi requests.\n##### luchFor(baseUrl [, baseOptions])\n\n```js\n// base.js\nimport {luchFor} from 'luch';\n\nconst localhostUrl = 'http://localhost:3000';\n\nconst localhostOptions = {\n  headers: new Headers({\n    \"Content-Type\": \"application/json\",\n  }),\n};\n\nexport const localhostLuch = luchFor(localhostUrl, localhostOptions);\n\n// Voila! Now you can use it like original luch, but with predefined configuration\n\nlocalhostLuch('/');\n// =\u003e same as luch(`${localhostUrl}/`, localhostOptions);\n\nconst coolArticle = {\n  title: 'LUCH FOR?? FOR WHAT??!!?',\n  desc: 'WUF WUF',\n};\n\nlocalhostLuch.post('/articles', coolArticle);\n// same as luch.post(`${localhostUrl}/articles`, coolArticle, localhostOptions);\n```\n###### Thats not all!\nYou can call **addToBaseConfig** method on luchFor objects, it especially usefull with modules using.\n##### addToBaseConfig(path [, options])\n```js\n// users.js\nimport {localhostLuch} from './base';\n\nconst usersPath = '/users';\n\nconst usersConfig = {\n  cache: 'default',\n};\n\nconst usersLuch = localhostLuch.addToBaseConfig(usersPath, usersConfig);\n\nusersLuch.post('/', {name: 'Dude'});\n\nexport const updateUser = (id, data) =\u003e userLuch.put(`/${id}`, data);\n```\n###### Note\nYou can pass *options* argument like in regular luch request, but it will be united with *baseOptions* by flat merging, so some *baseOptions* attributes values could be overriden by values from *options*.\n\nYou can read **baseUrl** and **baseOptions** values from luchFor object\n```js\nconsole.log(localhostLuch.baseUrl, localhostLuch.baseOptions);\n```\n## Utils\nThe `luch` lib provides some extra methods\n\n##### getAbsoluteUrl(baseUrl)(path)\n```js\nimport {luch, getAbsoluteUrl} from 'luch';\n\nconst apiUrl = 'http://localhost:3000';\nconst withApiUrl = getAbsoluteUrl(apiUrl);\n\n\nconst user = {...};\nluch.post(withApiUrl('/users'), user);\n// =\u003e same as luch.post('http://localhost:3000/users', user)\n\nluch.get(withApiUrl('/resource'));\n// =\u003e same as luch.get('http://localhost:3000/resource')\n\nluch.get(withApiUrl('/resource2'));\nluch.get(withApiUrl('/resource3'));\n\n// calling without path argument will return baseUrl\nluch.get(withApiUrl());\n// =\u003e luch.get('http://localhost:3000')\n```\n\n##### removeUndefinedAttrs(obj)\n```js\nimport {removeUndefinedAttrs} from 'luch';\n\nconst obj = {\n  a: undefined,\n  b: null,\n  c: false,\n  d: 0,\n  e: '',\n  f: 42,\n};\n\nremoveUndefinedAttrs(obj);\n/* =\u003e {\n  b: null,\n  c: false,\n  d: 0,\n  e: '',\n  f: 42,\n}; */\n\nconsole.log(obj);\n// =\u003e {a: undefined, b: null, ...}\n```\n\n##### appendToFormData(body [, formData])\nAppend or transform object to FormData.\n\n```js\nimport {luch, appendToFormData} from 'luch';\n\nconst data = {/* WUF WUF */};\n\nconst options = {\n  method: 'POST',\n  body: appendToFormData(data),\n};\n\nluch('https://wuf.wuf', options);\n```\n\n##### getJson\nUse it when you need call **json** method on response\n\n```js\nimport {luch, getJson} from 'luch';\n\n// from\nluch(someUrl)\n  .then(response =\u003e response.json())\n  .then(data =\u003e handleData(data);\n\n// to\nluch(someUrl).then(getJson).then(handleData);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurovv%2Fluch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsurovv%2Fluch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurovv%2Fluch/lists"}