Skip to content

Commit

Permalink
Merge pull request #4 from Typeform/refactor-http-transport
Browse files Browse the repository at this point in the history
Refactor http transport
  • Loading branch information
jepser authored Aug 13, 2018
2 parents 1bfb057 + 0e91267 commit f6d3d49
Show file tree
Hide file tree
Showing 25 changed files with 1,187 additions and 407 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ env:
before_install:
- yarn

before_script:
- yarn server &

script:
- TYPEFORM_TOKEN=${TYPEFORM_TOKEN} yarn test
- yarn test:unit
- yarn test:integration

after_success:
- yarn semantic-release
Expand Down
24 changes: 17 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/Typeform/js-api-clientk#readme",
"dependencies": {
"axios": "^0.18.0"
"isomorphic-fetch": "^2.2.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.44",
Expand All @@ -47,7 +47,10 @@
"husky": "^0.14.3",
"in-publish": "^2.0.0",
"jest": "^22.4.3",
"jest-fetch-mock": "^1.6.5",
"json-server": "^0.14.0",
"lint-staged": "^7.0.4",
"nodemon": "^1.18.3",
"prettier": "^1.12.1",
"semantic-release": "^15.1.7",
"sinon": "^4.5.0",
Expand All @@ -60,18 +63,25 @@
"git add"
]
},
"jest": {
"automock": false,
"setupFiles": [
"./tests/setupJest.js"
]
},
"scripts": {
"commitmsg": "commitlint -e $GIT_PARAMS",
"test": "TYPEFORM_TOKEN=$TYPEFORM_TOKEN jest",
"test:unit": "jest ./tests/unit",
"test:unit:watch": "jest ./tests/unit --watch",
"test:integration": "TYPEFORM_TOKEN=$TYPEFORM_TOKEN jest ./tests/integration/",
"test:integration:watch": "TYPEFORM_TOKEN=$TYPEFORM_TOKEN jest ./tests/integration/ --watch",
"test:unit": "MOCK_FETCH=true jest ./tests/unit",
"test:unit:watch": "MOCK_FETCH=true jest ./tests/unit --watch",
"test:integration": "jest ./tests/integration/",
"test:integration:watch": "jest ./tests/integration/ --watch",
"build:dist": "webpack --mode production",
"build:lib": "NODE_ENV=production babel src --out-dir lib",
"prepublish": "in-publish && npm run build:dist && npm run build:lib || not-in-publish",
"pretty": "prettier './{src,tests}/**/*.js' --write",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"server": "node ./tests/integration/mockServer.js",
"server:dev": "nodemon ./tests/integration/mockServer.js"
},
"peerDependencies": {}
}
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export const FONTS_AVAILABLE = [
'Source Sans Pro',
'Vollkorn'
]

export const API_BASE_URL = 'https://api.typeform.com'
33 changes: 25 additions & 8 deletions src/create-client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import axios from 'axios'
import 'isomorphic-fetch'
import { API_BASE_URL } from './constants'

export const clientConstructor = ({ token, ...options }) => {
return axios.create({
baseURL: 'https://api.typeform.com',
headers: {
Authorization: `bearer ${token}`
},
...options
})
return {
request: (args) => {

const {
url,
...otherArgs
} = args

const requestParameters = {
...options,
...otherArgs
}

return fetch(`${API_BASE_URL}${url}`, {
headers: {
Authorization: `bearer ${token}`
},
...requestParameters
})
.then(response => response.json())
// .catch(error => { throw `${error}` })
}
}
}
18 changes: 8 additions & 10 deletions src/forms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import axios from 'axios'

