dtwo 3.0.0 → 3.1.0-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.
Files changed (61) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +6 -2
  3. package/bin/dtwo.mjs +1 -1
  4. package/config.d.ts +1 -1
  5. package/dist/app/compat/idle-callback.d.ts +2 -0
  6. package/dist/app/compat/idle-callback.mjs +13 -0
  7. package/dist/app/components/dtwo-island.d.ts +33 -0
  8. package/dist/app/components/dtwo-island.mjs +62 -0
  9. package/dist/app/components/dtwo-link.d.ts +3 -3
  10. package/dist/app/components/dtwo-link.mjs +26 -28
  11. package/dist/app/components/dtwo-loading-indicator.d.ts +3 -3
  12. package/dist/app/components/dtwo-loading-indicator.mjs +8 -7
  13. package/dist/app/components/dtwo-root.kdu +10 -2
  14. package/dist/app/components/island-renderer.d.ts +20 -0
  15. package/dist/app/components/island-renderer.mjs +26 -0
  16. package/dist/app/components/layout.d.ts +1 -1
  17. package/dist/app/components/layout.mjs +12 -3
  18. package/dist/app/components/utils.mjs +1 -0
  19. package/dist/app/composables/asyncData.d.ts +4 -1
  20. package/dist/app/composables/asyncData.mjs +15 -2
  21. package/dist/app/composables/component.d.ts +1 -1
  22. package/dist/app/composables/cookie.d.ts +5 -4
  23. package/dist/app/composables/cookie.mjs +8 -2
  24. package/dist/app/composables/error.d.ts +1 -1
  25. package/dist/app/composables/fetch.d.ts +6 -6
  26. package/dist/app/composables/fetch.mjs +2 -1
  27. package/dist/app/composables/index.d.ts +2 -1
  28. package/dist/app/composables/index.mjs +2 -1
  29. package/dist/app/composables/ready.d.ts +1 -0
  30. package/dist/app/composables/ready.mjs +12 -0
  31. package/dist/app/composables/router.d.ts +1 -1
  32. package/dist/app/composables/router.mjs +5 -0
  33. package/dist/app/composables/ssr.d.ts +1 -1
  34. package/dist/app/config.mjs +1 -0
  35. package/dist/app/dtwo.d.ts +5 -4
  36. package/dist/app/plugins/cross-origin-prefetch.client.mjs +19 -14
  37. package/dist/app/plugins/router.mjs +13 -5
  38. package/dist/app/types/augments.d.ts +1 -1
  39. package/dist/components/runtime/server-component.d.ts +3 -0
  40. package/dist/components/runtime/server-component.mjs +87 -0
  41. package/dist/core/runtime/hole/renderer.d.ts +19 -0
  42. package/dist/core/runtime/hole/renderer.mjs +71 -9
  43. package/dist/head/runtime/components.d.ts +14 -14
  44. package/dist/head/runtime/components.mjs +12 -1
  45. package/dist/head/runtime/composables.d.ts +17 -0
  46. package/dist/head/runtime/composables.mjs +9 -0
  47. package/dist/head/runtime/lib/kduse-head.plugin.mjs +2 -1
  48. package/dist/index.mjs +298 -77
  49. package/dist/pages/runtime/composables.d.ts +1 -1
  50. package/dist/pages/runtime/page.d.ts +5 -1
  51. package/dist/pages/runtime/page.mjs +6 -3
  52. package/dist/pages/runtime/plugins/prefetch.client.mjs +36 -0
  53. package/dist/pages/runtime/{router.d.ts → plugins/router.d.ts} +1 -1
  54. package/dist/pages/runtime/{router.mjs → plugins/router.mjs} +11 -8
  55. package/dist/pages/runtime/utils.d.ts +1 -1
  56. package/dist/pages/runtime/utils.mjs +1 -1
  57. package/dist/pages/runtime/validate.mjs +4 -4
  58. package/package.json +51 -51
  59. package/types.d.ts +3 -0
  60. package/dist/head/runtime/lib/kdu-meta.plugin.mjs +0 -31
  61. /package/dist/{head/runtime/lib/kdu-meta.plugin.d.ts → pages/runtime/plugins/prefetch.client.d.ts} +0 -0
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 - Dtwo Project
3
+ Copyright (c) 2022 - Dtwo Team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
- # Dtwo 3
1
+ # Dtwo
2
2
 
