@sourceregistry/sveltekit-enhance 1.8.0 → 2.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.
- package/README.md +5 -4
- package/dist/helpers/devtools.js +1 -1
- package/dist/helpers/featureflag.d.ts +26 -5
- package/dist/helpers/featureflag.js +29 -6
- package/dist/helpers/index.d.ts +1 -1
- package/dist/helpers/index.js +1 -1
- package/dist/helpers/startup.js +1 -1
- package/dist/index.d.ts +10 -8
- package/dist/index.js +7 -1
- package/package.json +25 -18
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://www.npmjs.com/package/@sourceregistry/sveltekit-enhance)
|
|
8
8
|
[](https://www.npmjs.com/package/@sourceregistry/sveltekit-enhance)
|
|
9
9
|
[](./LICENSE)
|
|
10
|
-
[](https://svelte.dev/docs/kit)
|
|
11
11
|
[](https://github.com/SourceRegistry/sveltekit-enhance/issues)
|
|
12
12
|
|
|
13
13
|
Wrap actions, loads, methods, and hooks with composable enhancers. Stack auth guards, feature flags, request tracing, and form parsing without touching SvelteKit's internals.
|
|
@@ -24,14 +24,14 @@ Wrap actions, loads, methods, and hooks with composable enhancers. Stack auth gu
|
|
|
24
24
|
npm install @sourceregistry/sveltekit-enhance
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
**Peer dependency:** `@sveltejs/kit ^
|
|
27
|
+
**Peer dependency:** `@sveltejs/kit ^3.0.1`
|
|
28
28
|
|
|
29
29
|
---
|
|
30
30
|
|
|
31
31
|
## Overview
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
|
-
import { enhance, Auth, RequestCorrelation, RequestMonitor, Form } from '@sourceregistry/sveltekit-enhance';
|
|
34
|
+
import { enhance, Auth, FeatureFlag, RequestCorrelation, RequestMonitor, Form, success } from '@sourceregistry/sveltekit-enhance';
|
|
35
35
|
|
|
36
36
|
// hooks.server.ts
|
|
37
37
|
export const handle = enhance.handle(
|
|
@@ -242,7 +242,8 @@ export const handle = enhance.handle(myHandler, Devtools.ignore);
|
|
|
242
242
|
|
|
243
243
|
### `FeatureFlag`
|
|
244
244
|
|
|
245
|
-
Guards routes behind SvelteKit public env vars (`$env/
|
|
245
|
+
Guards routes behind SvelteKit public env vars (`$app/env/public`). Declare each flag
|
|
246
|
+
with `public: true` in your app's `src/env.ts`. Always passes in `dev` mode.
|
|
246
247
|
|
|
247
248
|
```ts
|
|
248
249
|
import { FeatureFlag } from '@sourceregistry/sveltekit-enhance';
|
package/dist/helpers/devtools.js
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
|
-
import { env } from '$env/dynamic/public';
|
|
2
1
|
import { type EnhanceInput } from "../index.js";
|
|
2
|
+
export declare const FeatureFlagChecker: {
|
|
3
|
+
enabled: (value: string) => boolean;
|
|
4
|
+
disabled: (value: string) => boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare const enabled: (value: string) => boolean;
|
|
7
|
+
export declare const disabled: (value: string) => boolean;
|
|
3
8
|
export declare const FeatureFlag: {
|
|
4
|
-
all: (...flags:
|
|
5
|
-
flags:
|
|
9
|
+
all: (...flags: string[]) => (input: EnhanceInput) => {
|
|
10
|
+
flags: string[];
|
|
6
11
|
};
|
|
7
|
-
oneOf: (...flags:
|
|
8
|
-
flags:
|
|
12
|
+
oneOf: (...flags: string[]) => (input: EnhanceInput) => {
|
|
13
|
+
flags: string[];
|
|
9
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* @experimental
|
|
17
|
+
* @param flag
|
|
18
|
+
*/
|
|
19
|
+
isEnabled: (flag: string) => boolean;
|
|
20
|
+
/**
|
|
21
|
+
* @experimental
|
|
22
|
+
* @param flag
|
|
23
|
+
*/
|
|
24
|
+
isDisabled: (flag: string) => boolean;
|
|
25
|
+
/**
|
|
26
|
+
* @experimental
|
|
27
|
+
* @param flag
|
|
28
|
+
* @param predicate
|
|
29
|
+
*/
|
|
30
|
+
is: (flag: string, predicate?: (value: string) => boolean) => boolean;
|
|
10
31
|
};
|
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { dev } from '$app/
|
|
1
|
+
import * as publicEnv from '$app/env/public';
|
|
2
|
+
import { dev } from '$app/env';
|
|
3
3
|
import { not_good } from "../index.js";
|
|
4
|
-
const
|
|
4
|
+
const _enabled = ['true', 'TRUE', 'on', 'ON', '1'];
|
|
5
|
+
const env = publicEnv;
|
|
6
|
+
export const FeatureFlagChecker = {
|
|
7
|
+
enabled: (value) => _enabled.includes(value),
|
|
8
|
+
disabled: (value) => !_enabled.includes(value)
|
|
9
|
+
};
|
|
10
|
+
export const enabled = FeatureFlagChecker.enabled;
|
|
11
|
+
export const disabled = FeatureFlagChecker.disabled;
|
|
5
12
|
export const FeatureFlag = {
|
|
6
13
|
all: (...flags) => {
|
|
7
14
|
return (input) => {
|
|
8
15
|
// Which of the requested flags are *not* enabled
|
|
9
|
-
const disabledFlags = flags.filter((flag) => !
|
|
16
|
+
const disabledFlags = flags.filter((flag) => !_enabled.includes((env[flag] ?? '').toUpperCase()));
|
|
10
17
|
if (!(disabledFlags.length === 0 || dev))
|
|
11
18
|
not_good(input, 503, {
|
|
12
19
|
message: 'Feature not enabled'
|
|
@@ -17,12 +24,28 @@ export const FeatureFlag = {
|
|
|
17
24
|
oneOf: (...flags) => {
|
|
18
25
|
return (input) => {
|
|
19
26
|
// True if at least one of the given flags is enabled
|
|
20
|
-
const anyEnabled = flags.some((flag) =>
|
|
27
|
+
const anyEnabled = flags.some((flag) => _enabled.includes((env[flag] ?? '').toUpperCase()));
|
|
21
28
|
if (!(anyEnabled || dev))
|
|
22
29
|
not_good(input, 503, {
|
|
23
30
|
message: 'Feature not enabled'
|
|
24
31
|
});
|
|
25
32
|
return { flags };
|
|
26
33
|
};
|
|
27
|
-
}
|
|
34
|
+
},
|
|
35
|
+
/**
|
|
36
|
+
* @experimental
|
|
37
|
+
* @param flag
|
|
38
|
+
*/
|
|
39
|
+
isEnabled: (flag) => FeatureFlag.is(flag),
|
|
40
|
+
/**
|
|
41
|
+
* @experimental
|
|
42
|
+
* @param flag
|
|
43
|
+
*/
|
|
44
|
+
isDisabled: (flag) => FeatureFlag.is(flag, disabled),
|
|
45
|
+
/**
|
|
46
|
+
* @experimental
|
|
47
|
+
* @param flag
|
|
48
|
+
* @param predicate
|
|
49
|
+
*/
|
|
50
|
+
is: (flag, predicate = enabled) => dev || predicate((env[flag] ?? '').toUpperCase())
|
|
28
51
|
};
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from './auth.js';
|
|
|
2
2
|
export * from './cache.js';
|
|
3
3
|
export * from './devtools.js';
|
|
4
4
|
export * from './featureflag.js';
|
|
5
|
-
export * from './form.
|
|
5
|
+
export * from './form.js';
|
|
6
6
|
export * from './request-correlation.js';
|
|
7
7
|
export * from './request-monitor.js';
|
|
8
8
|
export * from './request-rate-limit.js';
|
package/dist/helpers/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export * from './auth.js';
|
|
|
2
2
|
export * from './cache.js';
|
|
3
3
|
export * from './devtools.js';
|
|
4
4
|
export * from './featureflag.js';
|
|
5
|
-
export * from
|
|
5
|
+
export * from './form.js';
|
|
6
6
|
export * from './request-correlation.js';
|
|
7
7
|
export * from './request-monitor.js';
|
|
8
8
|
export * from './request-rate-limit.js';
|
package/dist/helpers/startup.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Deferred } from "./internal/Deferred.js";
|
|
2
|
-
import { dev } from '$app/
|
|
2
|
+
import { dev } from '$app/env';
|
|
3
3
|
export const StartUp = {
|
|
4
4
|
await: (waitOn, config = {}) => {
|
|
5
5
|
const { showPage = "/startup", showJSON = { error: 'service_starting', message: 'Application is initializing' }, allowedPaths = [], onFailure = (reason) => {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { type Action, type
|
|
1
|
+
import { type Action, type Cookies, redirect as SRedirect, type RequestEvent, type ServerLoadEvent } from '@sveltejs/kit';
|
|
2
|
+
import type { ActionResult } from '$app/forms';
|
|
3
|
+
import type { Handle, ResolveOptions } from '@sveltejs/kit/hooks';
|
|
2
4
|
import type { RouteId as AppRouteId, LayoutParams as AppLayoutParams } from '$app/types';
|
|
3
5
|
export type MaybePromise<T> = T | Promise<T>;
|
|
4
6
|
export type EnhanceErrorHandler = <T = any>(err: unknown) => MaybePromise<T | Response | undefined | never | void>;
|
|
@@ -81,18 +83,18 @@ export declare const load: <Params extends AppLayoutParams<"/"> = AppLayoutParam
|
|
|
81
83
|
export declare const method: <Params extends AppLayoutParams<"/"> = AppLayoutParams<"/">, RouteId extends AppRouteId | string | null = AppRouteId | string | null, const Enhances extends readonly EnhanceFunction<"method", Params, RouteId, object>[] = readonly EnhanceFunction<"method", Params, RouteId, object>[], EnhanceReturn extends Awaited<ConcatReturnTypes<Enhances>> = Awaited<ConcatReturnTypes<Enhances>>>(handle: EnhanceMethod<Params, RouteId, EnhanceReturn>, ...contexts: [...Enhances]) => (event: RequestEvent<Params, RouteId>) => Promise<Response>;
|
|
82
84
|
export declare const handle: <const Enhances extends readonly EnhanceFunction<"handle">[] = readonly EnhanceFunction<"handle">[], EnhanceReturn extends Awaited<ConcatReturnTypes<Enhances>> = Awaited<ConcatReturnTypes<Enhances>>>(handle: EnhanceHandle<EnhanceReturn>, ...contexts: [...Enhances]) => Handle;
|
|
83
85
|
export declare const enhance: {
|
|
84
|
-
action: <Params extends AppLayoutParams<"/"> = Record<string,
|
|
85
|
-
load: <Params extends AppLayoutParams<"/"> = Record<string,
|
|
86
|
-
method: <Params extends AppLayoutParams<"/"> = Record<string,
|
|
87
|
-
handle: <const Enhances extends readonly EnhanceFunction<"handle">[] = readonly EnhanceFunction<"handle", Record<string,
|
|
86
|
+
action: <Params extends AppLayoutParams<"/"> = Record<string, never>, OutputData extends Record<string, any> | void = void | Record<string, any>, RouteId extends AppRouteId | string | null = string | null, const Enhances extends readonly EnhanceFunction<"action", Params, RouteId, object>[] = readonly EnhanceFunction<"action", Params, RouteId, object>[], EnhanceReturn extends ConcatReturnTypes<Enhances> = ConcatReturnTypes<Enhances>>(action: EnhanceAction<Params, OutputData, RouteId, EnhanceReturn>, ...enhances: Enhances) => Action<Params, OutputData, RouteId>;
|
|
87
|
+
load: <Params extends AppLayoutParams<"/"> = Record<string, never>, ParentData extends Record<string, any> = Record<string, any>, OutputData extends Record<string, any> | void = void | Record<string, any>, RouteId extends AppRouteId | null = "/" | null, const Enhances extends readonly EnhanceFunction<"load", Params, RouteId, object>[] = readonly EnhanceFunction<"load", Params, RouteId, object>[], EnhanceReturn extends ConcatReturnTypes<Enhances> = ConcatReturnTypes<Enhances>>(load: EnhanceLoad<Params, ParentData, OutputData, RouteId, EnhanceReturn>, ...contexts: Enhances) => (event: ServerLoadEvent<Params, ParentData, RouteId>) => Promise<OutputData>;
|
|
88
|
+
method: <Params extends AppLayoutParams<"/"> = Record<string, never>, RouteId extends AppRouteId | string | null = string | null, const Enhances extends readonly EnhanceFunction<"method", Params, RouteId, object>[] = readonly EnhanceFunction<"method", Params, RouteId, object>[], EnhanceReturn extends Awaited<ConcatReturnTypes<Enhances>> = Awaited<ConcatReturnTypes<Enhances>>>(handle: EnhanceMethod<Params, RouteId, EnhanceReturn>, ...contexts: Enhances) => (event: RequestEvent<Params, RouteId>) => Promise<Response>;
|
|
89
|
+
handle: <const Enhances extends readonly EnhanceFunction<"handle">[] = readonly EnhanceFunction<"handle", Record<string, never>, string | null, any>[], EnhanceReturn extends Awaited<ConcatReturnTypes<Enhances>> = Awaited<ConcatReturnTypes<Enhances>>>(handle: EnhanceHandle<EnhanceReturn>, ...contexts: Enhances) => Handle;
|
|
88
90
|
};
|
|
89
91
|
export type Enhancers<Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>, RouteId extends AppRouteId | null = AppRouteId | null, EnhanceReturn extends never | any = any> = Record<string, EnhanceFunction<EnhanceCallType, Params, RouteId, EnhanceReturn>>;
|
|
90
92
|
export type Func = (...args: any[]) => any;
|
|
91
93
|
export type ConcatReturnTypes<T extends readonly Func[]> = T extends readonly [] ? Record<never, never> : T extends readonly [infer First, ...infer Rest] ? First extends Func ? Awaited<ReturnType<First>> & ConcatReturnTypes<Rest extends readonly Func[] ? Rest : []> : Record<never, never> : Record<never, never>;
|
|
92
94
|
export declare const fail: <T extends Record<string, unknown> | undefined = undefined>(status: number, data: T) => never;
|
|
93
|
-
export declare const error: (status: number, body?: {
|
|
94
|
-
|
|
95
|
-
}
|
|
95
|
+
export declare const error: (status: number, body?: string | (Omit<App.Error, "status"> & {
|
|
96
|
+
status?: number;
|
|
97
|
+
})) => never;
|
|
96
98
|
export declare const success: <T extends Record<string, unknown> | undefined = undefined>(data: T) => T;
|
|
97
99
|
export declare const redirect: typeof SRedirect;
|
|
98
100
|
export declare function not_good(input: {
|
package/dist/index.js
CHANGED
|
@@ -231,7 +231,13 @@ export const enhance = {
|
|
|
231
231
|
export const fail = (status, data) => {
|
|
232
232
|
throw SFail(status, data);
|
|
233
233
|
};
|
|
234
|
-
export const error = (status, body) =>
|
|
234
|
+
export const error = (status, body) => {
|
|
235
|
+
if (typeof body !== 'object' || body === undefined) {
|
|
236
|
+
return SError(status, body);
|
|
237
|
+
}
|
|
238
|
+
const { message, status: _status, ...properties } = body;
|
|
239
|
+
return SError(status, message, properties);
|
|
240
|
+
};
|
|
235
241
|
export const success = (data) => data;
|
|
236
242
|
export const redirect = SRedirect;
|
|
237
243
|
export function not_good(input, status, arg) {
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sourceregistry/sveltekit-enhance",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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
|
+
"engines": {
|
|
7
|
+
"node": ">=22.17"
|
|
8
|
+
},
|
|
6
9
|
"scripts": {
|
|
7
10
|
"dev": "vite dev",
|
|
8
11
|
"build": "vite build && npm run prepack",
|
|
@@ -23,9 +26,13 @@
|
|
|
23
26
|
"sideEffects": [
|
|
24
27
|
"**/*.css"
|
|
25
28
|
],
|
|
26
|
-
"svelte": "./dist/
|
|
29
|
+
"svelte": "./dist/index.js",
|
|
27
30
|
"types": "./dist/index.d.ts",
|
|
28
31
|
"type": "module",
|
|
32
|
+
"imports": {
|
|
33
|
+
"#lib": "./src/lib/index.js",
|
|
34
|
+
"#lib/*": "./src/lib/*"
|
|
35
|
+
},
|
|
29
36
|
"exports": {
|
|
30
37
|
".": {
|
|
31
38
|
"types": "./dist/index.d.ts",
|
|
@@ -45,27 +52,27 @@
|
|
|
45
52
|
"access": "public"
|
|
46
53
|
},
|
|
47
54
|
"peerDependencies": {
|
|
48
|
-
"@sveltejs/kit": "^
|
|
55
|
+
"@sveltejs/kit": "^3.0.1"
|
|
49
56
|
},
|
|
50
57
|
"devDependencies": {
|
|
51
|
-
"@semantic-release/changelog": "^
|
|
58
|
+
"@semantic-release/changelog": "^7.0.0",
|
|
52
59
|
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
53
|
-
"@semantic-release/git": "^
|
|
54
|
-
"@semantic-release/npm": "^13.
|
|
60
|
+
"@semantic-release/git": "^11.0.1",
|
|
61
|
+
"@semantic-release/npm": "^13.2.0",
|
|
55
62
|
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
56
|
-
"@sveltejs/adapter-auto": "^
|
|
57
|
-
"@sveltejs/kit": "^
|
|
58
|
-
"@sveltejs/package": "^
|
|
59
|
-
"@sveltejs/vite-plugin-svelte": "^7.
|
|
60
|
-
"@types/node": "^26.
|
|
61
|
-
"@vitest/coverage-v8": "^
|
|
62
|
-
"publint": "^0.3.
|
|
63
|
-
"svelte": "^5.
|
|
64
|
-
"svelte-check": "^4.7.
|
|
63
|
+
"@sveltejs/adapter-auto": "^8.0.0",
|
|
64
|
+
"@sveltejs/kit": "^3.0.1",
|
|
65
|
+
"@sveltejs/package": "^3.0.0",
|
|
66
|
+
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
67
|
+
"@types/node": "^26.6.4",
|
|
68
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
69
|
+
"publint": "^0.3.25",
|
|
70
|
+
"svelte": "^5.57.2",
|
|
71
|
+
"svelte-check": "^4.7.6",
|
|
65
72
|
"typedoc": "^0.28.20",
|
|
66
|
-
"typescript": "
|
|
67
|
-
"vite": "^8.
|
|
68
|
-
"vitest": "^
|
|
73
|
+
"typescript": "~6.0.3",
|
|
74
|
+
"vite": "^8.3.4",
|
|
75
|
+
"vitest": "^5.0.3"
|
|
69
76
|
},
|
|
70
77
|
"license": "Apache-2.0",
|
|
71
78
|
"bugs": {
|