Skip to content

Commit f6d3d49

Browse files
authored
Merge pull request #4 from Typeform/refactor-http-transport
Refactor http transport
2 parents 1bfb057 + 0e91267 commit f6d3d49

25 files changed

+1187
-407
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ env:
2020
before_install:
2121
- yarn
2222

23+
before_script:
24+
- yarn server &
25+
2326
script:
24-
- TYPEFORM_TOKEN=${TYPEFORM_TOKEN} yarn test
27+
- yarn test:unit
28+
- yarn test:integration
2529

2630
after_success:
2731
- yarn semantic-release

package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"homepage": "https://github.com/Typeform/js-api-clientk#readme",
2828
"dependencies": {
29-
"axios": "^0.18.0"
29+
"isomorphic-fetch": "^2.2.1"
3030
},
3131
"devDependencies": {
3232
"@babel/cli": "^7.0.0-beta.44",
@@ -47,7 +47,10 @@
4747
"husky": "^0.14.3",
4848
"in-publish": "^2.0.0",
4949
"jest": "^22.4.3",
50+
"jest-fetch-mock": "^1.6.5",
51+
"json-server": "^0.14.0",
5052
"lint-staged": "^7.0.4",
53+
"nodemon": "^1.18.3",
5154
"prettier": "^1.12.1",
5255
"semantic-release": "^15.1.7",
5356
"sinon": "^4.5.0",
@@ -60,18 +63,25 @@
6063
"git add"
6164
]
6265
},
66+
"jest": {
67+
"automock": false,
68+
"setupFiles": [
69+
"./tests/setupJest.js"
70+
]
71+
},
6372
"scripts": {
6473
"commitmsg": "commitlint -e $GIT_PARAMS",
65-
"test": "TYPEFORM_TOKEN=$TYPEFORM_TOKEN jest",
66-
"test:unit": "jest ./tests/unit",
67-
"test:unit:watch": "jest ./tests/unit --watch",
68-
"test:integration": "TYPEFORM_TOKEN=$TYPEFORM_TOKEN jest ./tests/integration/",
69-
"test:integration:watch": "TYPEFORM_TOKEN=$TYPEFORM_TOKEN jest ./tests/integration/ --watch",
74+
"test:unit": "MOCK_FETCH=true jest ./tests/unit",
75+
"test:unit:watch": "MOCK_FETCH=true jest ./tests/unit --watch",
76+
"test:integration": "jest ./tests/integration/",
77+
"test:integration:watch": "jest ./tests/integration/ --watch",
7078
"build:dist": "webpack --mode production",
7179
"build:lib": "NODE_ENV=production babel src --out-dir lib",
7280
"prepublish": "in-publish && npm run build:dist && npm run build:lib || not-in-publish",
7381
"pretty": "prettier './{src,tests}/**/*.js' --write",
74-
"semantic-release": "semantic-release"
82+
"semantic-release": "semantic-release",
83+
"server": "node ./tests/integration/mockServer.js",
84+
"server:dev": "nodemon ./tests/integration/mockServer.js"
7585
},
7686
"peerDependencies": {}
7787
}

src/constants/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ export const FONTS_AVAILABLE = [
3232
'Source Sans Pro',
3333
'Vollkorn'
3434
]
35+
36+
export const API_BASE_URL = 'https://api.typeform.com'

src/create-client.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
import axios from 'axios'
1+
import 'isomorphic-fetch'
2+
import { API_BASE_URL } from './constants'
23

34
export const clientConstructor = ({ token, ...options }) => {
4-
return axios.create({
5-
baseURL: 'https://api.typeform.com',
6-
headers: {
7-
Authorization: `bearer ${token}`
8-
},
9-
...options
10-
})
5+
return {
6+
request: (args) => {
7+
8+
const {
9+
url,
10+
...otherArgs
11+
} = args
12+
13+
const requestParameters = {
14+
...options,
15+
...otherArgs
16+
}
17+
18+
return fetch(`${API_BASE_URL}${url}`, {
19+
headers: {
20+
Authorization: `bearer ${token}`
21+
},
22+
...requestParameters
23+
})
24+
.then(response => response.json())
25+
// .catch(error => { throw `${error}` })
26+
}
27+
}
1128
}

