@taujs/server 0.3.0 → 0.3.2

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/README.md CHANGED
@@ -85,10 +85,10 @@ Integral to τjs is its internal routing:
85
85
 
86
86
  In ensuring a particular 'route' receives data for hydration there are two options:
87
87
 
88
- 1. An HTTP call syntactically not unlike 'fetch' providing params to a 'fetch' call
89
- 2. Internal service call returning data as per your architecture
88
+ 1. Internal service call returning data as per your architecture
89
+ 2. An HTTP call from your app passing resolved data to @taujs/server
90
90
 
91
- In supporting Option 2. there is a registry of services. More detail in 'Service Registry'.
91
+ In supporting Option 1. there is a registry of services. More detail in 'Service Registry'.
92
92
 
93
93
  Each routes 'path' is a simple URL regex as per below examples.
94
94
 
@@ -27,7 +27,7 @@ declare const TEMPLATE: {
27
27
  *
28
28
  * Licensed under the MIT License — attribution appreciated.
29
29
  * Part of the taujs [ τjs ] system for declarative, build-time orchestration of microfrontend applications,
30
- * including SSR, streaming, and middleware composition.
30
+ * including CSR, SSR, streaming, and middleware composition.
31
31
  */
32
32
 
33
33
  declare const createMaps: () => {
@@ -79,14 +79,6 @@ type RenderCallbacks = {
79
79
  onFinish: (initialDataResolved: unknown) => void;
80
80
  onError: (error: unknown) => void;
81
81
  };
82
- type FetchConfig = {
83
- url?: string;
84
- options: RequestInit & {
85
- params?: Record<string, unknown>;
86
- };
87
- serviceName?: string;
88
- serviceMethod?: string;
89
- };
90
82
  type SSRManifest = {
91
83
  [key: string]: string[];
92
84
  };
@@ -118,22 +110,28 @@ type BaseMiddleware = {
118
110
  strategy?: string;
119
111
  };
120
112
  };
113
+ type ServiceCall = {
114
+ serviceName: string;
115
+ serviceMethod: string;
116
+ args?: Record<string, unknown>;
117
+ };
118
+ type DataResult = Record<string, unknown> | ServiceCall;
119
+ type DataHandler<Params> = (params: Params, ctx: {
120
+ headers: Record<string, string>;
121
+ [key: string]: unknown;
122
+ }) => Promise<DataResult>;
121
123
  type RouteAttributes<Params = {}, Middleware = BaseMiddleware> = {
122
124
  render: 'ssr';
123
125
  hydrate?: boolean;
124
126
  meta?: Record<string, unknown>;
125
127
  middleware?: Middleware;
126
- fetch?: (params?: Params, options?: RequestInit & {
127
- params?: Record<string, unknown>;
128
- }) => Promise<FetchConfig>;
128
+ data?: DataHandler<Params>;
129
129
  } | {
130
130
  render: 'streaming';
131
131
  hydrate?: never;
132
132
  meta: Record<string, unknown>;
133
133
  middleware?: Middleware;
134
- fetch?: (params?: Params, options?: RequestInit & {
135
- params?: Record<string, unknown>;
136
- }) => Promise<FetchConfig>;
134
+ data?: DataHandler<Params>;
137
135
  };
138
136
  type Route<Params = {}> = {
139
137
  attr?: RouteAttributes<Params>;
@@ -147,4 +145,4 @@ interface InitialRouteParams extends Record<string, unknown> {
147
145
  type RouteParams = InitialRouteParams & Record<string, unknown>;
148
146
  type RoutePathsAndAttributes<Params = {}> = Omit<Route<Params>, 'element'>;
149
147
 
150
- export { type BaseMiddleware as B, type Config as C, type FetchConfig as F, type InitialRouteParams as I, type ManifestEntry as M, type NamedService as N, type ProcessedConfig as P, type Route as R, SSRServer as S, TEMPLATE as T, type RouteParams as a, type RouteAttributes as b, createMaps as c, type SSRServerOptions as d, type ServiceMethod as e, type ServiceRegistry as f, type RenderCallbacks as g, type SSRManifest as h, type Manifest as i, type RenderSSR as j, type RenderStream as k, type RenderModule as l, type RoutePathsAndAttributes as m, type CSPDirectives as n, type CSPOptions as o, processConfigs as p, defaultGenerateCSP as q, generateNonce as r, createCSPHook as s, getRequestNonce as t, applyCSP as u };
148
+ export { type BaseMiddleware as B, type Config as C, type DataResult as D, type InitialRouteParams as I, type ManifestEntry as M, type NamedService as N, type ProcessedConfig as P, type Route as R, SSRServer as S, TEMPLATE as T, type RouteParams as a, type RouteAttributes as b, createMaps as c, type SSRServerOptions as d, type ServiceMethod as e, type ServiceRegistry as f, type RenderCallbacks as g, type SSRManifest as h, type Manifest as i, type RenderSSR as j, type RenderStream as k, type RenderModule as l, type ServiceCall as m, type DataHandler as n, type RoutePathsAndAttributes as o, processConfigs as p, type CSPDirectives as q, type CSPOptions as r, defaultGenerateCSP as s, generateNonce as t, createCSPHook as u, getRequestNonce as v, applyCSP as w };
package/dist/build.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AppConfig } from './config.js';
2
2
  import 'vite';
3
- import './SSRServer-CmMH3qwx.js';
3
+ import './SSRServer-D_82uJ06.js';
4
4
  import 'node:http';
5
5
  import 'fastify';
6
6
 
@@ -11,7 +11,7 @@ import 'fastify';
11
11
  *
12
12
  * Licensed under the MIT License — attribution appreciated.
13
13
  * Part of the taujs [ τjs ] system for declarative, build-time orchestration of microfrontend applications,
14
- * including SSR, streaming, and middleware composition.
14
+ * including CSR, SSR, streaming, and middleware composition.
15
15
  */
16
16
 
17
17
  declare function taujsBuild({ configs, projectRoot, clientBaseDir, isSSRBuild, }: {
package/dist/build.js CHANGED
@@ -406,32 +406,25 @@ var callServiceMethod = async (registry, serviceName, methodName, params) => {
406
406
  throw new Error(`Expected object response from ${String(serviceName)}.${String(methodName)}, but got ${typeof data}`);
407
407
  return data;
408
408
  };
409
- var fetchData = async ({ url, options }) => {
410
- if (url) {
411
- const response = await fetch(url, options);
412
- if (!response.ok) throw new Error(`Failed to fetch data from ${url}`);
413
- const data = await response.json();
414
- if (typeof data !== "object" || data === null) throw new Error(`Expected object response from ${url}, but got ${typeof data}`);
415
- return data;
416
- }
417
- throw new Error("URL must be provided to fetch data");
409
+ var isServiceDescriptor = (obj) => {
410
+ if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return false;
411
+ const maybe = obj;
412
+ return typeof maybe.serviceName === "string" && typeof maybe.serviceMethod === "string";
418
413
  };
419
- var fetchInitialData = async (attr, params, serviceRegistry) => {
420
- if (attr && typeof attr.fetch === "function") {
421
- return attr.fetch(params, {
422
- headers: { "Content-Type": "application/json" },
423
- params
424
- }).then(async (data) => {
425
- if (data.serviceName && data.serviceMethod && typeof data.serviceName === "string" && typeof data.serviceMethod === "string")
426
- return await callServiceMethod(serviceRegistry, data.serviceName, data.serviceMethod, data.options?.params ?? {});
427
- if (data.url && typeof data.url === "string") return await fetchData(data);
428
- throw new Error("Invalid fetch configuration: must have either serviceName+serviceMethod or url");
429
- }).catch((error) => {
430
- console.error("Error fetching initial data:", error);
431
- throw error;
432
- });
414
+ var fetchInitialData = async (attr, params, serviceRegistry, ctx = { headers: {} }, callServiceMethodImpl = callServiceMethod) => {
415
+ if (!attr?.data || typeof attr.data !== "function") return {};
416
+ const result = await attr.data(params, ctx);
417
+ if (isServiceDescriptor(result)) {
418
+ const { serviceName, serviceMethod, args } = result;
419
+ if (serviceRegistry[serviceName]?.[serviceMethod]) {
420
+ return await callServiceMethodImpl(serviceRegistry, serviceName, serviceMethod, args ?? {});
421
+ }
422
+ throw new Error(`Invalid service: serviceName=${String(serviceName)}, method=${String(serviceMethod)}`);
423
+ }
424
+ if (typeof result === "object" && result !== null) {
425
+ return result;
433
426
  }
434
- return Promise.resolve({});
427
+ throw new Error("Invalid result from attr.data");
435
428
  };
436
429
  var matchRoute = (url, renderRoutes) => {
437
430
  for (const route of renderRoutes) {
package/dist/config.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { PluginOption } from 'vite';
2
- import { R as Route, a as RouteParams, b as RouteAttributes } from './SSRServer-CmMH3qwx.js';
2
+ import { R as Route, a as RouteParams, b as RouteAttributes } from './SSRServer-D_82uJ06.js';
3
3
  import 'node:http';
4
4
  import 'fastify';
5
5
 
@@ -10,7 +10,7 @@ import 'fastify';
10
10
  *
11
11
  * Licensed under the MIT License — attribution appreciated.
12
12
  * Part of the taujs [ τjs ] system for declarative, build-time orchestration of microfrontend applications,
13
- * including SSR, streaming, and middleware composition.
13
+ * including CSR, SSR, streaming, and middleware composition.
14
14
  */
15
15
 
16
16
  type AppRoute = Omit<Route<RouteParams>, 'appId'> & {
@@ -32,6 +32,6 @@ declare const extractBuildConfigs: (config: {
32
32
  plugins?: PluginOption[];
33
33
  }[];
34
34
  }) => AppConfig[];
35
- declare function extractRoutes(taujsConfig: TaujsConfig): Route<RouteParams>[];
35
+ declare const extractRoutes: (taujsConfig: TaujsConfig) => Route<RouteParams>[];
36
36
 
37
37
  export { type AppConfig, type AppRoute, type TaujsConfig, extractBuildConfigs, extractRoutes };
package/dist/config.js CHANGED
@@ -107,7 +107,7 @@ var extractBuildConfigs = (config) => {
107
107
  plugins
108
108
  }));
109
109
  };
110
- function extractRoutes(taujsConfig) {
110
+ var extractRoutes = (taujsConfig) => {
111
111
  console.log(import_picocolors.default.bold("Preparing taujs [ \u03C4js ]"));
112
112
  const t0 = performance.now();
113
113
  try {
@@ -136,10 +136,10 @@ function extractRoutes(taujsConfig) {
136
136
  console.log(import_picocolors.default.red("Failed to prepare routes"));
137
137
  throw err;
138
138
  }
139
- }
140
- function computeScore(path) {
141
- return path.split("/").filter(Boolean).length;
142
- }
139
+ };
140
+ var computeScore = (path) => {
141
+ return path.split("/").filter(Boolean).reduce((score, segment) => score + (segment.startsWith(":") ? 1 : 10), 0);
142
+ };
143
143
  export {
144
144
  extractBuildConfigs,
145
145
  extractRoutes
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FastifyReply } from 'fastify';
2
- export { B as BaseMiddleware, C as Config, F as FetchConfig, I as InitialRouteParams, i as Manifest, M as ManifestEntry, N as NamedService, P as ProcessedConfig, g as RenderCallbacks, l as RenderModule, j as RenderSSR, k as RenderStream, R as Route, b as RouteAttributes, a as RouteParams, m as RoutePathsAndAttributes, h as SSRManifest, S as SSRServer, d as SSRServerOptions, e as ServiceMethod, f as ServiceRegistry, T as TEMPLATE, c as createMaps, p as processConfigs } from './SSRServer-CmMH3qwx.js';
2
+ export { B as BaseMiddleware, C as Config, n as DataHandler, D as DataResult, I as InitialRouteParams, i as Manifest, M as ManifestEntry, N as NamedService, P as ProcessedConfig, g as RenderCallbacks, l as RenderModule, j as RenderSSR, k as RenderStream, R as Route, b as RouteAttributes, a as RouteParams, o as RoutePathsAndAttributes, h as SSRManifest, S as SSRServer, d as SSRServerOptions, m as ServiceCall, e as ServiceMethod, f as ServiceRegistry, T as TEMPLATE, c as createMaps, p as processConfigs } from './SSRServer-D_82uJ06.js';
3
3
  import 'node:http';
4
4
  import 'vite';
5
5
 
@@ -9,7 +9,7 @@ declare module 'fastify' {
9
9
  }
10
10
  interface FastifyInstance {
11
11
  /**
12
- * Optional authentication hook to be used by the TauJS SSRServer.
12
+ * Optional authentication hook to be used by the taujs SSRServer.
13
13
  * This method must be decorated by the user when using auth middleware in `taujs.config.ts`.
14
14
  *
15
15
  * Example usage:
package/dist/index.js CHANGED
@@ -404,32 +404,25 @@ var callServiceMethod = async (registry, serviceName, methodName, params) => {
404
404
  throw new Error(`Expected object response from ${String(serviceName)}.${String(methodName)}, but got ${typeof data}`);
405
405
  return data;
406
406
  };
407
- var fetchData = async ({ url, options }) => {
408
- if (url) {
409
- const response = await fetch(url, options);
410
- if (!response.ok) throw new Error(`Failed to fetch data from ${url}`);
411
- const data = await response.json();
412
- if (typeof data !== "object" || data === null) throw new Error(`Expected object response from ${url}, but got ${typeof data}`);
413
- return data;
414
- }
415
- throw new Error("URL must be provided to fetch data");
407
+ var isServiceDescriptor = (obj) => {
408
+ if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return false;
409
+ const maybe = obj;
410
+ return typeof maybe.serviceName === "string" && typeof maybe.serviceMethod === "string";
416
411
  };
417
- var fetchInitialData = async (attr, params, serviceRegistry) => {
418
- if (attr && typeof attr.fetch === "function") {
419
- return attr.fetch(params, {
420
- headers: { "Content-Type": "application/json" },
421
- params
422
- }).then(async (data) => {
423
- if (data.serviceName && data.serviceMethod && typeof data.serviceName === "string" && typeof data.serviceMethod === "string")
424
- return await callServiceMethod(serviceRegistry, data.serviceName, data.serviceMethod, data.options?.params ?? {});
425
- if (data.url && typeof data.url === "string") return await fetchData(data);
426
- throw new Error("Invalid fetch configuration: must have either serviceName+serviceMethod or url");
427
- }).catch((error) => {
428
- console.error("Error fetching initial data:", error);
429
- throw error;
430
- });
412
+ var fetchInitialData = async (attr, params, serviceRegistry, ctx = { headers: {} }, callServiceMethodImpl = callServiceMethod) => {
413
+ if (!attr?.data || typeof attr.data !== "function") return {};
414
+ const result = await attr.data(params, ctx);
415
+ if (isServiceDescriptor(result)) {
416
+ const { serviceName, serviceMethod, args } = result;
417
+ if (serviceRegistry[serviceName]?.[serviceMethod]) {
418
+ return await callServiceMethodImpl(serviceRegistry, serviceName, serviceMethod, args ?? {});
419
+ }
420
+ throw new Error(`Invalid service: serviceName=${String(serviceName)}, method=${String(serviceMethod)}`);
421
+ }
422
+ if (typeof result === "object" && result !== null) {
423
+ return result;
431
424
  }
432
- return Promise.resolve({});
425
+ throw new Error("Invalid result from attr.data");
433
426
  };
434
427
  var matchRoute = (url, renderRoutes) => {
435
428
  for (const route of renderRoutes) {
@@ -1,4 +1,4 @@
1
1
  import 'fastify';
2
- export { n as CSPDirectives, o as CSPOptions, u as applyCSP, s as createCSPHook, q as defaultGenerateCSP, r as generateNonce, t as getRequestNonce } from '../SSRServer-CmMH3qwx.js';
2
+ export { q as CSPDirectives, r as CSPOptions, w as applyCSP, u as createCSPHook, s as defaultGenerateCSP, t as generateNonce, v as getRequestNonce } from '../SSRServer-D_82uJ06.js';
3
3
  import 'node:http';
4
4
  import 'vite';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taujs/server",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "taujs [ τjs ]",
5
5
  "author": "John Smith | Aoede <taujs@aoede.uk.net> (https://www.aoede.uk.net)",
6
6
  "license": "MIT",