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

+5-1
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

+17-7
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

+2
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

+25-8
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

+8-10
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

+2-2
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

+1-1
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

+5-5
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

+1-1
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

+7-7
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) {

src/webhooks.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
export const webhooks = http => ({
1+
export default http => ({
22
get: args => getWebhook(http, args),
33
create: args => createWebhook(http, args),
44
update: args => updateWebhook(http, args),
55
delete: args => deleteWebhook(http, args)
66
})
77

8-
export const getWebhook = (http, { uid, tag }) => {
8+
const getWebhook = (http, { uid, tag }) => {
99
return http.request({
1010
method: 'get',
1111
url: `/forms/${uid}/webhooks/${tag}`
1212
})
1313
}
1414

15-
export const createWebhook = (http, args) => {
15+
const createWebhook = (http, args) => {
1616
return createOrUpdateWebhook(http, args)
1717
}
1818

19-
export const updateWebhook = (http, args) => {
19+
const updateWebhook = (http, args) => {
2020
return createOrUpdateWebhook(http, args)
2121
}
2222

23-
export const deleteWebhook = (http, { uid, tag }) => {
23+
const deleteWebhook = (http, { uid, tag }) => {
2424
return http.request({
2525
method: 'delete',
2626
url: `/forms/${uid}/webhooks/${tag}`
2727
})
2828
}
2929

30-
export const createOrUpdateWebhook = (
30+
const createOrUpdateWebhook = (
3131
http,
3232
{ uid, tag, url, enable = false }
3333
) => {
3434
if (url === undefined) {
3535
throw `Please provide an url for ${tag}`
3636
}
37+
if (tag === undefined) {
38+
throw `Please provide a tag name for the webhook`
39+
}
3740
return http.request({
3841
method: 'put',
3942
url: `/forms/${uid}/webhooks/${tag}`,

src/workspaces.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { isMemberPropValid, createMemberPatchQuery } from './utils'
22

3-
export const workspaces = http => ({
3+
export default http => ({
44
list: args => getWorkspaces(http, args),
55
get: args => getWorkspace(http, args),
6+
add: args => addWorkspace(http, args),
67
update: args => updateWorkspace(http, args),
78
delete: args => deleteWorkspace(http, args),
89
addMembers: args => addMembers(http, args),
910
removeMembers: args => removeMembers(http, args)
1011
})
1112

12-
export const getWorkspaces = (http, { search, page, page_size } = {}) => {
13+
const getWorkspaces = (http, { search, page, page_size } = {}) => {
1314
return http.request({
1415
method: 'get',
1516
url: '/workspaces',
@@ -21,14 +22,14 @@ export const getWorkspaces = (http, { search, page, page_size } = {}) => {
2122
})
2223
}
2324

24-
export const getWorkspace = (http, { id }) => {
25+
const getWorkspace = (http, { id }) => {
2526
return http.request({
2627
method: 'get',
2728
url: `/workspaces/${id}`
2829
})
2930
}
3031

31-
export const createWorkspace = (http, { name }) => {
32+
const addWorkspace = (http, { name }) => {
3233
if (name === undefined) {
3334
throw `A name is required`
3435
}
@@ -39,15 +40,15 @@ export const createWorkspace = (http, { name }) => {
3940
})
4041
}
4142

42-
export const updateWorkspace = (http, { id, data } = {}) => {
43+
const updateWorkspace = (http, { id, data } = {}) => {
4344
return http.request({
4445
method: 'patch',
4546
url: `/workspaces/${id}`,
4647
data
4748
})
4849
}
4950

50-
export const addMembers = (http, { id, members }) => {
51+
const addMembers = (http, { id, members }) => {
5152
if (!isMemberPropValid(members)) {
5253
throw `No member provided`
5354
}
@@ -61,7 +62,7 @@ export const addMembers = (http, { id, members }) => {
6162
return updateWorkspace(http, { id, data: membersQuery })
6263
}
6364

64-
export const removeMembers = (http, { id, members }) => {
65+
const removeMembers = (http, { id, members }) => {
6566
if (!isMemberPropValid(members)) {
6667
throw `No member provided`
6768
}
@@ -75,14 +76,14 @@ export const removeMembers = (http, { id, members }) => {
7576
return updateWorkspace(http, { id, data: membersQuery })
7677
}
7778

78-
export const deleteWorkspace = (http, { id }) => {
79+
const deleteWorkspace = (http, { id }) => {
7980
return http.request({
8081
method: 'delete',
8182
url: `/workspaces/${id}`
8283
})
8384
}
8485

85-
export const getWorkspaceForms = (
86+
const getWorkspaceForms = (
8687
http,
8788
{ id, from_id, page, page_size } = {}
8889
) => {

tests/integration/db.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"forms": [
3+
{
4+
"id": "abc123",
5+
"title": "test form",
6+
"fields": []
7+
},
8+
{
9+
"id": "xyz123",
10+
"title": "test form 2",
11+
"fields": []
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)