@markwharton/pwa-core 2.1.0 → 3.1.0
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/dist/client.d.ts +14 -0
- package/dist/client.js +23 -0
- package/dist/server.d.ts +18 -4
- package/dist/server.js +22 -4
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -83,6 +83,20 @@ export declare class ApiError extends Error {
|
|
|
83
83
|
* });
|
|
84
84
|
*/
|
|
85
85
|
export declare function initApiClient(config: ApiClientConfig): void;
|
|
86
|
+
/**
|
|
87
|
+
* Extract a user-friendly error message from an API error.
|
|
88
|
+
* Use in catch blocks to convert errors to displayable strings.
|
|
89
|
+
* @param error - The caught error (typically ApiError or Error)
|
|
90
|
+
* @param fallback - Message to use if error is not recognized
|
|
91
|
+
* @returns A user-friendly error message
|
|
92
|
+
* @example
|
|
93
|
+
* try {
|
|
94
|
+
* await apiGet('/users');
|
|
95
|
+
* } catch (error) {
|
|
96
|
+
* showToast(getApiErrorMessage(error, 'Failed to load users'), 'error');
|
|
97
|
+
* }
|
|
98
|
+
*/
|
|
99
|
+
export declare function getApiErrorMessage(error: unknown, fallback: string): string;
|
|
86
100
|
/**
|
|
87
101
|
* Makes an authenticated API call. Throws ApiError on non-2xx responses.
|
|
88
102
|
* @typeParam T - The expected response data type
|
package/dist/client.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.ApiError = void 0;
|
|
9
9
|
exports.initApiClient = initApiClient;
|
|
10
|
+
exports.getApiErrorMessage = getApiErrorMessage;
|
|
10
11
|
exports.apiCall = apiCall;
|
|
11
12
|
exports.apiGet = apiGet;
|
|
12
13
|
exports.apiPost = apiPost;
|
|
@@ -149,6 +150,28 @@ function initApiClient(config) {
|
|
|
149
150
|
onUnauthorized = config.onUnauthorized ?? null;
|
|
150
151
|
requestTimeout = config.timeout ?? 30000;
|
|
151
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Extract a user-friendly error message from an API error.
|
|
155
|
+
* Use in catch blocks to convert errors to displayable strings.
|
|
156
|
+
* @param error - The caught error (typically ApiError or Error)
|
|
157
|
+
* @param fallback - Message to use if error is not recognized
|
|
158
|
+
* @returns A user-friendly error message
|
|
159
|
+
* @example
|
|
160
|
+
* try {
|
|
161
|
+
* await apiGet('/users');
|
|
162
|
+
* } catch (error) {
|
|
163
|
+
* showToast(getApiErrorMessage(error, 'Failed to load users'), 'error');
|
|
164
|
+
* }
|
|
165
|
+
*/
|
|
166
|
+
function getApiErrorMessage(error, fallback) {
|
|
167
|
+
if (error instanceof ApiError) {
|
|
168
|
+
return error.message || `${fallback} (${error.status})`;
|
|
169
|
+
}
|
|
170
|
+
if (error instanceof Error) {
|
|
171
|
+
return error.message;
|
|
172
|
+
}
|
|
173
|
+
return fallback;
|
|
174
|
+
}
|
|
152
175
|
/**
|
|
153
176
|
* Makes an authenticated API call. Throws ApiError on non-2xx responses.
|
|
154
177
|
* @typeParam T - The expected response data type
|
package/dist/server.d.ts
CHANGED
|
@@ -8,13 +8,27 @@ import { TableClient } from '@azure/data-tables';
|
|
|
8
8
|
import { Result, BaseJwtPayload, RoleTokenPayload } from './shared';
|
|
9
9
|
/**
|
|
10
10
|
* Initializes the JWT authentication system. Call once at application startup.
|
|
11
|
-
* @param
|
|
12
|
-
* @param
|
|
11
|
+
* @param config - Configuration object
|
|
12
|
+
* @param config.secret - The JWT secret key (from environment variable)
|
|
13
|
+
* @param config.minLength - Minimum required secret length (default: 32)
|
|
13
14
|
* @throws Error if secret is missing or too short
|
|
14
15
|
* @example
|
|
15
|
-
* initAuth(process.env.JWT_SECRET);
|
|
16
|
+
* initAuth({ secret: process.env.JWT_SECRET });
|
|
17
|
+
* initAuth({ secret: process.env.JWT_SECRET, minLength: 64 });
|
|
18
|
+
*/
|
|
19
|
+
export declare function initAuth(config: {
|
|
20
|
+
secret?: string;
|
|
21
|
+
minLength?: number;
|
|
22
|
+
}): void;
|
|
23
|
+
/**
|
|
24
|
+
* Initializes JWT authentication from environment variables.
|
|
25
|
+
* Reads JWT_SECRET from process.env.
|
|
26
|
+
* @param minLength - Minimum required secret length (default: 32)
|
|
27
|
+
* @throws Error if JWT_SECRET is missing or too short
|
|
28
|
+
* @example
|
|
29
|
+
* initAuthFromEnv(); // Uses process.env.JWT_SECRET
|
|
16
30
|
*/
|
|
17
|
-
export declare function
|
|
31
|
+
export declare function initAuthFromEnv(minLength?: number): void;
|
|
18
32
|
/**
|
|
19
33
|
* Gets the configured JWT secret.
|
|
20
34
|
* @returns The JWT secret string
|
package/dist/server.js
CHANGED
|
@@ -10,6 +10,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.HTTP_STATUS = exports.isAdmin = exports.hasRole = exports.hasUsername = exports.getErrorMessage = exports.err = exports.okVoid = exports.ok = exports.DEFAULT_TOKEN_EXPIRY_SECONDS = exports.DEFAULT_TOKEN_EXPIRY = void 0;
|
|
12
12
|
exports.initAuth = initAuth;
|
|
13
|
+
exports.initAuthFromEnv = initAuthFromEnv;
|
|
13
14
|
exports.getJwtSecret = getJwtSecret;
|
|
14
15
|
exports.extractToken = extractToken;
|
|
15
16
|
exports.validateToken = validateToken;
|
|
@@ -50,18 +51,35 @@ const shared_1 = require("./shared");
|
|
|
50
51
|
let jwtSecret = null;
|
|
51
52
|
/**
|
|
52
53
|
* Initializes the JWT authentication system. Call once at application startup.
|
|
53
|
-
* @param
|
|
54
|
-
* @param
|
|
54
|
+
* @param config - Configuration object
|
|
55
|
+
* @param config.secret - The JWT secret key (from environment variable)
|
|
56
|
+
* @param config.minLength - Minimum required secret length (default: 32)
|
|
55
57
|
* @throws Error if secret is missing or too short
|
|
56
58
|
* @example
|
|
57
|
-
* initAuth(process.env.JWT_SECRET);
|
|
59
|
+
* initAuth({ secret: process.env.JWT_SECRET });
|
|
60
|
+
* initAuth({ secret: process.env.JWT_SECRET, minLength: 64 });
|
|
58
61
|
*/
|
|
59
|
-
function initAuth(
|
|
62
|
+
function initAuth(config) {
|
|
63
|
+
const { secret, minLength = 32 } = config;
|
|
60
64
|
if (!secret || secret.length < minLength) {
|
|
61
65
|
throw new Error(`JWT_SECRET must be at least ${minLength} characters`);
|
|
62
66
|
}
|
|
63
67
|
jwtSecret = secret;
|
|
64
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Initializes JWT authentication from environment variables.
|
|
71
|
+
* Reads JWT_SECRET from process.env.
|
|
72
|
+
* @param minLength - Minimum required secret length (default: 32)
|
|
73
|
+
* @throws Error if JWT_SECRET is missing or too short
|
|
74
|
+
* @example
|
|
75
|
+
* initAuthFromEnv(); // Uses process.env.JWT_SECRET
|
|
76
|
+
*/
|
|
77
|
+
function initAuthFromEnv(minLength = 32) {
|
|
78
|
+
initAuth({
|
|
79
|
+
secret: process.env.JWT_SECRET,
|
|
80
|
+
minLength
|
|
81
|
+
});
|
|
82
|
+
}
|
|
65
83
|
/**
|
|
66
84
|
* Gets the configured JWT secret.
|
|
67
85
|
* @returns The JWT secret string
|