├── .babelrc.js
├── .eslintignore
├── .eslintrc
├── .github
├── dependabot.yml
└── workflows
│ ├── ci.yml
│ ├── notify-release.yml
│ ├── release.yml
│ └── stale.yml
├── .gitignore
├── .husky
├── commit-msg
└── pre-commit
├── .nvmrc
├── .prettierrc
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── commitlint.config.js
├── jest.config.js
├── package.json
├── src
├── index.d.ts
└── index.js
├── test-d
└── index.test-d.ts
├── test
├── .eslintrc
├── axios.d.ts
├── index.test.jsx
├── index.test.ssr.jsx
└── testing-library__react-hooks.d.ts
└── tsconfig.json
/.babelrc.js:
--------------------------------------------------------------------------------
1 | const { NODE_ENV, BABEL_ENV } = process.env
2 | const cjs = NODE_ENV === 'test' || BABEL_ENV === 'commonjs'
3 | const loose = true
4 |
5 | module.exports = {
6 | presets: [['@babel/preset-env', { loose, modules: false }]],
7 | plugins: [
8 | ['@babel/plugin-transform-object-rest-spread', { loose }],
9 | cjs && ['@babel/transform-modules-commonjs', { loose }],
10 | ['@babel/transform-runtime', { useESModules: !cjs }]
11 | ].filter(Boolean),
12 | env: {
13 | test: {
14 | presets: ['@babel/preset-env', '@babel/preset-react']
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | cjs
2 | coverage
3 | es
4 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "root": true,
3 | "extends": [
4 | "eslint:recommended",
5 | "plugin:import/recommended",
6 | "plugin:prettier/recommended",
7 | "plugin:react-hooks/recommended"
8 | ],
9 | "parserOptions": {
10 | "ecmaVersion": "latest"
11 | },
12 | "env": {
13 | "browser": true,
14 | "node": true
15 | },
16 | "globals": {
17 | "Promise": true
18 | },
19 | "rules": {
20 | "valid-jsdoc": 2
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: npm
4 | directory: '/'
5 | schedule:
6 | interval: daily
7 | - package-ecosystem: github-actions
8 | directory: '/'
9 | schedule:
10 | interval: daily
11 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: ci
2 |
3 | on:
4 | push:
5 | branches: [master]
6 | pull_request:
7 |
8 | jobs:
9 | build:
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: actions/checkout@v4
13 | - uses: actions/setup-node@v4
14 | with:
15 | node-version-file: '.nvmrc'
16 | - run: npm i
17 | - run: npm run lint
18 | - run: npm test -- --coverage
19 | - run: bash <(curl -s https://codecov.io/bash)
20 |
21 | automerge:
22 | needs: build
23 | runs-on: ubuntu-latest
24 | permissions:
25 | pull-requests: write
26 | contents: write
27 | steps:
28 | - uses: fastify/github-action-merge-dependabot@v3
29 | with:
30 | github-token: ${{ secrets.GITHUB_TOKEN }}
31 |
--------------------------------------------------------------------------------
/.github/workflows/notify-release.yml:
--------------------------------------------------------------------------------
1 | name: notify-release
2 | on:
3 | workflow_dispatch:
4 | release:
5 | types: [published]
6 | issues:
7 | types: [closed]
8 | schedule:
9 | - cron: '30 8 * * *'
10 | jobs:
11 | setup:
12 | runs-on: ubuntu-latest
13 | permissions:
14 | issues: write
15 | contents: read
16 | steps:
17 | - name: Notify release
18 | uses: nearform-actions/github-action-notify-release@v1
19 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: release
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | semver:
7 | description: "The semver to use"
8 | required: true
9 | default: "patch"
10 | type: choice
11 | options:
12 | - patch
13 | - minor
14 | - major
15 | tag:
16 | description: "The npm tag"
17 | required: false
18 | default: "latest"
19 | commit-message:
20 | description: "The commit message template"
21 | required: false
22 | default: "chore(release): {version}"
23 | pull_request:
24 | types: [closed]
25 |
26 | jobs:
27 | release:
28 | runs-on: ubuntu-latest
29 | steps:
30 | - uses: nearform/optic-release-automation-action@v4
31 | with:
32 | github-token: ${{ secrets.github_token }}
33 | npm-token: ${{ secrets.NPM_TOKEN }}
34 | commit-message: ${{ github.event.inputs.commit-message }}
35 | semver: ${{ github.event.inputs.semver }}
36 | npm-tag: ${{ github.event.inputs.tag }}
37 | build-command: npm i
38 |
--------------------------------------------------------------------------------
/.github/workflows/stale.yml:
--------------------------------------------------------------------------------
1 | name: 'Close stale issues and PRs'
2 | on:
3 | workflow_dispatch:
4 | schedule:
5 | - cron: '30 1 * * *'
6 |
7 | jobs:
8 | stale:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/stale@v9
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | es
2 | cjs
3 | node_modules
4 | coverage
5 | npm-debug.log*
6 | *.test.ts*
7 | *.test.ssr.ts*
8 | package-lock.json
9 |
--------------------------------------------------------------------------------
/.husky/commit-msg:
--------------------------------------------------------------------------------
1 | npx --no-install commitlint --edit $1
2 |
--------------------------------------------------------------------------------
/.husky/pre-commit:
--------------------------------------------------------------------------------
1 | npx --no-install lint-staged
2 |
--------------------------------------------------------------------------------
/.nvmrc:
--------------------------------------------------------------------------------
1 | lts/*
2 |
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "semi": false,
3 | "singleQuote": true,
4 | "trailingComma": "none",
5 | "arrowParens": "avoid",
6 | "endOfLine": "auto"
7 | }
8 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | > ⚠ This file is deprecated. Releases after 3.0.0 use GitHub's built-in releases to track changes ⚠
2 |
3 | # Changelog
4 |
5 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
6 |
7 | ## [3.0.0](https://github.com/simoneb/axios-hooks/compare/v2.7.0...v3.0.0) (2021-10-31)
8 |
9 |
10 | ### ⚠ BREAKING CHANGES
11 |
12 | * requires axios@^0.24.0
13 |
14 | ### Features
15 |
16 | * apply type-safe for AxiosRequestConfig data parameter ([#611](https://github.com/simoneb/axios-hooks/issues/611)) ([eb05b0a](https://github.com/simoneb/axios-hooks/commit/eb05b0a3358585d051e03365a6924a5fb90a71b4))
17 |
18 | ## [2.7.0](https://github.com/simoneb/axios-hooks/compare/v2.6.3...v2.7.0) (2021-08-29)
19 |
20 |
21 | ### Features
22 |
23 | * skip automatic request cancellation ([#533](https://github.com/simoneb/axios-hooks/issues/533)) ([624ea44](https://github.com/simoneb/axios-hooks/commit/624ea4426a5609c2c928b56be1e99ab06c23ac93))
24 |
25 | ### [2.6.3](https://github.com/simoneb/axios-hooks/compare/v2.6.2...v2.6.3) (2021-05-17)
26 |
27 | ### [2.6.2](https://github.com/simoneb/axios-hooks/compare/v2.6.1...v2.6.2) (2021-05-13)
28 |
29 |
30 | ### Bug Fixes
31 |
32 | * **deps:** update dependency @babel/runtime to v7.13.17 ([259ea08](https://github.com/simoneb/axios-hooks/commit/259ea087174890284efda69f91b00537e9f9c7ce))
33 |
34 | ### [2.6.1](https://github.com/simoneb/axios-hooks/compare/v2.6.0...v2.6.1) (2021-03-30)
35 |
36 | ## [2.6.0](https://github.com/simoneb/axios-hooks/compare/v2.5.1...v2.6.0) (2021-03-28)
37 |
38 |
39 | ### Features
40 |
41 | * reset hook state when config changes with outstanding manual fetch ([8b2f166](https://github.com/simoneb/axios-hooks/commit/8b2f166c41c453026fde4dc9eded1c9ee927cc46))
42 | * update the hook state when cancelled manually ([203541b](https://github.com/simoneb/axios-hooks/commit/203541bb3ad52f2bd95d453ff0ffd3d1663e2794))
43 |
44 | ### [2.5.1](https://github.com/simoneb/axios-hooks/compare/v2.5.0...v2.5.1) (2021-03-28)
45 |
46 |
47 | ### Features
48 |
49 | * **typings:** expose return type of useAxios ([3273ba1](https://github.com/simoneb/axios-hooks/commit/3273ba1fba2f7417bdc7844e77fe47ecd6fe2761))
50 |
51 | ## [2.5.0](https://github.com/simoneb/axios-hooks/compare/v2.5.0-0...v2.5.0) (2021-03-12)
52 |
53 | ## [2.5.0-0](https://github.com/simoneb/axios-hooks/compare/v2.4.1...v2.5.0-0) (2021-03-06)
54 |
55 |
56 | ### Features
57 |
58 | * no config serialization ([15fe158](https://github.com/simoneb/axios-hooks/commit/15fe1588448fc58b0b5b83815cc3a12812a466a2))
59 |
60 |
61 | ### Bug Fixes
62 |
63 | * tests ([4176d94](https://github.com/simoneb/axios-hooks/commit/4176d9489085febda797dabddb104141197a901c))
64 |
65 | ### [2.4.1](https://github.com/simoneb/axios-hooks/compare/v2.4.0...v2.4.1) (2021-03-06)
66 |
67 |
68 | ### Bug Fixes
69 |
70 | * regression in config serialization ([c5e8645](https://github.com/simoneb/axios-hooks/commit/c5e86450811b64e1babdd2cff97ad463b5ee83b2))
71 |
72 | ## [2.4.0](https://github.com/simoneb/axios-hooks/compare/v2.4.0-0...v2.4.0) (2021-03-05)
73 |
74 |
75 | ### Features
76 |
77 | * remove config serialization ([06a8197](https://github.com/simoneb/axios-hooks/commit/06a819728d7631c0ac26e774b447885597ba6c2f))
78 |
79 | ## [2.4.0-0](https://github.com/simoneb/axios-hooks/compare/v2.3.0...v2.4.0-0) (2021-02-12)
80 |
81 |
82 | ### Features
83 |
84 | * add two more tests of the cancel method ([9612292](https://github.com/simoneb/axios-hooks/commit/961229226b05ab8d55a01e35b0adc3424f7415b8))
85 | * return the cancelOutstandingRequest method in the index after the refetch method ([c6346c0](https://github.com/simoneb/axios-hooks/commit/c6346c0bb0d6184060d9ccb97f21082b9344dc42))
86 |
87 | ## [2.3.0](https://github.com/simoneb/axios-hooks/compare/v2.2.0...v2.3.0) (2021-01-14)
88 |
89 |
90 | ### Features
91 |
92 | * default options ([b218e68](https://github.com/simoneb/axios-hooks/commit/b218e689ae3c0e69f0ff3645785bb2ad968f2696))
93 |
94 | ## [2.2.0](https://github.com/simoneb/axios-hooks/compare/v2.1.0...v2.2.0) (2020-11-18)
95 |
96 |
97 | ### Bug Fixes
98 |
99 | * **deps:** update dependency lru-cache to v6 ([e589be6](https://github.com/simoneb/axios-hooks/commit/e589be6100130269c4dcdc202288018d1a45cebb))
100 | * **typings:** improve typing of the error and response properties on ResponseValues ([4479a90](https://github.com/simoneb/axios-hooks/commit/4479a906a8db61511a06f5836c6cb9a5c7729e80))
101 |
102 | ## [2.1.0](https://github.com/simoneb/axios-hooks/compare/v2.0.0...v2.1.0) (2020-07-04)
103 |
104 |
105 | ### Features
106 |
107 | * **269:** options re-evaluation ([9218707](https://github.com/simoneb/axios-hooks/commit/9218707871750c472420bf1ae1a570969b96e3bf)), closes [#269](https://github.com/simoneb/axios-hooks/issues/269)
108 |
109 | ## [2.0.0](https://github.com/simoneb/axios-hooks/compare/v1.11.0...v2.0.0) (2020-06-24)
110 |
111 |
112 | ### ⚠ BREAKING CHANGES
113 |
114 | * This release introduces a fundamental change in the caching mechanism.
115 |
116 | The main difference is that requests that don't use the cache
117 | will store the response in the cache anyway, making the behavior
118 | of the library more intuitive and predictable.
119 |
120 | In other words, `{ useCache: false }` will only skip _reading_ from the cache,
121 | but it will write the response to the cache in any case.
122 |
123 | The docs contain a caching example providing a full overview of
124 | how the new caching behavior works.
125 |
126 | A potential side effect of the new behavior, which we tried mitigating,
127 | is that the `refetch` function returned by the hook, which was always
128 | skipping the cache previously, now stores the response in cache.
129 | Because of this, it must generate a key for the cache, which is created
130 | based on the configuration provided as the first argument to the `refetch`
131 | function itself.
132 |
133 | Because the `refetch` function is often provided directly to DOM event handlers:
134 |
135 | ```
136 |
137 | ```
138 |
139 | this would no longer work because the first argument will be the React event and
140 | we cannot generate a cache key from that, and it wouldn't make much sense either.
141 | Because this is a fairly common scenario, we implemented a specific handling for
142 | this case. If the first argument is an event, it is ignored and considered as if
143 | no configuration override was provided.
144 |
145 | ### Features
146 |
147 | * store response in cache when skipping cache for request ([fff9ffe](https://github.com/simoneb/axios-hooks/commit/fff9ffed1fe6d6f35c3c6bac7fa132470e16b5bb))
148 |
149 | ## [1.11.0](https://github.com/simoneb/axios-hooks/compare/v1.10.1...v1.11.0) (2020-06-21)
150 |
151 |
152 | ### Features
153 |
154 | * **250:** all axios-hooks-generated requests cancel each other ([0de127e](https://github.com/simoneb/axios-hooks/commit/0de127e304937e61013ad2415770ca6cc06bc401)), closes [#250](https://github.com/simoneb/axios-hooks/issues/250)
155 |
156 | ### [1.10.1](https://github.com/simoneb/axios-hooks/compare/v1.10.0...v1.10.1) (2020-06-21)
157 |
158 |
159 | * optimize re-rendering with correct initState ([d7aef70](https://github.com/simoneb/axios-hooks/commit/d7aef708e662b7acebfe1ed967e44609bd270816))
160 |
161 | ## [1.10.0](https://github.com/simoneb/axios-hooks/compare/v1.10.0-0...v1.10.0) (2020-04-13)
162 |
163 | ## [1.10.0-0](https://github.com/simoneb/axios-hooks/compare/v1.9.0...v1.10.0-0) (2020-04-11)
164 |
165 |
166 | ### Features
167 |
168 | * **199:** add ability to disable cache ([b8f504a](https://github.com/simoneb/axios-hooks/commit/b8f504a1e85f415d70f17d1d38b6c17b33a1372f)), closes [#199](https://github.com/simoneb/axios-hooks/issues/199)
169 |
170 | ## [1.9.0](https://github.com/simoneb/axios-hooks/compare/v1.8.0...v1.9.0) (2019-12-12)
171 |
172 |
173 | ### Features
174 |
175 | * **src/index.d.ts:** export useAxios related interfaces ([6f0fa2a](https://github.com/simoneb/axios-hooks/commit/6f0fa2a49470fa4276ad668baeeddd87e90c34ac))
176 |
177 | ## [1.8.0](https://github.com/simoneb/axios-hooks/compare/v1.8.0-2...v1.8.0) (2019-12-06)
178 |
179 |
180 | ### Bug Fixes
181 |
182 | * correct @babel/preset-react version ([181ddcf](https://github.com/simoneb/axios-hooks/commit/181ddcf9509007f661332fb0f9904ea30af90022))
183 |
184 | ## [1.8.0-2](https://github.com/simoneb/axios-hooks/compare/v1.8.0-1...v1.8.0-2) (2019-12-06)
185 |
186 |
187 | ### Bug Fixes
188 |
189 | * ts definitions for makeUseAxios ([13b20cc](https://github.com/simoneb/axios-hooks/commit/13b20ccf1a84e57b4771168b86338925052f6a9c))
190 |
191 | ## [1.8.0-1](https://github.com/simoneb/axios-hooks/compare/v1.7.2...v1.8.0-1) (2019-12-06)
192 |
193 |
194 | ### Features
195 |
196 | * support multiple instances of useAxios configured independently ([2b0e9a5](https://github.com/simoneb/axios-hooks/commit/2b0e9a58ed55d581f2b06244d68fa8d5609ed50d)), closes [#98](https://github.com/simoneb/axios-hooks/issues/98)
197 |
198 |
199 | ### Bug Fixes
200 |
201 | * ts definitions for makeUseAxios ([bbdb3e0](https://github.com/simoneb/axios-hooks/commit/bbdb3e0085d55027151b74690d63657ac0c1118d))
202 |
203 | ## [1.8.0-0](https://github.com/simoneb/axios-hooks/compare/v1.7.2...v1.8.0-0) (2019-11-30)
204 |
205 |
206 | ### Features
207 |
208 | * support multiple instances of useAxios configured independently ([2b0e9a5](https://github.com/simoneb/axios-hooks/commit/2b0e9a58ed55d581f2b06244d68fa8d5609ed50d)), closes [#98](https://github.com/simoneb/axios-hooks/issues/98)
209 |
210 | ### [1.7.2](https://github.com/simoneb/axios-hooks/compare/v1.7.1...v1.7.2) (2019-11-13)
211 |
212 |
213 | ### Bug Fixes
214 |
215 | * do not dispatch state updates when requests are cancelled ([307c7c1](https://github.com/simoneb/axios-hooks/commit/307c7c1565e9f2d36f0e90bd19a86815dde2d8ce)), closes [#74](https://github.com/simoneb/axios-hooks/issues/74)
216 | * use StaticAxios for cancellation as AxiosInstance doesn't expose it ([4a63b6e](https://github.com/simoneb/axios-hooks/commit/4a63b6eab98f642372e1e2bb9d4429650183a1a9)), closes [#80](https://github.com/simoneb/axios-hooks/issues/80)
217 | * **deps:** update dependency @babel/runtime to v7.7.2 ([ebeab51](https://github.com/simoneb/axios-hooks/commit/ebeab5102d0aa60d856f10addc68de4be82c69a9))
218 |
219 | ### [1.7.2-0](https://github.com/simoneb/axios-hooks/compare/v1.7.1...v1.7.2-0) (2019-11-10)
220 |
221 | ### [1.7.1](https://github.com/simoneb/axios-hooks/compare/v1.7.0...v1.7.1) (2019-11-07)
222 |
223 |
224 | ### Bug Fixes
225 |
226 | * **build:** include typings in package ([0b43f5e](https://github.com/simoneb/axios-hooks/commit/0b43f5ea256a556f6a22261f7130b92db63ddbcc))
227 |
228 | ## [1.7.0](https://github.com/simoneb/axios-hooks/compare/v1.6.0...v1.7.0) (2019-11-07)
229 |
230 |
231 | ### Features
232 |
233 | * support request cancellation ([8cf1f74](https://github.com/simoneb/axios-hooks/commit/8cf1f747c5ae839682f0a6e1da4170e3b1e7b066))
234 |
235 | ## [1.6.0](https://github.com/simoneb/axios-hooks/compare/v1.5.0...v1.6.0) (2019-10-23)
236 |
237 |
238 | ### Features
239 |
240 | * async refetch ([f018d0a](https://github.com/simoneb/axios-hooks/commit/f018d0a31685003244c2be677845c6195646f2bd)), closes [#51](https://github.com/simoneb/axios-hooks/issues/51)
241 |
242 | ## [1.5.0](https://github.com/simoneb/axios-hooks/compare/v1.5.0-0...v1.5.0) (2019-10-23)
243 |
244 | ## [1.5.0-0](https://github.com/simoneb/axios-hooks/compare/v1.4.1...v1.5.0-0) (2019-10-22)
245 |
246 |
247 | ### Features
248 |
249 | * maintain referential integrity of refetch function ([1c4c5ac](https://github.com/simoneb/axios-hooks/commit/1c4c5acdd56f5597269a0d9ae321daf2e587f5c8))
250 |
251 |
252 | ### Bug Fixes
253 |
254 | * typing of serializeCache function ([3040b80](https://github.com/simoneb/axios-hooks/commit/3040b80696e7ab9df07f64ac6c5beacc276178cd))
255 |
256 | ### [1.4.1](https://github.com/simoneb/axios-hooks/compare/v1.3.0...v1.4.1) (2019-10-19)
257 |
258 |
259 | ### Bug Fixes
260 |
261 | * do not execute manual requests on server ([88829b0](https://github.com/simoneb/axios-hooks/commit/88829b0206673f411367eae67b018dae0c48b059))
262 |
263 | ## [1.4.0](https://github.com/simoneb/axios-hooks/compare/v1.3.0...v1.4.0) (2019-10-12)
264 |
265 | Features
266 |
267 | - Add useCache option
268 |
269 | ## [1.3.0](https://github.com/simoneb/axios-hooks/compare/v1.2.1...v1.3.0) (2019-08-05)
270 |
271 | ### [1.2.1](https://github.com/simoneb/axios-hooks/compare/v1.2.0...v1.2.1) (2019-07-21)
272 |
273 | ## [1.2.0](https://github.com/simoneb/axios-hooks/compare/v1.1.5...v1.2.0) (2019-07-18)
274 |
275 | ### Features
276 |
277 | - Allow using the cache when triggering requests manually ([1fccd27](https://github.com/simoneb/axios-hooks/commit/1fccd27)), closes [#19](https://github.com/simoneb/axios-hooks/issues/19)
278 |
279 | ### [1.1.5](https://github.com/simoneb/axios-hooks/compare/v1.1.4...v1.1.5) (2019-07-16)
280 |
281 | ### [1.1.4](https://github.com/simoneb/axios-hooks/compare/v1.1.3...v1.1.4) (2019-07-14)
282 |
283 | ### [1.1.3](https://github.com/simoneb/axios-hooks/compare/v1.1.2...v1.1.3) (2019-07-12)
284 |
285 | ### [1.1.2](https://github.com/simoneb/axios-hooks/compare/v1.1.1...v1.1.2) (2019-06-30)
286 |
287 | ### [1.1.1](https://github.com/simoneb/axios-hooks/compare/v1.1.0...v1.1.1) (2019-06-30)
288 |
289 | ### Tests
290 |
291 | - Add tests ([a1412a5](https://github.com/simoneb/axios-hooks/commit/a1412a5))
292 |
293 | ## [1.1.0](https://github.com/simoneb/axios-hooks/compare/v1.0.1...v1.1.0) (2019-05-25)
294 |
295 | ### Features
296 |
297 | - Add manual option to skip automatic execution ([a98fba2](https://github.com/simoneb/axios-hooks/commit/a98fba2)), closes [#6](https://github.com/simoneb/axios-hooks/issues/6)
298 |
299 | # Changelog
300 |
301 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
302 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | AWISC License - Anti War ISC
2 |
3 | Copyright 2015-present Simone Busoli
4 |
5 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8 |
9 | ANTI WAR CLAUSE
10 |
11 | This license is a modification of the original ISC license. It is identical to ISC but explicitly forbids use by any businesses, institutions or individuals residing or financing countries whose governments engage in wars against other countries.
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > The license of this software has changed to AWISC - Anti War ISC license
2 |
3 | # axios-hooks
4 |
5 | [](https://github.com/simoneb/axios-hooks/actions/workflows/ci.yml)
6 | [](https://codecov.io/gh/simoneb/axios-hooks)
7 | [](https://badge.fury.io/js/axios-hooks)
8 | [](https://bundlephobia.com/result?p=axios-hooks)
9 |
10 | React hooks for [axios], with built-in support for server side rendering.
11 |
12 | ## Features
13 |
14 | - All the [axios] awesomeness you are familiar with
15 | - Zero configuration, but configurable if needed
16 | - One-line usage
17 | - Super straightforward to use with SSR
18 |
19 | ## Installation
20 |
21 | `npm install axios axios-hooks`
22 |
23 | > `axios` is a peer dependency and needs to be installed explicitly
24 |
25 | ## Version information
26 |
27 | - `axios-hooks@5.x` is compatible with `axios@1.x`
28 | - `axios-hooks@4.x` and below are compatible with `axios@0.x`
29 |
30 | ## Quick Start
31 |
32 | [](https://codesandbox.io/s/2oxrlq8rjr)
33 |
34 | ```js
35 | import useAxios from 'axios-hooks'
36 |
37 | function App() {
38 | const [{ data, loading, error }, refetch] = useAxios(
39 | 'https://reqres.in/api/users?delay=1'
40 | )
41 |
42 | if (loading) return
Loading...
43 | if (error) return
Error!
44 |
45 | return (
46 |
47 |
48 |
{JSON.stringify(data, null, 2)}
49 |
50 | )
51 | }
52 | ```
53 |
54 | ## Documentation
55 |
56 | ### API
57 |
58 | - [useAxios](#useaxiosurlconfig-options)
59 | - [configure](#configure-cache-axios-defaultoptions-)
60 | - [serializeCache](#serializeCache)
61 | - [loadCache](#loadcachecache)
62 | - [makeUseAxios](#makeuseaxios-cache-axios-defaultoptions-)
63 |
64 | ### Examples
65 |
66 | - [Quick start](https://codesandbox.io/s/2oxrlq8rjr)
67 | - [Manual request](https://codesandbox.io/s/axioshooks-manual-request-bq9w4)
68 | - [Error handling](https://codesandbox.io/s/axios-hooks-error-handling-gvi41)
69 | - [Authentication and token refresh](https://codesandbox.io/s/axios-hooks-authentication-zyeyh)
70 | - [Caching](https://codesandbox.io/s/axios-hooks-caching-nm62v)
71 | - [Using makeUseAxios](https://codesandbox.io/s/axios-hooks-makeuseaxios-kfuym)
72 | - [Configuration](https://codesandbox.io/s/oqvxw6mpyq)
73 | - [Pagination](https://codesandbox.io/s/axios-hooks-pagination-1wk3u)
74 | - [Infinite scrolling](https://codesandbox.io/s/axios-hooks-infinite-scrolling-42nw6)
75 | - [Request chaining](https://codesandbox.io/s/axios-hooks-request-chaining-wn12l)
76 | - [Options change detection](https://codesandbox.io/s/axios-hooks-options-change-v23tl)
77 | - [react-native](https://snack.expo.io/@simoneb/axios-hooks-react-native)
78 | - [With react-sortable-hoc](https://codesandbox.io/s/axios-hooks-react-sortable-hoc-eo3oy)
79 | - [With react-router](https://codesandbox.io/s/axios-hooks-react-router-26iwm)
80 |
81 | ### Guides
82 |
83 | - [Refresh Behavior](#refresh-behavior)
84 | - [Configuration](#configuration)
85 | - [Manual Requests](#manual-requests)
86 | - [Manual Cancellation](#manual-cancellation)
87 | - [Server Side Rendering](#server-side-rendering)
88 | - [Multiple Hook Instances](#multiple-hook-instances)
89 |
90 | ## API
91 |
92 | The package exports one default export and named exports:
93 |
94 | ```js
95 | import useAxios, {
96 | configure,
97 | loadCache,
98 | serializeCache,
99 | makeUseAxios
100 | } from 'axios-hooks'
101 | ```
102 |
103 | ### useAxios(url|config, options)
104 |
105 | The main React hook to execute HTTP requests.
106 |
107 | - `url|config` - The request URL or [config](https://github.com/axios/axios#request-config) object, the same argument accepted by `axios`.
108 | - `options` - An options object.
109 | - `manual` ( `false` ) - If true, the request is not executed immediately. Useful for non-GET requests that should not be executed when the component renders. Use the `execute` function returned when invoking the hook to execute the request manually.
110 | - `useCache` ( `true` ) - Allows caching to be enabled/disabled for the hook. It doesn't affect the `execute` function returned by the hook.
111 | - `ssr` ( `true` ) - Enables or disables SSR support
112 | - `autoCancel` ( `true` ) - Enables or disables automatic cancellation of pending requests whether it be
113 | from the automatic hook request or from the `manual` execute method
114 |
115 | > [!IMPORTANT]
116 | > Default caching behavior can interfere with test isolation. Read the [testing](#testing) section for more information.
117 |
118 | **Returns**
119 |
120 | `[{ data, loading, error, response }, execute, manualCancel]`
121 |
122 | - `data` - The [success response](https://github.com/axios/axios#response-schema) data property (for convenient access).
123 | - `loading` - True if the request is in progress, otherwise False.
124 | - `error` - The [error](https://github.com/axios/axios#handling-errors) value
125 | - `response` - The whole [success response](https://github.com/axios/axios#response-schema) object.
126 |
127 | - `execute([config[, options]])` - A function to execute the request manually, bypassing the cache by default.
128 |
129 | - `config` - Same `config` object as `axios`, which is _shallow-merged_ with the config object provided when invoking the hook. Useful to provide arguments to non-GET requests.
130 | - `options` - An options object.
131 | - `useCache` ( `false` ) - Allows caching to be enabled/disabled for this "execute" function.
132 |
133 | **Returns**
134 |
135 | A promise containing the response. If the request is unsuccessful, the promise rejects and the rejection must be handled manually.
136 |
137 | - `manualCancel()` - A function to cancel outstanding requests manually.
138 |
139 | ### configure({ cache, axios, defaultOptions })
140 |
141 | Allows to provide custom instances of cache and axios and to override the default options.
142 |
143 | - `cache` An instance of [lru-cache](https://github.com/isaacs/node-lru-cache), or `false` to disable the cache
144 | - `axios` An instance of [axios](https://github.com/axios/axios#creating-an-instance)
145 | - `defaultOptions` An object overriding the default Hook options. It will be merged with the default options.
146 |
147 | ### serializeCache()
148 |
149 | Dumps the request-response cache, to use in server side rendering scenarios.
150 |
151 | **Returns**
152 |
153 | `Promise` A serializable representation of the request-response cache ready to be used by `loadCache`
154 |
155 | ### loadCache(cache)
156 |
157 | Populates the cache with serialized data generated by `serializeCache`.
158 |
159 | - `cache` The serializable representation of the request-response cache generated by `serializeCache`
160 |
161 | ### makeUseAxios({ cache, axios, defaultOptions })
162 |
163 | Creates an instance of the `useAxios` hook configured with the supplied cache, axios instance and default options.
164 |
165 | - `cache` An instance of [lru-cache](https://github.com/isaacs/node-lru-cache), or `false` to disable the cache
166 | - `axios` An instance of [axios](https://github.com/axios/axios#creating-an-instance)
167 | - `defaultOptions` An object overriding the default Hook options. It will be merged with the default options.
168 |
169 | **Returns**
170 |
171 | An instance of `useAxios` React Hook which will always use the provided cache and axios instance.
172 |
173 | The returned value, besides being a function that can be used as a React Hook, also contains the properties:
174 |
175 | - `resetConfigure`
176 | - `configure`
177 | - `loadCache`
178 | - `serializeCache`
179 |
180 | which are the same as the package's named exports but limited to the `useAxios` instance returned by `makeUseAxios`.
181 |
182 | ## Refresh Behavior
183 |
184 | The arguments provided to `useAxios(config[,options])` are watched for changes and compared using deep object comparison.
185 |
186 | When they change, if the configuration allows a request to be fired (e.g. `manual:false`), any pending request is canceled and a new request is triggered, to avoid automatic cancellation you should use `autoCancel:false` option
187 |
188 | Because of this, it's important to make sure that the arguments to `useAxios` preserve deep equality across component renders. This is often the case unless functions (e.g. axios transformers) are provided to a configuration object. In that case, those functions need to be memoized or they will trigger a request execution at each render, leading to an infinite loop.
189 |
190 | ## Configuration
191 |
192 | Unless provided via the `configure` function, `axios-hooks` uses as defaults:
193 |
194 | - `axios` - the default `axios` package export
195 | - `cache` - a new instance of the default `lru-cache` package export, with no arguments
196 | - `defaultOptions` - `{ manual: false, useCache: true, ssr: true, autoCancel: true }`
197 |
198 | These defaults may not suit your needs, for example:
199 |
200 | - you may want a common base url for axios requests
201 | - the default (Infinite) cache size may not be a sensible default
202 | - you want to disable caching altogether
203 |
204 | In such cases you can use the `configure` function to provide your custom implementation of both.
205 |
206 | > When `configure` is used, it should be invoked once before any usages of the `useAxios` hook
207 |
208 | ### Example
209 |
210 | [](https://codesandbox.io/s/oqvxw6mpyq)
211 |
212 | ```js
213 | import { configure } from 'axios-hooks'
214 | import LRU from 'lru-cache'
215 | import Axios from 'axios'
216 |
217 | const axios = Axios.create({
218 | baseURL: 'https://reqres.in/api'
219 | })
220 |
221 | const cache = new LRU({ max: 10 })
222 |
223 | configure({ axios, cache })
224 | ```
225 |
226 | ## Manual Requests
227 |
228 | On the client, requests are executed when the component renders using a React `useEffect` hook.
229 |
230 | This may be undesirable, as in the case of non-GET requests. By using the `manual` option you can skip the automatic execution of requests and use the return value of the hook to execute them manually, optionally providing configuration overrides to `axios`.
231 |
232 | ### Example
233 |
234 | In the example below we use the `useAxios` hook twice. Once to load the data when the component renders, and once to submit data updates via a `PUT` request configured via the `manual` option.
235 |
236 | [](https://codesandbox.io/s/axioshooks-manual-request-bq9w4?fontsize=14)
237 |
238 | ```js
239 | import useAxios from 'axios-hooks'
240 |
241 | function App() {
242 | const [{ data: getData, loading: getLoading, error: getError }] = useAxios(
243 | 'https://reqres.in/api/users/1'
244 | )
245 |
246 | const [{ data: putData, loading: putLoading, error: putError }, executePut] =
247 | useAxios(
248 | {
249 | url: 'https://reqres.in/api/users/1',
250 | method: 'PUT'
251 | },
252 | { manual: true }
253 | )
254 |
255 | function updateData() {
256 | executePut({
257 | data: {
258 | ...getData,
259 | updatedAt: new Date().toISOString()
260 | }
261 | })
262 | }
263 |
264 | if (getLoading || putLoading) return
Loading...
265 | if (getError || putError) return
Error!
266 |
267 | return (
268 |
269 |
270 |
{JSON.stringify(putData || getData, null, 2)}
271 |
272 | )
273 | }
274 | ```
275 |
276 | ## Manual Cancellation
277 |
278 | The cancellation method can be used to cancel an outstanding request whether it be
279 | from the automatic hook request or from the `manual` execute method.
280 |
281 | ### Example
282 |
283 | In the example below we use the `useAxios` hook with its automatic and manual requests.
284 | We can call the cancellation programmatically or via controls.
285 |
286 | ```js
287 | function App() {
288 | const [pagination, setPagination] = useState({})
289 | const [{ data, loading }, refetch, cancelRequest] = useAxios({
290 | url: '/users?delay=5',
291 | params: { ...pagination }
292 | })
293 |
294 | const handleFetch = () => {
295 | setPagination({ per_page: 2, page: 2 })
296 | }
297 |
298 | const externalRefetch = async () => {
299 | try {
300 | await refetch()
301 | } catch (e) {
302 | // Handle cancellation
303 | }
304 | }
305 |
306 | return (
307 |
308 |
309 |
310 |
313 | {loading &&
...loading
}
314 |
{JSON.stringify(data, null, 2)}
315 |
316 | )
317 | }
318 | ```
319 |
320 | ## Server Side Rendering
321 |
322 | `axios-hooks` seamlessly supports server side rendering scenarios, by preloading data on the server and providing the data to the client, so that the client doesn't need to reload it.
323 |
324 | ### How it works
325 |
326 | 1. the React component tree is rendered on the server
327 | 2. `useAxios` HTTP requests are executed on the server
328 | 3. the server code awaits `serializeCache()` in order to obtain a serializable representation of the request-response cache
329 | 4. the server injects a JSON-serialized version of the cache in a `window` global variable
330 | 5. the client hydrates the cache from the global variable before rendering the application using `loadCache`
331 |
332 | ### Example
333 |
334 | [](https://codesandbox.io/s/v83l3mjq57)
335 |
336 | ```html
337 |
338 |
339 |
342 | ```
343 |
344 | ```js
345 | // server code for the server side rendering handler
346 |
347 | import { serializeCache } from 'axios-hooks'
348 |
349 | router.use(async (req, res) => {
350 | const index = fs.readFileSync(`${publicFolder}/index.html`, 'utf8')
351 | const html = ReactDOM.renderToString()
352 |
353 | // wait for axios-hooks HTTP requests to complete
354 | const cache = await serializeCache()
355 |
356 | res.send(
357 | index
358 | .replace('{{{html}}}', html)
359 | .replace('{{{cache}}}', JSON.stringify(cache).replace(/, document.getElementById('root'))
374 | ```
375 |
376 | ## Multiple Hook Instances
377 |
378 | Sometimes it is necessary to communicate with different APIs or use different caching strategies for different HTTP interactions.
379 |
380 | [`makeUseAxios`](#makeuseaxios-cache-axios) allows to create multiple instances of the `useAxios` React Hook which can be configured and managed independently.
381 |
382 | In other words, `makeUseAxios` is a factory of `useAxios`, which returns a React Hook configured against the provided `axios` or `cache` instances.
383 |
384 | > This feature can also be used to create a single pre configured React Hook instance as an alternative to the global `configure` feature
385 |
386 | ### Example
387 |
388 | [](https://codesandbox.io/s/axios-hooks-quick-start-kfuym)
389 |
390 | ```js
391 | import axios from 'axios'
392 | import { makeUseAxios } from 'axios-hooks'
393 |
394 | const useAxios = makeUseAxios({
395 | axios: axios.create({ baseURL: 'https://reqres.in/api' })
396 | })
397 |
398 | function App() {
399 | const [{ data, loading, error }, refetch] = useAxios('/users?delay=1')
400 |
401 | if (loading) return