-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add request and response interceptors #619
Changes from all commits
c21f3b3
555c716
1dbcf38
14e4077
a7a2ebf
09bd538
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024 Google LLC | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {GaxiosError, GaxiosOptions, GaxiosResponse} from './common'; | ||
|
||
/** | ||
* Interceptors that can be run for requests or responses. These interceptors run asynchronously. | ||
*/ | ||
export interface GaxiosInterceptor<T extends GaxiosOptions | GaxiosResponse> { | ||
/** | ||
* Function to be run when applying an interceptor. | ||
* | ||
* @param {T} configOrResponse The current configuration or response. | ||
* @returns {Promise<T>} Promise that resolves to the modified set of options or response. | ||
*/ | ||
resolved?: (configOrResponse: T) => Promise<T>; | ||
/** | ||
* Function to be run if the previous call to resolved throws / rejects or the request results in an invalid status | ||
* as determined by the call to validateStatus. | ||
* | ||
* @param {GaxiosError} err The error thrown from the previously called resolved function. | ||
*/ | ||
rejected?: (err: GaxiosError) => void; | ||
} | ||
|
||
/** | ||
* Class to manage collections of GaxiosInterceptors for both requests and responses. | ||
*/ | ||
export class GaxiosInterceptorManager< | ||
T extends GaxiosOptions | GaxiosResponse, | ||
> extends Set<GaxiosInterceptor<T> | null> {} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1102,3 +1102,234 @@ describe('🍂 defaults & instances', () => { | |||||
}); | ||||||
}); | ||||||
}); | ||||||
|
||||||
describe('interceptors', () => { | ||||||
describe('request', () => { | ||||||
it('should invoke a request interceptor when one is provided', async () => { | ||||||
const scope = nock(url) | ||||||
.matchHeader('hello', 'world') | ||||||
.get('/') | ||||||
.reply(200, {}); | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: config => { | ||||||
config.headers = {hello: 'world'}; | ||||||
return Promise.resolve(config); | ||||||
}, | ||||||
}); | ||||||
await instance.request({url}); | ||||||
scope.done(); | ||||||
}); | ||||||
|
||||||
it('should not invoke a request interceptor after it is removed', async () => { | ||||||
const scope = nock(url).persist().get('/').reply(200, {}); | ||||||
const spyFunc = sinon.fake( | ||||||
() => | ||||||
Promise.resolve({ | ||||||
url, | ||||||
validateStatus: () => { | ||||||
return true; | ||||||
}, | ||||||
}) as unknown as Promise<GaxiosOptions> | ||||||
); | ||||||
const instance = new Gaxios(); | ||||||
const interceptor = {resolved: spyFunc}; | ||||||
instance.interceptors.request.add(interceptor); | ||||||
await instance.request({url}); | ||||||
instance.interceptors.request.delete(interceptor); | ||||||
await instance.request({url}); | ||||||
scope.done(); | ||||||
assert.strictEqual(spyFunc.callCount, 1); | ||||||
}); | ||||||
|
||||||
it('should invoke multiple request interceptors in the order they were added', async () => { | ||||||
const scope = nock(url) | ||||||
.matchHeader('foo', 'bar') | ||||||
.matchHeader('bar', 'baz') | ||||||
.matchHeader('baz', 'buzz') | ||||||
.get('/') | ||||||
.reply(200, {}); | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: config => { | ||||||
config.headers!['foo'] = 'bar'; | ||||||
return Promise.resolve(config); | ||||||
}, | ||||||
}); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: config => { | ||||||
assert.strictEqual(config.headers!['foo'], 'bar'); | ||||||
config.headers!['bar'] = 'baz'; | ||||||
return Promise.resolve(config); | ||||||
}, | ||||||
}); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: config => { | ||||||
assert.strictEqual(config.headers!['foo'], 'bar'); | ||||||
assert.strictEqual(config.headers!['bar'], 'baz'); | ||||||
config.headers!['baz'] = 'buzz'; | ||||||
return Promise.resolve(config); | ||||||
}, | ||||||
}); | ||||||
await instance.request({url, headers: {}}); | ||||||
scope.done(); | ||||||
}); | ||||||
|
||||||
it('should not invoke a any request interceptors after they are removed', async () => { | ||||||
const scope = nock(url).persist().get('/').reply(200, {}); | ||||||
const spyFunc = sinon.fake( | ||||||
() => | ||||||
Promise.resolve({ | ||||||
url, | ||||||
validateStatus: () => { | ||||||
return true; | ||||||
}, | ||||||
}) as unknown as Promise<GaxiosOptions> | ||||||
); | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: spyFunc, | ||||||
}); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: spyFunc, | ||||||
}); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: spyFunc, | ||||||
}); | ||||||
await instance.request({url}); | ||||||
instance.interceptors.request.clear(); | ||||||
await instance.request({url}); | ||||||
scope.done(); | ||||||
assert.strictEqual(spyFunc.callCount, 3); | ||||||
}); | ||||||
|
||||||
it('should invoke the rejected function when a previous request interceptor rejects', async () => { | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: () => { | ||||||
throw new Error('Something went wrong'); | ||||||
}, | ||||||
}); | ||||||
instance.interceptors.request.add({ | ||||||
resolved: config => { | ||||||
config.headers = {hello: 'world'}; | ||||||
return Promise.resolve(config); | ||||||
}, | ||||||
rejected: err => { | ||||||
assert.strictEqual(err.message, 'Something went wrong'); | ||||||
}, | ||||||
}); | ||||||
// Because the options wind up being invalid the call will reject with a URL problem. | ||||||
assert.rejects(instance.request({url})); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need an
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe so |
||||||
}); | ||||||
}); | ||||||
|
||||||
describe('response', () => { | ||||||
it('should invoke a response interceptor when one is provided', async () => { | ||||||
const scope = nock(url).get('/').reply(200, {}); | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.response.add({ | ||||||
resolved(response) { | ||||||
response.headers['hello'] = 'world'; | ||||||
return Promise.resolve(response); | ||||||
}, | ||||||
}); | ||||||
const resp = await instance.request({url}); | ||||||
scope.done(); | ||||||
assert.strictEqual(resp.headers['hello'], 'world'); | ||||||
}); | ||||||
|
||||||
it('should not invoke a response interceptor after it is removed', async () => { | ||||||
const scope = nock(url).persist().get('/').reply(200, {}); | ||||||
const spyFunc = sinon.fake( | ||||||
() => | ||||||
Promise.resolve({ | ||||||
url, | ||||||
validateStatus: () => { | ||||||
return true; | ||||||
}, | ||||||
}) as unknown as Promise<GaxiosResponse> | ||||||
); | ||||||
const instance = new Gaxios(); | ||||||
const interceptor = {resolved: spyFunc}; | ||||||
instance.interceptors.response.add(interceptor); | ||||||
await instance.request({url}); | ||||||
instance.interceptors.response.delete(interceptor); | ||||||
await instance.request({url}); | ||||||
scope.done(); | ||||||
assert.strictEqual(spyFunc.callCount, 1); | ||||||
}); | ||||||
|
||||||
it('should invoke multiple response interceptors in the order they were added', async () => { | ||||||
const scope = nock(url).get('/').reply(200, {}); | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.response.add({ | ||||||
resolved: response => { | ||||||
response.headers!['foo'] = 'bar'; | ||||||
return Promise.resolve(response); | ||||||
}, | ||||||
}); | ||||||
instance.interceptors.response.add({ | ||||||
resolved: response => { | ||||||
assert.strictEqual(response.headers!['foo'], 'bar'); | ||||||
response.headers!['bar'] = 'baz'; | ||||||
return Promise.resolve(response); | ||||||
}, | ||||||
}); | ||||||
instance.interceptors.response.add({ | ||||||
resolved: response => { | ||||||
assert.strictEqual(response.headers!['foo'], 'bar'); | ||||||
assert.strictEqual(response.headers!['bar'], 'baz'); | ||||||
response.headers!['baz'] = 'buzz'; | ||||||
return Promise.resolve(response); | ||||||
}, | ||||||
}); | ||||||
const resp = await instance.request({url, headers: {}}); | ||||||
scope.done(); | ||||||
assert.strictEqual(resp.headers['foo'], 'bar'); | ||||||
assert.strictEqual(resp.headers['bar'], 'baz'); | ||||||
assert.strictEqual(resp.headers['baz'], 'buzz'); | ||||||
}); | ||||||
|
||||||
it('should not invoke a any response interceptors after they are removed', async () => { | ||||||
const scope = nock(url).persist().get('/').reply(200, {}); | ||||||
const spyFunc = sinon.fake( | ||||||
() => | ||||||
Promise.resolve({ | ||||||
url, | ||||||
validateStatus: () => { | ||||||
return true; | ||||||
}, | ||||||
}) as unknown as Promise<GaxiosResponse> | ||||||
); | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.response.add({ | ||||||
resolved: spyFunc, | ||||||
}); | ||||||
instance.interceptors.response.add({ | ||||||
resolved: spyFunc, | ||||||
}); | ||||||
instance.interceptors.response.add({ | ||||||
resolved: spyFunc, | ||||||
}); | ||||||
await instance.request({url}); | ||||||
instance.interceptors.response.clear(); | ||||||
await instance.request({url}); | ||||||
scope.done(); | ||||||
assert.strictEqual(spyFunc.callCount, 3); | ||||||
}); | ||||||
|
||||||
it('should invoke the rejected function when a request has an error', async () => { | ||||||
const scope = nock(url).get('/').reply(404, {}); | ||||||
const instance = new Gaxios(); | ||||||
instance.interceptors.response.add({ | ||||||
rejected: err => { | ||||||
assert.strictEqual(err.status, 404); | ||||||
}, | ||||||
}); | ||||||
|
||||||
await instance.request({url}); | ||||||
scope.done(); | ||||||
}); | ||||||
}); | ||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pinning compodoc because the downstream dep
@angular-devkit/schematics
appears to have dropped < Node 16 in the latest version which was breaking CI for us.