@markwharton/pwa-core 1.7.0 → 2.0.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/api.d.ts → client.d.ts} +85 -9
- package/dist/{client/api.js → client.js} +159 -56
- package/dist/index.d.ts +10 -2
- package/dist/index.js +14 -6
- package/dist/server.d.ts +283 -0
- package/dist/server.js +476 -0
- package/dist/shared.d.ts +150 -0
- package/dist/shared.js +124 -0
- package/package.json +11 -12
- package/dist/__tests__/auth/apiKey.test.d.ts +0 -1
- package/dist/__tests__/auth/apiKey.test.js +0 -80
- package/dist/__tests__/auth/token.test.d.ts +0 -1
- package/dist/__tests__/auth/token.test.js +0 -212
- package/dist/__tests__/auth/types.test.d.ts +0 -1
- package/dist/__tests__/auth/types.test.js +0 -77
- package/dist/__tests__/client/api.test.d.ts +0 -1
- package/dist/__tests__/client/api.test.js +0 -369
- package/dist/__tests__/client/apiError.test.d.ts +0 -1
- package/dist/__tests__/client/apiError.test.js +0 -91
- package/dist/__tests__/http/responses.test.d.ts +0 -1
- package/dist/__tests__/http/responses.test.js +0 -112
- package/dist/__tests__/http/status.test.d.ts +0 -1
- package/dist/__tests__/http/status.test.js +0 -27
- package/dist/__tests__/server/auth/apiKey.test.d.ts +0 -1
- package/dist/__tests__/server/auth/apiKey.test.js +0 -80
- package/dist/__tests__/server/auth/token.test.d.ts +0 -1
- package/dist/__tests__/server/auth/token.test.js +0 -299
- package/dist/__tests__/server/http/responses.test.d.ts +0 -1
- package/dist/__tests__/server/http/responses.test.js +0 -112
- package/dist/__tests__/server/storage/client.test.d.ts +0 -1
- package/dist/__tests__/server/storage/client.test.js +0 -173
- package/dist/__tests__/server/storage/keys.test.d.ts +0 -1
- package/dist/__tests__/server/storage/keys.test.js +0 -47
- package/dist/__tests__/shared/auth/types.test.d.ts +0 -1
- package/dist/__tests__/shared/auth/types.test.js +0 -77
- package/dist/__tests__/shared/http/status.test.d.ts +0 -1
- package/dist/__tests__/shared/http/status.test.js +0 -29
- package/dist/__tests__/storage/client.test.d.ts +0 -1
- package/dist/__tests__/storage/client.test.js +0 -173
- package/dist/__tests__/storage/keys.test.d.ts +0 -1
- package/dist/__tests__/storage/keys.test.js +0 -47
- package/dist/__tests__/types.test.d.ts +0 -1
- package/dist/__tests__/types.test.js +0 -56
- package/dist/auth/apiKey.d.ts +0 -44
- package/dist/auth/apiKey.js +0 -59
- package/dist/auth/index.d.ts +0 -3
- package/dist/auth/index.js +0 -22
- package/dist/auth/token.d.ts +0 -56
- package/dist/auth/token.js +0 -104
- package/dist/auth/types.d.ts +0 -63
- package/dist/auth/types.js +0 -41
- package/dist/client/apiError.d.ts +0 -48
- package/dist/client/apiError.js +0 -65
- package/dist/client/index.d.ts +0 -3
- package/dist/client/index.js +0 -14
- package/dist/client/types.d.ts +0 -12
- package/dist/client/types.js +0 -5
- package/dist/http/index.d.ts +0 -3
- package/dist/http/index.js +0 -14
- package/dist/http/responses.d.ts +0 -82
- package/dist/http/responses.js +0 -132
- package/dist/http/status.d.ts +0 -17
- package/dist/http/status.js +0 -19
- package/dist/http/types.d.ts +0 -10
- package/dist/http/types.js +0 -5
- package/dist/server/auth/apiKey.d.ts +0 -44
- package/dist/server/auth/apiKey.js +0 -59
- package/dist/server/auth/index.d.ts +0 -3
- package/dist/server/auth/index.js +0 -19
- package/dist/server/auth/token.d.ts +0 -102
- package/dist/server/auth/token.js +0 -158
- package/dist/server/http/index.d.ts +0 -1
- package/dist/server/http/index.js +0 -12
- package/dist/server/http/responses.d.ts +0 -82
- package/dist/server/http/responses.js +0 -132
- package/dist/server/index.d.ts +0 -4
- package/dist/server/index.js +0 -37
- package/dist/server/storage/client.d.ts +0 -48
- package/dist/server/storage/client.js +0 -107
- package/dist/server/storage/index.d.ts +0 -2
- package/dist/server/storage/index.js +0 -11
- package/dist/server/storage/keys.d.ts +0 -8
- package/dist/server/storage/keys.js +0 -14
- package/dist/shared/auth/index.d.ts +0 -2
- package/dist/shared/auth/index.js +0 -7
- package/dist/shared/auth/types.d.ts +0 -63
- package/dist/shared/auth/types.js +0 -41
- package/dist/shared/http/index.d.ts +0 -3
- package/dist/shared/http/index.js +0 -5
- package/dist/shared/http/status.d.ts +0 -19
- package/dist/shared/http/status.js +0 -21
- package/dist/shared/http/types.d.ts +0 -10
- package/dist/shared/http/types.js +0 -5
- package/dist/shared/index.d.ts +0 -5
- package/dist/shared/index.js +0 -10
- package/dist/storage/client.d.ts +0 -48
- package/dist/storage/client.js +0 -107
- package/dist/storage/index.d.ts +0 -2
- package/dist/storage/index.js +0 -11
- package/dist/storage/keys.d.ts +0 -8
- package/dist/storage/keys.js +0 -14
- package/dist/types.d.ts +0 -48
- package/dist/types.js +0 -41
package/dist/auth/apiKey.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { Result } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* API Key utilities for machine-to-machine authentication
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Extracts API key from the X-API-Key header.
|
|
7
|
-
* @param request - Request object with headers.get() method
|
|
8
|
-
* @returns The API key string, or null if not present
|
|
9
|
-
* @example
|
|
10
|
-
* const apiKey = extractApiKey(request);
|
|
11
|
-
*/
|
|
12
|
-
export declare function extractApiKey(request: {
|
|
13
|
-
headers: {
|
|
14
|
-
get(name: string): string | null;
|
|
15
|
-
};
|
|
16
|
-
}): string | null;
|
|
17
|
-
/**
|
|
18
|
-
* Hashes an API key using SHA-256 for secure storage.
|
|
19
|
-
* Store this hash in your database, never the raw key.
|
|
20
|
-
* @param apiKey - The raw API key to hash
|
|
21
|
-
* @returns The SHA-256 hash as a hex string
|
|
22
|
-
* @example
|
|
23
|
-
* const hash = hashApiKey(rawKey);
|
|
24
|
-
* await db.save({ apiKeyHash: hash });
|
|
25
|
-
*/
|
|
26
|
-
export declare function hashApiKey(apiKey: string): string;
|
|
27
|
-
/**
|
|
28
|
-
* Validates an API key against a stored hash.
|
|
29
|
-
* @param apiKey - The API key from the request
|
|
30
|
-
* @param storedHash - The hash stored in your database
|
|
31
|
-
* @returns Result with ok=true if valid, or error message if invalid
|
|
32
|
-
* @example
|
|
33
|
-
* const result = validateApiKey(apiKey, user.apiKeyHash);
|
|
34
|
-
* if (!result.ok) return httpUnauthorized();
|
|
35
|
-
*/
|
|
36
|
-
export declare function validateApiKey(apiKey: string, storedHash: string): Result<void>;
|
|
37
|
-
/**
|
|
38
|
-
* Generates a cryptographically secure API key.
|
|
39
|
-
* @returns A random 64-character hex string (32 bytes)
|
|
40
|
-
* @example
|
|
41
|
-
* const apiKey = generateApiKey();
|
|
42
|
-
* // Return to user once, store hash in database
|
|
43
|
-
*/
|
|
44
|
-
export declare function generateApiKey(): string;
|
package/dist/auth/apiKey.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractApiKey = extractApiKey;
|
|
4
|
-
exports.hashApiKey = hashApiKey;
|
|
5
|
-
exports.validateApiKey = validateApiKey;
|
|
6
|
-
exports.generateApiKey = generateApiKey;
|
|
7
|
-
const crypto_1 = require("crypto");
|
|
8
|
-
const types_1 = require("../types");
|
|
9
|
-
/**
|
|
10
|
-
* API Key utilities for machine-to-machine authentication
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* Extracts API key from the X-API-Key header.
|
|
14
|
-
* @param request - Request object with headers.get() method
|
|
15
|
-
* @returns The API key string, or null if not present
|
|
16
|
-
* @example
|
|
17
|
-
* const apiKey = extractApiKey(request);
|
|
18
|
-
*/
|
|
19
|
-
function extractApiKey(request) {
|
|
20
|
-
return request.headers.get('X-API-Key');
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Hashes an API key using SHA-256 for secure storage.
|
|
24
|
-
* Store this hash in your database, never the raw key.
|
|
25
|
-
* @param apiKey - The raw API key to hash
|
|
26
|
-
* @returns The SHA-256 hash as a hex string
|
|
27
|
-
* @example
|
|
28
|
-
* const hash = hashApiKey(rawKey);
|
|
29
|
-
* await db.save({ apiKeyHash: hash });
|
|
30
|
-
*/
|
|
31
|
-
function hashApiKey(apiKey) {
|
|
32
|
-
return (0, crypto_1.createHash)('sha256').update(apiKey).digest('hex');
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Validates an API key against a stored hash.
|
|
36
|
-
* @param apiKey - The API key from the request
|
|
37
|
-
* @param storedHash - The hash stored in your database
|
|
38
|
-
* @returns Result with ok=true if valid, or error message if invalid
|
|
39
|
-
* @example
|
|
40
|
-
* const result = validateApiKey(apiKey, user.apiKeyHash);
|
|
41
|
-
* if (!result.ok) return httpUnauthorized();
|
|
42
|
-
*/
|
|
43
|
-
function validateApiKey(apiKey, storedHash) {
|
|
44
|
-
const keyHash = hashApiKey(apiKey);
|
|
45
|
-
if (keyHash === storedHash) {
|
|
46
|
-
return (0, types_1.okVoid)();
|
|
47
|
-
}
|
|
48
|
-
return (0, types_1.err)('Invalid API key');
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Generates a cryptographically secure API key.
|
|
52
|
-
* @returns A random 64-character hex string (32 bytes)
|
|
53
|
-
* @example
|
|
54
|
-
* const apiKey = generateApiKey();
|
|
55
|
-
* // Return to user once, store hash in database
|
|
56
|
-
*/
|
|
57
|
-
function generateApiKey() {
|
|
58
|
-
return (0, crypto_1.randomBytes)(32).toString('hex');
|
|
59
|
-
}
|
package/dist/auth/index.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export { type BaseJwtPayload, type UserTokenPayload, type UsernameTokenPayload, type RoleTokenPayload, hasUsername, hasRole, isAdmin } from './types';
|
|
2
|
-
export { initAuth, getJwtSecret, extractToken, validateToken, generateToken, generateLongLivedToken } from './token';
|
|
3
|
-
export { extractApiKey, hashApiKey, validateApiKey, generateApiKey } from './apiKey';
|
package/dist/auth/index.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateApiKey = exports.validateApiKey = exports.hashApiKey = exports.extractApiKey = exports.generateLongLivedToken = exports.generateToken = exports.validateToken = exports.extractToken = exports.getJwtSecret = exports.initAuth = exports.isAdmin = exports.hasRole = exports.hasUsername = void 0;
|
|
4
|
-
// Types and type guards
|
|
5
|
-
var types_1 = require("./types");
|
|
6
|
-
Object.defineProperty(exports, "hasUsername", { enumerable: true, get: function () { return types_1.hasUsername; } });
|
|
7
|
-
Object.defineProperty(exports, "hasRole", { enumerable: true, get: function () { return types_1.hasRole; } });
|
|
8
|
-
Object.defineProperty(exports, "isAdmin", { enumerable: true, get: function () { return types_1.isAdmin; } });
|
|
9
|
-
// JWT functions
|
|
10
|
-
var token_1 = require("./token");
|
|
11
|
-
Object.defineProperty(exports, "initAuth", { enumerable: true, get: function () { return token_1.initAuth; } });
|
|
12
|
-
Object.defineProperty(exports, "getJwtSecret", { enumerable: true, get: function () { return token_1.getJwtSecret; } });
|
|
13
|
-
Object.defineProperty(exports, "extractToken", { enumerable: true, get: function () { return token_1.extractToken; } });
|
|
14
|
-
Object.defineProperty(exports, "validateToken", { enumerable: true, get: function () { return token_1.validateToken; } });
|
|
15
|
-
Object.defineProperty(exports, "generateToken", { enumerable: true, get: function () { return token_1.generateToken; } });
|
|
16
|
-
Object.defineProperty(exports, "generateLongLivedToken", { enumerable: true, get: function () { return token_1.generateLongLivedToken; } });
|
|
17
|
-
// API key functions
|
|
18
|
-
var apiKey_1 = require("./apiKey");
|
|
19
|
-
Object.defineProperty(exports, "extractApiKey", { enumerable: true, get: function () { return apiKey_1.extractApiKey; } });
|
|
20
|
-
Object.defineProperty(exports, "hashApiKey", { enumerable: true, get: function () { return apiKey_1.hashApiKey; } });
|
|
21
|
-
Object.defineProperty(exports, "validateApiKey", { enumerable: true, get: function () { return apiKey_1.validateApiKey; } });
|
|
22
|
-
Object.defineProperty(exports, "generateApiKey", { enumerable: true, get: function () { return apiKey_1.generateApiKey; } });
|
package/dist/auth/token.d.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { Result } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* Initializes the JWT authentication system. Call once at application startup.
|
|
4
|
-
* @param secret - The JWT secret key (from environment variable)
|
|
5
|
-
* @param minLength - Minimum required secret length (default: 32)
|
|
6
|
-
* @throws Error if secret is missing or too short
|
|
7
|
-
* @example
|
|
8
|
-
* initAuth(process.env.JWT_SECRET);
|
|
9
|
-
*/
|
|
10
|
-
export declare function initAuth(secret: string | undefined, minLength?: number): void;
|
|
11
|
-
/**
|
|
12
|
-
* Gets the configured JWT secret.
|
|
13
|
-
* @returns The JWT secret string
|
|
14
|
-
* @throws Error if initAuth() has not been called
|
|
15
|
-
*/
|
|
16
|
-
export declare function getJwtSecret(): string;
|
|
17
|
-
/**
|
|
18
|
-
* Extracts the Bearer token from an Authorization header.
|
|
19
|
-
* @param authHeader - The Authorization header value
|
|
20
|
-
* @returns The token string, or null if not a valid Bearer token
|
|
21
|
-
* @example
|
|
22
|
-
* const token = extractToken(request.headers.get('Authorization'));
|
|
23
|
-
*/
|
|
24
|
-
export declare function extractToken(authHeader: string | null): string | null;
|
|
25
|
-
/**
|
|
26
|
-
* Validates and decodes a JWT token.
|
|
27
|
-
* @typeParam T - The expected payload type (extends object)
|
|
28
|
-
* @param token - The JWT token string to validate
|
|
29
|
-
* @returns Result with decoded payload on success, or error message on failure
|
|
30
|
-
* @example
|
|
31
|
-
* const result = validateToken<UserPayload>(token);
|
|
32
|
-
* if (result.ok) {
|
|
33
|
-
* console.log(result.data.username);
|
|
34
|
-
* }
|
|
35
|
-
*/
|
|
36
|
-
export declare function validateToken<T extends object>(token: string): Result<T>;
|
|
37
|
-
/**
|
|
38
|
-
* Generates a signed JWT token with the given payload.
|
|
39
|
-
* @typeParam T - The payload type (extends object)
|
|
40
|
-
* @param payload - The data to encode in the token
|
|
41
|
-
* @param expiresIn - Token expiration time (default: '7d')
|
|
42
|
-
* @returns The signed JWT token string
|
|
43
|
-
* @example
|
|
44
|
-
* const token = generateToken({ userId: '123', role: 'admin' }, '1h');
|
|
45
|
-
*/
|
|
46
|
-
export declare function generateToken<T extends object>(payload: T, expiresIn?: string): string;
|
|
47
|
-
/**
|
|
48
|
-
* Generates a long-lived JWT token for machine/API access.
|
|
49
|
-
* @typeParam T - The payload type (extends object)
|
|
50
|
-
* @param payload - The data to encode in the token
|
|
51
|
-
* @param expiresInDays - Token expiration in days (default: 3650 ≈ 10 years)
|
|
52
|
-
* @returns The signed JWT token string
|
|
53
|
-
* @example
|
|
54
|
-
* const apiToken = generateLongLivedToken({ machineId: 'server-1' });
|
|
55
|
-
*/
|
|
56
|
-
export declare function generateLongLivedToken<T extends object>(payload: T, expiresInDays?: number): string;
|
package/dist/auth/token.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.initAuth = initAuth;
|
|
7
|
-
exports.getJwtSecret = getJwtSecret;
|
|
8
|
-
exports.extractToken = extractToken;
|
|
9
|
-
exports.validateToken = validateToken;
|
|
10
|
-
exports.generateToken = generateToken;
|
|
11
|
-
exports.generateLongLivedToken = generateLongLivedToken;
|
|
12
|
-
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
13
|
-
const types_1 = require("../types");
|
|
14
|
-
/**
|
|
15
|
-
* JWT token utilities - works with any payload structure
|
|
16
|
-
* Use BaseJwtPayload or extend it for type safety
|
|
17
|
-
*/
|
|
18
|
-
let jwtSecret = null;
|
|
19
|
-
/**
|
|
20
|
-
* Initializes the JWT authentication system. Call once at application startup.
|
|
21
|
-
* @param secret - The JWT secret key (from environment variable)
|
|
22
|
-
* @param minLength - Minimum required secret length (default: 32)
|
|
23
|
-
* @throws Error if secret is missing or too short
|
|
24
|
-
* @example
|
|
25
|
-
* initAuth(process.env.JWT_SECRET);
|
|
26
|
-
*/
|
|
27
|
-
function initAuth(secret, minLength = 32) {
|
|
28
|
-
if (!secret || secret.length < minLength) {
|
|
29
|
-
throw new Error(`JWT_SECRET must be at least ${minLength} characters`);
|
|
30
|
-
}
|
|
31
|
-
jwtSecret = secret;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Gets the configured JWT secret.
|
|
35
|
-
* @returns The JWT secret string
|
|
36
|
-
* @throws Error if initAuth() has not been called
|
|
37
|
-
*/
|
|
38
|
-
function getJwtSecret() {
|
|
39
|
-
if (!jwtSecret) {
|
|
40
|
-
throw new Error('Auth not initialized. Call initAuth() first.');
|
|
41
|
-
}
|
|
42
|
-
return jwtSecret;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Extracts the Bearer token from an Authorization header.
|
|
46
|
-
* @param authHeader - The Authorization header value
|
|
47
|
-
* @returns The token string, or null if not a valid Bearer token
|
|
48
|
-
* @example
|
|
49
|
-
* const token = extractToken(request.headers.get('Authorization'));
|
|
50
|
-
*/
|
|
51
|
-
function extractToken(authHeader) {
|
|
52
|
-
if (!authHeader?.startsWith('Bearer ')) {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
return authHeader.slice(7);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Validates and decodes a JWT token.
|
|
59
|
-
* @typeParam T - The expected payload type (extends object)
|
|
60
|
-
* @param token - The JWT token string to validate
|
|
61
|
-
* @returns Result with decoded payload on success, or error message on failure
|
|
62
|
-
* @example
|
|
63
|
-
* const result = validateToken<UserPayload>(token);
|
|
64
|
-
* if (result.ok) {
|
|
65
|
-
* console.log(result.data.username);
|
|
66
|
-
* }
|
|
67
|
-
*/
|
|
68
|
-
function validateToken(token) {
|
|
69
|
-
try {
|
|
70
|
-
const payload = jsonwebtoken_1.default.verify(token, getJwtSecret());
|
|
71
|
-
if (typeof payload === 'object' && payload !== null) {
|
|
72
|
-
return (0, types_1.ok)(payload);
|
|
73
|
-
}
|
|
74
|
-
return (0, types_1.err)('Invalid token payload');
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
const message = error instanceof Error ? error.message : 'Token validation failed';
|
|
78
|
-
return (0, types_1.err)(message);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Generates a signed JWT token with the given payload.
|
|
83
|
-
* @typeParam T - The payload type (extends object)
|
|
84
|
-
* @param payload - The data to encode in the token
|
|
85
|
-
* @param expiresIn - Token expiration time (default: '7d')
|
|
86
|
-
* @returns The signed JWT token string
|
|
87
|
-
* @example
|
|
88
|
-
* const token = generateToken({ userId: '123', role: 'admin' }, '1h');
|
|
89
|
-
*/
|
|
90
|
-
function generateToken(payload, expiresIn = '7d') {
|
|
91
|
-
return jsonwebtoken_1.default.sign(payload, getJwtSecret(), { expiresIn });
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Generates a long-lived JWT token for machine/API access.
|
|
95
|
-
* @typeParam T - The payload type (extends object)
|
|
96
|
-
* @param payload - The data to encode in the token
|
|
97
|
-
* @param expiresInDays - Token expiration in days (default: 3650 ≈ 10 years)
|
|
98
|
-
* @returns The signed JWT token string
|
|
99
|
-
* @example
|
|
100
|
-
* const apiToken = generateLongLivedToken({ machineId: 'server-1' });
|
|
101
|
-
*/
|
|
102
|
-
function generateLongLivedToken(payload, expiresInDays = 3650) {
|
|
103
|
-
return jsonwebtoken_1.default.sign(payload, getJwtSecret(), { expiresIn: `${expiresInDays}d` });
|
|
104
|
-
}
|
package/dist/auth/types.d.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base JWT payload - all tokens include these fields
|
|
3
|
-
* Projects extend this with their specific fields
|
|
4
|
-
*/
|
|
5
|
-
export interface BaseJwtPayload {
|
|
6
|
-
iat: number;
|
|
7
|
-
exp: number;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Standard user token payload
|
|
11
|
-
* Used by: azure-pwa-starter, azure-alert-service (admin), onsite-monitor
|
|
12
|
-
*/
|
|
13
|
-
export interface UserTokenPayload extends BaseJwtPayload {
|
|
14
|
-
authenticated: true;
|
|
15
|
-
tokenType: 'user' | 'machine';
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Username-based token payload
|
|
19
|
-
* Used by: financial-tracker
|
|
20
|
-
*/
|
|
21
|
-
export interface UsernameTokenPayload extends BaseJwtPayload {
|
|
22
|
-
username: string;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Role-based token payload
|
|
26
|
-
* Used by: azure-alert-service
|
|
27
|
-
*/
|
|
28
|
-
export interface RoleTokenPayload extends BaseJwtPayload {
|
|
29
|
-
authenticated: true;
|
|
30
|
-
tokenType: 'user' | 'machine';
|
|
31
|
-
role: 'admin' | 'viewer';
|
|
32
|
-
viewerTokenId?: string;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Type guard to check if a JWT payload contains a username field.
|
|
36
|
-
* @param payload - The JWT payload to check
|
|
37
|
-
* @returns True if payload has a string username field
|
|
38
|
-
* @example
|
|
39
|
-
* if (hasUsername(payload)) {
|
|
40
|
-
* console.log(payload.username); // TypeScript knows username exists
|
|
41
|
-
* }
|
|
42
|
-
*/
|
|
43
|
-
export declare function hasUsername(payload: BaseJwtPayload): payload is UsernameTokenPayload;
|
|
44
|
-
/**
|
|
45
|
-
* Type guard to check if a JWT payload contains a role field.
|
|
46
|
-
* @param payload - The JWT payload to check
|
|
47
|
-
* @returns True if payload has a role field
|
|
48
|
-
* @example
|
|
49
|
-
* if (hasRole(payload)) {
|
|
50
|
-
* console.log(payload.role); // 'admin' | 'viewer'
|
|
51
|
-
* }
|
|
52
|
-
*/
|
|
53
|
-
export declare function hasRole(payload: BaseJwtPayload): payload is RoleTokenPayload;
|
|
54
|
-
/**
|
|
55
|
-
* Checks if a JWT payload represents an admin user.
|
|
56
|
-
* @param payload - The JWT payload to check
|
|
57
|
-
* @returns True if payload has role='admin'
|
|
58
|
-
* @example
|
|
59
|
-
* if (!isAdmin(payload)) {
|
|
60
|
-
* return httpForbidden('Admin access required');
|
|
61
|
-
* }
|
|
62
|
-
*/
|
|
63
|
-
export declare function isAdmin(payload: BaseJwtPayload): boolean;
|
package/dist/auth/types.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasUsername = hasUsername;
|
|
4
|
-
exports.hasRole = hasRole;
|
|
5
|
-
exports.isAdmin = isAdmin;
|
|
6
|
-
/**
|
|
7
|
-
* Type guard to check if a JWT payload contains a username field.
|
|
8
|
-
* @param payload - The JWT payload to check
|
|
9
|
-
* @returns True if payload has a string username field
|
|
10
|
-
* @example
|
|
11
|
-
* if (hasUsername(payload)) {
|
|
12
|
-
* console.log(payload.username); // TypeScript knows username exists
|
|
13
|
-
* }
|
|
14
|
-
*/
|
|
15
|
-
function hasUsername(payload) {
|
|
16
|
-
return 'username' in payload && typeof payload.username === 'string';
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Type guard to check if a JWT payload contains a role field.
|
|
20
|
-
* @param payload - The JWT payload to check
|
|
21
|
-
* @returns True if payload has a role field
|
|
22
|
-
* @example
|
|
23
|
-
* if (hasRole(payload)) {
|
|
24
|
-
* console.log(payload.role); // 'admin' | 'viewer'
|
|
25
|
-
* }
|
|
26
|
-
*/
|
|
27
|
-
function hasRole(payload) {
|
|
28
|
-
return 'role' in payload;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Checks if a JWT payload represents an admin user.
|
|
32
|
-
* @param payload - The JWT payload to check
|
|
33
|
-
* @returns True if payload has role='admin'
|
|
34
|
-
* @example
|
|
35
|
-
* if (!isAdmin(payload)) {
|
|
36
|
-
* return httpForbidden('Admin access required');
|
|
37
|
-
* }
|
|
38
|
-
*/
|
|
39
|
-
function isAdmin(payload) {
|
|
40
|
-
return hasRole(payload) && payload.role === 'admin';
|
|
41
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom error class for API errors.
|
|
3
|
-
* Preserves HTTP status code and error message from the server.
|
|
4
|
-
* @example
|
|
5
|
-
* try {
|
|
6
|
-
* await apiGet('/users/123');
|
|
7
|
-
* } catch (error) {
|
|
8
|
-
* if (error instanceof ApiError && error.isNotFound()) {
|
|
9
|
-
* console.log('User not found');
|
|
10
|
-
* }
|
|
11
|
-
* }
|
|
12
|
-
*/
|
|
13
|
-
export declare class ApiError extends Error {
|
|
14
|
-
status: number;
|
|
15
|
-
details?: string | undefined;
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new ApiError instance.
|
|
18
|
-
* @param status - The HTTP status code
|
|
19
|
-
* @param message - The error message
|
|
20
|
-
* @param details - Optional additional error details
|
|
21
|
-
*/
|
|
22
|
-
constructor(status: number, message: string, details?: string | undefined);
|
|
23
|
-
/**
|
|
24
|
-
* Checks if this is a 401 Unauthorized error.
|
|
25
|
-
* @returns True if status is 401
|
|
26
|
-
*/
|
|
27
|
-
isUnauthorized(): boolean;
|
|
28
|
-
/**
|
|
29
|
-
* Checks if this is a 404 Not Found error.
|
|
30
|
-
* @returns True if status is 404
|
|
31
|
-
*/
|
|
32
|
-
isNotFound(): boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Checks if this is a 400 Bad Request error.
|
|
35
|
-
* @returns True if status is 400
|
|
36
|
-
*/
|
|
37
|
-
isBadRequest(): boolean;
|
|
38
|
-
/**
|
|
39
|
-
* Checks if this is a client error (4xx status).
|
|
40
|
-
* @returns True if status is 400-499
|
|
41
|
-
*/
|
|
42
|
-
isClientError(): boolean;
|
|
43
|
-
/**
|
|
44
|
-
* Checks if this is a server error (5xx status).
|
|
45
|
-
* @returns True if status is 500-599
|
|
46
|
-
*/
|
|
47
|
-
isServerError(): boolean;
|
|
48
|
-
}
|
package/dist/client/apiError.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ApiError = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Custom error class for API errors.
|
|
6
|
-
* Preserves HTTP status code and error message from the server.
|
|
7
|
-
* @example
|
|
8
|
-
* try {
|
|
9
|
-
* await apiGet('/users/123');
|
|
10
|
-
* } catch (error) {
|
|
11
|
-
* if (error instanceof ApiError && error.isNotFound()) {
|
|
12
|
-
* console.log('User not found');
|
|
13
|
-
* }
|
|
14
|
-
* }
|
|
15
|
-
*/
|
|
16
|
-
class ApiError extends Error {
|
|
17
|
-
/**
|
|
18
|
-
* Creates a new ApiError instance.
|
|
19
|
-
* @param status - The HTTP status code
|
|
20
|
-
* @param message - The error message
|
|
21
|
-
* @param details - Optional additional error details
|
|
22
|
-
*/
|
|
23
|
-
constructor(status, message, details) {
|
|
24
|
-
super(message);
|
|
25
|
-
this.status = status;
|
|
26
|
-
this.details = details;
|
|
27
|
-
this.name = 'ApiError';
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Checks if this is a 401 Unauthorized error.
|
|
31
|
-
* @returns True if status is 401
|
|
32
|
-
*/
|
|
33
|
-
isUnauthorized() {
|
|
34
|
-
return this.status === 401;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Checks if this is a 404 Not Found error.
|
|
38
|
-
* @returns True if status is 404
|
|
39
|
-
*/
|
|
40
|
-
isNotFound() {
|
|
41
|
-
return this.status === 404;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Checks if this is a 400 Bad Request error.
|
|
45
|
-
* @returns True if status is 400
|
|
46
|
-
*/
|
|
47
|
-
isBadRequest() {
|
|
48
|
-
return this.status === 400;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Checks if this is a client error (4xx status).
|
|
52
|
-
* @returns True if status is 400-499
|
|
53
|
-
*/
|
|
54
|
-
isClientError() {
|
|
55
|
-
return this.status >= 400 && this.status < 500;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Checks if this is a server error (5xx status).
|
|
59
|
-
* @returns True if status is 500-599
|
|
60
|
-
*/
|
|
61
|
-
isServerError() {
|
|
62
|
-
return this.status >= 500 && this.status < 600;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
exports.ApiError = ApiError;
|
package/dist/client/index.d.ts
DELETED
package/dist/client/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.apiCallSafe = exports.apiDelete = exports.apiPatch = exports.apiPut = exports.apiPost = exports.apiGet = exports.apiCall = exports.initApiClient = exports.ApiError = void 0;
|
|
4
|
-
var apiError_1 = require("./apiError");
|
|
5
|
-
Object.defineProperty(exports, "ApiError", { enumerable: true, get: function () { return apiError_1.ApiError; } });
|
|
6
|
-
var api_1 = require("./api");
|
|
7
|
-
Object.defineProperty(exports, "initApiClient", { enumerable: true, get: function () { return api_1.initApiClient; } });
|
|
8
|
-
Object.defineProperty(exports, "apiCall", { enumerable: true, get: function () { return api_1.apiCall; } });
|
|
9
|
-
Object.defineProperty(exports, "apiGet", { enumerable: true, get: function () { return api_1.apiGet; } });
|
|
10
|
-
Object.defineProperty(exports, "apiPost", { enumerable: true, get: function () { return api_1.apiPost; } });
|
|
11
|
-
Object.defineProperty(exports, "apiPut", { enumerable: true, get: function () { return api_1.apiPut; } });
|
|
12
|
-
Object.defineProperty(exports, "apiPatch", { enumerable: true, get: function () { return api_1.apiPatch; } });
|
|
13
|
-
Object.defineProperty(exports, "apiDelete", { enumerable: true, get: function () { return api_1.apiDelete; } });
|
|
14
|
-
Object.defineProperty(exports, "apiCallSafe", { enumerable: true, get: function () { return api_1.apiCallSafe; } });
|
package/dist/client/types.d.ts
DELETED
package/dist/client/types.js
DELETED
package/dist/http/index.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export { HTTP_STATUS, type HttpStatus } from './status';
|
|
2
|
-
export { type ErrorResponse } from './types';
|
|
3
|
-
export { badRequestResponse, unauthorizedResponse, forbiddenResponse, notFoundResponse, conflictResponse, handleFunctionError, isNotFoundError, isConflictError } from './responses';
|
package/dist/http/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isConflictError = exports.isNotFoundError = exports.handleFunctionError = exports.conflictResponse = exports.notFoundResponse = exports.forbiddenResponse = exports.unauthorizedResponse = exports.badRequestResponse = exports.HTTP_STATUS = void 0;
|
|
4
|
-
var status_1 = require("./status");
|
|
5
|
-
Object.defineProperty(exports, "HTTP_STATUS", { enumerable: true, get: function () { return status_1.HTTP_STATUS; } });
|
|
6
|
-
var responses_1 = require("./responses");
|
|
7
|
-
Object.defineProperty(exports, "badRequestResponse", { enumerable: true, get: function () { return responses_1.badRequestResponse; } });
|
|
8
|
-
Object.defineProperty(exports, "unauthorizedResponse", { enumerable: true, get: function () { return responses_1.unauthorizedResponse; } });
|
|
9
|
-
Object.defineProperty(exports, "forbiddenResponse", { enumerable: true, get: function () { return responses_1.forbiddenResponse; } });
|
|
10
|
-
Object.defineProperty(exports, "notFoundResponse", { enumerable: true, get: function () { return responses_1.notFoundResponse; } });
|
|
11
|
-
Object.defineProperty(exports, "conflictResponse", { enumerable: true, get: function () { return responses_1.conflictResponse; } });
|
|
12
|
-
Object.defineProperty(exports, "handleFunctionError", { enumerable: true, get: function () { return responses_1.handleFunctionError; } });
|
|
13
|
-
Object.defineProperty(exports, "isNotFoundError", { enumerable: true, get: function () { return responses_1.isNotFoundError; } });
|
|
14
|
-
Object.defineProperty(exports, "isConflictError", { enumerable: true, get: function () { return responses_1.isConflictError; } });
|
package/dist/http/responses.d.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { HttpResponseInit, InvocationContext } from '@azure/functions';
|
|
2
|
-
/**
|
|
3
|
-
* Creates a 400 Bad Request response.
|
|
4
|
-
* @param message - The error message to return
|
|
5
|
-
* @returns Azure Functions HttpResponseInit object
|
|
6
|
-
* @example
|
|
7
|
-
* if (!body.email) return badRequestResponse('Email is required');
|
|
8
|
-
*/
|
|
9
|
-
export declare function badRequestResponse(message: string): HttpResponseInit;
|
|
10
|
-
/**
|
|
11
|
-
* Creates a 401 Unauthorized response.
|
|
12
|
-
* @param message - The error message (default: 'Unauthorized')
|
|
13
|
-
* @returns Azure Functions HttpResponseInit object
|
|
14
|
-
* @example
|
|
15
|
-
* if (!token) return unauthorizedResponse();
|
|
16
|
-
*/
|
|
17
|
-
export declare function unauthorizedResponse(message?: string): HttpResponseInit;
|
|
18
|
-
/**
|
|
19
|
-
* Creates a 403 Forbidden response.
|
|
20
|
-
* @param message - The error message (default: 'Forbidden')
|
|
21
|
-
* @returns Azure Functions HttpResponseInit object
|
|
22
|
-
* @example
|
|
23
|
-
* if (!isAdmin(payload)) return forbiddenResponse('Admin access required');
|
|
24
|
-
*/
|
|
25
|
-
export declare function forbiddenResponse(message?: string): HttpResponseInit;
|
|
26
|
-
/**
|
|
27
|
-
* Creates a 404 Not Found response.
|
|
28
|
-
* @param resource - The name of the resource that wasn't found
|
|
29
|
-
* @returns Azure Functions HttpResponseInit object
|
|
30
|
-
* @example
|
|
31
|
-
* if (!user) return notFoundResponse('User');
|
|
32
|
-
* // Returns: { error: 'User not found' }
|
|
33
|
-
*/
|
|
34
|
-
export declare function notFoundResponse(resource: string): HttpResponseInit;
|
|
35
|
-
/**
|
|
36
|
-
* Creates a 409 Conflict response.
|
|
37
|
-
* @param message - The conflict error message
|
|
38
|
-
* @returns Azure Functions HttpResponseInit object
|
|
39
|
-
* @example
|
|
40
|
-
* if (existingUser) return conflictResponse('Email already registered');
|
|
41
|
-
*/
|
|
42
|
-
export declare function conflictResponse(message: string): HttpResponseInit;
|
|
43
|
-
/**
|
|
44
|
-
* Handles unexpected errors safely by logging details and returning a generic message.
|
|
45
|
-
* Use in catch blocks to avoid exposing internal error details to clients.
|
|
46
|
-
* @param error - The caught error
|
|
47
|
-
* @param context - Azure Functions InvocationContext for logging
|
|
48
|
-
* @returns Azure Functions HttpResponseInit with 500 status
|
|
49
|
-
* @example
|
|
50
|
-
* try {
|
|
51
|
-
* await riskyOperation();
|
|
52
|
-
* } catch (error) {
|
|
53
|
-
* return handleFunctionError(error, context);
|
|
54
|
-
* }
|
|
55
|
-
*/
|
|
56
|
-
export declare function handleFunctionError(error: unknown, context: InvocationContext): HttpResponseInit;
|
|
57
|
-
/**
|
|
58
|
-
* Checks if an error is an Azure Table Storage "not found" error.
|
|
59
|
-
* @param error - The caught error
|
|
60
|
-
* @returns True if error has statusCode 404
|
|
61
|
-
* @example
|
|
62
|
-
* try {
|
|
63
|
-
* await tableClient.getEntity(pk, rk);
|
|
64
|
-
* } catch (error) {
|
|
65
|
-
* if (isNotFoundError(error)) return notFoundResponse('Entity');
|
|
66
|
-
* throw error;
|
|
67
|
-
* }
|
|
68
|
-
*/
|
|
69
|
-
export declare function isNotFoundError(error: unknown): boolean;
|
|
70
|
-
/**
|
|
71
|
-
* Checks if an error is an Azure Table Storage "conflict" error.
|
|
72
|
-
* @param error - The caught error
|
|
73
|
-
* @returns True if error has statusCode 409
|
|
74
|
-
* @example
|
|
75
|
-
* try {
|
|
76
|
-
* await tableClient.createEntity(entity);
|
|
77
|
-
* } catch (error) {
|
|
78
|
-
* if (isConflictError(error)) return conflictResponse('Entity already exists');
|
|
79
|
-
* throw error;
|
|
80
|
-
* }
|
|
81
|
-
*/
|
|
82
|
-
export declare function isConflictError(error: unknown): boolean;
|