@obsidiane/auth-client-js 1.0.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/DEVELOPMENT.md +226 -0
- package/README.md +181 -0
- package/dist/index.js +10 -0
- package/index.ts +10 -0
- package/package.json +45 -0
- package/src/lib/bridge/rest/api-platform.adapter.ts +84 -0
- package/src/lib/bridge/rest/http-request.options.ts +21 -0
- package/src/lib/bridge/rest/query-builder.ts +43 -0
- package/src/lib/bridge/sse/eventsource-wrapper.ts +70 -0
- package/src/lib/bridge/sse/mercure-topic.mapper.ts +48 -0
- package/src/lib/bridge/sse/mercure-url.builder.ts +17 -0
- package/src/lib/bridge/sse/mercure.adapter.ts +261 -0
- package/src/lib/bridge/sse/ref-count-topic.registry.ts +45 -0
- package/src/lib/bridge.types.ts +33 -0
- package/src/lib/facades/bridge.facade.ts +108 -0
- package/src/lib/facades/facade.factory.ts +38 -0
- package/src/lib/facades/facade.interface.ts +30 -0
- package/src/lib/facades/resource.facade.ts +101 -0
- package/src/lib/interceptors/bridge-debug.interceptor.ts +32 -0
- package/src/lib/interceptors/bridge-defaults.interceptor.ts +53 -0
- package/src/lib/interceptors/content-type.interceptor.ts +49 -0
- package/src/lib/interceptors/singleflight.interceptor.ts +55 -0
- package/src/lib/ports/realtime.port.ts +36 -0
- package/src/lib/ports/resource-repository.port.ts +78 -0
- package/src/lib/provide-bridge.ts +148 -0
- package/src/lib/tokens.ts +20 -0
- package/src/lib/utils/url.ts +15 -0
- package/src/models/Auth.ts +5 -0
- package/src/models/AuthInviteCompleteInputInviteComplete.ts +7 -0
- package/src/models/AuthInviteUserInputInviteSend.ts +5 -0
- package/src/models/AuthLdJson.ts +5 -0
- package/src/models/AuthPasswordForgotInputPasswordForgot.ts +5 -0
- package/src/models/AuthPasswordResetInputPasswordReset.ts +6 -0
- package/src/models/AuthRegisterUserInputUserRegister.ts +6 -0
- package/src/models/FrontendConfig.ts +12 -0
- package/src/models/InvitePreview.ts +8 -0
- package/src/models/InviteUserInviteRead.ts +9 -0
- package/src/models/Setup.ts +5 -0
- package/src/models/SetupRegisterUserInputUserRegister.ts +6 -0
- package/src/models/UserUpdateUserRolesInputUserRoles.ts +5 -0
- package/src/models/UserUserRead.ts +9 -0
- package/src/models/index.ts +14 -0
- package/src/public-api.ts +9 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {HttpInterceptorFn, HttpResponse} from '@angular/common/http';
|
|
2
|
+
import {inject} from '@angular/core';
|
|
3
|
+
import {BRIDGE_LOGGER} from '../tokens';
|
|
4
|
+
import {catchError, finalize, tap, throwError} from 'rxjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Lightweight request/response logging controlled by `provideBridge({debug: true})`.
|
|
8
|
+
* Logs are delegated to the injected `BRIDGE_LOGGER`.
|
|
9
|
+
*/
|
|
10
|
+
export const bridgeDebugInterceptor: HttpInterceptorFn = (req, next) => {
|
|
11
|
+
const logger = inject(BRIDGE_LOGGER, {optional: true});
|
|
12
|
+
if (!logger) return next(req);
|
|
13
|
+
|
|
14
|
+
const startedAt = Date.now();
|
|
15
|
+
logger.debug('[Bridge] request', {method: req.method, url: req.urlWithParams});
|
|
16
|
+
|
|
17
|
+
return next(req).pipe(
|
|
18
|
+
tap((evt) => {
|
|
19
|
+
if (evt instanceof HttpResponse) {
|
|
20
|
+
logger.debug('[Bridge] response', {method: req.method, url: req.urlWithParams, status: evt.status});
|
|
21
|
+
}
|
|
22
|
+
}),
|
|
23
|
+
catchError((err) => {
|
|
24
|
+
logger.error('[Bridge] error', {method: req.method, url: req.urlWithParams, err});
|
|
25
|
+
return throwError(() => err);
|
|
26
|
+
}),
|
|
27
|
+
finalize(() => {
|
|
28
|
+
const durationMs = Date.now() - startedAt;
|
|
29
|
+
logger.debug('[Bridge] done', {method: req.method, url: req.urlWithParams, durationMs});
|
|
30
|
+
})
|
|
31
|
+
);
|
|
32
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {HttpInterceptorFn} from '@angular/common/http';
|
|
2
|
+
import {inject} from '@angular/core';
|
|
3
|
+
import {BRIDGE_DEFAULTS, BRIDGE_LOGGER} from '../tokens';
|
|
4
|
+
import {retry, timeout, timer} from 'rxjs';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_RETRY_METHODS = ['GET', 'HEAD', 'OPTIONS'];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Applies default headers, timeout and retry policy configured via `provideBridge({defaults: ...})`.
|
|
10
|
+
*/
|
|
11
|
+
export const bridgeDefaultsInterceptor: HttpInterceptorFn = (req, next) => {
|
|
12
|
+
const defaults = inject(BRIDGE_DEFAULTS, {optional: true}) ?? {};
|
|
13
|
+
const logger = inject(BRIDGE_LOGGER, {optional: true});
|
|
14
|
+
|
|
15
|
+
let nextReq = req;
|
|
16
|
+
|
|
17
|
+
if (defaults.headers) {
|
|
18
|
+
for (const [k, v] of Object.entries(defaults.headers)) {
|
|
19
|
+
if (!nextReq.headers.has(k)) {
|
|
20
|
+
nextReq = nextReq.clone({headers: nextReq.headers.set(k, v)});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let out$ = next(nextReq);
|
|
26
|
+
|
|
27
|
+
const timeoutMs = typeof defaults.timeoutMs === 'number' ? defaults.timeoutMs : undefined;
|
|
28
|
+
if (timeoutMs && timeoutMs > 0) {
|
|
29
|
+
out$ = out$.pipe(timeout({first: timeoutMs}));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const retryCfg = defaults.retries;
|
|
33
|
+
const retryCount = typeof retryCfg === 'number' ? retryCfg : retryCfg?.count;
|
|
34
|
+
if (retryCount && retryCount > 0) {
|
|
35
|
+
const methods = (typeof retryCfg === 'object' && retryCfg.methods) ? retryCfg.methods : DEFAULT_RETRY_METHODS;
|
|
36
|
+
const normalizedMethod = req.method.toUpperCase();
|
|
37
|
+
const methodAllowList = new Set(methods.map((m) => m.toUpperCase()));
|
|
38
|
+
if (methodAllowList.has(normalizedMethod)) {
|
|
39
|
+
const delayMs = typeof retryCfg === 'object' ? (retryCfg.delayMs ?? 250) : 250;
|
|
40
|
+
out$ = out$.pipe(
|
|
41
|
+
retry({
|
|
42
|
+
count: retryCount,
|
|
43
|
+
delay: (_err, retryIndex) => {
|
|
44
|
+
logger?.debug?.('[Bridge] retry', {url: req.urlWithParams, method: req.method, retryIndex});
|
|
45
|
+
return timer(delayMs);
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return out$;
|
|
53
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {HttpInterceptorFn} from '@angular/common/http';
|
|
2
|
+
|
|
3
|
+
export interface ApiMediaTypes {
|
|
4
|
+
accept: string;
|
|
5
|
+
post: string;
|
|
6
|
+
put: string;
|
|
7
|
+
patch: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_MEDIA_TYPES: ApiMediaTypes = {
|
|
11
|
+
accept: 'application/ld+json',
|
|
12
|
+
post: 'application/ld+json',
|
|
13
|
+
put: 'application/ld+json',
|
|
14
|
+
patch: 'application/merge-patch+json',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const contentTypeInterceptor: HttpInterceptorFn = (req, next) => {
|
|
18
|
+
const mediaTypes = DEFAULT_MEDIA_TYPES;
|
|
19
|
+
let headers = req.headers;
|
|
20
|
+
|
|
21
|
+
// Set Accept if missing.
|
|
22
|
+
if (!headers.has('Accept')) {
|
|
23
|
+
headers = headers.set('Accept', mediaTypes.accept);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Only methods with a body.
|
|
27
|
+
const isBodyMethod = ['POST', 'PUT', 'PATCH'].includes(req.method);
|
|
28
|
+
// Ignore FormData (browser sets multipart boundary).
|
|
29
|
+
const isFormData = typeof FormData !== 'undefined' && req.body instanceof FormData;
|
|
30
|
+
|
|
31
|
+
if (isBodyMethod && !isFormData && !headers.has('Content-Type')) {
|
|
32
|
+
let contentType: string;
|
|
33
|
+
|
|
34
|
+
switch (req.method) {
|
|
35
|
+
case 'PATCH':
|
|
36
|
+
contentType = mediaTypes.patch;
|
|
37
|
+
break;
|
|
38
|
+
case 'PUT':
|
|
39
|
+
contentType = mediaTypes.put;
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
contentType = mediaTypes.post;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
headers = headers.set('Content-Type', contentType);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return next(req.clone({headers}));
|
|
49
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {HttpEvent, HttpInterceptorFn, HttpRequest} from '@angular/common/http';
|
|
2
|
+
import {finalize, Observable, shareReplay} from 'rxjs';
|
|
3
|
+
|
|
4
|
+
export type SingleFlightMode = 'off' | 'safe';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_MODE: SingleFlightMode = 'safe';
|
|
7
|
+
const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
|
|
8
|
+
|
|
9
|
+
export function createSingleFlightInterceptor(mode: SingleFlightMode = DEFAULT_MODE): HttpInterceptorFn {
|
|
10
|
+
const inflight = new Map<string, Observable<HttpEvent<unknown>>>();
|
|
11
|
+
|
|
12
|
+
return (req, next) => {
|
|
13
|
+
if (mode === 'off') {
|
|
14
|
+
return next(req);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const method = req.method.toUpperCase();
|
|
18
|
+
if (!SAFE_METHODS.has(method)) {
|
|
19
|
+
return next(req);
|
|
20
|
+
}
|
|
21
|
+
if (req.reportProgress === true) {
|
|
22
|
+
return next(req);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const key = computeKey(req);
|
|
26
|
+
const existing = inflight.get(key);
|
|
27
|
+
if (existing) {
|
|
28
|
+
return existing;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const shared$ = next(req).pipe(
|
|
32
|
+
finalize(() => inflight.delete(key)),
|
|
33
|
+
shareReplay({bufferSize: 1, refCount: true})
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
inflight.set(key, shared$);
|
|
37
|
+
return shared$;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function computeKey(req: HttpRequest<unknown>): string {
|
|
42
|
+
const auth = req.headers.get('Authorization') ?? '';
|
|
43
|
+
const accept = req.headers.get('Accept') ?? '';
|
|
44
|
+
const contentType = req.headers.get('Content-Type') ?? '';
|
|
45
|
+
const creds = req.withCredentials ? '1' : '0';
|
|
46
|
+
return [
|
|
47
|
+
req.method.toUpperCase(),
|
|
48
|
+
req.urlWithParams,
|
|
49
|
+
`rt=${req.responseType}`,
|
|
50
|
+
`wc=${creds}`,
|
|
51
|
+
`a=${auth}`,
|
|
52
|
+
`acc=${accept}`,
|
|
53
|
+
`ct=${contentType}`,
|
|
54
|
+
].join('::');
|
|
55
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {Observable} from 'rxjs';
|
|
2
|
+
import {Iri} from './resource-repository.port';
|
|
3
|
+
|
|
4
|
+
export type RealtimeStatus = 'connecting' | 'connected' | 'closed';
|
|
5
|
+
|
|
6
|
+
export interface SseEvent {
|
|
7
|
+
type: string;
|
|
8
|
+
data: string;
|
|
9
|
+
lastEventId?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SseOptions {
|
|
13
|
+
withCredentials?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RealtimeEvent<T> {
|
|
17
|
+
iri: string;
|
|
18
|
+
data?: T;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type SubscribeFilter = {
|
|
22
|
+
field: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export interface RealtimePort {
|
|
26
|
+
/**
|
|
27
|
+
* Subscribes to Mercure events for the given topics.
|
|
28
|
+
*
|
|
29
|
+
* Note: topics must be stable strings. Undefined values are ignored by the adapter.
|
|
30
|
+
*/
|
|
31
|
+
subscribe$<T>(iris: Iri[], filter?: SubscribeFilter): Observable<RealtimeEvent<T>>;
|
|
32
|
+
|
|
33
|
+
unsubscribe(iris: Iri[]): void;
|
|
34
|
+
|
|
35
|
+
status$(): Observable<RealtimeStatus>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {HttpHeaders, HttpParams} from '@angular/common/http';
|
|
2
|
+
import {Observable} from 'rxjs';
|
|
3
|
+
|
|
4
|
+
export type Iri = string | undefined;
|
|
5
|
+
export type IriRequired = string;
|
|
6
|
+
|
|
7
|
+
type BaseHttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
8
|
+
export type HttpMethod = BaseHttpMethod | Lowercase<BaseHttpMethod>;
|
|
9
|
+
|
|
10
|
+
export type QueryParamValue = string | number | boolean | Array<string | number | boolean>;
|
|
11
|
+
export type AnyQuery = Query | Record<string, QueryParamValue> | HttpParams;
|
|
12
|
+
|
|
13
|
+
export interface Item {
|
|
14
|
+
'@id'?: Iri;
|
|
15
|
+
'@context'?: string;
|
|
16
|
+
'@type'?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface View extends Item {
|
|
20
|
+
first?: Iri;
|
|
21
|
+
last?: Iri;
|
|
22
|
+
next?: Iri;
|
|
23
|
+
previous?: Iri;
|
|
24
|
+
}
|
|
25
|
+
export interface IriTemplateMapping extends Item {
|
|
26
|
+
variable: string;
|
|
27
|
+
property?: string;
|
|
28
|
+
required?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface IriTemplate extends Item {
|
|
31
|
+
template: string;
|
|
32
|
+
variableRepresentation?: string;
|
|
33
|
+
mapping: IriTemplateMapping[];
|
|
34
|
+
}
|
|
35
|
+
export interface Collection<T> extends Item {
|
|
36
|
+
member: T[];
|
|
37
|
+
totalItems?: number;
|
|
38
|
+
search?: IriTemplate;
|
|
39
|
+
view?: View;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Query {
|
|
43
|
+
itemsPerPage?: number;
|
|
44
|
+
page?: number;
|
|
45
|
+
filters?: Record<string, QueryParamValue>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface HttpCallOptions {
|
|
49
|
+
headers?: HttpHeaders | Record<string, string>;
|
|
50
|
+
withCredentials?: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ResourceRepository<T> {
|
|
54
|
+
getCollection$(query?: AnyQuery, opts?: HttpCallOptions): Observable<Collection<T>>;
|
|
55
|
+
|
|
56
|
+
get$(iri: IriRequired, opts?: HttpCallOptions): Observable<T>;
|
|
57
|
+
|
|
58
|
+
post$(payload: Partial<T>, opts?: HttpCallOptions): Observable<T>;
|
|
59
|
+
|
|
60
|
+
patch$(iri: IriRequired, changes: Partial<T>, opts?: HttpCallOptions): Observable<T>;
|
|
61
|
+
|
|
62
|
+
put$(iri: IriRequired, payload: Partial<T>, opts?: HttpCallOptions): Observable<T>;
|
|
63
|
+
|
|
64
|
+
delete$(iri: IriRequired, opts?: HttpCallOptions): Observable<void>;
|
|
65
|
+
|
|
66
|
+
request$<R = unknown, B = unknown>(req: HttpRequestConfig<B>): Observable<R>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface HttpRequestConfig<TBody = unknown> {
|
|
70
|
+
method: HttpMethod;
|
|
71
|
+
url?: Iri;
|
|
72
|
+
query?: AnyQuery;
|
|
73
|
+
body?: TBody;
|
|
74
|
+
headers?: HttpHeaders | Record<string, string>;
|
|
75
|
+
responseType?: 'json' | 'text' | 'blob';
|
|
76
|
+
withCredentials?: boolean;
|
|
77
|
+
options?: Record<string, unknown>;
|
|
78
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {EnvironmentProviders, makeEnvironmentProviders} from '@angular/core';
|
|
2
|
+
import {HttpInterceptorFn, provideHttpClient, withFetch, withInterceptors} from '@angular/common/http';
|
|
3
|
+
import {
|
|
4
|
+
API_BASE_URL,
|
|
5
|
+
BRIDGE_DEFAULTS,
|
|
6
|
+
BRIDGE_LOGGER,
|
|
7
|
+
BRIDGE_WITH_CREDENTIALS,
|
|
8
|
+
MERCURE_HUB_URL,
|
|
9
|
+
MERCURE_TOPIC_MODE,
|
|
10
|
+
} from './tokens';
|
|
11
|
+
import {BridgeDefaults, BridgeLogger, MercureTopicMode} from './bridge.types';
|
|
12
|
+
import {contentTypeInterceptor} from './interceptors/content-type.interceptor';
|
|
13
|
+
import {bridgeDefaultsInterceptor} from './interceptors/bridge-defaults.interceptor';
|
|
14
|
+
import {bridgeDebugInterceptor} from './interceptors/bridge-debug.interceptor';
|
|
15
|
+
import {from, switchMap} from 'rxjs';
|
|
16
|
+
import {createSingleFlightInterceptor} from './interceptors/singleflight.interceptor';
|
|
17
|
+
|
|
18
|
+
export type BridgeAuth =
|
|
19
|
+
| string
|
|
20
|
+
| {type: 'bearer'; token: string}
|
|
21
|
+
| {type: 'bearer'; getToken: () => string | undefined | Promise<string | undefined>}
|
|
22
|
+
| HttpInterceptorFn;
|
|
23
|
+
|
|
24
|
+
export interface BridgeMercureOptions {
|
|
25
|
+
hubUrl?: string;
|
|
26
|
+
init?: RequestInit;
|
|
27
|
+
topicMode?: MercureTopicMode;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface BridgeOptions {
|
|
31
|
+
/** Base URL of the API (e.g. `http://localhost:8000`). */
|
|
32
|
+
baseUrl: string;
|
|
33
|
+
/** Auth strategy used to attach an Authorization header. */
|
|
34
|
+
auth?: BridgeAuth;
|
|
35
|
+
/**
|
|
36
|
+
* Use `topicMode` to control the `topic=` values sent to the hub.
|
|
37
|
+
*/
|
|
38
|
+
mercure?: BridgeMercureOptions;
|
|
39
|
+
/** Default HTTP behaviour (headers, timeout, retries). */
|
|
40
|
+
defaults?: BridgeDefaults;
|
|
41
|
+
/**
|
|
42
|
+
* De-duplicates in-flight HTTP requests.
|
|
43
|
+
*
|
|
44
|
+
* - `true` (default): single-flight for safe methods (`GET/HEAD/OPTIONS`)
|
|
45
|
+
* - `false`: disabled (each call triggers a new request)
|
|
46
|
+
*/
|
|
47
|
+
singleFlight?: boolean;
|
|
48
|
+
/** Enables debug logging via the debug interceptor and console logger. */
|
|
49
|
+
debug?: boolean;
|
|
50
|
+
/** Extra `HttpInterceptorFn` applied after bridge interceptors. */
|
|
51
|
+
extraInterceptors?: HttpInterceptorFn[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Registers the bridge HTTP client, interceptors, Mercure realtime adapter and configuration tokens. */
|
|
55
|
+
export function provideBridge(opts: BridgeOptions): EnvironmentProviders {
|
|
56
|
+
const {
|
|
57
|
+
baseUrl,
|
|
58
|
+
auth,
|
|
59
|
+
mercure,
|
|
60
|
+
defaults,
|
|
61
|
+
singleFlight = true,
|
|
62
|
+
debug = false,
|
|
63
|
+
extraInterceptors = [],
|
|
64
|
+
} = opts;
|
|
65
|
+
|
|
66
|
+
if (!baseUrl) {
|
|
67
|
+
throw new Error("provideBridge(): missing 'baseUrl'");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const resolvedMercureInit: RequestInit = mercure?.init ?? {credentials: 'include' as RequestCredentials};
|
|
71
|
+
const resolvedMercureHubUrl = mercure?.hubUrl;
|
|
72
|
+
const resolvedMercureTopicMode: MercureTopicMode = mercure?.topicMode ?? 'url';
|
|
73
|
+
const withCredentials = resolveWithCredentials(resolvedMercureInit);
|
|
74
|
+
|
|
75
|
+
const loggerProvider: BridgeLogger = createBridgeLogger(debug);
|
|
76
|
+
|
|
77
|
+
const interceptors: HttpInterceptorFn[] = [
|
|
78
|
+
contentTypeInterceptor,
|
|
79
|
+
bridgeDefaultsInterceptor,
|
|
80
|
+
...createAuthInterceptors(auth),
|
|
81
|
+
...(singleFlight ? [createSingleFlightInterceptor('safe')] : [createSingleFlightInterceptor('off')]),
|
|
82
|
+
...(debug ? [bridgeDebugInterceptor] : []),
|
|
83
|
+
...extraInterceptors,
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
return makeEnvironmentProviders([
|
|
87
|
+
provideHttpClient(
|
|
88
|
+
withFetch(),
|
|
89
|
+
withInterceptors(interceptors)
|
|
90
|
+
),
|
|
91
|
+
{provide: API_BASE_URL, useValue: baseUrl},
|
|
92
|
+
{provide: BRIDGE_WITH_CREDENTIALS, useValue: withCredentials},
|
|
93
|
+
...(resolvedMercureHubUrl ? [{provide: MERCURE_HUB_URL, useValue: resolvedMercureHubUrl}] : []),
|
|
94
|
+
{provide: MERCURE_TOPIC_MODE, useValue: resolvedMercureTopicMode},
|
|
95
|
+
{provide: BRIDGE_DEFAULTS, useValue: defaults ?? {}},
|
|
96
|
+
{provide: BRIDGE_LOGGER, useValue: loggerProvider},
|
|
97
|
+
]);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function createBridgeLogger(debug: boolean): BridgeLogger {
|
|
101
|
+
const noop = () => undefined;
|
|
102
|
+
return {
|
|
103
|
+
debug: debug ? console.debug.bind(console) : noop,
|
|
104
|
+
info: debug ? console.info.bind(console) : noop,
|
|
105
|
+
warn: debug ? console.warn.bind(console) : noop,
|
|
106
|
+
error: console.error.bind(console),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function resolveWithCredentials(init: RequestInit | undefined): boolean {
|
|
111
|
+
if (!init) return false;
|
|
112
|
+
const anyInit = init as RequestInit & {withCredentials?: boolean};
|
|
113
|
+
return anyInit.withCredentials === true || init.credentials === 'include';
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function createAuthInterceptors(auth?: BridgeAuth): HttpInterceptorFn[] {
|
|
117
|
+
if (!auth) return [];
|
|
118
|
+
|
|
119
|
+
if (typeof auth === 'function') {
|
|
120
|
+
return [auth as HttpInterceptorFn];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const bearer =
|
|
124
|
+
typeof auth === 'string'
|
|
125
|
+
? {type: 'bearer' as const, token: auth}
|
|
126
|
+
: (auth as Exclude<BridgeAuth, string | HttpInterceptorFn>);
|
|
127
|
+
|
|
128
|
+
if (bearer.type !== 'bearer') return [];
|
|
129
|
+
|
|
130
|
+
if ('token' in bearer) {
|
|
131
|
+
const token = bearer.token;
|
|
132
|
+
return [createBearerAuthInterceptor(() => token)];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return [createBearerAuthInterceptor(bearer.getToken)];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function createBearerAuthInterceptor(getToken: () => string | undefined | Promise<string | undefined>): HttpInterceptorFn {
|
|
139
|
+
return (req, next) => {
|
|
140
|
+
if (req.headers.has('Authorization')) return next(req);
|
|
141
|
+
return from(Promise.resolve(getToken())).pipe(
|
|
142
|
+
switchMap((token) => {
|
|
143
|
+
if (!token) return next(req);
|
|
144
|
+
return next(req.clone({headers: req.headers.set('Authorization', `Bearer ${token}`)}));
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {InjectionToken} from '@angular/core';
|
|
2
|
+
import {BridgeDefaults, BridgeLogger, MercureTopicMode} from './bridge.types';
|
|
3
|
+
|
|
4
|
+
/** Base URL of the API (e.g. `http://localhost:8000`). */
|
|
5
|
+
export const API_BASE_URL = new InjectionToken<string>('API_BASE_URL');
|
|
6
|
+
|
|
7
|
+
/** Mercure hub URL (e.g. `http://localhost:8000/.well-known/mercure`). */
|
|
8
|
+
export const MERCURE_HUB_URL = new InjectionToken<string>('MERCURE_HUB_URL');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Default credential policy for HTTP requests and Mercure EventSource.
|
|
12
|
+
* When `true`, the bridge sets `withCredentials: true` on HTTP calls and uses cookies for SSE.
|
|
13
|
+
*/
|
|
14
|
+
export const BRIDGE_WITH_CREDENTIALS = new InjectionToken<boolean>('BRIDGE_WITH_CREDENTIALS');
|
|
15
|
+
|
|
16
|
+
export const MERCURE_TOPIC_MODE = new InjectionToken<MercureTopicMode>('MERCURE_TOPIC_MODE');
|
|
17
|
+
|
|
18
|
+
export const BRIDGE_LOGGER = new InjectionToken<BridgeLogger>('BRIDGE_LOGGER');
|
|
19
|
+
|
|
20
|
+
export const BRIDGE_DEFAULTS = new InjectionToken<BridgeDefaults>('BRIDGE_DEFAULTS');
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function joinUrl(base: string, path: string): string {
|
|
2
|
+
const normalizedBase = base.endsWith('/') ? base.slice(0, -1) : base;
|
|
3
|
+
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
|
|
4
|
+
return `${normalizedBase}${normalizedPath}`;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resolves an API IRI (e.g. `/api/books/1`) to a full URL using the API base.
|
|
9
|
+
* If `path` is already absolute (http/https), it is returned as-is.
|
|
10
|
+
*/
|
|
11
|
+
export function resolveUrl(base: string, path: string): string {
|
|
12
|
+
if (/^https?:\/\//i.test(path)) return path;
|
|
13
|
+
if (path.startsWith('//')) return path;
|
|
14
|
+
return joinUrl(base, path);
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Item } from '../lib/ports/resource-repository.port';
|
|
2
|
+
|
|
3
|
+
export interface FrontendConfig extends Item {
|
|
4
|
+
id?: string | null;
|
|
5
|
+
registrationEnabled?: boolean;
|
|
6
|
+
passwordStrengthLevel?: number;
|
|
7
|
+
brandingName?: string;
|
|
8
|
+
frontendRedirectUrl?: string;
|
|
9
|
+
environment?: string;
|
|
10
|
+
themeMode?: string;
|
|
11
|
+
themeColor?: string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type { Auth } from './Auth';
|
|
2
|
+
export type { AuthInviteCompleteInputInviteComplete } from './AuthInviteCompleteInputInviteComplete';
|
|
3
|
+
export type { AuthInviteUserInputInviteSend } from './AuthInviteUserInputInviteSend';
|
|
4
|
+
export type { AuthLdJson } from './AuthLdJson';
|
|
5
|
+
export type { AuthPasswordForgotInputPasswordForgot } from './AuthPasswordForgotInputPasswordForgot';
|
|
6
|
+
export type { AuthPasswordResetInputPasswordReset } from './AuthPasswordResetInputPasswordReset';
|
|
7
|
+
export type { AuthRegisterUserInputUserRegister } from './AuthRegisterUserInputUserRegister';
|
|
8
|
+
export type { FrontendConfig } from './FrontendConfig';
|
|
9
|
+
export type { InvitePreview } from './InvitePreview';
|
|
10
|
+
export type { InviteUserInviteRead } from './InviteUserInviteRead';
|
|
11
|
+
export type { Setup } from './Setup';
|
|
12
|
+
export type { SetupRegisterUserInputUserRegister } from './SetupRegisterUserInputUserRegister';
|
|
13
|
+
export type { UserUserRead } from './UserUserRead';
|
|
14
|
+
export type { UserUpdateUserRolesInputUserRoles } from './UserUpdateUserRolesInputUserRoles';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './lib/provide-bridge';
|
|
2
|
+
export * from './lib/bridge.types';
|
|
3
|
+
export * from './lib/facades/facade.factory';
|
|
4
|
+
export * from './lib/facades/bridge.facade';
|
|
5
|
+
export * from './lib/facades/resource.facade';
|
|
6
|
+
export * from './lib/ports/resource-repository.port';
|
|
7
|
+
export * from './lib/utils/url';
|
|
8
|
+
|
|
9
|
+
export * from './models';
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2022", "dom"],
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": false,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./",
|
|
11
|
+
"strict": true,
|
|
12
|
+
"noImplicitAny": false,
|
|
13
|
+
"experimentalDecorators": true,
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
"moduleResolution": "bundler",
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"baseUrl": "./"
|
|
20
|
+
},
|
|
21
|
+
"include": ["index.ts", "src/**/*"],
|
|
22
|
+
"exclude": ["node_modules", "dist"]
|
|
23
|
+
}
|