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

Fixes for release #6

Merged
merged 5 commits into from
Aug 17, 2018
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
13 changes: 0 additions & 13 deletions .eslintrc.json

This file was deleted.

4 changes: 0 additions & 4 deletions .prettierrc.json

This file was deleted.

27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,32 @@
"isomorphic-fetch": "^2.2.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.44",
"@babel/core": "^7.0.0-beta.44",
"@babel/node": "^7.0.0-beta.44",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.44",
"@babel/preset-env": "^7.0.0-beta.44",
"@babel/cli": "^7.0.0-rc.1",
"@babel/core": "^7.0.0-rc.1",
"@babel/node": "^7.0.0-rc.1",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-rc.1",
"@babel/preset-env": "^7.0.0-rc.1",
"@commitlint/cli": "^6.1.3",
"@commitlint/config-conventional": "^6.1.3",
"axios-mock-adapter": "^1.15.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^8.2.3",
"babel-jest": "^22.4.3",
"babel-loader": "^7.1.4",
"eslint": "^4.19.1",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
"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",
"standard": "^11.0.1",
"webpack": "^4.6.0",
"webpack-cli": "^2.0.14"
},
"lint-staged": {
"*.js": [
"eslint",
"standard",
"git add"
]
},
Expand All @@ -78,10 +73,16 @@
"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",
"lint": "standard ./src",
"semantic-release": "semantic-release",
"server": "node ./tests/integration/mockServer.js",
"server:dev": "nodemon ./tests/integration/mockServer.js"
},
"standard": {
"env": [
"jest"
],
"globals": ["fetch"]
},
"peerDependencies": {}
}
17 changes: 13 additions & 4 deletions src/create-client.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
/* globals fetch */
import 'isomorphic-fetch'
import { API_BASE_URL } from './constants'

export const clientConstructor = ({ token, ...options }) => {
return {
request: (args) => {

const {
url,
data,
headers: argsHeaders = {},
...otherArgs
} = args

const {
headers = {}
} = options

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

return fetch(`${API_BASE_URL}${url}`, {
...requestParameters,
body: JSON.stringify(data),
headers: {
...headers,
...argsHeaders,
Authorization: `bearer ${token}`
},
...requestParameters
}
})
.then(response => response.json())
// .catch(error => { throw `${error}` })
.catch(error => { throw new Error(error) })
}
}
}
4 changes: 2 additions & 2 deletions src/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default http => ({
}
})

