alepha 0.13.1 → 0.13.2
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 +1 -1
- package/dist/api-files/index.d.ts +28 -91
- package/dist/api-files/index.js +10 -755
- package/dist/api-files/index.js.map +1 -1
- package/dist/api-jobs/index.d.ts +46 -46
- package/dist/api-jobs/index.js +13 -13
- package/dist/api-jobs/index.js.map +1 -1
- package/dist/api-notifications/index.d.ts +129 -146
- package/dist/api-notifications/index.js +17 -39
- package/dist/api-notifications/index.js.map +1 -1
- package/dist/api-parameters/index.d.ts +21 -22
- package/dist/api-parameters/index.js +22 -22
- package/dist/api-parameters/index.js.map +1 -1
- package/dist/api-users/index.d.ts +223 -2000
- package/dist/api-users/index.js +914 -4787
- package/dist/api-users/index.js.map +1 -1
- package/dist/api-verifications/index.d.ts +96 -96
- package/dist/batch/index.d.ts +13 -13
- package/dist/batch/index.js +8 -8
- package/dist/batch/index.js.map +1 -1
- package/dist/bucket/index.d.ts +14 -14
- package/dist/bucket/index.js +12 -12
- package/dist/bucket/index.js.map +1 -1
- package/dist/cache/index.d.ts +11 -11
- package/dist/cache/index.js +9 -9
- package/dist/cache/index.js.map +1 -1
- package/dist/cli/index.d.ts +28 -26
- package/dist/cli/index.js +50 -13
- package/dist/cli/index.js.map +1 -1
- package/dist/command/index.d.ts +19 -19
- package/dist/command/index.js +25 -25
- package/dist/command/index.js.map +1 -1
- package/dist/core/index.browser.js +218 -218
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +232 -232
- package/dist/core/index.js +218 -218
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +2113 -0
- package/dist/core/index.native.js.map +1 -0
- package/dist/datetime/index.d.ts +9 -9
- package/dist/datetime/index.js +7 -7
- package/dist/datetime/index.js.map +1 -1
- package/dist/email/index.d.ts +16 -16
- package/dist/email/index.js +9 -9
- package/dist/email/index.js.map +1 -1
- package/dist/file/index.js +1 -1
- package/dist/file/index.js.map +1 -1
- package/dist/lock/index.d.ts +9 -9
- package/dist/lock/index.js +8 -8
- package/dist/lock/index.js.map +1 -1
- package/dist/lock-redis/index.js +3 -66
- package/dist/lock-redis/index.js.map +1 -1
- package/dist/logger/index.d.ts +5 -5
- package/dist/logger/index.js +8 -8
- package/dist/logger/index.js.map +1 -1
- package/dist/orm/index.browser.js +114 -114
- package/dist/orm/index.browser.js.map +1 -1
- package/dist/orm/index.d.ts +218 -218
- package/dist/orm/index.js +46 -46
- package/dist/orm/index.js.map +1 -1
- package/dist/queue/index.d.ts +29 -29
- package/dist/queue/index.js +20 -20
- package/dist/queue/index.js.map +1 -1
- package/dist/queue-redis/index.d.ts +2 -2
- package/dist/redis/index.d.ts +10 -10
- package/dist/retry/index.d.ts +19 -19
- package/dist/retry/index.js +7 -7
- package/dist/retry/index.js.map +1 -1
- package/dist/scheduler/index.d.ts +16 -16
- package/dist/scheduler/index.js +9 -9
- package/dist/scheduler/index.js.map +1 -1
- package/dist/security/index.d.ts +80 -80
- package/dist/security/index.js +32 -32
- package/dist/security/index.js.map +1 -1
- package/dist/server/index.browser.js +1 -1
- package/dist/server/index.browser.js.map +1 -1
- package/dist/server/index.d.ts +101 -101
- package/dist/server/index.js +16 -16
- package/dist/server/index.js.map +1 -1
- package/dist/server-auth/index.browser.js +4 -982
- package/dist/server-auth/index.browser.js.map +1 -1
- package/dist/server-auth/index.d.ts +204 -785
- package/dist/server-auth/index.js +47 -1239
- package/dist/server-auth/index.js.map +1 -1
- package/dist/server-cache/index.d.ts +10 -10
- package/dist/server-cache/index.js +2 -2
- package/dist/server-cache/index.js.map +1 -1
- package/dist/server-compress/index.d.ts +4 -4
- package/dist/server-compress/index.js +1 -1
- package/dist/server-compress/index.js.map +1 -1
- package/dist/server-cookies/index.browser.js +8 -8
- package/dist/server-cookies/index.browser.js.map +1 -1
- package/dist/server-cookies/index.d.ts +17 -17
- package/dist/server-cookies/index.js +10 -10
- package/dist/server-cookies/index.js.map +1 -1
- package/dist/server-cors/index.d.ts +17 -17
- package/dist/server-cors/index.js +9 -9
- package/dist/server-cors/index.js.map +1 -1
- package/dist/server-health/index.d.ts +19 -19
- package/dist/server-helmet/index.d.ts +1 -1
- package/dist/server-links/index.browser.js +12 -12
- package/dist/server-links/index.browser.js.map +1 -1
- package/dist/server-links/index.d.ts +59 -251
- package/dist/server-links/index.js +23 -502
- package/dist/server-links/index.js.map +1 -1
- package/dist/server-metrics/index.d.ts +4 -4
- package/dist/server-multipart/index.d.ts +2 -2
- package/dist/server-proxy/index.d.ts +12 -12
- package/dist/server-proxy/index.js +10 -10
- package/dist/server-proxy/index.js.map +1 -1
- package/dist/server-rate-limit/index.d.ts +22 -22
- package/dist/server-rate-limit/index.js +12 -12
- package/dist/server-rate-limit/index.js.map +1 -1
- package/dist/server-security/index.d.ts +22 -22
- package/dist/server-security/index.js +15 -15
- package/dist/server-security/index.js.map +1 -1
- package/dist/server-static/index.d.ts +14 -14
- package/dist/server-static/index.js +8 -8
- package/dist/server-static/index.js.map +1 -1
- package/dist/server-swagger/index.d.ts +25 -184
- package/dist/server-swagger/index.js +21 -724
- package/dist/server-swagger/index.js.map +1 -1
- package/dist/sms/index.d.ts +14 -14
- package/dist/sms/index.js +9 -9
- package/dist/sms/index.js.map +1 -1
- package/dist/thread/index.d.ts +11 -11
- package/dist/thread/index.js +17 -17
- package/dist/thread/index.js.map +1 -1
- package/dist/topic/index.d.ts +26 -26
- package/dist/topic/index.js +16 -16
- package/dist/topic/index.js.map +1 -1
- package/dist/topic-redis/index.d.ts +1 -1
- package/dist/vite/index.d.ts +3 -3
- package/dist/vite/index.js +8 -8
- package/dist/vite/index.js.map +1 -1
- package/dist/websocket/index.browser.js +11 -11
- package/dist/websocket/index.browser.js.map +1 -1
- package/dist/websocket/index.d.ts +58 -58
- package/dist/websocket/index.js +13 -13
- package/dist/websocket/index.js.map +1 -1
- package/package.json +113 -52
- package/src/api-files/services/FileService.ts +5 -7
- package/src/api-jobs/index.ts +1 -1
- package/src/api-jobs/{descriptors → primitives}/$job.ts +8 -8
- package/src/api-jobs/providers/JobProvider.ts +9 -9
- package/src/api-jobs/services/JobService.ts +5 -5
- package/src/api-notifications/index.ts +5 -15
- package/src/api-notifications/{descriptors → primitives}/$notification.ts +10 -10
- package/src/api-notifications/services/NotificationSenderService.ts +3 -3
- package/src/api-parameters/index.ts +1 -1
- package/src/api-parameters/{descriptors → primitives}/$config.ts +7 -12
- package/src/api-users/index.ts +1 -1
- package/src/api-users/{descriptors → primitives}/$userRealm.ts +8 -8
- package/src/api-users/providers/UserRealmProvider.ts +1 -1
- package/src/batch/index.ts +3 -3
- package/src/batch/{descriptors → primitives}/$batch.ts +13 -16
- package/src/bucket/index.ts +8 -8
- package/src/bucket/{descriptors → primitives}/$bucket.ts +8 -8
- package/src/bucket/providers/LocalFileStorageProvider.ts +3 -3
- package/src/cache/index.ts +4 -4
- package/src/cache/{descriptors → primitives}/$cache.ts +15 -15
- package/src/cli/apps/AlephaPackageBuilderCli.ts +24 -2
- package/src/cli/commands/DrizzleCommands.ts +6 -6
- package/src/cli/commands/VerifyCommands.ts +1 -1
- package/src/cli/commands/ViteCommands.ts +6 -1
- package/src/cli/services/ProjectUtils.ts +34 -3
- package/src/command/index.ts +5 -5
- package/src/command/{descriptors → primitives}/$command.ts +9 -12
- package/src/command/providers/CliProvider.ts +10 -10
- package/src/core/Alepha.ts +30 -33
- package/src/core/constants/KIND.ts +1 -1
- package/src/core/constants/OPTIONS.ts +1 -1
- package/src/core/helpers/{descriptor.ts → primitive.ts} +18 -18
- package/src/core/helpers/ref.ts +1 -1
- package/src/core/index.shared.ts +8 -8
- package/src/core/{descriptors → primitives}/$context.ts +5 -5
- package/src/core/{descriptors → primitives}/$hook.ts +4 -4
- package/src/core/{descriptors → primitives}/$inject.ts +2 -2
- package/src/core/{descriptors → primitives}/$module.ts +9 -9
- package/src/core/{descriptors → primitives}/$use.ts +2 -2
- package/src/core/providers/CodecManager.ts +1 -1
- package/src/core/providers/JsonSchemaCodec.ts +1 -1
- package/src/core/providers/StateManager.ts +2 -2
- package/src/datetime/index.ts +3 -3
- package/src/datetime/{descriptors → primitives}/$interval.ts +6 -6
- package/src/email/index.ts +4 -4
- package/src/email/{descriptors → primitives}/$email.ts +8 -8
- package/src/file/index.ts +1 -1
- package/src/lock/index.ts +3 -3
- package/src/lock/{descriptors → primitives}/$lock.ts +10 -10
- package/src/logger/index.ts +8 -8
- package/src/logger/{descriptors → primitives}/$logger.ts +2 -2
- package/src/logger/services/Logger.ts +1 -1
- package/src/orm/constants/PG_SYMBOLS.ts +2 -2
- package/src/orm/index.browser.ts +2 -2
- package/src/orm/index.ts +8 -8
- package/src/orm/{descriptors → primitives}/$entity.ts +11 -11
- package/src/orm/{descriptors → primitives}/$repository.ts +2 -2
- package/src/orm/{descriptors → primitives}/$sequence.ts +8 -8
- package/src/orm/{descriptors → primitives}/$transaction.ts +4 -4
- package/src/orm/providers/PostgresTypeProvider.ts +3 -3
- package/src/orm/providers/RepositoryProvider.ts +4 -4
- package/src/orm/providers/drivers/DatabaseProvider.ts +7 -7
- package/src/orm/services/ModelBuilder.ts +9 -9
- package/src/orm/services/PgRelationManager.ts +2 -2
- package/src/orm/services/PostgresModelBuilder.ts +5 -5
- package/src/orm/services/Repository.ts +7 -7
- package/src/orm/services/SqliteModelBuilder.ts +5 -5
- package/src/queue/index.ts +7 -7
- package/src/queue/{descriptors → primitives}/$consumer.ts +15 -15
- package/src/queue/{descriptors → primitives}/$queue.ts +12 -12
- package/src/queue/providers/WorkerProvider.ts +7 -7
- package/src/retry/index.ts +3 -3
- package/src/retry/{descriptors → primitives}/$retry.ts +14 -14
- package/src/scheduler/index.ts +3 -3
- package/src/scheduler/{descriptors → primitives}/$scheduler.ts +9 -9
- package/src/scheduler/providers/CronProvider.ts +1 -1
- package/src/security/index.ts +9 -9
- package/src/security/{descriptors → primitives}/$permission.ts +7 -7
- package/src/security/{descriptors → primitives}/$realm.ts +6 -12
- package/src/security/{descriptors → primitives}/$role.ts +12 -12
- package/src/security/{descriptors → primitives}/$serviceAccount.ts +8 -8
- package/src/server/index.browser.ts +1 -1
- package/src/server/index.ts +14 -14
- package/src/server/{descriptors → primitives}/$action.ts +13 -13
- package/src/server/{descriptors → primitives}/$route.ts +9 -9
- package/src/server/providers/NodeHttpServerProvider.ts +1 -1
- package/src/server/services/HttpClient.ts +1 -1
- package/src/server-auth/index.browser.ts +1 -1
- package/src/server-auth/index.ts +6 -6
- package/src/server-auth/{descriptors → primitives}/$auth.ts +10 -10
- package/src/server-auth/{descriptors → primitives}/$authCredentials.ts +4 -4
- package/src/server-auth/{descriptors → primitives}/$authGithub.ts +4 -4
- package/src/server-auth/{descriptors → primitives}/$authGoogle.ts +4 -4
- package/src/server-auth/providers/ServerAuthProvider.ts +4 -4
- package/src/server-cache/providers/ServerCacheProvider.ts +7 -7
- package/src/server-compress/providers/ServerCompressProvider.ts +3 -3
- package/src/server-cookies/index.browser.ts +2 -2
- package/src/server-cookies/index.ts +5 -5
- package/src/server-cookies/{descriptors → primitives}/$cookie.browser.ts +12 -12
- package/src/server-cookies/{descriptors → primitives}/$cookie.ts +13 -13
- package/src/server-cookies/providers/ServerCookiesProvider.ts +4 -4
- package/src/server-cookies/services/CookieParser.ts +1 -1
- package/src/server-cors/index.ts +3 -3
- package/src/server-cors/{descriptors → primitives}/$cors.ts +11 -13
- package/src/server-cors/providers/ServerCorsProvider.ts +5 -5
- package/src/server-links/index.browser.ts +5 -5
- package/src/server-links/index.ts +9 -9
- package/src/server-links/{descriptors → primitives}/$remote.ts +11 -11
- package/src/server-links/providers/LinkProvider.ts +7 -7
- package/src/server-links/providers/{RemoteDescriptorProvider.ts → RemotePrimitiveProvider.ts} +6 -6
- package/src/server-links/providers/ServerLinksProvider.ts +3 -3
- package/src/server-proxy/index.ts +3 -3
- package/src/server-proxy/{descriptors → primitives}/$proxy.ts +8 -8
- package/src/server-proxy/providers/ServerProxyProvider.ts +4 -4
- package/src/server-rate-limit/index.ts +6 -6
- package/src/server-rate-limit/{descriptors → primitives}/$rateLimit.ts +13 -13
- package/src/server-rate-limit/providers/ServerRateLimitProvider.ts +5 -5
- package/src/server-security/index.ts +3 -3
- package/src/server-security/{descriptors → primitives}/$basicAuth.ts +13 -13
- package/src/server-security/providers/ServerBasicAuthProvider.ts +5 -5
- package/src/server-security/providers/ServerSecurityProvider.ts +4 -4
- package/src/server-static/index.ts +3 -3
- package/src/server-static/{descriptors → primitives}/$serve.ts +8 -10
- package/src/server-static/providers/ServerStaticProvider.ts +6 -6
- package/src/server-swagger/index.ts +5 -5
- package/src/server-swagger/{descriptors → primitives}/$swagger.ts +9 -9
- package/src/server-swagger/providers/ServerSwaggerProvider.ts +11 -10
- package/src/sms/index.ts +4 -4
- package/src/sms/{descriptors → primitives}/$sms.ts +8 -8
- package/src/thread/index.ts +3 -3
- package/src/thread/{descriptors → primitives}/$thread.ts +13 -13
- package/src/thread/providers/ThreadProvider.ts +7 -9
- package/src/topic/index.ts +5 -5
- package/src/topic/{descriptors → primitives}/$subscriber.ts +14 -14
- package/src/topic/{descriptors → primitives}/$topic.ts +10 -10
- package/src/topic/providers/TopicProvider.ts +4 -4
- package/src/vite/tasks/copyAssets.ts +1 -1
- package/src/vite/tasks/generateSitemap.ts +3 -3
- package/src/vite/tasks/prerenderPages.ts +2 -2
- package/src/vite/tasks/runAlepha.ts +2 -2
- package/src/websocket/index.browser.ts +3 -3
- package/src/websocket/index.shared.ts +2 -2
- package/src/websocket/index.ts +4 -4
- package/src/websocket/interfaces/WebSocketInterfaces.ts +3 -3
- package/src/websocket/{descriptors → primitives}/$channel.ts +10 -10
- package/src/websocket/{descriptors → primitives}/$websocket.ts +8 -8
- package/src/websocket/providers/NodeWebSocketServerProvider.ts +7 -7
- package/src/websocket/providers/WebSocketServerProvider.ts +3 -3
- package/src/websocket/services/WebSocketClient.ts +5 -5
- package/src/api-notifications/providers/MemorySmsProvider.ts +0 -20
- package/src/api-notifications/providers/SmsProvider.ts +0 -8
- /package/src/core/{descriptors → primitives}/$atom.ts +0 -0
- /package/src/core/{descriptors → primitives}/$env.ts +0 -0
- /package/src/server-auth/{descriptors → primitives}/$authApple.ts +0 -0
- /package/src/server-links/{descriptors → primitives}/$client.ts +0 -0
|
@@ -7,7 +7,7 @@ import * as TypeBoxValue from "typebox/value";
|
|
|
7
7
|
|
|
8
8
|
//#region src/core/constants/KIND.ts
|
|
9
9
|
/**
|
|
10
|
-
* Used for identifying
|
|
10
|
+
* Used for identifying primitives.
|
|
11
11
|
*
|
|
12
12
|
* @internal
|
|
13
13
|
*/
|
|
@@ -25,7 +25,7 @@ const MODULE = Symbol.for("Alepha.Module");
|
|
|
25
25
|
//#endregion
|
|
26
26
|
//#region src/core/constants/OPTIONS.ts
|
|
27
27
|
/**
|
|
28
|
-
* Used for
|
|
28
|
+
* Used for primitives options.
|
|
29
29
|
*
|
|
30
30
|
* @internal
|
|
31
31
|
*/
|
|
@@ -40,98 +40,6 @@ var AlephaError = class extends Error {
|
|
|
40
40
|
name = "AlephaError";
|
|
41
41
|
};
|
|
42
42
|
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region src/core/descriptors/$module.ts
|
|
45
|
-
/**
|
|
46
|
-
* Wrap Services and Descriptors into a Module.
|
|
47
|
-
*
|
|
48
|
-
* - A module is just a Service with some extra {@link Module}.
|
|
49
|
-
* - You must attach a `name` to it.
|
|
50
|
-
* - Name must follow the pattern: `project.module.submodule`. (e.g. `myapp.users.auth`).
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```ts
|
|
54
|
-
* import { $module } from "alepha";
|
|
55
|
-
* import { MyService } from "./MyService.ts";
|
|
56
|
-
*
|
|
57
|
-
* // export MyService, so it can be used everywhere (optional)
|
|
58
|
-
* export * from "./MyService.ts";
|
|
59
|
-
*
|
|
60
|
-
* export default $module({
|
|
61
|
-
* name: "my.project.module",
|
|
62
|
-
* // MyService will have a module context "my.project.module"
|
|
63
|
-
* services: [MyService],
|
|
64
|
-
* });
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* ### Why Modules?
|
|
68
|
-
*
|
|
69
|
-
* #### Logging
|
|
70
|
-
*
|
|
71
|
-
* By default, AlephaLogger will log the module name in the logs.
|
|
72
|
-
* This helps to identify where the logs are coming from.
|
|
73
|
-
*
|
|
74
|
-
* You can also set different log levels for different modules.
|
|
75
|
-
* It means you can set 'some.very.specific.module' to 'debug' and keep the rest of the application to 'info'.
|
|
76
|
-
*
|
|
77
|
-
* #### Modulith
|
|
78
|
-
*
|
|
79
|
-
* Force to structure your application in modules, even if it's a single deployable unit.
|
|
80
|
-
* It helps to keep a clean architecture and avoid monolithic applications.
|
|
81
|
-
*
|
|
82
|
-
* A strict mode flag will probably come to enforce module boundaries.
|
|
83
|
-
* -> Throwing errors when a service from another module is injected.
|
|
84
|
-
* But it's not implemented yet.
|
|
85
|
-
*
|
|
86
|
-
* ### When not to use Modules?
|
|
87
|
-
*
|
|
88
|
-
* Small applications does not need modules. It's better to keep it simple.
|
|
89
|
-
* Modules are more useful when the application grows and needs to be structured.
|
|
90
|
-
* If we speak with number of `$actions`, a module should be used when you have more than 30 actions in a single module.
|
|
91
|
-
* Meaning that if you have 100 actions, you should have at least 3 modules.
|
|
92
|
-
*/
|
|
93
|
-
const $module = (options) => {
|
|
94
|
-
const { services = [], descriptors = [], name } = options;
|
|
95
|
-
if (!name || !Module.NAME_REGEX.test(name)) throw new AlephaError(`Invalid module name '${name}'. It should be in the format of 'project.module.submodule'`);
|
|
96
|
-
const $ = class extends Module {
|
|
97
|
-
options = options;
|
|
98
|
-
register(alepha) {
|
|
99
|
-
if (typeof options.register === "function") {
|
|
100
|
-
options.register(alepha);
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
for (const service of services) alepha.inject(service, { parent: this.constructor });
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
Object.defineProperty($, "name", {
|
|
107
|
-
value: name,
|
|
108
|
-
writable: false
|
|
109
|
-
});
|
|
110
|
-
for (const service of services) if (!Module.is(service)) service[MODULE] = $;
|
|
111
|
-
for (const factory of descriptors) if (typeof factory[KIND] === "function") factory[KIND][MODULE] = $;
|
|
112
|
-
return $;
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* Base class for all modules.
|
|
116
|
-
*/
|
|
117
|
-
var Module = class Module {
|
|
118
|
-
static NAME_REGEX = /^[a-z]+(\.[a-z][a-z0-9-]*)*$/;
|
|
119
|
-
/**
|
|
120
|
-
* Check if a Service is a Module.
|
|
121
|
-
*/
|
|
122
|
-
static is(ctor) {
|
|
123
|
-
return ctor.prototype instanceof Module;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Get the Module of a Service.
|
|
127
|
-
*
|
|
128
|
-
* Returns undefined if the Service is not part of a Module.
|
|
129
|
-
*/
|
|
130
|
-
static of(ctor) {
|
|
131
|
-
return ctor[MODULE];
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
|
|
135
43
|
//#endregion
|
|
136
44
|
//#region src/core/errors/CircularDependencyError.ts
|
|
137
45
|
var CircularDependencyError = class extends AlephaError {
|
|
@@ -180,7 +88,7 @@ const __alephaRef = {};
|
|
|
180
88
|
/**
|
|
181
89
|
* Note:
|
|
182
90
|
*
|
|
183
|
-
* This file is used to share context between $
|
|
91
|
+
* This file is used to share context between $primitives and the Alepha core during the injection phase.
|
|
184
92
|
*
|
|
185
93
|
* There is no side effect as long as Alepha is not used concurrently in multiple contexts (which is not the case).
|
|
186
94
|
*
|
|
@@ -219,11 +127,11 @@ const __alephaRef = {};
|
|
|
219
127
|
*/
|
|
220
128
|
|
|
221
129
|
//#endregion
|
|
222
|
-
//#region src/core/
|
|
130
|
+
//#region src/core/primitives/$context.ts
|
|
223
131
|
/**
|
|
224
132
|
* Get Alepha instance and current service from the current context.
|
|
225
133
|
*
|
|
226
|
-
* It can only be used inside $
|
|
134
|
+
* It can only be used inside $primitive functions.
|
|
227
135
|
*
|
|
228
136
|
* ```ts
|
|
229
137
|
* import { $context } from "alepha";
|
|
@@ -232,7 +140,7 @@ const __alephaRef = {};
|
|
|
232
140
|
* const { alepha, service, module } = $context();
|
|
233
141
|
*
|
|
234
142
|
* // alepha - alepha instance
|
|
235
|
-
* // service - class which is creating this
|
|
143
|
+
* // service - class which is creating this primitive, this is NOT the instance but the service definition
|
|
236
144
|
* // module - module definition, if any
|
|
237
145
|
*
|
|
238
146
|
* return {};
|
|
@@ -257,8 +165,8 @@ const $context = () => {
|
|
|
257
165
|
};
|
|
258
166
|
|
|
259
167
|
//#endregion
|
|
260
|
-
//#region src/core/helpers/
|
|
261
|
-
var
|
|
168
|
+
//#region src/core/helpers/primitive.ts
|
|
169
|
+
var Primitive = class {
|
|
262
170
|
alepha;
|
|
263
171
|
options;
|
|
264
172
|
config;
|
|
@@ -272,14 +180,14 @@ var Descriptor = class {
|
|
|
272
180
|
};
|
|
273
181
|
}
|
|
274
182
|
/**
|
|
275
|
-
* Called automatically by Alepha after the
|
|
183
|
+
* Called automatically by Alepha after the primitive is created.
|
|
276
184
|
*/
|
|
277
185
|
onInit() {}
|
|
278
186
|
};
|
|
279
|
-
const
|
|
187
|
+
const createPrimitive = (primitive, options) => {
|
|
280
188
|
const { alepha, service } = $context();
|
|
281
|
-
if (MODULE in
|
|
282
|
-
return alepha.inject(
|
|
189
|
+
if (MODULE in primitive && primitive[MODULE]) alepha.with(primitive[MODULE]);
|
|
190
|
+
return alepha.inject(primitive, {
|
|
283
191
|
lifetime: "transient",
|
|
284
192
|
args: [{
|
|
285
193
|
options,
|
|
@@ -297,6 +205,98 @@ function isClass(func) {
|
|
|
297
205
|
return !!descriptor && !descriptor.writable;
|
|
298
206
|
}
|
|
299
207
|
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/core/primitives/$module.ts
|
|
210
|
+
/**
|
|
211
|
+
* Wrap Services and Primitives into a Module.
|
|
212
|
+
*
|
|
213
|
+
* - A module is just a Service with some extra {@link Module}.
|
|
214
|
+
* - You must attach a `name` to it.
|
|
215
|
+
* - Name must follow the pattern: `project.module.submodule`. (e.g. `myapp.users.auth`).
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { $module } from "alepha";
|
|
220
|
+
* import { MyService } from "./MyService.ts";
|
|
221
|
+
*
|
|
222
|
+
* // export MyService, so it can be used everywhere (optional)
|
|
223
|
+
* export * from "./MyService.ts";
|
|
224
|
+
*
|
|
225
|
+
* export default $module({
|
|
226
|
+
* name: "my.project.module",
|
|
227
|
+
* // MyService will have a module context "my.project.module"
|
|
228
|
+
* services: [MyService],
|
|
229
|
+
* });
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* ### Why Modules?
|
|
233
|
+
*
|
|
234
|
+
* #### Logging
|
|
235
|
+
*
|
|
236
|
+
* By default, AlephaLogger will log the module name in the logs.
|
|
237
|
+
* This helps to identify where the logs are coming from.
|
|
238
|
+
*
|
|
239
|
+
* You can also set different log levels for different modules.
|
|
240
|
+
* It means you can set 'some.very.specific.module' to 'debug' and keep the rest of the application to 'info'.
|
|
241
|
+
*
|
|
242
|
+
* #### Modulith
|
|
243
|
+
*
|
|
244
|
+
* Force to structure your application in modules, even if it's a single deployable unit.
|
|
245
|
+
* It helps to keep a clean architecture and avoid monolithic applications.
|
|
246
|
+
*
|
|
247
|
+
* A strict mode flag will probably come to enforce module boundaries.
|
|
248
|
+
* -> Throwing errors when a service from another module is injected.
|
|
249
|
+
* But it's not implemented yet.
|
|
250
|
+
*
|
|
251
|
+
* ### When not to use Modules?
|
|
252
|
+
*
|
|
253
|
+
* Small applications does not need modules. It's better to keep it simple.
|
|
254
|
+
* Modules are more useful when the application grows and needs to be structured.
|
|
255
|
+
* If we speak with number of `$actions`, a module should be used when you have more than 30 actions in a single module.
|
|
256
|
+
* Meaning that if you have 100 actions, you should have at least 3 modules.
|
|
257
|
+
*/
|
|
258
|
+
const $module = (options) => {
|
|
259
|
+
const { services = [], primitives = [], name } = options;
|
|
260
|
+
if (!name || !Module.NAME_REGEX.test(name)) throw new AlephaError(`Invalid module name '${name}'. It should be in the format of 'project.module.submodule'`);
|
|
261
|
+
const $ = class extends Module {
|
|
262
|
+
options = options;
|
|
263
|
+
register(alepha) {
|
|
264
|
+
if (typeof options.register === "function") {
|
|
265
|
+
options.register(alepha);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
for (const service of services) alepha.inject(service, { parent: this.constructor });
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
Object.defineProperty($, "name", {
|
|
272
|
+
value: name,
|
|
273
|
+
writable: false
|
|
274
|
+
});
|
|
275
|
+
for (const service of services) if (!Module.is(service)) service[MODULE] = $;
|
|
276
|
+
for (const factory of primitives) if (typeof factory[KIND] === "function") factory[KIND][MODULE] = $;
|
|
277
|
+
return $;
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Base class for all modules.
|
|
281
|
+
*/
|
|
282
|
+
var Module = class Module {
|
|
283
|
+
static NAME_REGEX = /^[a-z]+(\.[a-z][a-z0-9-]*)*$/;
|
|
284
|
+
/**
|
|
285
|
+
* Check if a Service is a Module.
|
|
286
|
+
*/
|
|
287
|
+
static is(ctor) {
|
|
288
|
+
return ctor.prototype instanceof Module;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Get the Module of a Service.
|
|
292
|
+
*
|
|
293
|
+
* Returns undefined if the Service is not part of a Module.
|
|
294
|
+
*/
|
|
295
|
+
static of(ctor) {
|
|
296
|
+
return ctor[MODULE];
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
300
|
//#endregion
|
|
301
301
|
//#region src/core/providers/AlsProvider.ts
|
|
302
302
|
var AlsProvider = class AlsProvider {
|
|
@@ -330,7 +330,7 @@ var AlsProvider = class AlsProvider {
|
|
|
330
330
|
};
|
|
331
331
|
|
|
332
332
|
//#endregion
|
|
333
|
-
//#region src/core/
|
|
333
|
+
//#region src/core/primitives/$inject.ts
|
|
334
334
|
/**
|
|
335
335
|
* Get the instance of the specified type from the context.
|
|
336
336
|
*
|
|
@@ -349,7 +349,7 @@ const $inject = (type, opts = {}) => {
|
|
|
349
349
|
...opts
|
|
350
350
|
});
|
|
351
351
|
};
|
|
352
|
-
var
|
|
352
|
+
var InjectPrimitive = class extends Primitive {};
|
|
353
353
|
|
|
354
354
|
//#endregion
|
|
355
355
|
//#region src/core/providers/Json.ts
|
|
@@ -617,7 +617,7 @@ var EventManager = class {
|
|
|
617
617
|
};
|
|
618
618
|
|
|
619
619
|
//#endregion
|
|
620
|
-
//#region src/core/
|
|
620
|
+
//#region src/core/primitives/$atom.ts
|
|
621
621
|
/**
|
|
622
622
|
* Define an atom for state management.
|
|
623
623
|
*
|
|
@@ -820,7 +820,7 @@ var StateManager = class {
|
|
|
820
820
|
* // You can access the environment variables using alepha.env
|
|
821
821
|
* console.log(alepha.env.MY_VAR); // "value"
|
|
822
822
|
*
|
|
823
|
-
* // But you should use $env()
|
|
823
|
+
* // But you should use $env() primitive to get typed values from the environment.
|
|
824
824
|
* class App {
|
|
825
825
|
* env = $env(
|
|
826
826
|
* t.object({
|
|
@@ -833,7 +833,7 @@ var StateManager = class {
|
|
|
833
833
|
* ### Modules
|
|
834
834
|
*
|
|
835
835
|
* Modules are a way to group services together.
|
|
836
|
-
* You can register a module using the `$module`
|
|
836
|
+
* You can register a module using the `$module` primitive.
|
|
837
837
|
*
|
|
838
838
|
* ```ts
|
|
839
839
|
* import { $module } from "alepha";
|
|
@@ -851,7 +851,7 @@ var StateManager = class {
|
|
|
851
851
|
* ### Hooks
|
|
852
852
|
*
|
|
853
853
|
* Hooks are a way to run async functions from all registered providers/services.
|
|
854
|
-
* You can register a hook using the `$hook`
|
|
854
|
+
* You can register a hook using the `$hook` primitive.
|
|
855
855
|
*
|
|
856
856
|
* ```ts
|
|
857
857
|
* import { $hook } from "alepha";
|
|
@@ -912,7 +912,7 @@ var Alepha = class Alepha {
|
|
|
912
912
|
try {
|
|
913
913
|
onTestFinished?.(() => alepha.stop());
|
|
914
914
|
} catch (_error) {}
|
|
915
|
-
alepha.
|
|
915
|
+
alepha.store.set("alepha.test.beforeAll", beforeAll).set("alepha.test.afterAll", afterAll).set("alepha.test.afterEach", afterEach).set("alepha.test.onTestFinished", onTestFinished);
|
|
916
916
|
}
|
|
917
917
|
return alepha;
|
|
918
918
|
}
|
|
@@ -966,9 +966,9 @@ var Alepha = class Alepha {
|
|
|
966
966
|
*/
|
|
967
967
|
substitutions = /* @__PURE__ */ new Map();
|
|
968
968
|
/**
|
|
969
|
-
* Registry of
|
|
969
|
+
* Registry of primitives.
|
|
970
970
|
*/
|
|
971
|
-
|
|
971
|
+
primitiveRegistry = /* @__PURE__ */ new Map();
|
|
972
972
|
/**
|
|
973
973
|
* List of all services + how they are provided.
|
|
974
974
|
*/
|
|
@@ -992,7 +992,7 @@ var Alepha = class Alepha {
|
|
|
992
992
|
/**
|
|
993
993
|
* State manager to store arbitrary values.
|
|
994
994
|
*/
|
|
995
|
-
get
|
|
995
|
+
get store() {
|
|
996
996
|
this.events;
|
|
997
997
|
return this.inject(StateManager, { args: [this.init] });
|
|
998
998
|
}
|
|
@@ -1008,13 +1008,13 @@ var Alepha = class Alepha {
|
|
|
1008
1008
|
* Get logger instance.
|
|
1009
1009
|
*/
|
|
1010
1010
|
get log() {
|
|
1011
|
-
return this.
|
|
1011
|
+
return this.store.get("alepha.logger");
|
|
1012
1012
|
}
|
|
1013
1013
|
/**
|
|
1014
1014
|
* The environment variables for the App.
|
|
1015
1015
|
*/
|
|
1016
1016
|
get env() {
|
|
1017
|
-
return this.
|
|
1017
|
+
return this.store.get("env") ?? {};
|
|
1018
1018
|
}
|
|
1019
1019
|
constructor(init = {}) {
|
|
1020
1020
|
this.init = init;
|
|
@@ -1103,7 +1103,7 @@ var Alepha = class Alepha {
|
|
|
1103
1103
|
* Starts the App.
|
|
1104
1104
|
*
|
|
1105
1105
|
* - Lock any further changes to the container.
|
|
1106
|
-
* - Run "configure" hook for all services.
|
|
1106
|
+
* - Run "configure" hook for all services. Primitives will be processed.
|
|
1107
1107
|
* - Run "start" hook for all services. Providers will connect/listen/...
|
|
1108
1108
|
* - Run "ready" hook for all services. This is the point where the App is ready to serve requests.
|
|
1109
1109
|
*
|
|
@@ -1123,10 +1123,10 @@ var Alepha = class Alepha {
|
|
|
1123
1123
|
const now = Date.now();
|
|
1124
1124
|
this.log?.info("Starting App...");
|
|
1125
1125
|
for (const [key] of this.substitutions.entries()) this.inject(key);
|
|
1126
|
-
const target = this.
|
|
1126
|
+
const target = this.store.get("alepha.target");
|
|
1127
1127
|
if (target) {
|
|
1128
1128
|
this.registry = /* @__PURE__ */ new Map();
|
|
1129
|
-
this.
|
|
1129
|
+
this.primitiveRegistry = /* @__PURE__ */ new Map();
|
|
1130
1130
|
this.with(target);
|
|
1131
1131
|
}
|
|
1132
1132
|
this.locked = true;
|
|
@@ -1320,19 +1320,19 @@ var Alepha = class Alepha {
|
|
|
1320
1320
|
return list;
|
|
1321
1321
|
}
|
|
1322
1322
|
/**
|
|
1323
|
-
* Get all
|
|
1323
|
+
* Get all primitives of the specified type.
|
|
1324
1324
|
*/
|
|
1325
|
-
|
|
1325
|
+
primitives(factory) {
|
|
1326
1326
|
if (typeof factory === "string") {
|
|
1327
1327
|
const key1 = factory.toLowerCase().replace("$", "");
|
|
1328
|
-
const key2 = `${key1}
|
|
1329
|
-
for (const [key, value] of this.
|
|
1328
|
+
const key2 = `${key1}primitive`;
|
|
1329
|
+
for (const [key, value] of this.primitiveRegistry.entries()) {
|
|
1330
1330
|
const name = key.name.toLowerCase();
|
|
1331
1331
|
if (name === key1 || name === key2) return value;
|
|
1332
1332
|
}
|
|
1333
1333
|
return [];
|
|
1334
1334
|
}
|
|
1335
|
-
return this.
|
|
1335
|
+
return this.primitiveRegistry.get(factory[KIND]) ?? [];
|
|
1336
1336
|
}
|
|
1337
1337
|
new(service, args = []) {
|
|
1338
1338
|
this.pendingInstantiations.push(service);
|
|
@@ -1341,10 +1341,10 @@ var Alepha = class Alepha {
|
|
|
1341
1341
|
const instance = isClass(service) ? new service(...args) : service(...args) ?? {};
|
|
1342
1342
|
const obj = instance;
|
|
1343
1343
|
for (const [key, value] of Object.entries(obj)) {
|
|
1344
|
-
if (value instanceof
|
|
1344
|
+
if (value instanceof Primitive) this.processPrimitive(value, key);
|
|
1345
1345
|
if (typeof value === "object" && value !== null && typeof value[OPTIONS] === "object" && "getter" in value[OPTIONS]) {
|
|
1346
1346
|
const getter = value[OPTIONS].getter;
|
|
1347
|
-
Object.defineProperty(obj, key, { get: () => this.
|
|
1347
|
+
Object.defineProperty(obj, key, { get: () => this.store.get(getter) });
|
|
1348
1348
|
}
|
|
1349
1349
|
}
|
|
1350
1350
|
this.pendingInstantiations.pop();
|
|
@@ -1352,15 +1352,95 @@ var Alepha = class Alepha {
|
|
|
1352
1352
|
__alephaRef.service = this.pendingInstantiations[this.pendingInstantiations.length - 1];
|
|
1353
1353
|
return instance;
|
|
1354
1354
|
}
|
|
1355
|
-
|
|
1355
|
+
processPrimitive(value, propertyKey = "") {
|
|
1356
1356
|
value.config.propertyKey = propertyKey;
|
|
1357
1357
|
value.onInit();
|
|
1358
1358
|
const kind = value.constructor;
|
|
1359
|
-
const list = this.
|
|
1360
|
-
this.
|
|
1359
|
+
const list = this.primitiveRegistry.get(kind) ?? [];
|
|
1360
|
+
this.primitiveRegistry.set(kind, [...list, value]);
|
|
1361
1361
|
}
|
|
1362
1362
|
};
|
|
1363
1363
|
|
|
1364
|
+
//#endregion
|
|
1365
|
+
//#region src/core/errors/AppNotStartedError.ts
|
|
1366
|
+
var AppNotStartedError = class extends AlephaError {
|
|
1367
|
+
name = "AppNotStartedError";
|
|
1368
|
+
constructor() {
|
|
1369
|
+
super("App not started. Please start the app before.");
|
|
1370
|
+
}
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1373
|
+
//#endregion
|
|
1374
|
+
//#region src/core/helpers/createPagination.ts
|
|
1375
|
+
/**
|
|
1376
|
+
* Create a pagination object from an array of entities.
|
|
1377
|
+
*
|
|
1378
|
+
* This is a pure function that works with any data source (databases, APIs, caches, arrays, etc.).
|
|
1379
|
+
* It handles the core pagination logic including:
|
|
1380
|
+
* - Slicing the content to the requested page size
|
|
1381
|
+
* - Calculating pagination metadata (offset, page number, etc.)
|
|
1382
|
+
* - Determining navigation state (isFirst, isLast)
|
|
1383
|
+
* - Including sort metadata when provided
|
|
1384
|
+
*
|
|
1385
|
+
* @param entities - The entities to paginate (should include one extra item to detect if there's a next page)
|
|
1386
|
+
* @param limit - The limit of the pagination (page size)
|
|
1387
|
+
* @param offset - The offset of the pagination (starting position)
|
|
1388
|
+
* @param sort - Optional sort metadata to include in response
|
|
1389
|
+
* @returns A complete Page object with content and metadata
|
|
1390
|
+
*
|
|
1391
|
+
* @example Basic pagination
|
|
1392
|
+
* ```ts
|
|
1393
|
+
* const users = await fetchUsers({ limit: 11, offset: 0 }); // Fetch limit + 1
|
|
1394
|
+
* const page = createPagination(users, 10, 0);
|
|
1395
|
+
* // page.content has max 10 items
|
|
1396
|
+
* // page.page.isLast tells us if there are more pages
|
|
1397
|
+
* ```
|
|
1398
|
+
*
|
|
1399
|
+
* @example With sorting
|
|
1400
|
+
* ```ts
|
|
1401
|
+
* const page = createPagination(
|
|
1402
|
+
* entities,
|
|
1403
|
+
* 10,
|
|
1404
|
+
* 0,
|
|
1405
|
+
* [{ column: "name", direction: "asc" }]
|
|
1406
|
+
* );
|
|
1407
|
+
* ```
|
|
1408
|
+
*
|
|
1409
|
+
* @example In a custom service
|
|
1410
|
+
* ```ts
|
|
1411
|
+
* class MyService {
|
|
1412
|
+
* async listItems(page: number, size: number) {
|
|
1413
|
+
* const items = await this.fetchItems({ limit: size + 1, offset: page * size });
|
|
1414
|
+
* return createPagination(items, size, page * size);
|
|
1415
|
+
* }
|
|
1416
|
+
* }
|
|
1417
|
+
* ```
|
|
1418
|
+
*/
|
|
1419
|
+
function createPagination(entities, limit = 10, offset = 0, sort) {
|
|
1420
|
+
const content = entities.slice(0, limit);
|
|
1421
|
+
const hasNext = entities.length === limit + 1;
|
|
1422
|
+
const pageNumber = Math.floor(offset / limit);
|
|
1423
|
+
return {
|
|
1424
|
+
content,
|
|
1425
|
+
page: {
|
|
1426
|
+
number: pageNumber,
|
|
1427
|
+
size: limit,
|
|
1428
|
+
offset,
|
|
1429
|
+
numberOfElements: content.length,
|
|
1430
|
+
isEmpty: content.length === 0,
|
|
1431
|
+
isFirst: pageNumber === 0,
|
|
1432
|
+
isLast: !hasNext,
|
|
1433
|
+
...sort && sort.length > 0 ? { sort: {
|
|
1434
|
+
sorted: true,
|
|
1435
|
+
fields: sort.map((s) => ({
|
|
1436
|
+
field: s.column,
|
|
1437
|
+
direction: s.direction
|
|
1438
|
+
}))
|
|
1439
|
+
} } : {}
|
|
1440
|
+
}
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1364
1444
|
//#endregion
|
|
1365
1445
|
//#region src/core/helpers/FileLike.ts
|
|
1366
1446
|
const isTypeFile = (value) => {
|
|
@@ -1816,7 +1896,7 @@ var TypeProvider = class TypeProvider {
|
|
|
1816
1896
|
const t = new TypeProvider();
|
|
1817
1897
|
|
|
1818
1898
|
//#endregion
|
|
1819
|
-
//#region src/core/
|
|
1899
|
+
//#region src/core/primitives/$env.ts
|
|
1820
1900
|
/**
|
|
1821
1901
|
* Get typed values from environment variables.
|
|
1822
1902
|
*
|
|
@@ -1850,7 +1930,7 @@ const $env = (type) => {
|
|
|
1850
1930
|
};
|
|
1851
1931
|
|
|
1852
1932
|
//#endregion
|
|
1853
|
-
//#region src/core/
|
|
1933
|
+
//#region src/core/primitives/$hook.ts
|
|
1854
1934
|
/**
|
|
1855
1935
|
* Registers a new hook.
|
|
1856
1936
|
*
|
|
@@ -1890,8 +1970,8 @@ const $env = (type) => {
|
|
|
1890
1970
|
* ```
|
|
1891
1971
|
*
|
|
1892
1972
|
*/
|
|
1893
|
-
const $hook = (options) =>
|
|
1894
|
-
var
|
|
1973
|
+
const $hook = (options) => createPrimitive(HookPrimitive, options);
|
|
1974
|
+
var HookPrimitive = class extends Primitive {
|
|
1895
1975
|
called = 0;
|
|
1896
1976
|
onInit() {
|
|
1897
1977
|
this.alepha.events.on(this.options.on, {
|
|
@@ -1904,10 +1984,10 @@ var HookDescriptor = class extends Descriptor {
|
|
|
1904
1984
|
});
|
|
1905
1985
|
}
|
|
1906
1986
|
};
|
|
1907
|
-
$hook[KIND] =
|
|
1987
|
+
$hook[KIND] = HookPrimitive;
|
|
1908
1988
|
|
|
1909
1989
|
//#endregion
|
|
1910
|
-
//#region src/core/
|
|
1990
|
+
//#region src/core/primitives/$use.ts
|
|
1911
1991
|
/**
|
|
1912
1992
|
* Subscribes to an atom's state and returns its current value for use in components.
|
|
1913
1993
|
*
|
|
@@ -1932,94 +2012,14 @@ $hook[KIND] = HookDescriptor;
|
|
|
1932
2012
|
*/
|
|
1933
2013
|
const $use = (atom) => {
|
|
1934
2014
|
const { alepha } = $context();
|
|
1935
|
-
alepha.
|
|
1936
|
-
const init = alepha.
|
|
2015
|
+
alepha.store.register(atom);
|
|
2016
|
+
const init = alepha.store.get(atom.key);
|
|
1937
2017
|
return {
|
|
1938
2018
|
[OPTIONS]: { getter: atom.key },
|
|
1939
2019
|
...init
|
|
1940
2020
|
};
|
|
1941
2021
|
};
|
|
1942
2022
|
|
|
1943
|
-
//#endregion
|
|
1944
|
-
//#region src/core/errors/AppNotStartedError.ts
|
|
1945
|
-
var AppNotStartedError = class extends AlephaError {
|
|
1946
|
-
name = "AppNotStartedError";
|
|
1947
|
-
constructor() {
|
|
1948
|
-
super("App not started. Please start the app before.");
|
|
1949
|
-
}
|
|
1950
|
-
};
|
|
1951
|
-
|
|
1952
|
-
//#endregion
|
|
1953
|
-
//#region src/core/helpers/createPagination.ts
|
|
1954
|
-
/**
|
|
1955
|
-
* Create a pagination object from an array of entities.
|
|
1956
|
-
*
|
|
1957
|
-
* This is a pure function that works with any data source (databases, APIs, caches, arrays, etc.).
|
|
1958
|
-
* It handles the core pagination logic including:
|
|
1959
|
-
* - Slicing the content to the requested page size
|
|
1960
|
-
* - Calculating pagination metadata (offset, page number, etc.)
|
|
1961
|
-
* - Determining navigation state (isFirst, isLast)
|
|
1962
|
-
* - Including sort metadata when provided
|
|
1963
|
-
*
|
|
1964
|
-
* @param entities - The entities to paginate (should include one extra item to detect if there's a next page)
|
|
1965
|
-
* @param limit - The limit of the pagination (page size)
|
|
1966
|
-
* @param offset - The offset of the pagination (starting position)
|
|
1967
|
-
* @param sort - Optional sort metadata to include in response
|
|
1968
|
-
* @returns A complete Page object with content and metadata
|
|
1969
|
-
*
|
|
1970
|
-
* @example Basic pagination
|
|
1971
|
-
* ```ts
|
|
1972
|
-
* const users = await fetchUsers({ limit: 11, offset: 0 }); // Fetch limit + 1
|
|
1973
|
-
* const page = createPagination(users, 10, 0);
|
|
1974
|
-
* // page.content has max 10 items
|
|
1975
|
-
* // page.page.isLast tells us if there are more pages
|
|
1976
|
-
* ```
|
|
1977
|
-
*
|
|
1978
|
-
* @example With sorting
|
|
1979
|
-
* ```ts
|
|
1980
|
-
* const page = createPagination(
|
|
1981
|
-
* entities,
|
|
1982
|
-
* 10,
|
|
1983
|
-
* 0,
|
|
1984
|
-
* [{ column: "name", direction: "asc" }]
|
|
1985
|
-
* );
|
|
1986
|
-
* ```
|
|
1987
|
-
*
|
|
1988
|
-
* @example In a custom service
|
|
1989
|
-
* ```ts
|
|
1990
|
-
* class MyService {
|
|
1991
|
-
* async listItems(page: number, size: number) {
|
|
1992
|
-
* const items = await this.fetchItems({ limit: size + 1, offset: page * size });
|
|
1993
|
-
* return createPagination(items, size, page * size);
|
|
1994
|
-
* }
|
|
1995
|
-
* }
|
|
1996
|
-
* ```
|
|
1997
|
-
*/
|
|
1998
|
-
function createPagination(entities, limit = 10, offset = 0, sort) {
|
|
1999
|
-
const content = entities.slice(0, limit);
|
|
2000
|
-
const hasNext = entities.length === limit + 1;
|
|
2001
|
-
const pageNumber = Math.floor(offset / limit);
|
|
2002
|
-
return {
|
|
2003
|
-
content,
|
|
2004
|
-
page: {
|
|
2005
|
-
number: pageNumber,
|
|
2006
|
-
size: limit,
|
|
2007
|
-
offset,
|
|
2008
|
-
numberOfElements: content.length,
|
|
2009
|
-
isEmpty: content.length === 0,
|
|
2010
|
-
isFirst: pageNumber === 0,
|
|
2011
|
-
isLast: !hasNext,
|
|
2012
|
-
...sort && sort.length > 0 ? { sort: {
|
|
2013
|
-
sorted: true,
|
|
2014
|
-
fields: sort.map((s) => ({
|
|
2015
|
-
field: s.column,
|
|
2016
|
-
direction: s.direction
|
|
2017
|
-
}))
|
|
2018
|
-
} } : {}
|
|
2019
|
-
}
|
|
2020
|
-
};
|
|
2021
|
-
}
|
|
2022
|
-
|
|
2023
2023
|
//#endregion
|
|
2024
2024
|
//#region src/core/schemas/pageQuerySchema.ts
|
|
2025
2025
|
const pageQuerySchema = t.object({
|
|
@@ -2110,5 +2110,5 @@ const run = (entry, opts) => {
|
|
|
2110
2110
|
};
|
|
2111
2111
|
|
|
2112
2112
|
//#endregion
|
|
2113
|
-
export { $atom, $context, $env, $hook, $inject, $module, $use, Alepha, AlephaError, AlsProvider, AppNotStartedError, Atom, CircularDependencyError, CodecManager, ContainerLockedError,
|
|
2113
|
+
export { $atom, $context, $env, $hook, $inject, $module, $use, Alepha, AlephaError, AlsProvider, AppNotStartedError, Atom, CircularDependencyError, CodecManager, ContainerLockedError, EventManager, HookPrimitive, InjectPrimitive, JsonSchemaCodec, KIND, Module, OPTIONS, Primitive, SchemaCodec, StateManager, TooLateSubstitutionError, TypeBox, TypeBoxError, TypeBoxFormat, TypeBoxValue, TypeGuard, TypeProvider, createPagination, createPrimitive, isClass, isDate, isDateTime, isDuration, isEmail, isFileLike, isTime, isTypeFile, isURL, isUUID, pageMetadataSchema, pageQuerySchema, pageSchema, run, t };
|
|
2114
2114
|
//# sourceMappingURL=index.browser.js.map
|