Skip to content

Commit c6b9fac

Browse files
committed
fix: send data as body for the request
1 parent 43824d5 commit c6b9fac

9 files changed

+40
-30
lines changed

src/create-client.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const clientConstructor = ({ token, ...options }) => {
77
request: (args) => {
88
const {
99
url,
10+
data,
1011
headers: argsHeaders = {},
1112
...otherArgs
1213
} = args
@@ -22,6 +23,7 @@ export const clientConstructor = ({ token, ...options }) => {
2223

2324
return fetch(`${API_BASE_URL}${url}`, {
2425
...requestParameters,
26+
body: JSON.stringify(data),
2527
headers: {
2628
...headers,
2729
...argsHeaders,

src/forms.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ export default http => ({
1010
}
1111
})
1212

13-
const getForms = (http, { page, page_size, search } = {}) => {
13+
const getForms = (http, { page, pageSize, search } = {}) => {
1414
return http.request({
1515
method: 'get',
1616
url: `/forms`,
1717
page,
18-
page_size,
18+
page_size: pageSize,
1919
search
2020
})
2121
}

src/typeform.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import webhooks from './webhooks'
99

1010
export const createClient = (args = {}) => {
1111
if (args.token === undefined) {
12-
throw 'Token is missing'
12+
throw new Error('Token is missing')
1313
}
1414

1515
const http = clientConstructor(args)

src/webhooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ const createOrUpdateWebhook = (
3232
{ uid, tag, url, enable = false }
3333
) => {
3434
if (url === undefined) {
35-
throw `Please provide an url for ${tag}`
35+
throw new Error(`Please provide an url for ${tag}`)
3636
}
3737
if (tag === undefined) {
38-
throw `Please provide a tag name for the webhook`
38+
throw new Error(`Please provide a tag name for the webhook`)
3939
}
4040
return http.request({
4141
method: 'put',

tests/unit/forms.test.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,26 @@ test('getForm sets get method', () => {
3030

3131
test('updateForm sends the correct UID and data', () => {
3232
formsRequest.update({
33-
uid: 'abc123', data: {
33+
uid: 'abc123',
34+
data: {
3435
title: 'hola'
3536
}
3637
})
38+
const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
3739
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`)
38-
expect(fetch.mock.calls[0][1].data.title).toBe('hola')
40+
expect(bodyParsed.title).toBe('hola')
3941
})
4042

4143
test('updateForm sets patch method in request by default', () => {
4244
formsRequest.update({
43-
uid: 'abc123', data: {
45+
uid: 'abc123',
46+
data: {
4447
title: 'hola'
4548
}
4649
})
4750
expect(fetch.mock.calls[0][1].method).toBe('patch')
4851
})
4952

50-
5153
test('updateForm sets put method in request when override option is set', () => {
5254
formsRequest.update({
5355
uid: 'abc123',

tests/unit/images.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ test('adding an image pass the required values', () => {
3333
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/images`)
3434
expect(fetch.mock.calls[0][1].method).toBe('post')
3535

36-
const imageData = fetch.mock.calls[0][1].data
37-
expect(imageData).toEqual({
36+
const imageData = fetch.mock.calls[0][1].body
37+
expect(imageData).toEqual(JSON.stringify({
3838
image: 'bGRqZmxzZGpmbHNoZmtoc2RrZmpoc2tqZA==',
39-
media_type: 'image/gif',
40-
file_name: 'newimage.gif'
41-
})
39+
file_name: 'newimage.gif',
40+
media_type: 'image/gif'
41+
}))
4242
})
4343

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

tests/unit/teams.test.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ test('addMember will set the proper method', () => {
2424

2525
test('if a member is sent as string it will work as expected', () => {
2626
teamsRequest.addMembers({ members: '[email protected]' })
27-
expect(fetch.mock.calls[0][1].data).toEqual([
27+
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify([
2828
{
2929
op: 'add',
3030
path: '/members',
3131
value: {
3232
3333
}
3434
}
35-
])
35+
]))
3636
})
3737

3838
test('it will support array or multiple members at a time', () => {
3939
teamsRequest.addMembers({ members: ['[email protected]', '[email protected]'] })
40-
expect(fetch.mock.calls[0][1].data.length).toEqual(2)
41-
expect(fetch.mock.calls[0][1].data).toEqual([
40+
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify([
4241
{
4342
op: 'add',
4443
path: '/members',
@@ -53,28 +52,27 @@ test('it will support array or multiple members at a time', () => {
5352
5453
}
5554
}
56-
])
55+
]))
5756
})
5857

5958
test('if no members or incorrect format defined throws', () => {
6059
expect(() => teamsRequest.addMembers({ members: {} })).toThrow()
6160
})
6261

63-
//removeMember
6462
test('removeMember will set the proper method', () => {
6563
teamsRequest.removeMembers({ members: ['[email protected]'] })
6664
expect(fetch.mock.calls[0][1].method).toBe('delete')
6765
})
6866

6967
test('if a member is sent as string it will work as expected', () => {
7068
teamsRequest.removeMembers({ members: '[email protected]' })
71-
expect(fetch.mock.calls[0][1].data).toEqual([
69+
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify([
7270
{
7371
op: 'remove',
7472
path: '/members',
7573
value: {
7674
7775
}
7876
}
79-
])
77+
]))
8078
})

tests/unit/webhooks.test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ test('Create a new webhooks has the correct path, method and url', () => {
2626
url: 'http://test.com',
2727
enable: true
2828
})
29+
30+
const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
2931
expect(fetch.mock.calls[0][1].method).toBe('put')
3032
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/2/webhooks/test`)
31-
expect(fetch.mock.calls[0][1].data.url).toBe('http://test.com')
33+
expect(bodyParsed.url).toBe('http://test.com')
3234
})
3335

3436
test('Create a new webhooks requires a url', () => {
@@ -41,7 +43,9 @@ test('update a new webhooks sends the correct payload', () => {
4143
tag: 'test',
4244
url: 'http://example.com'
4345
})
44-
expect(fetch.mock.calls[0][1].data.url).toBe('http://example.com')
46+
47+
const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
48+
expect(bodyParsed.url).toBe('http://example.com')
4549
expect(fetch.mock.calls[0][1].method).toBe('put')
4650
})
4751

tests/unit/workspaces.test.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ test(`add a member to a workscape has the correct payload`, () => {
4848
id: 2,
4949
5050
})
51-
expect(fetch.mock.calls[0][1].data).toEqual([
51+
const body = fetch.mock.calls[0][1].body
52+
expect(body).toEqual(JSON.stringify([
5253
{
5354
op: 'add',
5455
path: '/members',
@@ -63,25 +64,28 @@ test(`add a member to a workscape has the correct payload`, () => {
6364
6465
}
6566
}
66-
])
67-
expect(fetch.mock.calls[0][1].data.length).toEqual(2)
67+
]))
68+
expect(JSON.parse(body).length).toEqual(2)
6869
})
6970

7071
test(`remove a member to a workscape has the correct payload`, () => {
7172
workspacesRequest.removeMembers({
7273
id: 2,
7374
members: ['[email protected]']
7475
})
75-
expect(fetch.mock.calls[0][1].data).toEqual([
76+
77+
const body = fetch.mock.calls[0][1].body
78+
79+
expect(body).toEqual(JSON.stringify([
7680
{
7781
op: 'remove',
7882
path: '/members',
7983
value: {
8084
8185
}
8286
}
83-
])
84-
expect(fetch.mock.calls[0][1].data.length).toEqual(1)
87+
]))
88+
expect(JSON.parse(body).length).toEqual(1)
8589
})
8690

8791
test(`Deleting a workscape has the correct path and method`, () => {

0 commit comments

Comments
 (0)