@webamoki/web-svelte 2.5.0 → 2.5.2

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,8 +1,44 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
1
2
  import type { RemoteCommand } from '@sveltejs/kit';
2
- import { ResponseResult } from './remote.js';
3
- export declare class CommandAction<S, O> {
3
+ import type { CommandResult, CommandSuccess, ResponseError } from './remote.js';
4
+ type RC<Schema extends StandardSchemaV1, O extends CommandSuccess> = RemoteCommand<StandardSchemaV1.InferInput<Schema>, Promise<CommandResult<O>>>;
5
+ type RCInput<Schema extends StandardSchemaV1> = StandardSchemaV1.InferInput<Schema>;
6
+ /**
7
+ * Command Remote function handler for Client
8
+ * - state for dynamic input
9
+ * - execute function, which handles toast and callbacks
10
+ * - submitting state for tracking pending executions
11
+ */
12
+ export declare class CommandAction<Schema extends StandardSchemaV1, O extends CommandSuccess> {
4
13
  #private;
14
+ input: RCInput<Schema>;
5
15
  get submitting(): boolean;
6
- constructor(remote: RemoteCommand<S, Promise<ResponseResult<O>>>);
7
- execute(input: Parameters<RemoteCommand<S, Promise<ResponseResult<O>>>>[0]): Promise<O>;
16
+ constructor(remote: RC<Schema, O>, defaultInput: RCInput<Schema>, config?: {
17
+ onError?: (error: ResponseError) => void;
18
+ onSuccess?: (state: {
19
+ input: RCInput<Schema>;
20
+ result: O;
21
+ }) => void;
22
+ toastError?: boolean;
23
+ toastSuccess?: boolean;
24
+ });
25
+ execute(): Promise<void>;
8
26
  }
27
+ type RCV<O extends CommandSuccess> = RemoteCommand<void, Promise<CommandResult<O>>>;
28
+ /**
29
+ * Command Remote function handler for Client without input
30
+ * - execute function, which handles toast and callbacks
31
+ * - submitting state for tracking pending executions
32
+ */
33
+ export declare class CommandActionVoid<O extends CommandSuccess> {
34
+ #private;
35
+ get submitting(): boolean;
36
+ constructor(remote: RCV<O>, config?: {
37
+ onError?: (error: ResponseError) => void;
38
+ onSuccess?: (result: O) => void;
39
+ toastError?: boolean;
40
+ toastSuccess?: boolean;
41
+ });
42
+ execute(): Promise<void>;
43
+ }
44
+ export {};
@@ -1,19 +1,93 @@
1
1
  // Client side handling
2
- import { ResponseResult } from './remote.js';
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 {
11
+ input = $state();
4
12
  get submitting() {
5
13
  return this.#submitCount > 0;
6
14
  }
15
+ #onError;
16
+ #onSuccess;
7
17
  #remote;
8
18
  #submitCount = $state(0);
9
- constructor(remote) {
19
+ #toastError;
20
+ #toastSuccess;
21
+ constructor(remote, defaultInput, config) {
22
+ this.input = defaultInput;
10
23
  this.#remote = remote;
24
+ this.#toastError = config?.toastError ?? true;
25
+ this.#toastSuccess = config?.toastSuccess ?? true;
26
+ this.#onError = config?.onError;
27
+ this.#onSuccess = config?.onSuccess;
11
28
  }
12
- async execute(input) {
29
+ async execute() {
13
30
  this.#submitCount++;
14
31
  try {
15
- const result = await this.#remote(input);
16
- return ResponseResult.unwrap(result);
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' }));
80
+ if (result.ok) {
81
+ if (this.#toastSuccess)
82
+ toast.success(result.value.message);
83
+ this.#onSuccess?.(result.value);
84
+ return;
85
+ }
86
+ else {
87
+ if (this.#toastError)
88
+ toast.error(`${result.error.code} - ${result.error.message}`);
89
+ this.#onError?.(result.error);
90
+ }
17
91
  }
18
92
  finally {
19
93
  this.#submitCount--;
@@ -1,6 +1,10 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
2
  import { Result } from './functional/index.js';
3
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 ResponseError = {
13
- code: number;
18
+ export type CommandResult<T extends CommandSuccess> = Result<T, ResponseError>;
19
+ export type CommandSuccess = {
14
20
  message: string;
15
21
  };
16
- export type ResponseResult<T> = Result<T, ResponseError>;
17
- export declare const ResponseResult: {
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>;
@@ -5,14 +5,11 @@ export const CheckResult = {
5
5
  /* Helper function for ok check result. */
6
6
  ok() {
7
7
  return Result.ok(undefined);
8
- }
9
- };
10
- export const ResponseResult = {
11
- /**
12
- * Unwraps a ResponseResult, throwing a sveltekit error if it is an error result.
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
- ResponseResult.unwrap(await check());
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
- ResponseResult.unwrap(await check());
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
- ResponseResult.unwrap(await check());
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
- ResponseResult.unwrap(await check());
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.5.0",
7
+ "version": "2.5.2",
8
8
  "license": "MIT",
9
9
  "repository": {
10
10
  "type": "git",