alepha 0.7.3 → 0.7.4

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.
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ var cacheRedis = require('@alepha/cache-redis');
4
+
5
+
6
+
7
+ Object.keys(cacheRedis).forEach(function (k) {
8
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
9
+ enumerable: true,
10
+ get: function () { return cacheRedis[k]; }
11
+ });
12
+ });
@@ -0,0 +1,105 @@
1
+ import * as _alepha_core from '@alepha/core';
2
+ import { Static, Alepha, Module } from '@alepha/core';
3
+ import { CacheProvider } from '@alepha/cache';
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
+ 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);
16
+ type StringContentEncodingOption = '7bit' | '8bit' | 'binary' | 'quoted-printable' | 'base64' | ({} & string);
17
+ interface StringOptions extends SchemaOptions {
18
+ /** The maximum string length */
19
+ maxLength?: number;
20
+ /** The minimum string length */
21
+ minLength?: number;
22
+ /** A regular expression pattern this string should match */
23
+ pattern?: string;
24
+ /** A format this string should match */
25
+ format?: StringFormatOption;
26
+ /** The content encoding for this string */
27
+ contentEncoding?: StringContentEncodingOption;
28
+ /** The content media type for this string */
29
+ contentMediaType?: string;
30
+ }
31
+ interface TString extends TSchema, StringOptions {
32
+ [Kind]: 'String';
33
+ static: string;
34
+ type: 'string';
35
+ }
36
+
37
+ type TOptional<T extends TSchema> = T & {
38
+ [OptionalKind]: 'Optional';
39
+ };
40
+
41
+ interface SchemaOptions {
42
+ $schema?: string;
43
+ /** Id for this schema */
44
+ $id?: string;
45
+ /** Title of this schema */
46
+ title?: string;
47
+ /** Description of this schema */
48
+ description?: string;
49
+ /** Default value for this schema */
50
+ default?: any;
51
+ /** Example values matching this schema */
52
+ examples?: any;
53
+ /** Optional annotation for readOnly */
54
+ readOnly?: boolean;
55
+ /** Optional annotation for writeOnly */
56
+ writeOnly?: boolean;
57
+ [prop: string]: any;
58
+ }
59
+ interface TKind {
60
+ [Kind]: string;
61
+ }
62
+ interface TSchema extends TKind, SchemaOptions {
63
+ [ReadonlyKind]?: string;
64
+ [OptionalKind]?: string;
65
+ [Hint]?: string;
66
+ params: unknown[];
67
+ static: unknown;
68
+ }
69
+
70
+ declare const envSchema: _alepha_core.TObject<{
71
+ REDIS_CACHE_PREFIX: TOptional<TString>;
72
+ }>;
73
+ declare module "alepha" {
74
+ interface Env extends Partial<Static<typeof envSchema>> {
75
+ }
76
+ }
77
+ declare class RedisCacheProvider implements CacheProvider {
78
+ protected readonly log: _alepha_core.Logger;
79
+ protected readonly redisProvider: RedisProvider;
80
+ protected readonly env: {
81
+ REDIS_CACHE_PREFIX?: string | undefined;
82
+ };
83
+ protected readonly alepha: Alepha;
84
+ get(name: string, key: string): Promise<Buffer | undefined>;
85
+ set(name: string, key: string, value: Buffer | string, ttl?: number): Promise<Buffer>;
86
+ del(name: string, ...keys: string[]): Promise<void>;
87
+ has(name: string, key: string): Promise<boolean>;
88
+ keys(name: string): Promise<string[]>;
89
+ protected prefix(...path: string[]): string;
90
+ }
91
+
92
+ /**
93
+ * Alepha Cache Redis Module
94
+ *
95
+ * Plugin for Alepha Cache that provides Redis caching capabilities.
96
+ *
97
+ * @see {@link RedisCacheProvider}
98
+ * @module alepha.cache.redis
99
+ */
100
+ declare class AlephaRedisCache implements Module {
101
+ readonly name = "alepha.cache.redis";
102
+ readonly $services: (alepha: Alepha) => Alepha;
103
+ }
104
+
105
+ export { AlephaRedisCache, RedisCacheProvider };
package/cache/redis.js ADDED
@@ -0,0 +1 @@
1
+ export * from '@alepha/cache-redis';
package/cache.d.ts CHANGED
@@ -1,7 +1,119 @@
1
1
  import * as _alepha_core from '@alepha/core';
