
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
ng2-http-interceptor
Advanced tools
Http Interceptor library for Angular 2
This library uses Proxy
from ES6 spec so if you need to support browsers
that are ES5 compatible include proxy-polyfill.
If you are getting error like: ReferenceError: __extends is not defined
Please make sure that you are importing ts-helpers
module in your application.
To install this library, run:
$ npm install ng2-http-interceptor --save
Import HttpInterceptorModule
to your application module.
This will override original Http
Service and all requests will be intercepted.
Import as HttpInterceptorModule.noOverrideHttp()
to keep original Http
Service
and use InterceptableHttp
for requests to be intercepted.
To use it you must first declare providers in your @NgModule
.
You have 2 options:
InterceptableHttp
AND override original Http
service so
that all your requests will be interceptedInterceptableHttp
and keep original Http
service so
you can make requests which are intercepted and not.For case #1 use:
{
providers: [...HTTP_INTERCEPTOR_PROVIDER]
}
For case #2 use:
{
providers: [...HTTP_INTERCEPTOR_NO_OVERRIDE_PROVIDER]
}
You can use InterceptableHttp
for your requests in case #1 and #2
and Http
if you chose to override it (case #1 only):
constructor(http: Http, httpInterceptor: HttpInterceptorService) {
httpInterceptor.request().addInterceptor((data, method) => {
console.log(method, data);
return data;
});
httpInterceptor.response().addInterceptor((res, method) => {
res.subscribe(r => console.log(method, r));
return res;
});
this.http.get('/')
.map(r => r.text())
.subscribe(console.log);
}
In this setup every request and response will be logged to the console.
You can also cancel request by returning false
value (that coerce to boolean false)
from one of registered request interceptors.
You can return Observable
from request interceptors to do some async job.
You can find in-depth explanation of internal concepts here: https://goo.gl/GU9VWo
Also if you want to play with it check this repo.
Or check this plnkr demo.
All and every interception setup is made by HttpInterceptorService
service.
Inject this service in place where you want to manage interceptors.
HttpInterceptorService
HttpInterceptorService: {
request(url?: string|RegExp): Interceptable,
response(url?: string|RegExp): Interceptable
}
See src/http/http-interceptor.ts for full reference
Description: Methods will determine when to call interceptor - before
request (request()
) or after response (response()
).
You can also specify url filtering (string|RegExp
) which will indicate
when interceptor must be triggered depending on url.
By default all interceptors fall under '/'
url key which means every
interceptor registered that way will be triggered despite of actual url.
Interceptable
Interceptable: {
addInterceptor(interceptorFn: Interceptor): Interceptable,
removeInterceptor(interceptorFn: Interceptor): Interceptable,
clearInterceptors(interceptorFns?: Interceptor[]): Interceptable
}
See src/http/interceptable.ts for full reference
Description: This object will help you manage interceptors with respect to your selected configuration (url filtering).
Interceptor
export interface Interceptor {
(data: any, method: string): any;
}
See src/http/interceptable.ts for full reference
Description: This is generic type of interceptor - which is a plain old JavaScript function.
You will be dealing with specific one to satisfy it's criteria:
Interceptor<any[], any[]>
- for request interceptorsHttp
was made + method name as string (get
, post
, delete
...)
and should return array of the same structure or false
to cancel request.Interceptor<Observable<Response>, Observable<Response>>
- for response interceptorsget
, post
, delete
...)
and should return same or new Observable but with type Response
(this is made specifically to prevent other code being broken
because response was intercepted and structure changed)There are a bunch of helper functions added to simplify some common
work with data array (for ex. getting RequestOptions
or even Headers
).
getHttpHeadersOrInit()
export function getHttpHeadersOrInit(data: any[], method: string): Headers;
See src/http/helpers/getHttpHeadersOrInit.ts for full reference
Description: Gets Headers
from data array.
If no RequestOptions
found - creates it and updates original data array.
If no Headers
found - creates it and sets to RequestOptions
.
getHttpOptionsAndIdx()
export function getHttpOptionsAndIdx(
data: any[],
method: string,
alwaysOriginal?: boolean
): {
options: RequestOptions;
idx: number;
};
See src/http/helpers/getHttpOptionsAndIdx.ts for full reference
Description: Gets RequestOptions
and it's index location in data array.
If no options found and alwaysOriginal = false
- creates new RequestOptions
but does NOT set it back on data array.
alwaysOriginal
is false
by default.getHttpOptions()
export function getHttpOptions(
data: any[],
method: string,
alwaysOriginal?: boolean): RequestOptions;
See src/http/helpers/getHttpOptions.ts for full reference
Description: Gets http RequestOptions
from data array.
If no options found and alwaysOriginal = false
- returns new RequestOptions
but does NOT set it back on data array.
alwaysOriginal
is false
by default.getHttpOptionsIdx()
export function getHttpOptionsIdx(method: string): number;
See src/http/helpers/getHttpOptionsIdx.ts for full reference
Description: Gets index of RequestOptions
in http data array for specified method
.
To generate all *.js
, *.js.map
, *.d.ts
and *.metadata.json
files:
$ npm run build
To lint all *.ts
files:
$ npm run lint
To run unit tests:
$ npm test
MIT © Alex Malkevich
FAQs
> Http Interceptor library for Angular 2
The npm package ng2-http-interceptor receives a total of 1 weekly downloads. As such, ng2-http-interceptor popularity was classified as not popular.
We found that ng2-http-interceptor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.