@auth-craft/client 0.1.0 → 0.1.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 +144 -8
- package/dist/core/core.d.ts +72 -0
- package/dist/core/core.d.ts.map +1 -0
- package/dist/core/core.js +105 -0
- package/dist/core/core.js.map +1 -0
- package/dist/core/proxy.d.ts +79 -0
- package/dist/core/proxy.d.ts.map +1 -0
- package/dist/core/proxy.js +99 -0
- package/dist/core/proxy.js.map +1 -0
- package/dist/index.d.ts +15 -28
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -55
- package/dist/index.js.map +1 -1
- package/dist/transport/client.d.ts +250 -0
- package/dist/transport/client.d.ts.map +1 -0
- package/dist/transport/client.js +940 -0
- package/dist/transport/client.js.map +1 -0
- package/dist/transport/constants.d.ts +9 -0
- package/dist/transport/constants.d.ts.map +1 -0
- package/dist/transport/constants.js +9 -0
- package/dist/transport/constants.js.map +1 -0
- package/dist/transport/error-codes.d.ts +41 -0
- package/dist/transport/error-codes.d.ts.map +1 -0
- package/dist/transport/error-codes.js +38 -0
- package/dist/transport/error-codes.js.map +1 -0
- package/dist/transport/errors.d.ts +55 -0
- package/dist/transport/errors.d.ts.map +1 -0
- package/dist/transport/errors.js +55 -0
- package/dist/transport/errors.js.map +1 -0
- package/dist/transport/helpers.d.ts +135 -0
- package/dist/transport/helpers.d.ts.map +1 -0
- package/dist/transport/helpers.js +185 -0
- package/dist/transport/helpers.js.map +1 -0
- package/dist/transport/index.d.ts +22 -0
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/transport/index.js +32 -0
- package/dist/transport/index.js.map +1 -0
- package/dist/transport/messages.d.ts +104 -0
- package/dist/transport/messages.d.ts.map +1 -0
- package/dist/transport/messages.js +24 -0
- package/dist/transport/messages.js.map +1 -0
- package/dist/transport/provider/create-provider.d.ts +38 -0
- package/dist/transport/provider/create-provider.d.ts.map +1 -0
- package/dist/transport/provider/create-provider.js +119 -0
- package/dist/transport/provider/create-provider.js.map +1 -0
- package/dist/transport/provider/parser/body.d.ts +7 -0
- package/dist/transport/provider/parser/body.d.ts.map +1 -0
- package/dist/transport/provider/parser/body.js +17 -0
- package/dist/transport/provider/parser/body.js.map +1 -0
- package/dist/transport/provider/parser/cookie.d.ts +8 -0
- package/dist/transport/provider/parser/cookie.d.ts.map +1 -0
- package/dist/transport/provider/parser/cookie.js +17 -0
- package/dist/transport/provider/parser/cookie.js.map +1 -0
- package/dist/transport/provider/parser/normalize.d.ts +6 -0
- package/dist/transport/provider/parser/normalize.d.ts.map +1 -0
- package/dist/transport/provider/parser/normalize.js +21 -0
- package/dist/transport/provider/parser/normalize.js.map +1 -0
- package/dist/transport/provider/presets.d.ts +31 -0
- package/dist/transport/provider/presets.d.ts.map +1 -0
- package/dist/transport/provider/presets.js +35 -0
- package/dist/transport/provider/presets.js.map +1 -0
- package/dist/transport/provider/register-presets.d.ts +10 -0
- package/dist/transport/provider/register-presets.d.ts.map +1 -0
- package/dist/transport/provider/register-presets.js +32 -0
- package/dist/transport/provider/register-presets.js.map +1 -0
- package/dist/transport/provider/storage/indexeddb.d.ts +26 -0
- package/dist/transport/provider/storage/indexeddb.d.ts.map +1 -0
- package/dist/transport/provider/storage/indexeddb.js +72 -0
- package/dist/transport/provider/storage/indexeddb.js.map +1 -0
- package/dist/transport/provider/strategy/body.d.ts +28 -0
- package/dist/transport/provider/strategy/body.d.ts.map +1 -0
- package/dist/transport/provider/strategy/body.js +73 -0
- package/dist/transport/provider/strategy/body.js.map +1 -0
- package/dist/transport/provider/strategy/cookie.d.ts +29 -0
- package/dist/transport/provider/strategy/cookie.d.ts.map +1 -0
- package/dist/transport/provider/strategy/cookie.js +70 -0
- package/dist/transport/provider/strategy/cookie.js.map +1 -0
- package/dist/transport/result.d.ts +28 -0
- package/dist/transport/result.d.ts.map +1 -0
- package/dist/transport/result.js +87 -0
- package/dist/transport/result.js.map +1 -0
- package/dist/transport/types.d.ts +488 -0
- package/dist/transport/types.d.ts.map +1 -0
- package/dist/transport/types.js +3 -0
- package/dist/transport/types.js.map +1 -0
- package/dist/transport/utils/binary.d.ts +20 -0
- package/dist/transport/utils/binary.d.ts.map +1 -0
- package/dist/transport/utils/binary.js +51 -0
- package/dist/transport/utils/binary.js.map +1 -0
- package/dist/transport/utils/formdata.d.ts +26 -0
- package/dist/transport/utils/formdata.d.ts.map +1 -0
- package/dist/transport/utils/formdata.js +77 -0
- package/dist/transport/utils/formdata.js.map +1 -0
- package/dist/transport/utils/registry.d.ts +26 -0
- package/dist/transport/utils/registry.d.ts.map +1 -0
- package/dist/transport/utils/registry.js +51 -0
- package/dist/transport/utils/registry.js.map +1 -0
- package/dist/transport/worker-post.d.ts +44 -0
- package/dist/transport/worker-post.d.ts.map +1 -0
- package/dist/transport/worker-post.js +104 -0
- package/dist/transport/worker-post.js.map +1 -0
- package/dist/transport/worker.d.ts +10 -0
- package/dist/transport/worker.d.ts.map +1 -0
- package/dist/transport/worker.js +421 -0
- package/dist/transport/worker.js.map +1 -0
- package/dist/types.test-d.js +3 -9
- package/dist/types.test-d.js.map +1 -1
- package/eslint-isolation.mjs +82 -0
- package/package.json +30 -16
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/transport/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC1D,YAAY,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EACL,UAAU,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,aAAa,GACd,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,YAAY,EACZ,eAAe,EACf,SAAS,EACT,SAAS,EACT,aAAa,EACb,cAAc,EACd,aAAa,EACb,SAAS,EACT,WAAW,EACX,SAAS,GACV,MAAM,WAAW,CAAC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WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,kBAAkB,GACnB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Main exports
|
|
2
|
+
export { createClient, FetchGuardClient } from './client';
|
|
3
|
+
// Error codes for type-safe error matching
|
|
4
|
+
export { ERROR_CODES } from './error-codes';
|
|
5
|
+
// Organized error definitions
|
|
6
|
+
export { AuthErrors, DomainErrors, GeneralErrors, InitErrors, RequestErrors, } from './errors';
|
|
7
|
+
// Helper functions for common Result patterns
|
|
8
|
+
export { getErrorBody, getErrorMessage, getStatus, hasStatus, isClientError, isNetworkError, isServerError, isSuccess, matchResult, parseJson, } from './helpers';
|
|
9
|
+
// Message protocol types (for advanced usage)
|
|
10
|
+
export { MSG } from './messages';
|
|
11
|
+
// Provider factory
|
|
12
|
+
export { createProvider } from './provider/create-provider';
|
|
13
|
+
// Parser modules
|
|
14
|
+
export { bodyParser } from './provider/parser/body';
|
|
15
|
+
export { cookieParser } from './provider/parser/cookie';
|
|
16
|
+
// Preset providers (recommended)
|
|
17
|
+
export { createBodyProvider, createCookieProvider, } from './provider/presets';
|
|
18
|
+
// Storage modules
|
|
19
|
+
export { createIndexedDBStorage } from './provider/storage/indexeddb';
|
|
20
|
+
export { bodyStrategy, createBodyStrategy } from './provider/strategy/body';
|
|
21
|
+
// Strategy modules
|
|
22
|
+
export { cookieStrategy, createCookieStrategy } from './provider/strategy/cookie';
|
|
23
|
+
// Result parser — deserialize a backend ts-micro-result envelope into Result<T>.
|
|
24
|
+
// Used by callJson() and by the contract-driven typed client.
|
|
25
|
+
export { deserializeResult, toResult } from './result';
|
|
26
|
+
// Binary utilities (for decoding binary responses)
|
|
27
|
+
export { base64ToArrayBuffer, isBinaryContentType, } from './utils/binary';
|
|
28
|
+
// FormData utilities (for advanced usage)
|
|
29
|
+
export { deserializeFormData, isFormData, isSerializedFormData, serializeFormData, } from './utils/formdata';
|
|
30
|
+
// Registry utilities
|
|
31
|
+
export { clearProviders, getProvider, hasProvider, listProviders, registerProvider, unregisterProvider, } from './utils/registry';
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/transport/index.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE1D,2CAA2C;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,8BAA8B;AAC9B,OAAO,EACL,UAAU,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,aAAa,GACd,MAAM,UAAU,CAAC;AAClB,8CAA8C;AAC9C,OAAO,EACL,YAAY,EACZ,eAAe,EACf,SAAS,EACT,SAAS,EACT,aAAa,EACb,cAAc,EACd,aAAa,EACb,SAAS,EACT,WAAW,EACX,SAAS,GACV,MAAM,WAAW,CAAC;AAMnB,8CAA8C;AAC9C,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAGjC,mBAAmB;AACnB,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,iBAAiB;AACjB,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,iCAAiC;AACjC,OAAO,EACL,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,oBAAoB,CAAC;AAE5B,kBAAkB;AAClB,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC5E,mBAAmB;AACnB,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClF,iFAAiF;AACjF,8DAA8D;AAC9D,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AA4BvD,mDAAmD;AACnD,OAAO,EACL,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AACxB,0CAA0C;AAC1C,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,qBAAqB;AACrB,OAAO,EACL,cAAc,EACd,WAAW,EACX,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,kBAAkB,GACnB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { ErrorDetail, ResultMeta } from 'ts-micro-result';
|
|
2
|
+
import type { AuthResult, FetchEnvelope, FetchGuardRequestInit, ProviderPresetConfig, RefreshReason, WorkerConfig } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* MESSAGE PAYLOADS - SINGLE SOURCE OF TRUTH
|
|
5
|
+
*
|
|
6
|
+
* Define all message payloads here. Type unions and MSG constants are auto-generated.
|
|
7
|
+
*
|
|
8
|
+
* USAGE:
|
|
9
|
+
* - To add a new message: Just add one line to the appropriate interface
|
|
10
|
+
* - Payload types are automatically inferred
|
|
11
|
+
* - MSG constants are automatically generated
|
|
12
|
+
*
|
|
13
|
+
* EXAMPLE:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* interface MainPayloads {
|
|
16
|
+
* NEW_MESSAGE: { foo: string } // Add this line
|
|
17
|
+
* }
|
|
18
|
+
* // => Automatically get MainToWorkerMessage union with NEW_MESSAGE
|
|
19
|
+
* // => Automatically get MSG.NEW_MESSAGE = 'NEW_MESSAGE'
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Payloads for messages sent from Main thread → Worker thread
|
|
24
|
+
*/
|
|
25
|
+
export interface MainPayloads {
|
|
26
|
+
SETUP: {
|
|
27
|
+
config: WorkerConfig;
|
|
28
|
+
providerConfig: ProviderPresetConfig | string | null;
|
|
29
|
+
};
|
|
30
|
+
FETCH: {
|
|
31
|
+
url: string;
|
|
32
|
+
options?: FetchGuardRequestInit;
|
|
33
|
+
};
|
|
34
|
+
AUTH_CALL: {
|
|
35
|
+
method: string;
|
|
36
|
+
args: unknown[];
|
|
37
|
+
emitEvent?: boolean;
|
|
38
|
+
};
|
|
39
|
+
CANCEL: undefined;
|
|
40
|
+
PING: {
|
|
41
|
+
timestamp: number;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Payloads for messages sent from Worker thread → Main thread
|
|
46
|
+
*/
|
|
47
|
+
export interface WorkerPayloads {
|
|
48
|
+
ERROR: {
|
|
49
|
+
errors: readonly ErrorDetail[];
|
|
50
|
+
meta?: ResultMeta;
|
|
51
|
+
status?: number;
|
|
52
|
+
};
|
|
53
|
+
READY: undefined;
|
|
54
|
+
SETUP_ERROR: {
|
|
55
|
+
error: string;
|
|
56
|
+
};
|
|
57
|
+
PONG: {
|
|
58
|
+
timestamp: number;
|
|
59
|
+
};
|
|
60
|
+
LOG: {
|
|
61
|
+
level: 'info' | 'warn' | 'error';
|
|
62
|
+
message: string;
|
|
63
|
+
};
|
|
64
|
+
AUTH_STATE_CHANGED: AuthResult;
|
|
65
|
+
AUTH_CALL_RESULT: AuthResult;
|
|
66
|
+
FETCH_RESULT: FetchEnvelope;
|
|
67
|
+
FETCH_ERROR: {
|
|
68
|
+
error: string;
|
|
69
|
+
status?: number;
|
|
70
|
+
};
|
|
71
|
+
TOKEN_REFRESHED: {
|
|
72
|
+
reason: RefreshReason;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Generate message type from payload definition
|
|
77
|
+
* Handles optional payloads (undefined) gracefully
|
|
78
|
+
*/
|
|
79
|
+
type MessageFromPayloads<P> = {
|
|
80
|
+
[K in keyof P]: {
|
|
81
|
+
id: string;
|
|
82
|
+
type: K;
|
|
83
|
+
} & (P[K] extends undefined ? Record<never, never> : {
|
|
84
|
+
payload: P[K];
|
|
85
|
+
});
|
|
86
|
+
}[keyof P];
|
|
87
|
+
/**
|
|
88
|
+
* Message type unions - auto-generated from payload interfaces
|
|
89
|
+
*/
|
|
90
|
+
export type MainToWorkerMessage = MessageFromPayloads<MainPayloads>;
|
|
91
|
+
export type WorkerToMainMessage = MessageFromPayloads<WorkerPayloads>;
|
|
92
|
+
/**
|
|
93
|
+
* Message type unions for compile-time type checking
|
|
94
|
+
*/
|
|
95
|
+
export type MainType = keyof MainPayloads;
|
|
96
|
+
export type WorkerType = keyof WorkerPayloads;
|
|
97
|
+
export type MessageType = MainType | WorkerType;
|
|
98
|
+
/**
|
|
99
|
+
* MSG constants object
|
|
100
|
+
* Usage: MSG.SETUP, MSG.FETCH, etc.
|
|
101
|
+
*/
|
|
102
|
+
export declare const MSG: { readonly [K in MessageType]: K; };
|
|
103
|
+
export {};
|
|
104
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/transport/messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC/D,OAAO,KAAK,EACV,UAAU,EACV,aAAa,EACb,qBAAqB,EACrB,oBAAoB,EACpB,aAAa,EACb,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE;QAAE,MAAM,EAAE,YAAY,CAAC;QAAC,cAAc,EAAE,oBAAoB,GAAG,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IACtF,KAAK,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,qBAAqB,CAAA;KAAE,CAAC;IACxD,SAAS,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,OAAO,EAAE,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,C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,YAAY,CAAC,CAAC;AACpE,MAAM,MAAM,mBAAmB,GAAG,mBAAmB,CAAC,cAAc,CAAC,CAAC;AAEtE;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC;AAC1C,MAAM,MAAM,UAAU,GAAG,MAAM,cAAc,CAAC;AAC9C,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,UAAU,CAAC;AAEhD;;;GAGG;AACH,eAAO,MAAM,GAAG,EAmBV,EAAE,QAAQ,EAAE,CAAC,IAAI,WAAW,GAAG,CAAC,GAAE,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MSG constants object
|
|
3
|
+
* Usage: MSG.SETUP, MSG.FETCH, etc.
|
|
4
|
+
*/
|
|
5
|
+
export const MSG = Object.freeze({
|
|
6
|
+
// Main -> Worker messages
|
|
7
|
+
SETUP: 'SETUP',
|
|
8
|
+
FETCH: 'FETCH',
|
|
9
|
+
AUTH_CALL: 'AUTH_CALL',
|
|
10
|
+
CANCEL: 'CANCEL',
|
|
11
|
+
PING: 'PING',
|
|
12
|
+
// Worker -> Main messages
|
|
13
|
+
ERROR: 'ERROR',
|
|
14
|
+
READY: 'READY',
|
|
15
|
+
SETUP_ERROR: 'SETUP_ERROR',
|
|
16
|
+
PONG: 'PONG',
|
|
17
|
+
LOG: 'LOG',
|
|
18
|
+
AUTH_STATE_CHANGED: 'AUTH_STATE_CHANGED',
|
|
19
|
+
AUTH_CALL_RESULT: 'AUTH_CALL_RESULT',
|
|
20
|
+
FETCH_RESULT: 'FETCH_RESULT',
|
|
21
|
+
FETCH_ERROR: 'FETCH_ERROR',
|
|
22
|
+
TOKEN_REFRESHED: 'TOKEN_REFRESHED',
|
|
23
|
+
});
|
|
24
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/transport/messages.ts"],"names":[],"mappings":"AA8EA;;;GAGG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC;IAC/B,0BAA0B;IAC1B,KAAK,EAAE,OAAO;IACd,KAAK,EAAE,OAAO;IACd,SAAS,EAAE,WAAW;IACtB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;IAEZ,0BAA0B;IAC1B,KAAK,EAAE,OAAO;IACd,KAAK,EAAE,OAAO;IACd,WAAW,EAAE,aAAa;IAC1B,IAAI,EAAE,MAAM;IACZ,GAAG,EAAE,KAAK;IACV,kBAAkB,EAAE,oBAAoB;IACxC,gBAAgB,EAAE,kBAAkB;IACpC,YAAY,EAAE,cAAc;IAC5B,WAAW,EAAE,aAAa;IAC1B,eAAe,EAAE,iBAAiB;CACnC,CAAuC,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Result } from 'ts-micro-result';
|
|
2
|
+
import type { AuthStrategy, RefreshTokenStorage, TokenInfo, TokenParser, TokenProvider } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Custom auth method type
|
|
5
|
+
*/
|
|
6
|
+
type CustomAuthMethod = (...args: unknown[]) => Promise<Result<TokenInfo>>;
|
|
7
|
+
/**
|
|
8
|
+
* Configuration for creating provider
|
|
9
|
+
*
|
|
10
|
+
* refreshStorage: OPTIONAL - to load refresh token initially when worker starts
|
|
11
|
+
* - undefined: cookie-based auth (httpOnly cookie, no need to load)
|
|
12
|
+
* - RefreshTokenStorage: body-based auth (load from IndexedDB on startup)
|
|
13
|
+
*
|
|
14
|
+
* strategy: AuthStrategy with refresh (required), login/logout (required)
|
|
15
|
+
*
|
|
16
|
+
* customMethods: OPTIONAL - custom auth methods (loginWithPhone, loginWithGoogle, etc.)
|
|
17
|
+
*/
|
|
18
|
+
export interface ProviderConfig {
|
|
19
|
+
refreshStorage?: RefreshTokenStorage;
|
|
20
|
+
parser: TokenParser;
|
|
21
|
+
strategy: AuthStrategy;
|
|
22
|
+
customMethods?: Record<string, CustomAuthMethod>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Factory function to create TokenProvider from modular components
|
|
26
|
+
*
|
|
27
|
+
* Provider automatically handles refresh token:
|
|
28
|
+
* - If refreshToken is null and storage exists → load from storage initially
|
|
29
|
+
* - If refreshToken exists → use token from worker memory
|
|
30
|
+
* - Cookie-based (no storage) → always null
|
|
31
|
+
*
|
|
32
|
+
* Custom methods:
|
|
33
|
+
* - User can add custom auth methods (loginWithPhone, loginWithGoogle, etc.)
|
|
34
|
+
* - Custom methods will be spread into provider object
|
|
35
|
+
*/
|
|
36
|
+
export declare function createProvider(config: ProviderConfig): TokenProvider;
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=create-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-provider.d.ts","sourceRoot":"","sources":["../../../src/transport/provider/create-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEvD,OAAO,KAAK,EACV,YAAY,EAEZ,mBAAmB,EACnB,SAAS,EACT,WAAW,EACX,aAAa,EACd,MAAM,UAAU,CAAC;AAElB;;GAEG;AACH,KAAK,gBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;AAE3E;;;;;;;;;;GAUG;AACH,MAAM,WAAW,cAAc;IAC7B,cAAc,CAAC,EAAE,mBAAmB,CAAC;IACrC,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,YAAY,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAClD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,GAAG,aAAa,CAyHpE"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { err, ok } from 'ts-micro-result';
|
|
2
|
+
import { AuthErrors, RequestErrors } from '../errors';
|
|
3
|
+
/**
|
|
4
|
+
* Factory function to create TokenProvider from modular components
|
|
5
|
+
*
|
|
6
|
+
* Provider automatically handles refresh token:
|
|
7
|
+
* - If refreshToken is null and storage exists → load from storage initially
|
|
8
|
+
* - If refreshToken exists → use token from worker memory
|
|
9
|
+
* - Cookie-based (no storage) → always null
|
|
10
|
+
*
|
|
11
|
+
* Custom methods:
|
|
12
|
+
* - User can add custom auth methods (loginWithPhone, loginWithGoogle, etc.)
|
|
13
|
+
* - Custom methods will be spread into provider object
|
|
14
|
+
*/
|
|
15
|
+
export function createProvider(config) {
|
|
16
|
+
const baseProvider = {
|
|
17
|
+
async refreshToken(refreshToken) {
|
|
18
|
+
// Load from storage if no token provided (null or undefined)
|
|
19
|
+
let currentRefreshToken = refreshToken ?? null;
|
|
20
|
+
if (currentRefreshToken === null && config.refreshStorage) {
|
|
21
|
+
currentRefreshToken = await config.refreshStorage.get();
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const response = await config.strategy.refresh(currentRefreshToken);
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
// Read response body for error details
|
|
27
|
+
const body = await response.text().catch(() => '');
|
|
28
|
+
return err(AuthErrors.TokenRefreshFailed(), { params: { body, status: response.status } });
|
|
29
|
+
}
|
|
30
|
+
const tokenInfo = await config.parser.parse(response);
|
|
31
|
+
if (!tokenInfo.token) {
|
|
32
|
+
return err(AuthErrors.TokenRefreshFailed({ message: 'No access token in response' }));
|
|
33
|
+
}
|
|
34
|
+
if (config.refreshStorage && tokenInfo.refreshToken) {
|
|
35
|
+
await config.refreshStorage.set(tokenInfo.refreshToken);
|
|
36
|
+
}
|
|
37
|
+
return ok(tokenInfo);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
return err(RequestErrors.NetworkError({ message: String(error) }));
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
async login(payload, url) {
|
|
44
|
+
try {
|
|
45
|
+
const response = await config.strategy.login(payload, url);
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
// Read response body for error details
|
|
48
|
+
const body = await response.text().catch(() => '');
|
|
49
|
+
return err(AuthErrors.LoginFailed(), { params: { body, status: response.status } });
|
|
50
|
+
}
|
|
51
|
+
const tokenInfo = await config.parser.parse(response);
|
|
52
|
+
if (!tokenInfo.token) {
|
|
53
|
+
return err(AuthErrors.LoginFailed({ message: 'No access token in response' }));
|
|
54
|
+
}
|
|
55
|
+
if (config.refreshStorage && tokenInfo.refreshToken) {
|
|
56
|
+
await config.refreshStorage.set(tokenInfo.refreshToken);
|
|
57
|
+
}
|
|
58
|
+
return ok(tokenInfo);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
return err(RequestErrors.NetworkError({ message: String(error) }));
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
async logout(payload) {
|
|
65
|
+
try {
|
|
66
|
+
const response = await config.strategy.logout(payload);
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
// Read response body for error details
|
|
69
|
+
const body = await response.text().catch(() => '');
|
|
70
|
+
return err(AuthErrors.LogoutFailed(), { params: { body, status: response.status } });
|
|
71
|
+
}
|
|
72
|
+
if (config.refreshStorage) {
|
|
73
|
+
await config.refreshStorage.set(null);
|
|
74
|
+
}
|
|
75
|
+
return ok({
|
|
76
|
+
token: '',
|
|
77
|
+
refreshToken: undefined,
|
|
78
|
+
expiresAt: undefined,
|
|
79
|
+
user: null, // Explicitly clear user on logout
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return err(RequestErrors.NetworkError({ message: String(error) }));
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
async exchangeToken(accessToken, url, options = {}) {
|
|
87
|
+
if (!accessToken) {
|
|
88
|
+
return err(AuthErrors.NotAuthenticated());
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const response = await config.strategy.exchangeToken(accessToken, url, options);
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
const body = await response.text().catch(() => '');
|
|
94
|
+
return err(AuthErrors.TokenExchangeFailed(), { params: { body, status: response.status } });
|
|
95
|
+
}
|
|
96
|
+
const tokenInfo = await config.parser.parse(response);
|
|
97
|
+
if (!tokenInfo.token) {
|
|
98
|
+
return err(AuthErrors.TokenExchangeFailed({ message: 'No access token in response' }));
|
|
99
|
+
}
|
|
100
|
+
if (config.refreshStorage && tokenInfo.refreshToken) {
|
|
101
|
+
await config.refreshStorage.set(tokenInfo.refreshToken);
|
|
102
|
+
}
|
|
103
|
+
return ok(tokenInfo);
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
return err(RequestErrors.NetworkError({ message: String(error) }));
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
// Merge custom methods if provided
|
|
111
|
+
if (config.customMethods) {
|
|
112
|
+
return {
|
|
113
|
+
...baseProvider,
|
|
114
|
+
...config.customMethods,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return baseProvider;
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=create-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-provider.js","sourceRoot":"","sources":["../../../src/transport/provider/create-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAe,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAiCtD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,cAAc,CAAC,MAAsB;IACnD,MAAM,YAAY,GAA+E;QAC/F,KAAK,CAAC,YAAY,CAAC,YAA4B;YAC7C,6DAA6D;YAC7D,IAAI,mBAAmB,GAAG,YAAY,IAAI,IAAI,CAAC;YAC/C,IAAI,mBAAmB,KAAK,IAAI,IAAI,MAAM,CAAC,cAAc,EAAE,C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E,CAAC,CAAC;oBACnD,OAAO,GAAG,CAAC,UAAU,CAAC,mBAAmB,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;gBAC9F,CAAC;gBAED,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;gBACtD,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;oBACrB,OAAO,GAAG,CAAC,UAAU,CAAC,mBAAmB,CAAC,EAAE,OAAO,EAAE,6BAA6B,EAAE,CAAC,CAAC,CAAC;gBACzF,CAAC;gBAED,IAAI,MAAM,CAAC,cAAc,IAAI,SAAS,CAAC,YAAY,EAAE,CAAC;oBACpD,MAAM,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;gBAC1D,CAAC;gBAED,OAAO,EAAE,CAAC,SAAS,CAAC,CAAC;YACvB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,GAAG,CAAC,aAAa,CAAC,YAAY,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;YACrE,CAAC;QACH,CAAC;KACF,CAAC;IAEF,mCAAmC;IACnC,IAAI,MAAM,CAAC,aAAa,EAAE,CAAC;QACzB,OAAO;YACL,GAAG,YAAY;YACf,GAAG,MAAM,CAAC,aAAa;SACP,CAAC;IACrB,CAAC;IAED,OAAO,YAA6B,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { TokenParser } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Body parser - parse token from response body (JSON)
|
|
4
|
+
* Expects response format: { data: { accessToken, refreshToken, expiresAt?, user? } }
|
|
5
|
+
*/
|
|
6
|
+
export declare const bodyParser: TokenParser;
|
|
7
|
+
//# sourceMappingURL=body.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body.d.ts","sourceRoot":"","sources":["../../../../src/transport/provider/parser/body.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG/C;;;GAGG;AACH,eAAO,MAAM,UAAU,EAAE,WAUxB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { normalizeExpiresAt } from './normalize';
|
|
2
|
+
/**
|
|
3
|
+
* Body parser - parse token from response body (JSON)
|
|
4
|
+
* Expects response format: { data: { accessToken, refreshToken, expiresAt?, user? } }
|
|
5
|
+
*/
|
|
6
|
+
export const bodyParser = {
|
|
7
|
+
async parse(response) {
|
|
8
|
+
const json = await response.clone().json();
|
|
9
|
+
return {
|
|
10
|
+
token: json.data.accessToken,
|
|
11
|
+
refreshToken: json.data.refreshToken,
|
|
12
|
+
expiresAt: normalizeExpiresAt(json.data.expiresAt),
|
|
13
|
+
user: json.data.user,
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=body.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body.js","sourceRoot":"","sources":["../../../../src/transport/provider/parser/body.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAgB;IACrC,KAAK,CAAC,KAAK,CAAC,QAAQ;QAClB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;QAC3C,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,WAAW;YAC5B,YAAY,EAAE,IAAI,CAAC,IAAI,CAAC,YAAY;YACpC,SAAS,EAAE,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;YAClD,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI;SACrB,CAAC;IACJ,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { TokenParser } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Cookie parser - parse access token from response body
|
|
4
|
+
* Expects response format: { data: { accessToken, expiresAt?, user? } }
|
|
5
|
+
* Refresh token is automatically set by backend into httpOnly cookie
|
|
6
|
+
*/
|
|
7
|
+
export declare const cookieParser: TokenParser;
|
|
8
|
+
//# sourceMappingURL=cookie.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../../src/transport/provider/parser/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG/C;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,WAS1B,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { normalizeExpiresAt } from './normalize';
|
|
2
|
+
/**
|
|
3
|
+
* Cookie parser - parse access token from response body
|
|
4
|
+
* Expects response format: { data: { accessToken, expiresAt?, user? } }
|
|
5
|
+
* Refresh token is automatically set by backend into httpOnly cookie
|
|
6
|
+
*/
|
|
7
|
+
export const cookieParser = {
|
|
8
|
+
async parse(response) {
|
|
9
|
+
const json = await response.clone().json();
|
|
10
|
+
return {
|
|
11
|
+
token: json.data.accessToken,
|
|
12
|
+
expiresAt: normalizeExpiresAt(json.data.expiresAt),
|
|
13
|
+
user: json.data.user,
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=cookie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../../src/transport/provider/parser/cookie.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAgB;IACvC,KAAK,CAAC,KAAK,CAAC,QAAQ;QAClB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;QAC3C,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,WAAW;YAC5B,SAAS,EAAE,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;YAClD,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI;SACrB,CAAC;IACJ,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../../../src/transport/provider/parser/normalize.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAiBrE"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize expiresAt to milliseconds timestamp
|
|
3
|
+
* Supports: milliseconds, seconds, ISO string, null/undefined
|
|
4
|
+
*/
|
|
5
|
+
export function normalizeExpiresAt(value) {
|
|
6
|
+
if (value == null)
|
|
7
|
+
return undefined;
|
|
8
|
+
if (typeof value === 'number') {
|
|
9
|
+
// Detect seconds vs milliseconds:
|
|
10
|
+
// - Milliseconds: 13+ digits (e.g., 1767860146000)
|
|
11
|
+
// - Seconds: 10 digits (e.g., 1767860146)
|
|
12
|
+
// Threshold: 10^12 (Sep 2001 in ms, year 33658 in seconds)
|
|
13
|
+
return value < 1e12 ? value * 1000 : value;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === 'string') {
|
|
16
|
+
const ts = Date.parse(value);
|
|
17
|
+
return Number.isNaN(ts) ? undefined : ts;
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=normalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../../../../src/transport/provider/parser/normalize.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,IAAI,KAAK,IAAI,IAAI;QAAE,OAAO,SAAS,CAAC;IAEpC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,kCAAkC;QAClC,mDAAmD;QACnD,0CAA0C;QAC1C,2DAA2D;QAC3D,OAAO,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IAC7C,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAC3C,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { TokenProvider } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Cookie Provider - uses httpOnly cookies
|
|
4
|
+
* Suitable for SSR and cross-domain authentication
|
|
5
|
+
*
|
|
6
|
+
* Access token: Worker memory
|
|
7
|
+
* Refresh token: httpOnly cookie (managed by backend)
|
|
8
|
+
*/
|
|
9
|
+
export declare function createCookieProvider(config: {
|
|
10
|
+
refreshUrl: string;
|
|
11
|
+
loginUrl: string;
|
|
12
|
+
logoutUrl: string;
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
defaultHeaders?: Record<string, string>;
|
|
15
|
+
}): TokenProvider;
|
|
16
|
+
/**
|
|
17
|
+
* Body Provider - refresh token in response body, persisted to IndexedDB
|
|
18
|
+
* Suitable for SPA applications
|
|
19
|
+
*
|
|
20
|
+
* Access token: Worker memory
|
|
21
|
+
* Refresh token: IndexedDB (persists across reload)
|
|
22
|
+
*/
|
|
23
|
+
export declare function createBodyProvider(config: {
|
|
24
|
+
refreshUrl: string;
|
|
25
|
+
loginUrl: string;
|
|
26
|
+
logoutUrl: string;
|
|
27
|
+
refreshTokenKey?: string;
|
|
28
|
+
headers?: Record<string, string>;
|
|
29
|
+
defaultHeaders?: Record<string, string>;
|
|
30
|
+
}): TokenProvider;
|
|
31
|
+
//# sourceMappingURL=presets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presets.d.ts","sourceRoot":"","sources":["../../../src/transport/provider/presets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAQ9C;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACzC,GAAG,aAAa,CAMhB;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACzC,GAAG,aAAa,CAMhB"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createProvider } from './create-provider';
|
|
2
|
+
import { bodyParser } from './parser/body';
|
|
3
|
+
import { cookieParser } from './parser/cookie';
|
|
4
|
+
import { createIndexedDBStorage } from './storage/indexeddb';
|
|
5
|
+
import { createBodyStrategy } from './strategy/body';
|
|
6
|
+
import { createCookieStrategy } from './strategy/cookie';
|
|
7
|
+
/**
|
|
8
|
+
* Cookie Provider - uses httpOnly cookies
|
|
9
|
+
* Suitable for SSR and cross-domain authentication
|
|
10
|
+
*
|
|
11
|
+
* Access token: Worker memory
|
|
12
|
+
* Refresh token: httpOnly cookie (managed by backend)
|
|
13
|
+
*/
|
|
14
|
+
export function createCookieProvider(config) {
|
|
15
|
+
return createProvider({
|
|
16
|
+
refreshStorage: undefined,
|
|
17
|
+
parser: cookieParser,
|
|
18
|
+
strategy: createCookieStrategy(config),
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Body Provider - refresh token in response body, persisted to IndexedDB
|
|
23
|
+
* Suitable for SPA applications
|
|
24
|
+
*
|
|
25
|
+
* Access token: Worker memory
|
|
26
|
+
* Refresh token: IndexedDB (persists across reload)
|
|
27
|
+
*/
|
|
28
|
+
export function createBodyProvider(config) {
|
|
29
|
+
return createProvider({
|
|
30
|
+
refreshStorage: createIndexedDBStorage('FetchGuardDB', config.refreshTokenKey || 'refreshToken'),
|
|
31
|
+
parser: bodyParser,
|
|
32
|
+
strategy: createBodyStrategy(config),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=presets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presets.js","sourceRoot":"","sources":["../../../src/transport/provider/presets.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAMpC;IACC,OAAO,cAAc,CAAC;QACpB,cAAc,EAAE,SAAS;QACzB,MAAM,EAAE,YAAY;QACpB,QAAQ,EAAE,oBAAoB,CAAC,MAAM,CAAC;KACvC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAOlC;IACC,OAAO,cAAc,CAAC;QACpB,cAAc,EAAE,sBAAsB,CAAC,cAAc,EAAE,MAAM,CAAC,eAAe,IAAI,cAAc,CAAC;QAChG,MAAM,EAAE,UAAU;QAClB,QAAQ,EAAE,kBAAkB,CAAC,MAAM,CAAC;KACrC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ProviderPresetConfig, TokenProvider } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Build provider from preset config
|
|
4
|
+
* This is called in worker when receiving SETUP message
|
|
5
|
+
*
|
|
6
|
+
* @param config - Provider preset config
|
|
7
|
+
* @param defaultHeaders - Default headers from FetchGuardOptions (applied to all requests including auth)
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildProviderFromPreset(config: ProviderPresetConfig, defaultHeaders?: Record<string, string>): TokenProvider;
|
|
10
|
+
//# sourceMappingURL=register-presets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register-presets.d.ts","sourceRoot":"","sources":["../../../src/transport/provider/register-presets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGpE;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,oBAAoB,EAC5B,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACtC,aAAa,CAwBf"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createBodyProvider, createCookieProvider } from './presets';
|
|
2
|
+
/**
|
|
3
|
+
* Build provider from preset config
|
|
4
|
+
* This is called in worker when receiving SETUP message
|
|
5
|
+
*
|
|
6
|
+
* @param config - Provider preset config
|
|
7
|
+
* @param defaultHeaders - Default headers from FetchGuardOptions (applied to all requests including auth)
|
|
8
|
+
*/
|
|
9
|
+
export function buildProviderFromPreset(config, defaultHeaders) {
|
|
10
|
+
switch (config.type) {
|
|
11
|
+
case 'cookie-auth':
|
|
12
|
+
return createCookieProvider({
|
|
13
|
+
refreshUrl: config.refreshUrl,
|
|
14
|
+
loginUrl: config.loginUrl,
|
|
15
|
+
logoutUrl: config.logoutUrl,
|
|
16
|
+
headers: config.headers,
|
|
17
|
+
defaultHeaders,
|
|
18
|
+
});
|
|
19
|
+
case 'body-auth':
|
|
20
|
+
return createBodyProvider({
|
|
21
|
+
refreshUrl: config.refreshUrl,
|
|
22
|
+
loginUrl: config.loginUrl,
|
|
23
|
+
logoutUrl: config.logoutUrl,
|
|
24
|
+
refreshTokenKey: config.refreshTokenKey,
|
|
25
|
+
headers: config.headers,
|
|
26
|
+
defaultHeaders,
|
|
27
|
+
});
|
|
28
|
+
default:
|
|
29
|
+
throw new Error(`Unknown provider type: ${String(config.type)}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=register-presets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register-presets.js","sourceRoot":"","sources":["../../../src/transport/provider/register-presets.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAErE;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CACrC,MAA4B,EAC5B,cAAuC;IAEvC,QAAQ,MAAM,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,aAAa;YAChB,OAAO,oBAAoB,CAAC;gBAC1B,UAAU,EAAE,MAAM,CAAC,UAAU;gBAC7B,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,cAAc;aACf,CAAC,CAAC;QAEL,KAAK,WAAW;YACd,OAAO,kBAAkB,CAAC;gBACxB,UAAU,EAAE,MAAM,CAAC,UAAU;gBAC7B,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,eAAe,EAAE,MAAM,CAAC,eAAe;gBACvC,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,cAAc;aACf,CAAC,CAAC;QAEL;YACE,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAE,MAA6B,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC7F,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { RefreshTokenStorage, StorageErrorCallback } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* IndexedDB storage options
|
|
4
|
+
*/
|
|
5
|
+
export interface IndexedDBStorageOptions {
|
|
6
|
+
/** Database name (default: 'FetchGuardDB') */
|
|
7
|
+
dbName?: string;
|
|
8
|
+
/** Key for refresh token (default: 'refreshToken') */
|
|
9
|
+
refreshTokenKey?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Error callback for debugging storage failures
|
|
12
|
+
* Called when IndexedDB operations fail (quota exceeded, permission denied, etc.)
|
|
13
|
+
* Storage still fails closed (returns null), but this allows logging/debugging.
|
|
14
|
+
*/
|
|
15
|
+
onError?: StorageErrorCallback;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* IndexedDB storage - only stores refresh token in IndexedDB
|
|
19
|
+
* Suitable for body-based refresh strategy
|
|
20
|
+
* Persists refresh token for reuse after reload
|
|
21
|
+
*
|
|
22
|
+
* @param options - Storage options or legacy dbName string
|
|
23
|
+
* @param legacyRefreshTokenKey - Legacy refreshTokenKey (for backward compatibility)
|
|
24
|
+
*/
|
|
25
|
+
export declare function createIndexedDBStorage(options?: IndexedDBStorageOptions | string, legacyRefreshTokenKey?: string): RefreshTokenStorage;
|
|
26
|
+
//# sourceMappingURL=indexeddb.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexeddb.d.ts","sourceRoot":"","sources":["../../../../src/transport/provider/storage/indexeddb.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAE7E;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,8CAA8C;IAC9C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sDAAsD;IACtD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,OAAO,CAAC,EAAE,oBAAoB,CAAC;CAChC;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE,uBAAuB,GAAG,MAAuB,EAC1D,qBAAqB,CAAC,EAAE,MAAM,GAC7B,mBAAmB,CAmErB"}
|