Gathering detailed insights and metrics for @default-js/defaultjs-httpinterceptor
Gathering detailed insights and metrics for @default-js/defaultjs-httpinterceptor
Gathering detailed insights and metrics for @default-js/defaultjs-httpinterceptor
Gathering detailed insights and metrics for @default-js/defaultjs-httpinterceptor
npm install @default-js/defaultjs-httpinterceptor
Typescript
Module System
Node Version
NPM Version
JavaScript (98.01%)
HTML (1.99%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
30 Commits
1 Watchers
1 Branches
2 Contributors
Updated on Apr 01, 2025
Latest Version
1.3.8
Package Id
@default-js/defaultjs-httpinterceptor@1.3.8
Unpacked Size
23.48 kB
Size
7.09 kB
File Count
15
NPM Version
10.4.0
Node Version
21.6.0
Published on
Apr 01, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
This default-js modul provide functionality to intercept browser request and manipulate them. It's useful web pages with multi backend services on different domains or auhorization to provide the authorization to all requests.
The httpinterceptor works with XMLHttpRequest and fetch, but its supporting async requests only!
1npm install @default-js/defaultjs-httpinterceptor
1import {Manager} from "@default-js/defaultjs-httpinterceptor"; 2Manager.setup(async () => { 3//setup your interceptor(s) 4});
1<script type="application/javascript" src="/dist/defaultjs-httpinterceptor.min.js"></script> 2<script type="application/javascript"> 3 4defaultjs.httpinterceptorManager.setup(async () => { 5//setup your interceptor(s) -> see Javascript api 6}); 7</script>
Include the javascript file and your setup script at head or as first script tags on your body!
Manager
This manager provides the main logic and optimized the request handling by caching the used interceptors by origin.
Manager.setup(setup)
Add your setup logic to the Manager instance and blocks all requests until all setup finished!
Param | Required | Default | Description |
---|---|---|---|
setup | X | function or Promise object |
The result of the setup
can be a single object of Interceptor
, an array of Interceptor
objects or nothing, but then you must be add your Interceptor
object(s) by calling Manager.addInterceptor
manualy.
1import {Manager} from "@default-js/defaultjs-httpinterceptor"; 2Manager.setup(async () => { 3//setup your interceptor(s) 4});
Manager.addInterceptor(interceptor)
Add a Interceptor
object.
Param | Required | Default | Description |
---|---|---|---|
interceptor | X | Interceptor or Array of Interceptor |
1Manager.addInterceptor(new MyInterceptor()); 2Manager.addInterceptor([new InterceptorA(), new InterceptorB(), new InterceptorC()]);
Every interceptor
must be a instance of type Interceptor
or an object with a function doAccept(data)
and doHandle(data)
;
Manager.ready
This property is a Promise, that represented the current setup state.
Manager.ignoreDocumentOrigin
This property is tells the Manager
to ignore all requests with the same origin as the current page.
Manager.addOriginToIgnore(origin)
Add origin to be ignored by Manager
.
Param | Required | Default | Description |
---|---|---|---|
origin | X | string , URL , Array |
Manager.addUrlToIgnore(urls = [array of strings | array of URL | string | URL])
Add url to be ignored by Manager
.
Param | Required | Default | Description |
---|---|---|---|
url | X | string , URL , Array |
Manager.uncheckedFetch(url, request)
This function make a unchecked fetch request. See default browser fetch api.
Param | Required | Default | Description |
---|---|---|---|
url | X | string or URL | |
request | X | object or Request object |
Manager.doIntercept(data)
This function would be called automaticly by fetch
and XMLHttpRequest
to intercept the requests.
Param | Required | Default | Description |
---|---|---|---|
data | X | object |
Manager.reset()
This function reset the interceptor-origin cache.
Interceptor
Basic class to extends by your own interceptor implementation.
Interceptor.doAccept({url, metadata})
This functions must be return true
, if the interceptor apply to all requests by same origin.
Param | Default | Description |
---|---|---|
url | URL | |
metadata | Metadata |
@return: true
or false
Interceptor.doHandle({url, metadata, request})
This function would be called to maipulate the request.
Param | Default | Description |
---|---|---|
url | URL | |
metadata | Metadata | |
request | Object like fetch Request object |
@return: a object of {url, metadata, request}
Interceptor.uncheckedFetch
This function make a unchecked fetch request. See default browser fetch api.
Param | Required | Default | Description |
---|---|---|---|
url | X | string or URL | |
request | X | object or Request object |
TokenInterceptor
The TokenInterceptor
extends Interceptor
and provide logic to cache a auth token and make periodicly a refesh of auth token.
1import TokenInterceptor from "@default-js/defaultjs-httpinterceptor/src/interceptors/TokenInterceptor"; 2new TokenInterceptor(setup)
Structur of setup
Property | Requiered | Description |
---|---|---|
condition | X | string , Array of string , function({url, metadata}) |
fetchToken | X | function({url, metadata}) |
appendToken | function({url, metadata, request}) ; default: add token as header Authorization=Bearer ${token} | |
refreshInterval | number for milliseconds. A number zero or below -> disable automatic token refesh; default: 6000 | |
refreshToken | function({url, metadata}) |
Metadata
This object provide some meta infomation of request.
Property | Required | Description |
---|---|---|
method | X | string -> http verbs |
origin | X | origin of request |
hostname | X | hostname of request |
protocol | X | protocol of request |
port | ||
query | X | query string of request |
Create a new class and extend the class by Interceptor
class. Impement the function doAccept(data)
and doHandle(data)
.
1import Manager from "@default-js/defaultjs-httpinterceptor"; 2import Interceptor from "@default-js/defaultjs-httpinterceptor"; 3 4class MyInterceptor extends Interceptor { 5 #origin; 6 7 constructor(origin){ 8 super(); 9 this.#origin = origin; 10 } 11 12 async doAccept({url, metadata}){ 13 return url.origin == this.#origin; 14 } 15 16 async doHandle({url, metadata, request}){ 17 const token = await this.uncheckedFetch("/url/to/token"); 18 request.headers["Authorization"] = `Bearer ${await token.text()}`; 19 20 return {url, metadata, request}; 21 } 22} 23 24Manager.setup(async () => { 25 const response = await Manager.uncheckedFetch("/request/for/all/origins.json"); 26 const origins = await response.json(); // response: ["example-a.com", "example-b.com", "example-c.com"] 27 const interceptors = origins.map((origin) => new MyInterceptor(origin)); 28 29 return origins 30}); 31 32await fetch("example-a.com");
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
Found 0/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More