2
- import { KIND, OPTIONS, Static, Alepha } from '@alepha/core';
2
+ import { KIND, OPTIONS, Static, Alepha, Module } from '@alepha/core';
3
3
  import { DurationLike, DateTimeProvider, Timeout } from '@alepha/datetime';
4
- import { RedisProvider, RedisClient } from '@alepha/redis';
4
+
5
+ /**
6
+ * Cache provider interface.
7
+ *
8
+ * All methods are asynchronous and return promises.
9
+ * Values are stored as Buffer.
10
+ */
11
+ declare abstract class CacheProvider {
12
+ /**
13
+ * Get the value of a key.
14
+ *
15
+ * @param name Cache name, used to group keys. Should be Redis-like "some:group:name" format.
16
+ * @param key The key of the value to get.
17
+ *
18
+ * @return The value of the key, or undefined if the key does not exist.
19
+ */
20
+ abstract get(name: string, key: string): Promise<Buffer | undefined>;
21
+ /**
22
+ * Set the string value of a key.
23
+ *
24
+ * @param name Cache name, used to group keys. Should be Redis-like "some:group:name" format.
25
+ * @param key The key of the value to set.
26
+ * @param value The value to set.
27
+ * @param ttl The time-to-live of the key, in milliseconds.
28
+ *
29
+ * @return The value of the key.
30
+ */
31
+ abstract set(name: string, key: string, value: Buffer, ttl?: number): Promise<Buffer>;
32
+ /**
33
+ * Remove the specified keys.
34
+ *
35
+ * @param name Cache name, used to group keys. Should be Redis-like "some:group:name" format.
36
+ * @param keys The keys to delete.
37
+ */
38
+ abstract del(name: string, ...keys: string[]): Promise<void>;
39
+ abstract has(name: string, key: string): Promise<boolean>;
40
+ abstract keys(name: string): Promise<string[]>;
41
+ }
42
+
43
+ declare const KEY = "CACHE";
44
+ /**
45
+ * Cache Descriptor
46
+ */
47
+ declare const $cache: {
48
+ <TReturn = string, TParameter extends any[] = any[]>(options?: CacheDescriptorOptions<TReturn, TParameter>): CacheDescriptor<TReturn, TParameter>;
49
+ [KIND]: string;
50
+ };
51
+ interface CacheDescriptorOptions<TReturn, TParameter extends any[] = any[]> {
52
+ /**
53
+ * The cache name. This is useful for invalidating multiple caches at once.
54
+ *
55
+ * Store key as `cache:$name:$key`.
56
+ *
57
+ * @default ClassName:methodName
58
+ */
59
+ name?: string;
60
+ /**
61
+ * Function which returns cached data.
62
+ * @param args Arguments for handler.
63
+ */
64
+ handler?: (...args: TParameter) => TReturn;
65
+ /**
66
+ * The key generator for the cache.
67
+ * If not provided, the arguments will be json.stringify().
68
+ */
69
+ key?: (...args: TParameter) => string;
70
+ /**
71
+ * The store provider for the cache.
72
+ * If not provided, the default store provider will be used.
73
+ */
74
+ provider?: CacheProvider | (() => CacheProvider) | "memory";
75
+ /**
76
+ * The time-to-live for the cache in seconds.
77
+ * Set 0 to skip expiration.
78
+ *
79
+ * @default 300 (5 minutes).
80
+ */
81
+ ttl?: DurationLike;
82
+ /**
83
+ * If the cache is disabled.
84
+ */
85
+ disabled?: boolean;
86
+ }
87
+ interface CacheDescriptor<TReturn = any, TParameter extends any[] = any[]> {
88
+ [KIND]: typeof KEY;
89
+ [OPTIONS]: CacheDescriptorOptions<TReturn, TParameter>;
90
+ /**
91
+ * Cache handler.
92
+ */
93
+ (...args: TParameter): Promise<TReturn>;
94
+ /**
95
+ * Cache key generator.
96
+ */
97
+ key: (...args: TParameter) => string;
98
+ /**
99
+ * Invalidate cache by keys.
100
+ */
101
+ invalidate: (...keys: string[]) => Promise<void>;
102
+ /**
103
+ * Set cache with key, value and ttl.
104
+ *
105
+ * @param key
106
+ * @param value
107
+ * @param ttl
108
+ */
109
+ set: (key: string, value: TReturn, ttl?: DurationLike) => Promise<void>;
110
+ /**
111
+ * Get cache by key.
112
+ *
113
+ * @param key
114
+ */
115
+ get: (key: string) => Promise<TReturn | undefined>;
116
+ }
5
117
 
