alepha 0.7.0 → 0.7.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 +14 -3
- package/assets/logo.png +0 -0
- package/cache.cjs +0 -1
- package/cache.d.ts +30 -13
- package/cache.js +0 -1
- package/core.cjs +0 -1
- package/core.d.ts +465 -196
- package/core.js +0 -1
- package/datetime.cjs +0 -1
- package/datetime.d.ts +32 -31
- package/datetime.js +0 -1
- package/lock.cjs +0 -1
- package/lock.d.ts +2 -2
- package/lock.js +0 -1
- package/package.json +55 -47
- package/postgres.cjs +0 -1
- package/postgres.d.ts +3238 -275
- package/postgres.js +0 -1
- package/queue.cjs +0 -1
- package/queue.d.ts +2 -2
- package/queue.js +0 -1
- package/react/auth.cjs +0 -1
- package/react/auth.d.ts +12 -5
- package/react/auth.js +0 -1
- package/react.cjs +0 -1
- package/react.d.ts +361 -206
- package/react.js +0 -1
- package/redis.cjs +0 -1
- package/redis.js +0 -1
- package/retry.cjs +12 -0
- package/retry.d.ts +68 -0
- package/retry.js +1 -0
- package/scheduler.cjs +0 -1
- package/scheduler.js +0 -1
- package/security.cjs +0 -1
- package/security.d.ts +82 -35
- package/security.js +0 -1
- package/server/cookies.cjs +0 -1
- package/server/cookies.d.ts +1 -1
- package/server/cookies.js +0 -1
- package/server/metrics.cjs +0 -1
- package/server/metrics.js +0 -1
- package/server/proxy.cjs +0 -1
- package/server/proxy.js +0 -1
- package/server/static.cjs +0 -1
- package/server/static.d.ts +1 -0
- package/server/static.js +0 -1
- package/server/swagger.cjs +0 -1
- package/server/swagger.js +0 -1
- package/server.cjs +0 -1
- package/server.d.ts +402 -327
- package/server.js +0 -1
- package/src/retry.ts +1 -0
- package/topic.cjs +0 -1
- package/topic.d.ts +3 -3
- package/topic.js +0 -1
- package/vite.cjs +0 -1
- package/vite.d.ts +24 -35
- package/vite.js +0 -1
- package/cache.cjs.map +0 -1
- package/cache.js.map +0 -1
- package/core.cjs.map +0 -1
- package/core.js.map +0 -1
- package/datetime.cjs.map +0 -1
- package/datetime.js.map +0 -1
- package/lock.cjs.map +0 -1
- package/lock.js.map +0 -1
- package/postgres.cjs.map +0 -1
- package/postgres.js.map +0 -1
- package/queue.cjs.map +0 -1
- package/queue.js.map +0 -1
- package/react/auth.cjs.map +0 -1
- package/react/auth.js.map +0 -1
- package/react.cjs.map +0 -1
- package/react.js.map +0 -1
- package/redis.cjs.map +0 -1
- package/redis.js.map +0 -1
- package/scheduler.cjs.map +0 -1
- package/scheduler.js.map +0 -1
- package/security.cjs.map +0 -1
- package/security.js.map +0 -1
- package/server/cookies.cjs.map +0 -1
- package/server/cookies.js.map +0 -1
- package/server/metrics.cjs.map +0 -1
- package/server/metrics.js.map +0 -1
- package/server/proxy.cjs.map +0 -1
- package/server/proxy.js.map +0 -1
- package/server/static.cjs.map +0 -1
- package/server/static.js.map +0 -1
- package/server/swagger.cjs.map +0 -1
- package/server/swagger.js.map +0 -1
- package/server.cjs.map +0 -1
- package/server.js.map +0 -1
- package/topic.cjs.map +0 -1
- package/topic.js.map +0 -1
- package/vite.cjs.map +0 -1
- package/vite.js.map +0 -1
package/react.js
CHANGED
package/redis.cjs
CHANGED
package/redis.js
CHANGED
package/retry.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var retry = require('@alepha/retry');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.keys(retry).forEach(function (k) {
|
|
8
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return retry[k]; }
|
|
11
|
+
});
|
|
12
|
+
});
|
package/retry.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { MaybePromise } from '@alepha/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Retry Descriptor options.
|
|
5
|
+
*/
|
|
6
|
+
interface RetryDescriptorOptions<T extends (...args: any[]) => any> {
|
|
7
|
+
/**
|
|
8
|
+
* Maximum number of attempts.
|
|
9
|
+
*
|
|
10
|
+
* @default 3
|
|
11
|
+
*/
|
|
12
|
+
max?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Delay in milliseconds.
|
|
15
|
+
*
|
|
16
|
+
* @default 0
|
|
17
|
+
*/
|
|
18
|
+
delay?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Optional condition to determine when to retry.
|
|
21
|
+
*/
|
|
22
|
+
when?: (error: Error) => boolean;
|
|
23
|
+
/**
|
|
24
|
+
* The function to retry.
|
|
25
|
+
*/
|
|
26
|
+
handler: T;
|
|
27
|
+
/**
|
|
28
|
+
* Optional error handler.
|
|
29
|
+
*
|
|
30
|
+
* This will be called when an error occurs.
|
|
31
|
+
*
|
|
32
|
+
* @default undefined
|
|
33
|
+
*/
|
|
34
|
+
onError?: (error: Error, attempt: number, ...parameters: Parameters<T>) => void;
|
|
35
|
+
}
|
|
36
|
+
type RetryDescriptor<T extends (...args: any[]) => any> = (...parameters: Parameters<T>) => MaybePromise<ReturnType<T>>;
|
|
37
|
+
/**
|
|
38
|
+
* `$retry` creates a retry descriptor.
|
|
39
|
+
*
|
|
40
|
+
* It will retry the given function up to `max` times with a delay of `delay` milliseconds between attempts.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { $retry } from "@alepha/core";
|
|
45
|
+
*
|
|
46
|
+
* class MyService {
|
|
47
|
+
* fetchData = $retry({
|
|
48
|
+
* max: 5, // maximum number of attempts
|
|
49
|
+
* delay: 1000, // ms
|
|
50
|
+
* when: (error) => error.message.includes("Network Error"),
|
|
51
|
+
* handler: async (url: string) => {
|
|
52
|
+
* const response = await fetch(url);
|
|
53
|
+
* if (!response.ok) {
|
|
54
|
+
* throw new Error(`Failed to fetch: ${response.statusText}`);
|
|
55
|
+
* }
|
|
56
|
+
* return response.json();
|
|
57
|
+
* },
|
|
58
|
+
* onError: (error, attempt, url) => {
|
|
59
|
+
* // error happened, log it or handle it
|
|
60
|
+
* console.error(`Attempt ${attempt} failed for ${url}:`, error);
|
|
61
|
+
* },
|
|
62
|
+
* });
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare const $retry: <T extends (...args: any[]) => any>(opts: RetryDescriptorOptions<T>) => RetryDescriptor<T>;
|
|
67
|
+
|
|
68
|
+
export { $retry, type RetryDescriptor, type RetryDescriptorOptions };
|
package/retry.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@alepha/retry';
|
package/scheduler.cjs
CHANGED
package/scheduler.js
CHANGED
package/security.cjs
CHANGED
package/security.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as _alepha_core from '@alepha/core';
|
|
2
2
|
import { Static as Static$1, KIND, OPTIONS, Alepha } from '@alepha/core';
|
|
3
|
-
import { JWSHeaderParameters, FlattenedJWSInput, CryptoKey, KeyObject, JSONWebKeySet, JWTVerifyResult, JWTPayload
|
|
3
|
+
import { JWTHeaderParameters, JWSHeaderParameters, FlattenedJWSInput, CryptoKey, KeyObject, JSONWebKeySet, JWTVerifyResult, JWTPayload } from 'jose';
|
|
4
|
+
import { DateTimeProvider } from '@alepha/datetime';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Represents a User Account extracted from JWT.
|
|
@@ -18,6 +19,14 @@ interface UserAccountInfo {
|
|
|
18
19
|
* User full name, if available.
|
|
19
20
|
*/
|
|
20
21
|
name?: string;
|
|
22
|
+
/**
|
|
23
|
+
* User email, if available.
|
|
24
|
+
*/
|
|
25
|
+
email?: string;
|
|
26
|
+
/**
|
|
27
|
+
* User profile picture URL, if available.
|
|
28
|
+
*/
|
|
29
|
+
picture?: string;
|
|
21
30
|
/**
|
|
22
31
|
* Organization ID, if available.
|
|
23
32
|
*/
|
|
@@ -237,6 +246,7 @@ declare const roleSchema: TObject<{
|
|
|
237
246
|
permissions: TArray<TObject<{
|
|
238
247
|
name: TString;
|
|
239
248
|
ownership: TOptional<TBoolean>;
|
|
249
|
+
exclude: TOptional<TArray<TString>>;
|
|
240
250
|
}>>;
|
|
241
251
|
}>;
|
|
242
252
|
type Role = Static$1<typeof roleSchema>;
|
|
@@ -247,6 +257,7 @@ type Role = Static$1<typeof roleSchema>;
|
|
|
247
257
|
declare class JwtProvider {
|
|
248
258
|
protected readonly log: _alepha_core.Logger;
|
|
249
259
|
protected readonly keystore: KeyLoaderHolder[];
|
|
260
|
+
protected readonly dateTimeProvider: DateTimeProvider;
|
|
250
261
|
/**
|
|
251
262
|
* Adds a key loader to the embedded keystore.
|
|
252
263
|
*
|
|
@@ -312,7 +323,7 @@ interface KeyLoaderHolder {
|
|
|
312
323
|
interface JwtSignOptions {
|
|
313
324
|
issuedAt?: boolean;
|
|
314
325
|
protectedHeader?: JWTHeaderParameters;
|
|
315
|
-
expiresIn?:
|
|
326
|
+
expiresIn?: number;
|
|
316
327
|
}
|
|
317
328
|
interface ExtendedJWTPayload extends JWTPayload {
|
|
318
329
|
name?: string;
|
|
@@ -395,11 +406,11 @@ declare class SecurityProvider {
|
|
|
395
406
|
* Creates a user account from the provided payload.
|
|
396
407
|
*
|
|
397
408
|
* @param payload - The payload to create the user account from.
|
|
398
|
-
* @param [
|
|
409
|
+
* @param [realmName] - The realm containing the roles. Default is all.
|
|
399
410
|
*
|
|
400
411
|
* @returns The user info created from the payload.
|
|
401
412
|
*/
|
|
402
|
-
createInfoFromPayload(payload: JWTPayload,
|
|
413
|
+
createInfoFromPayload(payload: JWTPayload, realmName?: string): UserAccountInfo;
|
|
403
414
|
/**
|
|
404
415
|
* Checks if the user has the specified permission.
|
|
405
416
|
*
|
|
@@ -423,7 +434,11 @@ declare class SecurityProvider {
|
|
|
423
434
|
* @param permission - The permission to check for.
|
|
424
435
|
* @returns True if the user has the role, false otherwise.
|
|
425
436
|
*/
|
|
426
|
-
can(
|
|
437
|
+
can(roleName: string, permission: string | Permission): boolean;
|
|
438
|
+
/**
|
|
439
|
+
* Checks if a user has ownership of a specific permission.
|
|
440
|
+
*/
|
|
441
|
+
ownership(roleName: string, permission: string | Permission): string | boolean | undefined;
|
|
427
442
|
/**
|
|
428
443
|
* Converts a permission object to a string.
|
|
429
444
|
*
|
|
@@ -461,6 +476,8 @@ declare class SecurityProvider {
|
|
|
461
476
|
* @return An array of role strings.
|
|
462
477
|
*/
|
|
463
478
|
getRolesFromPayload(payload: Record<string, any>): string[];
|
|
479
|
+
getPictureFromPayload(payload: Record<string, any>): string | undefined;
|
|
480
|
+
getEmailFromPayload(payload: Record<string, any>): string | undefined;
|
|
464
481
|
/**
|
|
465
482
|
* Returns the name from the given payload.
|
|
466
483
|
*
|
|
@@ -494,6 +511,7 @@ interface Realm {
|
|
|
494
511
|
* This is useful when you want to use a custom user provider for a specific realm.
|
|
495
512
|
*/
|
|
496
513
|
userAccountProvider?: SecurityUserAccountProvider;
|
|
514
|
+
onLoadUser?: (user: UserAccountInfo) => Promise<void> | void;
|
|
497
515
|
}
|
|
498
516
|
interface SecurityUserAccountProvider {
|
|
499
517
|
jwks: string | undefined;
|
|
@@ -532,7 +550,7 @@ interface RealmDescriptorOptions {
|
|
|
532
550
|
*
|
|
533
551
|
* Note: You can skip this if you are using a user account provider with JWKS.
|
|
534
552
|
*/
|
|
535
|
-
secret?: string | JSONWebKeySet;
|
|
553
|
+
secret?: string | JSONWebKeySet | (() => string);
|
|
536
554
|
/**
|
|
537
555
|
* Attach a user account provider to the realm to manage roles.
|
|
538
556
|
* For example, you can use a KeycloakUserProvider to automatically create realm roles inside Keycloak.
|
|
@@ -596,41 +614,62 @@ declare const $role: {
|
|
|
596
614
|
};
|
|
597
615
|
|
|
598
616
|
/**
|
|
599
|
-
*
|
|
617
|
+
* Allow to get an access token for a service account.
|
|
618
|
+
*
|
|
619
|
+
* You have some options to configure the service account:
|
|
620
|
+
* - a OAUTH2 URL using client credentials grant type
|
|
621
|
+
* - a JWT secret shared between the services
|
|
622
|
+
*
|
|
623
|
+
* @example
|
|
624
|
+
* ```ts
|
|
625
|
+
* import { $serviceAccount } from "@alepha/security";
|
|
600
626
|
*
|
|
601
|
-
*
|
|
627
|
+
* class MyService {
|
|
628
|
+
* serviceAccount = $serviceAccount({
|
|
629
|
+
* oauth2: {
|
|
630
|
+
* url: "https://example.com/oauth2/token",
|
|
631
|
+
* clientId: "your-client-id",
|
|
632
|
+
* clientSecret: "your-client-secret",
|
|
633
|
+
* }
|
|
634
|
+
* });
|
|
635
|
+
*
|
|
636
|
+
* async fetchData() {
|
|
637
|
+
* const token = await this.serviceAccount.token();
|
|
638
|
+
* // or
|
|
639
|
+
* const response = await this.serviceAccount.fetch("https://api.example.com/data");
|
|
640
|
+
* }
|
|
641
|
+
* }
|
|
642
|
+
* ```
|
|
602
643
|
*/
|
|
603
644
|
declare const $serviceAccount: (options: ServiceAccountDescriptorOptions) => ServiceAccountDescriptor;
|
|
604
645
|
type ServiceAccountDescriptorOptions = {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
*/
|
|
609
|
-
url: string;
|
|
610
|
-
/**
|
|
611
|
-
* Client ID.
|
|
612
|
-
*/
|
|
613
|
-
clientId: string;
|
|
614
|
-
/**
|
|
615
|
-
* Client Secret.
|
|
616
|
-
*/
|
|
617
|
-
clientSecret: string;
|
|
618
|
-
/**
|
|
619
|
-
* Scopes to request.
|
|
620
|
-
*/
|
|
621
|
-
scope?: string;
|
|
622
|
-
};
|
|
646
|
+
gracePeriod?: number;
|
|
647
|
+
} & ({
|
|
648
|
+
oauth2: Oauth2ServiceAccountDescriptorOptions;
|
|
623
649
|
} | {
|
|
624
|
-
jwt:
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
650
|
+
jwt: JwtServiceAccountDescriptorOptions;
|
|
651
|
+
});
|
|
652
|
+
interface JwtServiceAccountDescriptorOptions {
|
|
653
|
+
secret: string;
|
|
654
|
+
roles?: string[];
|
|
655
|
+
signOptions?: JwtSignOptions;
|
|
656
|
+
}
|
|
657
|
+
interface Oauth2ServiceAccountDescriptorOptions {
|
|
658
|
+
/**
|
|
659
|
+
* Get Token URL.
|
|
660
|
+
*/
|
|
661
|
+
url: string;
|
|
662
|
+
/**
|
|
663
|
+
* Client ID.
|
|
664
|
+
*/
|
|
665
|
+
clientId: string;
|
|
666
|
+
/**
|
|
667
|
+
* Client Secret.
|
|
668
|
+
*/
|
|
669
|
+
clientSecret: string;
|
|
670
|
+
}
|
|
629
671
|
interface ServiceAccountDescriptor {
|
|
630
|
-
options: ServiceAccountDescriptorOptions;
|
|
631
|
-
store: ServiceAccountStore;
|
|
632
672
|
token: () => Promise<string>;
|
|
633
|
-
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
634
673
|
}
|
|
635
674
|
interface AccessTokenResponse {
|
|
636
675
|
access_token: string;
|
|
@@ -650,9 +689,17 @@ declare class SecurityError extends Error {
|
|
|
650
689
|
readonly code = "ERR_SECURITY";
|
|
651
690
|
}
|
|
652
691
|
|
|
692
|
+
declare module "alepha/core" {
|
|
693
|
+
interface Hooks {
|
|
694
|
+
"security:user:created": {
|
|
695
|
+
realm: string;
|
|
696
|
+
user: UserAccountInfo;
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
}
|
|
653
700
|
declare class SecurityModule {
|
|
654
701
|
protected readonly alepha: Alepha;
|
|
655
702
|
constructor();
|
|
656
703
|
}
|
|
657
704
|
|
|
658
|
-
export { $permission, $realm, $role, $serviceAccount, type AccessTokenResponse, type ExtendedJWTPayload, InvalidPermissionError, type JwtParseResult, JwtProvider, type JwtSignOptions, type KeyLoader, type KeyLoaderHolder, type Permission, type PermissionDescriptor, type PermissionDescriptorOptions, type Realm, type RealmConfig, type RealmDescriptor, type RealmDescriptorOptions, type Role, type RoleDescriptor, type RoleDescriptorOptions, type SecurityCheckResult, SecurityError, SecurityModule, SecurityProvider, type SecurityUserAccountProvider, type ServiceAccountDescriptor, type ServiceAccountDescriptorOptions, type ServiceAccountStore, type UserAccountInfo, type UserAccountToken, permissionSchema, roleSchema };
|
|
705
|
+
export { $permission, $realm, $role, $serviceAccount, type AccessTokenResponse, type ExtendedJWTPayload, InvalidPermissionError, type JwtParseResult, JwtProvider, type JwtServiceAccountDescriptorOptions, type JwtSignOptions, type KeyLoader, type KeyLoaderHolder, type Oauth2ServiceAccountDescriptorOptions, type Permission, type PermissionDescriptor, type PermissionDescriptorOptions, type Realm, type RealmConfig, type RealmDescriptor, type RealmDescriptorOptions, type Role, type RoleDescriptor, type RoleDescriptorOptions, type SecurityCheckResult, SecurityError, SecurityModule, SecurityProvider, type SecurityUserAccountProvider, type ServiceAccountDescriptor, type ServiceAccountDescriptorOptions, type ServiceAccountStore, type UserAccountInfo, type UserAccountToken, permissionSchema, roleSchema };
|
package/security.js
CHANGED
package/server/cookies.cjs
CHANGED
package/server/cookies.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ declare class ServerCookiesProvider {
|
|
|
44
44
|
readonly onRequest: _alepha_core.HookDescriptor<"server:onRequest">;
|
|
45
45
|
readonly onSend: _alepha_core.HookDescriptor<"server:onSend">;
|
|
46
46
|
fromHeader(header: string): Record<string, string>;
|
|
47
|
-
toHeader(cookies: Record<string, Cookie | null
|
|
47
|
+
toHeader(cookies: Record<string, Cookie | null>, isHttps?: boolean): string[];
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
declare module "alepha/server" {
|
package/server/cookies.js
CHANGED
package/server/metrics.cjs
CHANGED
package/server/metrics.js
CHANGED
package/server/proxy.cjs
CHANGED
package/server/proxy.js
CHANGED
package/server/static.cjs
CHANGED
package/server/static.d.ts
CHANGED
|
@@ -94,6 +94,7 @@ declare class ServerStaticProvider {
|
|
|
94
94
|
list(name: string): string[];
|
|
95
95
|
serve(options: ServeDescriptorOptions): Promise<void>;
|
|
96
96
|
createFileHandler(filepath: string, options: ServeDescriptorOptions): Promise<ServerHandler>;
|
|
97
|
+
protected getCacheFileTypes(): string[];
|
|
97
98
|
protected getCacheControl(filename: string, options: ServeDescriptorOptions): {
|
|
98
99
|
maxAge: number;
|
|
99
100
|
immutable: boolean;
|
package/server/static.js
CHANGED
package/server/swagger.cjs
CHANGED
package/server/swagger.js
CHANGED
package/server.cjs
CHANGED