@znight/core 0.3.0 → 0.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@znight/core",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Znight core runtime: compile-time DI container, lifecycle, plugins and marker decorators",
5
5
  "license": "MIT",
6
6
  "author": "Ugho STEPHAN",
package/src/index.ts CHANGED
@@ -78,6 +78,18 @@ export {
78
78
  silentLogger,
79
79
  } from './logger.ts'
80
80
  export * from './metadata.ts'
81
+ export {
82
+ createParamDecorator,
83
+ type ParamContexts,
84
+ type ParamDataArgs,
85
+ type ParamDecorator,
86
+ type ParamFunctionOf,
87
+ type ParamResolver,
88
+ type ParamResolverClass,
89
+ type ParamResolverFunction,
90
+ type ParamResolverOf,
91
+ type ParamResolvers,
92
+ } from './params.ts'
81
93
  export {
82
94
  type Plugin,
83
95
  type PluginContext,
package/src/params.ts ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Custom parameter decorators (RFC 0010). `createParamDecorator` returns a marker decorator, read
3
+ * by `@znight/compiler`, and keeps the resolvers that the generated `invoke` of each transport calls.
4
+ */
5
+ import type { ParameterMarker } from './decorators.ts'
6
+
7
+ /**
8
+ * Resolver contexts by transport. Each transport package adds its own entry through declaration
9
+ * merging (`http: HttpContext` in `@znight/http`, `graphql: ResolverContext`, `messaging:
10
+ * InboundMessage`); a transport that is not installed has no entry.
11
+ *
12
+ * ```ts
13
+ * declare module '@znight/core' {
14
+ * interface ParamContexts {
15
+ * http: HttpContext
16
+ * }
17
+ * }
18
+ * ```
19
+ */
20
+ // biome-ignore lint/suspicious/noEmptyInterface: augmented by the transport packages (declaration merging)
21
+ export interface ParamContexts {}
22
+
23
+ /** A provider computing the value of a parameter, in the scope of the call (RFC 0010 §4.6). */
24
+ export interface ParamResolver<C, T, D = undefined> {
25
+ resolve(ctx: C, data: D): T | PromiseLike<T>
26
+ }
27
+
28
+ /** A resolver function: receives the context of its transport and the data argument of the use. */
29
+ export type ParamResolverFunction<C, T, D> = (ctx: C, data: D) => T | PromiseLike<T>
30
+
31
+ /** The class of a provider implementing {@link ParamResolver} (bound at build time, resolved in the scope of the call). */
32
+ export type ParamResolverClass<C, T, D> = abstract new (...args: never[]) => ParamResolver<C, T, D>
33
+
34
+ /** Computes the value of a parameter: a function, or the class of a provider implementing {@link ParamResolver}. */
35
+ export type ParamResolverOf<C, T, D> = ParamResolverFunction<C, T, D> | ParamResolverClass<C, T, D>
36
+
37
+ /** The resolvers of a definition, by transport (keys of {@link ParamContexts}). */
38
+ export type ParamResolvers<T, D> = {
39
+ readonly [K in keyof ParamContexts]?: ParamResolverOf<ParamContexts[K], T, D>
40
+ }
41
+
42
+ /** Arguments of a use: none when `D` is `undefined`, optional when `D` includes `undefined`, required otherwise. */
43
+ export type ParamDataArgs<D> = undefined extends D ? [data?: D] : [data: D]
44
+
45
+ /** What {@link createParamDecorator} returns: the decorator (a no-op marker) and its resolvers, for generated code. */
46
+ export interface ParamDecorator<T, D = undefined> {
47
+ (...data: ParamDataArgs<D>): ParameterMarker
48
+ /** The resolvers of the definition, frozen. */
49
+ readonly resolvers: ParamResolvers<T, D>
50
+ }
51
+
52
+ /**
53
+ * The resolver function of a definition for a transport, as generated code calls it:
54
+ * `(CurrentUser.resolvers.http as ParamFunctionOf<typeof CurrentUser, 'http'>)(ctx, undefined)`.
55
+ * The compiler emits it only for function resolvers.
56
+ */
57
+ export type ParamFunctionOf<P extends { readonly resolvers: object }, K extends keyof P['resolvers']> = Extract<
58
+ NonNullable<P['resolvers'][K]>,
59
+ // biome-ignore lint/suspicious/noExplicitAny: matches every function type, whatever its parameters
60
+ (...args: any[]) => unknown
61
+ >
62
+
63
+ function marker(): void {
64
+ // intentionally empty: decorators are compile-time markers
65
+ }
66
+
67
+ const parameterMarker: ParameterMarker = marker
68
+
69
+ /**
70
+ * Defines a parameter decorator whose value is computed per call by a resolver of the handler's
71
+ * transport (RFC 0010). `T` (required) is the injected type, checked at build time against every
72
+ * decorated parameter; `D` is the type of the data argument (none by default). The definition must
73
+ * be an exported top-level `const`: the generated module imports it.
74
+ *
75
+ * ```ts
76
+ * export const CurrentUser = createParamDecorator<User>({
77
+ * http: ctx => userOf(ctx.state),
78
+ * graphql: ctx => userOf(ctx.operation.state),
79
+ * })
80
+ *
81
+ * @Get('/me')
82
+ * me(@CurrentUser() user: User) { ... }
83
+ * ```
84
+ */
85
+ export function createParamDecorator<T, D = undefined>(resolvers: ParamResolvers<T, D>): ParamDecorator<T, D> {
86
+ const decorator = ((..._data: ParamDataArgs<D>): ParameterMarker => parameterMarker) as ParamDecorator<T, D>
87
+ Object.defineProperty(decorator, 'resolvers', { value: Object.freeze({ ...resolvers }), enumerable: true })
88
+ return decorator
89
+ }