6
118
  /** Symbol key applied to readonly types */
7
119
  declare const ReadonlyKind: unique symbol;
@@ -12,11 +124,6 @@ declare const Hint: unique symbol;
12
124
  /** Symbol key applied to types */
13
125
  declare const Kind: unique symbol;
14
126
 
15
- interface TUnsafe<T> extends TSchema {
16
- [Kind]: string;
17
- static: T;
18
- }
19
-
20
127
  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);
21
128
  type StringContentEncodingOption = '7bit' | '8bit' | 'binary' | 'quoted-printable' | 'base64' | ({} & string);
22
129
  interface StringOptions extends SchemaOptions {
@@ -91,169 +198,35 @@ interface TSchema extends TKind, SchemaOptions {
91
198
  static: unknown;
92
199
  }
93
200
 
94
- interface CacheProvider {
95
- /**
96
- * Get the value of a key.
97
- *
98
- * @param group The group of the value to get.
99
- * @param key The key of the value to get.
100
- */
101
- get(group: string, key: string): Promise<string | undefined>;
102
- /**
103
- * Set the string value of a key.
104
- *
105
- * @param group The group of the value to get.
106
- * @param key The key of the value to set.
107
- * @param value The value to set.
108
- * @param ttl The time-to-live of the key, in milliseconds.
109
- */
110
- set(group: string, key: string, value: string, ttl?: number): Promise<string>;
111
- /**
112
- * Remove the specified keys.
113
- *
114
- * @param group The group of the value to get.
115
- * @param keys The keys to delete.
116
- */
117
- del(group: string, ...keys: string[]): Promise<void>;
118
- }
119
-
120
- declare class DefaultCacheProvider implements CacheProvider {
121
- constructor();
122
- get(): Promise<string | undefined>;
123
- set(): Promise<string>;
124
- del(): Promise<void>;
125
- }
126
-
127
- declare const KEY = "CACHE";
128
- interface CacheDescriptorOptions<TReturn, TParameter extends any[] = any[]> {
129
- /**
130
- * Function which returns cached data.
131
- * @param args Arguments for handler.
132
- */
133
- handler?: (...args: TParameter) => TReturn;
134
- /**
135
- * The key generator for the cache.
136
- * If not provided, the arguments will be json.stringify().
137
- */
138
- key?: (...args: TParameter) => string;
139
- /**
140
- * The store provider for the cache.
141
- * If not provided, the default store provider will be used.
142
- */
143
- provider?: (() => DefaultCacheProvider) | "memory";
144
- /**
145
- * The cache group. This is useful for invalidating multiple caches at once.
146
- * Key is used as the group if not provided.
147
- *
148
- * Store key as `cache:$group:$key`.
149
- *
150
- * @default ClassName:methodName
151
- */
152
- group?: string;
153
- /**
154
- * The time-to-live for the cache in seconds.
155
- * Set 0 to skip expiration.
156
- *
157
- * @default 300 (5 minutes).
158
- */
159
- ttl?: DurationLike;
160
- /**
161
- * If the cache is disabled.
162
- */
163
- disabled?: boolean;
164
- }
165
- interface CacheDescriptor<TReturn = any, TParameter extends any[] = any[]> {
166
- [KIND]: typeof KEY;
167
- [OPTIONS]: CacheDescriptorOptions<TReturn, TParameter>;
168
- /**
169
- * Cache handler.
170
- */
171
- (...args: TParameter): Promise<TReturn>;
172
- /**
173
- * Cache key generator.
174
- */
175
- key: (...args: TParameter) => string;
176
- /**
177
- * Invalidate cache by keys.
178
- */
179
- invalidate: (...keys: string[]) => Promise<void>;
180
- /**
181
- * Set cache with key, value and ttl.
182
- *
183
- * @param key
184
- * @param value
185
- * @param ttl
186
- */
187
- set: (key: string, value: TReturn, ttl?: DurationLike) => Promise<void>;
188
- /**
189
- * Get cache by key.
190
- *
191
- * @param key
192
- */
193
- get: (key: string) => Promise<TReturn | undefined>;
194
- }
195
- /**
196
- * Cache Descriptor
197
- *
198
- * @param options
199
- */
200
- declare const $cache: {
201
- <TReturn = string, TParameter extends any[] = any[]>(options?: CacheDescriptorOptions<TReturn, TParameter>): CacheDescriptor<TReturn, TParameter>;
202
- [KIND]: string;
201
+ type CacheName = string;
202
+ type CacheKey = string;
203
+ type CacheValue = {
204
+ data?: Buffer;
205
+ timeout?: Timeout;
203
206
  };
204
-
205
- /**
206
- * A simple in-memory store provider.
207
- */
208
207
  declare class MemoryCacheProvider implements CacheProvider {
209
208
  protected readonly dateTimeProvider: DateTimeProvider;
210
209
  protected readonly log: _alepha_core.Logger;
211
- /**
212
- * The in-memory store.
213
- */
214
- protected store: Record<string, Record<string, string>>;
215
- /**
216
- * Timeouts used to expire keys.
217
- */
218
- protected storeTimeout: Record<string, Record<string, Timeout>>;
219
- /**
220
- * Get the value of a key.
221
- *
222
- * @param group The group of the value to get.
223
- * @param key The key of the value to get.
224
- */
225
- get(group: string, key: string): Promise<string | undefined>;
226
- /**
227
- * Set the string value of a key.
228
- *
229
- * @param group The group of the value to get.
230
- * @param key The key of the value to set.
231
- * @param value The value to set.
232
- * @param ttl The time-to-live of the key, in milliseconds.
233
- */
234
- set(group: string, key: string, value: string, ttl?: number): Promise<string>;
235
- /**
236
- * Remove the specified keys.
237
- *
238
- * @param group The group of the value to get.
239
- * @param keys The keys to delete.
240
- */
241
- del(group: string, ...keys: string[]): Promise<void>;
242
- private ttl;
210
+ protected store: Record<CacheName, Record<CacheKey, CacheValue>>;
211
+ get(name: string, key: string): Promise<Buffer | undefined>;
212
+ set(name: string, key: string, value: Buffer, ttl?: number): Promise<Buffer>;
213
+ del(name: string, ...keys: string[]): Promise<void>;
214
+ has(name: string, key: string): Promise<boolean>;
215
+ keys(name: string): Promise<string[]>;
243
216
  }
244
217
 
245
- declare const envSchema$2: _alepha_core.TObject<{
218
+ declare const envSchema: _alepha_core.TObject<{
246
219
  CACHE_DEFAULT_TTL: TNumber;
247
220
  CACHE_PREFIX: TOptional<TString>;
248
221
  CACHE_ENABLED: TBoolean;
249
222
  }>;
250
223
  declare module "alepha" {
251
- interface Env extends Partial<Static<typeof envSchema$2>> {
224
+ interface Env extends Partial<Static<typeof envSchema>> {
252
225
  }
253
226
  }
254
227
  declare class CacheDescriptorProvider {
255
228
  protected readonly alepha: Alepha;
256
- protected readonly cacheProvider: DefaultCacheProvider;
229
+ protected readonly cacheProvider: CacheProvider;
257
230
  protected readonly memoryCacheProvider: MemoryCacheProvider;
258
231
  protected readonly dateTimeProvider: DateTimeProvider;
259
232
  protected readonly env: {
@@ -300,72 +273,26 @@ declare class CacheDescriptorProvider {
300
273
  * @param ttl Override cache.ttl option.
301
274
  */
302
275
  set<TReturn>(cache: Cache<TReturn>, key: string, value: TReturn, ttl?: DurationLike): Promise<void>;
303
- protected serialize<TReturn>(value: TReturn): string;
304
- protected deserialize<TReturn>(value: string): TReturn;
276
+ protected serialize<TReturn>(value: TReturn): Buffer;
277
+ protected deserialize<TReturn>(buffer: Buffer): TReturn;
305
278
  }
306
279
  interface Cache<TReturn = any, TParameter extends any[] = any[]> {
307
- group: string;
280
+ name: string;
308
281
  options: CacheDescriptorOptions<TReturn, TParameter>;
309
282
  }
310
283
 
311
- declare const envSchema$1: _alepha_core.TObject<{
312
- REDIS_CACHE_PREFIX: TOptional<TString>;
313
- }>;
314
- declare module "alepha/core" {
315
- interface Env extends Partial<Static<typeof envSchema$1>> {
316
- }
317
- }
318
- declare class RedisCacheProvider implements CacheProvider {
319
- protected readonly log: _alepha_core.Logger;
320
- protected readonly redisProvider: RedisProvider;
321
- protected readonly env: {
322
- REDIS_CACHE_PREFIX?: string | undefined;
323
- };
324
- get publisher(): RedisClient;
325
- /**
326
- * Get the value of a key.
327
- *
328
- * @param group - The group of the value to get.
329
- * @param key - The key of the value to get.
330
- */
331
- get(group: string, key: string): Promise<string | undefined>;
332
- /**
333
- * Set the string value of a key.
334
- *
335
- * @param group - The group of the value to get.
336
- * @param key - The key of the value to set.
337
- * @param value - The value to set.
338
- * @param ttl - The time-to-live of the key, in milliseconds.
339
- */
340
- set(group: string, key: string, value: string, ttl?: number): Promise<string>;
341
- /**
342
- * Remove the specified keys.
343
- *
344
- * @param group - The group of the value to get.
345
- * @param keys - The keys to delete.
346
- */
347
- del(group: string, ...keys: string[]): Promise<void>;
348
- /**
349
- * Prefix the cache key.
350
- *
351
- * @param path - The path to prefix.
352
- */
353
- protected prefix(...path: string[]): string;
354
- }
355
-
356
- declare const envSchema: _alepha_core.TObject<{
357
- CACHE_PROVIDER: TUnsafe<"memory" | "redis">;
358
- }>;
359
- declare module "@alepha/core" {
360
- interface Env extends Partial<Static<typeof envSchema>> {
361
- }
362
- }
363
- declare class CacheModule {
364
- protected readonly alepha: Alepha;
365
- protected readonly env: {
366
- CACHE_PROVIDER: "memory" | "redis";
367
- };
368
- constructor();
284
+ /**
285
+ * Alepha Cache Module
286
+ *
287
+ * This module provides a caching mechanism for Alepha applications.
288
+ *
289
+ * @see {@link $cache}
290
+ * @see {@link CacheProvider}
291
+ * @module alepha.cache
292
+ */
293
+ declare class AlephaCache implements Module {
294
+ readonly name = "alepha.cache";
295
+ readonly $services: (alepha: Alepha) => Alepha;
369
296
  }
370
297
 
371
- export { $cache, type Cache, type CacheDescriptor, type CacheDescriptorOptions, CacheDescriptorProvider, CacheModule, type CacheProvider, DefaultCacheProvider, MemoryCacheProvider, RedisCacheProvider };
298
+ export { $cache, AlephaCache, type Cache, type CacheDescriptor, type CacheDescriptorOptions, CacheDescriptorProvider, CacheProvider, MemoryCacheProvider };