Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions dist/ngRestAdapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare module NgRestAdapter {
patch(url: string, data: any, headers?: IHeaderConfig, configOverrides?: ng.IRequestShortcutConfig): ng.IHttpPromise<any>;
remove(url: string, data?: any, headers?: IHeaderConfig, configOverrides?: ng.IRequestShortcutConfig): ng.IHttpPromise<any>;
api(url: string): NgRestAdapter.NgRestAdapterService;
skipInterceptor(): NgRestAdapter.NgRestAdapterService;
skipInterceptor(shouldSkip?: ISkipInterceptorFunction): NgRestAdapter.NgRestAdapterService;
setSkipInterceptorRoutes(excludedRoutes: RegExp[]): NgRestAdapter.NgRestAdapterService;
getSkipInterceptorRoutes(): Array<RegExp | string>;
uuid(): string;
Expand All @@ -42,11 +42,14 @@ declare module NgRestAdapter {
interface INgRestAdapterServiceConfig {
baseUrl: string;
defaultHeaders?: IHeaderConfig;
skipInterceptor?: boolean;
skipInterceptor?: ISkipInterceptorFunction;
}
interface IApiErrorHandler {
(requestConfig: ng.IRequestConfig, responseObject: ng.IHttpPromiseCallbackArg<any>): void;
}
interface ISkipInterceptorFunction {
(rejection: ng.IHttpPromiseCallbackArg<any>): boolean;
}
}
declare module NgRestAdapter {
class NgRestAdapterService implements INgRestAdapterService {
Expand All @@ -71,7 +74,7 @@ declare module NgRestAdapter {
patch(url: string, data: any, headers?: IHeaderConfig, configOverrides?: ng.IRequestShortcutConfig): ng.IHttpPromise<any>;
remove(url: string, data?: any, headers?: IHeaderConfig, configOverrides?: ng.IRequestShortcutConfig): ng.IHttpPromise<any>;
api(url: string): NgRestAdapterService;
skipInterceptor(): NgRestAdapterService;
skipInterceptor(shouldSkip?: ISkipInterceptorFunction): NgRestAdapterService;
uuid(): string;
isUuid(uuid: string): boolean;
getConfig(): NgRestAdapter.INgRestAdapterServiceConfig;
Expand Down
11 changes: 6 additions & 5 deletions dist/ngRestAdapter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ngRestAdapter.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/ngRestAdapterInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ module NgRestAdapter {
public responseError = (rejection:ng.IHttpPromiseCallbackArg<any>):any => {

let ngRestAdapter = this.getNgRestAdapterService();

let skipInterceptor = _.get(rejection.config, 'ngRestAdapterServiceConfig.skipInterceptor', false);

if (skipInterceptor === true){
let skipInterceptor = <ISkipInterceptorFunction>_.get(rejection.config, 'ngRestAdapterServiceConfig.skipInterceptor');

if (_.isFunction(skipInterceptor) && skipInterceptor(rejection)){
return this.$q.reject(rejection); //exit early
}

Expand Down
8 changes: 6 additions & 2 deletions src/ngRestAdapterInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module NgRestAdapter {
remove(url:string, data?:any, headers?:IHeaderConfig, configOverrides?:ng.IRequestShortcutConfig):ng.IHttpPromise<any>;

api(url:string):NgRestAdapter.NgRestAdapterService;
skipInterceptor():NgRestAdapter.NgRestAdapterService;
skipInterceptor(shouldSkip?:ISkipInterceptorFunction):NgRestAdapter.NgRestAdapterService;
setSkipInterceptorRoutes(excludedRoutes:RegExp[]):NgRestAdapter.NgRestAdapterService;
getSkipInterceptorRoutes():Array<RegExp|string>;

Expand All @@ -35,11 +35,15 @@ module NgRestAdapter {
export interface INgRestAdapterServiceConfig {
baseUrl: string;
defaultHeaders?: IHeaderConfig
skipInterceptor?: boolean;
skipInterceptor?: ISkipInterceptorFunction;
}

export interface IApiErrorHandler {
(requestConfig:ng.IRequestConfig, responseObject:ng.IHttpPromiseCallbackArg<any>):void;
}

export interface ISkipInterceptorFunction {
(rejection:ng.IHttpPromiseCallbackArg<any>):boolean;
}

}
4 changes: 2 additions & 2 deletions src/ngRestAdapterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ module NgRestAdapter {
return new NgRestAdapterService(config, this.$http, this.uuid4);
}

public skipInterceptor():NgRestAdapterService {
public skipInterceptor(shouldSkip:ISkipInterceptorFunction = () => true):NgRestAdapterService {

let config = <INgRestAdapterServiceConfig>_.defaults({skipInterceptor:true}, this.config);
let config = <INgRestAdapterServiceConfig>_.defaults({skipInterceptor:shouldSkip}, this.config);

return new NgRestAdapterService(config, this.$http, this.uuid4);

Expand Down
2 changes: 1 addition & 1 deletion src/ngRestAdapterServiceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module NgRestAdapter {
defaultHeaders: {
'Requested-With': 'angular-rest-adapter'
},
skipInterceptor: false
skipInterceptor: () => false
}

}
Expand Down
42 changes: 33 additions & 9 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let fixtures = {
defaultHeaders: {
'Test-Header': 'This is a test header'
},
skipInterceptor: true
skipInterceptor: () => false
};
},

Expand Down Expand Up @@ -57,7 +57,8 @@ describe('Custom configuration', function () {

let configuration = customRestAdapter.getConfig();

return expect(configuration).to.deep.equal(fixtures.customConfig);
expect(configuration.baseUrl).to.equal(fixtures.customConfig.baseUrl);
expect(configuration.defaultHeaders).to.deep.equal(fixtures.customConfig.defaultHeaders);

});

Expand Down Expand Up @@ -285,10 +286,10 @@ describe('Service tests', () => {


let throwException = false;
let spyMethod = sinon.spy();
let errorHandlerSpy = sinon.spy();
let errorHandlerMock = (requestConfig:ng.IRequestConfig, responseObject:ng.IHttpPromiseCallbackArg<any>):void => {

spyMethod(requestConfig, responseObject); //spy on the options
errorHandlerSpy(requestConfig, responseObject); //spy on the options

if (throwException){
throw Error("An error occurred!");
Expand All @@ -303,7 +304,7 @@ describe('Service tests', () => {

$httpBackend.flush();

expect(spyMethod.called).to.be.false;
expect(errorHandlerSpy.called).to.be.false;
expect(response).eventually.to.be.rejected;
});

Expand All @@ -314,7 +315,7 @@ describe('Service tests', () => {
ngRestAdapterService.registerApiErrorHandler(errorHandlerMock);


expect(spyMethod.called).to.be.false;
expect(errorHandlerSpy.called).to.be.false;

});

Expand All @@ -337,7 +338,7 @@ describe('Service tests', () => {

$httpBackend.flush();

expect(spyMethod.called).to.be.false;
expect(errorHandlerSpy).not.to.be.called;
});

it('should call the api error handler when the api responds with an error', () => {
Expand All @@ -347,7 +348,7 @@ describe('Service tests', () => {

$httpBackend.flush();

expect(spyMethod.called).to.be.true;
expect(errorHandlerSpy).to.be.calledOnce;
});

it('should not call the api error handler the api service specifies the interceptor should be skipped', () => {
Expand All @@ -358,7 +359,30 @@ describe('Service tests', () => {

$httpBackend.flush();

expect(spyMethod.calledOnce).to.be.true;
expect(errorHandlerSpy.calledOnce).to.be.true;
});

it('should be able to define a custom interceptor function to only fail in some conditions', () => {


let customInterceptor = (rejection:ng.IHttpPromiseCallbackArg<any>):boolean => {

return rejection.status <= 500;
};

$httpBackend.expectGET('/api/fatal').respond(500);
ngRestAdapterService.skipInterceptor(customInterceptor).get('/fatal'); //get a failing url
$httpBackend.flush();

expect(errorHandlerSpy).to.have.been.calledOnce;

$httpBackend.expectGET('/api/recoverable').respond(416);
ngRestAdapterService.skipInterceptor(customInterceptor).get('/recoverable'); //get a recoverable url
$httpBackend.flush();

expect(errorHandlerSpy).to.have.been.calledOnce; //should not have been called again


});

it('should not catch an exception thrown from an error interceptor if it is user supplied', () => {
Expand Down