export const forms = http => ({
export default http => ({
list: args => getForms(http, args),
get: args => getForm(http, args),
update: args => updateForm(http, args),
Expand All @@ -12,7 +10,7 @@ export const forms = http => ({
}
})

export const getForms = (http, { page, page_size, search } = {}) => {
const getForms = (http, { page, page_size, search } = {}) => {
return http.request({
method: 'get',
url: `/forms`,
Expand All @@ -22,14 +20,14 @@ export const getForms = (http, { page, page_size, search } = {}) => {
})
}

export const getForm = (http, { uid }) => {
const getForm = (http, { uid }) => {
return http.request({
method: 'get',
url: `/forms/${uid}`
})
}

export const updateForm = (http, { uid, override, data } = {}) => {
const updateForm = (http, { uid, override, data } = {}) => {
let methodType = 'patch'
if (override) {
methodType = 'put'
Expand All @@ -42,29 +40,29 @@ export const updateForm = (http, { uid, override, data } = {}) => {
})
}

export const createForm = (http, { data } = {}) => {
const createForm = (http, { data } = {}) => {
return http.request({
method: 'post',
url: `/forms`,
data
})
}

export const deleteForm = (http, { uid }) => {
const deleteForm = (http, { uid }) => {
return http.request({
method: 'delete',
url: `/forms/${uid}`
})
}

export const getMessages = (http, { uid }) => {
const getMessages = (http, { uid }) => {
return http.request({
method: 'get',
url: `/forms/${uid}/messages`
})
}

export const updateMessages = (http, { uid, data }) => {
const updateMessages = (http, { uid, data }) => {
return http.request({
method: 'put',
url: `/forms/${uid}/messages`,
Expand Down
4 changes: 2 additions & 2 deletions src/images.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const images = http => ({
export default http => ({
list: () => getImages(http),
get: args => getImage(http, args),
add: args => addImage(http, args),
remove: args => removeImage(http, args)
delete: args => deleteImage(http, args)
})
export const getImages = http => {
return http.request({
Expand Down
2 changes: 1 addition & 1 deletion src/responses.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const responses = http => ({
export default http => ({
list: args => getResponses(http, args)
})

Expand Down
10 changes: 5 additions & 5 deletions src/teams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { isMemberPropValid, createMemberPatchQuery } from './utils'

export const teams = http => {
export default http => {
return {
get(http) {
get: () => {
return getTeam(http)
},
addMembers(http, args) {
addMembers: args => {
return addMembers(http, args)
},
removeMembers(http, args) {
return addMembers(http, args)
removeMembers: args => {
return removeMembers(http, args)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/themes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FONTS_AVAILABLE } from './constants'

export const themes = http => ({
export default http => ({
list: args => getThemes(http, args),
get: args => getTheme(http, args),
create: args => createTheme(http, args),
Expand Down
14 changes: 7 additions & 7 deletions src/typeform.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { clientConstructor } from './create-client'
import { forms } from './forms'
import { images } from './images'
import { teams } from './teams'
import { themes } from './themes'
import { workspaces } from './workspaces'
import { responses } from './responses'
import { webhooks } from './webhooks'
import forms from './forms'
import images from './images'
import teams from './teams'
import themes from './themes'
import workspaces from './workspaces'
import responses from './responses'
import webhooks from './webhooks'

export const createClient = (args = {}) => {
if (args.token === undefined) {
Expand Down
15 changes: 9 additions & 6 deletions src/webhooks.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
export const webhooks = http => ({
export default http => ({
get: args => getWebhook(http, args),
create: args => createWebhook(http, args),
update: args => updateWebhook(http, args),
delete: args => deleteWebhook(http, args)
})

export const getWebhook = (http, { uid, tag }) => {
const getWebhook = (http, { uid, tag }) => {
return http.request({
method: 'get',
url: `/forms/${uid}/webhooks/${tag}`
})
}

export const createWebhook = (http, args) => {
const createWebhook = (http, args) => {
return createOrUpdateWebhook(http, args)
}

export const updateWebhook = (http, args) => {
const updateWebhook = (http, args) => {
return createOrUpdateWebhook(http, args)
}

export const deleteWebhook = (http, { uid, tag }) => {
const deleteWebhook = (http, { uid, tag }) => {
return http.request({
method: 'delete',
url: `/forms/${uid}/webhooks/${tag}`
})
}

export const createOrUpdateWebhook = (
const createOrUpdateWebhook = (
http,
{ uid, tag, url, enable = false }
) => {
if (url === undefined) {
throw `Please provide an url for ${tag}`
}
if (tag === undefined) {
throw `Please provide a tag name for the webhook`
}
return http.request({
method: 'put',
url: `/forms/${uid}/webhooks/${tag}`,
Expand Down
19 changes: 10 additions & 9 deletions src/workspaces.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { isMemberPropValid, createMemberPatchQuery } from './utils'

export const workspaces = http => ({
export default http => ({
list: args => getWorkspaces(http, args),
get: args => getWorkspace(http, args),
add: args => addWorkspace(http, args),
update: args => updateWorkspace(http, args),
delete: args => deleteWorkspace(http, args),
addMembers: args => addMembers(http, args),
removeMembers: args => removeMembers(http, args)
})

export const getWorkspaces = (http, { search, page, page_size } = {}) => {
const getWorkspaces = (http, { search, page, page_size } = {}) => {
return http.request({
method: 'get',
url: '/workspaces',
Expand All @@ -21,14 +22,14 @@ export const getWorkspaces = (http, { search, page, page_size } = {}) => {
})
}

export const getWorkspace = (http, { id }) => {
const getWorkspace = (http, { id }) => {
return http.request({
method: 'get',
url: `/workspaces/${id}`
})
}

export const createWorkspace = (http, { name }) => {
const addWorkspace = (http, { name }) => {
if (name === undefined) {
throw `A name is required`
}
Expand All @@ -39,15 +40,15 @@ export const createWorkspace = (http, { name }) => {
})
}

export const updateWorkspace = (http, { id, data } = {}) => {
const updateWorkspace = (http, { id, data } = {}) => {
return http.request({
method: 'patch',
url: `/workspaces/${id}`,
data
})
}

export const addMembers = (http, { id, members }) => {
const addMembers = (http, { id, members }) => {
if (!isMemberPropValid(members)) {
throw `No member provided`
}
Expand All @@ -61,7 +62,7 @@ export const addMembers = (http, { id, members }) => {
return updateWorkspace(http, { id, data: membersQuery })
}

export const removeMembers = (http, { id, members }) => {
const removeMembers = (http, { id, members }) => {
if (!isMemberPropValid(members)) {
throw `No member provided`
}
Expand All @@ -75,14 +76,14 @@ export const removeMembers = (http, { id, members }) => {
return updateWorkspace(http, { id, data: membersQuery })
}

export const deleteWorkspace = (http, { id }) => {
const deleteWorkspace = (http, { id }) => {
return http.request({
method: 'delete',
url: `/workspaces/${id}`
})
}

export const getWorkspaceForms = (
const getWorkspaceForms = (
http,
{ id, from_id, page, page_size } = {}
) => {
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"forms": [
{
"id": "abc123",
"title": "test form",
"fields": []
},
{
"id": "xyz123",
"title": "test form 2",
"fields": []
}
]
}
Loading

0 comments on commit f6d3d49

Please sign in to comment.