@opensourcekd/ng-common-libs 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -3
- package/dist/index.cjs +0 -312
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -194
- package/dist/index.mjs +1 -312
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -794,197 +794,5 @@ declare class Logger {
|
|
|
794
794
|
}): void;
|
|
795
795
|
}
|
|
796
796
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
*
|
|
800
|
-
* @example
|
|
801
|
-
* ```typescript
|
|
802
|
-
* const config: BearerTokenInterceptorConfig = {
|
|
803
|
-
* apiUrl: 'https://api.example.com',
|
|
804
|
-
* getToken: () => authService.getTokenSync(),
|
|
805
|
-
* };
|
|
806
|
-
* ```
|
|
807
|
-
*/
|
|
808
|
-
interface BearerTokenInterceptorConfig {
|
|
809
|
-
/**
|
|
810
|
-
* API base URL to match against — only requests whose URL begins with this
|
|
811
|
-
* string will receive the `Authorization` header.
|
|
812
|
-
* Defaults to {@link APP_CONFIG}.apiUrl when not provided.
|
|
813
|
-
*/
|
|
814
|
-
apiUrl?: string;
|
|
815
|
-
/**
|
|
816
|
-
* Synchronous function that returns the current bearer token string,
|
|
817
|
-
* or `null` when no token is available (e.g. before login).
|
|
818
|
-
*/
|
|
819
|
-
getToken: () => string | null;
|
|
820
|
-
}
|
|
821
|
-
/**
|
|
822
|
-
* BearerTokenInterceptor
|
|
823
|
-
*
|
|
824
|
-
* A per-identifier singleton that patches the global `window.fetch` to
|
|
825
|
-
* automatically attach an `Authorization: Bearer <token>` header to every
|
|
826
|
-
* request whose URL begins with the configured API base URL.
|
|
827
|
-
*
|
|
828
|
-
* **Singleton behaviour**: the first call to {@link BearerTokenInterceptor.getInstance}
|
|
829
|
-
* for a given `id` creates the instance; subsequent calls with the same `id`
|
|
830
|
-
* return the previously created instance, regardless of the `config` argument.
|
|
831
|
-
*
|
|
832
|
-
* Designed for Module Federation micro-frontend environments where both the
|
|
833
|
-
* fetch wrapper and the XHR prototype must be shared across the shell and all
|
|
834
|
-
* remote applications.
|
|
835
|
-
*
|
|
836
|
-
* @example
|
|
837
|
-
* ```typescript
|
|
838
|
-
* import { BearerTokenInterceptor, APP_CONFIG } from '@opensourcekd/ng-common-libs';
|
|
839
|
-
*
|
|
840
|
-
* // In the shell app — creates the instance
|
|
841
|
-
* const interceptor = BearerTokenInterceptor.getInstance('shell', {
|
|
842
|
-
* apiUrl: APP_CONFIG.apiUrl,
|
|
843
|
-
* getToken: () => authService.getTokenSync(),
|
|
844
|
-
* });
|
|
845
|
-
* interceptor.activate();
|
|
846
|
-
*
|
|
847
|
-
* // In any MFE — same instance is returned
|
|
848
|
-
* const same = BearerTokenInterceptor.getInstance('shell', { getToken: () => null });
|
|
849
|
-
* console.log(same === interceptor); // true
|
|
850
|
-
* ```
|
|
851
|
-
*/
|
|
852
|
-
declare class BearerTokenInterceptor {
|
|
853
|
-
private static readonly instances;
|
|
854
|
-
private static originalFetch;
|
|
855
|
-
private static originalXhrOpen;
|
|
856
|
-
private static originalXhrSend;
|
|
857
|
-
private readonly id;
|
|
858
|
-
private readonly apiUrl;
|
|
859
|
-
private readonly tokenFn;
|
|
860
|
-
private active;
|
|
861
|
-
private constructor();
|
|
862
|
-
/**
|
|
863
|
-
* Get or create the singleton interceptor for the given identifier.
|
|
864
|
-
*
|
|
865
|
-
* The first invocation with a given `id` creates the instance using the
|
|
866
|
-
* supplied `config`. Subsequent calls with the same `id` return the existing
|
|
867
|
-
* instance — the `config` argument is ignored on subsequent calls.
|
|
868
|
-
*
|
|
869
|
-
* @param id - Unique identifier for this interceptor (e.g. `'shell'`, `'mfe-orders'`)
|
|
870
|
-
* @param config - Configuration used only when creating a new instance
|
|
871
|
-
* @returns The singleton {@link BearerTokenInterceptor} for the given `id`
|
|
872
|
-
*
|
|
873
|
-
* @example
|
|
874
|
-
* ```typescript
|
|
875
|
-
* const interceptor = BearerTokenInterceptor.getInstance('shell', {
|
|
876
|
-
* apiUrl: 'https://api.example.com',
|
|
877
|
-
* getToken: () => authService.getTokenSync(),
|
|
878
|
-
* });
|
|
879
|
-
* ```
|
|
880
|
-
*/
|
|
881
|
-
static getInstance(id: string, config: BearerTokenInterceptorConfig): BearerTokenInterceptor;
|
|
882
|
-
/**
|
|
883
|
-
* Get the identifier of this interceptor instance.
|
|
884
|
-
* @returns The `id` string supplied when the instance was created
|
|
885
|
-
*/
|
|
886
|
-
getId(): string;
|
|
887
|
-
/**
|
|
888
|
-
* Get the API base URL that this interceptor matches against.
|
|
889
|
-
* @returns The configured API URL string
|
|
890
|
-
*/
|
|
891
|
-
getApiUrl(): string;
|
|
892
|
-
/**
|
|
893
|
-
* Check whether this interceptor is currently active.
|
|
894
|
-
* @returns `true` if the interceptor has been activated and not yet deactivated
|
|
895
|
-
*/
|
|
896
|
-
isActive(): boolean;
|
|
897
|
-
/**
|
|
898
|
-
* Activate the interceptor.
|
|
899
|
-
*
|
|
900
|
-
* Patches `window.fetch` and `XMLHttpRequest` once (on the first active
|
|
901
|
-
* interceptor) so that matching requests receive the bearer token.
|
|
902
|
-
* Subsequent calls are no-ops. No-op in non-browser environments.
|
|
903
|
-
*/
|
|
904
|
-
activate(): void;
|
|
905
|
-
/**
|
|
906
|
-
* Deactivate the interceptor.
|
|
907
|
-
*
|
|
908
|
-
* Stops attaching the bearer token for this interceptor's URL pattern.
|
|
909
|
-
* Both the global fetch wrapper and the XHR prototype patches are removed
|
|
910
|
-
* automatically once all registered interceptors have been deactivated.
|
|
911
|
-
* No-op when already inactive.
|
|
912
|
-
*/
|
|
913
|
-
deactivate(): void;
|
|
914
|
-
/**
|
|
915
|
-
* Return the effective bearer token for a given request URL, or `null` if
|
|
916
|
-
* this interceptor should not modify the request.
|
|
917
|
-
*
|
|
918
|
-
* Returns `null` when the interceptor is inactive, the URL does not begin
|
|
919
|
-
* with the configured API base URL, or the token getter returns `null`.
|
|
920
|
-
*
|
|
921
|
-
* @param url - The absolute URL of the outgoing request
|
|
922
|
-
* @returns Bearer token string or `null`
|
|
923
|
-
*/
|
|
924
|
-
private getEffectiveToken;
|
|
925
|
-
/**
|
|
926
|
-
* Extract headers from a `RequestInit` object into a plain key-value map.
|
|
927
|
-
* Handles `Headers` instances, `[key, value]` arrays, and plain objects.
|
|
928
|
-
*
|
|
929
|
-
* @param init - Optional `RequestInit` whose headers to extract
|
|
930
|
-
* @returns A plain `Record<string, string>` copy of the headers
|
|
931
|
-
*/
|
|
932
|
-
private static extractHeaders;
|
|
933
|
-
/**
|
|
934
|
-
* Find the bearer token for a given URL by consulting all active interceptors.
|
|
935
|
-
* Uses first-match semantics to avoid ambiguity when multiple interceptors
|
|
936
|
-
* share the same API URL prefix.
|
|
937
|
-
*
|
|
938
|
-
* @param url - Absolute URL of the outgoing request
|
|
939
|
-
* @returns The first matching bearer token string, or `null`
|
|
940
|
-
*/
|
|
941
|
-
private static findToken;
|
|
942
|
-
/**
|
|
943
|
-
* Apply the `Authorization: Bearer` header from the first active interceptor
|
|
944
|
-
* that matches the given URL. Uses first-match semantics to avoid ambiguity
|
|
945
|
-
* when multiple interceptors share the same API URL prefix.
|
|
946
|
-
*
|
|
947
|
-
* @param url - Absolute URL of the outgoing fetch request
|
|
948
|
-
* @param init - Original `RequestInit` options
|
|
949
|
-
* @returns Modified `RequestInit` with the `Authorization` header added when applicable
|
|
950
|
-
*/
|
|
951
|
-
private static applyBearerToken;
|
|
952
|
-
/**
|
|
953
|
-
* Install the global fetch wrapper exactly once.
|
|
954
|
-
* The wrapper delegates to {@link applyBearerToken} for every outgoing request.
|
|
955
|
-
* No-op when already patched or in a non-browser environment.
|
|
956
|
-
*/
|
|
957
|
-
private static patchFetch;
|
|
958
|
-
/**
|
|
959
|
-
* Restore the original `window.fetch` if no interceptors remain active.
|
|
960
|
-
* No-op when the fetch has not been patched or some interceptors are still active.
|
|
961
|
-
*/
|
|
962
|
-
private static maybeRestoreFetch;
|
|
963
|
-
/**
|
|
964
|
-
* Install a global `XMLHttpRequest` prototype patch exactly once.
|
|
965
|
-
*
|
|
966
|
-
* Overrides `XMLHttpRequest.prototype.open` to capture the request URL on
|
|
967
|
-
* the XHR instance, and `XMLHttpRequest.prototype.send` to inject the
|
|
968
|
-
* `Authorization: Bearer` header (via `setRequestHeader`) before dispatching
|
|
969
|
-
* the request. Supports Angular's default XHR-based `HttpClient` backend.
|
|
970
|
-
*
|
|
971
|
-
* No-op when already patched or in a non-browser environment.
|
|
972
|
-
*/
|
|
973
|
-
private static patchXhr;
|
|
974
|
-
/**
|
|
975
|
-
* Restore `XMLHttpRequest.prototype.open` and `.send` to their original
|
|
976
|
-
* values if no interceptors remain active.
|
|
977
|
-
* No-op when XHR has not been patched or some interceptors are still active.
|
|
978
|
-
*/
|
|
979
|
-
private static maybeRestoreXhr;
|
|
980
|
-
/**
|
|
981
|
-
* Remove all registered instances and restore `window.fetch` and
|
|
982
|
-
* `XMLHttpRequest` prototype methods to their original values.
|
|
983
|
-
* Intended for use in test teardown only.
|
|
984
|
-
* @internal
|
|
985
|
-
*/
|
|
986
|
-
static _reset(): void;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
export { APP_CONFIG, AUTH0_CONFIG, AuthService, BearerTokenInterceptor, EventBus, LogSeverity, Logger, STANDARD_JWT_CLAIMS, STORAGE_CONFIG, STORAGE_KEYS, buildUserData, configureAuth0, createAuthService, decodeAndStoreToken, extractClaimValue, getCustomClaims, getDecodedToken, getStorageItem, isNamespacedClaim, removeStorageItem, resetAuth0Config, setStorageItem };
|
|
990
|
-
export type { AppState, Auth0Config, Auth0ConfigOptions, AuthServiceOptions, AuthorizationParams, BearerTokenInterceptorConfig, CallbackResult, EventBusOptions, EventPayload, LogAttributes, LogRecord, LoggerOptions, StorageConfig, StorageKeys, TokenPayload, UserData, UserInfo };
|
|
797
|
+
export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, LogSeverity, Logger, STANDARD_JWT_CLAIMS, STORAGE_CONFIG, STORAGE_KEYS, buildUserData, configureAuth0, createAuthService, decodeAndStoreToken, extractClaimValue, getCustomClaims, getDecodedToken, getStorageItem, isNamespacedClaim, removeStorageItem, resetAuth0Config, setStorageItem };
|
|
798
|
+
export type { AppState, Auth0Config, Auth0ConfigOptions, AuthServiceOptions, AuthorizationParams, CallbackResult, EventBusOptions, EventPayload, LogAttributes, LogRecord, LoggerOptions, StorageConfig, StorageKeys, TokenPayload, UserData, UserInfo };
|
package/dist/index.mjs
CHANGED
|
@@ -1071,316 +1071,5 @@ class Logger {
|
|
|
1071
1071
|
}
|
|
1072
1072
|
}
|
|
1073
1073
|
|
|
1074
|
-
|
|
1075
|
-
* BearerTokenInterceptor — HTTP interceptor for bearer token injection.
|
|
1076
|
-
* Patches both `window.fetch` and `XMLHttpRequest` (for Angular's default
|
|
1077
|
-
* HttpClient backend), pure TypeScript, framework-agnostic, truly singleton
|
|
1078
|
-
* per identifier.
|
|
1079
|
-
*/
|
|
1080
|
-
/**
|
|
1081
|
-
* BearerTokenInterceptor
|
|
1082
|
-
*
|
|
1083
|
-
* A per-identifier singleton that patches the global `window.fetch` to
|
|
1084
|
-
* automatically attach an `Authorization: Bearer <token>` header to every
|
|
1085
|
-
* request whose URL begins with the configured API base URL.
|
|
1086
|
-
*
|
|
1087
|
-
* **Singleton behaviour**: the first call to {@link BearerTokenInterceptor.getInstance}
|
|
1088
|
-
* for a given `id` creates the instance; subsequent calls with the same `id`
|
|
1089
|
-
* return the previously created instance, regardless of the `config` argument.
|
|
1090
|
-
*
|
|
1091
|
-
* Designed for Module Federation micro-frontend environments where both the
|
|
1092
|
-
* fetch wrapper and the XHR prototype must be shared across the shell and all
|
|
1093
|
-
* remote applications.
|
|
1094
|
-
*
|
|
1095
|
-
* @example
|
|
1096
|
-
* ```typescript
|
|
1097
|
-
* import { BearerTokenInterceptor, APP_CONFIG } from '@opensourcekd/ng-common-libs';
|
|
1098
|
-
*
|
|
1099
|
-
* // In the shell app — creates the instance
|
|
1100
|
-
* const interceptor = BearerTokenInterceptor.getInstance('shell', {
|
|
1101
|
-
* apiUrl: APP_CONFIG.apiUrl,
|
|
1102
|
-
* getToken: () => authService.getTokenSync(),
|
|
1103
|
-
* });
|
|
1104
|
-
* interceptor.activate();
|
|
1105
|
-
*
|
|
1106
|
-
* // In any MFE — same instance is returned
|
|
1107
|
-
* const same = BearerTokenInterceptor.getInstance('shell', { getToken: () => null });
|
|
1108
|
-
* console.log(same === interceptor); // true
|
|
1109
|
-
* ```
|
|
1110
|
-
*/
|
|
1111
|
-
class BearerTokenInterceptor {
|
|
1112
|
-
static instances = new Map();
|
|
1113
|
-
static originalFetch = null;
|
|
1114
|
-
static originalXhrOpen = null;
|
|
1115
|
-
static originalXhrSend = null;
|
|
1116
|
-
id;
|
|
1117
|
-
apiUrl;
|
|
1118
|
-
tokenFn;
|
|
1119
|
-
active = false;
|
|
1120
|
-
constructor(id, config) {
|
|
1121
|
-
this.id = id;
|
|
1122
|
-
this.apiUrl = config.apiUrl ?? APP_CONFIG.apiUrl;
|
|
1123
|
-
this.tokenFn = config.getToken;
|
|
1124
|
-
}
|
|
1125
|
-
/**
|
|
1126
|
-
* Get or create the singleton interceptor for the given identifier.
|
|
1127
|
-
*
|
|
1128
|
-
* The first invocation with a given `id` creates the instance using the
|
|
1129
|
-
* supplied `config`. Subsequent calls with the same `id` return the existing
|
|
1130
|
-
* instance — the `config` argument is ignored on subsequent calls.
|
|
1131
|
-
*
|
|
1132
|
-
* @param id - Unique identifier for this interceptor (e.g. `'shell'`, `'mfe-orders'`)
|
|
1133
|
-
* @param config - Configuration used only when creating a new instance
|
|
1134
|
-
* @returns The singleton {@link BearerTokenInterceptor} for the given `id`
|
|
1135
|
-
*
|
|
1136
|
-
* @example
|
|
1137
|
-
* ```typescript
|
|
1138
|
-
* const interceptor = BearerTokenInterceptor.getInstance('shell', {
|
|
1139
|
-
* apiUrl: 'https://api.example.com',
|
|
1140
|
-
* getToken: () => authService.getTokenSync(),
|
|
1141
|
-
* });
|
|
1142
|
-
* ```
|
|
1143
|
-
*/
|
|
1144
|
-
static getInstance(id, config) {
|
|
1145
|
-
if (BearerTokenInterceptor.instances.has(id)) {
|
|
1146
|
-
return BearerTokenInterceptor.instances.get(id);
|
|
1147
|
-
}
|
|
1148
|
-
const instance = new BearerTokenInterceptor(id, config);
|
|
1149
|
-
BearerTokenInterceptor.instances.set(id, instance);
|
|
1150
|
-
return instance;
|
|
1151
|
-
}
|
|
1152
|
-
/**
|
|
1153
|
-
* Get the identifier of this interceptor instance.
|
|
1154
|
-
* @returns The `id` string supplied when the instance was created
|
|
1155
|
-
*/
|
|
1156
|
-
getId() {
|
|
1157
|
-
return this.id;
|
|
1158
|
-
}
|
|
1159
|
-
/**
|
|
1160
|
-
* Get the API base URL that this interceptor matches against.
|
|
1161
|
-
* @returns The configured API URL string
|
|
1162
|
-
*/
|
|
1163
|
-
getApiUrl() {
|
|
1164
|
-
return this.apiUrl;
|
|
1165
|
-
}
|
|
1166
|
-
/**
|
|
1167
|
-
* Check whether this interceptor is currently active.
|
|
1168
|
-
* @returns `true` if the interceptor has been activated and not yet deactivated
|
|
1169
|
-
*/
|
|
1170
|
-
isActive() {
|
|
1171
|
-
return this.active;
|
|
1172
|
-
}
|
|
1173
|
-
/**
|
|
1174
|
-
* Activate the interceptor.
|
|
1175
|
-
*
|
|
1176
|
-
* Patches `window.fetch` and `XMLHttpRequest` once (on the first active
|
|
1177
|
-
* interceptor) so that matching requests receive the bearer token.
|
|
1178
|
-
* Subsequent calls are no-ops. No-op in non-browser environments.
|
|
1179
|
-
*/
|
|
1180
|
-
activate() {
|
|
1181
|
-
if (this.active || typeof window === 'undefined')
|
|
1182
|
-
return;
|
|
1183
|
-
this.active = true;
|
|
1184
|
-
BearerTokenInterceptor.patchFetch();
|
|
1185
|
-
BearerTokenInterceptor.patchXhr();
|
|
1186
|
-
}
|
|
1187
|
-
/**
|
|
1188
|
-
* Deactivate the interceptor.
|
|
1189
|
-
*
|
|
1190
|
-
* Stops attaching the bearer token for this interceptor's URL pattern.
|
|
1191
|
-
* Both the global fetch wrapper and the XHR prototype patches are removed
|
|
1192
|
-
* automatically once all registered interceptors have been deactivated.
|
|
1193
|
-
* No-op when already inactive.
|
|
1194
|
-
*/
|
|
1195
|
-
deactivate() {
|
|
1196
|
-
if (!this.active)
|
|
1197
|
-
return;
|
|
1198
|
-
this.active = false;
|
|
1199
|
-
if (typeof window !== 'undefined') {
|
|
1200
|
-
BearerTokenInterceptor.maybeRestoreFetch();
|
|
1201
|
-
BearerTokenInterceptor.maybeRestoreXhr();
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
/**
|
|
1205
|
-
* Return the effective bearer token for a given request URL, or `null` if
|
|
1206
|
-
* this interceptor should not modify the request.
|
|
1207
|
-
*
|
|
1208
|
-
* Returns `null` when the interceptor is inactive, the URL does not begin
|
|
1209
|
-
* with the configured API base URL, or the token getter returns `null`.
|
|
1210
|
-
*
|
|
1211
|
-
* @param url - The absolute URL of the outgoing request
|
|
1212
|
-
* @returns Bearer token string or `null`
|
|
1213
|
-
*/
|
|
1214
|
-
getEffectiveToken(url) {
|
|
1215
|
-
if (!this.active || !url.startsWith(this.apiUrl))
|
|
1216
|
-
return null;
|
|
1217
|
-
return this.tokenFn();
|
|
1218
|
-
}
|
|
1219
|
-
/**
|
|
1220
|
-
* Extract headers from a `RequestInit` object into a plain key-value map.
|
|
1221
|
-
* Handles `Headers` instances, `[key, value]` arrays, and plain objects.
|
|
1222
|
-
*
|
|
1223
|
-
* @param init - Optional `RequestInit` whose headers to extract
|
|
1224
|
-
* @returns A plain `Record<string, string>` copy of the headers
|
|
1225
|
-
*/
|
|
1226
|
-
static extractHeaders(init) {
|
|
1227
|
-
if (!init?.headers)
|
|
1228
|
-
return {};
|
|
1229
|
-
if (init.headers instanceof Headers) {
|
|
1230
|
-
const h = {};
|
|
1231
|
-
init.headers.forEach((v, k) => { h[k] = v; });
|
|
1232
|
-
return h;
|
|
1233
|
-
}
|
|
1234
|
-
if (Array.isArray(init.headers)) {
|
|
1235
|
-
return Object.fromEntries(init.headers);
|
|
1236
|
-
}
|
|
1237
|
-
return { ...init.headers };
|
|
1238
|
-
}
|
|
1239
|
-
/**
|
|
1240
|
-
* Find the bearer token for a given URL by consulting all active interceptors.
|
|
1241
|
-
* Uses first-match semantics to avoid ambiguity when multiple interceptors
|
|
1242
|
-
* share the same API URL prefix.
|
|
1243
|
-
*
|
|
1244
|
-
* @param url - Absolute URL of the outgoing request
|
|
1245
|
-
* @returns The first matching bearer token string, or `null`
|
|
1246
|
-
*/
|
|
1247
|
-
static findToken(url) {
|
|
1248
|
-
return ([...BearerTokenInterceptor.instances.values()]
|
|
1249
|
-
.map((i) => i.getEffectiveToken(url))
|
|
1250
|
-
.find((t) => t !== null) ?? null);
|
|
1251
|
-
}
|
|
1252
|
-
/**
|
|
1253
|
-
* Apply the `Authorization: Bearer` header from the first active interceptor
|
|
1254
|
-
* that matches the given URL. Uses first-match semantics to avoid ambiguity
|
|
1255
|
-
* when multiple interceptors share the same API URL prefix.
|
|
1256
|
-
*
|
|
1257
|
-
* @param url - Absolute URL of the outgoing fetch request
|
|
1258
|
-
* @param init - Original `RequestInit` options
|
|
1259
|
-
* @returns Modified `RequestInit` with the `Authorization` header added when applicable
|
|
1260
|
-
*/
|
|
1261
|
-
static applyBearerToken(url, init) {
|
|
1262
|
-
const headers = BearerTokenInterceptor.extractHeaders(init);
|
|
1263
|
-
const token = BearerTokenInterceptor.findToken(url);
|
|
1264
|
-
if (token) {
|
|
1265
|
-
headers['Authorization'] = `Bearer ${token}`;
|
|
1266
|
-
}
|
|
1267
|
-
return { ...init, headers };
|
|
1268
|
-
}
|
|
1269
|
-
/**
|
|
1270
|
-
* Install the global fetch wrapper exactly once.
|
|
1271
|
-
* The wrapper delegates to {@link applyBearerToken} for every outgoing request.
|
|
1272
|
-
* No-op when already patched or in a non-browser environment.
|
|
1273
|
-
*/
|
|
1274
|
-
static patchFetch() {
|
|
1275
|
-
if (BearerTokenInterceptor.originalFetch !== null || typeof window === 'undefined')
|
|
1276
|
-
return;
|
|
1277
|
-
// Store without bind so the original reference is preserved for identity checks on restore.
|
|
1278
|
-
BearerTokenInterceptor.originalFetch = window.fetch;
|
|
1279
|
-
window.fetch = (input, init) => {
|
|
1280
|
-
// Extract the URL string from all three possible input types:
|
|
1281
|
-
// • Request-like objects (e.g. the Fetch API Request) have a `.url` string property.
|
|
1282
|
-
// • URL objects do NOT have `.url`; String(urlObj) yields the full href (e.g. "https://…").
|
|
1283
|
-
// • Plain strings pass through String() unchanged.
|
|
1284
|
-
const hasUrl = typeof input.url === 'string';
|
|
1285
|
-
const url = hasUrl ? input.url : String(input);
|
|
1286
|
-
return BearerTokenInterceptor.originalFetch(input, BearerTokenInterceptor.applyBearerToken(url, init));
|
|
1287
|
-
};
|
|
1288
|
-
}
|
|
1289
|
-
/**
|
|
1290
|
-
* Restore the original `window.fetch` if no interceptors remain active.
|
|
1291
|
-
* No-op when the fetch has not been patched or some interceptors are still active.
|
|
1292
|
-
*/
|
|
1293
|
-
static maybeRestoreFetch() {
|
|
1294
|
-
if (BearerTokenInterceptor.originalFetch === null)
|
|
1295
|
-
return;
|
|
1296
|
-
const anyActive = [...BearerTokenInterceptor.instances.values()].some((i) => i.active);
|
|
1297
|
-
if (anyActive)
|
|
1298
|
-
return;
|
|
1299
|
-
window.fetch = BearerTokenInterceptor.originalFetch;
|
|
1300
|
-
BearerTokenInterceptor.originalFetch = null;
|
|
1301
|
-
}
|
|
1302
|
-
/**
|
|
1303
|
-
* Install a global `XMLHttpRequest` prototype patch exactly once.
|
|
1304
|
-
*
|
|
1305
|
-
* Overrides `XMLHttpRequest.prototype.open` to capture the request URL on
|
|
1306
|
-
* the XHR instance, and `XMLHttpRequest.prototype.send` to inject the
|
|
1307
|
-
* `Authorization: Bearer` header (via `setRequestHeader`) before dispatching
|
|
1308
|
-
* the request. Supports Angular's default XHR-based `HttpClient` backend.
|
|
1309
|
-
*
|
|
1310
|
-
* No-op when already patched or in a non-browser environment.
|
|
1311
|
-
*/
|
|
1312
|
-
static patchXhr() {
|
|
1313
|
-
if (BearerTokenInterceptor.originalXhrOpen !== null ||
|
|
1314
|
-
typeof window === 'undefined' ||
|
|
1315
|
-
typeof XMLHttpRequest === 'undefined')
|
|
1316
|
-
return;
|
|
1317
|
-
const originalOpen = XMLHttpRequest.prototype.open;
|
|
1318
|
-
const originalSend = XMLHttpRequest.prototype.send;
|
|
1319
|
-
BearerTokenInterceptor.originalXhrOpen = originalOpen;
|
|
1320
|
-
BearerTokenInterceptor.originalXhrSend = originalSend;
|
|
1321
|
-
// Capture the request URL so send() can match it against registered interceptors.
|
|
1322
|
-
// The `async` parameter is made optional to faithfully match both XHR `open()` overloads.
|
|
1323
|
-
XMLHttpRequest.prototype.open = function (method, url, async, username, password) {
|
|
1324
|
-
const urlStr = url instanceof URL ? url.href : url;
|
|
1325
|
-
try {
|
|
1326
|
-
this._interceptorUrl = new URL(urlStr, window.location.href).href;
|
|
1327
|
-
}
|
|
1328
|
-
catch {
|
|
1329
|
-
this._interceptorUrl = urlStr;
|
|
1330
|
-
}
|
|
1331
|
-
// `async` defaults to true per the XHR spec when not supplied by the caller.
|
|
1332
|
-
originalOpen.call(this, method, url, async ?? true, username, password);
|
|
1333
|
-
};
|
|
1334
|
-
// Inject the Authorization header before dispatching the request.
|
|
1335
|
-
XMLHttpRequest.prototype.send = function (body) {
|
|
1336
|
-
const url = this._interceptorUrl;
|
|
1337
|
-
if (url) {
|
|
1338
|
-
const token = BearerTokenInterceptor.findToken(url);
|
|
1339
|
-
if (token) {
|
|
1340
|
-
this.setRequestHeader('Authorization', `Bearer ${token}`);
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
originalSend.call(this, body);
|
|
1344
|
-
};
|
|
1345
|
-
}
|
|
1346
|
-
/**
|
|
1347
|
-
* Restore `XMLHttpRequest.prototype.open` and `.send` to their original
|
|
1348
|
-
* values if no interceptors remain active.
|
|
1349
|
-
* No-op when XHR has not been patched or some interceptors are still active.
|
|
1350
|
-
*/
|
|
1351
|
-
static maybeRestoreXhr() {
|
|
1352
|
-
if (BearerTokenInterceptor.originalXhrOpen === null)
|
|
1353
|
-
return;
|
|
1354
|
-
const anyActive = [...BearerTokenInterceptor.instances.values()].some((i) => i.active);
|
|
1355
|
-
if (anyActive)
|
|
1356
|
-
return;
|
|
1357
|
-
XMLHttpRequest.prototype.open = BearerTokenInterceptor.originalXhrOpen;
|
|
1358
|
-
XMLHttpRequest.prototype.send = BearerTokenInterceptor.originalXhrSend;
|
|
1359
|
-
BearerTokenInterceptor.originalXhrOpen = null;
|
|
1360
|
-
BearerTokenInterceptor.originalXhrSend = null;
|
|
1361
|
-
}
|
|
1362
|
-
/**
|
|
1363
|
-
* Remove all registered instances and restore `window.fetch` and
|
|
1364
|
-
* `XMLHttpRequest` prototype methods to their original values.
|
|
1365
|
-
* Intended for use in test teardown only.
|
|
1366
|
-
* @internal
|
|
1367
|
-
*/
|
|
1368
|
-
static _reset() {
|
|
1369
|
-
if (typeof window !== 'undefined') {
|
|
1370
|
-
if (BearerTokenInterceptor.originalFetch !== null) {
|
|
1371
|
-
window.fetch = BearerTokenInterceptor.originalFetch;
|
|
1372
|
-
}
|
|
1373
|
-
if (BearerTokenInterceptor.originalXhrOpen !== null && typeof XMLHttpRequest !== 'undefined') {
|
|
1374
|
-
XMLHttpRequest.prototype.open = BearerTokenInterceptor.originalXhrOpen;
|
|
1375
|
-
XMLHttpRequest.prototype.send = BearerTokenInterceptor.originalXhrSend;
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
BearerTokenInterceptor.originalFetch = null;
|
|
1379
|
-
BearerTokenInterceptor.originalXhrOpen = null;
|
|
1380
|
-
BearerTokenInterceptor.originalXhrSend = null;
|
|
1381
|
-
BearerTokenInterceptor.instances.clear();
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
export { APP_CONFIG, AUTH0_CONFIG, AuthService, BearerTokenInterceptor, EventBus, LogSeverity, Logger, STANDARD_JWT_CLAIMS, STORAGE_CONFIG, STORAGE_KEYS, buildUserData, configureAuth0, createAuthService, decodeAndStoreToken, extractClaimValue, getCustomClaims, getDecodedToken, getStorageItem, isNamespacedClaim, removeStorageItem, resetAuth0Config, setStorageItem };
|
|
1074
|
+
export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, LogSeverity, Logger, STANDARD_JWT_CLAIMS, STORAGE_CONFIG, STORAGE_KEYS, buildUserData, configureAuth0, createAuthService, decodeAndStoreToken, extractClaimValue, getCustomClaims, getDecodedToken, getStorageItem, isNamespacedClaim, removeStorageItem, resetAuth0Config, setStorageItem };
|
|
1386
1075
|
//# sourceMappingURL=index.mjs.map
|