@webamoki/web-svelte 2.4.0 → 2.5.1
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.
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RemoteCommand } from '@sveltejs/kit';
|
|
2
|
+
import type { CommandResult, CommandSuccess, ResponseError } from './remote.js';
|
|
3
|
+
export declare class CommandAction<S, O extends CommandSuccess> {
|
|
4
|
+
#private;
|
|
5
|
+
input: Parameters<RemoteCommand<S, Promise<CommandResult<O>>>>[0];
|
|
6
|
+
get submitting(): boolean;
|
|
7
|
+
constructor(remote: RemoteCommand<S, Promise<CommandResult<O>>>, defaultInput: Parameters<RemoteCommand<S, Promise<CommandResult<O>>>>[0], config?: {
|
|
8
|
+
onError?: (error: ResponseError) => void;
|
|
9
|
+
onSuccess?: (result: O) => void;
|
|
10
|
+
toastError?: boolean;
|
|
11
|
+
toastSuccess?: boolean;
|
|
12
|
+
});
|
|
13
|
+
execute(): Promise<O>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Client side handling
|
|
2
|
+
import { toast } from 'svelte-sonner';
|
|
3
|
+
export class CommandAction {
|
|
4
|
+
input = $state();
|
|
5
|
+
get submitting() {
|
|
6
|
+
return this.#submitCount > 0;
|
|
7
|
+
}
|
|
8
|
+
#onError;
|
|
9
|
+
#onSuccess;
|
|
10
|
+
#remote;
|
|
11
|
+
#submitCount = $state(0);
|
|
12
|
+
#toastError;
|
|
13
|
+
#toastSuccess;
|
|
14
|
+
constructor(remote, defaultInput, config) {
|
|
15
|
+
this.input = defaultInput;
|
|
16
|
+
this.#remote = remote;
|
|
17
|
+
this.#toastError = config?.toastError ?? true;
|
|
18
|
+
this.#toastSuccess = config?.toastSuccess ?? true;
|
|
19
|
+
this.#onError = config?.onError;
|
|
20
|
+
this.#onSuccess = config?.onSuccess;
|
|
21
|
+
}
|
|
22
|
+
async execute() {
|
|
23
|
+
this.#submitCount++;
|
|
24
|
+
try {
|
|
25
|
+
const result = await this.#remote(this.input);
|
|
26
|
+
if (result.ok) {
|
|
27
|
+
if (this.#toastSuccess)
|
|
28
|
+
toast.success(result.value.message);
|
|
29
|
+
this.#onSuccess?.(result.value);
|
|
30
|
+
return result.value;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (this.#toastError)
|
|
34
|
+
toast.error(`${result.error.code} - ${result.error.message}`);
|
|
35
|
+
this.#onError?.(result.error);
|
|
36
|
+
throw result.error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
this.#submitCount--;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import { type InvalidField, type RemoteCommand, type RemoteForm, type RemoteFormInput, type RemoteQueryFunction } from '@sveltejs/kit';
|
|
3
2
|
import { Result } from './functional/index.js';
|
|
3
|
+
import { type InvalidField, type RemoteCommand, type RemoteForm, type RemoteFormInput, type RemoteQueryFunction } from '@sveltejs/kit';
|
|
4
|
+
export type ResponseError = {
|
|
5
|
+
code: number;
|
|
6
|
+
message: string;
|
|
7
|
+
};
|
|
4
8
|
export type CheckFunction = () => Promise<CheckResult>;
|
|
5
9
|
export type CheckResult = Result<void, ResponseError>;
|
|
6
10
|
export declare const CheckResult: {
|
|
@@ -8,20 +12,15 @@ export declare const CheckResult: {
|
|
|
8
12
|
ok: true;
|
|
9
13
|
value: void;
|
|
10
14
|
};
|
|
15
|
+
/** Throws a sveltekit error if the result is an error result. */
|
|
16
|
+
enforceOk(r: CheckResult): void;
|
|
11
17
|
};
|
|
12
|
-
export type
|
|
13
|
-
|
|
18
|
+
export type CommandResult<T extends CommandSuccess> = Result<T, ResponseError>;
|
|
19
|
+
export type CommandSuccess = {
|
|
14
20
|
message: string;
|
|
15
21
|
};
|
|
16
|
-
export
|
|
17
|
-
export declare
|
|
18
|
-
/**
|
|
19
|
-
* Unwraps a ResponseResult, throwing a sveltekit error if it is an error result.
|
|
20
|
-
*/
|
|
21
|
-
unwrap<T>(result: ResponseResult<T>): T;
|
|
22
|
-
};
|
|
23
|
-
export declare function guardedCommand<Schema extends StandardSchemaV1, Output>(check: CheckFunction, schema: Schema, fn: (output: StandardSchemaV1.InferOutput<Schema>) => Promise<ResponseResult<Output>>): RemoteCommand<StandardSchemaV1.InferInput<Schema>, Promise<ResponseResult<Output>>>;
|
|
24
|
-
export declare function guardedCommandVoid<Output>(check: CheckFunction, fn: () => Promise<ResponseResult<Output>>): RemoteCommand<void, Promise<ResponseResult<Output>>>;
|
|
22
|
+
export declare function guardedCommand<Schema extends StandardSchemaV1, Output extends CommandSuccess>(check: CheckFunction, schema: Schema, fn: (output: StandardSchemaV1.InferOutput<Schema>) => Promise<CommandResult<Output>>): RemoteCommand<StandardSchemaV1.InferInput<Schema>, Promise<CommandResult<Output>>>;
|
|
23
|
+
export declare function guardedCommandVoid<Output extends CommandSuccess>(check: CheckFunction, fn: () => Promise<CommandResult<Output>>): RemoteCommand<void, Promise<CommandResult<Output>>>;
|
|
25
24
|
export declare function guardedForm<Schema extends StandardSchemaV1<RemoteFormInput, Record<string, unknown>>, Output>(schema: Schema, check: CheckFunction, fn: (output: StandardSchemaV1.InferOutput<Schema>, issue: InvalidField<StandardSchemaV1.InferInput<Schema>>) => Promise<Output>): RemoteForm<StandardSchemaV1.InferInput<Schema>, Output>;
|
|
26
25
|
export declare function guardedFormVoid<Output>(check: CheckFunction, fn: () => Promise<Output>): RemoteForm<void, Output>;
|
|
27
26
|
export declare function guardedQuery<Schema extends StandardSchemaV1, Output>(check: CheckFunction, schema: Schema, fn: (output: StandardSchemaV1.InferOutput<Schema>) => Promise<Output>): RemoteQueryFunction<StandardSchemaV1.InferInput<Schema>, Output>;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import { command, form, query } from '$app/server';
|
|
2
|
-
import { error } from '@sveltejs/kit';
|
|
3
2
|
import { Result } from './functional/index.js';
|
|
3
|
+
import { error } from '@sveltejs/kit';
|
|
4
4
|
export const CheckResult = {
|
|
5
5
|
/* Helper function for ok check result. */
|
|
6
6
|
ok() {
|
|
7
7
|
return Result.ok(undefined);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*/
|
|
14
|
-
unwrap(result) {
|
|
15
|
-
return Result.unwrapOrElse(result, (err) => error(err.code, err.message));
|
|
8
|
+
},
|
|
9
|
+
/** Throws a sveltekit error if the result is an error result. */
|
|
10
|
+
enforceOk(r) {
|
|
11
|
+
if (Result.isErr(r))
|
|
12
|
+
throw error(r.error.code, r.error.message);
|
|
16
13
|
}
|
|
17
14
|
};
|
|
18
15
|
/* Guards command remote function with a check function. */
|
|
@@ -39,7 +36,7 @@ export function guardedCommandVoid(check, fn) {
|
|
|
39
36
|
export function guardedForm(schema, check, fn) {
|
|
40
37
|
return form(schema, async (output, issue) => {
|
|
41
38
|
// Enforce auth check or throw sveltekit error
|
|
42
|
-
|
|
39
|
+
CheckResult.enforceOk(await check());
|
|
43
40
|
return await fn(output, issue);
|
|
44
41
|
});
|
|
45
42
|
}
|
|
@@ -47,7 +44,7 @@ export function guardedForm(schema, check, fn) {
|
|
|
47
44
|
export function guardedFormVoid(check, fn) {
|
|
48
45
|
return form(async () => {
|
|
49
46
|
// Enforce auth check or throw sveltekit error
|
|
50
|
-
|
|
47
|
+
CheckResult.enforceOk(await check());
|
|
51
48
|
return await fn();
|
|
52
49
|
});
|
|
53
50
|
}
|
|
@@ -55,7 +52,7 @@ export function guardedFormVoid(check, fn) {
|
|
|
55
52
|
export function guardedQuery(check, schema, fn) {
|
|
56
53
|
return query(schema, async (output) => {
|
|
57
54
|
// Enforce auth check or throw sveltekit error
|
|
58
|
-
|
|
55
|
+
CheckResult.enforceOk(await check());
|
|
59
56
|
return await fn(output);
|
|
60
57
|
});
|
|
61
58
|
}
|
|
@@ -63,7 +60,7 @@ export function guardedQuery(check, schema, fn) {
|
|
|
63
60
|
export function guardedQueryVoid(check, fn) {
|
|
64
61
|
return query(async () => {
|
|
65
62
|
// Enforce auth check or throw sveltekit error
|
|
66
|
-
|
|
63
|
+
CheckResult.enforceOk(await check());
|
|
67
64
|
return await fn();
|
|
68
65
|
});
|
|
69
66
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public",
|
|
5
5
|
"provenance": true
|
|
6
6
|
},
|
|
7
|
-
"version": "2.
|
|
7
|
+
"version": "2.5.1",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -57,6 +57,10 @@
|
|
|
57
57
|
"./utils/remote": {
|
|
58
58
|
"types": "./dist/shared/utils/remote.d.ts",
|
|
59
59
|
"import": "./dist/shared/utils/remote.js"
|
|
60
|
+
},
|
|
61
|
+
"./utils/command": {
|
|
62
|
+
"types": "./dist/shared/utils/command.d.ts",
|
|
63
|
+
"import": "./dist/shared/utils/command.js"
|
|
60
64
|
}
|
|
61
65
|
},
|
|
62
66
|
"peerDependencies": {
|