3
- > The core of Dtwo 3
3
+ Dtwo 3: **The Intuitive Web Framework** ✨
4
+
5
+ ## License
6
+
7
+ [MIT](./LICENSE)
package/bin/dtwo.mjs CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import '@dtwo/cli/cli'
2
+ import 'dtwc/cli'
package/config.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DtwoConfig } from '@dtwo/schema'
1
+ import type { DtwoConfig } from '@dtwo/schema'
2
2
  export { DtwoConfig } from '@dtwo/schema'
3
3
 
4
4
  export declare function defineDtwoConfig(config: DtwoConfig): DtwoConfig
@@ -0,0 +1,2 @@
1
+ export declare const requestIdleCallback: Window['requestIdleCallback'];
2
+ export declare const cancelIdleCallback: Window['cancelIdleCallback'];
@@ -0,0 +1,13 @@
1
+ export const requestIdleCallback = process.server ? void 0 : globalThis.requestIdleCallback || ((cb) => {
2
+ const start = Date.now();
3
+ const idleDeadline = {
4
+ didTimeout: false,
5
+ timeRemaining: () => Math.max(0, 50 - (Date.now() - start))
6
+ };
7
+ return setTimeout(() => {
8
+ cb(idleDeadline);
9
+ }, 1);
10
+ });
11
+ export const cancelIdleCallback = process.server ? null : globalThis.cancelIdleCallback || ((id) => {
12
+ clearTimeout(id);
13
+ });
@@ -0,0 +1,33 @@
1
+ declare const _default: import("kdu").DefineComponent<{
2
+ name: {
3
+ type: StringConstructor;
4
+ required: true;
5
+ };
6
+ props: {
7
+ type: ObjectConstructor;
8
+ default: () => undefined;
9
+ };
10
+ context: {
11
+ type: ObjectConstructor;
12
+ default: () => {};
13
+ };
14
+ }, () => import("kdu").KNode<import("kdu").RendererNode, import("kdu").RendererElement, {
15
+ [key: string]: any;
16
+ }>, unknown, {}, {}, import("kdu").ComponentOptionsMixin, import("kdu").ComponentOptionsMixin, {}, string, import("kdu").KNodeProps & import("kdu").AllowedComponentProps & import("kdu").ComponentCustomProps, Readonly<import("kdu").ExtractPropTypes<{
17
+ name: {
18
+ type: StringConstructor;
19
+ required: true;
20
+ };
21
+ props: {
22
+ type: ObjectConstructor;
23
+ default: () => undefined;
24
+ };
25
+ context: {
26
+ type: ObjectConstructor;
27
+ default: () => {};
28
+ };
29
+ }>>, {
30
+ props: Record<string, any>;
31
+ context: Record<string, any>;
32
+ }>;
33
+ export default _default;
@@ -0,0 +1,62 @@
1
+ import { defineComponent, createStaticKNode, computed, ref, watch } from "kdu";
2
+ import { debounce } from "perfect-debounce";
3
+ import { hash } from "ohash";
4
+ import { appendHeader } from "h3";
5
+ import { useHead, useDtwoApp, useRequestEvent } from "#app";
6
+ const pKey = "_islandPromises";
7
+ export default defineComponent({
8
+ name: "DtwoIsland",
9
+ props: {
10
+ name: {
11
+ type: String,
12
+ required: true
13
+ },
14
+ props: {
15
+ type: Object,
16
+ default: () => void 0
17
+ },
18
+ context: {
19
+ type: Object,
20
+ default: () => ({})
21
+ }
22
+ },
23
+ async setup(props) {
24
+ const dtwoApp = useDtwoApp();
25
+ const hashId = computed(() => hash([props.name, props.props, props.context]));
26
+ const event = useRequestEvent();
27
+ const html = ref("");
28
+ const cHead = ref({ link: [], style: [] });
29
+ useHead(cHead);
30
+ function _fetchComponent() {
31
+ const url = `/__dtwo_island/${props.name}:${hashId.value}`;
32
+ if (process.server && process.env.prerender) {
33
+ appendHeader(event, "x-hole-prerender", url);
34
+ }
35
+ return $fetch(url, {
36
+ params: {
37
+ ...props.context,
38
+ props: props.props ? JSON.stringify(props.props) : void 0
39
+ }
40
+ });
41
+ }
42
+ async function fetchComponent() {
43
+ dtwoApp[pKey] = dtwoApp[pKey] || {};
44
+ if (!dtwoApp[pKey][hashId.value]) {
45
+ dtwoApp[pKey][hashId.value] = _fetchComponent().finally(() => {
46
+ delete dtwoApp[pKey][hashId.value];
47
+ });
48
+ }
49
+ const res = await dtwoApp[pKey][hashId.value];
50
+ cHead.value.link = res.head.link;
51
+ cHead.value.style = res.head.style;
52
+ html.value = res.html;
53
+ }
54
+ if (process.client) {
55
+ watch(props, debounce(fetchComponent, 100));
56
+ }
57
+ if (process.server || !dtwoApp.isHydrating) {
58
+ await fetchComponent();
59
+ }
60
+ return () => createStaticKNode(html.value, 1);
61
+ }
62
+ });
@@ -1,4 +1,4 @@
1
- import { DefineComponent } from 'kdu';
1
+ import type { DefineComponent } from 'kdu';
2
2
  import type { RouteLocationRaw } from 'kdu-router';
