diff --git a/package.json b/package.json index fc0b2588..ee205018 100644 --- a/package.json +++ b/package.json @@ -164,9 +164,9 @@ "require": "./handlers/fetch-api/index.js" }, "./handlers/next-app-router": { - "types": "./handlers/fetch-api/index.d.ts", - "import": "./handlers/fetch-api/index.mjs", - "require": "./handlers/fetch-api/index.js" + "types": "./handlers/next-app-router/index.d.ts", + "import": "./handlers/next-app-router/index.mjs", + "require": "./handlers/next-app-router/index.js" }, "./handlers/aws-lambda": { "types": "./handlers/aws-lambda/index.d.ts", diff --git a/src/APL/saleor-cloud/saleor-cloud-apl.test.ts b/src/APL/saleor-cloud/saleor-cloud-apl.test.ts index fd07b93e..799b8eef 100644 --- a/src/APL/saleor-cloud/saleor-cloud-apl.test.ts +++ b/src/APL/saleor-cloud/saleor-cloud-apl.test.ts @@ -47,13 +47,17 @@ describe("APL", () => { saleor_api_url: "https://example.com/graphql/", jwks: "{}", token: "example-token", + /** + * Domain is appended to the request body, because APL requires it + */ + domain: "example.com", }), headers: { "Content-Type": "application/json", Authorization: "Bearer token", }, method: "POST", - } + }, ); }); @@ -66,7 +70,7 @@ describe("APL", () => { const apl = new SaleorCloudAPL(aplConfig); await expect(apl.set(stubAuthData)).rejects.toThrow( - "Fetch returned with non 200 status code 500" + "Fetch returned with non 200 status code 500", ); }); }); @@ -112,7 +116,7 @@ describe("APL", () => { expect(err.code).toEqual("RESPONSE_BODY_INVALID"); expect(err).toMatchInlineSnapshot( - "[SaleorCloudAplError: Cant parse response body: json error]" + "[SaleorCloudAplError: Cant parse response body: json error]", ); } }); @@ -143,7 +147,7 @@ describe("APL", () => { Authorization: "Bearer token", }, method: "GET", - } + }, ); }); @@ -190,7 +194,7 @@ describe("APL", () => { Authorization: "Bearer token", }, method: "GET", - } + }, ); }); }); diff --git a/src/APL/saleor-cloud/saleor-cloud-apl.ts b/src/APL/saleor-cloud/saleor-cloud-apl.ts index be1218e2..c5909f81 100644 --- a/src/APL/saleor-cloud/saleor-cloud-apl.ts +++ b/src/APL/saleor-cloud/saleor-cloud-apl.ts @@ -52,6 +52,7 @@ const mapAuthDataToAPIBody = (authData: AuthData) => ({ saleor_api_url: authData.saleorApiUrl, jwks: authData.jwks, token: authData.token, + domain: new URL(authData.saleorApiUrl).hostname, }); const mapAPIResponseToAuthData = (response: CloudAPLAuthDataShape): AuthData => ({ diff --git a/src/handlers/platforms/next-app-router/create-protected-handler.ts b/src/handlers/platforms/next-app-router/create-protected-handler.ts index 6d36dd04..7329456b 100644 --- a/src/handlers/platforms/next-app-router/create-protected-handler.ts +++ b/src/handlers/platforms/next-app-router/create-protected-handler.ts @@ -12,7 +12,7 @@ import { NextAppRouterAdapter, NextAppRouterHandler } from "./platform-adapter"; export type NextAppRouterProtectedHandler = ( request: NextRequest, ctx: ProtectedHandlerContext, -) => NextResponse | Promise; +) => Response | Promise; export const createProtectedHandler = ( diff --git a/src/handlers/platforms/next-app-router/platform-adapter.ts b/src/handlers/platforms/next-app-router/platform-adapter.ts index 7f94d61f..f2b398e7 100644 --- a/src/handlers/platforms/next-app-router/platform-adapter.ts +++ b/src/handlers/platforms/next-app-router/platform-adapter.ts @@ -1,12 +1,12 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextRequest } from "next/server"; import { WebApiAdapter } from "@/handlers/platforms/fetch-api"; export type NextAppRouterHandlerInput = NextRequest; -export type NextAppRouterHandler = (req: NextRequest) => NextResponse | Promise; +export type NextAppRouterHandler = (req: NextRequest) => Response | Promise; -export class NextAppRouterAdapter extends WebApiAdapter { +export class NextAppRouterAdapter extends WebApiAdapter { constructor(public request: NextRequest) { - super(request, NextResponse); + super(request, Response); } } diff --git a/src/handlers/platforms/next-app-router/saleor-webhooks/saleor-sync-webhook.test.ts b/src/handlers/platforms/next-app-router/saleor-webhooks/saleor-sync-webhook.test.ts new file mode 100644 index 00000000..67adc335 --- /dev/null +++ b/src/handlers/platforms/next-app-router/saleor-webhooks/saleor-sync-webhook.test.ts @@ -0,0 +1,34 @@ +import { NextResponse } from "next/server"; +import { describe, expect, it } from "vitest"; + +import { FileAPL } from "@/APL/file"; +import { SaleorSyncWebhook } from "@/handlers/platforms/next-app-router"; + +describe("SaleorSyncWebhook (Next App Router)", () => { + it("Constructs (and types are right)", () => { + /** + * This test doesn't test anything in the runtime. + * It's meant to ensure types are correct. If types are wrong, the project will not compile. + */ + expect.assertions(0); + + const handler = new SaleorSyncWebhook<{ foo: string }>({ + apl: new FileAPL(), + event: "CHECKOUT_CALCULATE_TAXES", + name: "asd", + query: "{}", + webhookPath: "/", + }); + + handler.createHandler((req, ctx) => { + const { body } = req; + + const { event } = ctx; + + return NextResponse.json({ + event, + body, + }); + }); + }); +}); diff --git a/src/handlers/platforms/next-app-router/saleor-webhooks/saleor-webhook.ts b/src/handlers/platforms/next-app-router/saleor-webhooks/saleor-webhook.ts index 568288ef..225714c7 100644 --- a/src/handlers/platforms/next-app-router/saleor-webhooks/saleor-webhook.ts +++ b/src/handlers/platforms/next-app-router/saleor-webhooks/saleor-webhook.ts @@ -1,4 +1,4 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextRequest } from "next/server"; import { createDebug } from "@/debug"; import { WebApiWebhookHandler } from "@/handlers/platforms/fetch-api"; @@ -22,7 +22,7 @@ export type WebhookConfig export type NextAppRouterWebhookHandler< TPayload = unknown, TRequest extends NextRequest = NextRequest, - TResponse extends NextResponse = NextResponse, + TResponse extends Response = Response, > = WebApiWebhookHandler; export abstract class SaleorNextAppRouterWebhook extends GenericSaleorWebhook< @@ -30,7 +30,7 @@ export abstract class SaleorNextAppRouterWebhook extends Gen TPayload > { createHandler(handlerFn: NextAppRouterWebhookHandler): NextAppRouterHandler { - return async (req): Promise => { + return async (req): Promise => { const adapter = new NextAppRouterAdapter(req); const prepareRequestResult = await super.prepareRequest(adapter);