revojs 0.0.84 → 0.0.86

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.
@@ -11,9 +11,11 @@ export type Module = {
11
11
  };
12
12
  export type ClientConfig = {
13
13
  entry: ClientEntry;
14
+ externals: Array<string>;
14
15
  };
15
16
  export type ServerConfig = {
16
17
  entry: ServerEntry;
18
+ externals: Array<string>;
17
19
  };
18
20
  export type DevelopmentConfig = {
19
21
  middleware: Array<Middleware>;
package/dist/index.d.ts CHANGED
@@ -5,4 +5,5 @@ export * from "./locale";
5
5
  export * from "./radix";
6
6
  export * from "./router";
7
7
  export * from "./runtime";
8
+ export * from "./schema";
8
9
  export * from "./signals";
package/dist/index.js CHANGED
@@ -16,8 +16,14 @@ function createApp(config) {
16
16
  return {
17
17
  config: mergeObjects(config, {
18
18
  modules: [],
19
- client: { entry: "index.html" },
20
- server: { entry: "@revojs/bun/runtime" },
19
+ client: {
20
+ entry: "index.html",
21
+ externals: []
22
+ },
23
+ server: {
24
+ entry: "@revojs/bun/runtime",
25
+ externals: []
26
+ },
21
27
  dev: { middleware: [] }
22
28
  }),
23
29
  virtuals: {}
@@ -399,6 +405,15 @@ function useRuntime(scope) {
399
405
  function useRoute(scope) {
400
406
  return scope.getContext(ROUTE_CONTEXT);
401
407
  }
408
+ async function useRoutes() {
409
+ return await import("#virtual/routes").then((module) => module.default);
410
+ }
411
+ async function useAssets() {
412
+ return await import("#virtual/assets").then((module) => module.default);
413
+ }
414
+ async function useLocales() {
415
+ return await import("#virtual/locales").then((module) => module.default);
416
+ }
402
417
  function defineRoute(route) {
403
418
  return route;
404
419
  }
@@ -441,8 +456,12 @@ async function $fetch(scope, input, options) {
441
456
  function useState(scope, name, value) {
442
457
  const state = createState(value);
443
458
  if (isClient()) {
444
- const element = document.querySelector(`#data-${name}`);
445
- if (element?.textContent) state.value = JSON.parse(element.textContent);
459
+ if (STATES === void 0) {
460
+ const element = document.getElementById("STATES");
461
+ STATES = element?.textContent ? JSON.parse(element.textContent) : {};
462
+ }
463
+ state.value = STATES[name];
464
+ createCompute(scope, () => STATES && (STATES[name] = state.value));
446
465
  }
447
466
  if (isServer()) {
448
467
  const { states } = useRuntime(scope);
@@ -450,24 +469,26 @@ function useState(scope, name, value) {
450
469
  }
451
470
  return state;
452
471
  }
453
- function useAsync(scope, name, invoke, options) {
472
+ function useAsync(scope, name, invoke, defaultOptions) {
454
473
  const { tasks } = useRuntime(scope);
455
474
  const state = useState(scope, name);
456
475
  const isLoading = createState(false);
457
- const execute = async () => {
476
+ const execute = async (options) => {
477
+ const onCatch = options?.catch ?? defaultOptions?.catch;
478
+ const viewTransition = options?.viewTransition ?? defaultOptions?.viewTransition;
458
479
  isLoading.value = true;
459
480
  try {
460
481
  const result = await invoke();
461
- if (options?.viewTransition) await startViewTransition(options.viewTransition, () => state.value = result);
482
+ if (JSON.stringify(state.value ?? {}) !== JSON.stringify(result)) if (viewTransition) await startViewTransition(viewTransition, () => state.value = result);
462
483
  else state.value = result;
463
484
  } catch (error) {
464
- options?.catch?.(error);
485
+ onCatch?.(error);
465
486
  } finally {
466
487
  isLoading.value = false;
467
488
  }
468
489
  return state.value;
469
490
  };
470
- const task = execute();
491
+ const task = execute(defaultOptions);
471
492
  if (isServer()) tasks.push(task);
472
493
  return {
473
494
  state,
@@ -481,18 +502,31 @@ function useFetch(scope, input, options) {
481
502
  async function createRuntime() {
482
503
  const radix = new Radix();
483
504
  const middlewares = new Array();
484
- const routes = await import("#virtual/routes").then((module) => module.default);
505
+ const routes = await useRoutes();
485
506
  for (const path in routes) {
486
507
  const [name, method] = toPath(path);
487
508
  radix.insert((method ?? "GET").toUpperCase() + name, defineRoute({ fetch: async (scope) => {
488
509
  const route = routes[path];
489
510
  if (isRoute(route)) return route.fetch(scope);
490
511
  const { states } = useRuntime(scope);
491
- const content = await renderToString(scope, await import("#virtual/client").then((module) => module.client), { head: { children: ["<!--DATA-->"] } });
492
- return sendHtml(scope, content.replace("<!--DATA-->", Object.entries(states).reduce((data, [name$1, state]) => `${data} <script id="data-${name$1}" type="application/json"> ${JSON.stringify(state.value)} <\/script>`, "")));
512
+ const content = await renderToString(scope, await import("#virtual/client").then((module) => module.client), { head: { children: ["<!--STATES-->"] } });
513
+ const entries = Object.entries(states).reduce((states$1, [name$1, state]) => {
514
+ return {
515
+ ...states$1,
516
+ [name$1]: state.value
517
+ };
518
+ }, {});
519
+ return sendHtml(scope, content.replace("<!--STATES-->", await renderToString(scope, {
520
+ tag: "script",
521
+ attributes: {
522
+ id: "STATES",
523
+ type: "application/json"
524
+ },
525
+ children: [JSON.stringify(entries)]
526
+ })));
493
527
  } }));
494
528
  }
495
- const assets = await import("#virtual/assets").then((module) => module.default);
529
+ const assets = await useAssets();
496
530
  for (const path in assets) radix.insert("GET/" + path, defineRoute({ fetch: async (scope) => {
497
531
  const { response } = useRuntime(scope);
498
532
  let content = assets[path];
@@ -532,6 +566,7 @@ async function createRuntime() {
532
566
  }
533
567
  };
534
568
  }
569
+ let STATES;
535
570
  const RUNTIME_CONTEXT = defineContext("RUNTIME_CONTEXT");
536
571
  const ROUTE_CONTEXT = defineContext("ROUTE_CONTEXT");
537
572
 
@@ -962,19 +997,19 @@ function useLocale(scope, context) {
962
997
  const $ = (key) => {
963
998
  return () => messages.value?.[key] ?? key;
964
999
  };
965
- const date = (date$1, options$1) => {
1000
+ const $date = (date, options$1) => {
966
1001
  const format = new Intl.DateTimeFormat(locale.value, options$1);
967
- if (date$1) return format.format(date$1);
1002
+ if (date) return format.format(date);
968
1003
  };
969
1004
  return {
970
1005
  locale,
971
1006
  messages,
972
1007
  options,
973
1008
  $,
974
- date
1009
+ $date
975
1010
  };
976
1011
  }
977
1012
  const LOCALE_CONTEXT = defineContext("LOCALE_CONTEXT");
978
1013
 
979
1014
  //#endregion
980
- export { $fetch, AfterNavigateEvent, CLIENT, Compute, HOST_CONTEXT, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, ROUTE_CONTEXT, RUNTIME_CONTEXT, Radix, SERVER, Scope, StopEvent, activeCompute, activeViewTransition, components, createApp, createCompute, createElement, createMemo, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isComponent, isCustomElement, isRoute, isServer, isTemplate, mergeObjects, mimeType, onMounted, onViewTransition, preventDefault, provideLocaleContext, provideRouterContext, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, startViewTransition, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, untrack, useAsync, useCookies, useEvent, useFetch, useHost, useLocale, useQuery, useRoute, useRouter, useRuntime, useSetCookies, useState, useUrl };
1015
+ export { $fetch, AfterNavigateEvent, CLIENT, Compute, HOST_CONTEXT, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, ROUTE_CONTEXT, RUNTIME_CONTEXT, Radix, SERVER, STATES, Scope, StopEvent, activeCompute, activeViewTransition, components, createApp, createCompute, createElement, createMemo, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isComponent, isCustomElement, isRoute, isServer, isTemplate, mergeObjects, mimeType, onMounted, onViewTransition, preventDefault, provideLocaleContext, provideRouterContext, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, startViewTransition, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, untrack, useAssets, useAsync, useCookies, useEvent, useFetch, useHost, useLocale, useLocales, useQuery, useRoute, useRouter, useRoutes, useRuntime, useSetCookies, useState, useUrl };
@@ -14,13 +14,13 @@ export declare function provideLocaleContext(scope: Scope, options: LocaleOption
14
14
  messages: State<Record<string, string> | undefined>;
15
15
  options: LocaleOptions;
16
16
  $: (key: string) => () => string | number | symbol;
17
- date: (date?: Date, options?: Intl.DateTimeFormatOptions) => string | undefined;
17
+ $date: (date?: Date, options?: Intl.DateTimeFormatOptions) => string | undefined;
18
18
  };
19
19
  export declare function useLocale<T extends LocaleContext>(scope: Scope, context?: Descriptor<T>): {
20
20
  locale: State<string | undefined>;
21
21
  messages: State<Record<string, string> | undefined>;
22
22
  options: LocaleOptions;
23
23
  $: (key: keyof T["options"]["locales"][keyof T["options"]["locales"]]) => () => string | number | symbol;
24
- date: (date?: Date, options?: Intl.DateTimeFormatOptions) => string | undefined;
24
+ $date: (date?: Date, options?: Intl.DateTimeFormatOptions) => string | undefined;
25
25
  };
26
26
  export declare const LOCALE_CONTEXT: Descriptor<LocaleContext>;
@@ -23,12 +23,15 @@ export type RouteContext = {
23
23
  inputs: State<Record<string, string>>;
24
24
  };
25
25
  export type AsyncOptions<T> = {
26
- viewTransition?: string;
26
+ viewTransition?: false | string;
27
27
  catch?: (error: T) => void | Promise<void>;
28
28
  };
29
29
  export declare function isRoute<T>(value?: T): value is Route & T;
30
30
  export declare function useRuntime<T = Record<string, unknown>>(scope: Scope): RuntimeContext<T>;
31
31
  export declare function useRoute(scope: Scope): RouteContext;
32
+ export declare function useRoutes(): Promise<Record<string, unknown>>;
33
+ export declare function useAssets(): Promise<Record<string, string>>;
34
+ export declare function useLocales(): Promise<Record<string, Record<string, string>>>;
32
35
  export declare function defineRoute(route: Route): Route;
33
36
  export declare function defineMiddleware(middleware: Middleware): Middleware;
34
37
  export declare function fileName(path: string): string | undefined;
@@ -36,16 +39,17 @@ export declare function toPath(value: string): (string | undefined)[];
36
39
  export declare function $fetch<T>(scope: Scope, input: string | URL, options?: RequestInit): Promise<T>;
37
40
  export declare function useState<T>(scope: Scope, name: string): State<T | undefined>;
38
41
  export declare function useState<T>(scope: Scope, name: string, value: T): State<T>;
39
- export declare function useAsync<T, TError = Error>(scope: Scope, name: string, invoke: () => Promise<T>, options?: AsyncOptions<TError>): {
42
+ export declare function useAsync<T, TError = Error>(scope: Scope, name: string, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): {
40
43
  state: State<T | undefined>;
41
44
  isLoading: State<boolean>;
42
- execute: () => Promise<T | undefined>;
45
+ execute: (options?: AsyncOptions<TError>) => Promise<T | undefined>;
43
46
  };
44
47
  export declare function useFetch<T, TError = Error>(scope: Scope, input: string | URL, options?: RequestInit & AsyncOptions<TError>): {
45
48
  state: State<T | undefined>;
46
49
  isLoading: State<boolean>;
47
- execute: () => Promise<T | undefined>;
50
+ execute: (options?: AsyncOptions<TError> | undefined) => Promise<T | undefined>;
48
51
  };
49
52
  export declare function createRuntime(): Promise<Runtime>;
53
+ export declare let STATES: Record<string, unknown>;
50
54
  export declare const RUNTIME_CONTEXT: import("..").Descriptor<RuntimeContext<Record<string, unknown>>>;
51
55
  export declare const ROUTE_CONTEXT: import("..").Descriptor<RouteContext>;
@@ -0,0 +1,21 @@
1
+ export type Issue = {
2
+ readonly message: string;
3
+ };
4
+ export type Success<T> = {
5
+ readonly value: T;
6
+ };
7
+ export type Failure = {
8
+ readonly issues: ReadonlyArray<Issue>;
9
+ };
10
+ export type Result<Output> = Success<Output> | Failure;
11
+ export type Schema<T = unknown, TOutput = T> = {
12
+ readonly "~standard": {
13
+ readonly validate: (value: unknown) => Result<TOutput> | Promise<Result<TOutput>>;
14
+ readonly types?: {
15
+ readonly input: T;
16
+ readonly output: TOutput;
17
+ };
18
+ };
19
+ };
20
+ export type InferInput<T extends Schema> = NonNullable<T["~standard"]["types"]>["input"];
21
+ export type InferOutput<T extends Schema> = NonNullable<T["~standard"]["types"]>["output"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.84",
3
+ "version": "0.0.86",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",