@hamur/adapter-svelte 0.0.1 → 0.0.3
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/src/index.js +1 -1
- package/dist/types/src/Announcer.svelte +26 -0
- package/dist/types/src/Await.svelte +43 -0
- package/dist/types/src/AwaitAsync.svelte +42 -0
- package/dist/types/src/BodyAttrs.svelte +7 -0
- package/dist/types/src/Caught.svelte +41 -0
- package/dist/types/src/ClientComponent.svelte +35 -0
- package/dist/types/src/ClientFallback.svelte +46 -0
- package/dist/types/src/ClientOnly.svelte +22 -0
- package/dist/types/src/DefaultErrorPage.svelte +15 -0
- package/dist/types/src/DevOnly.svelte +12 -0
- package/dist/types/src/ErrorBoundary.svelte +14 -0
- package/dist/types/src/FallbackEffect.svelte +9 -0
- package/dist/types/src/Form.svelte +96 -0
- package/dist/types/src/HeadLink.svelte +7 -0
- package/dist/types/src/HeadMeta.svelte +7 -0
- package/dist/types/src/HeadScript.svelte +7 -0
- package/dist/types/src/HeadStyle.svelte +7 -0
- package/dist/types/src/HeadTitle.svelte +6 -0
- package/dist/types/src/HtmlAttrs.svelte +7 -0
- package/dist/types/src/HttpHeader.svelte +7 -0
- package/dist/types/src/HttpStatusCode.svelte +7 -0
- package/dist/types/src/Island.svelte +59 -0
- package/dist/types/src/IslandChildren.svelte +7 -0
- package/dist/types/src/KeptOutlet.svelte +46 -0
- package/dist/types/src/KeptPage.svelte +55 -0
- package/dist/types/src/LazyHydration.svelte +54 -0
- package/dist/types/src/Link.svelte +209 -0
- package/dist/types/src/LoadingIndicator.svelte +18 -0
- package/dist/types/src/MarkdownNodes.svelte +36 -0
- package/dist/types/src/MarkdownRenderer.svelte +20 -0
- package/dist/types/src/MarkdownSlots.svelte +33 -0
- package/dist/types/src/Navigate.svelte +10 -0
- package/dist/types/src/Nest.svelte +83 -0
- package/dist/types/src/Outlet.svelte +31 -0
- package/dist/types/src/Rethrow.svelte +7 -0
- package/dist/types/src/Root.svelte +29 -0
- package/dist/types/src/RouteAnnouncer.svelte +47 -0
- package/dist/types/src/Scoped.svelte +11 -0
- package/dist/types/src/ServerComponent.svelte +19 -0
- package/dist/types/src/ServerComponentClient.svelte +64 -0
- package/dist/types/src/ServerIsland.svelte +26 -0
- package/dist/types/src/Slotted.svelte +11 -0
- package/dist/types/src/Thrown.svelte +9 -0
- package/dist/types/src/Time.svelte +23 -0
- package/dist/types/src/index.d.ts +7 -7
- package/package.json +22 -7
- package/src/index.ts +8 -7
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onDestroy } from "svelte"
|
|
3
|
+
import {
|
|
4
|
+
fetchServerComponent,
|
|
5
|
+
serverComponentAttributes,
|
|
6
|
+
serverComponentProps,
|
|
7
|
+
setServerHTML,
|
|
8
|
+
takeServerHTML,
|
|
9
|
+
} from "hamur/adapter"
|
|
10
|
+
import type { ServerComponentView } from "hamur/adapter"
|
|
11
|
+
import type { HeadEntry } from "hamur/app"
|
|
12
|
+
import { APP_KEY } from "./context.ts"
|
|
13
|
+
import type { AppState } from "./context.ts"
|
|
14
|
+
import type { Source } from "./store.ts"
|
|
15
|
+
|
|
16
|
+
let { server, children, ...rest }: { server: { name: string }; children?: unknown } & Record<string, unknown> =
|
|
17
|
+
$props()
|
|
18
|
+
const uid = $props.id()
|
|
19
|
+
const { name } = server
|
|
20
|
+
const app = getContext<Source<AppState>>(APP_KEY).get().app
|
|
21
|
+
const text = $derived(serverComponentProps(name, rest, children !== undefined && !name.startsWith("page:")))
|
|
22
|
+
const taken = takeServerHTML(app, uid, name)
|
|
23
|
+
let html = $state.raw<ServerComponentView["html"]>(taken ?? "")
|
|
24
|
+
let failed = $state.raw<{ error: unknown }>()
|
|
25
|
+
let requested = taken === undefined ? undefined : text
|
|
26
|
+
let applied: string | undefined
|
|
27
|
+
let head: HeadEntry | undefined
|
|
28
|
+
|
|
29
|
+
$effect(() => {
|
|
30
|
+
const current = text
|
|
31
|
+
if (current === requested) return
|
|
32
|
+
const controller = new AbortController()
|
|
33
|
+
fetchServerComponent(app, name, current, controller.signal).then(
|
|
34
|
+
(view) => {
|
|
35
|
+
if (controller.signal.aborted) return
|
|
36
|
+
head?.dispose()
|
|
37
|
+
head = view.head ? app.head.push(view.head) : undefined
|
|
38
|
+
requested = current
|
|
39
|
+
html = view.html
|
|
40
|
+
},
|
|
41
|
+
(error: unknown) => {
|
|
42
|
+
if (!controller.signal.aborted) failed = { error }
|
|
43
|
+
},
|
|
44
|
+
)
|
|
45
|
+
return () => controller.abort()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
$effect(() => {
|
|
49
|
+
if (failed) throw failed.error
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
onDestroy(() => head?.dispose())
|
|
53
|
+
|
|
54
|
+
// The element has no children of its own, so hydration leaves the server's nodes as they are; a new element has none to keep.
|
|
55
|
+
function show(node: HTMLElement): void {
|
|
56
|
+
const next = html
|
|
57
|
+
if (applied === undefined && taken !== undefined && node.hasChildNodes()) applied = String(next)
|
|
58
|
+
if (applied === String(next)) return
|
|
59
|
+
applied = String(next)
|
|
60
|
+
setServerHTML(node, next)
|
|
61
|
+
}
|
|
62
|
+
</script>
|
|
63
|
+
|
|
64
|
+
<hamur-component {name} {uid} {...serverComponentAttributes(name)} {@attach show}></hamur-component>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext } from "svelte"
|
|
3
|
+
import type { Snippet } from "svelte"
|
|
4
|
+
import { renderServerIsland, serverIslandProps } from "hamur/adapter"
|
|
5
|
+
import { APP_KEY, PENDING_ISLANDS_KEY } from "./context.ts"
|
|
6
|
+
import type { AppState } from "./context.ts"
|
|
7
|
+
import type { Source } from "./store.ts"
|
|
8
|
+
import { settledIsland } from "./island-html.ts"
|
|
9
|
+
|
|
10
|
+
let { name, props = {}, fallback }: { name: string; props?: Record<string, unknown>; fallback?: Snippet } = $props()
|
|
11
|
+
const id = $props.id()
|
|
12
|
+
const app = getContext<Source<AppState> | undefined>(APP_KEY)?.get().app
|
|
13
|
+
if (!app) throw new Error("ServerIsland must be rendered inside a Hamur app")
|
|
14
|
+
// While hydrating, the server's element keeps its attributes and Svelte hydrates the fallback it wrote, so the spread is empty.
|
|
15
|
+
const attributes =
|
|
16
|
+
typeof document === "undefined"
|
|
17
|
+
? (settledIsland(
|
|
18
|
+
renderServerIsland(app, id, name, props).attributes,
|
|
19
|
+
getContext<Promise<void>[] | undefined>(PENDING_ISLANDS_KEY),
|
|
20
|
+
) ?? {})
|
|
21
|
+
: app.asyncData.hydrating
|
|
22
|
+
? {}
|
|
23
|
+
: { name, ...(Object.keys(props).length ? { props: serverIslandProps(name, props) } : {}) }
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<hamur-server-island {...attributes}>{@render fallback?.()}</hamur-server-island>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component, Snippet } from "svelte"
|
|
3
|
+
|
|
4
|
+
let {
|
|
5
|
+
component: Island,
|
|
6
|
+
props = {},
|
|
7
|
+
slot,
|
|
8
|
+
}: { component: Component<any>; props?: Record<string, unknown>; slot: Snippet } = $props()
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<Island {...props} children={slot} />
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createError } from "hamur/app"
|
|
3
|
+
import type { ErrorPageProps } from "hamur/app"
|
|
4
|
+
|
|
5
|
+
let { error }: { error: ErrorPageProps } = $props()
|
|
6
|
+
|
|
7
|
+
// A boundary entry sits in the same svelte:boundary markup that the server writes for a render error, so hydration finds it.
|
|
8
|
+
throw createError({ status: error.status, message: error.message })
|
|
9
|
+
</script>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { HTMLAttributes } from "svelte/elements"
|
|
3
|
+
import { timeAttributes, timeView, watchTime, type TimeInput } from "hamur/adapter"
|
|
4
|
+
import { useApp, useRoute } from "./index.ts"
|
|
5
|
+
|
|
6
|
+
type Props = TimeInput & Omit<HTMLAttributes<HTMLElement>, keyof TimeInput | "children">
|
|
7
|
+
|
|
8
|
+
let props: Props = $props()
|
|
9
|
+
const app = useApp()
|
|
10
|
+
const route = useRoute()
|
|
11
|
+
let now: number | undefined = $state()
|
|
12
|
+
const view = $derived(timeView(app, route.path, props, now))
|
|
13
|
+
const attributes = $derived(timeAttributes(props))
|
|
14
|
+
const interval = $derived(view.interval)
|
|
15
|
+
$effect(() => {
|
|
16
|
+
if (interval === undefined) return
|
|
17
|
+
return watchTime(interval, (next) => {
|
|
18
|
+
now = next
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<time {...attributes} datetime={view.iso} data-locale={view.locale} data-time-zone={view.timeZone}>{view.text}</time>
|
|
@@ -5,10 +5,10 @@ import type { PreviewMode } from "hamur/app";
|
|
|
5
5
|
import type { $Fetch, FetchCheckedInput, FetchInputField } from "hamur/app";
|
|
6
6
|
import type { StateKey } from "hamur/app";
|
|
7
7
|
import type { HamurEvent } from "hamur/server";
|
|
8
|
-
import type { AnnouncerActions, AnnouncerPoliteness, AnnouncerState, RouteAnnouncerActions, RouteAnnouncerState, LoadingIndicatorActions, LoadingIndicatorOptions, LoadingIndicatorState, ActionOptions, ActionRunner, FormStatus, Handler, AfterNavigateHook, BeforeNavigateHook, NavigateHook, NavigationBlocker, NavigationGuard, Blocker, ShouldBlock, UseBlockerOptions, SnapshotOptions } from "hamur/adapter";
|
|
8
|
+
import type { AnnouncerActions, AnnouncerPoliteness, AnnouncerState, RouteAnnouncerActions, RouteAnnouncerState, LoadingIndicatorActions, LoadingIndicatorOptions, LoadingIndicatorState, ActionOptions, ActionRunner, ActionValue, FailureOf, FormStatus, Handler, AfterNavigateHook, BeforeNavigateHook, NavigateHook, NavigationBlocker, NavigationGuard, Blocker, ShouldBlock, UseBlockerOptions, SnapshotOptions } from "hamur/adapter";
|
|
9
9
|
import type { HandlerForm } from "hamur/app";
|
|
10
10
|
import type { CheckedTarget, LinkDefaults, LinkSlot, UseLinkOptions } from "hamur/adapter";
|
|
11
|
-
import type {
|
|
11
|
+
import type { AppContext, RouteLocation, PendingRun, PickFrom, ClientRuntimeConfig, SiteConfig, FetchData, AsyncDataOptions, AsyncDataStatus, HeadInput, HeadSafeInput, SeoMetaInput, UseCookieOptions, HamurError } from "hamur/app";
|
|
12
12
|
import type { RouteLoaderData, UseDataArgs, AsyncDataHandler, FetchRequest, GetCachedData, NavigationState, RouteValue, RouterApi } from "hamur/adapter";
|
|
13
13
|
export { default as Link } from "./Link.svelte";
|
|
14
14
|
import LinkComponent from "./Link.svelte";
|
|
@@ -170,10 +170,10 @@ export declare function useAction<I, O>(fn: Handler<I, O>, options?: ActionOptio
|
|
|
170
170
|
instance?: string | number;
|
|
171
171
|
}): {
|
|
172
172
|
readonly run: ActionRunner<I, O>["run"];
|
|
173
|
-
readonly state: O | undefined;
|
|
173
|
+
readonly state: ActionValue<O> | undefined;
|
|
174
174
|
readonly pending: boolean;
|
|
175
175
|
readonly error: HamurError | undefined;
|
|
176
|
-
readonly failure:
|
|
176
|
+
readonly failure: FailureOf<O> | undefined;
|
|
177
177
|
reset(): void;
|
|
178
178
|
};
|
|
179
179
|
export declare function useOptimistic<T, A = PendingRun>(value: T | (() => T), reduce: (current: T, pending: A[]) => T): {
|
|
@@ -203,7 +203,7 @@ export declare function useCookie<T = string>(name: string, options?: Omit<UseCo
|
|
|
203
203
|
export interface UseAsyncDataOptions<T = unknown, R = T, K extends keyof T & string = never, D = T> extends Omit<AsyncDataOptions<T, R, D>, "pick"> {
|
|
204
204
|
/** Getters whose change re-runs the handler. `false` is accepted and does nothing: nothing watches implicitly. */
|
|
205
205
|
watch?: ReadonlyArray<() => unknown> | false;
|
|
206
|
-
getCachedData?: GetCachedData<T
|
|
206
|
+
getCachedData?: GetCachedData<NoInfer<T>>;
|
|
207
207
|
/** Top-level keys of an object result to keep. */
|
|
208
208
|
pick?: readonly K[];
|
|
209
209
|
}
|
|
@@ -227,10 +227,10 @@ export declare function useAsyncData<I, R, T = R, K extends keyof T & string = n
|
|
|
227
227
|
default: () => D;
|
|
228
228
|
}): UseAsyncDataResult<PickFrom<T, K>, D>;
|
|
229
229
|
export declare function useAsyncData<I, R, T = R, K extends keyof T & string = never>(fn: Handler<I, R>, input?: NoInfer<I> | (() => NoInfer<I>), options?: UseAsyncDataOptions<T, R, K, undefined>): UseAsyncDataResult<PickFrom<T, K>>;
|
|
230
|
-
export declare function useAsyncData<T =
|
|
230
|
+
export declare function useAsyncData<R, T = R, K extends keyof T & string = never, D = T>(key: string | (() => string), handler: AsyncDataHandler<R>, options: UseAsyncDataOptions<T, R, K, D> & {
|
|
231
231
|
default: () => D;
|
|
232
232
|
}): UseAsyncDataResult<PickFrom<T, K>, D>;
|
|
233
|
-
export declare function useAsyncData<T =
|
|
233
|
+
export declare function useAsyncData<R, T = R, K extends keyof T & string = never>(key: string | (() => string), handler: AsyncDataHandler<R>, options?: UseAsyncDataOptions<T, R, K, undefined>): UseAsyncDataResult<PickFrom<T, K>>;
|
|
234
234
|
/** What a literal `useFetch` call is checked against; any other call stays loose. */
|
|
235
235
|
type FetchInput<X> = X extends {
|
|
236
236
|
key: infer K extends string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/adapter-svelte",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -11,24 +11,39 @@
|
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
13
|
"types": "./dist/types/src/index.d.ts",
|
|
14
|
-
"
|
|
14
|
+
"bun": "./src/index.ts",
|
|
15
|
+
"hamur-source": "./src/index.ts",
|
|
16
|
+
"node": "./dist/src/index.js",
|
|
17
|
+
"default": "./src/index.ts"
|
|
15
18
|
},
|
|
16
19
|
"./adapter": {
|
|
17
20
|
"types": "./dist/types/src/adapter.d.ts",
|
|
18
|
-
"
|
|
21
|
+
"bun": "./src/adapter.ts",
|
|
22
|
+
"hamur-source": "./src/adapter.ts",
|
|
23
|
+
"node": "./dist/src/adapter.js",
|
|
24
|
+
"default": "./src/adapter.ts"
|
|
19
25
|
},
|
|
20
26
|
"./runtime/server": {
|
|
21
27
|
"types": "./dist/types/src/runtime/server.d.ts",
|
|
22
|
-
"
|
|
28
|
+
"bun": "./src/runtime/server.ts",
|
|
29
|
+
"hamur-source": "./src/runtime/server.ts",
|
|
30
|
+
"node": "./dist/src/runtime/server.js",
|
|
31
|
+
"default": "./src/runtime/server.ts"
|
|
23
32
|
},
|
|
24
33
|
"./runtime/client": {
|
|
25
34
|
"types": "./dist/types/src/runtime/client.d.ts",
|
|
26
|
-
"
|
|
35
|
+
"bun": "./src/runtime/client.ts",
|
|
36
|
+
"hamur-source": "./src/runtime/client.ts",
|
|
37
|
+
"node": "./dist/src/runtime/client.js",
|
|
38
|
+
"default": "./src/runtime/client.ts"
|
|
27
39
|
},
|
|
28
40
|
"./runtime/keepalive": "./src/KeptOutlet.svelte",
|
|
29
41
|
"./runtime/island": {
|
|
30
42
|
"types": "./dist/types/src/runtime/island.d.ts",
|
|
31
|
-
"
|
|
43
|
+
"bun": "./src/runtime/island.ts",
|
|
44
|
+
"hamur-source": "./src/runtime/island.ts",
|
|
45
|
+
"node": "./dist/src/runtime/island.js",
|
|
46
|
+
"default": "./src/runtime/island.ts"
|
|
32
47
|
},
|
|
33
48
|
"./error-page": "./src/DefaultErrorPage.svelte",
|
|
34
49
|
"./package.json": "./package.json"
|
|
@@ -40,6 +55,6 @@
|
|
|
40
55
|
"dependencies": {},
|
|
41
56
|
"peerDependencies": {
|
|
42
57
|
"svelte": "^5.57.2",
|
|
43
|
-
"hamur": "^0.0.
|
|
58
|
+
"hamur": "^0.0.3"
|
|
44
59
|
}
|
|
45
60
|
}
|
package/src/index.ts
CHANGED
|
@@ -55,6 +55,8 @@ import type {
|
|
|
55
55
|
LoadingIndicatorState,
|
|
56
56
|
ActionOptions,
|
|
57
57
|
ActionRunner,
|
|
58
|
+
ActionValue,
|
|
59
|
+
FailureOf,
|
|
58
60
|
FormStatus,
|
|
59
61
|
FormStatusStore,
|
|
60
62
|
Handler,
|
|
@@ -82,7 +84,6 @@ import {
|
|
|
82
84
|
import { prehydrate } from "hamur/adapter"
|
|
83
85
|
import type { CheckedTarget, LinkDefaults, LinkSlot, UseLinkOptions } from "hamur/adapter"
|
|
84
86
|
import type {
|
|
85
|
-
ActionFailure,
|
|
86
87
|
AppContext,
|
|
87
88
|
RouteLocation,
|
|
88
89
|
PendingRun,
|
|
@@ -781,7 +782,7 @@ export function useSearch<S>(select?: (search: RouteValue["search"]) => S): {
|
|
|
781
782
|
return {
|
|
782
783
|
get value() {
|
|
783
784
|
const search = read().route.search
|
|
784
|
-
if (shown
|
|
785
|
+
if (shown && shown.search === search) return shown.value
|
|
785
786
|
const next = select ? select(search) : search
|
|
786
787
|
shown = { search, value: shown && equalSearch(shown.value, next) ? shown.value : next }
|
|
787
788
|
return shown.value
|
|
@@ -886,10 +887,10 @@ export function useAction<I, O>(
|
|
|
886
887
|
options: ActionOptions<O> & { instance?: string | number } = {},
|
|
887
888
|
): {
|
|
888
889
|
readonly run: ActionRunner<I, O>["run"]
|
|
889
|
-
readonly state: O | undefined
|
|
890
|
+
readonly state: ActionValue<O> | undefined
|
|
890
891
|
readonly pending: boolean
|
|
891
892
|
readonly error: HamurError | undefined
|
|
892
|
-
readonly failure:
|
|
893
|
+
readonly failure: FailureOf<O> | undefined
|
|
893
894
|
reset(): void
|
|
894
895
|
} {
|
|
895
896
|
const read = useAppState("useAction")
|
|
@@ -1028,7 +1029,7 @@ export interface UseAsyncDataOptions<T = unknown, R = T, K extends keyof T & str
|
|
|
1028
1029
|
> {
|
|
1029
1030
|
/** Getters whose change re-runs the handler. `false` is accepted and does nothing: nothing watches implicitly. */
|
|
1030
1031
|
watch?: ReadonlyArray<() => unknown> | false
|
|
1031
|
-
getCachedData?: GetCachedData<T
|
|
1032
|
+
getCachedData?: GetCachedData<NoInfer<T>>
|
|
1032
1033
|
/** Top-level keys of an object result to keep. */
|
|
1033
1034
|
pick?: readonly K[]
|
|
1034
1035
|
}
|
|
@@ -1064,12 +1065,12 @@ export function useAsyncData<I, R, T = R, K extends keyof T & string = never>(
|
|
|
1064
1065
|
input?: NoInfer<I> | (() => NoInfer<I>),
|
|
1065
1066
|
options?: UseAsyncDataOptions<T, R, K, undefined>,
|
|
1066
1067
|
): UseAsyncDataResult<PickFrom<T, K>>
|
|
1067
|
-
export function useAsyncData<T =
|
|
1068
|
+
export function useAsyncData<R, T = R, K extends keyof T & string = never, D = T>(
|
|
1068
1069
|
key: string | (() => string),
|
|
1069
1070
|
handler: AsyncDataHandler<R>,
|
|
1070
1071
|
options: UseAsyncDataOptions<T, R, K, D> & { default: () => D },
|
|
1071
1072
|
): UseAsyncDataResult<PickFrom<T, K>, D>
|
|
1072
|
-
export function useAsyncData<T =
|
|
1073
|
+
export function useAsyncData<R, T = R, K extends keyof T & string = never>(
|
|
1073
1074
|
key: string | (() => string),
|
|
1074
1075
|
handler: AsyncDataHandler<R>,
|
|
1075
1076
|
options?: UseAsyncDataOptions<T, R, K, undefined>,
|