@webamoki/web-svelte 2.5.1 → 2.5.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.
@@ -1,14 +1,42 @@
1
1
  import type { RemoteCommand } from '@sveltejs/kit';
2
2
  import type { CommandResult, CommandSuccess, ResponseError } from './remote.js';
3
+ type RC<S, O extends CommandSuccess> = RemoteCommand<S, Promise<CommandResult<O>>>;
4
+ /**
5
+ * Command Remote function handler for Client
6
+ * - state for dynamic input
7
+ * - execute function, which handles toast and callbacks
8
+ * - submitting state for tracking pending executions
9
+ */
3
10
  export declare class CommandAction<S, O extends CommandSuccess> {
4
11
  #private;
5
- input: Parameters<RemoteCommand<S, Promise<CommandResult<O>>>>[0];
12
+ input: S;
6
13
  get submitting(): boolean;
7
- constructor(remote: RemoteCommand<S, Promise<CommandResult<O>>>, defaultInput: Parameters<RemoteCommand<S, Promise<CommandResult<O>>>>[0], config?: {
14
+ constructor(remote: RC<S, O>, defaultInput: S, config?: {
15
+ onError?: (error: ResponseError) => void;
16
+ onSuccess?: (state: {
17
+ input: S;
18
+ result: O;
19
+ }) => void;
20
+ toastError?: boolean;
21
+ toastSuccess?: boolean;
22
+ });
23
+ execute(): Promise<void>;
24
+ }
25
+ type RCV<O extends CommandSuccess> = RemoteCommand<void, Promise<CommandResult<O>>>;
26
+ /**
27
+ * Command Remote function handler for Client without input
28
+ * - execute function, which handles toast and callbacks
29
+ * - submitting state for tracking pending executions
30
+ */
31
+ export declare class CommandActionVoid<O extends CommandSuccess> {
32
+ #private;
33
+ get submitting(): boolean;
34
+ constructor(remote: RCV<O>, config?: {
8
35
  onError?: (error: ResponseError) => void;
9
36
  onSuccess?: (result: O) => void;
10
37
  toastError?: boolean;
11
38
  toastSuccess?: boolean;
12
39
  });
13
- execute(): Promise<O>;
40
+ execute(): Promise<void>;
14
41
  }
42
+ export {};
@@ -1,5 +1,12 @@
1
1
  // Client side handling
2
2
  import { toast } from 'svelte-sonner';
3
+ import { Result } from './functional/result.js';
4
+ /**
5
+ * Command Remote function handler for Client
6
+ * - state for dynamic input
7
+ * - execute function, which handles toast and callbacks
8
+ * - submitting state for tracking pending executions
9
+ */
3
10
  export class CommandAction {
4
11
  input = $state();
5
12
  get submitting() {
@@ -22,18 +29,64 @@ export class CommandAction {
22
29
  async execute() {
23
30
  this.#submitCount++;
24
31
  try {
25
- const result = await this.#remote(this.input);
32
+ const input = this.input;
33
+ const result = await this.#remote(input).catch(() => Result.err({ code: 500, message: 'Internal Server Error' }));
34
+ if (result.ok) {
35
+ if (this.#toastSuccess)
36
+ toast.success(result.value.message);
37
+ this.#onSuccess?.({
38
+ input: input,
39
+ result: result.value
40
+ });
41
+ return;
42
+ }
43
+ else {
44
+ if (this.#toastError)
45
+ toast.error(`${result.error.code} - ${result.error.message}`);
46
+ this.#onError?.(result.error);
47
+ }
48
+ }
49
+ finally {
50
+ this.#submitCount--;
51
+ }
52
+ }
53
+ }
54
+ /**
55
+ * Command Remote function handler for Client without input
56
+ * - execute function, which handles toast and callbacks
57
+ * - submitting state for tracking pending executions
58
+ */
59
+ export class CommandActionVoid {
60
+ get submitting() {
61
+ return this.#submitCount > 0;
62
+ }
63
+ #onError;
64
+ #onSuccess;
65
+ #remote;
66
+ #submitCount = $state(0);
67
+ #toastError;
68
+ #toastSuccess;
69
+ constructor(remote, config) {
70
+ this.#remote = remote;
71
+ this.#toastError = config?.toastError ?? true;
72
+ this.#toastSuccess = config?.toastSuccess ?? true;
73
+ this.#onError = config?.onError;
74
+ this.#onSuccess = config?.onSuccess;
75
+ }
76
+ async execute() {
77
+ this.#submitCount++;
78
+ try {
79
+ const result = await this.#remote().catch(() => Result.err({ code: 500, message: 'Internal Server Error' }));
26
80
  if (result.ok) {
27
81
  if (this.#toastSuccess)
28
82
  toast.success(result.value.message);
29
83
  this.#onSuccess?.(result.value);
30
- return result.value;
84
+ return;
31
85
  }
32
86
  else {
33
87
  if (this.#toastError)
34
88
  toast.error(`${result.error.code} - ${result.error.message}`);
35
89
  this.#onError?.(result.error);
36
- throw result.error;
37
90
  }
38
91
  }
39
92
  finally {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public",
5
5
  "provenance": true
6
6
  },
7
- "version": "2.5.1",
7
+ "version": "2.5.3",
8
8
  "license": "MIT",
9
9
  "repository": {
10
10
  "type": "git",