diff --git a/src/resolvers/yup/ValidAndFormat.ts b/src/resolvers/yup/ValidAndFormat.ts new file mode 100644 index 0000000..ea75c14 --- /dev/null +++ b/src/resolvers/yup/ValidAndFormat.ts @@ -0,0 +1,119 @@ +import { NextRequest } from "next/server"; +import { ObjectSchema, AnyObject, InferType } from "yup"; +import { IYupSchemasValid } from "../types"; +import { headers as Headers } from "next/headers"; +import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers"; + +export default class ValidAndFormat< + B extends ObjectSchema, + C extends ObjectSchema, + Q extends ObjectSchema, + H extends ObjectSchema, + R extends ObjectSchema, +> { + Schemas?: IYupSchemasValid; + NativeContext: InferType; + nativeRequest: NextRequest; + bodyNative: InferType = {}; + + constructor( + nativeRequest: NextRequest, + context: InferType, + Schemas?: IYupSchemasValid, + ) { + this.Schemas = Schemas; + this.NativeContext = context; + this.nativeRequest = nativeRequest; + } + + headers(): InferType | ReadonlyHeaders { + if (!this.Schemas?.headers) return Headers(); + + return this.Schemas.headers.validateSync(Headers()); + } + + context(): InferType { + if (!this.Schemas?.context) return this.NativeContext; + + return this.Schemas.context.validateSync(this.NativeContext); + } + + private getQueryWhoNoHasSchema( + queriesArray: Array>, + ): Partial> { + // + const resQueries: any = {}; + + const symbolsReq = Object.getOwnPropertySymbols(this.nativeRequest); + + const UrlNative = symbolsReq + .filter((S) => { + //@ts-ignore + const item = nativeRequest[S]; + + return item?.url; + }) + .map((S) => { + //@ts-ignore + const item = nativeRequest[S]; + + return item?.url; + })[0]; + + const validUrlNative = Object.keys(this.nativeRequest).includes("url"); + + const url = validUrlNative ? new URL(this.nativeRequest.url) : UrlNative; + + queriesArray.map((q) => { + resQueries[q] = url.searchParams.get(String(q)); + }); + + return resQueries; + } + + private createGetQueryWhoHasSchema(queryFormat: InferType) { + return (queriesArray: Array>): Partial> => { + const querysEntrys = Object.entries(queryFormat); + + const queryFilter = querysEntrys.filter(([k]) => + queriesArray.includes(k), + ); + + const queryObj = Object.fromEntries(queryFilter) as Partial>; + + return queryObj; + }; + } + + query() { + if (!this.Schemas?.query) return this.getQueryWhoNoHasSchema; + + const keys = Object.keys(this.Schemas.query.shape); + + const query = this.getQueryWhoNoHasSchema(keys); + + const queryFormat = this.Schemas.query.validateSync(query) as InferType; + + const getQueryWhoHasSchema = this.createGetQueryWhoHasSchema(queryFormat); + + return getQueryWhoHasSchema; + } + + private async defineBody() { + const valid_methods = !["DELETE", "GET"].includes( + this.nativeRequest.method, + ); + + if (valid_methods && this.Schemas?.body) { + this.bodyNative = await this.nativeRequest.json(); + } + } + + async body(): Promise> { + await this.defineBody(); + + if (!this.Schemas?.body) return this.bodyNative; + + return this.Schemas.body.validateSync(this.bodyNative); + } +}