{"_id":"@acato/axios-utils","_rev":"3-e29ee80695c2274ee6a0a5d627fbb378","name":"@acato/axios-utils","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.1":{"name":"@acato/axios-utils","version":"1.0.1","author":{"name":"Marcel Bos","email":"marcel@acato.nl"},"license":"ICS","_id":"@acato/axios-utils@1.0.1","maintainers":[{"name":"beheeracato","email":"beheer@acato.nl"}],"homepage":"https://bitbucket.org/acato/axios-utils#readme","dist":{"shasum":"fc01ab2486ba2fd55efc99811da1aed1bbc3bbf0","tarball":"https://registry.npmjs.org/@acato/axios-utils/-/axios-utils-1.0.1.tgz","fileCount":3,"integrity":"sha512-eDIRf1rS06jxbGpoPNNLtEc4uHcqwO0dafD6/9j23imspSI1+OgKfLhkW1/mPcJ7RiTjurtSEJM/C1LoNVNOTg==","signatures":[{"sig":"MEUCIQDQ/XvQyajxS0H4MYuMOgGO4BrptXR1hFUPw3DKyxBEdwIgexq2/YpdigbDGDfncEB7oTXPjjVGvF1GYCzgmExDXTM=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":6202,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiWTaGACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmrnxg/+KG5p5Y+hV69DmIeMtjqQpOFBjoLJxOkP0yuod1pzSZxflsc8\r\nBxto/mOhnStuJaXlAPDrj2UZAC0ZshL/nsKUDmYQTN00CjXDm7/hTqrIqTAE\r\np/EA3GkUXYarew9jj6BTMnc6UqLdHRLI8PuRX3R7zqWIKSQ06r9ffB8A7Q+v\r\n2Wp/FCZ5hGsEtHcEGq2mmODuSyh+D3F94n3Ces8SyRUPtnXS1W0vncxIy5Xi\r\nm+GI0/4I4pzQqcgah2n0++q0o/LfelJ26AO2YenCjYqEvsYYfl11hctk+vei\r\nrMVxoJDCQ4XH2JeB14ZDKkofTvg4VukOgXvGnexsqmZ5sBWPD6/7EB0g2Xu5\r\nNnYFU6xb+LYigOzJT5Ukb6TYgxL31WrChn6vPK1/pLbnVWt+h0DnCtWDjI2c\r\nAhEL12vok7cosR5yWx7lhEzUTVdSYD2fOkZPp8YVbqSdNPpB/BgLnb0zyY4a\r\ncxT2ucx4HdqeBUd2PqbRrvMAcFlSw57eeZM/90pCFjjE3bmKAyD520Zz/urZ\r\nPg8G7AEWCOAcRQBYqcpHhYOKPD8YGp3IzCuxmG/VEmT1HvsS1ugzxmPA9574\r\n6NyCriJ+Ocjny+Q0hZ7YBXq9WPPdyhr/iXoB0UWEuaVVJ4doAVJ2nTiERk3B\r\npXJNpV072WA9r+K+nI4lwN9mgChuufP6dvQ=\r\n=udkp\r\n-----END PGP SIGNATURE-----\r\n"},"main":"index.js","husky":{"hooks":{"pre-commit":"lint"}},"scripts":{"lint":"eslint ./src","test":"jest ./src","build":"yarn clean & rollup --config","clean":"rimraf ./dist","format":"prettier --write './src/**/*.{spec,js,jsx,ts,tsx,css,md,json}' --config ./.prettierrc","release":"standard-version","lint:fix":"eslint ./src --fix","prerelease":"yarn format & yarn lint & yarn build"},"_npmUser":{"name":"beheeracato","email":"beheer@acato.nl"},"repository":{"url":"git+ssh://git@bitbucket.org/acato/axios-utils.git","type":"git"},"_npmVersion":"6.14.16","description":"axios interceptors","directories":{},"_nodeVersion":"14.19.0","browserslist":["last 1 chrome versions"],"_hasShrinkwrap":false,"devDependencies":{"jest":"^27.5.1","axios":"^0.26.0","husky":"^7.0.4","eslint":"^8.10.0","rimraf":"^3.0.2","rollup":"^2.68.0","prettier":"^2.5.1","standard-version":"^9.3.2","axios-mock-adapter":"^1.20.0","rollup-plugin-copy":"^3.4.0","eslint-plugin-react":"^7.29.2","rollup-plugin-terser":"^7.0.2","eslint-config-prettier":"^8.4.0","eslint-plugin-prettier":"^4.0.0","@rollup/plugin-commonjs":"^21.0.3","@rollup/plugin-node-resolve":"^13.1.3","@babel/plugin-transform-modules-commonjs":"^7.16.8"},"peerDependencies":{"axios":"^0.26.0"},"standard-version":{"scripts":{"prerelease":"yarn prerelease"}},"_npmOperationalInternal":{"tmp":"tmp/axios-utils_1.0.1_1650013830499_0.8504479246465781","host":"s3://npm-registry-packages"}},"1.0.2":{"name":"@acato/axios-utils","version":"1.0.2","author":{"name":"Marcel Bos","email":"marcel@acato.nl"},"license":"ICS","_id":"@acato/axios-utils@1.0.2","maintainers":[{"name":"beheeracato","email":"beheer@acato.nl"}],"homepage":"https://bitbucket.org/acato/axios-utils#readme","bugs":{"url":"https://bitbucket.org/acato/axios-utils/issues"},"dist":{"shasum":"79696bcd76116f855ba6dd3a0eb5f46258ef183d","tarball":"https://registry.npmjs.org/@acato/axios-utils/-/axios-utils-1.0.2.tgz","fileCount":3,"integrity":"sha512-N2sXjelsuO4s/vRTJeVTndUiSsLQ7EgyAs2Gd2LIOUJCzcI2Af3H98jJwLca3WdUsnhvcbgGT++UzYzXOc23dw==","signatures":[{"sig":"MEUCICKCCJs97bM+LGbbda+40uqOSkNQ7YxCHuhpEHyQzF5lAiEAwZnYP44+bOhR6gAuXAJjuTNCOZKUnlX/zqzG8RyPjqw=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":6225,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJivZvDACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpylQ//eqBHWnxeuinu3ORZm6hiiVjaZH8dAQr9E8vTBDDU5cHhEvhh\r\nFHXi6dETrs5Vb65kFyEYVFE5CcWRvXUiAoeAY+Usfjt6+1jV59h10qTv+wGR\r\n/hEIDQf0+3sroH63RcffPRASwEwOkpWiublKdHdYNISi30F+ij2AmnGUf2ZM\r\nxkLYXjyYwRSIajDRRAWLoSRyQp5z2Nj4s1kU45V49r+ILuYsH7Bghsz017N+\r\nMqEXOXEOamaLMugy1GmD8di42gHpWAvMwvfw2JsQhAs/+h0Vo8r17Zfj+dSq\r\njjos472gU4/KVPgM2I/RTWejRwJLkik6VCui5nDy7LVan15lzz5gWEsS1wy2\r\n3YCA3luawNAB1/CDrnY1xfNlFE57rHksLhfYAboThGLezjU0yl3MymsXMxms\r\nd9+Du8433a5LsTDjLv03c5K35NbhTLotsBG5OSLolJWeOk0Cs9goCoiDf+GY\r\ndjlWbiofwbHnfI2rkbCoDWGj3iQMgcc9V0ahaHO1KQXP5MT2+DPpAtSedSq1\r\ngqGObMuh4YkARHAH3TKGo5QMRmPMfepyHz/GvhXWHnBGT3ckcsJkgmQuG0ln\r\nnJXM4BTc9r62L1MbSFIlTltO3iq9CbqPwTxC2PBeZy47NmzKbx/1fn4vxASy\r\nyaO1E4dtMAcbDZghGVbudBDkNRjmdUh4wwM=\r\n=I3nf\r\n-----END PGP SIGNATURE-----\r\n"},"main":"index.js","husky":{"hooks":{"pre-commit":"lint"}},"gitHead":"f924e02db1aaee71b5d5b4864659b0b2a051c1b3","scripts":{"lint":"eslint ./src","test":"jest ./src","build":"yarn clean & rollup --config","clean":"rimraf ./dist","format":"prettier --write './src/**/*.{spec,js,jsx,ts,tsx,css,md,json}' --config ./.prettierrc","release":"standard-version","lint:fix":"eslint ./src --fix","prerelease":"yarn format & yarn lint & yarn build"},"_npmUser":{"name":"beheeracato","email":"beheer@acato.nl"},"repository":{"url":"git+ssh://git@bitbucket.org/acato/axios-utils.git","type":"git"},"_npmVersion":"8.1.2","description":"axios interceptors","directories":{},"_nodeVersion":"16.13.2","browserslist":["last 1 chrome versions"],"_hasShrinkwrap":false,"devDependencies":{"jest":"^27.5.1","axios":"^0.26.0","husky":"^7.0.4","eslint":"^8.10.0","rimraf":"^3.0.2","rollup":"^2.68.0","prettier":"^2.5.1","standard-version":"^9.3.2","axios-mock-adapter":"^1.20.0","rollup-plugin-copy":"^3.4.0","eslint-plugin-react":"^7.29.2","rollup-plugin-terser":"^7.0.2","eslint-config-prettier":"^8.4.0","eslint-plugin-prettier":"^4.0.0","@rollup/plugin-commonjs":"^21.0.3","@rollup/plugin-node-resolve":"^13.1.3","@babel/plugin-transform-modules-commonjs":"^7.16.8"},"peerDependencies":{"axios":"^0.26.0"},"standard-version":{"scripts":{"prerelease":"yarn prerelease"}},"_npmOperationalInternal":{"tmp":"tmp/axios-utils_1.0.2_1656593347619_0.7290959017359908","host":"s3://npm-registry-packages"}}},"time":{"created":"2022-04-15T09:10:30.414Z","modified":"2025-05-26T12:15:26.318Z","1.0.1":"2022-04-15T09:10:30.666Z","1.0.2":"2022-06-30T12:49:07.789Z"},"bugs":{"url":"https://bitbucket.org/acato/axios-utils/issues"},"author":{"name":"Marcel Bos","email":"marcel@acato.nl"},"license":"ICS","homepage":"https://bitbucket.org/acato/axios-utils#readme","repository":{"url":"git+ssh://git@bitbucket.org/acato/axios-utils.git","type":"git"},"description":"axios interceptors","maintainers":[{"email":"beheer@acato.nl","name":"beheeracato"},{"email":"paul@acato.nl","name":"paulacato"}],"readme":"# axios-utils\n\nEasy intercepting of axios resources (instances).\n\n# Table of contents\n\n- [Features](#features)\n- [Installing](#installing)\n- [Example](#example)\n- [API](#api)\n\n# Features\n\n- Create axios instances (resources) with default request config and/or custom\n  request config\n- Add error interceptors for specific status codes and/or based on regexp's\n\n# Installing\n\nUsing yarn:\n\n```js\nyarn add @acato/axios-utils\n```\n\nUsing npm:\n\n```js\nnpm i @acato/axios-utils\n```\n\n# Example\n\nCreating a (axios) resource:\n\n```js\nimport { createResource } from '@acato/axios-utils'\n\nexport const myResource = createResource({ baseUrl: 'https://myhost.com/api' })\n```\n\nDefine interceptors:\n\n```js\nimport { applyInterceptors, addHandler } from '@acato/axios-utils'\n\n// Authorization config:\nconst requestConfig = (config) => {\n  config.headers['Authorization'] = `Bearer ${getTokenFromSomewhere()}`\n  return config\n}\n// Request interceptor:\nexport const intercept = applyInterceptors({\n  request: requestConfig\n})\n// Request with extra config (makes use of re-usable 'requestConfig'):\nexport const intercept = applyInterceptors({\n   request: (config) => {\n    config.headers['x-header'] = 'x-value'\n    return requestConfig(config)\n  },\n})\n\n// Response interceptor:\nexport const intercept = applyInterceptors({\n   response: res => res // or do something useful instead\n})\n\n// Error handlers:\nconst responseErrorConfig = addHandler(\n  {\n    match: 502, // matches 502 errors (exact match)\n    handler: (error, axiosInstance) => // Promise.reject or do something else\n  },\n  {\n    match: /40/, // matches all 40* errors (regexp)\n    handler: (error, axiosInstance) => // Promise.reject or do something else\n  }\n)\n\nexport const intercept = applyInterceptors({\n  error: responseErrorConfig\n})\n```\n\nOur interceptor function is ready and we can intercept axios instances easily:\n\n```js\nimport { createResource } from '@acato/axios-utils'\nimport { intercept } from './my-interceptors'\n\nexport const myResource = intercept(createResource({ baseURL: 'https://...' }))\n```\n\n# API\n\nInterceptors can be connected to your resources by passing relevant interceptor\nconfig to `applyInterceptors`.\n\n**applyInterceptors(interceptorConfig)**\n\n## Interceptor config\n\n```js\n{\n // Not required\n request: config => {\n  config.headers['Authorization'] = `Bearer ${getTokenFromSomewhere()}`\n  return config\n },\n\n // Not required\n response: res => {\n   // do something with response before sending it\n },\n\n // Not required\n error: addHandler(\n    {\n      match: /502/,\n      handler: (err, axiosInstance) => ...\n    },\n    {\n      match: /40/,\n      handler: (err, axiosInstance) => ...\n    }\n  )\nNote that the order of the handlers matters, because the first match will be used to handle the specific error.\nSo to be sure your handler will be executed, add handlers from specific to more generic, like this:\n  addErrorHandler({\n        match: 401,\n        handler:...\n    },\n    {\n        match: /40/,\n        handler: ...\n    }\n  )\n\n This errorhandler will first match a 401 error, then all other 40* errors.\n\n}\n```\n\n## Return value\n\n`applyInterceptors` returns a function which accepts an axios resource (instance) as\nthe only argument.\n\n```js\n  export const applyInterceptors =\n  ({ request, response, error }) =>\n  (axiosResource) => {\n\n    // attach interceptors here to the axiosResource:\n  ...\n  }\n```\n\n**Usage**\n\nCreate a re-usable intercept function:\n\n```js\nimport { createResource } from '@acato/axios-utils'\n\nexport const intercept = applyInterceptors({\n  request: ...,\n  response: ...,\n  error: ...\n})\n```\n\nApply the intercept function on an axios resource:\n\n```js\nimport { createResource } from '@acato/axios-utils'\nimport { intercept } from 'my-interceptor'\n\nexport const myResource = intercept(\n  createResource({\n    baseURL: `${API_URL}/api`\n  })\n)\n```\n\nNow every fetch call with any axios resource 'wrapped' with the intercept function,\nwill trigger our interceptor handlers.\n","readmeFilename":"README.md"}