@imqueue/rpc 3.4.4 → 3.5.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/CHANGELOG.md +12 -0
- package/index.d.ts +58 -0
- package/index.js +58 -0
- package/package.json +1 -1
- package/src/IMQCache.d.ts +20 -11
- package/src/IMQCache.js +20 -11
- package/src/IMQClient.d.ts +191 -38
- package/src/IMQClient.js +204 -57
- package/src/IMQDelay.d.ts +29 -6
- package/src/IMQDelay.js +20 -6
- package/src/IMQLock.d.ts +81 -35
- package/src/IMQLock.js +38 -35
- package/src/IMQMetadata.d.ts +9 -1
- package/src/IMQMetadata.js +1 -1
- package/src/IMQRPCDescription.d.ts +183 -9
- package/src/IMQRPCDescription.js +22 -0
- package/src/IMQRPCError.d.ts +91 -9
- package/src/IMQRPCError.js +23 -7
- package/src/IMQRPCOptions.d.ts +221 -18
- package/src/IMQRPCOptions.js +22 -6
- package/src/IMQRPCRequest.d.ts +39 -1
- package/src/IMQRPCResponse.d.ts +34 -0
- package/src/IMQRequestContext.d.ts +2 -5
- package/src/IMQRequestContext.js +2 -5
- package/src/IMQService.d.ts +144 -19
- package/src/IMQService.js +151 -33
- package/src/cache/ICache.d.ts +62 -16
- package/src/cache/RedisCache.d.ts +58 -17
- package/src/cache/RedisCache.js +43 -17
- package/src/decorators/cache.d.ts +46 -2
- package/src/decorators/cache.js +2 -2
- package/src/decorators/classType.d.ts +28 -13
- package/src/decorators/classType.js +28 -13
- package/src/decorators/expose.d.ts +59 -7
- package/src/decorators/expose.js +77 -30
- package/src/decorators/indexed.d.ts +27 -8
- package/src/decorators/indexed.js +27 -8
- package/src/decorators/lock.d.ts +20 -2
- package/src/decorators/lock.js +2 -2
- package/src/decorators/logged.d.ts +33 -2
- package/src/decorators/logged.js +2 -2
- package/src/decorators/property.d.ts +97 -4
- package/src/decorators/property.js +84 -6
- package/src/decorators/remote.d.ts +1 -1
- package/src/decorators/remote.js +1 -1
- package/src/helpers/fs.d.ts +5 -7
- package/src/helpers/fs.js +8 -10
- package/src/helpers/os-uuid.d.ts +0 -2
- package/src/helpers/os-uuid.js +2 -10
- package/src/helpers/pid.d.ts +7 -7
- package/src/helpers/pid.js +10 -11
- package/src/helpers/signature.d.ts +4 -4
- package/src/helpers/signature.js +7 -7
package/src/decorators/expose.js
CHANGED
|
@@ -51,8 +51,7 @@ const OWN_PATH = (() => {
|
|
|
51
51
|
/**
|
|
52
52
|
* Lookup and returns a list of argument names for a given function
|
|
53
53
|
*
|
|
54
|
-
* @param
|
|
55
|
-
* @return {string[]}
|
|
54
|
+
* @param fn -
|
|
56
55
|
*/
|
|
57
56
|
function argumentNames(fn) {
|
|
58
57
|
let src = fn.toString();
|
|
@@ -68,8 +67,8 @@ function argumentNames(fn) {
|
|
|
68
67
|
* and a later `}` in the description cannot extend the captured type. The type,
|
|
69
68
|
* if present, must be the leading token of the definition.
|
|
70
69
|
*
|
|
71
|
-
* @param
|
|
72
|
-
* @
|
|
70
|
+
* @param tagDef - tag definition text following the tag name
|
|
71
|
+
* @returns captured type and the
|
|
73
72
|
* remaining definition (name + description), or null when there is no type
|
|
74
73
|
*/
|
|
75
74
|
function extractType(tagDef) {
|
|
@@ -96,8 +95,7 @@ function extractType(tagDef) {
|
|
|
96
95
|
/**
|
|
97
96
|
* Parses given multi-line comment block
|
|
98
97
|
*
|
|
99
|
-
* @param
|
|
100
|
-
* @return {CommentMetadata}
|
|
98
|
+
* @param src - class source code
|
|
101
99
|
*/
|
|
102
100
|
function parseComment(src) {
|
|
103
101
|
let cleanSrc = src.replace(RX_MULTILINE_CLEANUP, '\n').trim();
|
|
@@ -163,8 +161,8 @@ function parseComment(src) {
|
|
|
163
161
|
* comment between the previous class member (or the class body start) and
|
|
164
162
|
* the method itself, i.e. the last block comment in its leading trivia.
|
|
165
163
|
*
|
|
166
|
-
* @param
|
|
167
|
-
* @param
|
|
164
|
+
* @param name - class name
|
|
165
|
+
* @param src - class source code
|
|
168
166
|
*/
|
|
169
167
|
function parseDescriptions(name, src) {
|
|
170
168
|
const comments = [];
|
|
@@ -243,9 +241,9 @@ function parseDescriptions(name, src) {
|
|
|
243
241
|
* method whose declaration head immediately follows it. When several blocks
|
|
244
242
|
* precede a method, the closest one wins, matching the runtime parser.
|
|
245
243
|
*
|
|
246
|
-
* @param
|
|
247
|
-
* @param
|
|
248
|
-
* @
|
|
244
|
+
* @param src - source file text (TypeScript or JavaScript)
|
|
245
|
+
* @param className - class to extract method docs for
|
|
246
|
+
* @returns map of method name to raw JSDoc
|
|
249
247
|
* block body (delimiters excluded, as acorn reports comment values)
|
|
250
248
|
*/
|
|
251
249
|
export function parseSourceComments(src, className) {
|
|
@@ -278,8 +276,7 @@ const sourceFileCache = {};
|
|
|
278
276
|
/**
|
|
279
277
|
* Reads and caches a source file, returning null when unreadable.
|
|
280
278
|
*
|
|
281
|
-
* @param
|
|
282
|
-
* @return {string | null}
|
|
279
|
+
* @param path - absolute source file path
|
|
283
280
|
*/
|
|
284
281
|
function readSourceFile(path) {
|
|
285
282
|
if (!(path in sourceFileCache)) {
|
|
@@ -297,8 +294,6 @@ function readSourceFile(path) {
|
|
|
297
294
|
* physically the module defining the decorated class. Own and internal
|
|
298
295
|
* frames are skipped. Returns an empty string when no frame is usable
|
|
299
296
|
* (e.g. bundled builds without source maps).
|
|
300
|
-
*
|
|
301
|
-
* @return {string}
|
|
302
297
|
*/
|
|
303
298
|
function captureCallerPath() {
|
|
304
299
|
const stack = new Error().stack || '';
|
|
@@ -332,8 +327,8 @@ function captureCallerPath() {
|
|
|
332
327
|
* it carries any JSDoc; extraction misses leave methods at their current
|
|
333
328
|
* (untyped) state, so behavior never degrades below the status quo.
|
|
334
329
|
*
|
|
335
|
-
* @param
|
|
336
|
-
* @param
|
|
330
|
+
* @param className - class to describe
|
|
331
|
+
* @param sourcePath - captured defining file path
|
|
337
332
|
*/
|
|
338
333
|
function applySourceFallback(className, sourcePath) {
|
|
339
334
|
const hasJsdoc = Object.keys(descriptions[className] || {}).some(key => key !== 'inherits');
|
|
@@ -354,11 +349,11 @@ function applySourceFallback(className, sourcePath) {
|
|
|
354
349
|
/**
|
|
355
350
|
* Helper function to make easy descriptions parts extractions
|
|
356
351
|
*
|
|
357
|
-
* @param
|
|
358
|
-
* @param
|
|
359
|
-
* @param
|
|
360
|
-
* @param
|
|
361
|
-
* @
|
|
352
|
+
* @param prop - property name to extract
|
|
353
|
+
* @param className - class name to lookup
|
|
354
|
+
* @param method - method name to lookup
|
|
355
|
+
* @param defaults - a default value to use if nothing found
|
|
356
|
+
* @returns found value
|
|
362
357
|
*/
|
|
363
358
|
function get(prop, className, method, defaults) {
|
|
364
359
|
if (descriptions[className] && descriptions[className][method]) {
|
|
@@ -377,10 +372,10 @@ function get(prop, className, method, defaults) {
|
|
|
377
372
|
* preserved by `removeComments: false`); standard decorators provide no
|
|
378
373
|
* runtime `design:type` metadata, so JSDoc is the sole type source.
|
|
379
374
|
*
|
|
380
|
-
* @param
|
|
381
|
-
* @param
|
|
382
|
-
* @param
|
|
383
|
-
* @param
|
|
375
|
+
* @param ctor - class that declares the method
|
|
376
|
+
* @param methodName - exposed method name
|
|
377
|
+
* @param fn - the method implementation
|
|
378
|
+
* @param sourcePath - defining file path captured at decoration
|
|
384
379
|
*/
|
|
385
380
|
function buildMethodDescription(ctor, methodName, fn, sourcePath) {
|
|
386
381
|
const className = ctor.name;
|
|
@@ -437,11 +432,63 @@ function buildMethodDescription(ctor, methodName, fn, sourcePath) {
|
|
|
437
432
|
};
|
|
438
433
|
}
|
|
439
434
|
/**
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
435
|
+
* Makes a service method callable remotely by registering it in the RPC service
|
|
436
|
+
* description.
|
|
437
|
+
*
|
|
438
|
+
* @returns a dual-mode method decorator, typed `any` so one function serves both
|
|
439
|
+
* decorator protocols. It never replaces or wraps the decorated method:
|
|
440
|
+
* under standard (TC39) decorators it registers a
|
|
441
|
+
* `context.addInitializer()` hook and returns `undefined`; under legacy
|
|
442
|
+
* decorators it registers immediately and returns the property descriptor
|
|
443
|
+
* unchanged.
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* ```typescript
|
|
447
|
+
* import { expose, IMQService } from '@imqueue/rpc';
|
|
448
|
+
*
|
|
449
|
+
* class UserService extends IMQService {
|
|
450
|
+
* @expose()
|
|
451
|
+
* public async count(active: boolean): Promise<number> {
|
|
452
|
+
* return 0;
|
|
453
|
+
* }
|
|
454
|
+
* }
|
|
455
|
+
* ```
|
|
456
|
+
*
|
|
457
|
+
* @remarks
|
|
458
|
+
* Applying this is mandatory for remote callability: a service rejects calls
|
|
459
|
+
* to any class method absent from the service description with the
|
|
460
|
+
* `IMQ_RPC_NO_ACCESS` error. Undecorated methods remain callable in-process only.
|
|
461
|
+
*
|
|
462
|
+
* Every exposed method must carry a JSDoc block with typed `@param` and `@returns`
|
|
463
|
+
* tags. Standard decorators provide no runtime type reflection, so JSDoc is the
|
|
464
|
+
* only type source: undocumented types fall back to `any` in generated clients,
|
|
465
|
+
* and the documented `@param` list is also what the service's argument-count check
|
|
466
|
+
* validates — it must match the method's real arity, or calls fail with
|
|
467
|
+
* `IMQ_RPC_INVALID_ARGS_COUNT`. Consuming projects must therefore compile with
|
|
468
|
+
* `removeComments: false`.
|
|
469
|
+
*
|
|
470
|
+
* Decorator order matters: this must be the innermost decorator (listed last,
|
|
471
|
+
* closest to the method) when combined with {@link lock}, {@link cache} or
|
|
472
|
+
* {@link logged}. Those replace the method with a `(...args)` wrapper, and if this
|
|
473
|
+
* decorator is applied after them it records the wrapper's rest parameter as the
|
|
474
|
+
* method's only argument, breaking both argument validation and generated client
|
|
475
|
+
* signatures.
|
|
476
|
+
*
|
|
477
|
+
* Registration timing differs by protocol. Under standard decorators the class is
|
|
478
|
+
* not available at decoration time, so registration is deferred to an initializer
|
|
479
|
+
* that runs on first construction of an instance — the service description
|
|
480
|
+
* stays empty until then. Under legacy decorators it happens at class-definition
|
|
481
|
+
* time.
|
|
482
|
+
*
|
|
483
|
+
* Instance (prototype) methods only. Applied to a `static` method it silently
|
|
484
|
+
* registers under the pseudo-class name `Function` and the method stays
|
|
485
|
+
* unreachable remotely. Methods inherited from a base class are registered under
|
|
486
|
+
* the class that declares them and resolved through the description's `inherits`
|
|
487
|
+
* chain, so that base class must be loaded for the subclass description to be
|
|
488
|
+
* complete.
|
|
443
489
|
*
|
|
444
|
-
*
|
|
490
|
+
* Complex argument and return types are registered separately, by
|
|
491
|
+
* {@link classType} or {@link indexed} on those classes.
|
|
445
492
|
*/
|
|
446
493
|
export function expose() {
|
|
447
494
|
// the factory executes at class-definition time inside the defining
|
|
@@ -23,20 +23,39 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { type Thunk } from '../index.js';
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
* Exposes a complex service type that carries an index signature.
|
|
27
|
+
*
|
|
28
|
+
* @param indexTypedef - the index signature as raw source text, for example
|
|
29
|
+
* `'[fieldName: string]: any'`, or a {@link Thunk} returning it. A falsy
|
|
30
|
+
* value makes the decoration a silent no-op; a non-string is coerced with
|
|
31
|
+
* `String()`.
|
|
32
|
+
* @returns a dual-mode class decorator `(value, context?) => any`. Under standard
|
|
33
|
+
* (TC39) decorators it flushes the class's `@property` fields and
|
|
34
|
+
* records the index signature, returning `undefined`; under legacy
|
|
35
|
+
* decorators the fields are already registered, so it only attaches the
|
|
36
|
+
* index signature and returns the class unchanged.
|
|
29
37
|
*
|
|
30
38
|
* @example
|
|
31
|
-
*
|
|
32
|
-
* import {
|
|
39
|
+
* ```typescript
|
|
40
|
+
* import { indexed, property } from '@imqueue/rpc';
|
|
33
41
|
*
|
|
42
|
+
* // @indexed() also flushes this class's @property fields,
|
|
43
|
+
* // so a separate @classType() is not needed
|
|
34
44
|
* @indexed('[fieldName: string]: any')
|
|
35
45
|
* class Schema {
|
|
36
|
-
*
|
|
46
|
+
* @property('string')
|
|
47
|
+
* name!: string;
|
|
48
|
+
*
|
|
49
|
+
* [fieldName: string]: any;
|
|
37
50
|
* }
|
|
38
|
-
*
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* Under standard decorators this is a superset of {@link classType} — it performs
|
|
55
|
+
* the same `@property` flush in addition to recording the index signature.
|
|
39
56
|
*
|
|
40
|
-
*
|
|
57
|
+
* The index signature is injected verbatim into generated client interfaces and is
|
|
58
|
+
* not validated, so a malformed string produces a client that does not
|
|
59
|
+
* compile. A thunk is evaluated once, on first use.
|
|
41
60
|
*/
|
|
42
61
|
export declare function indexed(indexTypedef: string | Thunk): any;
|
|
@@ -24,21 +24,40 @@
|
|
|
24
24
|
import { IMQRPCDescription } from '../index.js';
|
|
25
25
|
import { registerType } from './property.js';
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
27
|
+
* Exposes a complex service type that carries an index signature.
|
|
28
|
+
*
|
|
29
|
+
* @param indexTypedef - the index signature as raw source text, for example
|
|
30
|
+
* `'[fieldName: string]: any'`, or a {@link Thunk} returning it. A falsy
|
|
31
|
+
* value makes the decoration a silent no-op; a non-string is coerced with
|
|
32
|
+
* `String()`.
|
|
33
|
+
* @returns a dual-mode class decorator `(value, context?) => any`. Under standard
|
|
34
|
+
* (TC39) decorators it flushes the class's `@property` fields and
|
|
35
|
+
* records the index signature, returning `undefined`; under legacy
|
|
36
|
+
* decorators the fields are already registered, so it only attaches the
|
|
37
|
+
* index signature and returns the class unchanged.
|
|
30
38
|
*
|
|
31
39
|
* @example
|
|
32
|
-
*
|
|
33
|
-
* import {
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { indexed, property } from '@imqueue/rpc';
|
|
34
42
|
*
|
|
43
|
+
* // @indexed() also flushes this class's @property fields,
|
|
44
|
+
* // so a separate @classType() is not needed
|
|
35
45
|
* @indexed('[fieldName: string]: any')
|
|
36
46
|
* class Schema {
|
|
37
|
-
*
|
|
47
|
+
* @property('string')
|
|
48
|
+
* name!: string;
|
|
49
|
+
*
|
|
50
|
+
* [fieldName: string]: any;
|
|
38
51
|
* }
|
|
39
|
-
*
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Under standard decorators this is a superset of {@link classType} — it performs
|
|
56
|
+
* the same `@property` flush in addition to recording the index signature.
|
|
40
57
|
*
|
|
41
|
-
*
|
|
58
|
+
* The index signature is injected verbatim into generated client interfaces and is
|
|
59
|
+
* not validated, so a malformed string produces a client that does not
|
|
60
|
+
* compile. A thunk is evaluated once, on first use.
|
|
42
61
|
*/
|
|
43
62
|
export function indexed(indexTypedef) {
|
|
44
63
|
// Dual-mode: standard (TC39) class decorators pass a context object with a
|
package/src/decorators/lock.d.ts
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for the {@link lock} decorator.
|
|
3
|
+
*/
|
|
1
4
|
export interface LockOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Set to `true` to turn locking off for the decorated method.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Inverted sense: omitted or `false` means locking is enabled.
|
|
10
|
+
*/
|
|
2
11
|
disabled?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Positional indices of arguments to exclude when computing the lock key —
|
|
14
|
+
* useful for request-context or callback arguments that must not affect
|
|
15
|
+
* similarity.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Excluded arguments are still passed to the method unchanged; only the key
|
|
19
|
+
* ignores them.
|
|
20
|
+
*/
|
|
3
21
|
skipArgs?: number[];
|
|
4
22
|
}
|
|
5
23
|
/**
|
|
@@ -10,8 +28,8 @@ export interface LockOptions {
|
|
|
10
28
|
* dual-mode: it works both as a standard (TC39) and as a legacy method
|
|
11
29
|
* decorator.
|
|
12
30
|
*
|
|
13
|
-
* @param
|
|
31
|
+
* @param enabledOrOptions - whether locking is enabled,
|
|
14
32
|
* or the lock options
|
|
15
|
-
* @
|
|
33
|
+
* @returns a dual-mode method decorator
|
|
16
34
|
*/
|
|
17
35
|
export declare function lock(enabledOrOptions?: boolean | LockOptions): any;
|
package/src/decorators/lock.js
CHANGED
|
@@ -31,9 +31,9 @@ import { signature } from '../helpers/index.js';
|
|
|
31
31
|
* dual-mode: it works both as a standard (TC39) and as a legacy method
|
|
32
32
|
* decorator.
|
|
33
33
|
*
|
|
34
|
-
* @param
|
|
34
|
+
* @param enabledOrOptions - whether locking is enabled,
|
|
35
35
|
* or the lock options
|
|
36
|
-
* @
|
|
36
|
+
* @returns a dual-mode method decorator
|
|
37
37
|
*/
|
|
38
38
|
export function lock(enabledOrOptions = true) {
|
|
39
39
|
const enabled = typeof enabledOrOptions === 'boolean'
|
|
@@ -22,10 +22,41 @@
|
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
24
|
import { type ILogger } from '@imqueue/core';
|
|
25
|
+
/**
|
|
26
|
+
* Names of the `ILogger` methods {@link logged} can use to record a caught
|
|
27
|
+
* error.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* `ILogger` is declared by `@imqueue/core` and is not re-exported here — import it
|
|
31
|
+
* from that package if you need the type. A plain `console` satisfies it.
|
|
32
|
+
*/
|
|
25
33
|
export type LoggedLogLevel = 'info' | 'log' | 'warn' | 'error';
|
|
34
|
+
/**
|
|
35
|
+
* Options for the {@link logged} decorator.
|
|
36
|
+
*/
|
|
26
37
|
export interface LoggedDecoratorOptions {
|
|
38
|
+
/**
|
|
39
|
+
* Which `ILogger` method records the error.
|
|
40
|
+
*
|
|
41
|
+
* @defaultValue 'error'
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* The resolved logger must implement this method, or the call throws a
|
|
45
|
+
* `TypeError` from inside the catch block.
|
|
46
|
+
*/
|
|
27
47
|
level?: LoggedLogLevel;
|
|
48
|
+
/**
|
|
49
|
+
* An explicit logger, taking precedence over the instance, class and console
|
|
50
|
+
* fallbacks.
|
|
51
|
+
*/
|
|
28
52
|
logger?: ILogger;
|
|
53
|
+
/**
|
|
54
|
+
* Set to `true` to swallow the error after logging it, in which case the method
|
|
55
|
+
* resolves to `undefined`.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* Inverted sense: by default the error is re-thrown.
|
|
59
|
+
*/
|
|
29
60
|
doNotThrow?: boolean;
|
|
30
61
|
}
|
|
31
62
|
/**
|
|
@@ -37,8 +68,8 @@ export interface LoggedDecoratorOptions {
|
|
|
37
68
|
* returned decorator is dual-mode: it works both as a standard (TC39) and as a
|
|
38
69
|
* legacy method decorator.
|
|
39
70
|
*
|
|
40
|
-
* @param
|
|
71
|
+
* @param options - a logger to use, or the
|
|
41
72
|
* logged-decorator options
|
|
42
|
-
* @
|
|
73
|
+
* @returns a dual-mode method decorator
|
|
43
74
|
*/
|
|
44
75
|
export declare function logged(options?: ILogger | LoggedDecoratorOptions): any;
|
package/src/decorators/logged.js
CHANGED
|
@@ -31,9 +31,9 @@ import {} from '@imqueue/core';
|
|
|
31
31
|
* returned decorator is dual-mode: it works both as a standard (TC39) and as a
|
|
32
32
|
* legacy method decorator.
|
|
33
33
|
*
|
|
34
|
-
* @param
|
|
34
|
+
* @param options - a logger to use, or the
|
|
35
35
|
* logged-decorator options
|
|
36
|
-
* @
|
|
36
|
+
* @returns a dual-mode method decorator
|
|
37
37
|
*/
|
|
38
38
|
export function logged(options) {
|
|
39
39
|
const level = options && options.level
|
|
@@ -1,14 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A zero-argument function whose return value is resolved lazily.
|
|
3
|
+
*
|
|
4
|
+
* Used by {@link property} and {@link indexed} so a type definition can reference
|
|
5
|
+
* a class that is not yet initialized at decoration time — self-references and
|
|
6
|
+
* forward references.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Detection is by the absence of a function name, so the thunk must be an
|
|
10
|
+
* anonymous function or arrow function. A named function is treated as a
|
|
11
|
+
* constructor instead, and resolves to its own name.
|
|
12
|
+
*/
|
|
1
13
|
export interface Thunk {
|
|
14
|
+
/**
|
|
15
|
+
* @returns the deferred value — a type definition or an index-signature string
|
|
16
|
+
*/
|
|
2
17
|
(): any;
|
|
3
18
|
}
|
|
4
19
|
/**
|
|
5
|
-
* Flushes
|
|
20
|
+
* Flushes `@property` definitions collected on a class into the RPC type
|
|
6
21
|
* description. Invoked by class-level decorators once the class (and hence
|
|
7
22
|
* its name) is available.
|
|
8
23
|
*
|
|
9
|
-
* @param
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
24
|
+
* @param ctor - the decorated class constructor
|
|
25
|
+
* @param metadata - shared decorator metadata carrying the collected properties
|
|
26
|
+
* @param indexType - optional index signature definition, as raw source text
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* Only the class's own collected properties are flushed — fields inherited
|
|
30
|
+
* from a base class are not copied, and are represented solely by `inherits`. So
|
|
31
|
+
* every class in a hierarchy that declares `@property` fields needs its own
|
|
32
|
+
* {@link classType} or {@link indexed}, or those fields are lost.
|
|
33
|
+
*
|
|
34
|
+
* `inherits` is re-derived from the runtime prototype chain on every call, and is
|
|
35
|
+
* the empty string for a class with no `extends`.
|
|
36
|
+
*
|
|
37
|
+
* Each property is installed as an accessor whose `type` is a memoising getter,
|
|
38
|
+
* so the type argument is resolved on first read rather than at decoration time.
|
|
39
|
+
* Existing entries are merged into, never cleared.
|
|
12
40
|
*/
|
|
13
41
|
export declare function registerType(ctor: Function, metadata: DecoratorMetadata | undefined, indexType?: string): void;
|
|
42
|
+
/**
|
|
43
|
+
* Marks a class field as part of an exposed complex type, so it is described to
|
|
44
|
+
* clients and appears in the generated client interfaces.
|
|
45
|
+
*
|
|
46
|
+
* @param type - the field's RPC type: a type-definition string (`'string'`,
|
|
47
|
+
* `'Address'`, `'Array<Address>'`), a constructor (its `name` is used), a
|
|
48
|
+
* single-element array such as `[Address]` (which yields `Address[]`), or
|
|
49
|
+
* an anonymous {@link Thunk} returning any of those — required for
|
|
50
|
+
* self- or forward-referencing types, since a thunk is not invoked until
|
|
51
|
+
* the description is first read. A named function is treated as a
|
|
52
|
+
* constructor, not a thunk.
|
|
53
|
+
* @param isOptional - marks the field optional in the generated type. Not
|
|
54
|
+
* inferred from the TypeScript `?` modifier — pass `true` explicitly.
|
|
55
|
+
* @returns a dual-mode field decorator `(target, context) => any`, typed `any`
|
|
56
|
+
* so one function serves both decorator protocols. Under standard (TC39)
|
|
57
|
+
* decorators it records the field on the class's decorator metadata for a
|
|
58
|
+
* later flush and returns `undefined`; under legacy decorators it writes
|
|
59
|
+
* the field into the RPC type description immediately.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* import { classType, property, expose, IMQService } from '@imqueue/rpc';
|
|
64
|
+
*
|
|
65
|
+
* // every class using @property also needs a class-level @classType()
|
|
66
|
+
* @classType()
|
|
67
|
+
* class Address {
|
|
68
|
+
* @property('string')
|
|
69
|
+
* country!: string;
|
|
70
|
+
*
|
|
71
|
+
* @property('string', true)
|
|
72
|
+
* zipCode?: string; // optional
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* @classType()
|
|
76
|
+
* class User {
|
|
77
|
+
* @property('string')
|
|
78
|
+
* firstName!: string;
|
|
79
|
+
*
|
|
80
|
+
* // thunk + array form, for a forward reference
|
|
81
|
+
* @property(() => [Address], true)
|
|
82
|
+
* addresses?: Address[];
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* class UserService extends IMQService {
|
|
86
|
+
* // exposed methods need a JSDoc block with typed @param/@returns tags —
|
|
87
|
+
* // see the `expose` decorator
|
|
88
|
+
* @expose()
|
|
89
|
+
* public async save(user: User): Promise<boolean> {
|
|
90
|
+
* return true;
|
|
91
|
+
* }
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @remarks
|
|
96
|
+
* Every class that uses `@property` must also carry a class-level
|
|
97
|
+
* {@link classType} — or {@link indexed}, which does the same flush — when
|
|
98
|
+
* compiling with standard (TC39) decorators, the protocol this package targets.
|
|
99
|
+
* Standard field decorators cannot see their class, so a class-level decorator is
|
|
100
|
+
* what registers the collected fields under the class name. Omitting it fails
|
|
101
|
+
* silently: the type simply never appears in the RPC type description, and
|
|
102
|
+
* generated clients reference an undeclared type.
|
|
103
|
+
*
|
|
104
|
+
* Passing a falsy `type` returns `undefined`, which TypeScript accepts as a no-op
|
|
105
|
+
* decoration — the field is then silently absent from the type description.
|
|
106
|
+
*/
|
|
14
107
|
export declare function property(type: string | Thunk | any, isOptional?: boolean): any;
|
|
@@ -34,8 +34,8 @@ const PROPERTIES = Symbol('@imqueue/rpc:properties');
|
|
|
34
34
|
* Resolves a @property type argument (string, constructor, thunk, or array
|
|
35
35
|
* form) to its RPC type-definition string.
|
|
36
36
|
*
|
|
37
|
-
* @param
|
|
38
|
-
* @
|
|
37
|
+
* @param input - the type argument as given to `@property`
|
|
38
|
+
* @returns the resolved RPC type-definition string
|
|
39
39
|
*/
|
|
40
40
|
function resolveTypeDef(input) {
|
|
41
41
|
let type = input;
|
|
@@ -58,13 +58,26 @@ function resolveTypeDef(input) {
|
|
|
58
58
|
return typeDef;
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
|
-
* Flushes
|
|
61
|
+
* Flushes `@property` definitions collected on a class into the RPC type
|
|
62
62
|
* description. Invoked by class-level decorators once the class (and hence
|
|
63
63
|
* its name) is available.
|
|
64
64
|
*
|
|
65
|
-
* @param
|
|
66
|
-
* @param
|
|
67
|
-
* @param
|
|
65
|
+
* @param ctor - the decorated class constructor
|
|
66
|
+
* @param metadata - shared decorator metadata carrying the collected properties
|
|
67
|
+
* @param indexType - optional index signature definition, as raw source text
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Only the class's own collected properties are flushed — fields inherited
|
|
71
|
+
* from a base class are not copied, and are represented solely by `inherits`. So
|
|
72
|
+
* every class in a hierarchy that declares `@property` fields needs its own
|
|
73
|
+
* {@link classType} or {@link indexed}, or those fields are lost.
|
|
74
|
+
*
|
|
75
|
+
* `inherits` is re-derived from the runtime prototype chain on every call, and is
|
|
76
|
+
* the empty string for a class with no `extends`.
|
|
77
|
+
*
|
|
78
|
+
* Each property is installed as an accessor whose `type` is a memoising getter,
|
|
79
|
+
* so the type argument is resolved on first read rather than at decoration time.
|
|
80
|
+
* Existing entries are merged into, never cleared.
|
|
68
81
|
*/
|
|
69
82
|
export function registerType(ctor, metadata, indexType) {
|
|
70
83
|
const typeName = ctor.name;
|
|
@@ -103,6 +116,71 @@ export function registerType(ctor, metadata, indexType) {
|
|
|
103
116
|
description.indexType = indexType;
|
|
104
117
|
}
|
|
105
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Marks a class field as part of an exposed complex type, so it is described to
|
|
121
|
+
* clients and appears in the generated client interfaces.
|
|
122
|
+
*
|
|
123
|
+
* @param type - the field's RPC type: a type-definition string (`'string'`,
|
|
124
|
+
* `'Address'`, `'Array<Address>'`), a constructor (its `name` is used), a
|
|
125
|
+
* single-element array such as `[Address]` (which yields `Address[]`), or
|
|
126
|
+
* an anonymous {@link Thunk} returning any of those — required for
|
|
127
|
+
* self- or forward-referencing types, since a thunk is not invoked until
|
|
128
|
+
* the description is first read. A named function is treated as a
|
|
129
|
+
* constructor, not a thunk.
|
|
130
|
+
* @param isOptional - marks the field optional in the generated type. Not
|
|
131
|
+
* inferred from the TypeScript `?` modifier — pass `true` explicitly.
|
|
132
|
+
* @returns a dual-mode field decorator `(target, context) => any`, typed `any`
|
|
133
|
+
* so one function serves both decorator protocols. Under standard (TC39)
|
|
134
|
+
* decorators it records the field on the class's decorator metadata for a
|
|
135
|
+
* later flush and returns `undefined`; under legacy decorators it writes
|
|
136
|
+
* the field into the RPC type description immediately.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* import { classType, property, expose, IMQService } from '@imqueue/rpc';
|
|
141
|
+
*
|
|
142
|
+
* // every class using @property also needs a class-level @classType()
|
|
143
|
+
* @classType()
|
|
144
|
+
* class Address {
|
|
145
|
+
* @property('string')
|
|
146
|
+
* country!: string;
|
|
147
|
+
*
|
|
148
|
+
* @property('string', true)
|
|
149
|
+
* zipCode?: string; // optional
|
|
150
|
+
* }
|
|
151
|
+
*
|
|
152
|
+
* @classType()
|
|
153
|
+
* class User {
|
|
154
|
+
* @property('string')
|
|
155
|
+
* firstName!: string;
|
|
156
|
+
*
|
|
157
|
+
* // thunk + array form, for a forward reference
|
|
158
|
+
* @property(() => [Address], true)
|
|
159
|
+
* addresses?: Address[];
|
|
160
|
+
* }
|
|
161
|
+
*
|
|
162
|
+
* class UserService extends IMQService {
|
|
163
|
+
* // exposed methods need a JSDoc block with typed @param/@returns tags —
|
|
164
|
+
* // see the `expose` decorator
|
|
165
|
+
* @expose()
|
|
166
|
+
* public async save(user: User): Promise<boolean> {
|
|
167
|
+
* return true;
|
|
168
|
+
* }
|
|
169
|
+
* }
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* Every class that uses `@property` must also carry a class-level
|
|
174
|
+
* {@link classType} — or {@link indexed}, which does the same flush — when
|
|
175
|
+
* compiling with standard (TC39) decorators, the protocol this package targets.
|
|
176
|
+
* Standard field decorators cannot see their class, so a class-level decorator is
|
|
177
|
+
* what registers the collected fields under the class name. Omitting it fails
|
|
178
|
+
* silently: the type simply never appears in the RPC type description, and
|
|
179
|
+
* generated clients reference an undeclared type.
|
|
180
|
+
*
|
|
181
|
+
* Passing a falsy `type` returns `undefined`, which TypeScript accepts as a no-op
|
|
182
|
+
* decoration — the field is then silently absent from the type description.
|
|
183
|
+
*/
|
|
106
184
|
export function property(type, isOptional = false) {
|
|
107
185
|
if (!type) {
|
|
108
186
|
return;
|
package/src/decorators/remote.js
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* both as a standard (TC39) and as a legacy (experimentalDecorators) method
|
|
29
29
|
* decorator.
|
|
30
30
|
*
|
|
31
|
-
* @
|
|
31
|
+
* @returns a dual-mode method decorator
|
|
32
32
|
*/
|
|
33
33
|
export function remote() {
|
|
34
34
|
const wrap = (original, methodName) => function (...args) {
|
package/src/helpers/fs.d.ts
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Checks whether a file exists at the given path.
|
|
3
3
|
*
|
|
4
|
-
* @param
|
|
5
|
-
* @
|
|
4
|
+
* @param path - path to the file to check
|
|
5
|
+
* @returns true if the file exists, false otherwise
|
|
6
6
|
*/
|
|
7
7
|
export declare function fileExists(path: string): Promise<boolean>;
|
|
8
8
|
/**
|
|
9
9
|
* Asynchronously creates a directory at the given path.
|
|
10
10
|
*
|
|
11
|
-
* @param
|
|
12
|
-
* @return {Promise<void>}
|
|
11
|
+
* @param path - path of the directory to create
|
|
13
12
|
*/
|
|
14
13
|
export declare function mkdir(path: string): Promise<void>;
|
|
15
14
|
/**
|
|
16
15
|
* Asynchronously writes the given content to a file at the given path.
|
|
17
16
|
*
|
|
18
|
-
* @param
|
|
19
|
-
* @param
|
|
20
|
-
* @return {Promise<void>}
|
|
17
|
+
* @param path - path of the file to write
|
|
18
|
+
* @param content - content to write to the file
|
|
21
19
|
*/
|
|
22
20
|
export declare function writeFile(path: string, content: string): Promise<void>;
|