From 7d466ddcaf43066f7ccb031124b710569d03d65f Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 18:17:56 +0800 Subject: [PATCH 01/11] refactor: change parameter to camelcase --- README.md | 10 +++++----- src/responses.js | 4 ++-- src/themes.js | 16 ++++++++-------- src/workspaces.js | 12 ++++++------ tests/unit/themes.test.js | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c502ca2..5003e36 100644 --- a/README.md +++ b/README.md @@ -141,16 +141,16 @@ Each one of them encapsulates the operations related to it (like listing, updati #### `themes.list({ page, pageSize })` - Gets your themes collection - `page`: default `1` -- `page_size: default `10` +- `pageSize: default `10` #### `themes.get({ id })` - Gets a theme for the given ID -#### `themes.create({ background, colors, font, has_transparent_button, name })` +#### `themes.create({ background, colors, font, hasTransparentButton, name })` - Creates a theme with the given configuration - See more details of the payload in [the documentation](https://developer.typeform.com/create/reference/create-theme/) -#### `themes.update({ background, colors, font, has_transparent_button, name })` +#### `themes.update({ background, colors, font, hasTransparentButton, name })` - Creates a theme with the given configuration - See more details of the payload in [the documentation](https://developer.typeform.com/create/reference/update-theme/) @@ -162,7 +162,7 @@ Each one of them encapsulates the operations related to it (like listing, updati #### `workspaces.list({ page, pageSize, search })` - Gets your workspaces - `page`: default `1` -- `page_size: default `10` +- `pageSize: default `10` - `search`: search a workspace that partially matches the search string #### `workspaces.get({ id })` @@ -190,7 +190,7 @@ Each one of them encapsulates the operations related to it (like listing, updati ### Responses -#### `responses.list({ uid, page_size, since, until, after, before, completed, sort, query, fields })` +#### `responses.list({ uid, pageSize, since, until, after, before, completed, sort, query, fields })` - List responses from the given ID - `uid`: typeform UID - For parameter details check [the documentation](https://developer.typeform.com/responses/reference/retrieve-responses/) diff --git a/src/responses.js b/src/responses.js index e7dd670..0a4e7c3 100644 --- a/src/responses.js +++ b/src/responses.js @@ -6,7 +6,7 @@ const getResponses = ( http, { uid, - page_size, + pageSize, since, until, after, @@ -20,7 +20,7 @@ const getResponses = ( return http.request({ method: 'get', url: `/forms/${uid}/responses`, - page_size, + page_size: pageSize, since, until, after, diff --git a/src/themes.js b/src/themes.js index d542f7d..544ee7b 100644 --- a/src/themes.js +++ b/src/themes.js @@ -8,13 +8,13 @@ export default http => ({ update: args => updateTheme(http, args) }) -const getThemes = (http, { page, page_size } = {}) => { +const getThemes = (http, { page, pageSize } = {}) => { return http.request({ method: 'get', url: '/themes', params: { page, - page_size + pageSize } }) } @@ -28,9 +28,9 @@ const getTheme = (http, { id }) => { const createTheme = ( http, - { background, colors, font, has_transparent_button, name } + { background, colors, font, hasTransparentButton, name } ) => { - //check if required properties are defined + // check if required properties are defined if ([name, font, colors].includes(undefined)) { throw `Please add the required fields` } @@ -45,7 +45,7 @@ const createTheme = ( background, colors, font, - has_transparent_button, + has_transparent_button: hasTransparentButton, name }) } @@ -59,9 +59,9 @@ const deleteTheme = (http, { id }) => { const updateTheme = ( http, - { id, background, colors, font, has_transparent_button, name } + { id, background, colors, font, hasTransparentButton, name } ) => { - //check if required properties are defined + // check if required properties are defined if ([name, font, colors].includes(undefined)) { throw `Please add the required fields` } @@ -76,7 +76,7 @@ const updateTheme = ( background, colors, font, - has_transparent_button, + has_transparent_button: hasTransparentButton, name }) } diff --git a/src/workspaces.js b/src/workspaces.js index 6a39190..9e999f9 100644 --- a/src/workspaces.js +++ b/src/workspaces.js @@ -10,13 +10,13 @@ export default http => ({ removeMembers: args => removeMembers(http, args) }) -const getWorkspaces = (http, { search, page, page_size } = {}) => { +const getWorkspaces = (http, { search, page, pageSize } = {}) => { return http.request({ method: 'get', url: '/workspaces', params: { page, - page_size, + page_size: pageSize, search } }) @@ -83,17 +83,17 @@ const deleteWorkspace = (http, { id }) => { }) } -const getWorkspaceForms = ( +export const getWorkspaceForms = ( http, - { id, from_id, page, page_size } = {} + { id, fromId, page, pageSize } = {} ) => { return http.request({ method: 'get', url: `/workspaces/${id}/forms`, params: { page, - page_size, - from_id + page_size: pageSize, + from_id: fromId } }) } diff --git a/tests/unit/themes.test.js b/tests/unit/themes.test.js index eac3ef5..c471dd7 100644 --- a/tests/unit/themes.test.js +++ b/tests/unit/themes.test.js @@ -29,7 +29,7 @@ test('Get themes has the correct path', () => { }) test('Get themes has the correct parameters', () => { - themesRequest.list({ page: 3, page_size: 15 }) + themesRequest.list({ page: 3, pageSize: 15 }) expect(fetch.mock.calls[0][1].params.page).toBe(3) expect(fetch.mock.calls[0][1].params.page_size).toBe(15) }) From 233286b4bfd27df26f3cb698ff9a6cf22a6ebac4 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 18:28:46 +0800 Subject: [PATCH 02/11] fix: camelcased parameter --- src/themes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/themes.js b/src/themes.js index 544ee7b..3f60ebb 100644 --- a/src/themes.js +++ b/src/themes.js @@ -14,7 +14,7 @@ const getThemes = (http, { page, pageSize } = {}) => { url: '/themes', params: { page, - pageSize + page_size: pageSize } }) } From ab78174fb9754d678172ad944041aae0df1f2b21 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 19:01:46 +0800 Subject: [PATCH 03/11] feat: add parameters as part of the url for fetch --- src/create-client.js | 6 +++++- src/utils.js | 18 ++++++++++++++++++ tests/unit/utils.test.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/unit/utils.test.js diff --git a/src/create-client.js b/src/create-client.js index 1cc7e1c..1a45ecb 100644 --- a/src/create-client.js +++ b/src/create-client.js @@ -1,6 +1,7 @@ /* globals fetch */ import 'isomorphic-fetch' import { API_BASE_URL } from './constants' +import { buildUrlWithParams } from './utils'; export const clientConstructor = ({ token, ...options }) => { return { @@ -9,9 +10,12 @@ export const clientConstructor = ({ token, ...options }) => { url, data, headers: argsHeaders = {}, + params, ...otherArgs } = args + const requestUrl = buildUrlWithParams(`${API_BASE_URL}${url}`, params) + const { headers = {} } = options @@ -21,7 +25,7 @@ export const clientConstructor = ({ token, ...options }) => { ...otherArgs } - return fetch(`${API_BASE_URL}${url}`, { + return fetch(requestUrl, { ...requestParameters, body: JSON.stringify(data), headers: { diff --git a/src/utils.js b/src/utils.js index 6c6293f..3bb6b5e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -19,3 +19,21 @@ export const createMemberPatchQuery = ({ members, operation }) => { } })) } + +export const buildUrlWithParams = (url, params = {}) => { + const queryParams = Object.keys(params) + .reduce((list, key) => { + const currentValue = params[key] + if (currentValue) { + return [ + ...list, + `${encodeURIComponent(key)}=${encodeURIComponent(currentValue)}` + ] + } else { + return list + } + }, []) + .join('&') + + return queryParams ? `${url}?${queryParams}` : url +} diff --git a/tests/unit/utils.test.js b/tests/unit/utils.test.js new file mode 100644 index 0000000..d7e6c30 --- /dev/null +++ b/tests/unit/utils.test.js @@ -0,0 +1,33 @@ +import { buildUrlWithParams } from '../../src/utils' + +test('the url reminds the same if no parameter passed', () => { + const url = 'http://typeform.com' + expect(buildUrlWithParams(url)).toBe(url) +}) + +test('the url has a query string if parameters are passed', () => { + const url = 'http://typeform.com' + const params = { + a: '1', + b: '2' + } + expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=1&b=2') +}) + +test('parameters should be enconded', () => { + const url = 'http://typeform.com' + const params = { + a: '@1', + b: '#2' + } + expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401&b=%232') +}) + +test.only('undefined values for parameter will be skipped', () => { + const url = 'http://typeform.com' + const params = { + a: '@1', + b: undefined + } + expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401') +}) From 09181cb19466dc1740fd65930bfefcad04ec2185 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 19:02:32 +0800 Subject: [PATCH 04/11] fix: send filter parameters to responses --- src/responses.js | 20 +++++++++++--------- tests/unit/responses.test.js | 11 +++++++++++ tests/unit/themes.test.js | 5 +++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/responses.js b/src/responses.js index 0a4e7c3..bfb1c97 100644 --- a/src/responses.js +++ b/src/responses.js @@ -20,14 +20,16 @@ const getResponses = ( return http.request({ method: 'get', url: `/forms/${uid}/responses`, - page_size: pageSize, - since, - until, - after, - before, - completed, - sort, - query, - fields + params: { + page_size: pageSize, + since, + until, + after, + before, + completed, + sort, + query, + fields + } }) } diff --git a/tests/unit/responses.test.js b/tests/unit/responses.test.js index a982336..837799e 100644 --- a/tests/unit/responses.test.js +++ b/tests/unit/responses.test.js @@ -16,3 +16,14 @@ test('List responses has the correct path and method', () => { expect(fetch.mock.calls[0][1].method).toBe('get') expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/2/responses`) }) + +test('List responses with the given filters', () => { + const http = clientConstructor({ + token: '123', + }) + const responsesRequest = responses(http) + responsesRequest.list({ uid: 2, pageSize: 15, after: '12345' }) + const params = (new URL(fetch.mock.calls[0][0])).searchParams + expect(params.get('page_size')).toBe('15') + expect(params.get('after')).toBe('12345') +}) diff --git a/tests/unit/themes.test.js b/tests/unit/themes.test.js index c471dd7..5b0dc7a 100644 --- a/tests/unit/themes.test.js +++ b/tests/unit/themes.test.js @@ -30,8 +30,9 @@ test('Get themes has the correct path', () => { test('Get themes has the correct parameters', () => { themesRequest.list({ page: 3, pageSize: 15 }) - expect(fetch.mock.calls[0][1].params.page).toBe(3) - expect(fetch.mock.calls[0][1].params.page_size).toBe(15) + const params = (new URL(fetch.mock.calls[0][0])).searchParams + expect(params.get('page')).toBe('3') + expect(params.get('page_size')).toBe('15') }) test('Get themes has the correct path', () => { From 665ad34e8987a5e8ae19138d567ca5f759a7301c Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 19:12:41 +0800 Subject: [PATCH 05/11] fix: send parameters to forms request --- src/forms.js | 11 +++++++---- tests/unit/forms.test.js | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/forms.js b/src/forms.js index 8c20e51..7400e8b 100644 --- a/src/forms.js +++ b/src/forms.js @@ -10,13 +10,16 @@ export default http => ({ } }) -const getForms = (http, { page, pageSize, search } = {}) => { +const getForms = (http, { page, pageSize, search, workspaceId } = {}) => { return http.request({ method: 'get', url: `/forms`, - page, - page_size: pageSize, - search + params: { + page, + page_size: pageSize, + search, + workspace_id: workspaceId + } }) } diff --git a/tests/unit/forms.test.js b/tests/unit/forms.test.js index 1ee43d2..428c999 100644 --- a/tests/unit/forms.test.js +++ b/tests/unit/forms.test.js @@ -14,10 +14,27 @@ const formsRequest = forms(http) test('get all forms has the correct method and path', () => { formsRequest.list() - expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms`) + + const url = fetch.mock.calls[0][0].split('?') + expect(url[0]).toBe(`${API_BASE_URL}/forms`) expect(fetch.mock.calls[0][1].method).toBe('get') }) +test('paramters are sent correctly', () => { + formsRequest.list({ + page: 2, + pageSize: 10, + search: 'hola', + workspaceId: 'abc' + }) + const url = fetch.mock.calls[0][0].split('?') + const params = new URLSearchParams(url[1]) + expect(params.get('page')).toBe('2') + expect(params.get('page')).toBe('2') + expect(params.get('page')).toBe('2') + expect(params.get('page')).toBe('2') +}) + test('getForm sends the correct UID', () => { formsRequest.get({ uid: 'abc123' }) expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`) From 2c979f014fbff7c5cba149b376e8c9618047757c Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 19:19:20 +0800 Subject: [PATCH 06/11] chore: move method outside the test condition --- tests/unit/responses.test.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/unit/responses.test.js b/tests/unit/responses.test.js index 837799e..b421b6a 100644 --- a/tests/unit/responses.test.js +++ b/tests/unit/responses.test.js @@ -7,21 +7,19 @@ beforeEach(() => { fetch.mockResponse(JSON.stringify({})) }) +const http = clientConstructor({ + token: '123' +}) + +const responsesRequest = responses(http) + test('List responses has the correct path and method', () => { - const http = clientConstructor({ - token: '123' - }) - const responsesRequest = responses(http) responsesRequest.list({ uid: 2 }) expect(fetch.mock.calls[0][1].method).toBe('get') expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/2/responses`) }) test('List responses with the given filters', () => { - const http = clientConstructor({ - token: '123', - }) - const responsesRequest = responses(http) responsesRequest.list({ uid: 2, pageSize: 15, after: '12345' }) const params = (new URL(fetch.mock.calls[0][0])).searchParams expect(params.get('page_size')).toBe('15') From e12fe9cdff8f039cea6a888a1acfd75755ce6f28 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 19:19:35 +0800 Subject: [PATCH 07/11] test: add coverage to parameters --- tests/unit/workspaces.test.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/unit/workspaces.test.js b/tests/unit/workspaces.test.js index c158c36..4f69bb7 100644 --- a/tests/unit/workspaces.test.js +++ b/tests/unit/workspaces.test.js @@ -12,12 +12,24 @@ const http = clientConstructor({ }) const workspacesRequest = workspaces(http) - test(`Get workspaces has the correct path`, () => { workspacesRequest.list() expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/workspaces`) }) +test(`Get workspaces has the correct query parameters`, () => { + workspacesRequest.list({ + search: 'hola', + page: 2, + pageSize: 10 + }) + + const params = new URL(fetch.mock.calls[0][0]).searchParams + expect(params.get('search')).toBe('hola') + expect(params.get('page')).toBe('2') + expect(params.get('page_size')).toBe('10') +}) + test(`Get specific workscape has the correct path and method`, () => { workspacesRequest.get({ id: 2 }) expect(fetch.mock.calls[0][1].method).toBe(`get`) From 03be8e705a22cff8d74bda483fbdd52d233e88c7 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 19:22:12 +0800 Subject: [PATCH 08/11] docs: update parameters --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5003e36..8577af9 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Each one of them encapsulates the operations related to it (like listing, updati ### Forms -#### `forms.list({ page: 1, pageSize = 10, search = '' })` +#### `forms.list({ page: 1, pageSize = 10, search = '', page })` - Get a list of your typeforms - Returns a list of typeform with the payload [refenced here](https://developer.typeform.com/create/reference/retrieve-forms/). From 69f28323f76c6400abd3e4ef4d4cc70345794e07 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Fri, 21 Sep 2018 19:38:47 +0800 Subject: [PATCH 09/11] chore: typos in reference --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8577af9..3159d39 100644 --- a/README.md +++ b/README.md @@ -85,15 +85,15 @@ Each one of them encapsulates the operations related to it (like listing, updati #### `forms.list({ page: 1, pageSize = 10, search = '', page })` - Get a list of your typeforms -- Returns a list of typeform with the payload [refenced here](https://developer.typeform.com/create/reference/retrieve-forms/). +- Returns a list of typeform with the payload [referenced here](https://developer.typeform.com/create/reference/retrieve-forms/). #### `forms.get({ uid })` - Get a typeform by UID -- Returns a typeform with the payload [refenced here](https://developer.typeform.com/create/reference/retrieve-form/). +- Returns a typeform with the payload [referenced here](https://developer.typeform.com/create/reference/retrieve-form/). #### `forms.update({ uid, data = {}, override = false })` - Get a typeform by UID -- Returns a typeform with the payload [refenced here](https://developer.typeform.com/create/reference/retrieve-form/). +- Returns a typeform with the payload [referenced here](https://developer.typeform.com/create/reference/retrieve-form/). #### `forms.delete({ uid })` From eded16c54395a236d7f5ce3b8bf18918eda48235 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Sat, 22 Sep 2018 11:18:19 +0800 Subject: [PATCH 10/11] chore: support 0 as value and remove null from values --- src/utils.js | 2 +- tests/unit/utils.test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 3bb6b5e..83f814e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -24,7 +24,7 @@ export const buildUrlWithParams = (url, params = {}) => { const queryParams = Object.keys(params) .reduce((list, key) => { const currentValue = params[key] - if (currentValue) { + if (currentValue !== undefined && currentValue !== null) { return [ ...list, `${encodeURIComponent(key)}=${encodeURIComponent(currentValue)}` diff --git a/tests/unit/utils.test.js b/tests/unit/utils.test.js index d7e6c30..a46ebbe 100644 --- a/tests/unit/utils.test.js +++ b/tests/unit/utils.test.js @@ -31,3 +31,13 @@ test.only('undefined values for parameter will be skipped', () => { } expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401') }) + +test.only('falsy values should be passed', () => { + const url = 'http://typeform.com' + const params = { + a: '0', + b: 0, + c: null + } + expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=0&b=0') +}) From daf4b3a7f8931ba6a74e45ae9b10ddde3b8a9881 Mon Sep 17 00:00:00 2001 From: Jepser Bernardino Date: Sat, 22 Sep 2018 11:23:28 +0800 Subject: [PATCH 11/11] refactor: simplify query params --- src/utils.js | 13 ++----------- tests/unit/utils.test.js | 4 ++-- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/utils.js b/src/utils.js index 83f814e..c5010de 100644 --- a/src/utils.js +++ b/src/utils.js @@ -22,17 +22,8 @@ export const createMemberPatchQuery = ({ members, operation }) => { export const buildUrlWithParams = (url, params = {}) => { const queryParams = Object.keys(params) - .reduce((list, key) => { - const currentValue = params[key] - if (currentValue !== undefined && currentValue !== null) { - return [ - ...list, - `${encodeURIComponent(key)}=${encodeURIComponent(currentValue)}` - ] - } else { - return list - } - }, []) + .filter((k) => params[k] !== undefined && params[k] !== null) + .map(k => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`) .join('&') return queryParams ? `${url}?${queryParams}` : url diff --git a/tests/unit/utils.test.js b/tests/unit/utils.test.js index a46ebbe..0045253 100644 --- a/tests/unit/utils.test.js +++ b/tests/unit/utils.test.js @@ -23,7 +23,7 @@ test('parameters should be enconded', () => { expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401&b=%232') }) -test.only('undefined values for parameter will be skipped', () => { +test('undefined values for parameter will be skipped', () => { const url = 'http://typeform.com' const params = { a: '@1', @@ -32,7 +32,7 @@ test.only('undefined values for parameter will be skipped', () => { expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401') }) -test.only('falsy values should be passed', () => { +test('falsy values should be passed', () => { const url = 'http://typeform.com' const params = { a: '0',