Skip to content

Commit

Permalink
FormData and FormEvents can be handled on locally provided (partial) …
Browse files Browse the repository at this point in the history
…module
  • Loading branch information
doseofted committed Mar 23, 2024
1 parent 7368d11 commit cfc6eb0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion libs/rpc/src/client/local.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -43,8 +44,10 @@ export function handleLocalModule(rpc: RpcCall<string, unknown[]>, 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
}
Expand Down
2 changes: 1 addition & 1 deletion libs/rpc/src/client/wrapper.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions libs/rpc/src/extract/blobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand All @@ -38,18 +38,18 @@ 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()
}
const formData =
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<string, FormDataEntryValue | FormDataEntryValue[]> = {}
for (const [key, val] of formData) {
if (data[key]) {
Expand Down

0 comments on commit cfc6eb0

Please sign in to comment.