@sourceregistry/sveltekit-enhance 1.7.1 → 1.8.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.
- package/README.md +16 -0
- package/dist/helpers/form.js +8 -4
- package/dist/index.d.ts +7 -1
- package/dist/index.js +12 -1
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -80,6 +80,22 @@ export const handle = enhance.handle(
|
|
|
80
80
|
);
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
+
An enhancer can bypass all remaining enhancers and the custom handler while still resolving the request through SvelteKit's normal routing with `resolveToRoute`:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
export const handle = enhance.handle(
|
|
87
|
+
async ({ event, resolveToRoute }) => {
|
|
88
|
+
if (event.url.pathname === '/healthz') {
|
|
89
|
+
return resolveToRoute(event);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
enhancerA,
|
|
93
|
+
enhancerB,
|
|
94
|
+
);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`resolveToRoute` invokes SvelteKit's native resolver, so the matching page or `+server.ts` endpoint still runs and normal headers and cookies are preserved. It skips the remaining `enhance.handle` enhancers and the custom handler. `resolve` continues through the remaining enhancers as usual. `resolveToRoute` may also be thrown when integrating with code that uses exception-based control flow.
|
|
98
|
+
|
|
83
99
|
#### SSE / streaming responses
|
|
84
100
|
|
|
85
101
|
`enhance.handle` automatically detects Server-Sent Events and other streaming requests (via `Accept: text/event-stream`) and bypasses the main handler entirely — going straight to SvelteKit's `resolve`. This prevents the handler from blocking indefinitely on `await resolve(event)`, which never settles for a streaming response.
|
package/dist/helpers/form.js
CHANGED
|
@@ -127,10 +127,12 @@ export function number(formdata, name) {
|
|
|
127
127
|
if (!formdata.has(name)) {
|
|
128
128
|
return undefined;
|
|
129
129
|
}
|
|
130
|
-
const
|
|
131
|
-
if (
|
|
130
|
+
const val = formdata.get(name);
|
|
131
|
+
if (val === '')
|
|
132
|
+
return undefined;
|
|
133
|
+
if (val === null)
|
|
132
134
|
return null;
|
|
133
|
-
const num = Number(
|
|
135
|
+
const num = Number(val);
|
|
134
136
|
if (isNaN(num)) {
|
|
135
137
|
fail(400, { targets: [name], message: `${name} isn't of type number` });
|
|
136
138
|
}
|
|
@@ -147,6 +149,8 @@ export function boolean(formdata, name) {
|
|
|
147
149
|
if (!formdata.has(name))
|
|
148
150
|
return undefined;
|
|
149
151
|
const data = formdata.get(name);
|
|
152
|
+
if (data === '')
|
|
153
|
+
return false;
|
|
150
154
|
if (data === null)
|
|
151
155
|
return null;
|
|
152
156
|
const num = Number(data);
|
|
@@ -159,7 +163,7 @@ export function boolean(formdata, name) {
|
|
|
159
163
|
}
|
|
160
164
|
export function boolean$(formdata, name) {
|
|
161
165
|
if (!formdata.has(name)) {
|
|
162
|
-
fail(400, { targets: [name], message: `${name} is required` });
|
|
166
|
+
return fail(400, { targets: [name], message: `${name} is required` });
|
|
163
167
|
}
|
|
164
168
|
const data = formdata.get(name);
|
|
165
169
|
const num = Number(data);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Action, type ActionResult, type Cookies, type Handle, type RequestEvent, type ResolveOptions, type ServerLoadEvent } from '@sveltejs/kit';
|
|
1
|
+
import { type Action, type ActionResult, type Cookies, redirect as SRedirect, type Handle, type RequestEvent, type ResolveOptions, type ServerLoadEvent } from '@sveltejs/kit';
|
|
2
2
|
import type { RouteId as AppRouteId, LayoutParams as AppLayoutParams } from '$app/types';
|
|
3
3
|
export type MaybePromise<T> = T | Promise<T>;
|
|
4
4
|
export type EnhanceErrorHandler = <T = any>(err: unknown) => MaybePromise<T | Response | undefined | never | void>;
|
|
@@ -49,6 +49,11 @@ export type EnhanceInput<CallType extends EnhanceCallType = EnhanceCallType, Par
|
|
|
49
49
|
} & (CallType extends 'handle' ? {
|
|
50
50
|
get responseHandlers(): EnhanceResponseHandler[];
|
|
51
51
|
resolve: (event?: RequestEvent, opts?: ResolveOptions) => MaybePromise<Response>;
|
|
52
|
+
/**
|
|
53
|
+
* Resolve the request through SvelteKit directly, bypassing all remaining
|
|
54
|
+
* enhancers and the custom handle function.
|
|
55
|
+
*/
|
|
56
|
+
resolveToRoute: (event?: RequestEvent, opts?: ResolveOptions) => MaybePromise<Response>;
|
|
52
57
|
readonly event: RequestEvent;
|
|
53
58
|
} : CallType extends 'load' ? {
|
|
54
59
|
parent: ServerLoadEvent<Params, ParentData, RouteId>['parent'];
|
|
@@ -89,6 +94,7 @@ export declare const error: (status: number, body?: {
|
|
|
89
94
|
message: string;
|
|
90
95
|
} extends App.Error ? App.Error | string | undefined : never) => never;
|
|
91
96
|
export declare const success: <T extends Record<string, unknown> | undefined = undefined>(data: T) => T;
|
|
97
|
+
export declare const redirect: typeof SRedirect;
|
|
92
98
|
export declare function not_good(input: {
|
|
93
99
|
callType: EnhanceInput['callType'];
|
|
94
100
|
}, status: number, arg?: App.Error | string | Record<string, unknown> | undefined): never;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { error as SError, fail as SFail, isActionFailure, isHttpError, isRedirect } from '@sveltejs/kit';
|
|
1
|
+
import { error as SError, fail as SFail, redirect as SRedirect, isActionFailure, isHttpError, isRedirect } from '@sveltejs/kit';
|
|
2
2
|
const EnhanceErrorHandle = async (e, contextInput) => {
|
|
3
3
|
if (isRedirect(e) || isHttpError(e)) {
|
|
4
4
|
throw e;
|
|
@@ -134,6 +134,10 @@ export const handle = (handle, ...contexts) => async (input) => {
|
|
|
134
134
|
resolved = true;
|
|
135
135
|
return apply_handle(contexts.length, event, opts);
|
|
136
136
|
},
|
|
137
|
+
resolveToRoute: (event = input.event, opts) => {
|
|
138
|
+
resolved = true;
|
|
139
|
+
return input.resolve(event, opts);
|
|
140
|
+
},
|
|
137
141
|
get event() {
|
|
138
142
|
return input.event;
|
|
139
143
|
}
|
|
@@ -159,10 +163,15 @@ export const handle = (handle, ...contexts) => async (input) => {
|
|
|
159
163
|
if (i < contexts.length) {
|
|
160
164
|
const context = contexts[i];
|
|
161
165
|
const previous_resolve = contextInput.resolve;
|
|
166
|
+
const previous_resolveToRoute = contextInput.resolveToRoute;
|
|
162
167
|
contextInput.resolve = (next_event = event, options) => {
|
|
163
168
|
resolved = true;
|
|
164
169
|
return apply_handle(i + 1, next_event, merge_options(parent_options, options));
|
|
165
170
|
};
|
|
171
|
+
contextInput.resolveToRoute = (next_event = event, options) => {
|
|
172
|
+
resolved = true;
|
|
173
|
+
return input.resolve(next_event, merge_options(parent_options, options));
|
|
174
|
+
};
|
|
166
175
|
try {
|
|
167
176
|
resolved = false;
|
|
168
177
|
const result = await context(contextInput);
|
|
@@ -179,6 +188,7 @@ export const handle = (handle, ...contexts) => async (input) => {
|
|
|
179
188
|
}
|
|
180
189
|
finally {
|
|
181
190
|
contextInput.resolve = previous_resolve;
|
|
191
|
+
contextInput.resolveToRoute = previous_resolveToRoute;
|
|
182
192
|
}
|
|
183
193
|
}
|
|
184
194
|
// SSE responses cannot be awaited — bypass the main handler to avoid blocking
|
|
@@ -223,6 +233,7 @@ export const fail = (status, data) => {
|
|
|
223
233
|
};
|
|
224
234
|
export const error = (status, body) => SError(status, body);
|
|
225
235
|
export const success = (data) => data;
|
|
236
|
+
export const redirect = SRedirect;
|
|
226
237
|
export function not_good(input, status, arg) {
|
|
227
238
|
// Based on callType, call the appropriate function.
|
|
228
239
|
return (input.callType === 'action' ? fail : error)(status, arg);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sourceregistry/sveltekit-enhance",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Composable enhance and form utilities for SvelteKit actions, loads, methods, and hooks.",
|
|
5
5
|
"author": "A.P.A. Slaa (a.p.a.slaa@projectsource.nl)",
|
|
6
6
|
"scripts": {
|
|
@@ -52,17 +52,17 @@
|
|
|
52
52
|
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
53
53
|
"@semantic-release/git": "^10.0.1",
|
|
54
54
|
"@semantic-release/npm": "^13.1.5",
|
|
55
|
-
"@semantic-release/release-notes-generator": "^14.1.
|
|
55
|
+
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
56
56
|
"@sveltejs/adapter-auto": "^7.0.1",
|
|
57
57
|
"@sveltejs/kit": "^2.58.0",
|
|
58
|
-
"@sveltejs/package": "^2.5.
|
|
58
|
+
"@sveltejs/package": "^2.5.8",
|
|
59
59
|
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
|
60
|
-
"@types/node": "^
|
|
61
|
-
"@vitest/coverage-v8": "^4.1.
|
|
62
|
-
"publint": "^0.3.
|
|
63
|
-
"svelte": "^5.
|
|
64
|
-
"svelte-check": "^4.
|
|
65
|
-
"typedoc": "^0.28.
|
|
60
|
+
"@types/node": "^26.1.2",
|
|
61
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
62
|
+
"publint": "^0.3.23",
|
|
63
|
+
"svelte": "^5.56.8",
|
|
64
|
+
"svelte-check": "^4.7.5",
|
|
65
|
+
"typedoc": "^0.28.20",
|
|
66
66
|
"typescript": "^6.0.3",
|
|
67
67
|
"vite": "^8.0.10",
|
|
68
68
|
"vitest": "^4.1.5"
|