3
3
  export type DtwoLinkOptions = {
4
4
  componentName?: string;
@@ -23,11 +23,11 @@ export type DtwoLinkProps = {
23
23
  ariaCurrentValue?: string;
24
24
  };
25
25
  export declare function defineDtwoLink(options: DtwoLinkOptions): DefineComponent<DtwoLinkProps, {}, {}, import("kdu").ComputedOptions, import("kdu").MethodOptions, import("kdu").ComponentOptionsMixin, import("kdu").ComponentOptionsMixin, {}, string, import("kdu").KNodeProps & import("kdu").AllowedComponentProps & import("kdu").ComponentCustomProps, Readonly<DtwoLinkProps>, {
26
- to?: any;
27
26
  href?: any;
27
+ to?: any;
28
28
  }>;
29
29
  declare const _default: DefineComponent<DtwoLinkProps, {}, {}, import("kdu").ComputedOptions, import("kdu").MethodOptions, import("kdu").ComponentOptionsMixin, import("kdu").ComponentOptionsMixin, {}, string, import("kdu").KNodeProps & import("kdu").AllowedComponentProps & import("kdu").ComponentCustomProps, Readonly<DtwoLinkProps>, {
30
- to?: any;
31
30
  href?: any;
31
+ to?: any;
32
32
  }>;
33
33
  export default _default;
@@ -1,23 +1,12 @@
1
1
  import { defineComponent, h, ref, resolveComponent, computed, onMounted, onBeforeUnmount } from "kdu";
2
2
  import { hasProtocol } from "ufo";
3
3
  import { preloadRouteComponents } from "../composables/preload.mjs";
4
+ import { onDtwoReady } from "../composables/ready.mjs";
4
5
  import { navigateTo, useRouter } from "../composables/router.mjs";
5
6
  import { useDtwoApp } from "../dtwo.mjs";
7
+ import { cancelIdleCallback, requestIdleCallback } from "../compat/idle-callback.mjs";
6
8
  const firstNonUndefined = (...args) => args.find((arg) => arg !== void 0);
7
9
  const DEFAULT_EXTERNAL_REL_ATTRIBUTE = "noopener noreferrer";
8
- const requestIdleCallback = process.server ? void 0 : globalThis.requestIdleCallback || ((cb) => {
9
- const start = Date.now();
10
- const idleDeadline = {
11
- didTimeout: false,
12
- timeRemaining: () => Math.max(0, 50 - (Date.now() - start))
13
- };
14
- return setTimeout(() => {
15
- cb(idleDeadline);
16
- }, 1);
17
- });
18
- const cancelIdleCallback = process.server ? null : globalThis.cancelIdleCallback || ((id) => {
19
- clearTimeout(id);
20
- });
21
10
  export function defineDtwoLink(options) {
22
11
  const componentName = options.componentName || "DtwoLink";
23
12
  const checkPropConflicts = (props, main, sub) => {
@@ -28,6 +17,7 @@ export function defineDtwoLink(options) {
28
17
  return defineComponent({
29
18
  name: componentName,
30
19
  props: {
20
+ // Routing
31
21
  to: {
32
22
  type: [String, Object],
33
23
  default: void 0,
@@ -38,6 +28,7 @@ export function defineDtwoLink(options) {
38
28
  default: void 0,
39
29
  required: false
40
30
  },
31
+ // Attributes
41
32
  target: {
42
33
  type: String,
43
34
  default: void 0,
@@ -53,6 +44,7 @@ export function defineDtwoLink(options) {
53
44
  default: void 0,
54
45
  required: false
55
46
  },
47
+ // Prefetching
56
48
  prefetch: {
57
49
  type: Boolean,
58
50
  default: void 0,
@@ -63,6 +55,7 @@ export function defineDtwoLink(options) {
63
55
  default: void 0,
64
56
  required: false
65
57
  },
58
+ // Styling
66
59
  activeClass: {
67
60
  type: String,
68
61
  default: void 0,
@@ -78,6 +71,7 @@ export function defineDtwoLink(options) {
78
71
  default: void 0,
79
72
  required: false
80
73
  },
74
+ // Kdu Router's `<RouterLink>` additional props
81
75
  replace: {
82
76
  type: Boolean,
83
77
  default: void 0,
@@ -88,11 +82,13 @@ export function defineDtwoLink(options) {
88
82
  default: void 0,
89
83
  required: false
90
84
  },
85
+ // Edge cases handling
91
86
  external: {
92
87
  type: Boolean,
93
88
  default: void 0,
94
89
  required: false
95
90
  },
91
+ // Slot API
96
92
  custom: {
97
93
  type: Boolean,
98
94
  default: void 0,
@@ -124,24 +120,26 @@ export function defineDtwoLink(options) {
124
120
  const shouldPrefetch = props.prefetch !== false && props.noPrefetch !== true && typeof to.value === "string" && props.target !== "_blank" && !isSlowConnection();
125
121
  if (shouldPrefetch) {
126
122
  const dtwoApp = useDtwoApp();
127
- const observer = useObserver();
128
123
  let idleId;
129
124
  let unobserve = null;
130
125
  onMounted(() => {
131
- idleId = requestIdleCallback(() => {
132
- if (el?.value?.tagName) {
133
- unobserve = observer.observe(el.value, async () => {
134
- unobserve?.();
135
- unobserve = null;
136
- await Promise.all([
137
- dtwoApp.hooks.callHook("link:prefetch", to.value).catch(() => {
138
- }),
139
- !isExternal.value && preloadRouteComponents(to.value, router).catch(() => {
140
- })
141
- ]);
142
- prefetched.value = true;
143
- });
144
- }
126
+ const observer = useObserver();
127
+ onDtwoReady(() => {
128
+ idleId = requestIdleCallback(() => {
129
+ if (el?.value?.tagName) {
130
+ unobserve = observer.observe(el.value, async () => {
131
+ unobserve?.();
132
+ unobserve = null;
133
+ await Promise.all([
134
+ dtwoApp.hooks.callHook("link:prefetch", to.value).catch(() => {
135
+ }),
136
+ !isExternal.value && preloadRouteComponents(to.value, router).catch(() => {
137
+ })
138
+ ]);
139
+ prefetched.value = true;
140
+ });
141
+ }
142
+ });
145
143
  });
146
144
  });
147
145
  onBeforeUnmount(() => {
@@ -12,7 +12,7 @@ declare const _default: import("kdu").DefineComponent<{
12
12
  default: number;
13
13
  };
14
14
  color: {
15
- type: StringConstructor;
15
+ type: (BooleanConstructor | StringConstructor)[];
16
16
  default: string;
17
17
  };
18
18
  }, () => import("kdu").KNode<import("kdu").RendererNode, import("kdu").RendererElement, {
@@ -31,13 +31,13 @@ declare const _default: import("kdu").DefineComponent<{
31
31
  default: number;
32
32
  };
33
33
  color: {
34
- type: StringConstructor;
34
+ type: (BooleanConstructor | StringConstructor)[];
35
35
  default: string;
36
36
  };
37
37
  }>>, {
38
38
  duration: number;
39
+ color: string | boolean;
39
40
  throttle: number;
40
41
  height: number;
41
- color: string;
42
42
  }>;
43
43
  export default _default;
@@ -16,7 +16,7 @@ export default defineComponent({
16
16
  default: 3
17
17
  },
18
18
  color: {
19
- type: String,
19
+ type: [String, Boolean],
20
20
  default: "repeating-linear-gradient(to right,#d7555d 0%,#e5757b 50%,#0047e1 100%)"
21
21
  }
22
22
  },
@@ -40,7 +40,7 @@ export default defineComponent({
40
40
  width: `${indicator.progress.value}%`,
41
41
  height: `${props.height}px`,
42
42
  opacity: indicator.isLoading.value ? 1 : 0,
43
- background: props.color,
43
+ background: props.color || void 0,
44
44
  backgroundSize: `${100 / indicator.progress.value * 100}% auto`,
45
45
  transition: "width 0.1s, height 0.4s, opacity 0.4s",
46
46
  zIndex: 999999
@@ -57,12 +57,13 @@ function useLoadingIndicator(opts) {
57
57
  function start() {
58
58
  clear();
59
59
  progress.value = 0;
60
- isLoading.value = true;
61
- if (opts.throttle) {
62
- if (process.client) {
63
- _throttle = setTimeout(_startTimer, opts.throttle);
64
- }
60
+ if (opts.throttle && process.client) {
61
+ _throttle = setTimeout(() => {
62
+ isLoading.value = true;
63
+ _startTimer();
64
+ }, opts.throttle);
65
65
  } else {
66
+ isLoading.value = true;
66
67
  _startTimer();
67
68
  }
68
69
  }
@@ -1,16 +1,20 @@
1
1
  <template>
2
2
  <Suspense @resolve="onResolve">
3
3
  <ErrorComponent k-if="error" :error="error" />
4
+ <IslandRendererer k-else-if="islandContext" :context="islandContext" />
4
5
  <AppComponent k-else />
5
6
  </Suspense>
6
7
  </template>
7
8
 
8
9
  <script setup>
9
- import { defineAsyncComponent, onErrorCaptured, provide } from 'kdu'
10
+ import { defineAsyncComponent, onErrorCaptured, onServerPrefetch, provide } from 'kdu'
10
11
  import { callWithDtwo, isDtwoError, showError, useError, useRoute, useDtwoApp } from '#app'
11
12
  import AppComponent from '#build/app-component.mjs'
12
13
 
13
14
  const ErrorComponent = defineAsyncComponent(() => import('#build/error-component.mjs').then(r => r.default || r))
15
+ const IslandRendererer = process.server
16
+ ? defineAsyncComponent(() => import('./island-renderer').then(r => r.default || r))
17
+ : () => null
14
18
 
15
19
  const dtwoApp = useDtwoApp()
16
20
  const onResolve = dtwoApp.deferHydration()
@@ -29,7 +33,11 @@ const error = useError()
29
33
  onErrorCaptured((err, target, info) => {
30
34
  dtwoApp.hooks.callHook('kdu:error', err, target, info).catch(hookError => console.error('[dtwo] Error in `kdu:error` hook', hookError))
31
35
  if (process.server || (isDtwoError(err) && (err.fatal || err.unhandled))) {
32
- callWithDtwo(dtwoApp, showError, [err])
36
+ const p = callWithDtwo(dtwoApp, showError, [err])
37
+ onServerPrefetch(() => p)
33
38
  }
34
39
  })
40
+
41
+ // Component islands context
42
+ const { islandContext } = process.server && dtwoApp.ssrContext
35
43
  </script>
@@ -0,0 +1,20 @@
1
+ declare const _default: import("kdu").DefineComponent<{
2
+ context: {
3
+ type: () => {
4
+ name: string;
5
+ props?: Record<string, any>;
6
+ };
7
+ required: true;
8
+ };
9
+ }, () => import("kdu").KNode<import("kdu").RendererNode, import("kdu").RendererElement, {
10
+ [key: string]: any;
11
+ }>[], unknown, {}, {}, import("kdu").ComponentOptionsMixin, import("kdu").ComponentOptionsMixin, {}, string, import("kdu").KNodeProps & import("kdu").AllowedComponentProps & import("kdu").ComponentCustomProps, Readonly<import("kdu").ExtractPropTypes<{
12
+ context: {
13
+ type: () => {
14
+ name: string;
15
+ props?: Record<string, any>;
16
+ };
17
+ required: true;
18
+ };
19
+ }>>, {}>;
20
+ export default _default;
@@ -0,0 +1,26 @@
1
+ import { createBlock, defineComponent, h, Teleport } from "kdu";
2
+ import * as islandComponents from "#build/components.islands.mjs";
3
+ import { createError } from "#app";
4
+ export default defineComponent({
5
+ props: {
6
+ context: {
7
+ type: Object,
8
+ required: true
9
+ }
10
+ },
11
+ async setup(props) {
12
+ const component = islandComponents[props.context.name];
13
+ if (!component) {
14
+ throw createError({
15
+ statusCode: 404,
16
+ statusMessage: `Island component not found: ${JSON.stringify(component)}`
17
+ });
18
+ }
19
+ if (typeof component === "object") {
20
+ await component.__asyncLoader?.();
21
+ }
22
+ return () => [
23
+ createBlock(Teleport, { to: "dtwo-island" }, [h(component || "span", props.context.props)])
24
+ ];
25
+ }
26
+ });
@@ -1,4 +1,4 @@
1
- import { Ref, KNode } from 'kdu';
1
+ import type { Ref, KNode } from 'kdu';
2
2
  declare const _default: import("kdu").DefineComponent<{
3
3
  name: {
4
4
  type: () => string | false | Ref<string | false>;
@@ -5,6 +5,8 @@ import { useRoute as useKduRouterRoute } from "#build/pages";
5
5
  import layouts from "#build/layouts";
6
6
  import { appLayoutTransition as defaultLayoutTransition } from "#build/dtwo.config.mjs";
7
7
  const LayoutLoader = defineComponent({
8
+ name: "LayoutLoader",
9
+ inheritAttrs: false,
8
10
  props: {
9
11
  name: String,
10
12
  ...process.dev ? { hasTransition: Boolean } : {}
@@ -23,14 +25,16 @@ const LayoutLoader = defineComponent({
23
25
  const LayoutComponent = await layouts[props.name]().then((r) => r.default || r);
24
26
  return () => {
25
27
  if (process.dev && process.client && props.hasTransition) {
26
- knode = h(LayoutComponent, {}, context.slots);
28
+ knode = h(LayoutComponent, context.attrs, context.slots);
27
29
  return knode;
28
30
  }
29
- return h(LayoutComponent, {}, context.slots);
31
+ return h(LayoutComponent, context.attrs, context.slots);
30
32
  };
31
33
  }
32
34
  });
33
35
  export default defineComponent({
36
+ name: "DtwoLayout",
37
+ inheritAttrs: false,
34
38
  props: {
35
39
  name: {
36
40
  type: [String, Boolean, Object],
@@ -59,7 +63,12 @@ export default defineComponent({
59
63
  }
60
64
  const transitionProps = route.meta.layoutTransition ?? defaultLayoutTransition;
61
65
  return _wrapIf(Transition, hasLayout && transitionProps, {
62
- default: () => _wrapIf(LayoutLoader, hasLayout && { key: layout.value, name: layout.value, hasTransition: process.dev ? !!transitionProps : void 0 }, context.slots).default()
66
+ default: () => _wrapIf(LayoutLoader, hasLayout && {
67
+ key: layout.value,
68
+ name: layout.value,
69
+ ...process.dev ? { hasTransition: !!transitionProps } : {},
70
+ ...context.attrs
71
+ }, context.slots).default()
63
72
  }).default();
64
73
  };
65
74
  }
@@ -1,5 +1,6 @@
1
1
  import { defineComponent, h } from "kdu";
2
2
  const Fragment = defineComponent({
3
+ name: "FragmentWrapper",
3
4
  setup(_props, { slots }) {
4
5
  return () => slots.default?.();
5
6
  }
@@ -1,5 +1,5 @@
1
1
  import type { Ref, WatchSource } from 'kdu';
2
- import { DtwoApp } from '../dtwo';
2
+ import type { DtwoApp } from '../dtwo';
3
3
  export type _Transform<Input = any, Output = any> = (input: Input) => Output;
4
4
  export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : Pick<T, K[number]> : T;
5
5
  export type KeysOf<T> = Array<T extends T ? keyof T extends string ? keyof T : string : never>;
@@ -35,6 +35,9 @@ export declare function useAsyncData<DataT, DataE = Error, Transform extends _Tr
35
35
  export declare function useAsyncData<DataT, DataE = Error, Transform extends _Transform<DataT> = _Transform<DataT, DataT>, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>>(key: string, handler: (ctx?: DtwoApp) => Promise<DataT>, options?: AsyncDataOptions<DataT, Transform, PickKeys>): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null>;
36
36
  export declare function useLazyAsyncData<DataT, DataE = Error, Transform extends _Transform<DataT> = _Transform<DataT, DataT>, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>>(handler: (ctx?: DtwoApp) => Promise<DataT>, options?: Omit<AsyncDataOptions<DataT, Transform, PickKeys>, 'lazy'>): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null>;
37
37
  export declare function useLazyAsyncData<DataT, DataE = Error, Transform extends _Transform<DataT> = _Transform<DataT, DataT>, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>>(key: string, handler: (ctx?: DtwoApp) => Promise<DataT>, options?: Omit<AsyncDataOptions<DataT, Transform, PickKeys>, 'lazy'>): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null>;
38
+ export declare function useDtwoData<DataT = any>(key: string): {
39
+ data: Ref<DataT | null>;
40
+ };
38
41
  export declare function refreshDtwoData(keys?: string | string[]): Promise<void>;
39
42
  export declare function clearDtwoData(keys?: string | string[] | ((key: string) => boolean)): void;
40
43
  export {};
@@ -1,4 +1,4 @@
1
- import { onBeforeMount, onServerPrefetch, onUnmounted, ref, getCurrentInstance, watch, unref } from "kdu";
1
+ import { onBeforeMount, onServerPrefetch, onUnmounted, ref, getCurrentInstance, watch, unref, toRef } from "kdu";
2
2
  import { useDtwoApp } from "../dtwo.mjs";
3
3
  import { createError } from "./error.mjs";
4
4
  const getDefault = () => null;
@@ -84,7 +84,11 @@ export function useAsyncData(...args) {
84
84
  const fetchOnServer = options.server !== false && dtwo.payload.serverRendered;
85
85
  if (process.server && fetchOnServer && options.immediate) {
86
86
  const promise = initialFetch();
87
- onServerPrefetch(() => promise);
87
+ if (getCurrentInstance()) {
88
+ onServerPrefetch(() => promise);
89
+ } else {
90
+ dtwo.hook("app:created", () => promise);
91
+ }
88
92
  }
89
93
  if (process.client) {
90
94
  const instance = getCurrentInstance();
@@ -132,6 +136,15 @@ export function useLazyAsyncData(...args) {
132
136
  const [key, handler, options] = args;
133
137
  return useAsyncData(key, handler, { ...options, lazy: true }, null);
134
138
  }
139
+ export function useDtwoData(key) {
140
+ const dtwo = useDtwoApp();
141
+ if (!(key in dtwo.payload.data)) {
142
+ dtwo.payload.data[key] = null;
143
+ }
144
+ return {
145
+ data: toRef(dtwo.payload.data, key)
146
+ };
147
+ }
135
148
  export async function refreshDtwoData(keys) {
136
149
  if (process.server) {
137
150
  return Promise.resolve();
@@ -1,3 +1,3 @@
1
- import { defineComponent } from 'kdu';
1
+ import type { defineComponent } from 'kdu';
2
2
  export declare const DtwoComponentIndicator = "__dtwo_component";
3
3
  export declare const defineDtwoComponent: typeof defineComponent;
@@ -1,12 +1,13 @@
1
- import { Ref } from 'kdu';
2
- import { CookieParseOptions, CookieSerializeOptions } from 'cookie-es';
1
+ import type { Ref } from 'kdu';
2
+ import type { CookieParseOptions, CookieSerializeOptions } from 'cookie-es';
3
3
  type _CookieOptions = Omit<CookieSerializeOptions & CookieParseOptions, 'decode' | 'encode'>;
4
4
  export interface CookieOptions<T = any> extends _CookieOptions {
5
5
  decode?(value: string): T;
6
6
  encode?(value: T): string;
7
7
  default?: () => T | Ref<T>;
8
+ watch?: boolean | 'shallow';
8
9
  }
9
- export interface CookieRef<T> extends Ref<T | null> {
10
+ export interface CookieRef<T> extends Ref<T> {
10
11
  }
11
- export declare function useCookie<T = string>(name: string, _opts?: CookieOptions<T>): CookieRef<T>;
12
+ export declare function useCookie<T = string | null>(name: string, _opts?: CookieOptions<T>): CookieRef<T>;
12
13
  export {};
@@ -7,6 +7,7 @@ import { useDtwoApp } from "../dtwo.mjs";
7
7
  import { useRequestEvent } from "./ssr.mjs";
8
8
  const CookieDefaults = {
9
9
  path: "/",
10
+ watch: true,
10
11
  decode: (val) => destr(decodeURIComponent(val)),
11
12
  encode: (val) => encodeURIComponent(typeof val === "string" ? val : JSON.stringify(val))
12
13
  };
@@ -15,9 +16,14 @@ export function useCookie(name, _opts) {
15
16
  const cookies = readRawCookies(opts) || {};
16
17
  const cookie = ref(cookies[name] ?? opts.default?.());
17
18
  if (process.client) {
18
- watch(cookie, () => {
19
+ const callback = () => {
19
20
  writeClientCookie(name, cookie.value, opts);
20
- });
21
+ };
22
+ if (opts.watch) {
23
+ watch(cookie, callback, { deep: opts.watch !== "shallow" });
24
+ } else {
25
+ callback();
26
+ }
21
27
  } else if (process.server) {
22
28
  const dtwoApp = useDtwoApp();
23
29
  const writeFinalCookieValue = () => {
@@ -1,4 +1,4 @@
1
- import { H3Error } from 'h3';
1
+ import type { H3Error } from 'h3';
2
2
  export declare const useError: () => import("kdu").Ref<Error | {
3
3
  url: string;
4
4
  statusCode: string;
@@ -1,15 +1,15 @@
1
- import type { FetchError, FetchOptions } from 'ofetch';
2
- import type { TypedInternalResponse, HoleFetchRequest } from 'holepack';
3
- import { Ref } from 'kdu';
1
+ import type { FetchError } from 'ofetch';
2
+ import type { TypedInternalResponse, HoleFetchOptions, HoleFetchRequest } from 'holepack';
3
+ import type { Ref } from 'kdu';
4
4
  import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData';
5
5
  export type FetchResult<ReqT extends HoleFetchRequest> = TypedInternalResponse<ReqT, unknown>;
6
6
  type ComputedOptions<T extends Record<string, any>> = {
7
7
  [K in keyof T]: T[K] extends Function ? T[K] : T[K] extends Record<string, any> ? ComputedOptions<T[K]> | Ref<T[K]> | T[K] : Ref<T[K]> | T[K];
8
8
  };
9
- type ComputedFetchOptions = ComputedOptions<FetchOptions>;
10
- export interface UseFetchOptions<DataT, Transform extends _Transform<DataT, any> = _Transform<DataT, DataT>, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>> extends AsyncDataOptions<DataT, Transform, PickKeys>, ComputedFetchOptions {
9
+ type ComputedFetchOptions<R extends HoleFetchRequest> = ComputedOptions<HoleFetchOptions<R>>;
10
+ export interface UseFetchOptions<DataT, Transform extends _Transform<DataT, any> = _Transform<DataT, DataT>, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>, R extends HoleFetchRequest = string & {}> extends AsyncDataOptions<DataT, Transform, PickKeys>, ComputedFetchOptions<R> {
11
11
  key?: string;
12
12
  }
13
- export declare function useFetch<ResT = void, ErrorT = FetchError, ReqT extends HoleFetchRequest = HoleFetchRequest, _ResT = ResT extends void ? FetchResult<ReqT> : ResT, Transform extends (res: _ResT) => any = (res: _ResT) => _ResT, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>>(request: Ref<ReqT> | ReqT | (() => ReqT), opts?: UseFetchOptions<_ResT, Transform, PickKeys>): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, ErrorT | null>;
13
+ export declare function useFetch<ResT = void, ErrorT = FetchError, ReqT extends HoleFetchRequest = HoleFetchRequest, _ResT = ResT extends void ? FetchResult<ReqT> : ResT, Transform extends (res: _ResT) => any = (res: _ResT) => _ResT, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>>(request: Ref<ReqT> | ReqT | (() => ReqT), opts?: UseFetchOptions<_ResT, Transform, PickKeys, ReqT>): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, ErrorT | null>;
14
14
  export declare function useLazyFetch<ResT = void, ErrorT = FetchError, ReqT extends HoleFetchRequest = HoleFetchRequest, _ResT = ResT extends void ? FetchResult<ReqT> : ResT, Transform extends (res: _ResT) => any = (res: _ResT) => _ResT, PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>>(request: Ref<ReqT> | ReqT | (() => ReqT), opts?: Omit<UseFetchOptions<_ResT, Transform, PickKeys>, 'lazy'>): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, ErrorT | null>;
15
15
  export {};
@@ -3,7 +3,7 @@ import { hash } from "ohash";
3
3
  import { useAsyncData } from "./asyncData.mjs";
4
4
  export function useFetch(request, arg1, arg2) {
5
5
  const [opts = {}, autoKey] = typeof arg1 === "string" ? [{}, arg1] : [arg1, arg2];
6
- const _key = opts.key || hash([autoKey, unref(opts.baseURL), typeof request === "string" ? request : "", unref(opts.params)]);
6
+ const _key = opts.key || hash([autoKey, unref(opts.baseURL), typeof request === "string" ? request : "", unref(opts.params || opts.query)]);
7
7
  if (!_key || typeof _key !== "string") {
8
8
  throw new TypeError("[dtwo] [useFetch] key must be a string: " + _key);
9
9
  }
@@ -61,6 +61,7 @@ export function useLazyFetch(request, arg1, arg2) {
61
61
  ...opts,
62
62
  lazy: true
63
63
  },
64
+ // @ts-ignore
64
65
  autoKey
65
66
  );
66
67
  }