@sourceregistry/sveltekit-enhance 1.0.1 → 1.1.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.
@@ -53,7 +53,7 @@ declare const createContext: (data: FormData) => {
53
53
  date$: (name: string, parser?: (formdata: FormData, name: string) => number | string | Date) => Date;
54
54
  process: <T, R>(name: string, parser: ((formdata: FormData, name: string) => T) | ((name: string) => T), processor: (val: T, name: string) => R) => R;
55
55
  number: (name: string) => number | undefined;
56
- number$: (name: string) => number | undefined;
56
+ number$: (name: string) => number;
57
57
  boolean: (name: string) => boolean | undefined;
58
58
  boolean$: (name: string) => boolean;
59
59
  json: <T = unknown, F = T>(name: string, transformer?: (val: F) => T) => T | undefined;
@@ -102,7 +102,7 @@ export declare function string$(formdata: FormData, name: string): string;
102
102
  */
103
103
  export declare function pattern$(formdata: FormData, name: string, pattern: string | RegExp): string;
104
104
  export declare function number(formdata: FormData, name: string): number | undefined;
105
- export declare function number$(formdata: FormData, name: string): number | undefined;
105
+ export declare function number$(formdata: FormData, name: string): number;
106
106
  export declare function boolean(formdata: FormData, name: string): boolean | undefined;
107
107
  export declare function boolean$(formdata: FormData, name: string): boolean;
108
108
  export declare function date(formdata: FormData, name: string, parser?: (formdata: FormData, name: string) => number | string | Date | undefined): Date | undefined;
@@ -190,7 +190,7 @@ export declare const schema: <const T extends AnyValidator>(validator: T, option
190
190
  readonly date$: (name: string, parser?: (formdata: FormData, name: string) => number | string | Date) => Date;
191
191
  readonly process: <T_1, R>(name: string, parser: ((formdata: FormData, name: string) => T_1) | ((name: string) => T_1), processor: (val: T_1, name: string) => R) => R;
192
192
  readonly number: (name: string) => number | undefined;
193
- readonly number$: (name: string) => number | undefined;
193
+ readonly number$: (name: string) => number;
194
194
  readonly boolean: (name: string) => boolean | undefined;
195
195
  readonly boolean$: (name: string) => boolean;
196
196
  readonly json: <T_1 = unknown, F = T_1>(name: string, transformer?: (val: F) => T_1) => T_1 | undefined;
@@ -270,7 +270,7 @@ export declare const Form: {
270
270
  readonly date$: (name: string, parser?: (formdata: FormData, name: string) => number | string | Date) => Date;
271
271
  readonly process: <T_1, R>(name: string, parser: ((formdata: FormData, name: string) => T_1) | ((name: string) => T_1), processor: (val: T_1, name: string) => R) => R;
272
272
  readonly number: (name: string) => number | undefined;
273
- readonly number$: (name: string) => number | undefined;
273
+ readonly number$: (name: string) => number;
274
274
  readonly boolean: (name: string) => boolean | undefined;
275
275
  readonly boolean$: (name: string) => boolean;
276
276
  readonly json: <T_1 = unknown, F = T_1>(name: string, transformer?: (val: F) => T_1) => T_1 | undefined;
@@ -133,7 +133,7 @@ export function number(formdata, name) {
133
133
  export function number$(formdata, name) {
134
134
  const _number = number(formdata, name);
135
135
  if (_number === undefined) {
136
- fail(400, { targets: [name], message: `${name} is required` });
136
+ return fail(400, { targets: [name], message: `${name} is required` });
137
137
  }
138
138
  return _number;
139
139
  }
@@ -425,7 +425,7 @@ export function selector$(formData, cases, useArray = false) {
425
425
  if ('$error' in cases) {
426
426
  cases?.['$error']?.('Unable to find value');
427
427
  }
428
- fail(400, { targets: Object.keys(cases), message: `Unable to find value` });
428
+ return fail(400, { targets: Object.keys(cases), message: `Unable to find value` });
429
429
  }
430
430
  return result;
431
431
  }
package/dist/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { type Action, type ActionResult, type Cookies, type Handle, type RequestEvent, type ResolveOptions, type ServerLoadEvent } from '@sveltejs/kit';
2
2
  import type { RouteId as AppRouteId, LayoutParams as AppLayoutParams } from '$app/types';
3
3
  export type MaybePromise<T> = T | Promise<T>;
4
- export type EnhanceErrorHandler = <T = any>(err: unknown) => MaybePromise<T> | undefined | never | void;
4
+ export type EnhanceErrorHandler = <T = any>(err: unknown) => MaybePromise<T | Response | undefined | never | void>;
5
5
  export type EnhanceResponseHandler = (input: {
6
6
  event: RequestEvent;
7
7
  response: Response;
8
8
  }) => MaybePromise<unknown>;
9
9
  export type EnhanceCallType = 'handle' | 'load' | 'method' | 'action';
10
- export type EnhanceInput<CallType extends EnhanceCallType = EnhanceCallType, Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>, RouteId extends AppRouteId | null = AppRouteId | null, ParentData extends Record<string, any> = Record<string, any>> = {
10
+ export type EnhanceInput<CallType extends EnhanceCallType = EnhanceCallType, Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>, RouteId extends AppRouteId | string | null = AppRouteId | string | null, ParentData extends Record<string, any> = Record<string, any>> = {
11
11
  cookies: Cookies;
12
12
  params: Params;
13
13
  route: {
@@ -28,8 +28,8 @@ export type EnhanceInput<CallType extends EnhanceCallType = EnhanceCallType, Par
28
28
  depends: ServerLoadEvent<Params, ParentData, RouteId>['depends'];
29
29
  untrack: ServerLoadEvent<Params, ParentData, RouteId>['untrack'];
30
30
  } : object);
31
- export type EnhanceFunction<CallType extends EnhanceCallType = EnhanceCallType, Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>, RouteId extends AppRouteId | null = AppRouteId | null, EnhanceReturn = any> = (event: EnhanceInput<CallType, Params, RouteId>) => MaybePromise<EnhanceReturn>;
32
- export type EnhanceAction<Params extends AppLayoutParams<'/'>, OutputData extends Record<string, any> | void, RouteId extends AppRouteId | null, EnhanceReturn extends ActionResult | never | any = ActionResult> = (event: RequestEvent<Params, RouteId> & {
31
+ export type EnhanceFunction<CallType extends EnhanceCallType = EnhanceCallType, Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>, RouteId extends AppRouteId | string | null = AppRouteId | string | null, EnhanceReturn = any> = (event: EnhanceInput<CallType, Params, RouteId>) => MaybePromise<EnhanceReturn>;
32
+ export type EnhanceAction<Params extends AppLayoutParams<'/'>, OutputData extends Record<string, any> | void, RouteId extends AppRouteId | string | null, EnhanceReturn extends ActionResult | never | any = ActionResult> = (event: RequestEvent<Params, RouteId> & {
33
33
  context: EnhanceReturn;
34
34
  }) => MaybePromise<OutputData>;
35
35
  export type EnhanceLoad<Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>, ParentData extends Record<string, any> = Record<string, any>, OutputData extends Record<string, any> | void = Record<string, any> | void, RouteId extends AppRouteId | null = AppRouteId | null, EnhanceReturn extends never | any = any> = (event: ServerLoadEvent<Params, ParentData, RouteId> & {
@@ -44,14 +44,14 @@ export type EnhanceHandle<EnhanceReturn extends never | any = any> = (input: {
44
44
  export type EnhanceMethod<Params extends AppLayoutParams<'/'> = AppLayoutParams<'/'>, RouteId extends AppRouteId | null = AppRouteId | null, EnhanceReturn extends never | any = any> = (event: RequestEvent<Params, RouteId> & {
45
45
  context: EnhanceReturn;
46
46
  }) => MaybePromise<Response>;
47
- export declare const action: <Params extends AppLayoutParams<"/"> = AppLayoutParams<"/">, OutputData extends Record<string, any> | void = Record<string, any> | void, RouteId extends AppRouteId | null = AppRouteId | 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>;
47
+ export declare const action: <Params extends AppLayoutParams<"/"> = AppLayoutParams<"/">, OutputData extends Record<string, any> | void = Record<string, any> | void, RouteId extends AppRouteId | string | null = AppRouteId | 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>;
48
48
  export declare const load: <Params extends AppLayoutParams<"/"> = AppLayoutParams<"/">, ParentData extends Record<string, any> = Record<string, any>, OutputData extends Record<string, any> | void = Record<string, any> | void, RouteId extends AppRouteId | null = AppRouteId | 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>;
49
- export declare const method: <Params extends AppLayoutParams<"/"> = AppLayoutParams<"/">, RouteId extends AppRouteId | null = AppRouteId | 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>;
49
+ 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>;
50
50
  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;
51
51
  export declare const enhance: {
52
- action: <Params extends AppLayoutParams<"/"> = Record<string, string>, OutputData extends Record<string, any> | void = void | Record<string, any>, RouteId extends AppRouteId | 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>;
52
+ action: <Params extends AppLayoutParams<"/"> = Record<string, string>, 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>;
53
53
  load: <Params extends AppLayoutParams<"/"> = Record<string, string>, ParentData extends Record<string, any> = Record<string, any>, OutputData extends Record<string, any> | void = void | Record<string, any>, RouteId extends AppRouteId | null = string | 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>;
54
- method: <Params extends AppLayoutParams<"/"> = Record<string, string>, RouteId extends AppRouteId | 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>;
54
+ method: <Params extends AppLayoutParams<"/"> = Record<string, string>, 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>;
55
55
  handle: <const Enhances extends readonly EnhanceFunction<"handle">[] = readonly EnhanceFunction<"handle", Record<string, string>, string | null, any>[], EnhanceReturn extends Awaited<ConcatReturnTypes<Enhances>> = Awaited<ConcatReturnTypes<Enhances>>>(handle: EnhanceHandle<EnhanceReturn>, ...contexts: Enhances) => Handle;
56
56
  };
57
57
  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>>;
package/dist/index.js CHANGED
@@ -25,8 +25,13 @@ const EnhanceErrorHandle = async (e, contextInput) => {
25
25
  }
26
26
  throw e;
27
27
  };
28
- export const action = (action, ...enhances) => async (event) => {
28
+ export const action = (action, ...enhances
29
+ // @ts-expect-error relax the route_id when integrating with libraries
30
+ ) =>
31
+ // @ts-expect-error relax the route_id when integrating with libraries
32
+ async (event) => {
29
33
  let combined = {};
34
+ // @ts-expect-error relax the route_id when integrating with libraries
30
35
  const input = Object.assign(event, {
31
36
  __errorHandlers__: [],
32
37
  isAction: false,
@@ -79,9 +84,13 @@ export const load = (load, ...contexts) => {
79
84
  }
80
85
  };
81
86
  };
82
- export const method = (handle, ...contexts) => {
87
+ export const method = (
88
+ // @ts-expect-error this is to relax the typing around external integrations
89
+ handle, ...contexts) => {
90
+ // @ts-expect-error this is to relax the typing around external integrations
83
91
  return async (event) => {
84
92
  let combined = {};
93
+ // @ts-expect-error this is to relax the typing around external integrations
85
94
  const contextInput = Object.assign(event, {
86
95
  __errorHandlers__: [],
87
96
  isAction: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sourceregistry/sveltekit-enhance",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
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
6
  "scripts": {