{"id":15184596,"url":"https://github.com/gund/ng-http-interceptor","last_synced_at":"2025-10-01T23:30:41.623Z","repository":{"id":57113431,"uuid":"69654246","full_name":"gund/ng-http-interceptor","owner":"gund","description":"Http Interceptor library for Angular","archived":true,"fork":false,"pushed_at":"2017-11-08T19:40:41.000Z","size":387,"stargazers_count":104,"open_issues_count":5,"forks_count":16,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-01-14T08:52:26.191Z","etag":null,"topics":["angular","angular2","angular4","http","interceptor","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gund.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-30T09:38:25.000Z","updated_at":"2024-01-22T10:03:11.000Z","dependencies_parsed_at":"2022-08-21T21:40:36.108Z","dependency_job_id":null,"html_url":"https://github.com/gund/ng-http-interceptor","commit_stats":null,"previous_names":["gund/ng2-http-interceptor"],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gund%2Fng-http-interceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gund%2Fng-http-interceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gund%2Fng-http-interceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gund%2Fng-http-interceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gund","download_url":"https://codeload.github.com/gund/ng-http-interceptor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234909086,"owners_count":18905504,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","angular2","angular4","http","interceptor","typescript"],"created_at":"2024-09-27T17:20:42.726Z","updated_at":"2025-10-01T23:30:41.322Z","avatar_url":"https://github.com/gund.png","language":"TypeScript","readme":"# ng-http-interceptor\n\n\u003e Http Interceptor library for Angular\n\n*Previously was called `ng2-http-interceptor`*\n\n[![Travis CI](https://img.shields.io/travis/gund/ng-http-interceptor/master.svg?maxAge=2592000)](https://travis-ci.org/gund/ng-http-interceptor)\n[![Coverage](https://img.shields.io/codecov/c/github/gund/ng-http-interceptor.svg?maxAge=2592000)](https://codecov.io/gh/gund/ng-http-interceptor)\n[![Code Climate](https://img.shields.io/codeclimate/github/gund/ng-http-interceptor.svg?maxAge=2592000)](https://codeclimate.com/github/gund/ng-http-interceptor)\n[![Npm](https://img.shields.io/npm/v/ng-http-interceptor.svg?maxAge=2592000)](https://badge.fury.io/js/ng-http-interceptor)\n[![Npm Downloads](https://img.shields.io/npm/dt/ng2-http-interceptor.svg?maxAge=2592000)](https://www.npmjs.com/package/ng-http-interceptor)\n[![Licence](https://img.shields.io/npm/l/ng-http-interceptor.svg?maxAge=2592000)](https://github.com/gund/ng-http-interceptor/blob/master/LICENSE)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nVersion 4.x.x supports Angular 5 (`ng-http-interceptor@^4.0.0`)\n\nVersion 3.x.x supports Angular 4 (`ng-http-interceptor@^3.0.0`)\n\nVersion 2.x.x supports Angular 2 (`ng-http-interceptor@^2.0.0`)\n\n## Features\n\n* Registering interceptors globally\n* Separate interceptors for requests and responses\n* Attach interceptors for specific urls via strings or RegExp's\n* Remove specific/all interceptor(s)\n* Modify requests (even url) from request interceptors\n* Cancel requests from request interceptors\n* Modify responses from response interceptors\n* Interceptor Service is not coupled with Http Service\n* Choose between overriding original Http Service or keep it and still use interceptors\n* Comprehensive type assistance for your interceptor functions\n* Supports AOT compilation with shipped _*.metadata.json_ files\n* UMD builds in _dist/bundles_ folder ready to use in Browsers\n* Simple http data extraction and manipulation with [Helpers Functions](#helpers-functions-since-v130)\n* Sharing context object between all interceptors\n\n## Table of Contents\n\n* [Features](#features)\n* [Prerequisites](#prerequisites)\n* [Installation](#installation)\n* [Usage](#usage)\n* [Documentation](#documentation)\n* [Development](#development)\n\n## Prerequisites\n\nThis library uses `Proxy` from ES6 spec so if you need to support browsers\nthat are ES5 compatible include [proxy-polyfill](https://github.com/GoogleChrome/proxy-polyfill).\n\nLibrary uses `tslib` (standard Typescript runtime library) so please make sure\nyou have this module installed via `npm`.\n\n## Installation\n\nTo install this library, run:\n\n```bash\n$ npm install ng-http-interceptor --save\n```\n\n## Usage\n\n#### Case #1\n\nImport `HttpInterceptorModule` to your application module.\nThis will override original `Http` Service and all requests will be intercepted.\n\n#### Case #2\n\nImport as `HttpInterceptorModule.noOverrideHttp()` to keep original `Http` Service\nand use `InterceptableHttp` for requests to be intercepted.\n\n### Example use-case\n\nYou can use `InterceptableHttp` for your requests in case #1 and #2\nand `Http` if you chose to override it (case #1 only):\n```ts\nconstructor(http: Http, httpInterceptor: HttpInterceptorService) {\n    httpInterceptor.request().addInterceptor((data, method) =\u003e {\n      console.log(method, data);\n      return data;\n    });\n\n    httpInterceptor.response().addInterceptor((res, method) =\u003e {\n      return res.do(r =\u003e console.log(method, r));\n    });\n\n    this.http.get('/')\n          .map(r =\u003e r.text())\n          .subscribe(console.log);\n}\n```\n\nIn this setup every request and response will be logged to the console.\nYou can also cancel request by returning `false` value (that coerce to boolean false)\nfrom one of registered _request_ interceptors.\nYou can return `Observable` from _request_ interceptors to do some async job.\n\nYou can find in-depth explanation of internal concepts here: https://goo.gl/GU9VWo\nAlso if you want to play with it check this [repo](https://github.com/gund/angular2-http-interceptor-test).\nOr check this [plnkr demo](https://plnkr.co/edit/qTTdPl8EggQleTfTSbch).\n\n## Documentation\n\nAll and every interception setup is made by `HttpInterceptorService` service.\nInject this service in place where you want to manage interceptors.\n\n### Public API\n\n**HttpInterceptorService**\n```ts\nHttpInterceptorService: {\n    request(url?: string|RegExp): Interceptable,\n    response(url?: string|RegExp): Interceptable\n}\n```\n\n\u003csub\u003eSee [src/http/http-interceptor.ts](./src/http/http-interceptor.ts#L8) for full reference\u003c/sub\u003e\n\n_Description_: Methods will determine when to call interceptor - before\nrequest (`request()`) or after response (`response()`).\nYou can also specify url filtering (`string|RegExp`) which will indicate\nwhen interceptor must be triggered depending on url.\nBy default all interceptors fall under `'/'` url key which means every\ninterceptor registered that way will be triggered despite of actual url.\n\n**Interceptable**\n```ts\nInterceptable: {\n    addInterceptor(interceptorFn: Interceptor): Interceptable,\n    removeInterceptor(interceptorFn: Interceptor): Interceptable,\n    clearInterceptors(interceptorFns?: Interceptor[]): Interceptable\n}\n```\n\n\u003csub\u003eSee [src/http/interceptable.ts](./src/http/interceptable.ts#L1) for full reference\u003c/sub\u003e\n\n_Description_: This object will help you manage interceptors with\nrespect to your selected configuration (url filtering).\n\n**Interceptor**\n```ts\nInterceptor\u003cT, D\u003e {\n  (data: T, method: string, ctx?: any): D;\n}\n```\n\n\u003csub\u003eSee [src/http/interceptable.ts](./src/http/interceptable.ts#L7) for full reference\u003c/sub\u003e\n\n_Description_: This is generic type of interceptor - which is a plain old JavaScript function.\nYou will be dealing with specific one to satisfy it's criteria:\n* `Interceptor\u003cany[], any[]\u003e` - for **request** interceptors\n    Function will get an array of parameters with which call on `Http`\n    was made + method name as string (`get`, `post`, `delete`...)\n    and should return array of the same structure or `false` to cancel request.\n* `Interceptor\u003cObservable\u003cResponse\u003e, Observable\u003cResponse\u003e\u003e` - for **response** interceptors\n    Function will get Observable + method name as string (`get`, `post`, `delete`...)\n    and should return same or new Observable but with type Response\n    (this is made specifically to prevent other code being broken\n    because response was intercepted and structure changed)\n\n#### Helpers Functions (since v1.3.0)\n\nThere are a bunch of helper functions added to simplify some common\nwork with _data_ array (for ex. getting `RequestOptions` or even `Headers`).\n\n**getHttpHeadersOrInit()**\n```ts\nfunction getHttpHeadersOrInit(data: any[], method: string): Headers;\n```\n\n\u003csub\u003eSee [src/http/helpers/getHttpHeadersOrInit.ts](./src/http/helpers/getHttpHeadersOrInit.ts) for full reference\u003c/sub\u003e\n\n_Description_: Gets `Headers` from _data_ array.\nIf no `RequestOptions` found - creates it and updates original _data_ array.\nIf no `Headers` found - creates it and sets to `RequestOptions`.\n\nExmaple how to add custom headers to requests:\n```ts\nhttpInterceptor.request().addInterceptor((data, method) =\u003e {\n  const headers = getHttpHeadersOrInit(data, method);\n  headers.set('X-Custom-Header', 'value');\n  return data;\n});\n```\n\n**getHttpOptionsAndIdx()**\n```ts\nfunction getHttpOptionsAndIdx(\n    data: any[],\n    method: string,\n    alwaysOriginal?: boolean\n): {\n    options: RequestOptions;\n    idx: number;\n};\n```\n\n\u003csub\u003eSee [src/http/helpers/getHttpOptionsAndIdx.ts](./src/http/helpers/getHttpOptionsAndIdx.ts) for full reference\u003c/sub\u003e\n\n_Description_: Gets `RequestOptions` and it's index location in _data_ array.\nIf no options found and `alwaysOriginal = false` - creates new `RequestOptions`\nbut does NOT set it back on _data_ array.\n* Param `alwaysOriginal` is `false` by default.\n\n**getHttpOptions()**\n```ts\nfunction getHttpOptions(\n    data: any[],\n    method: string,\n    alwaysOriginal?: boolean): RequestOptions;\n```\n\n\u003csub\u003eSee [src/http/helpers/getHttpOptions.ts](./src/http/helpers/getHttpOptions.ts) for full reference\u003c/sub\u003e\n\n_Description_: Gets http `RequestOptions` from data array.\nIf no options found and `alwaysOriginal = false` - returns new `RequestOptions`\nbut does NOT set it back on _data_ array.\n* Param `alwaysOriginal` is `false` by default.\n\n**getHttpOptionsIdx()**\n```ts\nfunction getHttpOptionsIdx(method: string): number;\n```\n\n\u003csub\u003eSee [src/http/helpers/getHttpOptionsIdx.ts](./src/http/helpers/getHttpOptionsIdx.ts) for full reference\u003c/sub\u003e\n\n_Description_: Gets index of `RequestOptions` in http data array for specified `method`.\n\n\n## Development\n\nTo generate all `*.js`, `*.js.map`, `*.d.ts` and `*.metadata.json` files:\n\n```bash\n$ npm run build\n```\n\nTo lint all `*.ts` files:\n\n```bash\n$ npm run lint\n```\n\nTo run unit tests:\n\n```bash\n$ npm test\n```\n\n## License\n\nMIT © [Alex Malkevich](malkevich.alex@gmail.com)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgund%2Fng-http-interceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgund%2Fng-http-interceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgund%2Fng-http-interceptor/lists"}