{"id":21189257,"url":"https://github.com/uptick/js-tinyapi","last_synced_at":"2025-03-14T20:42:25.447Z","repository":{"id":46198483,"uuid":"76771017","full_name":"uptick/js-tinyapi","owner":"uptick","description":"A simple and lightweight API helper.","archived":false,"fork":false,"pushed_at":"2021-11-08T01:01:28.000Z","size":221,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-02-21T15:04:25.238Z","etag":null,"topics":[],"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/uptick.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":"2016-12-18T08:15:39.000Z","updated_at":"2021-11-08T01:01:45.000Z","dependencies_parsed_at":"2022-09-26T18:31:34.925Z","dependency_job_id":null,"html_url":"https://github.com/uptick/js-tinyapi","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fjs-tinyapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fjs-tinyapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fjs-tinyapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fjs-tinyapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uptick","download_url":"https://codeload.github.com/uptick/js-tinyapi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243646524,"owners_count":20324582,"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-11-20T18:51:11.758Z","updated_at":"2025-03-14T20:42:25.417Z","avatar_url":"https://github.com/uptick.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# js-tinyapi\n\n[![npm version](https://badge.fury.io/js/js-tinyapi.svg)](http://badge.fury.io/js/js-tinyapi)\n![Downloads](http://img.shields.io/npm/dm/js-tinyapi.svg?style=flat)\n\n## Installing\n\n```sh\nyarn add js-tinyapi\n```\n\n\n## Setup\n\nCreate a custom api object containing all the endpoints you create\n\n```javascript\nimport API from 'js-tinyapi'\nconst api = new API()\n```\n\n\n## Example\n\nPerform a `GET` request to an endpoint\n\n```javascript\n// make endpoint\napi.makeEndpoint('people', '/api/people', 'GET')\n\n// call the endpoint\napi.people()\n  .then(data =\u003e {\n    console.log(data)\n  })\n  .catch(error =\u003e {\n    console.log(error)\n  })\n```\n\nPerform a `POST` request an endpoint\n\n```javascript\n// make endpoint\napi.makeEndpoint('people', '/api/people', 'POST')\n\n// call the endpoint passing in the post payload\napi.people({\n  payload: {name: 'Mary'}\n})\n  .then(data =\u003e {\n    console.log(data)\n  })\n  .catch(error =\u003e {\n    console.log(error)\n  })\n```\n\nPerform a custom request\n\n```javascript\n  options = {\n    method: 'GET',\n    path: '/api/people',\n    params: {},\n    type: 'json',\n    payload: undefined,\n    contentType: undefined,\n    include: []\n  }\n\n  // call the endpoint with options\n  api.request(null, options)\n    .then(data =\u003e {\n      console.log(data)\n    })\n    .catch(error =\u003e {\n      console.log(error)\n    })\n```\n\n\n## Create CRUD endpoints\n\n```javascript\napi.makeCrudEndpoints('people', '/api/')\n\napi.peopleList() // GET /api/people\napi.peopleCreate(payload) // POST /api/people with payload\napi.peopleDetail(123) // GET /api/people?id=123\napi.peopleUpdate(123, payload) // PATCH /api/people?id=123 with payload\napi.peopleRemove(123) // DELETE /api/people?id=123\napi.peopleOptions() // OPTIONS /api/people\n```\n\n\n## Merge in new endpoints\n\nMerge in `POST` and/or `GET` endpoints\n\n```javascript\napi.merge({\n  api: {\n    people: {\n      GET: {\n        name: 'peopleGet'\n      },\n      POST: {\n        name: 'peoplePost'\n      }\n    }\n  }\n})\n\napi.peopleGet() // GET /api/people\napi.peoplePost(payload) // POST /api/people with payload\n```\n\nMerge in a `CRUD` endpoint. The result of this merge is equivalent to the above [Create CRUD endpoints](#Create-CRUD-endpoints) example.\n\n```javascript\napi.merge({\n  api: {\n    people: {\n      CRUD: {\n        name: 'people'\n      }\n    }\n  }\n})\n// OR\napi.merge({\n  api: {\n    people: 'CRUD'\n  }\n})\n\n// equivalent to\napi.makeCrudEndpoints('people', '/api/')\n```\n\n\n## Middleware\n\nA middleware layer is provided in order to easily alter the characteristics\nof requests made through `js-tinyapi`. Three kinds of middleware may be\ncreated:\n\n 1. Request altering middleware.\n\n 2. Response altering middleware.\n\n 3. Fetch middleware.\n\nThe first, request altering middleware, is able to modify a request prior to\nbeing fetched. The second, response altering middleware, is able to modify\na response after having been returned. The last, fetch middleware, is able\nto alter how each request is sent to a server.\n\nTo create a basic middleware for modifying a request, inherit from the provided\nmiddleware baseclass and override the `process` method:\n\n```javascript\nimport Middleware from './middleware'\n\n// Add an extra slash to all request URLs.\nclass AddSlash extends Middleware {\n  process = request =\u003e {\n    return {\n      ...request,\n      url: request.url + '/'\n    }\n  }\n}\n```\n\nThe above middleware returns a new request with an extra slash added to the URL.\n\nTo create a middleware that performs a fetch, simply return a promise that will\nbe resolved once the fetch has completed:\n\n```javascript\nimport Middleware from './middleware'\n\n// Add an extra slash to all request URLs.\nclass DelayedFetch extends Middleware {\n  process = request =\u003e {\n    return new Promise( (resolve, reject) =\u003e {\n      setTimeout( () =\u003e {\n        this.submit( request )\n            .then( r =\u003e resolve( r ) )\n      }, 500 )\n    })\n  }\n}\n```\n\nThe above will add a 500ms delay to all requests. Notice the use of `this.submit`;\nthis is a helper method to submit the supplied request object.\n\nAs an example, a batching middleware is provided. It can be enabled as such:\n\n```javascript\nimport API, { Batch } from 'js-tinyapi'\nconst api = new API()\n// prepare API as usual\napi.pushMiddleware(\n  new Batch({\n    batchUrl: 'http://your.domain/api/batch/',\n    timeout: 50\n  })\n)\n```\n\nThis causes each incoming request to be \"held\" for up to 50 milliseconds,\nwaiting for further requests to be made. Once the timeout has expired, all\ncollected requests are sent to the batch endpoint simultaneously.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptick%2Fjs-tinyapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuptick%2Fjs-tinyapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptick%2Fjs-tinyapi/lists"}