revojs 0.1.8 → 0.1.9

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/dist/index.d.ts CHANGED
@@ -90,9 +90,12 @@ interface Route {
90
90
  interface Middleware {
91
91
  fetch: (scope: Scope, next?: () => Result) => Result;
92
92
  }
93
+ interface Exception {
94
+ fetch: (scope: Scope, exception: unknown) => Result;
95
+ }
93
96
  interface Server {
94
97
  router: Router;
95
- pipeline: Array<Middleware>;
98
+ middlewares: Array<Middleware>;
96
99
  fetch: (scope: Scope) => Promise<Response>;
97
100
  }
98
101
  interface WildcardNode<T> {
@@ -157,13 +160,16 @@ type Virtual = (environment: Environment) => undefined | string | Promise<string
157
160
  interface DevelopmentConfig {
158
161
  middlewares: Array<Middleware>;
159
162
  }
163
+ interface BuildConfig {
164
+ externals: Array<string>;
165
+ }
160
166
  interface Config {
161
167
  modules: Array<Module>;
162
168
  client?: string;
163
169
  server?: string;
164
- externals: Array<string>;
165
170
  sources: Record<string, Source>;
166
171
  development: DevelopmentConfig;
172
+ build: BuildConfig;
167
173
  }
168
174
  interface Module {
169
175
  config?: Mergeable<Config>;
@@ -172,7 +178,7 @@ interface Module {
172
178
  interface Source {
173
179
  match: string;
174
180
  entries: Array<string>;
175
- suffix?: string;
181
+ resolve?: (path: string) => string;
176
182
  }
177
183
  interface App {
178
184
  config: Config;
@@ -186,4 +192,4 @@ declare const CLIENT = "client";
186
192
  //#region src/client/index.d.ts
187
193
  declare function $fetch<T>(scope: Scope, input: string | URL, options?: RequestInit): Promise<T>;
188
194
  //#endregion
189
- export { $fetch, App, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode, StopEvent, Success, Virtual, WILDCARD, WildcardNode, createApp, createServer, defineContext, defineMiddleware, defineRoute, getState, invoke, isFailure, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useQuery, useRouter, useServer, useSetCookies, useUrl };
195
+ export { $fetch, App, BuildConfig, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Exception, Failure, HttpMethod, InferInput, InferOutput, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode, StopEvent, Success, Virtual, WILDCARD, WildcardNode, createApp, createServer, defineContext, defineMiddleware, defineRoute, getState, invoke, isFailure, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useQuery, useRouter, useServer, useSetCookies, useUrl };
package/dist/index.js CHANGED
@@ -203,9 +203,7 @@ function invoke(scope, pipeline, index = 0) {
203
203
  }
204
204
  async function createServer() {
205
205
  const router = new Router();
206
- const pipeline = new Array();
207
- const middlewares = await import("#virtual/middlewares").then((module) => Object.entries(module.default));
208
- for (const [_, middleware] of middlewares) pipeline.push(middleware);
206
+ const middlewares = new Array();
209
207
  const assets = await import("#virtual/assets").then((module) => Object.entries(module.default));
210
208
  for (const [path, asset] of assets) router.use(`GET/${path}`, defineRoute({ async fetch(scope) {
211
209
  const { response } = useServer(scope);
@@ -217,12 +215,24 @@ async function createServer() {
217
215
  const [name, method] = toRoutePath(path);
218
216
  router.use(method?.toUpperCase() + name, route);
219
217
  }
220
- pipeline.push(router);
218
+ const exceptions = await import("#virtual/exceptions").then((module) => Object.values(module.default));
219
+ middlewares.push(defineMiddleware({ async fetch(scope, next) {
220
+ try {
221
+ return await next?.();
222
+ } catch (value) {
223
+ for (const exception of exceptions) {
224
+ const result = exception.fetch(scope, value);
225
+ if (result) return result;
226
+ }
227
+ if (value instanceof Response) return value;
228
+ }
229
+ } }));
230
+ middlewares.push(router);
221
231
  return {
222
232
  router,
223
- pipeline,
233
+ middlewares,
224
234
  async fetch(scope) {
225
- return await invoke(scope, pipeline) ?? sendText(scope, "NOT_FOUND", { status: 404 });
235
+ return await invoke(scope, middlewares) ?? sendText(scope, "NOT_FOUND", { status: 404 });
226
236
  }
227
237
  };
228
238
  }
@@ -324,7 +334,7 @@ function parseSchema(scope, schema, value) {
324
334
  }
325
335
  function mergeObjects(base, input) {
326
336
  if (input === null || input === void 0) return mergeObjects(base, {});
327
- const object = structuredClone(input);
337
+ const object = Object.assign({}, input);
328
338
  for (const key in base) {
329
339
  if (key === "__proto__" || key === "constructor") continue;
330
340
  const value = base[key];
@@ -341,23 +351,23 @@ function mergeObjects(base, input) {
341
351
  function createApp(inputConfig) {
342
352
  let config = mergeObjects(inputConfig, {
343
353
  modules: [],
344
- externals: [],
345
354
  sources: {
346
355
  assets: {
347
356
  match: "**/*",
348
357
  entries: ["./public"],
349
- suffix: "?raw"
358
+ resolve: (path) => path + "?raw"
350
359
  },
351
360
  routes: {
352
361
  match: "**/*.{js,ts}",
353
362
  entries: ["./routes"]
354
363
  },
355
- middlewares: {
364
+ exceptions: {
356
365
  match: "**/*.{js,ts}",
357
- entries: ["./middlewares"]
366
+ entries: ["./exceptions"]
358
367
  }
359
368
  },
360
- development: { middlewares: [] }
369
+ development: { middlewares: [] },
370
+ build: { externals: [] }
361
371
  });
362
372
  for (const module of config.modules) config = mergeObjects(config, module.config);
363
373
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",
@@ -26,12 +26,12 @@ declare module "#virtual/routes" {
26
26
  export default routes;
27
27
  }
28
28
 
29
- declare module "#virtual/middlewares" {
30
- import type { Middleware } from "revojs";
29
+ declare module "#virtual/exceptions" {
30
+ import type { Exception } from "revojs";
31
31
 
32
- const middlewares: Record<string, Middleware>;
32
+ const exceptions: Record<string, Exception>;
33
33
 
34
- export default middlewares;
34
+ export default exceptions;
35
35
  }
36
36
 
37
37
  interface ImportMeta {