Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upgrade axios #342

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 19
node-version: 22
- run: yarn install --frozen-lockfile
- run: yarn lint && yarn test
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0y0/reaxios",
"version": "1.1.0",
"version": "1.2.0",
"description": "Easy-to-use HTTP client based on axios for the browser and NodeJS.",
"src": "src/index.js",
"main": "dist/index.cjs.js",
Expand Down Expand Up @@ -36,19 +36,21 @@
"pkg": "[ $(yarn info $npm_package_name version) != $npm_package_version ] && yarn publish || echo Skip publishing due to v$npm_package_version exist"
},
"dependencies": {
"@babel/runtime": "^7.22.6",
"axios": "^1.2.6"
"@babel/runtime": "^7.26.0",
"axios": "^1.7.9",
"axios-mock-adapter": "^2.1.0"
},
"devDependencies": {
"@0y0/babel-preset-vanilla": "^1.1.6",
"@0y0/eslint-config-vanilla": "^1.4.0",
"@rollup/plugin-babel": "^6.0.3",
"babel-jest": "^29.5.0",
"@0y0/babel-preset-vanilla": "^1.1.7",
"@0y0/eslint-config-vanilla": "^1.5.0",
"@rollup/plugin-babel": "^6.0.4",
"babel-jest": "^29.7.0",
"eslint": "^8.46.0",
"jest": "^29.5.0",
"jest-mock-axios": "^4.6.2",
"prettier": "^2.8.8",
"rollup": "^3.27.2",
"jest": "^29.7.0",
"jest-mock-axios": "^4.8.0",
"prettier": "^3.4.2",
"rollup": "^4.29.1",
"rollup-plugin-terser": "^7.0.2"
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
25 changes: 16 additions & 9 deletions src/Reaxios.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export default class Reaxios {
#headers = {}
#params = {}
#body
#cancel
#requestTransformer = val => val
#responseTransformer = val => val.data
#abortController
#requestTransformers = []
#responseTransformers = []

constructor(url, method = 'GET') {
if (!url) throw new TypeError('URL is required')
Expand Down Expand Up @@ -58,29 +58,36 @@ export default class Reaxios {
}

transformRequest(fn) {
if (isFn(fn)) this.#requestTransformer = fn
if (fn == null) this.#requestTransformers = []
if (isFn(fn)) this.#requestTransformers.push(fn)
if (Array.isArray(fn)) fn.forEach(f => this.transformRequest(f))
return this
}

transformResponse(fn) {
if (isFn(fn)) this.#responseTransformer = fn
if (fn == null) this.#responseTransformers = []
if (isFn(fn)) this.#responseTransformers.push(fn)
if (Array.isArray(fn)) fn.forEach(f => this.transformResponse(f))
return this
}

cancel() {
this.#cancel?.()
this.#abortController?.abort()
}

then(onFulfill, onReject) {
this.#abortController = new AbortController()
const promise = axios({
url: this.#url,
method: this.#method,
headers: this.#headers,
params: this.#params,
data: this.#requestTransformer(this.#body),
cancelToken: new axios.CancelToken(c => (this.#cancel = c))
data: this.#body,
transformRequest: this.#requestTransformers,
transformResponse: this.#responseTransformers,
signal: this.#abortController.signal
})
.then(this.#responseTransformer)
.then(response => response.data)
.then(onFulfill, onReject)
return new Repromise(promise, () => this.cancel())
}
Expand Down
32 changes: 16 additions & 16 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import mockAxios from 'jest-mock-axios'
import axios from 'axios'
import AxiosMockAdapter from 'axios-mock-adapter'

jest.mock('axios', () => mockAxios)
const Reaxios = require('./index').default

const testUrl = 'https://foo.bar'
const res = { data: { foo: 'bar' } }

describe('Reaxios', () => {
afterEach(() => mockAxios.reset())
const mock = new AxiosMockAdapter(axios)
mock.onGet(testUrl).reply(200, res)
mock.onPost(testUrl).reply(200, res)
mock.onPut(testUrl).reply(200, res)
mock.onDelete(testUrl).reply(200, res)

describe('Reaxios', () => {
it('should send GET request', async () => {
const successFn = jest.fn()
Reaxios.get(testUrl).then(successFn)
mockAxios.mockResponseFor({ url: testUrl, method: 'GET' }, res)
expect(successFn).toBeCalledWith(res.data)
await Reaxios.get(testUrl).then(successFn)
expect(successFn).toBeCalledWith(res)
})

it('should send POST request', async () => {
const successFn = jest.fn()
Reaxios.post(testUrl).then(successFn)
mockAxios.mockResponseFor({ url: testUrl, method: 'POST' }, res)
expect(successFn).toBeCalledWith(res.data)
await Reaxios.post(testUrl).then(successFn)
expect(successFn).toBeCalledWith(res)
})

it('should send PUT request', async () => {
const successFn = jest.fn()
Reaxios.put(testUrl).then(successFn)
mockAxios.mockResponseFor({ url: testUrl, method: 'PUT' }, res)
expect(successFn).toBeCalledWith(res.data)
await Reaxios.put(testUrl).then(successFn)
expect(successFn).toBeCalledWith(res)
})

it('should send DELETE request', async () => {
const successFn = jest.fn()
Reaxios.delete(testUrl).then(successFn)
mockAxios.mockResponseFor({ url: testUrl, method: 'DELETE' }, res)
expect(successFn).toBeCalledWith(res.data)
await Reaxios.delete(testUrl).then(successFn)
expect(successFn).toBeCalledWith(res)
})
})
Loading
Loading