Skip to content

Commit

Permalink
improve yup-action
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanfajlur committed Oct 9, 2024
1 parent 88fb7dd commit ecb4500
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions app/utils/yup-action.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import * as yup from 'yup'

export async function yupAction<T extends yup.Schema>(
request: FormData,
schema: T,
): Promise<yup.InferType<T>>

export async function yupAction<T extends yup.Schema>(
request: Request,
schema: T,
type: 'form-data' | 'search-params',
): Promise<yup.InferType<T>> {
type?: 'form-data' | 'search-params',
): Promise<yup.InferType<T>>

export async function yupAction<T extends yup.Schema>(
request: Request | FormData,
schema: T,
type?: 'form-data' | 'search-params',
) {
let data: any = null

if (type === 'form-data') {
if (request instanceof FormData) {
data = Object.fromEntries(request)
}

if (type === 'form-data' && request instanceof Request) {
const formData = await request.formData()
data = Object.fromEntries(formData)
}

if (type === 'search-params') {
if (type === 'search-params' && request instanceof Request) {
const { searchParams } = new URL(request.url)
data = Object.fromEntries(searchParams)
}
Expand Down

0 comments on commit ecb4500

Please sign in to comment.