arcway 0.3.2 → 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": "arcway",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "description": "A convention-based framework for building modular monoliths with strict domain boundaries.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -124,6 +124,7 @@ async function boot(options) {
124
124
  sessionConfig: config.session,
125
125
  });
126
126
  await apiRouter.init();
127
+ appContext.api = apiRouter;
127
128
  if (fileWatcher) plugins.watch(fileWatcher, { jobRunner });
128
129
 
129
130
  const pagesRouter = new PagesRouter(config, { rootDir, log, mode, fileWatcher, appContext });
package/server/context.js CHANGED
@@ -156,6 +156,7 @@ function buildContext(appContext, extras) {
156
156
  };
157
157
  ctx.callbacks = appContext.callbacks?.bind?.(ctx);
158
158
  ctx.plugins = appContext.plugins?.buildContextProxy?.(ctx);
159
+ ctx.api = appContext.api?.buildContextApi?.(ctx) ?? null;
159
160
  return ctx;
160
161
  }
161
162
 
@@ -71,6 +71,76 @@ class ApiRouter {
71
71
  return matchRoute(this._routes, method, path);
72
72
  }
73
73
 
74
+ async forward({ method: fwdMethod, path: fwdPath, params: fwdParams, body: fwdBody, query: fwdQuery }, callerCtx) {
75
+ const matched = matchRoute(this._routes, fwdMethod, fwdPath);
76
+ if (!matched) {
77
+ return { status: 404, error: { code: ErrorCodes.NOT_FOUND, message: `No route for ${fwdMethod} ${fwdPath}` } };
78
+ }
79
+ const { route, params: matchedParams } = matched;
80
+
81
+ const mergedQuery = { ...(callerCtx.req?.query ?? {}), ...(fwdQuery ?? {}), ...matchedParams, ...(fwdParams ?? {}) };
82
+ const reqBody = fwdBody !== undefined ? fwdBody : callerCtx.req?.body;
83
+ const validated = validateRequestSchema(route.config.schema, mergedQuery, reqBody);
84
+ if (validated.error) {
85
+ return { status: 400, error: validated.error };
86
+ }
87
+
88
+ const reqInfo = {
89
+ id: callerCtx.req?.id,
90
+ ip: callerCtx.req?.ip ?? '127.0.0.1',
91
+ method: fwdMethod,
92
+ path: fwdPath,
93
+ query: validated.query,
94
+ body: validated.body,
95
+ rawBody: callerCtx.req?.rawBody,
96
+ headers: callerCtx.req?.headers ?? {},
97
+ cookies: callerCtx.req?.cookies ?? {},
98
+ session: callerCtx.req?.session,
99
+ };
100
+
101
+ const middlewareFns = getMiddlewareForRoute(this._middleware, route.pattern, fwdMethod);
102
+ const chainedHandler = buildMiddlewareChain(middlewareFns, route.config.handler);
103
+ const ctx = this._buildCtx(reqInfo);
104
+ try {
105
+ const response = await chainedHandler(ctx);
106
+ response.status = response.status ?? (response.error ? 400 : 200);
107
+ return response;
108
+ } catch (err) {
109
+ this._log.error(`Forward handler error in ${fwdMethod} ${route.pattern}`, {
110
+ error: toErrorMessage(err),
111
+ });
112
+ const errorMessage =
113
+ this._mode === 'development'
114
+ ? err instanceof Error
115
+ ? err.stack || err.message
116
+ : String(err)
117
+ : 'An internal error occurred';
118
+ return {
119
+ status: 500,
120
+ error: { code: ErrorCodes.HANDLER_ERROR, message: errorMessage },
121
+ };
122
+ }
123
+ }
124
+
125
+ routesApi() {
126
+ return this._routes.map((route) => ({
127
+ method: route.method,
128
+ pattern: route.pattern,
129
+ tags: route.config._tags,
130
+ name: route.config._name,
131
+ description: route.config._description,
132
+ mcp: route.config._mcp,
133
+ schema: route.config.schema,
134
+ }));
135
+ }
136
+
137
+ buildContextApi(ctx) {
138
+ return {
139
+ forward: (args) => this.forward(args, ctx),
140
+ routes: () => this.routesApi(),
141
+ };
142
+ }
143
+
74
144
  async executeRoute(route, reqInfo) {
75
145
  if (route.config._parsedRateLimit && this._redis) {
76
146
  const { max, windowSec } = route.config._parsedRateLimit;
@@ -68,6 +68,38 @@ function buildRoutesFromModules(entries, { prefix = '', plugin } = {}) {
68
68
  }
69
69
  config._parsedRateLimit = parseRateLimit(rl.limit);
70
70
  }
71
+ if (config.tags !== undefined) {
72
+ if (!Array.isArray(config.tags) || !config.tags.every((t) => typeof t === 'string')) {
73
+ throw new Error(
74
+ `Route ${method} ${urlPattern} (${filePath}) tags must be an array of strings`,
75
+ );
76
+ }
77
+ config._tags = config.tags;
78
+ }
79
+ if (config.name !== undefined) {
80
+ if (typeof config.name !== 'string') {
81
+ throw new Error(
82
+ `Route ${method} ${urlPattern} (${filePath}) name must be a string`,
83
+ );
84
+ }
85
+ config._name = config.name;
86
+ }
87
+ if (config.description !== undefined) {
88
+ if (typeof config.description !== 'string') {
89
+ throw new Error(
90
+ `Route ${method} ${urlPattern} (${filePath}) description must be a string`,
91
+ );
92
+ }
93
+ config._description = config.description;
94
+ }
95
+ if (config.mcp !== undefined) {
96
+ if (typeof config.mcp !== 'object' || config.mcp === null) {
97
+ throw new Error(
98
+ `Route ${method} ${urlPattern} (${filePath}) mcp must be an object`,
99
+ );
100
+ }
101
+ config._mcp = config.mcp;
102
+ }
71
103
  routes.push({
72
104
  method,
73
105
  pattern: urlPattern,