revojs 0.1.8 → 0.1.10

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> {
@@ -122,6 +125,7 @@ declare class Router extends Radix<Route> implements Middleware {
122
125
  }
123
126
  declare function defineRoute<T extends Route>(route: T): T;
124
127
  declare function defineMiddleware<T extends Middleware>(middleware: T): T;
128
+ declare function defineException<T extends Exception>(exception: T): T;
125
129
  declare function useRouter(scope: Scope): RouterContext;
126
130
  declare function useServer<T extends Context>(scope: Scope): ServerContext<T>;
127
131
  declare function useUrl(scope: Scope, base?: string): URL;
@@ -157,13 +161,16 @@ type Virtual = (environment: Environment) => undefined | string | Promise<string
157
161
  interface DevelopmentConfig {
158
162
  middlewares: Array<Middleware>;
159
163
  }
164
+ interface BuildConfig {
165
+ externals: Array<string>;
166
+ }
160
167
  interface Config {
161
168
  modules: Array<Module>;
162
169
  client?: string;
163
170
  server?: string;
164
- externals: Array<string>;
165
171
  sources: Record<string, Source>;
166
172
  development: DevelopmentConfig;
173
+ build: BuildConfig;
167
174
  }
168
175
  interface Module {
169
176
  config?: Mergeable<Config>;
@@ -172,7 +179,7 @@ interface Module {
172
179
  interface Source {
173
180
  match: string;
174
181
  entries: Array<string>;
175
- suffix?: string;
182
+ resolve?: (path: string) => string;
176
183
  }
177
184
  interface App {
178
185
  config: Config;
@@ -186,4 +193,4 @@ declare const CLIENT = "client";
186
193
  //#region src/client/index.d.ts
187
194
  declare function $fetch<T>(scope: Scope, input: string | URL, options?: RequestInit): Promise<T>;
188
195
  //#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 };
196
+ 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, defineException, 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
@@ -88,6 +88,9 @@ function defineRoute(route) {
88
88
  function defineMiddleware(middleware) {
89
89
  return middleware;
90
90
  }
91
+ function defineException(exception) {
92
+ return exception;
93
+ }
91
94
  function useRouter(scope) {
92
95
  return scope.getContext(ROUTER_CONTEXT);
93
96
  }
@@ -132,8 +135,8 @@ function setCookie(scope, name, value, options) {
132
135
  if (options?.priority) cookie += `; Priority=${options.priority}`;
133
136
  if (options?.sameSite) cookie += `; SameSite=${options.sameSite}`;
134
137
  if (options?.secure) cookie += `; Secure`;
135
- if (import.meta.client) document.cookie = cookie;
136
- else response.headers.append("Set-Cookie", cookie);
138
+ if (import.meta.server) response.headers.append("Set-Cookie", cookie);
139
+ else document.cookie = cookie;
137
140
  }
138
141
  function getState(scope, name) {
139
142
  if (import.meta.server) {
@@ -203,9 +206,7 @@ function invoke(scope, pipeline, index = 0) {
203
206
  }
204
207
  async function createServer() {
205
208
  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);
209
+ const middlewares = new Array();
209
210
  const assets = await import("#virtual/assets").then((module) => Object.entries(module.default));
210
211
  for (const [path, asset] of assets) router.use(`GET/${path}`, defineRoute({ async fetch(scope) {
211
212
  const { response } = useServer(scope);
@@ -217,12 +218,24 @@ async function createServer() {
217
218
  const [name, method] = toRoutePath(path);
218
219
  router.use(method?.toUpperCase() + name, route);
219
220
  }
220
- pipeline.push(router);
221
+ const exceptions = await import("#virtual/exceptions").then((module) => Object.values(module.default));
222
+ middlewares.push(defineMiddleware({ async fetch(scope, next) {
223
+ try {
224
+ return await next?.();
225
+ } catch (value) {
226
+ for (const exception of exceptions) {
227
+ const result = exception.fetch(scope, value);
228
+ if (result) return result;
229
+ }
230
+ if (value instanceof Response) return value;
231
+ }
232
+ } }));
233
+ middlewares.push(router);
221
234
  return {
222
235
  router,
223
- pipeline,
236
+ middlewares,
224
237
  async fetch(scope) {
225
- return await invoke(scope, pipeline) ?? sendText(scope, "NOT_FOUND", { status: 404 });
238
+ return await invoke(scope, middlewares) ?? sendText(scope, "NOT_FOUND", { status: 404 });
226
239
  }
227
240
  };
228
241
  }
@@ -324,7 +337,7 @@ function parseSchema(scope, schema, value) {
324
337
  }
325
338
  function mergeObjects(base, input) {
326
339
  if (input === null || input === void 0) return mergeObjects(base, {});
327
- const object = structuredClone(input);
340
+ const object = Object.assign({}, input);
328
341
  for (const key in base) {
329
342
  if (key === "__proto__" || key === "constructor") continue;
330
343
  const value = base[key];
@@ -341,23 +354,23 @@ function mergeObjects(base, input) {
341
354
  function createApp(inputConfig) {
342
355
  let config = mergeObjects(inputConfig, {
343
356
  modules: [],
344
- externals: [],
345
357
  sources: {
346
358
  assets: {
347
359
  match: "**/*",
348
360
  entries: ["./public"],
349
- suffix: "?raw"
361
+ resolve: (path) => path + "?raw"
350
362
  },
351
363
  routes: {
352
364
  match: "**/*.{js,ts}",
353
365
  entries: ["./routes"]
354
366
  },
355
- middlewares: {
367
+ exceptions: {
356
368
  match: "**/*.{js,ts}",
357
- entries: ["./middlewares"]
369
+ entries: ["./exceptions"]
358
370
  }
359
371
  },
360
- development: { middlewares: [] }
372
+ development: { middlewares: [] },
373
+ build: { externals: [] }
361
374
  });
362
375
  for (const module of config.modules) config = mergeObjects(config, module.config);
363
376
  return {
@@ -393,4 +406,4 @@ async function $fetch(scope, input, options) {
393
406
  }
394
407
 
395
408
  //#endregion
396
- export { $fetch, CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope, StopEvent, WILDCARD, 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 };
409
+ export { $fetch, CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope, StopEvent, WILDCARD, createApp, createServer, defineContext, defineException, 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
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 {