alepha 0.6.7 → 0.6.9

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/queue.d.ts CHANGED
@@ -1 +1,305 @@
1
- export * from '@alepha/queue';
1
+ import * as _alepha_core from '@alepha/core';
2
+ import { TSchema as TSchema$1, KIND, OPTIONS, Static, Alepha } from '@alepha/core';
3
+ import { DateTimeProvider } from '@alepha/datetime';
4
+ import { RedisProvider } from '@alepha/redis';
5
+
6
+ /** Symbol key applied to readonly types */
7
+ declare const ReadonlyKind: unique symbol;
8
+ /** Symbol key applied to optional types */
9
+ declare const OptionalKind: unique symbol;
10
+ /** Symbol key applied to types */
11
+ declare const Hint: unique symbol;
12
+ /** Symbol key applied to types */
13
+ declare const Kind: unique symbol;
14
+
15
+ interface TUnsafe<T> extends TSchema {
16
+ [Kind]: string;
17
+ static: T;
18
+ }
19
+
20
+ interface NumberOptions extends SchemaOptions {
21
+ exclusiveMaximum?: number;
22
+ exclusiveMinimum?: number;
23
+ maximum?: number;
24
+ minimum?: number;
25
+ multipleOf?: number;
26
+ }
27
+ interface TNumber extends TSchema, NumberOptions {
28
+ [Kind]: 'Number';
29
+ static: number;
30
+ type: 'number';
31
+ }
32
+
33
+ interface SchemaOptions {
34
+ $schema?: string;
35
+ /** Id for this schema */
36
+ $id?: string;
37
+ /** Title of this schema */
38
+ title?: string;
39
+ /** Description of this schema */
40
+ description?: string;
41
+ /** Default value for this schema */
42
+ default?: any;
43
+ /** Example values matching this schema */
44
+ examples?: any;
45
+ /** Optional annotation for readOnly */
46
+ readOnly?: boolean;
47
+ /** Optional annotation for writeOnly */
48
+ writeOnly?: boolean;
49
+ [prop: string]: any;
50
+ }
51
+ interface TKind {
52
+ [Kind]: string;
53
+ }
54
+ interface TSchema extends TKind, SchemaOptions {
55
+ [ReadonlyKind]?: string;
56
+ [OptionalKind]?: string;
57
+ [Hint]?: string;
58
+ params: unknown[];
59
+ static: unknown;
60
+ }
61
+
62
+ declare class QueueProvider {
63
+ /**
64
+ * Push a message to the queue.
65
+ *
66
+ * @param queue - The queue name.
67
+ * @param message - The message to push.
68
+ */
69
+ push(queue: string, message: string): Promise<void>;
70
+ /**
71
+ * Pop a message from the queue.
72
+ *
73
+ * @param queue - The queue name.
74
+ * @returns The message popped.
75
+ */
76
+ pop(queue: string): Promise<string | undefined>;
77
+ }
78
+
79
+ declare const KEY$1 = "QUEUE";
80
+ interface QueueMessageSchema {
81
+ headers?: TSchema$1;
82
+ payload: TSchema$1;
83
+ }
84
+ interface QueueDescriptorOptions<T extends QueueMessageSchema> {
85
+ name?: string;
86
+ description?: string;
87
+ provider?: "memory" | (() => QueueProvider);
88
+ schema: T;
89
+ handler?: (message: {
90
+ payload: Static<T["payload"]>;
91
+ }) => Promise<void>;
92
+ }
93
+ interface QueueDescriptor<T extends QueueMessageSchema = QueueMessageSchema> {
94
+ [KIND]: typeof KEY$1;
95
+ [OPTIONS]: QueueDescriptorOptions<T>;
96
+ name(): string;
97
+ provider(): QueueProvider;
98
+ push(...payload: Array<Static<T["payload"]>>): Promise<void>;
99
+ }
100
+ /**
101
+ * Queue descriptor.
102
+ *
103
+ * @param options - The queue options.
104
+ * @returns The descriptor value.
105
+ */
106
+ declare const $queue: {
107
+ <T extends QueueMessageSchema>(options: QueueDescriptorOptions<T>): QueueDescriptor<T>;
108
+ [KIND]: string;
109
+ };
110
+
111
+ declare const KEY = "CONSUMER";
112
+ interface ConsumerDescriptorOptions<T extends QueueMessageSchema = QueueMessageSchema> {
113
+ queue: QueueDescriptor<T>;
114
+ handler: (message: {
115
+ payload: Static<T["payload"]>;
116
+ }) => Promise<void>;
117
+ }
118
+ interface ConsumerDescriptor<T extends QueueMessageSchema = QueueMessageSchema> {
119
+ [KIND]: typeof KEY;
120
+ [OPTIONS]: ConsumerDescriptorOptions<T>;
121
+ queue(): QueueDescriptor<T>;
122
+ stop(): Promise<void>;
123
+ }
124
+ /**
125
+ * Consumer descriptor.
126
+ *
127
+ * @param options - The consumer options.
128
+ * @returns The descriptor value.
129
+ */
130
+ declare const $consumer: {
131
+ <T extends QueueMessageSchema>(options: ConsumerDescriptorOptions<T>): ConsumerDescriptor<T>;
132
+ [KIND]: string;
133
+ };
134
+
135
+ declare class MemoryQueueProvider implements QueueProvider {
136
+ /**
137
+ * The in-memory queue list.
138
+ */
139
+ protected queueList: Record<string, string[]>;
140
+ protected readonly log: _alepha_core.Logger;
141
+ /**
142
+ * Push a message to a queue.
143
+ * The message is added to the end of the queue.
144
+ * The queue is created if it does not exist.
145
+ *
146
+ * @param queue - The queue to push to.
147
+ * @param messages The messages to push.
148
+ */
149
+ push(queue: string, ...messages: string[]): Promise<void>;
150
+ /**
151
+ * Pop a message from a queue.
152
+ * The message is removed from the queue.
153
+ * If the queue is empty, this method will return undefined.
154
+ *
155
+ * @param queue - The queue to pop from.
156
+ * @returns The message that was popped from the queue.
157
+ */
158
+ pop(queue: string): Promise<string | undefined>;
159
+ }
160
+
161
+ declare const envSchema$1: _alepha_core.TObject<{
162
+ /**
163
+ * The interval in milliseconds to wait before checking for new messages.
164
+ */
165
+ QUEUE_WORKER_INTERVAL: TNumber;
166
+ /**
167
+ * The maximum interval in milliseconds to wait before checking for new messages.
168
+ */
169
+ QUEUE_WORKER_MAX_INTERVAL: TNumber;
170
+ /**
171
+ * The number of workers to run concurrently. Defaults to 1.
172
+ * Useful only if you are doing a lot of I/O.
173
+ */
174
+ QUEUE_WORKER_CONCURRENCY: TNumber;
175
+ }>;
176
+ declare module "alepha" {
177
+ interface Env extends Partial<Static<typeof envSchema$1>> {
178
+ }
179
+ }
180
+ interface QueueDescriptorProviderState {
181
+ queues: Array<QueueDescriptor>;
182
+ consumers: Array<ConsumerDescriptorOptions>;
183
+ workerPromises: Array<Promise<void>>;
184
+ isWorkersRunning: boolean;
185
+ abortController: AbortController;
186
+ workerIntervals: Record<number, number>;
187
+ }
188
+ declare class QueueDescriptorProvider {
189
+ protected readonly log: _alepha_core.Logger;
190
+ protected readonly env: {
191
+ QUEUE_WORKER_INTERVAL: number;
192
+ QUEUE_WORKER_MAX_INTERVAL: number;
193
+ QUEUE_WORKER_CONCURRENCY: number;
194
+ };
195
+ protected readonly alepha: Alepha;
196
+ protected readonly queueProvider: QueueProvider;
197
+ protected readonly memoryQueueProvider: MemoryQueueProvider;
198
+ protected readonly dateTimeProvider: DateTimeProvider;
199
+ protected readonly state: QueueDescriptorProviderState;
200
+ protected readonly configure: _alepha_core.HookDescriptor<"configure">;
201
+ protected readonly start: _alepha_core.HookDescriptor<"start">;
202
+ protected readonly stop: _alepha_core.HookDescriptor<"stop">;
203
+ getQueues(): QueueDescriptor<QueueMessageSchema>[];
204
+ getConsumers(): ConsumerDescriptorOptions<QueueMessageSchema>[];
205
+ /**
206
+ * Process the descriptors.
207
+ *
208
+ * @protected
209
+ */
210
+ protected processDescriptors(): void;
211
+ /**
212
+ * Process the queue descriptors.
213
+ *
214
+ * @protected
215
+ */
216
+ protected processQueueDescriptors(): void;
217
+ /**
218
+ * Process the consumer descriptors.
219
+ *
220
+ * @protected
221
+ */
222
+ protected processConsumerDescriptors(): void;
223
+ /**
224
+ * Start the workers.
225
+ *
226
+ * @param consumers
227
+ * @protected
228
+ */
229
+ protected startWorkers(consumers: Array<ConsumerDescriptorOptions>): void;
230
+ /**
231
+ * Wait for the next message.
232
+ *
233
+ * @param n
234
+ * @protected
235
+ */
236
+ protected waitForNextMessage(n: number): Promise<void>;
237
+ /**
238
+ * Get the next message.
239
+ *
240
+ * @param consumers
241
+ * @protected
242
+ */
243
+ protected getNextMessage(consumers: Array<ConsumerDescriptorOptions>): Promise<undefined | {
244
+ message: any;
245
+ consumer: ConsumerDescriptorOptions;
246
+ }>;
247
+ /**
248
+ * Process a message from a queue.
249
+ *
250
+ * @param response
251
+ * @protected
252
+ */
253
+ protected processMessage(response: {
254
+ message: any;
255
+ consumer: ConsumerDescriptorOptions;
256
+ }): Promise<void>;
257
+ /**
258
+ * Stop the workers
259
+ *
260
+ * @protected
261
+ */
262
+ protected stopWorkers(): Promise<void>;
263
+ /**
264
+ * Get the provider for the queue.
265
+ *
266
+ * @param options - The queue options.
267
+ * @protected
268
+ */
269
+ protected provider(options: QueueDescriptorOptions<any>): QueueProvider;
270
+ /**
271
+ * Push an item to the queue.
272
+ *
273
+ * @param queue
274
+ * @param payloads
275
+ * @protected
276
+ */
277
+ protected push<T extends QueueMessageSchema>(queue: QueueDescriptor<T>, ...payloads: Array<Static<T["payload"]>>): Promise<void>;
278
+ }
279
+
280
+ declare class RedisQueueProvider implements QueueProvider {
281
+ protected readonly env: {
282
+ REDIS_QUEUE_PREFIX: string;
283
+ };
284
+ protected readonly redisProvider: RedisProvider;
285
+ prefix(queue: string): string;
286
+ push(queue: string, message: string): Promise<void>;
287
+ pop(queue: string): Promise<string | undefined>;
288
+ }
289
+
290
+ declare const envSchema: _alepha_core.TObject<{
291
+ QUEUE_PROVIDER: TUnsafe<"memory" | "redis">;
292
+ }>;
293
+ declare module "alepha/core" {
294
+ interface Env extends Partial<Static<typeof envSchema>> {
295
+ }
296
+ }
297
+ declare class QueueModule {
298
+ protected readonly alepha: Alepha;
299
+ protected readonly env: {
300
+ QUEUE_PROVIDER: "memory" | "redis";
301
+ };
302
+ constructor();
303
+ }
304
+
305
+ export { $consumer, $queue, type ConsumerDescriptor, type ConsumerDescriptorOptions, MemoryQueueProvider, type QueueDescriptor, type QueueDescriptorOptions, QueueDescriptorProvider, type QueueDescriptorProviderState, type QueueMessageSchema, QueueModule, QueueProvider, RedisQueueProvider };
package/react/auth.d.ts CHANGED
@@ -1 +1,300 @@
1
- export * from '@alepha/react-auth';
1
+ import * as _alepha_core from '@alepha/core';
2
+ import { Alepha, KIND, OPTIONS } from '@alepha/core';
3
+ import { UserAccountToken } from '@alepha/security';
4
+ import * as _alepha_server from '@alepha/server';
5
+ import { HttpClient } from '@alepha/server';
6
+ import * as _alepha_server_cookies from '@alepha/server-cookies';
7
+ import { ServerCookiesProvider, Cookies } from '@alepha/server-cookies';
8
+ import { Configuration } from 'openid-client';
9
+
10
+ /** Symbol key applied to readonly types */
11
+ declare const ReadonlyKind: unique symbol;
12
+ /** Symbol key applied to optional types */
13
+ declare const OptionalKind: unique symbol;
14
+ /** Symbol key applied to types */
15
+ declare const Hint: unique symbol;
16
+ /** Symbol key applied to types */
17
+ declare const Kind: unique symbol;
18
+
19
+ type TReadonly<T extends TSchema> = T & {
20
+ [ReadonlyKind]: 'Readonly';
21
+ };
22
+
23
+ type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex' | ({} & string);
24
+ type StringContentEncodingOption = '7bit' | '8bit' | 'binary' | 'quoted-printable' | 'base64' | ({} & string);
25
+ interface StringOptions extends SchemaOptions {
26
+ /** The maximum string length */
27
+ maxLength?: number;
28
+ /** The minimum string length */
29
+ minLength?: number;
30
+ /** A regular expression pattern this string should match */
31
+ pattern?: string;
32
+ /** A format this string should match */
33
+ format?: StringFormatOption;
34
+ /** The content encoding for this string */
35
+ contentEncoding?: StringContentEncodingOption;
36
+ /** The content media type for this string */
37
+ contentMediaType?: string;
38
+ }
39
+ interface TString extends TSchema, StringOptions {
40
+ [Kind]: 'String';
41
+ static: string;
42
+ type: 'string';
43
+ }
44
+
45
+ interface NumberOptions extends SchemaOptions {
46
+ exclusiveMaximum?: number;
47
+ exclusiveMinimum?: number;
48
+ maximum?: number;
49
+ minimum?: number;
50
+ multipleOf?: number;
51
+ }
52
+ interface TNumber extends TSchema, NumberOptions {
53
+ [Kind]: 'Number';
54
+ static: number;
55
+ type: 'number';
56
+ }
57
+
58
+ type TOptional<T extends TSchema> = T & {
59
+ [OptionalKind]: 'Optional';
60
+ };
61
+
62
+ /** Creates a static type from a TypeBox type */
63
+ type Static<Type extends TSchema, Params extends unknown[] = [], Result = (Type & {
64
+ params: Params;
65
+ })['static']> = Result;
66
+
67
+ type ReadonlyOptionalPropertyKeys<T extends TProperties> = {
68
+ [K in keyof T]: T[K] extends TReadonly<TSchema> ? (T[K] extends TOptional<T[K]> ? K : never) : never;
69
+ }[keyof T];
70
+ type ReadonlyPropertyKeys<T extends TProperties> = {
71
+ [K in keyof T]: T[K] extends TReadonly<TSchema> ? (T[K] extends TOptional<T[K]> ? never : K) : never;
72
+ }[keyof T];
73
+ type OptionalPropertyKeys<T extends TProperties> = {
74
+ [K in keyof T]: T[K] extends TOptional<TSchema> ? (T[K] extends TReadonly<T[K]> ? never : K) : never;
75
+ }[keyof T];
76
+ type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
77
+ type ObjectStaticProperties<T extends TProperties, R extends Record<keyof any, unknown>> = Evaluate<(Readonly<Partial<Pick<R, ReadonlyOptionalPropertyKeys<T>>>> & Readonly<Pick<R, ReadonlyPropertyKeys<T>>> & Partial<Pick<R, OptionalPropertyKeys<T>>> & Required<Pick<R, RequiredPropertyKeys<T>>>)>;
78
+ type ObjectStatic<T extends TProperties, P extends unknown[]> = ObjectStaticProperties<T, {
79
+ [K in keyof T]: Static<T[K], P>;
80
+ }>;
81
+ type TPropertyKey = string | number;
82
+ type TProperties = Record<TPropertyKey, TSchema>;
83
+ type TAdditionalProperties = undefined | TSchema | boolean;
84
+ interface ObjectOptions extends SchemaOptions {
85
+ /** Additional property constraints for this object */
86
+ additionalProperties?: TAdditionalProperties;
87
+ /** The minimum number of properties allowed on this object */
88
+ minProperties?: number;
89
+ /** The maximum number of properties allowed on this object */
90
+ maxProperties?: number;
91
+ }
92
+ interface TObject<T extends TProperties = TProperties> extends TSchema, ObjectOptions {
93
+ [Kind]: 'Object';
94
+ static: ObjectStatic<T, this['params']>;
95
+ additionalProperties?: TAdditionalProperties;
96
+ type: 'object';
97
+ properties: T;
98
+ required?: string[];
99
+ }
100
+
101
+ type Evaluate<T> = T extends infer O ? {
102
+ [K in keyof O]: O[K];
103
+ } : never;
104
+
105
+ interface SchemaOptions {
106
+ $schema?: string;
107
+ /** Id for this schema */
108
+ $id?: string;
109
+ /** Title of this schema */
110
+ title?: string;
111
+ /** Description of this schema */
112
+ description?: string;
113
+ /** Default value for this schema */
114
+ default?: any;
115
+ /** Example values matching this schema */
116
+ examples?: any;
117
+ /** Optional annotation for readOnly */
118
+ readOnly?: boolean;
119
+ /** Optional annotation for writeOnly */
120
+ writeOnly?: boolean;
121
+ [prop: string]: any;
122
+ }
123
+ interface TKind {
124
+ [Kind]: string;
125
+ }
126
+ interface TSchema extends TKind, SchemaOptions {
127
+ [ReadonlyKind]?: string;
128
+ [OptionalKind]?: string;
129
+ [Hint]?: string;
130
+ params: unknown[];
131
+ static: unknown;
132
+ }
133
+
134
+ declare class ReactAuthProvider {
135
+ protected readonly log: _alepha_core.Logger;
136
+ protected readonly alepha: Alepha;
137
+ protected readonly serverCookiesProvider: ServerCookiesProvider;
138
+ protected authProviders: AuthProvider[];
139
+ protected readonly authorizationCode: _alepha_server_cookies.CookieDescriptor<TObject<{
140
+ codeVerifier: TOptional<TString>;
141
+ redirectUri: TOptional<TString>;
142
+ }>>;
143
+ protected readonly tokens: _alepha_server_cookies.CookieDescriptor<TObject<{
144
+ access_token: TOptional<TString>;
145
+ expires_in: TOptional<TNumber>;
146
+ refresh_token: TOptional<TString>;
147
+ id_token: TOptional<TString>;
148
+ scope: TOptional<TString>;
149
+ issued_at: TOptional<TNumber>;
150
+ }>>;
151
+ readonly user: _alepha_server_cookies.CookieDescriptor<TObject<{
152
+ id: TString;
153
+ name: TOptional<TString>;
154
+ email: TOptional<TString>;
155
+ }>>;
156
+ readonly onRender: _alepha_core.HookDescriptor<"react:server:render">;
157
+ protected readonly configure: _alepha_core.HookDescriptor<"configure">;
158
+ /**
159
+ * Configure Fastify to forward Session Access Token to Header Authorization.
160
+ */
161
+ protected readonly onRequest: _alepha_core.HookDescriptor<"server:onRequest">;
162
+ /**
163
+ *
164
+ * @param cookies
165
+ * @protected
166
+ */
167
+ protected refresh(cookies: Cookies): Promise<SessionTokens | undefined>;
168
+ /**
169
+ *
170
+ */
171
+ readonly login: _alepha_server.RouteDescriptor<{
172
+ query: TObject<{
173
+ redirect: TOptional<TString>;
174
+ provider: TOptional<TString>;
175
+ }>;
176
+ }>;
177
+ /**
178
+ *
179
+ */
180
+ readonly callback: _alepha_server.RouteDescriptor<{
181
+ query: TObject<{
182
+ provider: TOptional<TString>;
183
+ }>;
184
+ }>;
185
+ /**
186
+ *
187
+ * @param accessToken
188
+ * @protected
189
+ */
190
+ protected userFromAccessToken(accessToken: string): {
191
+ id: any;
192
+ name: any;
193
+ email: any;
194
+ } | undefined;
195
+ /**
196
+ *
197
+ */
198
+ readonly logout: _alepha_server.RouteDescriptor<{
199
+ query: TObject<{
200
+ redirect: TOptional<TString>;
201
+ provider: TOptional<TString>;
202
+ }>;
203
+ }>;
204
+ /**
205
+ *
206
+ * @param name
207
+ * @protected
208
+ */
209
+ protected provider(name?: string): Promise<{
210
+ client: Configuration;
211
+ name: string;
212
+ redirectUri: string;
213
+ }>;
214
+ /**
215
+ *
216
+ * @param file
217
+ * @protected
218
+ */
219
+ protected isViteFile(file: string): boolean;
220
+ }
221
+ interface SessionTokens {
222
+ access_token?: string;
223
+ expires_in?: number;
224
+ refresh_token?: string;
225
+ id_token?: string;
226
+ scope?: string;
227
+ issued_at?: number;
228
+ }
229
+ interface AuthProvider {
230
+ name: string;
231
+ redirectUri: string;
232
+ client: {
233
+ get: () => Promise<Configuration>;
234
+ };
235
+ }
236
+ interface ReactUser {
237
+ id: string;
238
+ name?: string;
239
+ email?: string;
240
+ }
241
+
242
+ declare const KEY = "AUTH";
243
+ interface AuthDescriptorOptions {
244
+ name?: string;
245
+ oidc?: {
246
+ issuer: string;
247
+ clientId: string;
248
+ clientSecret?: string;
249
+ redirectUri?: string;
250
+ };
251
+ }
252
+ interface AuthDescriptor {
253
+ [KIND]: typeof KEY;
254
+ [OPTIONS]: AuthDescriptorOptions;
255
+ jwks: () => string;
256
+ }
257
+ declare const $auth: {
258
+ (options: AuthDescriptorOptions): AuthDescriptor;
259
+ [KIND]: string;
260
+ };
261
+
262
+ declare const useAuth: () => AuthHook;
263
+ interface AuthHook {
264
+ user?: UserAccountToken;
265
+ logout: () => void;
266
+ login: (provider?: string) => void;
267
+ can: (something: {
268
+ can: () => boolean;
269
+ }) => boolean;
270
+ }
271
+
272
+ declare class ReactAuth {
273
+ protected readonly log: _alepha_core.Logger;
274
+ protected readonly alepha: Alepha;
275
+ protected readonly client: HttpClient;
276
+ readonly slugs: {
277
+ login: string;
278
+ logout: string;
279
+ };
280
+ readonly start: _alepha_core.HookDescriptor<"client:onError">;
281
+ readonly onRender: _alepha_core.HookDescriptor<"react:browser:render">;
282
+ protected getUserFromCookies(): UserAccountToken | undefined;
283
+ login(): void;
284
+ logout(): void;
285
+ }
286
+
287
+ declare module "alepha/react" {
288
+ interface PageReactContext {
289
+ user?: UserAccountToken;
290
+ }
291
+ interface ReactHydrationState {
292
+ user?: ReactUser;
293
+ }
294
+ }
295
+ declare class ReactAuthModule {
296
+ protected readonly alepha: Alepha;
297
+ constructor();
298
+ }
299
+
300
+ export { $auth, type AuthDescriptor, type AuthDescriptorOptions, type AuthHook, type AuthProvider, ReactAuth, ReactAuthModule, ReactAuthProvider, type ReactUser, type SessionTokens, useAuth };