generated from Blazity/next-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
133 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextResponse } from "next/server" | ||
|
||
export class HttpError extends Error { | ||
status: number | ||
tag: string | ||
readonly isHttpError = true | ||
|
||
constructor(tag: string, status: number) { | ||
super() | ||
this.tag = tag | ||
this.status = status | ||
} | ||
} | ||
|
||
export function isHttpError(error: unknown): error is HttpError { | ||
return (error as HttpError).isHttpError | ||
} | ||
|
||
export function errorToNextResponse(error: unknown) { | ||
if (!isHttpError(error)) return NextResponse.json({ message: "UnexpectedError" }, { status: 500 }) | ||
return NextResponse.json({ message: error.tag }, { status: error.status }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { NextRequest } from "next/server" | ||
|
||
import { HttpError } from "./httpError" | ||
import { NextRequestWithBody, hasParsedBody } from "./validateSignature" | ||
|
||
export const validateBody = | ||
<T>(schema: Zod.Schema<T>) => | ||
async (req: NextRequest | NextRequestWithBody) => { | ||
try { | ||
const hasBody = hasParsedBody(req) | ||
|
||
const parseResult = schema.safeParse(hasBody ? req.body : await req.json()) | ||
if (!parseResult.success) throw new HttpError("BadRequest", 400) | ||
|
||
const reqWithBody: NextRequestWithValidBody<T> = Object.assign(req, { validBody: parseResult.data }) | ||
|
||
return reqWithBody | ||
} catch { | ||
throw new HttpError("BadRequest", 400) | ||
} | ||
} | ||
|
||
export type NextRequestWithValidBody<T> = NextRequest & { validBody: T } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { NextRequest } from "next/server" | ||
import { HttpError } from "./httpError" | ||
import { verifyWebhookSignature } from "@hygraph/utils" | ||
import { env } from "env.mjs" | ||
|
||
export const validateSignature = async (req: NextRequest) => { | ||
const authHeader = req.headers.get("gcms-signature") | ||
if (!authHeader) throw new HttpError("Unauthorized", 401) | ||
const parsedBody = await req.json() | ||
|
||
const isSignatureValid = verifyWebhookSignature({ | ||
body: parsedBody, | ||
signature: authHeader, | ||
secret: env.HYGRAPH_WEBOOK_SECRET, | ||
}) | ||
if (!isSignatureValid) throw new HttpError("Unauthorized", 401) | ||
|
||
const reqWithBody = Object.assign(req, { parsedBody }) | ||
return reqWithBody | ||
} | ||
|
||
export type NextRequestWithBody = NextRequest & { parsedBody: unknown } | ||
|
||
export const hasParsedBody = (req: NextRequestWithBody | NextRequest): req is NextRequestWithBody => | ||
Boolean((req as NextRequestWithBody).body) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
type Func<A, B> = (a: A) => B | Promise<B> | ||
|
||
export async function pipe<A>(a: A): Promise<A> | ||
export async function pipe<A, B>(a: A, ab: Func<A, B>): Promise<B> | ||
export async function pipe<A, B, C>(a: A, ab: Func<A, B>, bc: Func<B, C>): Promise<C> | ||
export async function pipe<A, B, C, D>(a: A, ab: Func<A, B>, bc: Func<B, C>, cd: Func<C, D>): Promise<D> | ||
|
||
export async function pipe(...args: any[]): Promise<any> { | ||
return await args.slice(1).reduce(async (accPromise, fn) => { | ||
const acc = await accPromise | ||
return fn(acc) | ||
}, Promise.resolve(args[0])) | ||
} |