Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egde wrappers #86

Draft
wants to merge 3 commits into
base: edge-response
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { UnauthorizedException, MiddlewareEdge } from "nextlove"
import { UnauthorizedException, Middleware } from "nextlove"
import { NextloveRequest, NextloveResponse } from "nextlove/dist/edge-helpers"

export const withAuthTokenEdge: MiddlewareEdge<NextloveRequest,
export const withAuthTokenEdge: Middleware<
NextloveRequest,
NextloveResponse,
{
auth: {
Expand Down
13 changes: 9 additions & 4 deletions apps/example-todo-app/src/lib/middlewares/with-auth-token.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { NextApiRequest, NextApiResponse } from "next"
import { UnauthorizedException, Middleware } from "nextlove"

export const withAuthToken: Middleware<{
auth: {
authorized_by: "auth_token"
export const withAuthToken: Middleware<
NextApiRequest,
NextApiResponse,
{
auth: {
authorized_by: "auth_token"
}
}
}> = (next) => async (req, res) => {
> = (next) => async (req, res) => {
if (req.headers.authorization?.split("Bearer ")?.[1] !== "auth_token") {
throw new UnauthorizedException({
type: "unauthorized",
Expand Down
3 changes: 1 addition & 2 deletions packages/nextlove/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export * from "./with-route-spec"
export * from "./with-route-spec-edge"
export * from "./http-exceptions"
export * from "./types-edge"
export * from "./wrappers-edge"
export * from "./wrappers-nodejs"
export * from "./wrappers"

28 changes: 8 additions & 20 deletions packages/nextlove/src/types-edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { z } from "zod"
import { SecuritySchemeObject } from "openapi3-ts"
import { NextloveRequest, NextloveResponse } from "../edge-helpers"
import { NextResponse } from "next/server"
import { MiddlewareEdge } from "../wrappers-edge"
import { Middleware } from "../wrappers"
import { MiddlewareChainOutput } from "../types"

type ParamDef = z.ZodTypeAny | z.ZodEffects<z.ZodTypeAny>

Expand All @@ -13,7 +14,7 @@ export interface RouteSpecEdge<
JsonBody extends ParamDef = z.ZodObject<any, any, any, any, any>,
QueryParams extends ParamDef = z.ZodObject<any, any, any, any, any>,
CommonParams extends ParamDef = z.ZodObject<any, any, any, any, any>,
Middlewares extends readonly MiddlewareEdge<any, any, any, any>[] = any[],
Middlewares extends readonly Middleware<any, any, any, any>[] = any[],
JsonResponse extends ParamDef = z.ZodObject<any, any, any, any, any>,
FormData extends ParamDef = z.ZodTypeAny
> {
Expand All @@ -28,26 +29,13 @@ export interface RouteSpecEdge<
formData?: FormData
}

export type MiddlewareEdgeChainOutput<
MWChain extends readonly MiddlewareEdge<any, any, any, any>[]
> = MWChain extends readonly []
? {}
: MWChain extends readonly [infer First, ...infer Rest]
? First extends MiddlewareEdge<any, any, infer T, any>
? T &
(Rest extends readonly MiddlewareEdge<any, any, any, any>[]
? MiddlewareEdgeChainOutput<Rest>
: never)
: never
: never

export type AuthMiddlewaresEdge = {
[auth_type: string]: MiddlewareEdge<any, any, any, any>
[auth_type: string]: Middleware<any, any, any, any>
}

export interface SetupParamsEdge<
AuthMW extends AuthMiddlewaresEdge = AuthMiddlewaresEdge,
GlobalMW extends MiddlewareEdge<any, any, any, any>[] = any[]
GlobalMW extends Middleware<any, any, any, any>[] = any[]
> {
authMiddlewareMap: AuthMW
globalMiddlewares: GlobalMW
Expand Down Expand Up @@ -98,16 +86,16 @@ export type RouteEdgeFunction<
RS extends RouteSpecEdge
> = (
req: (SP["authMiddlewareMap"] &
typeof defaultMiddlewareMap)[RS["auth"]] extends MiddlewareEdge<
typeof defaultMiddlewareMap)[RS["auth"]] extends Middleware<
any,
any,
infer AuthMWOut,
any
>
? Omit<NextloveRequest, "responseEdge"> &
AuthMWOut &
MiddlewareEdgeChainOutput<
RS["middlewares"] extends readonly MiddlewareEdge<any, any, any, any>[]
MiddlewareChainOutput<
RS["middlewares"] extends readonly Middleware<any, any, any, any>[]
? [...SP["globalMiddlewares"], ...RS["middlewares"]]
: SP["globalMiddlewares"]
> & {
Expand Down
18 changes: 10 additions & 8 deletions packages/nextlove/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextApiResponse, NextApiRequest } from "next"
import { z } from "zod"
import { HTTPMethods } from "../with-route-spec/middlewares/with-methods"
import { SecuritySchemeObject } from "openapi3-ts"
import { Middleware } from "../wrappers-nodejs"
import { Middleware } from "../wrappers"

type ParamDef = z.ZodTypeAny | z.ZodEffects<z.ZodTypeAny>

Expand All @@ -12,7 +12,7 @@ export interface RouteSpec<
JsonBody extends ParamDef = z.ZodObject<any, any, any, any, any>,
QueryParams extends ParamDef = z.ZodObject<any, any, any, any, any>,
CommonParams extends ParamDef = z.ZodObject<any, any, any, any, any>,
Middlewares extends readonly Middleware<any, any>[] = any[],
Middlewares extends readonly Middleware<any, any, any, any>[] = any[],
JsonResponse extends ParamDef = z.ZodObject<any, any, any, any, any>,
FormData extends ParamDef = z.ZodTypeAny
> {
Expand All @@ -27,25 +27,25 @@ export interface RouteSpec<
}

export type MiddlewareChainOutput<
MWChain extends readonly Middleware<any, any>[]
MWChain extends readonly Middleware<any, any, any, any>[]
> = MWChain extends readonly []
? {}
: MWChain extends readonly [infer First, ...infer Rest]
? First extends Middleware<infer T, any>
? First extends Middleware<any, any, infer T, any>
? T &
(Rest extends readonly Middleware<any, any>[]
(Rest extends readonly Middleware<any, any, any, any>[]
? MiddlewareChainOutput<Rest>
: never)
: never
: never

export type AuthMiddlewares = {
[auth_type: string]: Middleware<any, any>
[auth_type: string]: Middleware<any, any, any, any>
}

export interface SetupParams<
AuthMW extends AuthMiddlewares = AuthMiddlewares,
GlobalMW extends Middleware<any, any>[] = any[]
GlobalMW extends Middleware<any, any, any, any>[] = any[]
> {
authMiddlewareMap: AuthMW
globalMiddlewares: GlobalMW
Expand Down Expand Up @@ -94,13 +94,15 @@ export type RouteFunction<
> = (
req: (SP["authMiddlewareMap"] &
typeof defaultMiddlewareMap)[RS["auth"]] extends Middleware<
any,
any,
infer AuthMWOut,
any
>
? Omit<NextApiRequest, "query" | "body"> &
AuthMWOut &
MiddlewareChainOutput<
RS["middlewares"] extends readonly Middleware<any, any>[]
RS["middlewares"] extends readonly Middleware<any, any, any, any>[]
? [...SP["globalMiddlewares"], ...RS["middlewares"]]
: SP["globalMiddlewares"]
> & {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextlove/src/with-route-spec-edge/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { wrappersEdge } from "../wrappers-edge"
import { wrappers } from "../wrappers"
import { withValidationEdge } from "./with-validation-edge"
import { NextloveRequest, NextloveResponse, getResponse } from "../edge-helpers"
import { CreateWithRouteSpecEdgeFunction, RouteSpecEdge } from "../types-edge"
Expand Down Expand Up @@ -42,7 +42,7 @@ export const createWithRouteSpecEdge: CreateWithRouteSpecEdgeFunction = ((
const auth_middleware = authMiddlewareMap[spec.auth]
if (!auth_middleware) throw new Error(`Unknown auth type: ${spec.auth}`)

return wrappersEdge<
return wrappers<
NextloveRequest,
NextloveResponse,
NextloveRequest,
Expand Down
6 changes: 4 additions & 2 deletions packages/nextlove/src/with-route-spec/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiResponse, NextApiRequest } from "next"
import { withExceptionHandling } from "../exceptions-middleware-nodejs"
import wrappers, { Middleware } from "../wrappers-nodejs"
import { Middleware, wrappers } from "../wrappers"
import { CreateWithRouteSpecFunction, RouteSpec } from "../types"
import withMethods, { HTTPMethods } from "./middlewares/with-methods"
import withValidation from "./middlewares/with-validation"
Expand All @@ -14,7 +14,9 @@ export const checkRouteSpec = <
JsonBody extends ParamDef = z.ZodTypeAny,
QueryParams extends ParamDef = z.ZodTypeAny,
CommonParams extends ParamDef = z.ZodTypeAny,
Middlewares extends readonly Middleware<any, any>[] = readonly Middleware<
Middlewares extends readonly Middleware<any, any, any, any>[] = readonly Middleware<
any,
any,
any,
any
>[],
Expand Down
Loading
Loading