{"id":15065757,"url":"https://github.com/auru/unity-api-mw","last_synced_at":"2026-02-10T02:05:57.528Z","repository":{"id":57386496,"uuid":"72122449","full_name":"auru/unity-api-mw","owner":"auru","description":"Middleware for unity-api","archived":false,"fork":false,"pushed_at":"2020-06-01T01:19:14.000Z","size":3859,"stargazers_count":0,"open_issues_count":17,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-18T17:40:49.523Z","etag":null,"topics":["middleware","unity","unity-api","unity-cache"],"latest_commit_sha":null,"homepage":"https://github.com/auru/unity-api","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/auru.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-27T15:33:55.000Z","updated_at":"2020-09-09T02:25:18.000Z","dependencies_parsed_at":"2022-09-27T10:50:28.026Z","dependency_job_id":null,"html_url":"https://github.com/auru/unity-api-mw","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/auru/unity-api-mw","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/auru%2Funity-api-mw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/auru%2Funity-api-mw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/auru%2Funity-api-mw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/auru%2Funity-api-mw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/auru","download_url":"https://codeload.github.com/auru/unity-api-mw/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/auru%2Funity-api-mw/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270612039,"owners_count":24616122,"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","status":"online","status_checked_at":"2025-08-15T02:00:12.559Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["middleware","unity","unity-api","unity-cache"],"created_at":"2024-09-25T00:47:21.806Z","updated_at":"2026-02-10T02:05:57.476Z","avatar_url":"https://github.com/auru.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unity API Middleware\n\n[![Travis-CI](https://api.travis-ci.org/auru/unity-api-mw.svg?branch=master)](https://travis-ci.org/auru/unity-api-mw)\n[![Coverage Status](https://coveralls.io/repos/github/auru/unity-api-mw/badge.svg?branch=master)](https://coveralls.io/github/auru/unity-api-mw?branch=master)\n[![npm version](https://badge.fury.io/js/unity-api-mw.svg)](https://badge.fury.io/js/unity-api-mw)\n[![Scrutinizer](https://scrutinizer-ci.com/g/auru/unity-api-mw/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/auru/unity-api-mw/)\n[![Deps](https://david-dm.org/auru/unity-api-mw/status.svg)](https://david-dm.org/auru/unity-api-mw)\n[![Deps-Dev](https://david-dm.org/auru/unity-api-mw/dev-status.svg)](https://david-dm.org/auru/unity-api-mw)\n[![Dependency Status](https://dependencyci.com/github/auru/unity-api-mw/badge)](https://dependencyci.com/github/auru/unity-api-mw)\n\n\u003e Collection of middleware for [unity-api](https://github.com/auru/unity-api).\n\n# Table of Contents\n  * [Installation](#installation)\n  * [Usage](#usage)\n  * [API](#api)\n    * [retry](#retrysettings)\n    * [response](#responsesettings)\n    * [cache](#cachesettings)\n  * [Contributing](#contributing)\n  * [License](#license)\n\n# Installation\n\n```bash\nnpm i --save unity-api-mw\n```\n# Usage\n\nIf API has been created with [unity-api](https://github.com/auru/unity-api), then its methods can be called like this: `API[resource][method](methodParams, middlewareOptions)`. \n\nEven though the entire `middlewareOptions` object is available to every middleware in chain, it's best to namespace every middleware with its own key in `middlewareOptions`. That's why middleware should initially come in a form of high-order function with plain object `settings` as its argument, so that end-user can override the defaults.\n\n# API \n\n## retry(settings)\n\nIf [`Response.ok`](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok) from [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/fetch) is `false`, retry the request again.\n\n### settings {Object} *Optional*\nMiddleware settings.\n\n#### key {String} *Optional*\n**Default:** `'retry'`\n\nKey in `middlewareOptions` to look up.\n\n#### count {Number} *Optional*\n**Default:** `0`\n\nHow many times middleware should attempt to re-`fetch` if it fails the first time.\n\n\u003e Now you can make API calls, changing the default number of retries on per-call basis:\n\n```js\nAPI.user.get({ id: 1 }, { retry: 2 });\n```\n\n**Example:**\n```js\n// api.js\nimport resources from './api/resources';\nimport createAPI from 'unity-api';\nimport { retry } from 'unity-api-mw'; // or import retry from 'unity-api-mw/lib/retry'\n\nconst middleware = [\n  retry({ key: 'retry', count: 1 }) // retry on every fail once\n]\n\nconst API = createAPI(resources, middleware, 'api');\n\nexport default API;\n```\n\n```js\n// user.js\nimport API from './api';\nAPI.user.get({ id: 1 }, { retry: 2 }); // get user with id 1, retry twice on fail instead of once.\n```\n\n## response(settings)\n\nIf the response option is enabled, you can get the whole [response](https://developer.mozilla.org/en-US/docs/Web/API/Response). \n\n### settings {Object} *Optional*\nMiddleware settings.\n\n#### key {String} *Optional*\n**Default:** `'response'`\n\nKey in `middlewareOptions` to look up.\n\n\u003e Now you can make API calls and get the whole response:\n\n```js\nAPI.user.get({ id: 1 }, { response: true });\n```\n\n**Example:**\n```js\n// api.js\nimport resources from './api/resources';\nimport createAPI from 'unity-api';\nimport { response } from 'unity-api-mw'; // or import retry from 'unity-api-mw/lib/response'\n\nconst middleware = [\n  response({ key: 'response' })\n]\n\nconst API = createAPI(resources, middleware, 'api');\n\nexport default API;\n```\n\n```js\n// user.js\nimport API from './api';\nAPI.user.get({ id: 1 }, { response: true }); // get response.\nAPI.user.get({ id: 1 }, { response: false }); // get response body.\n```\n\n## cache(settings)\n\nCache [unity-api](https://github.com/unity/unity-api) responses with [unity-cache](https://github.com/unity/unity-cache).\n\nThis middleware doesn't support caching on per-call basis, meaning it doesn't use anything from `middlewareOptions` parameter of the API call.\n\n### settings {Object} *Optional*\n\nMiddleware settings.\n\n#### cache {Instance of unity-api} *Optional*\n\n**Default:** `undefined`\n\nAlthough this parameter is *optional*, without an instance of [unity-cache](https://github.com/unity/unity-cache) this middleware is useless.\n\n#### bin {String} *Optional*\n\n**Default:** `'api'`\n\nName of the **cache bin** reserved for caching API calls in [unity-cache](https://github.com/unity/unity-cache) instance.\n\n#### expire {Number} *Optional*\n\n**Default:** `Number.MAX_SAFE_INTEGER` (forever)\n\nThe amount of milliseconds API calls should be cached for by default.\n\n#### resources {Object} *Optional*\n\n**Default:** `{}`\n\nA hash map of [resources](https://github.com/auru/unity-api#resources-object-optional) that should be cached.\n\nEach entry should be indicated by the resource's **key**, the same one used in setting up of [unity-api](https://github.com/unity/unity-api) instance. **Values** are `arrays` of plain `objects` with the following structure:\n\n##### method {String}\n\nName of a API's resource method that should be cached.\n\n##### expire {Number} *Optional*\n\n**Default:** `Number.MAX_SAFE_INTEGER` (forever)\n\nThe amount of milliseconds API call to this method should be cached for.\n\n**Example:**\n```js\n{\n    ['user' /* key */ ]: [\n        { method: 'get', expire: 1000 },\n        { method: 'set', expire: 1000 },\n        { method: 'delete' }\n    ]\n}\n```\n\n**Putting it all together:**\n```js\n// api.js\nimport createAPI from 'unity-api';\nimport createCache from 'unity-cache';\nimport { cache as cacheMw } from 'unity-api-mw'; // or import cache from 'unity-api-mw/lib/cache'\n\nconst resources = {\n  user: {\n    namespace: 'people',\n    methods: {\n      getById: ({ id }) =\u003e ({ path: ['id', id] }),\n      getByName: ({ name }) =\u003e ({ path: ['name', name] }),\n    }\n  }\n}\n\nconst apiCacheBin = 'api';\nconst cache = createCache([apiCacheBin]);\n\nconst middleware = [\n  cacheMw(\n     cache,       // cache instance created with unity-cache\n     apiCacheBin, // name of the cache bin\n     1000,        // cache resources for 1 second by default\n     {\n       user: [\n         { method: 'getById', expire: 1000 * 60 }, // cache all responses from API.user.getById for 1 minute\n         { method: 'getByName' }                   // cache all responses from API.user.getByName for 1 second (default)\n       ]\n     }\n  ) \n]\n\nconst API = createAPI(resources, middleware, 'api');\n\nexport default API;\n```\n\n# Contributing\n\n* Provide [conventional commit messages](https://github.com/conventional-changelog/conventional-changelog-angular/blob/master/convention.md) by using `npm run commit` instead of `git commit`.\n* **Core contributors:** use GitHub's *Rebase and merge* as a default way of merging PRs.\n\n# License\nMIT © AuRu\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fauru%2Funity-api-mw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fauru%2Funity-api-mw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fauru%2Funity-api-mw/lists"}