diff --git a/libs/rpc/src/client/local.ts b/libs/rpc/src/client/local.ts index 5e6645de..6df80add 100644 --- a/libs/rpc/src/client/local.ts +++ b/libs/rpc/src/client/local.ts @@ -1,3 +1,4 @@ +import { givenFormLike, handlePossibleForm } from "../extract/blobs" import type { AnyFunction, PossibleModule, PrimOptions, RpcCall } from "../interfaces" import { handlePotentialPromise } from "./wrapper" import getProperty from "just-safe-get" @@ -43,8 +44,10 @@ export function handleLocalModule(rpc: RpcCall, options: Prim const method = getProperty(providedModule, rpc.method) as AnyFunction // eslint-disable-next-line @typescript-eslint/no-unsafe-return if (method) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const preprocessed = options.preRequest?.(rpc.args, rpc.method) ?? { args: rpc.args } + if (Array.isArray(preprocessed.args) && givenFormLike(preprocessed.args[0])) { + preprocessed.args[0] = handlePossibleForm(preprocessed.args[0]) + } if ("result" in preprocessed) { return options.postRequest?.(preprocessed.result, rpc.method) ?? preprocessed.result } diff --git a/libs/rpc/src/client/wrapper.test.ts b/libs/rpc/src/client/wrapper.test.ts index eb9891bc..a536bf21 100644 --- a/libs/rpc/src/client/wrapper.test.ts +++ b/libs/rpc/src/client/wrapper.test.ts @@ -1,7 +1,7 @@ import { expect, test, describe } from "vitest" import { handlePotentialPromise } from "./wrapper" -describe("Promises can be easily handled", () => { +describe("Promises can be easily handled without using async functions", () => { test("promise given", async () => { const hello = handlePotentialPromise(Promise.resolve("hello"), value => { return value diff --git a/libs/rpc/src/extract/blobs.ts b/libs/rpc/src/extract/blobs.ts index 48749ca1..42d84c08 100644 --- a/libs/rpc/src/extract/blobs.ts +++ b/libs/rpc/src/extract/blobs.ts @@ -24,7 +24,7 @@ function isBinaryLike(possiblyBinary: unknown) { * @param maybeForm - FormData, FormElement containing FormData, or SubmitEvent (whose target is a FormElement with FormData) * @returns Boolean if given is a form-like object */ -function givenFormLike(maybeForm: unknown): maybeForm is HTMLFormElement | FormData | SubmitEvent { +export function givenFormLike(maybeForm: unknown): maybeForm is HTMLFormElement | FormData | SubmitEvent { return ( (typeof HTMLFormElement === "function" && maybeForm instanceof HTMLFormElement) || (typeof FormData === "function" && maybeForm instanceof FormData) || @@ -38,7 +38,7 @@ function givenFormLike(maybeForm: unknown): maybeForm is HTMLFormElement | FormD * into an object. If given a `SubmitEvent`, prevent page navigation since the * event should be handled by this library. */ -function handlePossibleForm(form: HTMLFormElement | FormData | SubmitEvent) { +export function handlePossibleForm(form: HTMLFormElement | FormData | SubmitEvent) { if (form instanceof SubmitEvent && form.target instanceof HTMLFormElement) { form.preventDefault() } @@ -46,10 +46,10 @@ function handlePossibleForm(form: HTMLFormElement | FormData | SubmitEvent) { form instanceof HTMLFormElement ? new FormData(form) : form instanceof SubmitEvent - ? form.target instanceof HTMLFormElement - ? new FormData(form.target) - : undefined - : form + ? form.target instanceof HTMLFormElement + ? new FormData(form.target) + : undefined + : form const data: Record = {} for (const [key, val] of formData) { if (data[key]) {