Skip to content

Commit 5c5844f

Browse files
authored
Merge pull request #8 from Typeform/fix-responses-after
Fix: parameters in requests
2 parents 5c577f3 + daf4b3a commit 5c5844f

12 files changed

+147
-47
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ Each one of them encapsulates the operations related to it (like listing, updati
8383

8484
### Forms
8585

86-
#### `forms.list({ page: 1, pageSize = 10, search = '' })`
86+
#### `forms.list({ page: 1, pageSize = 10, search = '', page })`
8787
- Get a list of your typeforms
88-
- Returns a list of typeform with the payload [refenced here](https://developer.typeform.com/create/reference/retrieve-forms/).
88+
- Returns a list of typeform with the payload [referenced here](https://developer.typeform.com/create/reference/retrieve-forms/).
8989

9090
#### `forms.get({ uid })`
9191
- Get a typeform by UID
92-
- Returns a typeform with the payload [refenced here](https://developer.typeform.com/create/reference/retrieve-form/).
92+
- Returns a typeform with the payload [referenced here](https://developer.typeform.com/create/reference/retrieve-form/).
9393

9494
#### `forms.update({ uid, data = {}, override = false })`
9595
- Get a typeform by UID
96-
- Returns a typeform with the payload [refenced here](https://developer.typeform.com/create/reference/retrieve-form/).
96+
- Returns a typeform with the payload [referenced here](https://developer.typeform.com/create/reference/retrieve-form/).
9797

9898
#### `forms.delete({ uid })`
9999

@@ -141,16 +141,16 @@ Each one of them encapsulates the operations related to it (like listing, updati
141141
#### `themes.list({ page, pageSize })`
142142
- Gets your themes collection
143143
- `page`: default `1`
144-
- `page_size: default `10`
144+
- `pageSize: default `10`
145145

146146
#### `themes.get({ id })`
147147
- Gets a theme for the given ID
148148

149-
#### `themes.create({ background, colors, font, has_transparent_button, name })`
149+
#### `themes.create({ background, colors, font, hasTransparentButton, name })`
150150
- Creates a theme with the given configuration
151151
- See more details of the payload in [the documentation](https://developer.typeform.com/create/reference/create-theme/)
152152

153-
#### `themes.update({ background, colors, font, has_transparent_button, name })`
153+
#### `themes.update({ background, colors, font, hasTransparentButton, name })`
154154
- Creates a theme with the given configuration
155155
- See more details of the payload in [the documentation](https://developer.typeform.com/create/reference/update-theme/)
156156

@@ -162,7 +162,7 @@ Each one of them encapsulates the operations related to it (like listing, updati
162162
#### `workspaces.list({ page, pageSize, search })`
163163
- Gets your workspaces
164164
- `page`: default `1`
165-
- `page_size: default `10`
165+
- `pageSize: default `10`
166166
- `search`: search a workspace that partially matches the search string
167167

168168
#### `workspaces.get({ id })`
@@ -190,7 +190,7 @@ Each one of them encapsulates the operations related to it (like listing, updati
190190

191191
### Responses
192192

193-
#### `responses.list({ uid, page_size, since, until, after, before, completed, sort, query, fields })`
193+
#### `responses.list({ uid, pageSize, since, until, after, before, completed, sort, query, fields })`
194194
- List responses from the given ID
195195
- `uid`: typeform UID
196196
- For parameter details check [the documentation](https://developer.typeform.com/responses/reference/retrieve-responses/)

src/create-client.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* globals fetch */
22
import 'isomorphic-fetch'
33
import { API_BASE_URL } from './constants'
4+
import { buildUrlWithParams } from './utils';
45

56
export const clientConstructor = ({ token, ...options }) => {
67
return {
@@ -9,9 +10,12 @@ export const clientConstructor = ({ token, ...options }) => {
910
url,
1011
data,
1112
headers: argsHeaders = {},
13+
params,
1214
...otherArgs
1315
} = args
1416

17+
const requestUrl = buildUrlWithParams(`${API_BASE_URL}${url}`, params)
18+
1519
const {
1620
headers = {}
1721
} = options
@@ -21,7 +25,7 @@ export const clientConstructor = ({ token, ...options }) => {
2125
...otherArgs
2226
}
2327

24-
return fetch(`${API_BASE_URL}${url}`, {
28+
return fetch(requestUrl, {
2529
...requestParameters,
2630
body: JSON.stringify(data),
2731
headers: {

src/forms.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ export default http => ({
1010
}
1111
})
1212

13-
const getForms = (http, { page, pageSize, search } = {}) => {
13+
const getForms = (http, { page, pageSize, search, workspaceId } = {}) => {
1414
return http.request({
1515
method: 'get',
1616
url: `/forms`,
17-
page,
18-
page_size: pageSize,
19-
search
17+
params: {
18+
page,
19+
page_size: pageSize,
20+
search,
21+
workspace_id: workspaceId
22+
}
2023
})
2124
}
2225

src/responses.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getResponses = (
66
http,
77
{
88
uid,
9-
page_size,
9+
pageSize,
1010
since,
1111
until,
1212
after,
@@ -20,14 +20,16 @@ const getResponses = (
2020
return http.request({
2121
method: 'get',
2222
url: `/forms/${uid}/responses`,
23-
page_size,
24-
since,
25-
until,
26-
after,
27-
before,
28-
completed,
29-
sort,
30-
query,
31-
fields
23+
params: {
24+
page_size: pageSize,
25+
since,
26+
until,
27+
after,
28+
before,
29+
completed,
30+
sort,
31+
query,
32+
fields
33+
}
3234
})
3335
}

src/themes.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default http => ({
88
update: args => updateTheme(http, args)
99
})
1010

11-
const getThemes = (http, { page, page_size } = {}) => {
11+
const getThemes = (http, { page, pageSize } = {}) => {
1212
return http.request({
1313
method: 'get',
1414
url: '/themes',
1515
params: {
1616
page,
17-
page_size
17+
page_size: pageSize
1818
}
1919
})
2020
}
@@ -28,9 +28,9 @@ const getTheme = (http, { id }) => {
2828

2929
const createTheme = (
3030
http,
31-
{ background, colors, font, has_transparent_button, name }
31+
{ background, colors, font, hasTransparentButton, name }
3232
) => {
33-
//check if required properties are defined
33+
// check if required properties are defined
3434
if ([name, font, colors].includes(undefined)) {
3535
throw `Please add the required fields`
3636
}
@@ -45,7 +45,7 @@ const createTheme = (
4545
background,
4646
colors,
4747
font,
48-
has_transparent_button,
48+
has_transparent_button: hasTransparentButton,
4949
name
5050
})
5151
}
@@ -59,9 +59,9 @@ const deleteTheme = (http, { id }) => {
5959

6060
const updateTheme = (
6161
http,
62-
{ id, background, colors, font, has_transparent_button, name }
62+
{ id, background, colors, font, hasTransparentButton, name }
6363
) => {
64-
//check if required properties are defined
64+
// check if required properties are defined
6565
if ([name, font, colors].includes(undefined)) {
6666
throw `Please add the required fields`
6767
}
@@ -76,7 +76,7 @@ const updateTheme = (
7676
background,
7777
colors,
7878
font,
79-
has_transparent_button,
79+
has_transparent_button: hasTransparentButton,
8080
name
8181
})
8282
}

src/utils.js

+9
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ export const createMemberPatchQuery = ({ members, operation }) => {
1919
}
2020
}))
2121
}
22+
23+
export const buildUrlWithParams = (url, params = {}) => {
24+
const queryParams = Object.keys(params)
25+
.filter((k) => params[k] !== undefined && params[k] !== null)
26+
.map(k => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
27+
.join('&')
28+
29+
return queryParams ? `${url}?${queryParams}` : url
30+
}

src/workspaces.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ export default http => ({
1010
removeMembers: args => removeMembers(http, args)
1111
})
1212

13-
const getWorkspaces = (http, { search, page, page_size } = {}) => {
13+
const getWorkspaces = (http, { search, page, pageSize } = {}) => {
1414
return http.request({
1515
method: 'get',
1616
url: '/workspaces',
1717
params: {
1818
page,
19-
page_size,
19+
page_size: pageSize,
2020
search
2121
}
2222
})
@@ -83,17 +83,17 @@ const deleteWorkspace = (http, { id }) => {
8383
})
8484
}
8585

86-
const getWorkspaceForms = (
86+
export const getWorkspaceForms = (
8787
http,
88-
{ id, from_id, page, page_size } = {}
88+
{ id, fromId, page, pageSize } = {}
8989
) => {
9090
return http.request({
9191
method: 'get',
9292
url: `/workspaces/${id}/forms`,
9393
params: {
9494
page,
95-
page_size,
96-
from_id
95+
page_size: pageSize,
96+
from_id: fromId
9797
}
9898
})
9999
}

tests/unit/forms.test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,27 @@ const formsRequest = forms(http)
1414

1515
test('get all forms has the correct method and path', () => {
1616
formsRequest.list()
17-
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms`)
17+
18+
const url = fetch.mock.calls[0][0].split('?')
19+
expect(url[0]).toBe(`${API_BASE_URL}/forms`)
1820
expect(fetch.mock.calls[0][1].method).toBe('get')
1921
})
2022

23+
test('paramters are sent correctly', () => {
24+
formsRequest.list({
25+
page: 2,
26+
pageSize: 10,
27+
search: 'hola',
28+
workspaceId: 'abc'
29+
})
30+
const url = fetch.mock.calls[0][0].split('?')
31+
const params = new URLSearchParams(url[1])
32+
expect(params.get('page')).toBe('2')
33+
expect(params.get('page')).toBe('2')
34+
expect(params.get('page')).toBe('2')
35+
expect(params.get('page')).toBe('2')
36+
})
37+
2138
test('getForm sends the correct UID', () => {
2239
formsRequest.get({ uid: 'abc123' })
2340
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`)

tests/unit/responses.test.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ beforeEach(() => {
77
fetch.mockResponse(JSON.stringify({}))
88
})
99

10+
const http = clientConstructor({
11+
token: '123'
12+
})
13+
14+
const responsesRequest = responses(http)
15+
1016
test('List responses has the correct path and method', () => {
11-
const http = clientConstructor({
12-
token: '123'
13-
})
14-
const responsesRequest = responses(http)
1517
responsesRequest.list({ uid: 2 })
1618
expect(fetch.mock.calls[0][1].method).toBe('get')
1719
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/2/responses`)
1820
})
21+
22+
test('List responses with the given filters', () => {
23+
responsesRequest.list({ uid: 2, pageSize: 15, after: '12345' })
24+
const params = (new URL(fetch.mock.calls[0][0])).searchParams
25+
expect(params.get('page_size')).toBe('15')
26+
expect(params.get('after')).toBe('12345')
27+
})

tests/unit/themes.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ test('Get themes has the correct path', () => {
2929
})
3030

3131
test('Get themes has the correct parameters', () => {
32-
themesRequest.list({ page: 3, page_size: 15 })
33-
expect(fetch.mock.calls[0][1].params.page).toBe(3)
34-
expect(fetch.mock.calls[0][1].params.page_size).toBe(15)
32+
themesRequest.list({ page: 3, pageSize: 15 })
33+
const params = (new URL(fetch.mock.calls[0][0])).searchParams
34+
expect(params.get('page')).toBe('3')
35+
expect(params.get('page_size')).toBe('15')
3536
})
3637

3738
test('Get themes has the correct path', () => {

tests/unit/utils.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { buildUrlWithParams } from '../../src/utils'
2+
3+
test('the url reminds the same if no parameter passed', () => {
4+
const url = 'http://typeform.com'
5+
expect(buildUrlWithParams(url)).toBe(url)
6+
})
7+
8+
test('the url has a query string if parameters are passed', () => {
9+
const url = 'http://typeform.com'
10+
const params = {
11+
a: '1',
12+
b: '2'
13+
}
14+
expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=1&b=2')
15+
})
16+
17+
test('parameters should be enconded', () => {
18+
const url = 'http://typeform.com'
19+
const params = {
20+
a: '@1',
21+
b: '#2'
22+
}
23+
expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401&b=%232')
24+
})
25+
26+
test('undefined values for parameter will be skipped', () => {
27+
const url = 'http://typeform.com'
28+
const params = {
29+
a: '@1',
30+
b: undefined
31+
}
32+
expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401')
33+
})
34+
35+
test('falsy values should be passed', () => {
36+
const url = 'http://typeform.com'
37+
const params = {
38+
a: '0',
39+
b: 0,
40+
c: null
41+
}
42+
expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=0&b=0')
43+
})

tests/unit/workspaces.test.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ const http = clientConstructor({
1212
})
1313
const workspacesRequest = workspaces(http)
1414

15-
1615
test(`Get workspaces has the correct path`, () => {
1716
workspacesRequest.list()
1817
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/workspaces`)
1918
})
2019

20+
test(`Get workspaces has the correct query parameters`, () => {
21+
workspacesRequest.list({
22+
search: 'hola',
23+
page: 2,
24+
pageSize: 10
25+
})
26+
27+
const params = new URL(fetch.mock.calls[0][0]).searchParams
28+
expect(params.get('search')).toBe('hola')
29+
expect(params.get('page')).toBe('2')
30+
expect(params.get('page_size')).toBe('10')
31+
})
32+
2133
test(`Get specific workscape has the correct path and method`, () => {
2234
workspacesRequest.get({ id: 2 })
2335
expect(fetch.mock.calls[0][1].method).toBe(`get`)

0 commit comments

Comments
 (0)