@sourceregistry/sveltekit-enhance 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.
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ import { error as SError, fail as SFail, isActionFailure, isHttpError, isRedirect } from '@sveltejs/kit';
2
+ const EnhanceErrorHandle = async (e, contextInput) => {
3
+ if (isRedirect(e) || isHttpError(e)) {
4
+ throw e;
5
+ }
6
+ else if (contextInput.callType === 'action' && isActionFailure(e)) {
7
+ return e;
8
+ }
9
+ else if (e instanceof Response) {
10
+ return e;
11
+ }
12
+ else if (e instanceof Promise) {
13
+ return e;
14
+ }
15
+ for (const errorHandler of contextInput.errorHandlers) {
16
+ try {
17
+ await errorHandler(e);
18
+ }
19
+ catch (e) {
20
+ if (typeof e === 'function') {
21
+ return e();
22
+ }
23
+ throw e;
24
+ }
25
+ }
26
+ throw e;
27
+ };
28
+ export const action = (action, ...enhances) => async (event) => {
29
+ let combined = {};
30
+ const input = Object.assign(event, {
31
+ __errorHandlers__: [],
32
+ isAction: false,
33
+ callType: 'action',
34
+ get errorHandlers() {
35
+ return this.__errorHandlers__;
36
+ }
37
+ });
38
+ for (const enhancer of enhances) {
39
+ try {
40
+ const result = await enhancer(input);
41
+ combined = Object.assign(combined, result);
42
+ }
43
+ catch (e) {
44
+ return EnhanceErrorHandle(e, input);
45
+ }
46
+ }
47
+ try {
48
+ return await action(Object.assign(input, { context: combined }));
49
+ }
50
+ catch (e) {
51
+ return EnhanceErrorHandle(e, input);
52
+ }
53
+ };
54
+ export const load = (load, ...contexts) => {
55
+ return async (event) => {
56
+ let combined = {};
57
+ const contextInput = Object.assign(event, {
58
+ __errorHandlers__: [],
59
+ isAction: false,
60
+ callType: 'load',
61
+ get errorHandlers() {
62
+ return this.__errorHandlers__;
63
+ }
64
+ });
65
+ for (const context of contexts) {
66
+ try {
67
+ const result = await context(contextInput);
68
+ combined = Object.assign(combined, result);
69
+ }
70
+ catch (e) {
71
+ return EnhanceErrorHandle(e, contextInput);
72
+ }
73
+ }
74
+ try {
75
+ return load(Object.assign(contextInput, { context: combined }));
76
+ }
77
+ catch (e) {
78
+ return EnhanceErrorHandle(e, contextInput);
79
+ }
80
+ };
81
+ };
82
+ export const method = (handle, ...contexts) => {
83
+ return async (event) => {
84
+ let combined = {};
85
+ const contextInput = Object.assign(event, {
86
+ __errorHandlers__: [],
87
+ isAction: false,
88
+ callType: 'method',
89
+ get errorHandlers() {
90
+ return this.__errorHandlers__;
91
+ }
92
+ });
93
+ for (const context of contexts) {
94
+ try {
95
+ const result = await context(contextInput);
96
+ combined = Object.assign(combined, result);
97
+ }
98
+ catch (e) {
99
+ return EnhanceErrorHandle(e, contextInput);
100
+ }
101
+ }
102
+ try {
103
+ return await handle(Object.assign(contextInput, { context: combined }));
104
+ }
105
+ catch (e) {
106
+ return EnhanceErrorHandle(e, contextInput);
107
+ }
108
+ };
109
+ };
110
+ export const handle = (handle, ...contexts) => async (input) => {
111
+ let combined = {};
112
+ const contextInput = Object.assign(input.event, {
113
+ __errorHandlers__: [],
114
+ __responseHandler__: [],
115
+ isAction: false,
116
+ callType: 'handle',
117
+ get errorHandlers() {
118
+ return this.__errorHandlers__;
119
+ },
120
+ get responseHandlers() {
121
+ return this.__responseHandler__;
122
+ },
123
+ resolve: (event = input.event) => {
124
+ throw input.resolve(event);
125
+ },
126
+ get event() {
127
+ return input.event;
128
+ }
129
+ });
130
+ for (const context of contexts) {
131
+ try {
132
+ const result = await context(contextInput);
133
+ combined = Object.assign(combined, result);
134
+ }
135
+ catch (e) {
136
+ return EnhanceErrorHandle(e, contextInput);
137
+ }
138
+ }
139
+ try {
140
+ const response = await handle(Object.assign(input, { context: combined }));
141
+ if (contextInput.responseHandlers.length > 0) {
142
+ for (const responseHandler of contextInput.responseHandlers) {
143
+ await responseHandler({ event: contextInput, response });
144
+ }
145
+ }
146
+ return response;
147
+ }
148
+ catch (e) {
149
+ return EnhanceErrorHandle(e, contextInput);
150
+ }
151
+ };
152
+ export const enhance = {
153
+ action,
154
+ load,
155
+ method,
156
+ handle
157
+ };
158
+ export const fail = (status, data) => {
159
+ throw SFail(status, data);
160
+ };
161
+ export const error = (status, body) => SError(status, body);
162
+ export const success = (data) => data;
163
+ export function not_good(input, status, arg) {
164
+ // Based on callType, call the appropriate function.
165
+ return (input.callType === 'action' ? fail : error)(status, arg);
166
+ }
167
+ export * from './helpers/index.js';
package/package.json ADDED
@@ -0,0 +1,107 @@
1
+ {
2
+ "name": "@sourceregistry/sveltekit-enhance",
3
+ "version": "1.0.0",
4
+ "description": "Composable enhance and form utilities for SvelteKit actions, loads, methods, and hooks.",
5
+ "author": "A.P.A. Slaa (a.p.a.slaa@projectsource.nl)",
6
+ "scripts": {
7
+ "dev": "vite dev",
8
+ "build": "vite build && npm run prepack",
9
+ "preview": "vite preview",
10
+ "prepare": "svelte-kit sync || echo ''",
11
+ "prepack": "svelte-kit sync && svelte-package && publint",
12
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
13
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
14
+ "test:unit": "vitest",
15
+ "test": "npm run check && npm run test:unit -- --run",
16
+ "docs:build": "typedoc --entryPoints src/lib/index.ts --out docs --name \"sveltekit-enhance\" --theme default --excludePrivate --excludeProtected"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "!dist/**/*.test.*",
21
+ "!dist/**/*.spec.*"
22
+ ],
23
+ "sideEffects": [
24
+ "**/*.css"
25
+ ],
26
+ "svelte": "./dist/client/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "type": "module",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "svelte": "./dist/index.js",
33
+ "default": "./dist/index.js"
34
+ }
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/SourceRegistry/sveltekit-enhance.git"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "peerDependencies": {
44
+ "@sveltejs/kit": "^2.58.0"
45
+ },
46
+ "devDependencies": {
47
+ "@semantic-release/changelog": "^6.0.3",
48
+ "@semantic-release/commit-analyzer": "^13.0.1",
49
+ "@semantic-release/git": "^10.0.1",
50
+ "@semantic-release/npm": "^13.1.5",
51
+ "@semantic-release/release-notes-generator": "^14.1.0",
52
+ "@sveltejs/adapter-auto": "^7.0.1",
53
+ "@sveltejs/kit": "^2.58.0",
54
+ "@sveltejs/package": "^2.5.7",
55
+ "@sveltejs/vite-plugin-svelte": "^7.0.0",
56
+ "@types/node": "^25.6.0",
57
+ "@vitest/coverage-v8": "^4.1.4",
58
+ "publint": "^0.3.18",
59
+ "svelte": "^5.55.4",
60
+ "svelte-check": "^4.4.6",
61
+ "typedoc": "^0.28.19",
62
+ "typescript": "^6.0.3",
63
+ "vite": "^8.0.10",
64
+ "vitest": "^4.1.5"
65
+ },
66
+ "license": "Apache-2.0",
67
+ "bugs": {
68
+ "url": "https://github.com/SourceRegistry/sveltekit-enhance/issues"
69
+ },
70
+ "homepage": "https://sourceregistry.github.io/sveltekit-enhance/",
71
+ "keywords": [
72
+ "svelte",
73
+ "kit",
74
+ "sveltekit",
75
+ "middleware",
76
+ "guards",
77
+ "actions",
78
+ "hooks",
79
+ "hook",
80
+ "load",
81
+ "form",
82
+ "validation",
83
+ "feature-flags",
84
+ "observability"
85
+ ],
86
+ "release": {
87
+ "branches": [
88
+ "main"
89
+ ],
90
+ "plugins": [
91
+ "@semantic-release/commit-analyzer",
92
+ "@semantic-release/release-notes-generator",
93
+ "@semantic-release/changelog",
94
+ "@semantic-release/npm",
95
+ [
96
+ "@semantic-release/git",
97
+ {
98
+ "assets": [
99
+ "package.json",
100
+ "CHANGELOG.md"
101
+ ],
102
+ "message": "sveltekit-enhance(release): 🚀 v${nextRelease.version}\n\n${nextRelease.notes}"
103
+ }
104
+ ]
105
+ ]
106
+ }
107
+ }