{"id":15369113,"url":"https://github.com/developit/sleeper","last_synced_at":"2025-04-15T13:43:25.864Z","repository":{"id":29122498,"uuid":"32652244","full_name":"developit/sleeper","owner":"developit","description":":sleeping: REST abstraction so easy you could use it with your eyes closed. :zzz:","archived":false,"fork":false,"pushed_at":"2019-11-17T22:15:16.000Z","size":129,"stargazers_count":31,"open_issues_count":2,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T11:16:12.143Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://npm.im/sleeper","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developit.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":"2015-03-21T22:17:34.000Z","updated_at":"2024-07-20T15:24:25.000Z","dependencies_parsed_at":"2022-07-25T18:03:02.679Z","dependency_job_id":null,"html_url":"https://github.com/developit/sleeper","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fsleeper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fsleeper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fsleeper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fsleeper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/sleeper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986315,"owners_count":21194025,"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":[],"created_at":"2024-10-01T13:34:14.396Z","updated_at":"2025-04-15T13:43:25.846Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"sleeper [![Version](https://img.shields.io/npm/v/sleeper.svg?style=flat)](https://www.npmjs.org/package/sleeper) ⎔ [![Build Status](https://img.shields.io/travis/developit/sleeper.svg?style=flat\u0026branch=master)](https://travis-ci.org/developit/sleeper)\n=======\n\nA high-level network abstraction that makes working with REST APIs simple.\n\nThis library is very old and you should probably use [ky](https://github.com/sindresorhus/ky) instead, which has a similar API but is based on Fetch rather than XMLHttpRequest.\n\n---\n\n\nInstantiation\n-------------\n\nSleeper is available in ES Modules, CommonJS and UMD formats.\n\n**Using Modules:**  \n\n```js\nimport sleeper from 'sleeper';\n\nconst users = sleeper('/api/users');\n\n// users is a sleeper.Resource instance:\nalert(users instanceof sleeper.Resource);\n```\n\n**Without AMD:**  \n\n```html\n\u003cscript src=\"https://unpkg.com/sleeper/dist/sleeper.umd.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n\tvar users = sleeper('/api/users');\n\n\t// users is a sleeper.Resource instance:\n\talert(users instanceof sleeper.Resource);\n\u003c/script\u003e\n```\n\n\n---\n\nAPI\n---\n\n\n# sleeper(url) / new sleeper.Resource(url)\nCreate a new `sleeper.Resource` instance for the resource at a given URL.  \n\n```js\nconst users = sleeper('/api/users');\n\n// equivalent to:\nconst users = new sleeper.Resource('/api/users');\n```\n\n\n# .index(callback)\nGet a list of resources\n\n```js\nusers.index(list =\u003e {\n\t// list is an Array of users\n\tconsole.log('Users: ', list);\n});\n```\n\n\n# .get(id, callback)\nGet a single resource\n\n```js\nusers.get('myid', user =\u003e {\n\tconsole.log('User \"myid\": ', user);\n});\n```\n\n\n# .post(data, callback)\nCreate a new resource.\n\n```js\nusers.post({\n\tusername : 'joe',\n\tpassword : 'super secret password'\n}, user =\u003e {\n\tconsole.log('New user: ', user);\n});\n```\n\n\n# .put(id, data, callback)\nUpdate/replace an existing resource, indicated by its ID.\n\n```js\nusers.put('myid', {\n\tusername : 'joe',\n\tpassword : 'super secret password',\n\tstatus : 'awesome'\n}, user =\u003e {\n\tconsole.log('Updated user: ', user);\n});\n```\n\n\n# .patch(id, data, callback)\nUpdate/patch an existing resource, indicated by its ID.\n\n```js\nusers.patch('myid', {\n\tstatus : 'awesome'\n}, user =\u003e {\n\tconsole.log('Updated user: ', user);\n});\n```\n\n\n# .delete(id, callback)\nUpdate an existing resource, indicated by its ID.  \n*If you care about old browsers, a `del()` alias is provided.*\n\n```js\nusers.delete('myid', res =\u003e {\n\tconsole.log('Delete response: ', res);\n});\n```\n\n\n# .param(key [, value])\nGet or set a querystring parameter to send on each request.  \n\n- If `value` is set: adds a global querystring parameter `key` with a value of `value`  \n- If `value` is empty: returns the current value of the global parameter `key`  \n- If `key` is an Object: adds `key`'s key-value property pairs as global parameters  \n\n```js\n// Send a token on all subsequent requests:\nusers.param('token', 'abcdefg');\n\n// Get the current token value:\nconst token = users.param('token');\nconsole.log(token);\n```\n\n\n---\n\n\nHooks\n-----\n\nModify functionality by changing these values.\n\n\n# .serializeBody(body, req)\nWhen a request has a body, this function is responsible for serializing it.\n\nThe default implementation of `serializeBody()` provides an example usage:\n\n```js\nserializeBody(body, req) {\n\tif (!req.headers['content-type']) {\n\t\treq.headers['content-type'] = 'application/json';\n\t}\n\treturn JSON.stringify(body);\n}\n```\n\n\n# .idKey\nThis key path is used to obtain an Object identifier if one is not explicitly passed to `put()` / `patch()`.\n\n\u003e Defaults to `\"id\"`.\n\n\n---\n\n\nEvents\n------\n\n\n# req\n\n`(req) =\u003e {}`\n\nHook the `req` event to be notified prior to all requests.\n\nEvent handlers get passed a `Request` instance.\n\n```js\nusers.on('req', req =\u003e {\n\tconsole.log('Request: ', req.method, req.url, req.body);\n});\n```\n\n\n# req:/url\n\n`(req) =\u003e {}`\n\nAdd an event handler for \"req:\" followed by relative URL (ex: `req:/users`) to be notified when a request is made to the given URL.\n\nThis is just a more specific version of the `req` event.\n\n```js\nusers.on('req:/users', req =\u003e {\n\tconsole.log('User list request: ', req.method);\n});\n```\n\n\n# status\n\n`(req, res) =\u003e {}`\n\nHook the `status` event to be notified of the status of every response.\n\nEvent handlers get passed `Request` and `Response`.\n\n```js\nusers.on('status', (req, res) =\u003e {\n\tconsole.log('Status: ', res.status);\n});\n```\n\n\n# status:N\n\n`(req, res) =\u003e {}`\n\nAdd an event handler for \"status:\" followed by a specific response status code (ex: `status:401`) to be notified when a response is issued with that status.\n\nThis is just a more specific version of the `status` event.\n\n```js\nusers.on('status:401', (req, res) =\u003e {\n\tconsole.log('Unauthorized: ', res.response);\n});\n```\n\n\n# res\n\n`(req, res) =\u003e {}`\n\nHook the `res` event to be notified of all responses.\n\nEvent handlers get passed a `Request` and `Response`.\n\n```js\nusers.on('res', (req, res) =\u003e {\n\tconsole.log('Response: ', req.url, res.headers, res.json);\n});\n```\n\n\n# res:/url\n\n`(req, res) =\u003e {}`\n\nAdd an event handler for \"res:\" followed by relative URL (ex: `res:/users`) to be notified when a response is received from the given URL.\n\nThis is just a more specific version of the `res` event.  \n\n```js\nusers.on('res:/users', (req, res) =\u003e {\n\tconsole.log('User list response: ', res.headers, res.json);\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fsleeper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Fsleeper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fsleeper/lists"}