@taujs/server 0.5.9 → 0.6.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.
@@ -23,7 +23,6 @@ interface Logs extends BaseLogger {
23
23
  isDebugEnabled(category: DebugCategory): boolean;
24
24
  }
25
25
 
26
- type RegistryCaller<R extends ServiceRegistry = ServiceRegistry> = (serviceName: keyof R & string, methodName: string, args?: JsonObject) => Promise<JsonObject>;
27
26
  type JsonPrimitive = string | number | boolean | null;
28
27
  type JsonValue = JsonPrimitive | JsonValue[] | {
29
28
  [k: string]: JsonValue;
@@ -34,7 +33,7 @@ type JsonObject = {
34
33
  type NarrowSchema<T> = {
35
34
  parse: (u: unknown) => T;
36
35
  } | ((u: unknown) => T);
37
- type ServiceContext = {
36
+ type BaseServiceContext = {
38
37
  signal?: AbortSignal;
39
38
  deadlineMs?: number;
40
39
  traceId?: string;
@@ -43,26 +42,46 @@ type ServiceContext = {
43
42
  id: string;
44
43
  roles: string[];
45
44
  } | null;
46
- call?: (service: string, method: string, args?: JsonObject) => Promise<JsonObject>;
47
45
  };
48
- declare function withDeadline(signal: AbortSignal | undefined, ms?: number): AbortSignal | undefined;
49
- type ServiceMethod<P, R extends JsonObject = JsonObject> = (params: P, ctx: ServiceContext) => Promise<R>;
50
- type ServiceDefinition = Readonly<Record<string, ServiceMethod<any, JsonObject>>>;
46
+ type UntypedRegistryCaller = (serviceName: string, methodName: string, args?: JsonObject) => Promise<JsonObject>;
47
+ type RuntimeServiceContext = BaseServiceContext & {
48
+ call?: UntypedRegistryCaller;
49
+ };
50
+ interface ServiceContext extends BaseServiceContext {
51
+ }
52
+ type ServiceMethod<P extends JsonObject = JsonObject, R extends JsonObject = JsonObject, Ctx extends BaseServiceContext = TypedServiceContext> = (params: P, ctx: Ctx) => Promise<R>;
53
+ type RuntimeServiceMethod<P extends JsonObject = JsonObject, R extends JsonObject = JsonObject> = (params: P, ctx: RuntimeServiceContext) => Promise<R>;
54
+ type ServiceDefinition = Readonly<Record<string, RuntimeServiceMethod<any, JsonObject>>>;
51
55
  type ServiceRegistry = Readonly<Record<string, ServiceDefinition>>;
56
+ type ServiceMethodParams<M> = M extends (params: infer P, ctx: any) => Promise<any> ? P : never;
57
+ type ServiceMethodResult<M> = Awaited<M extends (...args: any[]) => Promise<infer R> ? R : never>;
58
+ type RegistryCallerArgs<R extends ServiceRegistry, S extends keyof R & string, M extends keyof R[S] & string> = undefined extends ServiceMethodParams<R[S][M]> ? [serviceName: S, methodName: M, args?: ServiceMethodParams<R[S][M]>] : [serviceName: S, methodName: M, args: ServiceMethodParams<R[S][M]>];
59
+ type RegistryCaller<R extends ServiceRegistry = ServiceRegistry> = <S extends keyof R & string, M extends keyof R[S] & string>(...args: RegistryCallerArgs<R, S, M>) => Promise<ServiceMethodResult<R[S][M]>>;
60
+ type TypedServiceContext<R extends ServiceRegistry = ServiceRegistry> = ServiceContext & {
61
+ call?: RegistryCaller<R>;
62
+ };
63
+ declare function withDeadline(signal: AbortSignal | undefined, ms?: number): AbortSignal | undefined;
52
64
  type ServiceDescriptor = {
53
65
  serviceName: string;
54
66
  serviceMethod: string;
55
67
  args?: JsonObject;
56
68
  };
57
- declare function defineService<T extends Record<string, ServiceMethod<any, JsonObject> | {
69
+ type ServiceSpecEntry = ServiceMethod<any, JsonObject> | {
58
70
  handler: ServiceMethod<any, JsonObject>;
59
71
  params?: NarrowSchema<any>;
60
72
  result?: NarrowSchema<any>;
61
- }>>(spec: T): { [K in keyof T]: T[K] extends ServiceMethod<infer P, infer R> ? ServiceMethod<P, R> : T[K] extends {
62
- handler: ServiceMethod<infer P, infer R_1>;
63
- } ? ServiceMethod<P, R_1> : never; };
73
+ };
74
+ type ServiceSpec = Record<string, ServiceSpecEntry>;
75
+ type ExtractServiceMethod<T> = T extends {
76
+ handler: infer H;
77
+ } ? H : T;
78
+ type NormalizeServiceMethod<M> = M extends (params: infer P extends JsonObject, ctx: any) => Promise<infer R extends JsonObject> ? RuntimeServiceMethod<P, R> : never;
79
+ type NormalizedServiceSpec<T extends ServiceSpec> = {
80
+ [K in keyof T]: NormalizeServiceMethod<ExtractServiceMethod<T[K]>>;
81
+ };
82
+ declare function defineService<T extends ServiceSpec>(spec: T): NormalizedServiceSpec<T>;
64
83
  declare const defineServiceRegistry: <R extends ServiceRegistry>(registry: R) => R;
65
- declare function callServiceMethod(registry: ServiceRegistry, serviceName: string, methodName: string, params: JsonObject | undefined, ctx: ServiceContext): Promise<JsonObject>;
84
+ declare function callServiceMethod(registry: ServiceRegistry, serviceName: string, methodName: string, params: JsonObject | undefined, ctx: BaseServiceContext): Promise<JsonObject>;
66
85
 
67
86
  type RequestContext<L extends Logs = Logs> = {
68
87
  traceId: string;
@@ -260,4 +279,4 @@ type RouteData<C extends TaujsConfig = TaujsConfig, P extends string = string> =
260
279
 
261
280
  declare function defineConfig<const C extends TaujsConfig>(config: C): C;
262
281
 
263
- export { type AppConfig as A, type BaseLogger as B, type CoreAppConfig as C, type DebugConfig as D, type RouteContext as R, type ServiceRegistry as S, type TaujsConfig as T, type SecurityConfig as a, type RouteData as b, callServiceMethod as c, defineConfig as d, defineService as e, defineServiceRegistry as f, type RegistryCaller as g, type ServiceContext as h, AppError as i, withDeadline as w };
282
+ export { type AppConfig as A, type BaseLogger as B, type CoreAppConfig as C, type DebugConfig as D, type JsonObject as J, type RouteContext as R, type ServiceRegistry as S, type TaujsConfig as T, type SecurityConfig as a, type RouteData as b, callServiceMethod as c, defineConfig as d, defineService as e, defineServiceRegistry as f, type JsonPrimitive as g, type JsonValue as h, type RegistryCaller as i, type ServiceContext as j, type TypedServiceContext as k, AppError as l, withDeadline as w };
package/dist/Config.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import 'fastify';
2
2
  import 'vite';
3
- export { A as AppConfig, i as AppError, g as RegistryCaller, R as RouteContext, b as RouteData, a as SecurityConfig, h as ServiceContext, T as TaujsConfig, c as callServiceMethod, d as defineConfig, e as defineService, f as defineServiceRegistry, w as withDeadline } from './Config-2Loymxo2.js';
3
+ export { A as AppConfig, l as AppError, J as JsonObject, g as JsonPrimitive, h as JsonValue, i as RegistryCaller, R as RouteContext, b as RouteData, a as SecurityConfig, j as ServiceContext, T as TaujsConfig, k as TypedServiceContext, c as callServiceMethod, d as defineConfig, e as defineService, f as defineServiceRegistry, w as withDeadline } from './Config-BODjH3XQ.js';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FastifyPluginCallback, FastifyPluginAsync, FastifyInstance } from 'fastify';
2
- import { T as TaujsConfig, S as ServiceRegistry, D as DebugConfig, B as BaseLogger, C as CoreAppConfig } from './Config-2Loymxo2.js';
2
+ import { T as TaujsConfig, S as ServiceRegistry, D as DebugConfig, B as BaseLogger, C as CoreAppConfig } from './Config-BODjH3XQ.js';
3
3
  import { InlineConfig } from 'vite';
4
4
 
5
5
  type NetResolved = {
package/dist/index.js CHANGED
@@ -785,7 +785,7 @@ var resolveLogs = (logger) => logger ?? noopLogger;
785
785
 
786
786
  // src/core/services/DataServices.ts
787
787
  function createCaller(registry, ctx) {
788
- return (serviceName, methodName, args) => callServiceMethod(registry, serviceName, methodName, args ?? {}, ctx);
788
+ return ((serviceName, methodName, args) => callServiceMethod(registry, serviceName, methodName, args ?? {}, ctx));
789
789
  }
790
790
  function ensureServiceCaller(registry, ctx) {
791
791
  if (!ctx.call) ctx.call = createCaller(registry, ctx);
@@ -896,7 +896,10 @@ var fetchInitialData = async (attr, params, serviceRegistry, ctx, callServiceMet
896
896
  };
897
897
  ensureServiceCaller(serviceRegistry, ctxForData);
898
898
  try {
899
- const result = await dataHandler(params, ctxForData);
899
+ const result = await dataHandler(
900
+ params,
901
+ ctxForData
902
+ );
900
903
  if (isServiceDescriptor(result)) {
901
904
  const { serviceName, serviceMethod, args } = result;
902
905
  return callServiceMethodImpl(serviceRegistry, serviceName, serviceMethod, args ?? {}, ctxForData);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taujs/server",
3
- "version": "0.5.9",
3
+ "version": "0.6.0",
4
4
  "description": "τjs [ taujs ]",
5
5
  "author": "John Smith | Aoede <taujs@aoede.uk.net> (https://www.aoede.uk.net)",
6
6
  "license": "MIT",