-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi.js
45 lines (38 loc) · 1.14 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const axios = require('axios');
const { withVersioning, VersioningStrategy } = require('axios-api-versioning');
const axiosInstance = axios.create({
baseURL: 'http://localhost:4000'
});
const client = withVersioning(axiosInstance, {
apiVersion: '1',
versioningStrategy: VersioningStrategy.QueryString
});
/**
* here we manually set the apiVersion & versioningStrategy on every request
* as to override any default setting.
*
* If you have an app where you don't need to mix api versions,
* then set your active version inside the withVersioning()
*/
function getBooksByQueryString() {
return client.get('books')
}
function getBooksByUrlPath() {
return client.get('v{apiVersion}/books', {
// override the settings for this request only
apiVersion: '2',
versioningStrategy: VersioningStrategy.UrlPath
})
}
function getBooksByMediaType() {
return client.get('books', {
// override the settings for this request only
apiVersion: '3',
versioningStrategy: VersioningStrategy.MediaType
})
}
module.exports = {
getBooksByQueryString,
getBooksByUrlPath,
getBooksByMediaType
}