-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middlewares for FetchAPI, add Edge register handler
- Loading branch information
1 parent
b2947b2
commit eca6911
Showing
13 changed files
with
496 additions
and
0 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
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,6 @@ | ||
export * from "./to-next-edge-handler"; | ||
export * from "./with-auth-token-required"; | ||
export * from "./with-method"; | ||
export * from "./with-registered-saleor-domain-header"; | ||
export * from "./with-saleor-app"; | ||
export * from "./with-saleor-domain-present"; |
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,4 @@ | ||
import { createDebug } from "../debug"; | ||
|
||
export const createFetchMiddlewareDebug = (middleware: string) => | ||
createDebug(`FetchMiddleware:${middleware}`); |
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,20 @@ | ||
import { FetchHandler, FetchPipeline, ReveredFetchPipeline } from "./types"; | ||
|
||
const isPipeline = (maybePipeline: unknown): maybePipeline is FetchPipeline => | ||
Array.isArray(maybePipeline); | ||
|
||
const compose = | ||
<T extends Function>(...functions: T[]) => | ||
(args: any) => | ||
functions.reduce((arg, fn) => fn(arg), args); | ||
|
||
const preparePipeline = (pipeline: FetchPipeline): FetchHandler => { | ||
const [action, ...middleware] = pipeline.reverse() as ReveredFetchPipeline; | ||
return compose(...middleware)(action); | ||
}; | ||
|
||
export const toNextEdgeHandler = (flow: FetchHandler | FetchPipeline): FetchHandler => { | ||
const handler = isPipeline(flow) ? preparePipeline(flow) : flow; | ||
|
||
return async (request: Request) => handler(request); | ||
}; |
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,5 @@ | ||
export type SaleorRequest = Request & { context?: Record<string, any> }; | ||
export type FetchHandler = (req: SaleorRequest) => Response | Promise<Response>; | ||
export type FetchMiddleware = (handler: FetchHandler) => FetchHandler; | ||
export type FetchPipeline = [...FetchMiddleware[], FetchHandler]; | ||
export type ReveredFetchPipeline = [FetchHandler, ...FetchMiddleware[]]; |
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,32 @@ | ||
import { createFetchMiddlewareDebug } from "./middleware-debug"; | ||
import { FetchMiddleware } from "./types"; | ||
|
||
const debug = createFetchMiddlewareDebug("withAuthTokenRequired"); | ||
|
||
export const withAuthTokenRequired: FetchMiddleware = (handler) => async (request) => { | ||
debug("Middleware called"); | ||
|
||
try { | ||
// If we read `request.json()` without cloning it will throw an error | ||
// next time we run request.json() | ||
const clone = request.clone(); | ||
const json = await clone.json(); | ||
const authToken = json.auth_token; | ||
|
||
if (!authToken) { | ||
debug("Found missing authToken param"); | ||
|
||
return Response.json( | ||
{ | ||
success: false, | ||
message: "Missing auth token.", | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
} catch { | ||
return Response.json({ success: false, message: "Invalid request body" }, { status: 400 }); | ||
} | ||
|
||
return handler(request); | ||
}; |
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 { FetchMiddleware } from "./types"; | ||
|
||
export const HTTPMethod = { | ||
GET: "GET", | ||
POST: "POST", | ||
PUT: "PUT", | ||
PATH: "PATCH", | ||
HEAD: "HEAD", | ||
OPTIONS: "OPTIONS", | ||
DELETE: "DELETE", | ||
} as const; | ||
export type HTTPMethod = typeof HTTPMethod[keyof typeof HTTPMethod]; | ||
|
||
export const withMethod = | ||
(...methods: HTTPMethod[]): FetchMiddleware => | ||
(handler) => | ||
async (request) => { | ||
if (!methods.includes(request.method as HTTPMethod)) { | ||
return new Response("Method not allowed", { status: 405 }); | ||
} | ||
|
||
const response = await handler(request); | ||
|
||
return response; | ||
}; |
51 changes: 51 additions & 0 deletions
51
src/fetch-middleware/with-registered-saleor-domain-header.ts
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,51 @@ | ||
import { getSaleorHeadersFetchAPI } from "../headers"; | ||
import { createFetchMiddlewareDebug } from "./middleware-debug"; | ||
import { FetchMiddleware } from "./types"; | ||
import { getSaleorAppFromRequest } from "./with-saleor-app"; | ||
|
||
const debug = createFetchMiddlewareDebug("withRegisteredSaleorDomainHeader"); | ||
|
||
export const withRegisteredSaleorDomainHeader: FetchMiddleware = (handler) => async (request) => { | ||
const { saleorApiUrl } = getSaleorHeadersFetchAPI(request.headers); | ||
|
||
if (!saleorApiUrl) { | ||
return Response.json( | ||
{ success: false, message: "saleorApiUrl header missing" }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
debug("Middleware called with saleorApiUrl: \"%s\"", saleorApiUrl); | ||
|
||
const saleorApp = getSaleorAppFromRequest(request); | ||
|
||
if (!saleorApp) { | ||
console.error( | ||
"SaleorApp not found in request context. Ensure your API handler is wrapped with withSaleorApp middleware" | ||
); | ||
|
||
return Response.json( | ||
{ | ||
success: false, | ||
message: "SaleorApp is misconfigured", | ||
}, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
const authData = await saleorApp?.apl.get(saleorApiUrl); | ||
|
||
if (!authData) { | ||
debug("Auth was not found in APL, will respond with Forbidden status"); | ||
|
||
return Response.json( | ||
{ | ||
success: false, | ||
message: `Saleor: ${saleorApiUrl} not registered.`, | ||
}, | ||
{ status: 403 } | ||
); | ||
} | ||
|
||
return handler(request); | ||
}; |
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,20 @@ | ||
import { SaleorApp } from "../saleor-app"; | ||
import { createFetchMiddlewareDebug } from "./middleware-debug"; | ||
import { FetchMiddleware, SaleorRequest } from "./types"; | ||
|
||
const debug = createFetchMiddlewareDebug("withSaleorApp"); | ||
|
||
export const withSaleorApp = | ||
(saleorApp: SaleorApp): FetchMiddleware => | ||
(handler) => | ||
async (request: SaleorRequest) => { | ||
debug("Middleware called"); | ||
|
||
request.context ??= {}; | ||
request.context.saleorApp = saleorApp; | ||
|
||
return handler(request); | ||
}; | ||
|
||
export const getSaleorAppFromRequest = (request: SaleorRequest): SaleorApp | undefined => | ||
request.context?.saleorApp; |
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 { getSaleorHeadersFetchAPI } from "../headers"; | ||
import { createFetchMiddlewareDebug } from "./middleware-debug"; | ||
import { FetchMiddleware } from "./types"; | ||
|
||
const debug = createFetchMiddlewareDebug("withSaleorDomainPresent"); | ||
|
||
export const withSaleorDomainPresent: FetchMiddleware = (handler) => async (request) => { | ||
const { domain } = getSaleorHeadersFetchAPI(request.headers); | ||
|
||
debug("Middleware called with domain in header: %s", domain); | ||
|
||
if (!domain) { | ||
debug("Domain not found in header, will respond with Bad Request"); | ||
|
||
return Response.json( | ||
{ | ||
success: false, | ||
message: "Missing Saleor domain header.", | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
return handler(request); | ||
}; |
Oops, something went wrong.