Skip to content

Commit 2e4dc9e

Browse files
justin-taywopian
andauthored
feat(kitsu): add axios options for all operations (#623)
Co-authored-by: James Harris <wopian@wopian.me>
1 parent d77aa4a commit 2e4dc9e

8 files changed

Lines changed: 79 additions & 8 deletions

File tree

packages/kitsu/src/delete.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ afterEach(() => {
1010

1111
describe('kitsu', () => {
1212
describe('delete', () => {
13+
it('uses provided axios options', async () => {
14+
expect.assertions(1)
15+
const api = new Kitsu()
16+
api.axios = { delete: jest.fn().mockReturnValue({ data: '' }) }
17+
await api.delete('anime', 1, { axiosOptions: { withCredentials: true } })
18+
expect(api.axios.delete).toHaveBeenCalledWith('anime/1', expect.objectContaining({ withCredentials: true }))
19+
})
20+
1321
it('sends and recieves headers', async () => {
1422
expect.assertions(2)
1523
const api = new Kitsu({ headers: { Authorization: true } })

packages/kitsu/src/get.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ afterEach(() => {
1919

2020
describe('kitsu', () => {
2121
describe('get', () => {
22+
it('uses provided axios options', async () => {
23+
expect.assertions(1)
24+
const api = new Kitsu()
25+
api.axios = { get: jest.fn().mockReturnValue({ data: '' }) }
26+
await api.get('anime', { axiosOptions: { withCredentials: true } })
27+
expect(api.axios.get).toHaveBeenCalledWith('anime', expect.objectContaining({ withCredentials: true }))
28+
})
29+
2230
it('sends and recieves headers', async () => {
2331
expect.assertions(2)
2432
const api = new Kitsu({ headers: { init: true } })

packages/kitsu/src/index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export default class Kitsu {
132132
* @param {number} [config.params.page.size] Number of resources to return in request (Page-based and cursor-based) - **Note:** Not supported on Kitsu.io
133133
* @param {string} [config.params.page.before] Get the previous page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
134134
* @param {string} [config.params.page.after] Get the next page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
135+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
135136
* @returns {Object} JSON-parsed response
136137
* @example <caption>Getting a resource with JSON:API parameters</caption>
137138
* api.get('users', {
@@ -228,7 +229,8 @@ export default class Kitsu {
228229
const { data, headers: responseHeaders } = await this.axios.get(url, {
229230
headers,
230231
params,
231-
paramsSerializer: /* istanbul ignore next */ p => query(p)
232+
paramsSerializer: /* istanbul ignore next */ p => query(p),
233+
...config.axiosOptions
232234
})
233235

234236
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
@@ -246,6 +248,7 @@ export default class Kitsu {
246248
* @param {Object} [config] Additional configuration
247249
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
248250
* @param {Object} [config.headers] Additional headers to send with the request
251+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
249252
* @returns {Object|Object[]} JSON-parsed response
250253
* @example <caption>Update a resource</caption>
251254
* api.update('posts', {
@@ -289,7 +292,8 @@ export default class Kitsu {
289292
{
290293
headers,
291294
params,
292-
paramsSerializer: /* istanbul ignore next */ p => query(p)
295+
paramsSerializer: /* istanbul ignore next */ p => query(p),
296+
...config.axiosOptions
293297
}
294298
)
295299

@@ -344,7 +348,8 @@ export default class Kitsu {
344348
{
345349
headers,
346350
params,
347-
paramsSerializer: /* istanbul ignore next */ p => query(p)
351+
paramsSerializer: /* istanbul ignore next */ p => query(p),
352+
...config.axiosOptions
348353
}
349354
)
350355

@@ -363,6 +368,7 @@ export default class Kitsu {
363368
* @param {Object} [config] Additional configuration
364369
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
365370
* @param {Object} [config.headers] Additional headers to send with the request
371+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
366372
* @returns {Object|Object[]} JSON-parsed response
367373
* @example <caption>Remove a single resource</caption>
368374
* api.delete('posts', 123)
@@ -395,7 +401,8 @@ export default class Kitsu {
395401
}),
396402
headers,
397403
params,
398-
paramsSerializer: /* istanbul ignore next */ p => query(p)
404+
paramsSerializer: /* istanbul ignore next */ p => query(p),
405+
...config.axiosOptions
399406
})
400407

401408
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
@@ -413,6 +420,7 @@ export default class Kitsu {
413420
* @param {Object} [config] Additional configuration
414421
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
415422
* @param {Object} [config.headers] Additional headers to send with the request
423+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
416424
* @returns {Object} JSON-parsed response
417425
* @example <caption>Get the authenticated user's resource</caption>
418426
* api.self()
@@ -429,7 +437,7 @@ export default class Kitsu {
429437
try {
430438
const headers = { ...this.headers, ...config.headers }
431439
const params = { ...config.params, ...{ filter: { self: true } } }
432-
const res = await this.get('users', { ...{ headers }, ...{ params } })
440+
const res = await this.get('users', { ...{ headers }, ...{ params }, ...config.axiosOptions })
433441
return res.headers ? { ...{ data: res.data[0] }, ...{ headers: res.headers } } : { data: res.data[0] }
434442
} catch (E) {
435443
throw error(E)
@@ -449,6 +457,7 @@ export default class Kitsu {
449457
* @param {string} [config.method] Request method - `GET`, `PATCH`, `POST` or `DELETE` (defaults to `GET`, case-insensitive)
450458
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
451459
* @param {Object} [config.headers] Additional headers to send with the request
460+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
452461
* @returns {Object} JSON-parsed response
453462
* @example <caption>Raw GET request</caption>
454463
* api.request({
@@ -488,7 +497,7 @@ export default class Kitsu {
488497
* ]
489498
* })
490499
*/
491-
async request ({ body, method, params, type, url, headers }) {
500+
async request ({ body, method, params, type, url, headers, axiosOptions }) {
492501
try {
493502
method = method?.toUpperCase() || 'GET'
494503
const { data, headers: responseHeaders } = await this.axios.request({
@@ -502,7 +511,8 @@ export default class Kitsu {
502511
}),
503512
headers: { ...this.headers, ...headers },
504513
params,
505-
paramsSerializer: /* istanbul ignore next */ p => query(p)
514+
paramsSerializer: /* istanbul ignore next */ p => query(p),
515+
...axiosOptions
506516
})
507517

508518
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)

packages/kitsu/src/patch.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ afterEach(() => {
1414

1515
describe('kitsu', () => {
1616
describe('patch', () => {
17+
it('uses provided axios options', async () => {
18+
expect.assertions(1)
19+
const api = new Kitsu()
20+
api.axios = { patch: jest.fn().mockReturnValue({ data: '' }) }
21+
await api.patch('anime', { id: '1', type: 'anime' }, { axiosOptions: { withCredentials: true } })
22+
expect(api.axios.patch).toHaveBeenCalledWith('anime/1', { data: { id: '1', type: 'anime' } }, expect.objectContaining({ withCredentials: true }))
23+
})
24+
1725
it('sends and receieves headers', async () => {
1826
expect.assertions(2)
1927
const api = new Kitsu({ headers: { Authorization: true } })

packages/kitsu/src/post.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ afterEach(() => {
1010

1111
describe('kitsu', () => {
1212
describe('post', () => {
13+
it('uses provided axios options', async () => {
14+
expect.assertions(1)
15+
const api = new Kitsu()
16+
api.axios = { post: jest.fn().mockReturnValue({ data: '' }) }
17+
await api.post('anime', { id: '1', type: 'anime' }, { axiosOptions: { withCredentials: true } })
18+
expect(api.axios.post).toHaveBeenCalledWith('anime', { data: { id: '1', type: 'anime' } }, expect.objectContaining({ withCredentials: true }))
19+
})
20+
1321
it('sends and recieves headers', async () => {
1422
expect.assertions(2)
1523
const api = new Kitsu({ headers: { Authorization: true } })

packages/kitsu/src/request.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ afterEach(() => {
2929

3030
describe('kitsu', () => {
3131
describe('request', () => {
32+
it('uses provided axios options', async () => {
33+
expect.assertions(1)
34+
const api = new Kitsu()
35+
api.axios = { request: jest.fn().mockReturnValue({ data: '' }) }
36+
await api.request({ axiosOptions: { withCredentials: true } })
37+
expect(api.axios.request).toHaveBeenCalledWith(expect.objectContaining({ withCredentials: true }))
38+
})
39+
3240
it('sends and receives headers', async () => {
3341
expect.assertions(2)
3442
const api = new Kitsu({ headers: { Authorization: true } })

packages/kitsu/src/self.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ afterEach(() => {
1313

1414
describe('kitsu', () => {
1515
describe('self', () => {
16+
it('uses provided axios options', async () => {
17+
expect.assertions(1)
18+
const api = new Kitsu()
19+
api.get = jest.fn().mockReturnValue({ data: '', headers: '' })
20+
await api.self({ axiosOptions: { withCredentials: true } })
21+
expect(api.get).toHaveBeenCalledWith('users', expect.objectContaining({ withCredentials: true }))
22+
})
23+
1624
it('sends and recieves headers', async () => {
1725
expect.assertions(2)
1826
const api = new Kitsu({ headers: { Authorization: true } })

packages/kitsu/types/index.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ export default class Kitsu {
5858
after?: string;
5959
};
6060
};
61+
axiosOptions?: any;
6162
}) => any;
6263
update: (model: string, body: any | any[], config?: {
6364
params?: any;
6465
headers?: any;
66+
axiosOptions?: any;
6567
}) => any | any[];
6668
create: (model: string, body: any | any[], config?: {
6769
params?: any;
@@ -70,6 +72,7 @@ export default class Kitsu {
7072
remove: (model: string, id: string | number | number[], config?: {
7173
params?: any;
7274
headers?: any;
75+
axiosOptions?: any;
7376
}) => any | any[];
7477
/**
7578
* Axios Interceptors (alias of `axios.interceptors`)
@@ -125,6 +128,7 @@ export default class Kitsu {
125128
* @param {number} [config.params.page.size] Number of resources to return in request (Page-based and cursor-based) - **Note:** Not supported on Kitsu.io
126129
* @param {string} [config.params.page.before] Get the previous page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
127130
* @param {string} [config.params.page.after] Get the next page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
131+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
128132
* @returns {Object} JSON-parsed response
129133
* @example <caption>Getting a resource with JSON:API parameters</caption>
130134
* api.get('users', {
@@ -216,6 +220,7 @@ export default class Kitsu {
216220
after?: string;
217221
};
218222
};
223+
axiosOptions?: any;
219224
}): any;
220225
/**
221226
* Update a resource (alias `update`)
@@ -226,6 +231,7 @@ export default class Kitsu {
226231
* @param {Object} [config] Additional configuration
227232
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
228233
* @param {Object} [config.headers] Additional headers to send with the request
234+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
229235
* @returns {Object|Object[]} JSON-parsed response
230236
* @example <caption>Update a resource</caption>
231237
* api.update('posts', {
@@ -253,6 +259,7 @@ export default class Kitsu {
253259
patch(model: string, body: any | any[], config?: {
254260
params?: any;
255261
headers?: any;
262+
axiosOptions?: any;
256263
}): any | any[];
257264
/**
258265
* Create a new resource (alias `create`)
@@ -295,6 +302,7 @@ export default class Kitsu {
295302
* @param {Object} [config] Additional configuration
296303
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
297304
* @param {Object} [config.headers] Additional headers to send with the request
305+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
298306
* @returns {Object|Object[]} JSON-parsed response
299307
* @example <caption>Remove a single resource</caption>
300308
* api.delete('posts', 123)
@@ -304,6 +312,7 @@ export default class Kitsu {
304312
delete(model: string, id: string | number | number[], config?: {
305313
params?: any;
306314
headers?: any;
315+
axiosOptions?: any;
307316
}): any | any[];
308317
/**
309318
* Get the authenticated user's data
@@ -314,6 +323,7 @@ export default class Kitsu {
314323
* @param {Object} [config] Additional configuration
315324
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
316325
* @param {Object} [config.headers] Additional headers to send with the request
326+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
317327
* @returns {Object} JSON-parsed response
318328
* @example <caption>Get the authenticated user's resource</caption>
319329
* api.self()
@@ -329,6 +339,7 @@ export default class Kitsu {
329339
self(config?: {
330340
params?: any;
331341
headers?: any;
342+
axiosOptions?: any;
332343
}): any;
333344
/**
334345
* Send arbitrary requests
@@ -343,6 +354,7 @@ export default class Kitsu {
343354
* @param {string} [config.method] Request method - `GET`, `PATCH`, `POST` or `DELETE` (defaults to `GET`, case-insensitive)
344355
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
345356
* @param {Object} [config.headers] Additional headers to send with the request
357+
* @param {Object} [config.axiosOptions] Additional options for the axios instance (see [axios/axios#request-config](https://github.com/axios/axios#request-config) for details)
346358
* @returns {Object} JSON-parsed response
347359
* @example <caption>Raw GET request</caption>
348360
* api.request({
@@ -382,12 +394,13 @@ export default class Kitsu {
382394
* ]
383395
* })
384396
*/
385-
request({ body, method, params, type, url, headers }?: {
397+
request({ body, method, params, type, url, headers, axiosOptions }?: {
386398
url: string;
387399
type: string;
388400
body?: any | any[];
389401
method?: string;
390402
params?: any;
391403
headers?: any;
404+
axiosOptions?: any;
392405
}): any;
393406
}

0 commit comments

Comments
 (0)