Skip to content

Commit 82138e2

Browse files
committed
Move JSON:API media type header into library
1 parent f7395d8 commit 82138e2

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ const token = 'FILL_ME';
4848
const httpClient = axios.create({
4949
baseURL: 'https://jsonapi-sandbox.herokuapp.com',
5050
headers: {
51-
'Content-Type': 'application/vnd.api+json',
5251
'Authentication': `Bearer ${token}`,
5352
},
5453
});

src/ResourceClient.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const contentTypeHeaders = {
2+
headers: {'Content-Type': 'application/vnd.api+json'},
3+
};
4+
15
function filterQueryString(obj) {
26
return Object.keys(obj)
37
.map(k => `filter[${k}]=${encodeURIComponent(obj[k])}`)
@@ -84,7 +88,11 @@ class Resource {
8488
}
8589
const requestData = {data: record};
8690
return this.api
87-
.post(`${this.name}?${getOptionsQuery(options)}`, requestData)
91+
.post(
92+
`${this.name}?${getOptionsQuery(options)}`,
93+
requestData,
94+
contentTypeHeaders,
95+
)
8896
.then(extractData)
8997
.catch(extractErrorResponse);
9098
}
@@ -103,6 +111,7 @@ class Resource {
103111
.patch(
104112
`${this.name}/${record.id}?${getOptionsQuery(options)}`,
105113
requestData,
114+
contentTypeHeaders,
106115
)
107116
.then(extractData)
108117
.catch(extractErrorResponse);

test/ResourceClient.spec.js

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const ResourceClient = require('../src/ResourceClient');
22

33
describe('ResourceClient', () => {
4+
const contentTypeHeaders = {
5+
headers: {'Content-Type': 'application/vnd.api+json'},
6+
};
7+
48
const name = 'widgets';
59
let api;
610
let resource;
@@ -254,13 +258,17 @@ describe('ResourceClient', () => {
254258

255259
const result = resource.create({attributes, relationships});
256260

257-
expect(api.post).toHaveBeenCalledWith('widgets?', {
258-
data: {
259-
type: 'widgets',
260-
attributes,
261-
relationships,
261+
expect(api.post).toHaveBeenCalledWith(
262+
'widgets?',
263+
{
264+
data: {
265+
type: 'widgets',
266+
attributes,
267+
relationships,
268+
},
262269
},
263-
});
270+
contentTypeHeaders,
271+
);
264272
return expect(result).resolves.toEqual(responseBody);
265273
});
266274

@@ -274,9 +282,13 @@ describe('ResourceClient', () => {
274282
options: optionsWithInclude,
275283
});
276284

277-
expect(api.post).toHaveBeenCalledWith('widgets?include=comments', {
278-
data: {type: 'widgets', attributes, relationships},
279-
});
285+
expect(api.post).toHaveBeenCalledWith(
286+
'widgets?include=comments',
287+
{
288+
data: {type: 'widgets', attributes, relationships},
289+
},
290+
contentTypeHeaders,
291+
);
280292
});
281293

282294
it('rejects with the response upon error', () => {
@@ -302,9 +314,13 @@ describe('ResourceClient', () => {
302314

303315
const result = resource.update({id, attributes, relationships});
304316

305-
expect(api.patch).toHaveBeenCalledWith('widgets/1?', {
306-
data: {id, type: 'widgets', attributes, relationships},
307-
});
317+
expect(api.patch).toHaveBeenCalledWith(
318+
'widgets/1?',
319+
{
320+
data: {id, type: 'widgets', attributes, relationships},
321+
},
322+
contentTypeHeaders,
323+
);
308324
return expect(result).resolves.toEqual(responseBody);
309325
});
310326

@@ -319,9 +335,13 @@ describe('ResourceClient', () => {
319335
options: optionsWithInclude,
320336
});
321337

322-
expect(api.patch).toHaveBeenCalledWith('widgets/1?include=comments', {
323-
data: {id, type: 'widgets', attributes, relationships},
324-
});
338+
expect(api.patch).toHaveBeenCalledWith(
339+
'widgets/1?include=comments',
340+
{
341+
data: {id, type: 'widgets', attributes, relationships},
342+
},
343+
contentTypeHeaders,
344+
);
325345
});
326346

327347
it('rejects with the response upon error', () => {

0 commit comments

Comments
 (0)