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
5 changes: 5 additions & 0 deletions dist/ngRestAdapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ declare module NgRestAdapter {
remove(url: string, data?: any, headers?: IHeaderConfig, configOverrides?: ng.IRequestShortcutConfig): ng.IHttpPromise<any>;
api(url: string): NgRestAdapter.NgRestAdapterService;
skipInterceptor(): NgRestAdapter.NgRestAdapterService;
setSkipInterceptorRoutes(excludedRoutes: RegExp[]): NgRestAdapter.NgRestAdapterService;
getSkipInterceptorRoutes(): Array<RegExp | string>;
uuid(): string;
isUuid(uuid: string): boolean;
getConfig(): INgRestAdapterServiceConfig;
Expand All @@ -52,6 +54,7 @@ declare module NgRestAdapter {
private $http;
private uuid4;
private apiErrorHandler;
private skipInterceptorRoutes;
/**
* Construct the service with dependencies injected
* @param config
Expand All @@ -74,6 +77,8 @@ declare module NgRestAdapter {
getConfig(): NgRestAdapter.INgRestAdapterServiceConfig;
registerApiErrorHandler(apiErrorHandler: IApiErrorHandler): NgRestAdapterService;
getErrorHandler(): IApiErrorHandler;
getSkipInterceptorRoutes(): Array<RegExp | string>;
setSkipInterceptorRoutes(excludedRoutes: Array<RegExp | string>): NgRestAdapter.NgRestAdapterService;
}
}
declare module NgRestAdapter {
Expand Down
22 changes: 22 additions & 0 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.

22 changes: 21 additions & 1 deletion src/ngRestAdapterInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,33 @@ module NgRestAdapter {
public responseError = (rejection:ng.IHttpPromiseCallbackArg<any>):any => {

let ngRestAdapter = this.getNgRestAdapterService();

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

if (skipInterceptor === true){
return this.$q.reject(rejection); //exit early
}

let skipInterceptorRoutes = ngRestAdapter.getSkipInterceptorRoutes();
let routeUrl = rejection.config.url;
if (!_.isEmpty(skipInterceptorRoutes)){

let routeMatches = _.any(skipInterceptorRoutes, (routeMatch:RegExp|string) => {

if (_.isRegExp(routeMatch)){
return (<RegExp>routeMatch).test(routeUrl);
}else{
return routeMatch === routeUrl;
}

});

if (routeMatches){
return this.$q.reject(rejection); //exit early
}

}

try {

let errorHandler = ngRestAdapter.getErrorHandler();
Expand Down
2 changes: 2 additions & 0 deletions src/ngRestAdapterInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module NgRestAdapter {

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

uuid():string;
isUuid(uuid:string):boolean;
Expand Down
15 changes: 14 additions & 1 deletion src/ngRestAdapterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module NgRestAdapter {

export class NgRestAdapterService implements INgRestAdapterService {

private apiErrorHandler;
private apiErrorHandler:IApiErrorHandler;
private skipInterceptorRoutes:Array<RegExp|string>;

/**
* Construct the service with dependencies injected
Expand Down Expand Up @@ -130,6 +131,18 @@ module NgRestAdapter {
throw new NgRestAdapterErrorHandlerNotFoundException("API Error handler is not set");
}

public getSkipInterceptorRoutes():Array<RegExp|string>{
return this.skipInterceptorRoutes;
}


public setSkipInterceptorRoutes(excludedRoutes:Array<RegExp|string>):NgRestAdapter.NgRestAdapterService {

this.skipInterceptorRoutes = excludedRoutes;

return this;
}

}

}
51 changes: 49 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,20 @@ describe('Service tests', () => {

describe('Base $http usage', () => {

let spiedHandler;
beforeEach(() => {

$exceptionHandler.errors = []; //clear errors

});

it('should allow the $http service to be used as normal (success)', () => {

(<any>ngRestAdapterService).apiErrorHandler = null; //unset the error handler (normally not allowed)

spiedHandler = sinon.spy(); //spy on (private) error handler
ngRestAdapterService.registerApiErrorHandler(spiedHandler);

$httpBackend.expectGET('/any').respond('ok'); //the original base

let httpPromise = $http.get('/any');
Expand All @@ -429,7 +436,45 @@ describe('Service tests', () => {

});

it('should allow the $http service to be used as normal (error)', () => {
it ('should be able to set interceptor routes', () => {

let routeRegex = /\/excluded\/regex.*/;
let stringMatch = '/excluded/string/example';

ngRestAdapterService.setSkipInterceptorRoutes([routeRegex, stringMatch]);

expect(ngRestAdapterService.getSkipInterceptorRoutes()[0]).to.equal(routeRegex);
expect(ngRestAdapterService.getSkipInterceptorRoutes()[1]).to.equal(stringMatch);

});

it('should not intercept excluded (by regex) domains', () => {

$httpBackend.expectGET('/excluded/regex/example').respond(500, 'error');

let httpPromise = $http.get('/excluded/regex/example');

expect(httpPromise).eventually.to.be.rejected.and.have.deep.property('data', 'error');

$httpBackend.flush();

expect(spiedHandler).not.to.have.been.called;
});

it('should not intercept excluded (by string match) domains', () => {

$httpBackend.expectGET('/excluded/string/example').respond(500, 'error');

let httpPromise = $http.get('/excluded/string/example');

expect(httpPromise).eventually.to.be.rejected.and.have.deep.property('data', 'error');

$httpBackend.flush();

expect(spiedHandler).not.to.have.been.called;
});

it('should allow the $http service to be used as normal (error intercepted)', () => {


$httpBackend.expectGET('/any').respond(500, 'error'); //the original base
Expand All @@ -440,9 +485,11 @@ describe('Service tests', () => {

$httpBackend.flush();

expect(spiedHandler).to.have.been.calledOnce; //interceptor should have been called

expect($exceptionHandler.errors).to.be.empty; //no errors after the fact

})
});

});

Expand Down