Skip to content

Commit f285480

Browse files
committed
feat(axios): move from fetch to axios, fixes #19 #32
1 parent de4cc82 commit f285480

15 files changed

+5814
-4375
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ module.exports = {
44
"browser": true,
55
"jest": true,
66
"node": true
7+
},
8+
"globals": {
9+
"axios": "readonly"
710
}
811
};

package-lock.json

Lines changed: 2539 additions & 1284 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
"name": "@typeform/api-client",
33
"version": "0.0.0-semantic-released",
44
"description": "JS SDK for Typeform API",
5+
"scripts": {
6+
"test:unit": "MOCK_AXIOS=true jest ./tests/unit",
7+
"test:unit:watch": "MOCK_AXIOS=true jest ./tests/unit --watch",
8+
"test:integration": "jest ./tests/integration/",
9+
"test:integration:watch": "jest ./tests/integration/ --watch",
10+
"build": "npm run build:dist && npm run build:lib",
11+
"build:dist": "webpack --mode production",
12+
"build:lib": "NODE_ENV=production babel src --out-dir lib",
13+
"prepublish": "in-publish && npm run build || not-in-publish",
14+
"lint": "standard ./src",
15+
"semantic-release": "semantic-release",
16+
"server": "node ./tests/integration/mockServer.js",
17+
"server:dev": "nodemon ./tests/integration/mockServer.js"
18+
},
519
"main": "lib/typeform.js",
620
"directories": {
721
"lib": "lib",
@@ -29,7 +43,7 @@
2943
},
3044
"homepage": "https://github.com/Typeform/js-api-client#readme",
3145
"dependencies": {
32-
"isomorphic-fetch": "^2.2.1"
46+
"axios": "^0.19.0"
3347
},
3448
"devDependencies": {
3549
"@babel/cli": "^7.0.0-rc.1",
@@ -39,6 +53,7 @@
3953
"@babel/preset-env": "^7.0.0-rc.1",
4054
"@commitlint/cli": "^7.5.2",
4155
"@commitlint/config-conventional": "^7.5.0",
56+
"axios-mock-adapter": "^1.17.0",
4257
"babel-core": "^7.0.0-bridge.0",
4358
"babel-eslint": "^8.2.3",
4459
"babel-jest": "^22.4.3",
@@ -52,7 +67,6 @@
5267
"husky": "^2.1.0",
5368
"in-publish": "^2.0.0",
5469
"jest": "^22.4.3",
55-
"jest-fetch-mock": "^1.6.5",
5670
"json-server": "^0.14.0",
5771
"lint-staged": "^7.0.4",
5872
"nodemon": "^1.18.3",
@@ -74,26 +88,12 @@
7488
],
7589
"testURL": "http://localhost/"
7690
},
77-
"scripts": {
78-
"test:unit": "MOCK_FETCH=true jest ./tests/unit",
79-
"test:unit:watch": "MOCK_FETCH=true jest ./tests/unit --watch",
80-
"test:integration": "jest ./tests/integration/",
81-
"test:integration:watch": "jest ./tests/integration/ --watch",
82-
"build": "npm run build:dist && npm run build:lib",
83-
"build:dist": "webpack --mode production",
84-
"build:lib": "NODE_ENV=production babel src --out-dir lib",
85-
"prepublish": "in-publish && npm run build || not-in-publish",
86-
"lint": "standard ./src",
87-
"semantic-release": "semantic-release",
88-
"server": "node ./tests/integration/mockServer.js",
89-
"server:dev": "nodemon ./tests/integration/mockServer.js"
90-
},
9191
"standard": {
9292
"env": [
9393
"jest"
9494
],
9595
"globals": [
96-
"fetch"
96+
"axios"
9797
]
9898
},
9999
"husky": {

src/create-client.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/* globals fetch */
2-
import 'isomorphic-fetch'
1+
import axios from 'axios'
32
import { API_BASE_URL } from './constants'
43

54
export const clientConstructor = ({ token, ...options }) => {
@@ -24,16 +23,18 @@ export const clientConstructor = ({ token, ...options }) => {
2423
...otherArgs
2524
}
2625

27-
return fetch(requestUrl, {
26+
return axios({
27+
url: requestUrl,
2828
...requestParameters,
29-
body: JSON.stringify(data),
29+
data,
3030
headers: {
31+
Accept: 'application/json, text/plain, */*',
3132
...headers,
3233
...argsHeaders,
3334
Authorization: `bearer ${token}`
3435
}
3536
})
36-
.then(response => response.json())
37+
.then(response => response.data)
3738
}
3839
}
3940
}

tests/setupJest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
if (process.env.MOCK_FETCH) {
2-
global.fetch = require('jest-fetch-mock')
1+
if (process.env.MOCK_AXIOS) {
2+
global.axios = new (require('axios-mock-adapter'))(require('axios'))
33
}

tests/unit/create-client.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ import { clientConstructor, buildUrlWithParams } from '../../src/create-client'
22
import { API_BASE_URL } from '../../src/constants'
33

44
beforeEach(() => {
5-
fetch.resetMocks()
6-
fetch.mockResponse(JSON.stringify({}))
5+
axios.reset()
6+
axios.onAny().reply(200)
77
})
88

99
const client = clientConstructor({
1010
token: 'abc'
1111
})
1212

13-
test('request pass correct headers', () => {
14-
client.request({
13+
test('request pass correct headers', async () => {
14+
await client.request({
1515
url: '/forms',
1616
headers: {
1717
Accepts: 'application/json'
1818
}
1919
})
20-
21-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms`)
22-
expect(fetch.mock.calls[0][1].headers).toEqual({
20+
expect(axios.history.get[0].url).toBe(`${API_BASE_URL}/forms`)
21+
expect(axios.history.get[0].headers).toEqual({
22+
Accept: 'application/json, text/plain, */*',
2323
Accepts: 'application/json',
2424
Authorization: 'bearer abc'
2525
})

tests/unit/forms.test.js

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,110 +3,105 @@ import { API_BASE_URL } from '../../src/constants'
33
import forms from '../../src/forms'
44

55
beforeEach(() => {
6-
fetch.resetMocks()
7-
fetch.mockResponse(JSON.stringify({}))
6+
axios.reset()
7+
axios.onAny().reply(200)
88
})
99

1010
const http = clientConstructor({
1111
token: '123'
1212
})
1313
const formsRequest = forms(http)
1414

15-
test('get all forms has the correct method and path', () => {
16-
formsRequest.list()
17-
18-
const url = fetch.mock.calls[0][0].split('?')
19-
expect(url[0]).toBe(`${API_BASE_URL}/forms`)
20-
expect(fetch.mock.calls[0][1].method).toBe('get')
15+
test('get all forms has the correct method and path', async () => {
16+
await formsRequest.list()
17+
expect(axios.history.get[0].url).toBe(`${API_BASE_URL}/forms`)
18+
expect(axios.history.get[0].method).toBe('get')
2119
})
2220

23-
test('paramters are sent correctly', () => {
24-
formsRequest.list({
21+
test('paramters are sent correctly', async () => {
22+
await formsRequest.list({
2523
page: 2,
2624
pageSize: 10,
2725
search: 'hola',
2826
workspaceId: 'abc'
2927
})
30-
const url = fetch.mock.calls[0][0].split('?')
28+
const url = axios.history.get[0].url.split('?')
3129
const params = new URLSearchParams(url[1])
3230
expect(params.get('page')).toBe('2')
33-
expect(params.get('page')).toBe('2')
34-
expect(params.get('page')).toBe('2')
35-
expect(params.get('page')).toBe('2')
31+
expect(params.get('page_size')).toBe('10')
32+
expect(params.get('search')).toBe('hola')
33+
expect(params.get('workspace_id')).toBe('abc')
3634
})
3735

38-
test('getForm sends the correct UID', () => {
39-
formsRequest.get({ uid: 'abc123' })
40-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`)
36+
test('getForm sends the correct UID', async () => {
37+
await formsRequest.get({ uid: 'abc123' })
38+
expect(axios.history.get[0].url).toBe(`${API_BASE_URL}/forms/abc123`)
4139
})
4240

43-
test('getForm sets get method', () => {
44-
formsRequest.get({ uid: 'abc123' })
45-
expect(fetch.mock.calls[0][1].method).toBe('get')
41+
test('getForm sets get method', async () => {
42+
await formsRequest.get({ uid: 'abc123' })
43+
expect(axios.history.get[0].method).toBe('get')
4644
})
4745

48-
test('updateForm sends the correct UID and data', () => {
49-
formsRequest.update({
46+
test('updateForm sends the correct UID and data', async () => {
47+
await formsRequest.update({
5048
uid: 'abc123',
5149
data: {
5250
title: 'hola'
5351
}
5452
})
55-
const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
56-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`)
53+
const bodyParsed = JSON.parse(axios.history.patch[0].data)
54+
expect(axios.history.patch[0].url).toBe(`${API_BASE_URL}/forms/abc123`)
5755
expect(bodyParsed.title).toBe('hola')
5856
})
5957

60-
test('updateForm sets patch method in request by default', () => {
61-
formsRequest.update({
58+
test('updateForm sets patch method in request by default', async () => {
59+
await formsRequest.update({
6260
uid: 'abc123',
6361
data: {
6462
title: 'hola'
6563
}
6664
})
67-
expect(fetch.mock.calls[0][1].method).toBe('patch')
65+
expect(axios.history.patch[0].method).toBe('patch')
6866
})
6967

70-
test('updateForm sets put method in request when override option is set', () => {
71-
formsRequest.update({
68+
test('updateForm sets put method in request when override option is set', async () => {
69+
await formsRequest.update({
7270
uid: 'abc123',
7371
data: {
7472
title: 'hola'
7573
},
7674
override: true
7775
})
78-
79-
expect(fetch.mock.calls[0][1].method).toBe('put')
76+
expect(axios.history.put[0].method).toBe('put')
8077
})
8178

82-
test('deleteForm removes the correct uid form ', () => {
83-
formsRequest.delete({
79+
test('deleteForm removes the correct uid form ', async () => {
80+
await formsRequest.delete({
8481
uid: 'abc123'
8582
})
86-
87-
expect(fetch.mock.calls[0][1].method).toBe('delete')
88-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`)
83+
expect(axios.history.delete[0].method).toBe('delete')
84+
expect(axios.history.delete[0].url).toBe(`${API_BASE_URL}/forms/abc123`)
8985
})
9086

91-
test('create form has the correct path and method ', () => {
92-
formsRequest.create({})
93-
94-
expect(fetch.mock.calls[0][1].method).toBe('post')
95-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms`)
87+
test('create form has the correct path and method ', async () => {
88+
await formsRequest.create({})
89+
expect(axios.history.post[0].method).toBe('post')
90+
expect(axios.history.post[0].url).toBe(`${API_BASE_URL}/forms`)
9691
})
9792

98-
test('get messages has the correct path and method ', () => {
99-
formsRequest.messages.get({ uid: 'abc123' })
93+
test('get messages has the correct path and method ', async () => {
94+
await formsRequest.messages.get({ uid: 'abc123' })
10095

101-
expect(fetch.mock.calls[0][1].method).toBe('get')
102-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123/messages`)
96+
expect(axios.history.get[0].method).toBe('get')
97+
expect(axios.history.get[0].url).toBe(`${API_BASE_URL}/forms/abc123/messages`)
10398
})
10499

105-
test('update messages has the correct path and method ', () => {
106-
formsRequest.messages.update({
100+
test('update messages has the correct path and method ', async () => {
101+
await formsRequest.messages.update({
107102
uid: 'abc123'
108103
})
109104

110-
expect(fetch.mock.calls[0][1].method).toBe('put')
111-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123/messages`)
105+
expect(axios.history.put[0].method).toBe('put')
106+
expect(axios.history.put[0].url).toBe(`${API_BASE_URL}/forms/abc123/messages`)
112107
})

0 commit comments

Comments
 (0)