const getForms = (http, { page, page_size, search } = {}) => {
const getForms = (http, { page, pageSize, search } = {}) => {
return http.request({
method: 'get',
url: `/forms`,
page,
page_size,
page_size: pageSize,
search
})
}
Expand Down
20 changes: 10 additions & 10 deletions src/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ export default http => ({
add: args => addImage(http, args),
delete: args => deleteImage(http, args)
})
export const getImages = http => {
const getImages = http => {
return http.request({
method: 'get',
url: '/images'
})
}

export const getImage = (
const getImage = (
http,
{ id, returns, size, backgroundSize, choiceSize }
) => {
Expand All @@ -21,7 +21,7 @@ export const getImage = (
}

if (returns === 'json') {
requestQuery['headers'] = {
requestQuery.headers = {
Accept: 'application/json'
}
}
Expand All @@ -30,15 +30,15 @@ export const getImage = (
if (['default', 'thumbnail', 'mobile'].includes(size)) {
requestQuery['url'] += `/image/${size}`
} else {
throw `Image size doesn't exists`
throw new Error(`Image size doesn't exists`)
}
}

if (backgroundSize !== undefined) {
if (['default', 'thumbnail', 'mobile', 'tablet'].includes(backgroundSize)) {
requestQuery['url'] += `/background/${backgroundSize}`
} else {
throw `Image background size doesn't exists`
throw new Error(`Image background size doesn't exists`)
}
}

Expand All @@ -54,26 +54,26 @@ export const getImage = (
if (choiceImageSizes.includes(choiceSize)) {
requestQuery['url'] += `/choice/${choiceSize}`
} else {
throw `Image choice size doesn't exists`
throw new Error(`Image choice size doesn't exists`)
}
}

return http.request(requestQuery)
}

export const addImage = (http, { image, media_type, file_name }) => {
const addImage = (http, { image, mediaType, fileName }) => {
return http.request({
method: 'post',
url: `/images`,
data: {
image,
file_name,
media_type
file_name: fileName,
media_type: mediaType
}
})
}

export const deleteImage = (http, { id }) => {
const deleteImage = (http, { id }) => {
return http.request({
method: 'delete',
url: `/images/${id}`
Expand Down
2 changes: 1 addition & 1 deletion src/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default http => ({
list: args => getResponses(http, args)
})

export const getResponses = (
const getResponses = (
http,
{
uid,
Expand Down
6 changes: 3 additions & 3 deletions src/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default http => {
}
}

export const getTeam = http => {
const getTeam = http => {
return http.request({
method: 'get',
url: '/teams/mine'
})
}

export const addMembers = (http, { members }) => {
const addMembers = (http, { members }) => {
if (!isMemberPropValid(members)) {
throw `No member provided`
}
Expand All @@ -39,7 +39,7 @@ export const addMembers = (http, { members }) => {
})
}

export const removeMembers = (http, { members }) => {
const removeMembers = (http, { members }) => {
if (!isMemberPropValid(members)) {
throw `No member provided`
}
Expand Down
10 changes: 5 additions & 5 deletions src/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default http => ({
update: args => updateTheme(http, args)
})

export const getThemes = (http, { page, page_size } = {}) => {
const getThemes = (http, { page, page_size } = {}) => {
return http.request({
method: 'get',
url: '/themes',
Expand All @@ -19,14 +19,14 @@ export const getThemes = (http, { page, page_size } = {}) => {
})
}

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

export const createTheme = (
const createTheme = (
http,
{ background, colors, font, has_transparent_button, name }
) => {
Expand All @@ -50,14 +50,14 @@ export const createTheme = (
})
}

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

export const updateTheme = (
const updateTheme = (
http,
{ id, background, colors, font, has_transparent_button, name }
) => {
Expand Down
2 changes: 1 addition & 1 deletion src/typeform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import webhooks from './webhooks'

export const createClient = (args = {}) => {
if (args.token === undefined) {
throw 'Token is missing'
throw new Error('Token is missing')
}

const http = clientConstructor(args)
Expand Down
4 changes: 2 additions & 2 deletions src/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const createOrUpdateWebhook = (
{ uid, tag, url, enable = false }
) => {
if (url === undefined) {
throw `Please provide an url for ${tag}`
throw new Error(`Please provide an url for ${tag}`)
}
if (tag === undefined) {
throw `Please provide a tag name for the webhook`
throw new Error(`Please provide a tag name for the webhook`)
}
return http.request({
method: 'put',
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/create-client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { clientConstructor } from '../../src/create-client'
import { API_BASE_URL } from '../../src/constants'

beforeEach(() => {
fetch.resetMocks()
fetch.mockResponse(JSON.stringify({}))
})

const client = clientConstructor({
token: 'abc'
})

test('request pass correct headers', () => {
client.request({
url: '/forms',
headers: {
Accepts: 'application/json'
}
})

expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms`)
expect(fetch.mock.calls[0][1].headers).toEqual({
Accepts: 'application/json',
Authorization: 'bearer abc'
})
})
10 changes: 6 additions & 4 deletions tests/unit/forms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,26 @@ test('getForm sets get method', () => {

test('updateForm sends the correct UID and data', () => {
formsRequest.update({
uid: 'abc123', data: {
uid: 'abc123',
data: {
title: 'hola'
}
})
const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`)
expect(fetch.mock.calls[0][1].data.title).toBe('hola')
expect(bodyParsed.title).toBe('hola')
})

test('updateForm sets patch method in request by default', () => {
formsRequest.update({
uid: 'abc123', data: {
uid: 'abc123',
data: {
title: 'hola'
}
})
expect(fetch.mock.calls[0][1].method).toBe('patch')
})


test('updateForm sets put method in request when override option is set', () => {
formsRequest.update({
uid: 'abc123',
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/images.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ test('get images by ID', () => {
test('adding an image pass the required values', () => {
imagesRequest.add({
image: 'bGRqZmxzZGpmbHNoZmtoc2RrZmpoc2tqZA==',
media_type: 'image/gif',
file_name: 'newimage.gif'
mediaType: 'image/gif',
fileName: 'newimage.gif'
})

expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/images`)
expect(fetch.mock.calls[0][1].method).toBe('post')

const imageData = fetch.mock.calls[0][1].data
expect(imageData).toEqual({
const imageData = fetch.mock.calls[0][1].body
expect(imageData).toEqual(JSON.stringify({
image: 'bGRqZmxzZGpmbHNoZmtoc2RrZmpoc2tqZA==',
media_type: 'image/gif',
file_name: 'newimage.gif'
})
file_name: 'newimage.gif',
media_type: 'image/gif'
}))
})

test('deleting an image sets the correct method and id', () => {
Expand Down
Loading