src/forms.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import axios from 'axios'
2-
3-
export const forms = http => ({
1+
export default http => ({
42
list: args => getForms(http, args),
53
get: args => getForm(http, args),
64
update: args => updateForm(http, args),
@@ -12,7 +10,7 @@ export const forms = http => ({
1210
}
1311
})
1412

15-
export const getForms = (http, { page, page_size, search } = {}) => {
13+
const getForms = (http, { page, page_size, search } = {}) => {
1614
return http.request({
1715
method: 'get',
1816
url: `/forms`,
@@ -22,14 +20,14 @@ export const getForms = (http, { page, page_size, search } = {}) => {
2220
})
2321
}
2422

25-
export const getForm = (http, { uid }) => {
23+
const getForm = (http, { uid }) => {
2624
return http.request({
2725
method: 'get',
2826
url: `/forms/${uid}`
2927
})
3028
}
3129

32-
export const updateForm = (http, { uid, override, data } = {}) => {
30+
const updateForm = (http, { uid, override, data } = {}) => {
3331
let methodType = 'patch'
3432
if (override) {
3533
methodType = 'put'
@@ -42,29 +40,29 @@ export const updateForm = (http, { uid, override, data } = {}) => {
4240
})
4341
}
4442

45-
export const createForm = (http, { data } = {}) => {
43+
const createForm = (http, { data } = {}) => {
4644
return http.request({
4745
method: 'post',
4846
url: `/forms`,
4947
data
5048
})
5149
}
5250

53-
export const deleteForm = (http, { uid }) => {
51+
const deleteForm = (http, { uid }) => {
5452
return http.request({
5553
method: 'delete',
5654
url: `/forms/${uid}`
5755
})
5856
}
5957

60-
export const getMessages = (http, { uid }) => {
58+
const getMessages = (http, { uid }) => {
6159
return http.request({
6260
method: 'get',
6361
url: `/forms/${uid}/messages`
6462
})
6563
}
6664

67-
export const updateMessages = (http, { uid, data }) => {
65+
const updateMessages = (http, { uid, data }) => {
6866
return http.request({
6967
method: 'put',
7068
url: `/forms/${uid}/messages`,

src/images.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export const images = http => ({
1+
export default http => ({
22
list: () => getImages(http),
33
get: args => getImage(http, args),
44
add: args => addImage(http, args),
5-
remove: args => removeImage(http, args)
5+
delete: args => deleteImage(http, args)
66
})
77
export const getImages = http => {
88
return http.request({

src/responses.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const responses = http => ({
1+
export default http => ({
22
list: args => getResponses(http, args)
33
})
44

src/teams.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { isMemberPropValid, createMemberPatchQuery } from './utils'
22

3-
export const teams = http => {
3+
export default http => {
44
return {
5-
get(http) {
5+
get: () => {
66
return getTeam(http)
77
},
8-
addMembers(http, args) {
8+
addMembers: args => {
99
return addMembers(http, args)
1010
},
11-
removeMembers(http, args) {
12-
return addMembers(http, args)
11+
removeMembers: args => {
12+
return removeMembers(http, args)
1313
}
1414
}
1515
}

src/themes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FONTS_AVAILABLE } from './constants'
22

3-
export const themes = http => ({
3+
export default http => ({
44
list: args => getThemes(http, args),
55
get: args => getTheme(http, args),
66
create: args => createTheme(http, args),

src/typeform.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { clientConstructor } from './create-client'
2-
import { forms } from './forms'
3-
import { images } from './images'
4-
import { teams } from './teams'
5-
import { themes } from './themes'
6-
import { workspaces } from './workspaces'
7-
import { responses } from './responses'
8-
import { webhooks } from './webhooks'
2+
import forms from './forms'
3+
import images from './images'
4+
import teams from './teams'
5+
import themes from './themes'
6+
import workspaces from './workspaces'
7+
import responses from './responses'
8+
import webhooks from './webhooks'
99

1010
export const createClient = (args = {}) => {
1111
if (args.token === undefined) {

0 commit comments

Comments
 (0)