Skip to content

Commit 12b2fd1

Browse files
lkostrowskikzuraw
andauthored
restore domain in SaleorCloudAPL to keep the API contract (#422)
* restore domain in SaleorCloudAPL to keep the API contract * fix test * fix export path * add changeset for release * Loose handler response to be Response, not NextResponse * fix types * remove changeset --------- Co-authored-by: Krzysztof Żuraw <9116238+krzysztofzuraw@users.noreply.github.com>
1 parent db9fa7c commit 12b2fd1

File tree

7 files changed

+55
-16
lines changed

7 files changed

+55
-16
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@
164164
"require": "./handlers/fetch-api/index.js"
165165
},
166166
"./handlers/next-app-router": {
167-
"types": "./handlers/fetch-api/index.d.ts",
168-
"import": "./handlers/fetch-api/index.mjs",
169-
"require": "./handlers/fetch-api/index.js"
167+
"types": "./handlers/next-app-router/index.d.ts",
168+
"import": "./handlers/next-app-router/index.mjs",
169+
"require": "./handlers/next-app-router/index.js"
170170
},
171171
"./handlers/aws-lambda": {
172172
"types": "./handlers/aws-lambda/index.d.ts",

src/APL/saleor-cloud/saleor-cloud-apl.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ describe("APL", () => {
4747
saleor_api_url: "https://example.com/graphql/",
4848
jwks: "{}",
4949
token: "example-token",
50+
/**
51+
* Domain is appended to the request body, because APL requires it
52+
*/
53+
domain: "example.com",
5054
}),
5155
headers: {
5256
"Content-Type": "application/json",
5357
Authorization: "Bearer token",
5458
},
5559
method: "POST",
56-
}
60+
},
5761
);
5862
});
5963

@@ -66,7 +70,7 @@ describe("APL", () => {
6670
const apl = new SaleorCloudAPL(aplConfig);
6771

6872
await expect(apl.set(stubAuthData)).rejects.toThrow(
69-
"Fetch returned with non 200 status code 500"
73+
"Fetch returned with non 200 status code 500",
7074
);
7175
});
7276
});
@@ -112,7 +116,7 @@ describe("APL", () => {
112116

113117
expect(err.code).toEqual("RESPONSE_BODY_INVALID");
114118
expect(err).toMatchInlineSnapshot(
115-
"[SaleorCloudAplError: Cant parse response body: json error]"
119+
"[SaleorCloudAplError: Cant parse response body: json error]",
116120
);
117121
}
118122
});
@@ -143,7 +147,7 @@ describe("APL", () => {
143147
Authorization: "Bearer token",
144148
},
145149
method: "GET",
146-
}
150+
},
147151
);
148152
});
149153

@@ -190,7 +194,7 @@ describe("APL", () => {
190194
Authorization: "Bearer token",
191195
},
192196
method: "GET",
193-
}
197+
},
194198
);
195199
});
196200
});

src/APL/saleor-cloud/saleor-cloud-apl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const mapAuthDataToAPIBody = (authData: AuthData) => ({
5252
saleor_api_url: authData.saleorApiUrl,
5353
jwks: authData.jwks,
5454
token: authData.token,
55+
domain: new URL(authData.saleorApiUrl).hostname,
5556
});
5657

5758
const mapAPIResponseToAuthData = (response: CloudAPLAuthDataShape): AuthData => ({

src/handlers/platforms/next-app-router/create-protected-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { NextAppRouterAdapter, NextAppRouterHandler } from "./platform-adapter";
1212
export type NextAppRouterProtectedHandler = (
1313
request: NextRequest,
1414
ctx: ProtectedHandlerContext,
15-
) => NextResponse | Promise<NextResponse>;
15+
) => Response | Promise<Response>;
1616

1717
export const createProtectedHandler =
1818
(
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { NextRequest, NextResponse } from "next/server";
1+
import { NextRequest } from "next/server";
22

33
import { WebApiAdapter } from "@/handlers/platforms/fetch-api";
44

55
export type NextAppRouterHandlerInput = NextRequest;
6-
export type NextAppRouterHandler = (req: NextRequest) => NextResponse | Promise<NextResponse>;
6+
export type NextAppRouterHandler = (req: NextRequest) => Response | Promise<Response>;
77

8-
export class NextAppRouterAdapter extends WebApiAdapter<NextRequest, NextResponse> {
8+
export class NextAppRouterAdapter extends WebApiAdapter<NextRequest> {
99
constructor(public request: NextRequest) {
10-
super(request, NextResponse);
10+
super(request, Response);
1111
}
1212
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { NextResponse } from "next/server";
2+
import { describe, expect, it } from "vitest";
3+
4+
import { FileAPL } from "@/APL/file";
5+
import { SaleorSyncWebhook } from "@/handlers/platforms/next-app-router";
6+
7+
describe("SaleorSyncWebhook (Next App Router)", () => {
8+
it("Constructs (and types are right)", () => {
9+
/**
10+
* This test doesn't test anything in the runtime.
11+
* It's meant to ensure types are correct. If types are wrong, the project will not compile.
12+
*/
13+
expect.assertions(0);
14+
15+
const handler = new SaleorSyncWebhook<{ foo: string }>({
16+
apl: new FileAPL(),
17+
event: "CHECKOUT_CALCULATE_TAXES",
18+
name: "asd",
19+
query: "{}",
20+
webhookPath: "/",
21+
});
22+
23+
handler.createHandler((req, ctx) => {
24+
const { body } = req;
25+
26+
const { event } = ctx;
27+
28+
return NextResponse.json({
29+
event,
30+
body,
31+
});
32+
});
33+
});
34+
});

src/handlers/platforms/next-app-router/saleor-webhooks/saleor-webhook.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NextRequest, NextResponse } from "next/server";
1+
import { NextRequest } from "next/server";
22

33
import { createDebug } from "@/debug";
44
import { WebApiWebhookHandler } from "@/handlers/platforms/fetch-api";
@@ -22,15 +22,15 @@ export type WebhookConfig<Event = AsyncWebhookEventType | SyncWebhookEventType>
2222
export type NextAppRouterWebhookHandler<
2323
TPayload = unknown,
2424
TRequest extends NextRequest = NextRequest,
25-
TResponse extends NextResponse = NextResponse,
25+
TResponse extends Response = Response,
2626
> = WebApiWebhookHandler<TPayload, TRequest, TResponse>;
2727

2828
export abstract class SaleorNextAppRouterWebhook<TPayload = unknown> extends GenericSaleorWebhook<
2929
NextAppRouterHandlerInput,
3030
TPayload
3131
> {
3232
createHandler(handlerFn: NextAppRouterWebhookHandler<TPayload>): NextAppRouterHandler {
33-
return async (req): Promise<NextResponse> => {
33+
return async (req): Promise<Response> => {
3434
const adapter = new NextAppRouterAdapter(req);
3535
const prepareRequestResult = await super.prepareRequest<NextAppRouterAdapter>(adapter);
3636

0 commit comments

Comments
 (0)