Skip to content

Commit

Permalink
[re fact] upgrade in yup route, validShema to ValidAndFormat for can …
Browse files Browse the repository at this point in the history
…use transformt
  • Loading branch information
leomerida15 committed Sep 19, 2023
1 parent 8a8054e commit fd863a9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/resolvers/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from "./requestFactory";
export * from "./responseFactory";
export * from "./route";
export * from "./types";
export * from "./validSchema";
export * from "./ValidAndFormat";
75 changes: 21 additions & 54 deletions src/resolvers/yup/requestFactory.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,39 @@
import { NextRequest } from "next/server";
import { IYupSchemasValid } from "./types";
import { formatParams } from "../methods/formatParams";
import { headers } from "next/headers";
import { IYupRequestFactoryResp } from "./types";
import { AnyObject, InferType, ISchema } from "yup";
import { AnyObject, InferType, ObjectSchema } from "yup";
import ValidAndFormat from "./ValidAndFormat";

export const requestFactory = async <
B extends ISchema<any>,
C extends ISchema<any>,
Q extends ISchema<AnyObject>,
H extends ISchema<any>,
R extends ISchema<any>,
B extends ObjectSchema<AnyObject>,
C extends ObjectSchema<AnyObject>,
Q extends ObjectSchema<AnyObject>,
H extends ObjectSchema<AnyObject>,
R extends ObjectSchema<AnyObject>,
>(
nativeRequest: NextRequest,
context: InferType<C>,
Schemas?: IYupSchemasValid<B, C, Q, H, R>,
) => {
const body = await (async () => {
const valid_methods = !["DELETE", "GET"].includes(nativeRequest.method);
const validAndFormat = new ValidAndFormat<B, C, Q, H, R>(
nativeRequest,
context,
Schemas,
);

if (!(valid_methods && Schemas?.body)) return {};
const Headers = validAndFormat.headers();

return await nativeRequest.json();
})();
const Context = validAndFormat.context();

const resp = {
getHeaders: headers,
getContext: (): InferType<C> => {
const params = formatParams(context.params);

return { ...context, params };
},
getQuery: (queriesArray: string[]): InferType<Q> => {
//
const resQueries: any = {};

const symbolsReq = Object.getOwnPropertySymbols(nativeRequest);

const UrlNative = symbolsReq
.filter((S) => {
//@ts-ignore
const item = nativeRequest[S];

return item?.url;
})
.map<URL>((S) => {
//@ts-ignore
const item = nativeRequest[S];
const Query = validAndFormat.query();

return item?.url;
})[0];
const body = await validAndFormat.body();

const validUrlNative = Object.keys(nativeRequest).includes("url");

const url = validUrlNative ? new URL(nativeRequest.url) : UrlNative;

queriesArray.map((q: string) => {
const validItem = Number(url.searchParams.get(q));
if (validItem !== 0 && !validItem) {
resQueries[q] = url.searchParams.get(q);
} else {
resQueries[q] = validItem;
}
});

return resQueries;
},
getBody: (): InferType<B> => body,
const resp = {
getHeaders: () => Headers,
getContext: () => Context,
getQuery: (keys: Array<keyof InferType<Q> | string>) => Query(keys),
getBody: () => body,
};

return { ...resp, ...nativeRequest } as IYupRequestFactoryResp<B, C, Q>;
Expand Down
52 changes: 26 additions & 26 deletions src/resolvers/yup/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextRequest } from "next/server";
import { headers } from "next/headers";
import { AnyObject, ISchema, InferType } from "yup";
import { AnyObject, ISchema, InferType, ObjectSchema } from "yup";
import { yupRoute } from "./route";
import { responseFactory } from "./responseFactory";

Expand All @@ -14,47 +14,47 @@ export type YupHandlerReturn =
| ReturnType<ReturnType<YupResponseFactoryType>["next"]>;

export type YupHandlerType<
B extends ISchema<any>,
C extends ISchema<any>,
Q extends ISchema<AnyObject>,
H extends ISchema<any>,
R extends ISchema<any>,
B extends ObjectSchema<AnyObject>,
C extends ObjectSchema<AnyObject>,
Q extends ObjectSchema<AnyObject>,
H extends ObjectSchema<AnyObject>,
R extends ObjectSchema<AnyObject>,
> = IYupRouteParams<B, C, Q, H, R>["Handler"];

export type YupActionReturnType<
B extends ISchema<any>,
C extends ISchema<any>,
Q extends ISchema<AnyObject>,
H extends ISchema<any>,
R extends ISchema<any>,
B extends ObjectSchema<AnyObject>,
C extends ObjectSchema<AnyObject>,
Q extends ObjectSchema<AnyObject>,
H extends ObjectSchema<AnyObject>,
R extends ObjectSchema<AnyObject>,
> = ReturnType<YupHandlerType<B, C, Q, H, R>>;

type MethodKeyType = "Handler";
type MethodIndexType = "0";

export type YupReqType<
B extends ISchema<any>,
C extends ISchema<any>,
Q extends ISchema<AnyObject>,
H extends ISchema<any>,
R extends ISchema<any>,
B extends ObjectSchema<AnyObject>,
C extends ObjectSchema<AnyObject>,
Q extends ObjectSchema<AnyObject>,
H extends ObjectSchema<AnyObject>,
R extends ObjectSchema<AnyObject>,
> = Parameters<IYupRouteParams<B, C, Q, H, R>[MethodKeyType]>[MethodIndexType];

type ShemaKeyType = "schemas";
export type IYupSchema<
B extends ISchema<any>,
C extends ISchema<any>,
Q extends ISchema<AnyObject>,
H extends ISchema<any>,
R extends ISchema<any>,
B extends ObjectSchema<AnyObject>,
C extends ObjectSchema<AnyObject>,
Q extends ObjectSchema<AnyObject>,
H extends ObjectSchema<AnyObject>,
R extends ObjectSchema<AnyObject>,
> = IYupRouteParams<B, C, Q, H, R>[ShemaKeyType];

export interface IYupRouteParams<
B extends ISchema<any>,
C extends ISchema<any>,
Q extends ISchema<AnyObject>,
H extends ISchema<any>,
R extends ISchema<any>,
B extends ObjectSchema<AnyObject>,
C extends ObjectSchema<AnyObject>,
Q extends ObjectSchema<AnyObject>,
H extends ObjectSchema<AnyObject>,
R extends ObjectSchema<AnyObject>,
> {
schemas?: IYupSchemasValid<B, C, Q, H, R>;
Handler: (
Expand Down
36 changes: 0 additions & 36 deletions src/resolvers/yup/validSchema.ts

This file was deleted.

0 comments on commit fd863a9

Please sign in to comment.