-
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.
Add central SaleorApp instance (#71)
* Add SaleorApp class * Add middleware and tests * Move APL validation to APL * Fix test * Add prepush hook * Add better error for missing vercel envs * Add test
- Loading branch information
1 parent
61f5ab6
commit a839314
Showing
15 changed files
with
258 additions
and
33 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,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
pnpm run test:ci |
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
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
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./const"; | ||
export * from "./headers"; | ||
export * from "./infer-webhooks"; | ||
export * from "./saleor-app"; | ||
export * from "./types"; | ||
export * from "./urls"; |
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
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 |
---|---|---|
@@ -1,37 +1,47 @@ | ||
import { Middleware } from "retes"; | ||
import { Response } from "retes/response"; | ||
|
||
import { APL } from "../APL"; | ||
import { getSaleorHeaders } from "../headers"; | ||
import { createMiddlewareDebug } from "./middleware-debug"; | ||
import { getSaleorAppFromRequest } from "./with-saleor-app"; | ||
|
||
const debug = createMiddlewareDebug("withRegisteredSaleorDomainHeader"); | ||
|
||
export const withRegisteredSaleorDomainHeader = | ||
({ apl }: { apl: APL }): Middleware => | ||
(handler) => | ||
async (request) => { | ||
const { domain: saleorDomain } = getSaleorHeaders(request.headers); | ||
export const withRegisteredSaleorDomainHeader: Middleware = (handler) => async (request) => { | ||
const { domain: saleorDomain } = getSaleorHeaders(request.headers); | ||
|
||
if (!saleorDomain) { | ||
return Response.BadRequest({ | ||
success: false, | ||
message: "Domain header missing.", | ||
}); | ||
} | ||
if (!saleorDomain) { | ||
return Response.BadRequest({ | ||
success: false, | ||
message: "Domain header missing.", | ||
}); | ||
} | ||
|
||
debug("Middleware called with domain: \"%s\"", saleorDomain); | ||
debug("Middleware called with domain: \"%s\"", saleorDomain); | ||
|
||
const authData = await apl.get(saleorDomain); | ||
const saleorApp = getSaleorAppFromRequest(request); | ||
|
||
if (!authData) { | ||
debug("Auth was not found in APL, will respond with Forbidden status"); | ||
if (!saleorApp) { | ||
console.error( | ||
"SaleorApp not found in request context. Ensure your API handler is wrapped with withSaleorApp middleware" | ||
); | ||
|
||
return Response.Forbidden({ | ||
success: false, | ||
message: `Domain ${saleorDomain} not registered.`, | ||
}); | ||
} | ||
return Response.InternalServerError({ | ||
success: false, | ||
message: "SaleorApp is misconfigured", | ||
}); | ||
} | ||
|
||
return handler(request); | ||
}; | ||
const authData = await saleorApp?.apl.get(saleorDomain); | ||
|
||
if (!authData) { | ||
debug("Auth was not found in APL, will respond with Forbidden status"); | ||
|
||
return Response.Forbidden({ | ||
success: false, | ||
message: `Domain ${saleorDomain} not registered.`, | ||
}); | ||
} | ||
|
||
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,27 @@ | ||
import { Request } from "retes"; | ||
import { Response } from "retes/response"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { FileAPL } from "../APL"; | ||
import { SALEOR_DOMAIN_HEADER } from "../const"; | ||
import { SaleorApp } from "../saleor-app"; | ||
import { withSaleorApp } from "./with-saleor-app"; | ||
|
||
describe("middleware", () => { | ||
describe("withSaleorApp", () => { | ||
it("Adds SaleorApp instance to request context", async () => { | ||
const mockRequest = { | ||
context: {}, | ||
headers: { | ||
[SALEOR_DOMAIN_HEADER]: "example.com", | ||
}, | ||
} as unknown as Request; | ||
|
||
await withSaleorApp(new SaleorApp({ apl: new FileAPL() }))((request) => { | ||
expect(request.context.saleorApp).toBeDefined(); | ||
|
||
return Response.OK(""); | ||
})(mockRequest); | ||
}); | ||
}); | ||
}); |
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,21 @@ | ||
import { Middleware, Request } from "retes"; | ||
|
||
import { SaleorApp } from "../saleor-app"; | ||
import { createMiddlewareDebug } from "./middleware-debug"; | ||
|
||
const debug = createMiddlewareDebug("withSaleorApp"); | ||
|
||
export const withSaleorApp = | ||
(saleorApp: SaleorApp): Middleware => | ||
(handler) => | ||
async (request) => { | ||
debug("Middleware called"); | ||
|
||
request.context ??= {}; | ||
request.context.saleorApp = saleorApp; | ||
|
||
return handler(request); | ||
}; | ||
|
||
export const getSaleorAppFromRequest = (request: Request): SaleorApp | undefined => | ||
request.context?.saleorApp; |
Oops, something went wrong.