{"id":22014841,"url":"https://github.com/nobrainr/axios-morphism","last_synced_at":"2025-07-10T11:11:06.412Z","repository":{"id":33268081,"uuid":"157260794","full_name":"nobrainr/axios-morphism","owner":"nobrainr","description":"Axios plugin to transform data requests/responses based on a schema.","archived":false,"fork":false,"pushed_at":"2024-09-11T10:25:48.000Z","size":1996,"stargazers_count":9,"open_issues_count":22,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-25T13:03:45.815Z","etag":null,"topics":["api","axios","client","data","http","interceptor","javascript","network","nodejs","request","response","transform","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/nobrainr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2018-11-12T18:52:59.000Z","updated_at":"2023-04-13T20:47:29.000Z","dependencies_parsed_at":"2024-10-24T22:17:10.544Z","dependency_job_id":"595725c6-4f7f-4b7c-a90a-2a312fb74812","html_url":"https://github.com/nobrainr/axios-morphism","commit_stats":{"total_commits":92,"total_committers":3,"mean_commits":"30.666666666666668","dds":"0.26086956521739135","last_synced_commit":"f54583918dd9246cba324d8902d34651d4ed87e7"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Faxios-morphism","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Faxios-morphism/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Faxios-morphism/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Faxios-morphism/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nobrainr","download_url":"https://codeload.github.com/nobrainr/axios-morphism/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252672442,"owners_count":21786249,"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","axios","client","data","http","interceptor","javascript","network","nodejs","request","response","transform","typescript"],"created_at":"2024-11-30T04:18:21.689Z","updated_at":"2025-05-07T00:30:47.012Z","avatar_url":"https://github.com/nobrainr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# axios-morphism\n\n[![npm](https://img.shields.io/npm/v/axios-morphism.svg?style=for-the-badge)](https://www.npmjs.com/package/axios-morphism) [![npm bundle size (minified)](https://img.shields.io/bundlephobia/min/axios-morphism.svg?style=for-the-badge)](https://bundlephobia.com/result?p=axios-morphism@latest) [![Coveralls github](https://img.shields.io/coveralls/github/nobrainr/axios-morphism.svg?style=for-the-badge)](https://coveralls.io/github/nobrainr/axios-morphism?branch=master) [![CircleCI (all branches)](https://img.shields.io/circleci/project/github/nobrainr/axios-morphism/master.svg?style=for-the-badge)](https://circleci.com/gh/nobrainr/axios-morphism) [![Deps](https://img.shields.io/david/nobrainr/axios-morphism.svg?style=for-the-badge)](https://www.npmjs.com/package/axios-morphism) \n\n\u003e Axios plugin to transform data requests/responses based on a schema.\n\nThis package is built upon [`Morphism`](https://www.npmjs.com/package/morphism). Read the documentation [here](https://github.com/nobrainr/morphism).\n\n---\n\n- [axios-morphism](#axios-morphism)\n  - [Getting started](#getting-started)\n    - [Installation](#installation)\n    - [Example](#example)\n  - [Usage](#usage)\n    - [Schema](#schema)\n    - [Interceptors Configuration](#interceptors-configuration)\n      - [Example:](#example)\n      - [Axios-Morphism Configuration](#axios-morphism-configuration)\n    - [Apply Configurations](#apply-configurations)\n    - [Remove Configurations](#remove-configurations)\n    - [Combine Configurations](#combine-configurations)\n  - [License](#license)\n\n## Getting started\n\n### Installation\n\n```sh\nnpm install --save axios morphism # Axios and Morphism are defined as a peer dependencies in axios-morphism\nnpm install --save axios-morphism\n```\n\n### Example\n\n```typescript\nimport axios from 'axios';\nimport { apply, AxiosMorphismConfiguration } from 'axios-morphism';\n\nconst peopleSchema = {\n  name: 'name',\n  height: 'height',\n  weight: 'mass'\n};\n\nconst configuration: AxiosMorphismConfiguration = {\n  url: 'https://swapi.co/api/',\n  interceptors: {\n    responses: [\n      { matcher: '/people', schema: peopleSchema, dataSelector: 'results' },\n      { matcher: '/people/:id', schema: peopleSchema }\n      ],\n    requests: []\n  }\n};\n\nconst client = axios.create({baseURL: 'https://swapi.co/api/'});\napply(client, configuration);\n\nawait client.get('/people/1');\n▼\n// {\n//   name: \"Luke Skywalker\"\n//   height: \"172\"\n//   weight: \"77\"\n// }\n\nawait client.get('/people');\n▼\n// [\n//  {\n//    name: \"Luke Skywalker\"\n//    height: \"172\"\n//    weight: \"77\"\n//  },....\n// ]\n```\n\n▶ [Try on Repl.it](https://repl.it/@yrnd1/axios-morphism-simple)\n\n## Usage\n\n### Schema\n\nDefine a schema corresponding to the shape you expect to have after the transformation has been applied.\n\n▶ [Read About Morphism's Schema Capabilities](https://github.com/nobrainr/morphism#1-the-schema)\n\n```typescript\nconst peopleSchema = {\n  name: 'name',\n  height: 'height',\n  weight: ({ mass }) =\u003e `${mass} KG`\n};\n```\n\n### Interceptors Configuration\n\nCreate your configurations to be applied on Axios requests or responses.\n\n#### Example:\n\n```typescript\nconst configuration: AxiosMorphismConfiguration = {\n  url: 'https://swapi.co/api/',\n  interceptors: {\n    responses: [\n      { matcher: '/people', schema: peopleSchema, dataSelector: 'results' },\n      { matcher: /\\/people\\/([^\\/]+?)(?:\\/)?$/i, schema: peopleSchema }, // matches /people/:id\n      {\n        matcher: (response: AxiosResponse) =\u003e response.config.method === 'POST', // matches every responses obtained using a POST\n        schema: peopleSchema,\n        dataSelector: 'results'\n      }\n    ],\n    requests: []\n  }\n};\n```\n\n#### Axios-Morphism Configuration\n\n| Property                                                                   | Type                              | Description                                                                         | Example                                                                                                          |\n| -------------------------------------------------------------------------- | --------------------------------- | ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |\n| url                                                                        | `string`                          | Base URL to listen on                                                               | `https://swapi.co/api`                                                                                           |\n| interceptors                                                               | `{ responses: [], requests: []};` | List of Responses and Requests Interceptors Configuration to register against Axios |\n| interceptors.responses[].matcher                                           | `string` `RegExp` `Function`      | Matcher used to detect on which response to apply the transformer                   | - `'people/:id'` \u003cbr\u003e - `/people$/i` \u003cbr\u003e - `(response: AxiosResponse) =\u003e response.config.method === 'POST'`     |\n| interceptors.requests[].matcher                                            | `string` `RegExp` `Function`      | Matcher used to detect on which request to apply the transformer                    | - `'planets/:id'` \u003cbr\u003e - `/planets$/i` \u003cbr\u003e - `(request: AxiosRequestConfig) =\u003e request.url.includes('planets')` |\n| interceptors.requests[].schema interceptors.responses[].schema             | `Schema` `StrictSchema`           | A schema is an object-preserving map from one data structure to another.            | [Morphism Schema Examples](https://github.com/nobrainr/morphism#schema-example)                                  |\n| interceptors.requests[].dataSelector interceptors.responses[].dataSelector | `string`                          | A selector to access the data in the Axios returned data                            | With this Axios Response: `{ data: { results: [] }}`. Pick the data with `{ dataSelector: 'results' }`           |\n\n### Apply Configurations\n\nApply your interceptors on your axios instance and there you go!\n\n```typescript\nimport { apply } from 'axios-morphism';\n\nconst configuration: AxiosMorphismConfiguration = {\n  url: 'https://swapi.co/api/',\n  interceptors: {\n    responses: [\n      { matcher: '/people', schema: peopleSchema, dataSelector: 'results' },\n      { matcher: /\\/people\\/([^\\/]+?)(?:\\/)?$/i, schema: peopleSchema } // Will match /people/:id\n    ],\n    requests: []\n  }\n};\n\nconst client = axios.create({ baseURL: 'https://swapi.co/api/' });\napply(client, configuration);\n\n// Start making requests to see you data transformed\nawait client.get('/people');\nawait client.get('/people/1');\n```\n\n### Remove Configurations\n\nUse the `unsubscribe` method returned from the `apply` function to opt-out from the interceptors\n\n```typescript\nconst configuration: AxiosMorphismConfiguration = {...};\n\nconst axiosMorphism = apply(client, config);\naxiosMorphism.unsubscribe(); // Remove all registered interceptors\n```\n\n### Combine Configurations\n\n`axios-morphism` provides the `combine` function in order to help you merge multiple configurations under a `baseURL`.\n\n```typescript\nimport { apply, combine, AxiosMorphismConfiguration } from 'axios-morphism';\n\nconst peopleMorphism: AxiosMorphismConfiguration = {\n  url: '/people',\n  interceptors: {\n    requests: [],\n    responses: [\n      { matcher: '/', schema: { name: 'name', url: 'url' }, dataSelector: 'results' },\n      { matcher: '/:id', schema: { name: 'name', url: 'url' }, dataSelector: 'results' }\n    ]\n  }\n};\nconst planetMorphism: AxiosMorphismConfiguration = {\n  url: '/planets',\n  interceptors: {\n    requests: [],\n    responses: [\n      { matcher: '/', schema: { name: 'name', url: 'url' }, dataSelector: 'results' },\n      { matcher: '/:id', schema: { name: 'name', url: 'url' }, dataSelector: 'results' }\n    ]\n  }\n};\n\nconst client = axios.create({ baseURL: 'https://swapi.co/api/' });\napply(client, combine('https://swapi.co/api/', peopleMorphism, planetMorphism));\n\n// Start making requests to see you data transformed\nawait client.get('/people');\nawait client.get('/planets/1');\n```\n\n## License\n\nMIT © [Yann Renaudin][twitter-account]\n\n[twitter-account]: https://twitter.com/renaudin_yann\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnobrainr%2Faxios-morphism","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnobrainr%2Faxios-morphism","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnobrainr%2Faxios-morphism/lists"}