revojs 0.0.31 → 0.0.33

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.js CHANGED
@@ -591,6 +591,9 @@ var Radix = class Radix {
591
591
 
592
592
  //#endregion
593
593
  //#region src/runtime/index.ts
594
+ const useRuntime = (scope, context) => {
595
+ return scope.getContext(context ?? RUNTIME_CONTEXT);
596
+ };
594
597
  const defineRoute = (route) => {
595
598
  return route;
596
599
  };
@@ -604,7 +607,7 @@ const toPath = (value) => {
604
607
  return split.length === 3 ? [split.at(0), split.at(1)] : [split.at(0)];
605
608
  };
606
609
  const $fetch = async (scope, input, init) => {
607
- const { event } = scope.getContext(RUNTIME_CONTEXT);
610
+ const { event } = useRuntime(scope);
608
611
  let response;
609
612
  if (event) {
610
613
  const url = new URL(input.toString(), event.request.url);
@@ -683,6 +686,7 @@ const ROUTER_CONTEXT = defineContext("ROUTER_CONTEXT");
683
686
  const createRouter = (options) => {
684
687
  const navigator = new EventTarget();
685
688
  const radix = new Radix();
689
+ const url = createState();
686
690
  const route = createState();
687
691
  const inputs = createState();
688
692
  for (const path in options.routes) {
@@ -693,10 +697,10 @@ const createRouter = (options) => {
693
697
  }
694
698
  }
695
699
  const registerRouterContext = async (scope) => {
696
- const { event } = scope.getContext(RUNTIME_CONTEXT);
700
+ const { event } = useRuntime(scope);
697
701
  const fetch$1 = async () => {
698
- const url = new URL(event ? event.request.url : window.location.href);
699
- const match = radix.match(url.pathname);
702
+ url.value = new URL(event?.request.url ?? window?.location.href);
703
+ const match = radix.match(url.value.pathname);
700
704
  inputs.value = match.inputs;
701
705
  const Page$1 = await match.value?.();
702
706
  if (Page$1) route.value = /* @__PURE__ */ h(Page$1, inputs.value);
@@ -707,6 +711,7 @@ const createRouter = (options) => {
707
711
  scope.setContext(ROUTER_CONTEXT, {
708
712
  options,
709
713
  navigator,
714
+ url,
710
715
  radix,
711
716
  route,
712
717
  inputs
@@ -719,7 +724,7 @@ const createRouter = (options) => {
719
724
  };
720
725
  };
721
726
  const useRouter = (scope, context) => {
722
- const { route, inputs, navigator } = scope.getContext(context ?? ROUTER_CONTEXT);
727
+ const { url, route, inputs, navigator } = scope.getContext(context ?? ROUTER_CONTEXT);
723
728
  const navigate = (path) => {
724
729
  if (isClient()) window.history.pushState(window.history.state, "", path);
725
730
  navigator.dispatchEvent(new NavigateEvent());
@@ -729,6 +734,7 @@ const useRouter = (scope, context) => {
729
734
  navigate(event.currentTarget?.getAttribute("href") ?? "/");
730
735
  };
731
736
  return {
737
+ url,
732
738
  route,
733
739
  inputs,
734
740
  navigator,
@@ -888,4 +894,4 @@ const markdownToSlot = (input, options) => {
888
894
  };
889
895
 
890
896
  //#endregion
891
- export { $fetch, Compute, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, RUNTIME_CONTEXT, Radix, Scope, StopEvent, activeCompute, components, createApp, createCompute, createElement, createEvent, createLocale, createMemo, createRouter, createRuntime, createState, defineComponent, defineContext, defineRoute, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString, useEvent, useLocale, useRouter };
897
+ export { $fetch, Compute, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, RUNTIME_CONTEXT, Radix, Scope, StopEvent, activeCompute, components, createApp, createCompute, createElement, createEvent, createLocale, createMemo, createRouter, createRuntime, createState, defineComponent, defineContext, defineRoute, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString, useEvent, useLocale, useRouter, useRuntime };
@@ -8,6 +8,7 @@ export type RouterOptions<T extends Routes = Routes> = {
8
8
  export type RouterContext<T extends RouterOptions = RouterOptions> = {
9
9
  options: T;
10
10
  navigator: EventTarget;
11
+ url: State<URL | undefined>;
11
12
  radix: Radix<() => Promise<ComponentConstructor<Events, Attributes>>>;
12
13
  route: State<Slot | undefined>;
13
14
  inputs: State<Record<string, string> | undefined>;
@@ -19,6 +20,7 @@ export declare const ROUTER_CONTEXT: Descriptor<RouterContext<RouterOptions<Rout
19
20
  export declare const createRouter: <T extends RouterOptions>(options: T) => {
20
21
  ROUTER_CONTEXT: Descriptor<RouterContext<T>>;
21
22
  registerRouterContext: (scope: Scope) => Promise<{
23
+ url: State<URL | undefined>;
22
24
  route: State<unknown>;
23
25
  inputs: State<Record<string, string> | undefined>;
24
26
  navigator: EventTarget;
@@ -27,6 +29,7 @@ export declare const createRouter: <T extends RouterOptions>(options: T) => {
27
29
  }>;
28
30
  };
29
31
  export declare const useRouter: <T extends RouterContext>(scope: Scope, context?: Descriptor<T>) => {
32
+ url: State<URL | undefined>;
30
33
  route: State<unknown>;
31
34
  inputs: State<Record<string, string> | undefined>;
32
35
  navigator: EventTarget;
@@ -1,6 +1,6 @@
1
1
  import { type Context, type Event, type Handle, type Middleware } from "../http";
2
2
  import { Radix } from "../radix";
3
- import { Scope } from "../signals";
3
+ import { type Descriptor, Scope } from "../signals";
4
4
  export type Route<T> = {
5
5
  fetch: Handle<T>;
6
6
  };
@@ -9,12 +9,14 @@ export type Runtime<T> = {
9
9
  middlewares: Array<Middleware<T>>;
10
10
  fetch: (request: Request, context: Context<T>) => Promise<Response>;
11
11
  };
12
+ export type RuntimeContext<T = Record<string, unknown>> = {
13
+ event?: Event<T>;
14
+ };
15
+ export declare const useRuntime: <T extends RuntimeContext>(scope: Scope, context?: Descriptor<T>) => RuntimeContext<Record<string, unknown>>;
12
16
  export declare const defineRoute: <T>(route: Route<T>) => Route<T>;
13
17
  export declare const fileName: (path: string) => string | undefined;
14
18
  export declare const toPath: (value: string) => (string | undefined)[];
15
19
  export declare const $fetch: <T>(scope: Scope, input: string | URL, init?: RequestInit) => Promise<T>;
16
20
  export declare const getVariables: <T>(event?: Event<T>) => T;
17
21
  export declare const createRuntime: <T>() => Promise<Runtime<T>>;
18
- export declare const RUNTIME_CONTEXT: import("..").Descriptor<{
19
- event?: Event;
20
- }>;
22
+ export declare const RUNTIME_CONTEXT: Descriptor<RuntimeContext<Record<string, unknown>>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  "types": "./dist/index.d.ts",
25
25
  "module": "./dist/index.js",
26
26
  "main": "./dist/index.js",
27
- "files": ["dist", "src/virtual"],
27
+ "files": ["dist", "src/types"],
28
28
  "scripts": {
29
29
  "build": "rolldown -c rolldown.config.ts && tsc",
30
30
  "watch": "rolldown -w -c rolldown.config.ts && tsc --watch"
@@ -0,0 +1,23 @@
1
+ declare module "#virtual/locales" {
2
+ export const locales: Record<string, () => Promise<Record<string, string>>>;
3
+ }
4
+
5
+ declare module "#virtual/assets" {
6
+ export const assets: Record<string, () => Promise<T>>;
7
+ }
8
+
9
+ declare module "#virtual/client" {
10
+ import type { Slot } from "revojs";
11
+
12
+ export const client: Slot;
13
+ }
14
+
15
+ declare module "#virtual/routes" {
16
+ export const routes: Record<string, () => Promise<T>>;
17
+ }
18
+
19
+ declare module "#virtual/runtime" {
20
+ import type { Runtime } from "revojs";
21
+
22
+ export const runtime: Runtime<T>;
23
+ }