Skip to content

Commit d339a69

Browse files
authored
Refactor handlers, remove deprecated exports, v1 (#399)
* Simplify tsconfig, add aliases * Refactor handlers/next -> handlers/platform/next * Fix imports * Move to shared * Fix vitest config * Fix failing tests due to change in aliases * Add missing shared files * Move sync-webhook response builder * Refactor commonly used types to shared folder * Add deprecated exports notes, add index file * Export shared folder * Fix broken build * Fix broken build * Remove re-exports for compatibility * Make `schemaVersion` required, remove alpha note, combine files * Fix imports
1 parent 003b1ca commit d339a69

32 files changed

+401
-368
lines changed

README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@
88

99
SDK for building [Saleor Apps](https://github.com/saleor/apps).
1010

11+
Supports Saleor version 3.20+
12+
1113
<div>
1214

1315
[![npm version badge](https://img.shields.io/npm/v/@saleor/app-sdk)](https://www.npmjs.com/package/@saleor/app-sdk)
1416
[![npm downloads count](https://img.shields.io/npm/dt/@saleor/app-sdk)](https://www.npmjs.com/package/@saleor/app-sdk)
1517

1618
</div>
1719

18-
## 🚨 Alpha phase
19-
20-
App SDK is in the early stage at the moment. Every API below 1.x.x release is likely to change.
21-
22-
Feel free to play with SDK and move its code directly to your app.
23-
2420
## Release flow
2521

2622
- The `main` branch is a current, latest branch.

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"typescript": "4.9.5",
8585
"vi-fetch": "^0.8.0",
8686
"vite": "6.0.11 ",
87+
"vite-tsconfig-paths": "^5.1.4",
8788
"vitest": "3.0.4"
8889
},
8990
"peerDependenciesMeta": {
@@ -150,6 +151,11 @@
150151
"import": "./handlers/next/index.mjs",
151152
"require": "./handlers/next/index.js"
152153
},
154+
"./handlers/shared": {
155+
"types": "./handlers/shared/index.d.ts",
156+
"import": "./handlers/shared/index.mjs",
157+
"require": "./handlers/shared/index.js"
158+
},
153159
"./saleor-app": {
154160
"types": "./saleor-app.d.ts",
155161
"import": "./saleor-app.mjs",

pnpm-lock.yaml

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/handlers/next/create-manifest-handler.ts

-48
This file was deleted.

src/handlers/next/create-protected-handler.ts

-64
This file was deleted.

src/handlers/next/create-app-register-handler.test.ts src/handlers/platforms/next/create-app-register-handler.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { createMocks } from "node-mocks-http";
22
import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
33

4-
import { APL, AuthData } from "../../APL";
5-
import { MockAPL } from "../../test-utils/mock-apl";
4+
import { APL, AuthData } from "@/APL";
5+
import * as fetchRemoteJwksModule from "@/fetch-remote-jwks";
6+
import * as getAppIdModule from "@/get-app-id";
7+
import { MockAPL } from "@/test-utils/mock-apl";
8+
69
import { createAppRegisterHandler } from "./create-app-register-handler";
710

811
const mockJwksValue = "{}";
912
const mockAppId = "42";
1013

11-
vi.mock("../../get-app-id", () => ({
12-
getAppId: vi.fn().mockResolvedValue("42"), // can't use var reference, due to hoisting
13-
}));
14-
15-
vi.mock("../../fetch-remote-jwks", () => ({
16-
fetchRemoteJwks: vi.fn().mockResolvedValue("{}"), // can't use var reference, due to hoisting
17-
}));
14+
// Cannot use vi.mock on module, due to issues with alias resolution
15+
// in vitest vs TypeScript: https://github.com/vitest-dev/vitest/issues/3105
16+
vi.spyOn(fetchRemoteJwksModule, "fetchRemoteJwks").mockResolvedValue("{}");
17+
vi.spyOn(getAppIdModule, "getAppId").mockResolvedValue("42");
1818

1919
describe("create-app-register-handler", () => {
2020
let mockApl: APL;

src/handlers/next/create-app-register-handler.ts src/handlers/platforms/next/create-app-register-handler.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@ import { toNextHandler } from "retes/adapter";
33
import { withMethod } from "retes/middleware";
44
import { Response } from "retes/response";
55

6-
import { AuthData } from "../../APL";
7-
import { SALEOR_API_URL_HEADER } from "../../const";
8-
import { createDebug } from "../../debug";
9-
import { fetchRemoteJwks } from "../../fetch-remote-jwks";
10-
import { getAppId } from "../../get-app-id";
11-
import { withAuthTokenRequired } from "../../middleware";
12-
import { HasAPL } from "../../saleor-app";
13-
import { validateAllowSaleorUrls } from "./validate-allow-saleor-urls";
6+
import { AuthData } from "@/APL";
7+
import { SALEOR_API_URL_HEADER } from "@/const";
8+
import { createDebug } from "@/debug";
9+
import { fetchRemoteJwks } from "@/fetch-remote-jwks";
10+
import { getAppId } from "@/get-app-id";
11+
import { HookCallbackErrorParams } from "@/handlers/shared/create-app-register-handler-types";
12+
import { validateAllowSaleorUrls } from "@/handlers/shared/validate-allow-saleor-urls";
13+
import { withAuthTokenRequired } from "@/middleware";
14+
import { HasAPL } from "@/saleor-app";
1415

1516
const debug = createDebug("createAppRegisterHandler");
1617

17-
type HookCallbackErrorParams = {
18-
status?: number;
19-
message?: string;
20-
};
21-
2218
class RegisterCallbackError extends Error {
2319
public status = 500;
2420

src/handlers/next/create-manifest-handler.test.ts src/handlers/platforms/next/create-manifest-handler.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createMocks } from "node-mocks-http";
22
import { describe, expect, it } from "vitest";
33

4-
import { AppManifest } from "../../types";
4+
import { AppManifest } from "@/types";
5+
56
import { createManifestHandler } from "./create-manifest-handler";
67

78
describe("createManifestHandler", () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { NextApiHandler, NextApiRequest } from "next";
2+
3+
import { createDebug } from "@/debug";
4+
import { getBaseUrl, getSaleorHeaders } from "@/headers";
5+
import { AppManifest } from "@/types";
6+
7+
export type CreateManifestHandlerOptions = {
8+
manifestFactory(context: {
9+
appBaseUrl: string;
10+
request: NextApiRequest;
11+
/** For Saleor < 3.15 it will be null. */
12+
schemaVersion: number | null;
13+
}): AppManifest | Promise<AppManifest>;
14+
};
15+
16+
const debug = createDebug("create-manifest-handler");
17+
18+
/**
19+
* Creates API handler for Next.js. Helps with Manifest creation, hides
20+
* implementation details if possible
21+
* In the future this will be extracted to separate sdk/next package
22+
*/
23+
export const createManifestHandler =
24+
(options: CreateManifestHandlerOptions): NextApiHandler =>
25+
async (request, response) => {
26+
const { schemaVersion } = getSaleorHeaders(request.headers);
27+
const baseURL = getBaseUrl(request.headers);
28+
29+
debug("Received request with schema version \"%s\" and base URL \"%s\"", schemaVersion, baseURL);
30+
31+
try {
32+
const manifest = await options.manifestFactory({
33+
appBaseUrl: baseURL,
34+
request,
35+
schemaVersion,
36+
});
37+
38+
debug("Executed manifest file");
39+
40+
return response.status(200).json(manifest);
41+
} catch (e) {
42+
debug("Error while resolving manifest: %O", e);
43+
44+
return response.status(500).json({
45+
message: "Error resolving manifest file",
46+
});
47+
}
48+
};

0 commit comments

Comments
 (0)