From 99c34ffa8c92e53e34644b328cacc13500835d8a Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 25 Oct 2024 18:16:03 +0800 Subject: [PATCH 1/5] add action failure helper --- packages/kit/src/exports/index.js | 9 +++++++++ packages/kit/types/index.d.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/kit/src/exports/index.js b/packages/kit/src/exports/index.js index 32fb827dc629..9adf1ea03648 100644 --- a/packages/kit/src/exports/index.js +++ b/packages/kit/src/exports/index.js @@ -111,6 +111,15 @@ export function isRedirect(e) { return e instanceof Redirect; } +/** + * Checks whether this is an action failure thrown by {@link fail}. + * @param {unknown} e The object to check. + * @return {e is ActionFailure} + */ +export function isActionFailure(e) { + return e instanceof ActionFailure; +} + /** * Create a JSON `Response` object from the supplied data. * @param {any} data The value that will be serialized as JSON. diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index a9634841c4cd..da7a0bc1fad3 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1804,6 +1804,11 @@ declare module '@sveltejs/kit' { * @param e The object to check. * */ export function isRedirect(e: unknown): e is Redirect_1; + /** + * Checks whether this is an action failure thrown by {@link fail}. + * @param e The object to check. + * */ + export function isActionFailure(e: unknown): e is ActionFailure_1; /** * Create a JSON `Response` object from the supplied data. * @param data The value that will be serialized as JSON. @@ -1846,6 +1851,13 @@ declare module '@sveltejs/kit' { location: string; } + class ActionFailure_1 | undefined = undefined> { + + constructor(status: number, data: T); + status: number; + data: T; + } + export {}; } From 85d07d65e0ca9c8e81a87a9bf7913920770008fb Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 25 Oct 2024 18:16:49 +0800 Subject: [PATCH 2/5] changeset --- .changeset/fresh-guests-knock.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fresh-guests-knock.md diff --git a/.changeset/fresh-guests-knock.md b/.changeset/fresh-guests-knock.md new file mode 100644 index 000000000000..19db1582ae4e --- /dev/null +++ b/.changeset/fresh-guests-knock.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': minor +--- + +feat: add helper to identify `ActionFailure` objects From 504a87f682bee10816c5462c1b2e805f04e9c1bf Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Sat, 26 Oct 2024 14:56:38 +0800 Subject: [PATCH 3/5] change to interface instead --- packages/kit/src/exports/index.js | 2 +- packages/kit/types/index.d.ts | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/kit/src/exports/index.js b/packages/kit/src/exports/index.js index 9adf1ea03648..b6befad6c7a4 100644 --- a/packages/kit/src/exports/index.js +++ b/packages/kit/src/exports/index.js @@ -114,7 +114,7 @@ export function isRedirect(e) { /** * Checks whether this is an action failure thrown by {@link fail}. * @param {unknown} e The object to check. - * @return {e is ActionFailure} + * @return {e is import('./public.js').ActionFailure} */ export function isActionFailure(e) { return e instanceof ActionFailure; diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 5920a9c30b24..22a41020b97f 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1807,7 +1807,7 @@ declare module '@sveltejs/kit' { * Checks whether this is an action failure thrown by {@link fail}. * @param e The object to check. * */ - export function isActionFailure(e: unknown): e is ActionFailure_1; + export function isActionFailure(e: unknown): e is import("@sveltejs/kit").ActionFailure; /** * Create a JSON `Response` object from the supplied data. * @param data The value that will be serialized as JSON. @@ -1830,7 +1830,7 @@ declare module '@sveltejs/kit' { * @param status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599. * @param data Data associated with the failure (e.g. validation errors) * */ - export function fail | undefined = undefined>(status: number, data: T): ActionFailure; + export function fail | undefined = undefined>(status: number, data: T): import("@sveltejs/kit").ActionFailure; export type LessThan = TNumber extends TArray['length'] ? TArray[number] : LessThan; export type NumericRange = Exclude, LessThan>; export const VERSION: string; @@ -1850,13 +1850,6 @@ declare module '@sveltejs/kit' { location: string; } - class ActionFailure_1 | undefined = undefined> { - - constructor(status: number, data: T); - status: number; - data: T; - } - export {}; } From 7bfa82d8ad61417a43f4d0126b6b45ae7cd0e2f0 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Sat, 26 Oct 2024 14:57:06 +0800 Subject: [PATCH 4/5] use public.js --- packages/kit/types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 22a41020b97f..15d722df8bdd 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1807,7 +1807,7 @@ declare module '@sveltejs/kit' { * Checks whether this is an action failure thrown by {@link fail}. * @param e The object to check. * */ - export function isActionFailure(e: unknown): e is import("@sveltejs/kit").ActionFailure; + export function isActionFailure(e: unknown): e is ActionFailure; /** * Create a JSON `Response` object from the supplied data. * @param data The value that will be serialized as JSON. @@ -1830,7 +1830,7 @@ declare module '@sveltejs/kit' { * @param status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses). Must be in the range 400-599. * @param data Data associated with the failure (e.g. validation errors) * */ - export function fail | undefined = undefined>(status: number, data: T): import("@sveltejs/kit").ActionFailure; + export function fail | undefined = undefined>(status: number, data: T): ActionFailure; export type LessThan = TNumber extends TArray['length'] ? TArray[number] : LessThan; export type NumericRange = Exclude, LessThan>; export const VERSION: string; From ac9a3258202a564ff2b661d41e656eeae068f917 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Sat, 26 Oct 2024 14:59:34 +0800 Subject: [PATCH 5/5] move this below fail --- packages/kit/src/exports/index.js | 18 +++++++++--------- packages/kit/types/index.d.ts | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/kit/src/exports/index.js b/packages/kit/src/exports/index.js index b6befad6c7a4..5454a2e15e31 100644 --- a/packages/kit/src/exports/index.js +++ b/packages/kit/src/exports/index.js @@ -111,15 +111,6 @@ export function isRedirect(e) { return e instanceof Redirect; } -/** - * Checks whether this is an action failure thrown by {@link fail}. - * @param {unknown} e The object to check. - * @return {e is import('./public.js').ActionFailure} - */ -export function isActionFailure(e) { - return e instanceof ActionFailure; -} - /** * Create a JSON `Response` object from the supplied data. * @param {any} data The value that will be serialized as JSON. @@ -199,3 +190,12 @@ export function fail(status, data) { // @ts-expect-error unique symbol missing return new ActionFailure(status, data); } + +/** + * Checks whether this is an action failure thrown by {@link fail}. + * @param {unknown} e The object to check. + * @return {e is import('./public.js').ActionFailure} + */ +export function isActionFailure(e) { + return e instanceof ActionFailure; +} diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 15d722df8bdd..6382dc1cf8bb 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1803,11 +1803,6 @@ declare module '@sveltejs/kit' { * @param e The object to check. * */ export function isRedirect(e: unknown): e is Redirect_1; - /** - * Checks whether this is an action failure thrown by {@link fail}. - * @param e The object to check. - * */ - export function isActionFailure(e: unknown): e is ActionFailure; /** * Create a JSON `Response` object from the supplied data. * @param data The value that will be serialized as JSON. @@ -1831,6 +1826,11 @@ declare module '@sveltejs/kit' { * @param data Data associated with the failure (e.g. validation errors) * */ export function fail | undefined = undefined>(status: number, data: T): ActionFailure; + /** + * Checks whether this is an action failure thrown by {@link fail}. + * @param e The object to check. + * */ + export function isActionFailure(e: unknown): e is ActionFailure; export type LessThan = TNumber extends TArray['length'] ? TArray[number] : LessThan; export type NumericRange = Exclude, LessThan>; export const VERSION: string;