{"id":16485165,"url":"https://github.com/single9/api-wrapper","last_synced_at":"2025-03-23T12:32:46.330Z","repository":{"id":57160436,"uuid":"419652333","full_name":"single9/api-wrapper","owner":"single9","description":"Call your RESTful APIs like a function.","archived":false,"fork":false,"pushed_at":"2024-10-15T02:10:09.000Z","size":305,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-16T09:06:25.259Z","etag":null,"topics":["api","http","npm","npm-package","restful-api","wrapper"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@single9/api-wrapper","language":"TypeScript","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/single9.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-21T09:02:56.000Z","updated_at":"2024-10-15T02:10:13.000Z","dependencies_parsed_at":"2024-04-11T07:40:32.784Z","dependency_job_id":"7cdbb37b-c90c-4c1d-8ceb-3c1cc1716f54","html_url":"https://github.com/single9/api-wrapper","commit_stats":{"total_commits":39,"total_committers":3,"mean_commits":13.0,"dds":"0.20512820512820518","last_synced_commit":"7ac07187ff6af4f37ee80022037a5cfb0e4e5ee1"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/single9%2Fapi-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/single9%2Fapi-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/single9%2Fapi-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/single9%2Fapi-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/single9","download_url":"https://codeload.github.com/single9/api-wrapper/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244787014,"owners_count":20510041,"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","http","npm","npm-package","restful-api","wrapper"],"created_at":"2024-10-11T13:24:23.870Z","updated_at":"2025-03-23T12:32:45.875Z","avatar_url":"https://github.com/single9.png","language":"TypeScript","readme":"API Wrapper\n===========\n\n[![codecov](https://codecov.io/gh/single9/api-wrapper/branch/main/graph/badge.svg?token=SXD7LZW7MU)](https://codecov.io/gh/single9/api-wrapper)\n\nDefine and Call your restful APIs like a function.\n\nThis package is based on [`@single9/api-tester`](https://www.npmjs.com/package/@single9/api-tester) but use [Axios](https://www.npmjs.com/package/axios) instead of [Request](https://www.npmjs.com/package/request).\n\nInstallation\n============\n\n    npm i @single9/api-wrapper\n\nUsage\n=====\n\n```js\nconst ApiWrapper = require('@single9/api-wrapper');\n```\n\nCreate\n------\n\n```js\nconst api = new ApiWrapper([\n  {\n    name: '\u003cApi Name\u003e',       // only allow certain words and digits\n    path: '\u003cApi Path\u003e',       // e.g. /api/posts\n    method: '\u003cHTTP Method\u003e',  // e.g. post or POST\n  },\n], {\n  configureAxios(axios){\n    // The axios you can add interceptors or global functions.\n  },\n  baseUrl: '\u003cBase URL of API\u003e',   // e.g. https://jsonplaceholder.typicode.com\n                                  // Default: http://localhost:3000\n  headers: {\n    // The headers you want to send. e.g. 'authorization': 'Bearer SAdoweasd...',\n  },\n  auth: { // authorization\n    username: 'username',\n    password: 'password',\n  }\n})\n```\n\n### Factory `baseUrl`\n\nYou can use factory function to dynamically set the base URL. This is useful if your host domain is\na SRV record.\n\n**Example**\n\n```js\nconst api = new ApiWrapper([\n  {\n    name: '\u003cApi Name\u003e',       // only allow certain words and digits\n    path: '\u003cApi Path\u003e',       // e.g. /api/posts\n    method: '\u003cHTTP Method\u003e',  // e.g. post or POST\n  },\n], {\n  baseUrl: async () =\u003e resolveSRV(process.env.API_HOST),\n});\n```\n\nUse\n---\n\n    api.\u003capi_name\u003e(params)\n\n- **api**: Your `ApiWrapper` instance.\n- **api_name**: The name of the API that you set before.\n- **params**: Ｃompatible with [axios request config](https://axios-http.com/docs/req_config)\n  - queryString\n  - pathParams\n\n### Params\n#### params.queryString\n\nUsed for query string. e.g. /users?limit=100\n\n```js\napi.test({\n  queryString: {\n    key: value\n  }\n})\n```\n\n```js\napi.test({\n  queryString: [\n    {\n      name: string,\n      value: string | number,\n    }\n  ]\n})\n```\n\n#### params.pathParams\n\nUsed for path parameters. e.g. /user/:id\n\n```js\napi.test({\n  pathParams: {\n    key: value\n  }\n})\n```\n\n```js\napi.test({\n  pathParams: [\n    {\n      name: string,\n      value: string | number,\n    }\n  ]\n})\n```\n\nExample\n-------\n\n```js\nconst ApiWrapper = require('@single9/api-wrapper');\n\n// Create your API schema\nconst schema = [\n  {\n    name: 'newPost',  // this is your api function name\n    path: '/posts',\n    method: 'post',\n  },\n  {\n    name: 'getTodo',\n    path: '/todos/:todoId',  // path parameter\n    method: 'get',\n  },\n];\n\nconst api = new ApiWrapper(schema, {\n  configureAxios(item){\n    item.interceptors.request.use(\n      (request) =\u003e { console.log('url: %s , req: %o', request.url); return request; },\n    )\n    item.interceptors.response.use(\n      (response) =\u003e { console.log('url: %s , res: %o', response.url, response.data); return response; },\n    )\n  },\n  baseUrl: 'https://jsonplaceholder.typicode.com',\n});\n\nasync function start() {\n  try {\n    const post = await api.newPost({\n      // Post Body\n      data: {\n        title: 'foo!!!!!!',\n        body: 'bar!!',\n        userId: 1\n      },\n    });\n\n    console.log(post.data);\n\n    const get = await api.getTodo({\n      pathParams: {\n        todoId: 2, // replace `:todoId` with value 2.\n      },\n    });\n\n    console.log(get.data);\n  } catch (err) {\n    console.error(err);\n  }\n}\n\nstart();\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingle9%2Fapi-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsingle9%2Fapi-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingle9%2Fapi-wrapper/lists"}