Skip to content

Commit

Permalink
Merge pull request #76 from storyblok/feature/axios-response-intercep…
Browse files Browse the repository at this point in the history
…tor-feature

Feature/axios response interceptor feature
  • Loading branch information
onefriendaday authored Nov 27, 2020
2 parents 933dc19 + dd3c2c0 commit 2fb34b4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import StoryblokClient from 'storyblok-js-client/dist/es5/index.es'
- `accessToken` String, The preview token you can find in your space dashboard at https://app.storyblok.com
- `cache` Object
- `type` String, `none` or `memory`
- (`responseInterceptor` Function, optional - You can pass a function and return the result, like axios' interceptors. For security reasons, Storyblok client will deal only with the response interceptor.)
- (`region` String, optional)
- (`https` Boolean, optional)
- (`rateLimit` Integer, optional, defaults to 3 for management api and 5 for cdn api)
Expand All @@ -127,6 +128,28 @@ let Storyblok = new StoryblokClient({
}
})
```
### Passing response interceptor

The Storyblok client lets you pass a function that serves as a response interceptor to axios.
Usage:

```javascript
let Storyblok = new StoryblokClient({
accessToken: 'xf5dRMMjltLzKOcNgMaU9Att',
cache: {
clear: 'auto',
type: 'memory'
},
responseInterceptor: (response) => {
// one can handle status codes and more with the response
if (response.status === 200) {
// handle your status here
}
// ALWAYS return the response
return response
},
})
```

### Method `Storyblok#get`

Expand Down
5 changes: 5 additions & 0 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class Storyblok {
headers: headers,
proxy: (config.proxy || false)
})
if (config.responseInterceptor) {
this.client.interceptors.response.use((res) => {
return config.responseInterceptor(res)
})
}
}

setComponentResolver(resolver) {
Expand Down
98 changes: 98 additions & 0 deletions tests/axiosInterceptor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import StoryblokClient from '../source/index'

const accessToken = 'trB5kgOeDD22QJQDdPNCjAtt'
const cache = {
type: 'memory',
clear: 'auto',
}

describe('Client should accept response interceptor as a function', () => {
const Storyblok = new StoryblokClient({
accessToken,
cache,
responseInterceptor: (res) => {
return res
},
})
it('should RESPONSE to be TRUTHY', async () => {
await Storyblok.getAll('cdn/links')
expect(Storyblok.client.interceptors.response.handlers.length).toBeTruthy()
})
it('should REQUEST to be FALSY', async () => {
await Storyblok.getAll('cdn/links')
expect(Storyblok.client.interceptors.request.handlers.length).toBeFalsy()
})
})

describe('Client should be initialized without interceptors', () => {
const Storyblok = new StoryblokClient({
accessToken,
cache,
})
it('should RESPONSE to be FALSY', async () => {
await Storyblok.getAll('cdn/links')
expect(Storyblok.client.interceptors.response.handlers.length).toBeFalsy()
})
it('should REQUEST to be FALSY', async () => {
await Storyblok.getAll('cdn/links')
expect(Storyblok.client.interceptors.request.handlers.length).toBeFalsy()
})
})

describe('Client should throw an error if NO RESPONSE is returned by the responseInterceptor function', () => {
it('should throw a TypeError if NOTHING is returned', async () => {
const Storyblok = new StoryblokClient({
accessToken,
cache,
responseInterceptor: () => {
},
})
let error = ''
try {
await Storyblok.getAll('cdn/links')
} catch(err) {
error = err
}
expect(error).toEqual(new TypeError('Cannot read property \'data\' of undefined'));
})
it('should throw a TypeError if NULL is returned', async () => {
const Storyblok = new StoryblokClient({
accessToken,
cache,
responseInterceptor: () => null,
})
let error = ''
try {
await Storyblok.getAll('cdn/links')
} catch(err) {
error = err
}
expect(error).toEqual(new TypeError('Cannot read property \'data\' of null'));
})
})

describe('Client should throw an error if NO FUNCTION is passed AS the responseInterceptor', () => {
it('should throw a TypeError', async () => {
const Storyblok = new StoryblokClient({
accessToken,
cache,
responseInterceptor: 'something else',
})
let error = ''
try {
await Storyblok.getAll('cdn/links')
} catch(err) {
error = err
}
expect(error).toEqual(new TypeError('config.responseInterceptor is not a function'));
})
it('should REQUEST to be FALSY if NULL is passed', async () => {
const Storyblok = new StoryblokClient({
accessToken,
cache,
responseInterceptor: null,
})
await Storyblok.getAll('cdn/links')
expect(Storyblok.client.interceptors.request.handlers.length).toBeFalsy()
})
})
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface StoryblokConfig {
accessToken?: string
oauthToken?: string
cache?: StoryblokCache
responseInterceptor?: (response: any) => any
timeout?: number
headers?: any
region?: string
Expand Down

0 comments on commit 2fb34b4

Please sign in to comment.