diff --git a/.changeset/tough-socks-tease.md b/.changeset/tough-socks-tease.md new file mode 100644 index 00000000..262c887f --- /dev/null +++ b/.changeset/tough-socks-tease.md @@ -0,0 +1,5 @@ +--- +"@saleor/app-sdk": major +--- + +Breaking change: Remove checking "domain" header from Saleor requests. It should be replaced with the "saleor-api-url" header. diff --git a/.changeset/witty-carrots-occur.md b/.changeset/witty-carrots-occur.md new file mode 100644 index 00000000..44b9d903 --- /dev/null +++ b/.changeset/witty-carrots-occur.md @@ -0,0 +1,5 @@ +--- +"@saleor/app-sdk": patch +--- + +Marked `/middleware`s as deprecated in favor of `/handlers` diff --git a/.github/workflows/prepare-release.yml b/.github/workflows/prepare-release.yml index bd6b984e..13e72651 100644 --- a/.github/workflows/prepare-release.yml +++ b/.github/workflows/prepare-release.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - "v[0-9]+.x" concurrency: ${{ github.workflow }}-${{ github.ref }} diff --git a/README.md b/README.md index fdee3e75..a1461ad7 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,21 @@ App SDK is in the early stage at the moment. Every API below 1.x.x release is li Feel free to play with SDK and move its code directly to your app. +## Release flow + +- The `main` branch is a current, latest branch. +- Branches matching `v[0-9]+.x` (like `v1.x`, v0.x`) are release branches +- PRs should be opened to `main` branch and contain changesets (run `npx changeset`). Once changeset is merged to main, the release PR is opened. After the release PR is merged, the version is being pushed to NPM and changesets are pruned +- To patch older version, commit from `main` (including changeset) should be also ported to release branch (e.g. v0.x). Release branch will also detect changes and open release PR +- To release new major version (e.g. start working on `v2.x` from `v1.x`): + - Create a legacy release branch (e.g. `v1.x` branch) + - Mark changeset to `main` with `major` change, which will start counting next `main` releases as `2.x.x` + - Do not merge release PR until it's ready to be merged + +### Deploying test snapshots + +PRs can be pushed to NPM by adding label to PR `release dev tag`. Workflow will run and print version that has been released. + ## Installing ```bash diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..ea2cdd76 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,8 @@ +coverage: + status: + patch: + default: + informational: true + project: + default: + informational: true diff --git a/src/APL/apl.ts b/src/APL/apl.ts index fc9edbbf..ec652b3b 100644 --- a/src/APL/apl.ts +++ b/src/APL/apl.ts @@ -1,5 +1,4 @@ export interface AuthData { - domain?: string; token: string; saleorApiUrl: string; appId: string; diff --git a/src/APL/auth-data-from-object.ts b/src/APL/auth-data-from-object.ts index 4c3e7964..cd14f1cf 100644 --- a/src/APL/auth-data-from-object.ts +++ b/src/APL/auth-data-from-object.ts @@ -12,11 +12,10 @@ export const authDataFromObject = (parsed: unknown): AuthData | undefined => { debug("Given object did not contained AuthData"); return undefined; } - const { saleorApiUrl, appId, domain, token, jwks } = parsed as AuthData; + const { saleorApiUrl, appId, token, jwks } = parsed as AuthData; return { saleorApiUrl, appId, - domain, token, jwks, }; diff --git a/src/APL/env-apl.test.ts b/src/APL/env-apl.test.ts index 5cf6048e..3321daa9 100644 --- a/src/APL/env-apl.test.ts +++ b/src/APL/env-apl.test.ts @@ -14,7 +14,6 @@ const getMockAuthData = (): AuthData => ({ appId: "app-id", token: "some-token", jwks: "{}", - domain: "my-saleor-instance.cloud", }); describe("EnvAPL", () => { @@ -58,8 +57,7 @@ describe("EnvAPL", () => { "saleorApiUrl": "https://my-saleor-instance.cloud/graphql/", "appId": "app-id", "token": "some-token", - "jwks": "{}", - "domain": "my-saleor-instance.cloud" + "jwks": "{}" }` ); }); diff --git a/src/APL/file-apl.test.ts b/src/APL/file-apl.test.ts index 2de00bc5..bbfcedfb 100644 --- a/src/APL/file-apl.test.ts +++ b/src/APL/file-apl.test.ts @@ -5,7 +5,6 @@ import { AuthData } from "./apl"; import { FileAPL } from "./file-apl"; const stubAuthData: AuthData = { - domain: "example.com", token: "example-token", saleorApiUrl: "https://example.com/graphql/", appId: "42", diff --git a/src/APL/file-apl.ts b/src/APL/file-apl.ts index 9003bd2f..b3396101 100644 --- a/src/APL/file-apl.ts +++ b/src/APL/file-apl.ts @@ -48,10 +48,10 @@ export class FileAPL implements APL { return undefined; } - const { token, domain, saleorApiUrl, appId, jwks } = parsedData; + const { token, saleorApiUrl, appId, jwks } = parsedData; if (token && saleorApiUrl && appId) { - debug("Token and domain found, returning values: %s, %s", domain, `${token[0]}***`); + debug("Token found, returning values: %s", `${token[0]}***`); const authData: AuthData = { token, saleorApiUrl, appId }; @@ -59,10 +59,6 @@ export class FileAPL implements APL { authData.jwks = jwks; } - if (domain) { - authData.domain = domain; - } - return authData; } diff --git a/src/APL/has-auth-data.ts b/src/APL/has-auth-data.ts index 0901c66c..ea9d3df9 100644 --- a/src/APL/has-auth-data.ts +++ b/src/APL/has-auth-data.ts @@ -4,8 +4,6 @@ import { hasProp } from "../has-prop"; * Checks if given object has fields used by the AuthData */ export const hasAuthData = (data: unknown) => - hasProp(data, "domain") && - data.domain && hasProp(data, "token") && data.token && hasProp(data, "appId") && diff --git a/src/APL/saleor-cloud/saleor-cloud-apl.test.ts b/src/APL/saleor-cloud/saleor-cloud-apl.test.ts index e151b16e..fd07b93e 100644 --- a/src/APL/saleor-cloud/saleor-cloud-apl.test.ts +++ b/src/APL/saleor-cloud/saleor-cloud-apl.test.ts @@ -14,7 +14,6 @@ const aplConfig: SaleorCloudAPLConfig = { }; const stubAuthData: AuthData = { - domain: "example.com", token: "example-token", saleorApiUrl: "https://example.com/graphql/", appId: "42", @@ -47,7 +46,6 @@ describe("APL", () => { saleor_app_id: "42", saleor_api_url: "https://example.com/graphql/", jwks: "{}", - domain: "example.com", token: "example-token", }), headers: { @@ -129,7 +127,6 @@ describe("APL", () => { saleor_app_id: stubAuthData.appId, saleor_api_url: stubAuthData.saleorApiUrl, jwks: stubAuthData.jwks, - domain: stubAuthData.domain, token: stubAuthData.token, }), }); @@ -170,7 +167,6 @@ describe("APL", () => { saleor_app_id: stubAuthData.appId, saleor_api_url: stubAuthData.saleorApiUrl, jwks: stubAuthData.jwks, - domain: stubAuthData.domain, token: stubAuthData.token, }), }); @@ -219,7 +215,7 @@ describe("APL", () => { saleor_app_id: "x", }, { - domain: "example2.com", + domain: "example.com", jwks: "{}", token: "token2", saleor_api_url: "https://example2.com/graphql/", @@ -237,14 +233,12 @@ describe("APL", () => { expect(await apl.getAll()).toStrictEqual([ { appId: "x", - domain: "example.com", jwks: "{}", saleorApiUrl: "https://example.com/graphql/", token: "token1", }, { appId: "y", - domain: "example2.com", jwks: "{}", saleorApiUrl: "https://example2.com/graphql/", token: "token2", @@ -285,7 +279,7 @@ describe("APL", () => { previous: "https://example.com?page=1", results: [ { - domain: "example2.com", + domain: "example.com", jwks: "{}", token: "token2", saleor_api_url: "https://example2.com/graphql/", @@ -303,14 +297,12 @@ describe("APL", () => { expect(await apl.getAll()).toStrictEqual([ { appId: "x", - domain: "example.com", jwks: "{}", saleorApiUrl: "https://example.com/graphql/", token: "token1", }, { appId: "y", - domain: "example2.com", jwks: "{}", saleorApiUrl: "https://example2.com/graphql/", token: "token2", diff --git a/src/APL/saleor-cloud/saleor-cloud-apl.ts b/src/APL/saleor-cloud/saleor-cloud-apl.ts index 64368fa6..0e23e2a2 100644 --- a/src/APL/saleor-cloud/saleor-cloud-apl.ts +++ b/src/APL/saleor-cloud/saleor-cloud-apl.ts @@ -51,13 +51,11 @@ const mapAuthDataToAPIBody = (authData: AuthData) => ({ saleor_app_id: authData.appId, saleor_api_url: authData.saleorApiUrl, jwks: authData.jwks, - domain: authData.domain, token: authData.token, }); const mapAPIResponseToAuthData = (response: CloudAPLAuthDataShape): AuthData => ({ appId: response.saleor_app_id, - domain: response.domain, jwks: response.jwks, saleorApiUrl: response.saleor_api_url, token: response.token, diff --git a/src/APL/upstash-apl.test.ts b/src/APL/upstash-apl.test.ts index 0c475b69..74b52ac3 100644 --- a/src/APL/upstash-apl.test.ts +++ b/src/APL/upstash-apl.test.ts @@ -13,7 +13,6 @@ const aplConfig: UpstashAPLConfig = { }; const stubAuthData: AuthData = { - domain: "example.com", token: "example-token", saleorApiUrl: "https://example.com/graphql/", appId: "42", diff --git a/src/APL/vercel-kv/vercel-kv-apl.test.ts b/src/APL/vercel-kv/vercel-kv-apl.test.ts index 927b3013..7f2fbc8c 100644 --- a/src/APL/vercel-kv/vercel-kv-apl.test.ts +++ b/src/APL/vercel-kv/vercel-kv-apl.test.ts @@ -22,7 +22,6 @@ const getMockAuthData = (saleorApiUrl = "https://demo.saleor.io/graphql"): AuthD appId: "foobar", saleorApiUrl, token: "token", - domain: "domain", jwks: "{}", }); diff --git a/src/app-bridge/fetch.test.ts b/src/app-bridge/fetch.test.ts index 16aac979..5602c147 100644 --- a/src/app-bridge/fetch.test.ts +++ b/src/app-bridge/fetch.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vitest"; -import { SALEOR_AUTHORIZATION_BEARER_HEADER, SALEOR_DOMAIN_HEADER } from "../const"; +import { SALEOR_AUTHORIZATION_BEARER_HEADER } from "../const"; import { AppBridge } from "./app-bridge"; import { AppBridgeState } from "./app-bridge-state"; import { createAuthenticatedFetch } from "./fetch"; @@ -35,9 +35,6 @@ describe("createAuthenticatedFetch", () => { const fetchCallArguments = spiedFetch.mock.lastCall; const fetchCallHeaders = fetchCallArguments![1]?.headers; - expect((fetchCallHeaders as Headers).get(SALEOR_DOMAIN_HEADER)).toBe( - "master.staging.saleor.cloud" - ); expect((fetchCallHeaders as Headers).get(SALEOR_AUTHORIZATION_BEARER_HEADER)).toBe("XXX_YYY"); }); @@ -59,9 +56,6 @@ describe("createAuthenticatedFetch", () => { const fetchCallArguments = spiedFetch.mock.lastCall; const fetchCallHeaders = fetchCallArguments![1]?.headers; - expect((fetchCallHeaders as Headers).get(SALEOR_DOMAIN_HEADER)).toBe( - "master.staging.saleor.cloud" - ); expect((fetchCallHeaders as Headers).get(SALEOR_AUTHORIZATION_BEARER_HEADER)).toBe("XXX_YYY"); expect((fetchCallHeaders as Headers).get("foo")).toBe("bar"); }); diff --git a/src/app-bridge/fetch.ts b/src/app-bridge/fetch.ts index 3556d37f..1f0d7a96 100644 --- a/src/app-bridge/fetch.ts +++ b/src/app-bridge/fetch.ts @@ -1,10 +1,6 @@ import { useMemo } from "react"; -import { - SALEOR_API_URL_HEADER, - SALEOR_AUTHORIZATION_BEARER_HEADER, - SALEOR_DOMAIN_HEADER, -} from "../const"; +import { SALEOR_API_URL_HEADER, SALEOR_AUTHORIZATION_BEARER_HEADER } from "../const"; import { AppBridge } from "./app-bridge"; import { useAppBridge } from "./app-bridge-provider"; @@ -16,11 +12,10 @@ type HasAppBridgeState = Pick; export const createAuthenticatedFetch = (appBridge: HasAppBridgeState, fetch = global.fetch): typeof global.fetch => (input, init) => { - const { token, domain, saleorApiUrl } = appBridge.getState(); + const { token, saleorApiUrl } = appBridge.getState(); const headers = new Headers(init?.headers); - headers.set(SALEOR_DOMAIN_HEADER, domain); headers.set(SALEOR_AUTHORIZATION_BEARER_HEADER, token ?? ""); headers.set(SALEOR_API_URL_HEADER, saleorApiUrl ?? ""); diff --git a/src/const.ts b/src/const.ts index 92b6c435..d50a0d25 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1,4 +1,3 @@ -export const SALEOR_DOMAIN_HEADER = "saleor-domain"; export const SALEOR_EVENT_HEADER = "saleor-event"; export const SALEOR_SIGNATURE_HEADER = "saleor-signature"; export const SALEOR_AUTHORIZATION_BEARER_HEADER = "authorization-bearer"; diff --git a/src/handlers/next/create-app-register-handler.test.ts b/src/handlers/next/create-app-register-handler.test.ts index 709ed0f9..23a9bd84 100644 --- a/src/handlers/next/create-app-register-handler.test.ts +++ b/src/handlers/next/create-app-register-handler.test.ts @@ -52,7 +52,6 @@ describe("create-app-register-handler", () => { */ expect(mockApl.set).toHaveBeenCalledWith({ saleorApiUrl: "https://mock-saleor-domain.saleor.cloud/graphql/", - domain: "https://mock-saleor-domain.saleor.cloud/", token: "mock-auth-token", appId: "42", jwks: "{}", @@ -122,7 +121,6 @@ describe("create-app-register-handler", () => { const expectedAuthData: AuthData = { token: "mock-auth-token", - domain: "https://mock-saleor-domain.saleor.cloud/", saleorApiUrl: "https://mock-saleor-domain.saleor.cloud/graphql/", jwks: mockJwksValue, appId: mockAppId, @@ -134,7 +132,6 @@ describe("create-app-register-handler", () => { expect.anything(/* Assume original request */), expect.objectContaining({ authToken: "mock-auth-token", - saleorDomain: "https://mock-saleor-domain.saleor.cloud/", saleorApiUrl: "https://mock-saleor-domain.saleor.cloud/graphql/", }) ); @@ -186,7 +183,6 @@ describe("create-app-register-handler", () => { const expectedAuthData: AuthData = { token: "mock-auth-token", - domain: "https://mock-saleor-domain.saleor.cloud/", saleorApiUrl: "https://mock-saleor-domain.saleor.cloud/graphql/", jwks: mockJwksValue, appId: mockAppId, diff --git a/src/handlers/next/create-app-register-handler.ts b/src/handlers/next/create-app-register-handler.ts index 1e0da6de..9b930666 100644 --- a/src/handlers/next/create-app-register-handler.ts +++ b/src/handlers/next/create-app-register-handler.ts @@ -4,11 +4,11 @@ import { withMethod } from "retes/middleware"; import { Response } from "retes/response"; import { AuthData } from "../../APL"; -import { SALEOR_API_URL_HEADER, SALEOR_DOMAIN_HEADER } from "../../const"; +import { SALEOR_API_URL_HEADER } from "../../const"; import { createDebug } from "../../debug"; import { fetchRemoteJwks } from "../../fetch-remote-jwks"; import { getAppId } from "../../get-app-id"; -import { withAuthTokenRequired, withSaleorDomainPresent } from "../../middleware"; +import { withAuthTokenRequired } from "../../middleware"; import { HasAPL } from "../../saleor-app"; import { validateAllowSaleorUrls } from "./validate-allow-saleor-urls"; @@ -134,7 +134,6 @@ export const createAppRegisterHandler = ({ debug("Request received"); const authToken = request.params.auth_token; - const saleorDomain = request.headers[SALEOR_DOMAIN_HEADER] as string; const saleorApiUrl = request.headers[SALEOR_API_URL_HEADER] as string; if (onRequestStart) { @@ -144,7 +143,6 @@ export const createAppRegisterHandler = ({ await onRequestStart(request, { authToken, saleorApiUrl, - saleorDomain, respondWithError: createCallbackError, }); } catch (e: RegisterCallbackError | unknown) { @@ -218,7 +216,6 @@ export const createAppRegisterHandler = ({ } const authData = { - domain: saleorDomain, token: authToken, saleorApiUrl, appId, @@ -288,10 +285,5 @@ export const createAppRegisterHandler = ({ return Response.OK(createRegisterHandlerResponseBody(true)); }; - return toNextHandler([ - withMethod("POST"), - withSaleorDomainPresent, - withAuthTokenRequired, - baseHandler, - ]); + return toNextHandler([withMethod("POST"), withAuthTokenRequired, baseHandler]); }; diff --git a/src/handlers/next/process-protected-handler.test.ts b/src/handlers/next/process-protected-handler.test.ts index 69c3e221..ccc68e4f 100644 --- a/src/handlers/next/process-protected-handler.test.ts +++ b/src/handlers/next/process-protected-handler.test.ts @@ -56,7 +56,6 @@ describe("processSaleorProtectedHandler", () => { expect(await processSaleorProtectedHandler({ apl: mockAPL, req: mockRequest })).toStrictEqual({ authData: { - domain: mockAPL.workingSaleorDomain, token: mockAPL.mockToken, saleorApiUrl: mockAPL.workingSaleorApiUrl, appId: mockAPL.mockAppId, diff --git a/src/handlers/next/saleor-webhooks/process-saleor-webhook.test.ts b/src/handlers/next/saleor-webhooks/process-saleor-webhook.test.ts index ef16a5fa..a1dd35ff 100644 --- a/src/handlers/next/saleor-webhooks/process-saleor-webhook.test.ts +++ b/src/handlers/next/saleor-webhooks/process-saleor-webhook.test.ts @@ -160,7 +160,6 @@ describe("processAsyncSaleorWebhook", () => { ).resolves.toStrictEqual({ authData: { appId: "mock-app-id", - domain: "example.com", jwks: "{}", saleorApiUrl: "https://example.com/graphql/", token: "mock-token", diff --git a/src/handlers/next/saleor-webhooks/process-saleor-webhook.ts b/src/handlers/next/saleor-webhooks/process-saleor-webhook.ts index a6451802..74f20d10 100644 --- a/src/handlers/next/saleor-webhooks/process-saleor-webhook.ts +++ b/src/handlers/next/saleor-webhooks/process-saleor-webhook.ts @@ -16,7 +16,6 @@ const debug = createDebug("processSaleorWebhook"); export type SaleorWebhookError = | "OTHER" | "MISSING_HOST_HEADER" - | "MISSING_DOMAIN_HEADER" | "MISSING_API_URL_HEADER" | "MISSING_EVENT_HEADER" | "MISSING_PAYLOAD_HEADER" diff --git a/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts b/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts index 7c4dbeeb..24930540 100644 --- a/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts +++ b/src/handlers/next/saleor-webhooks/saleor-async-webhook.test.ts @@ -65,7 +65,6 @@ describe("SaleorAsyncWebhook", () => { payload: { data: "test_payload" }, schemaVersion: 3.19, authData: { - domain: "example.com", token: "token", jwks: "", saleorApiUrl: "https://example.com/graphql/", diff --git a/src/handlers/next/saleor-webhooks/saleor-sync-webhook.test.ts b/src/handlers/next/saleor-webhooks/saleor-sync-webhook.test.ts index 22431a83..282c39e4 100644 --- a/src/handlers/next/saleor-webhooks/saleor-sync-webhook.test.ts +++ b/src/handlers/next/saleor-webhooks/saleor-sync-webhook.test.ts @@ -17,7 +17,6 @@ describe("SaleorSyncWebhook", () => { payload: { data: "test_payload" }, schemaVersion: 3.19, authData: { - domain: mockApl.workingSaleorDomain, token: mockApl.mockToken, jwks: mockApl.mockJwks, saleorApiUrl: mockApl.workingSaleorApiUrl, diff --git a/src/handlers/next/saleor-webhooks/saleor-webhook.ts b/src/handlers/next/saleor-webhooks/saleor-webhook.ts index 9a0d7632..1a18527b 100644 --- a/src/handlers/next/saleor-webhooks/saleor-webhook.ts +++ b/src/handlers/next/saleor-webhooks/saleor-webhook.ts @@ -39,7 +39,6 @@ export interface WebhookConfig = { OTHER: 500, MISSING_HOST_HEADER: 400, - MISSING_DOMAIN_HEADER: 400, MISSING_API_URL_HEADER: 400, MISSING_EVENT_HEADER: 400, MISSING_PAYLOAD_HEADER: 400, diff --git a/src/headers.ts b/src/headers.ts index e5fc701b..b33ab93d 100644 --- a/src/headers.ts +++ b/src/headers.ts @@ -1,7 +1,6 @@ import { SALEOR_API_URL_HEADER, SALEOR_AUTHORIZATION_BEARER_HEADER, - SALEOR_DOMAIN_HEADER, SALEOR_EVENT_HEADER, SALEOR_SCHEMA_VERSION, SALEOR_SIGNATURE_HEADER, @@ -17,7 +16,6 @@ const toFloatOrNull = (value: string | string[] | undefined) => * Extracts Saleor-specific headers from the response. */ export const getSaleorHeaders = (headers: { [name: string]: string | string[] | undefined }) => ({ - domain: toStringOrUndefined(headers[SALEOR_DOMAIN_HEADER]), authorizationBearer: toStringOrUndefined(headers[SALEOR_AUTHORIZATION_BEARER_HEADER]), signature: toStringOrUndefined(headers[SALEOR_SIGNATURE_HEADER]), event: toStringOrUndefined(headers[SALEOR_EVENT_HEADER]), diff --git a/src/middleware/index.ts b/src/middleware/index.ts index f09e8217..561669bd 100644 --- a/src/middleware/index.ts +++ b/src/middleware/index.ts @@ -4,6 +4,5 @@ export * from "./with-base-url"; export * from "./with-jwt-verified"; export * from "./with-registered-saleor-domain-header"; export * from "./with-saleor-app"; -export * from "./with-saleor-domain-present"; export * from "./with-saleor-event-match"; export * from "./with-webhook-signature-verified"; diff --git a/src/middleware/middleware-debug.ts b/src/middleware/middleware-debug.ts index a44ea0ed..fbe0baa6 100644 --- a/src/middleware/middleware-debug.ts +++ b/src/middleware/middleware-debug.ts @@ -9,6 +9,8 @@ type DebugFactory = (handlerName: string) => (msg: string, ...args: unknown[]) = /** * Experimental. Needs to be tested and evaluated on security + * + * @depreacted Use `/handlers` instead of middlewares */ export const withReqResDebugging = (debugFactory: DebugFactory = createMiddlewareDebug): Middleware => diff --git a/src/middleware/with-auth-token-required.ts b/src/middleware/with-auth-token-required.ts index c54e9a28..e68a92df 100644 --- a/src/middleware/with-auth-token-required.ts +++ b/src/middleware/with-auth-token-required.ts @@ -5,6 +5,9 @@ import { createMiddlewareDebug } from "./middleware-debug"; const debug = createMiddlewareDebug("withAuthTokenRequired"); +/** + * @deprecated Use `/handlers` instead of middlewares + * */ export const withAuthTokenRequired: Middleware = (handler) => async (request) => { debug("Middleware called"); diff --git a/src/middleware/with-base-url.ts b/src/middleware/with-base-url.ts index 93f93d7a..0e0b7d59 100644 --- a/src/middleware/with-base-url.ts +++ b/src/middleware/with-base-url.ts @@ -5,6 +5,9 @@ import { createMiddlewareDebug } from "./middleware-debug"; const debug = createMiddlewareDebug("withBaseURL"); +/** + * @deprecated Use `/handlers` instead of middlewares + * */ export const withBaseURL: Middleware = (handler) => async (request) => { const { host, "x-forwarded-proto": protocol = "http" } = request.headers; diff --git a/src/middleware/with-jwt-verified.ts b/src/middleware/with-jwt-verified.ts index 16ff6e96..30a11ceb 100644 --- a/src/middleware/with-jwt-verified.ts +++ b/src/middleware/with-jwt-verified.ts @@ -15,6 +15,9 @@ export interface DashboardTokenPayload extends jose.JWTPayload { const ERROR_MESSAGE = "JWT verification failed:"; +/** + * @deprecated Use `/handlers` instead of middlewares + * */ export const withJWTVerified = (getAppId: (request: Request) => Promise): Middleware => (handler) => diff --git a/src/middleware/with-registered-saleor-domain-header.test.ts b/src/middleware/with-registered-saleor-domain-header.test.ts index ac093c4d..707d820c 100644 --- a/src/middleware/with-registered-saleor-domain-header.test.ts +++ b/src/middleware/with-registered-saleor-domain-header.test.ts @@ -2,7 +2,7 @@ import { Handler, Request } from "retes"; import { Response } from "retes/response"; import { beforeEach, describe, expect, it, vi } from "vitest"; -import { SALEOR_API_URL_HEADER, SALEOR_DOMAIN_HEADER } from "../const"; +import { SALEOR_API_URL_HEADER } from "../const"; import { SaleorApp } from "../saleor-app"; import { MockAPL } from "../test-utils/mock-apl"; import { withRegisteredSaleorDomainHeader } from "./with-registered-saleor-domain-header"; @@ -26,7 +26,6 @@ describe("middleware", () => { headers: { host: "my-saleor-env.saleor.cloud", "x-forwarded-proto": "https", - [SALEOR_DOMAIN_HEADER]: mockAPL.workingSaleorDomain, [SALEOR_API_URL_HEADER]: mockAPL.workingSaleorApiUrl, }, } as unknown as Request; @@ -49,7 +48,6 @@ describe("middleware", () => { headers: { host: "my-saleor-env.saleor.cloud", "x-forwarded-proto": "https", - [SALEOR_DOMAIN_HEADER]: "not-registered.example.com", [SALEOR_API_URL_HEADER]: "https://not-registered.example.com/graphql/", }, } as unknown as Request; @@ -71,7 +69,6 @@ describe("middleware", () => { headers: { host: "my-saleor-env.saleor.cloud", "x-forwarded-proto": "https", - [SALEOR_DOMAIN_HEADER]: mockAPL.workingSaleorDomain, [SALEOR_API_URL_HEADER]: mockAPL.workingSaleorApiUrl, }, } as unknown as Request; diff --git a/src/middleware/with-registered-saleor-domain-header.ts b/src/middleware/with-registered-saleor-domain-header.ts index 7551e2c4..8ffb28ef 100644 --- a/src/middleware/with-registered-saleor-domain-header.ts +++ b/src/middleware/with-registered-saleor-domain-header.ts @@ -7,6 +7,9 @@ import { getSaleorAppFromRequest } from "./with-saleor-app"; const debug = createMiddlewareDebug("withRegisteredSaleorDomainHeader"); +/** + * @deprecated Use `/handlers` instead of middlewares + * */ export const withRegisteredSaleorDomainHeader: Middleware = (handler) => async (request) => { const { saleorApiUrl } = getSaleorHeaders(request.headers); diff --git a/src/middleware/with-saleor-app.test.ts b/src/middleware/with-saleor-app.test.ts index b026f6c6..c291f77e 100644 --- a/src/middleware/with-saleor-app.test.ts +++ b/src/middleware/with-saleor-app.test.ts @@ -3,7 +3,6 @@ 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"; @@ -12,9 +11,6 @@ describe("middleware", () => { 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) => { diff --git a/src/middleware/with-saleor-app.ts b/src/middleware/with-saleor-app.ts index b7decc30..75c8c068 100644 --- a/src/middleware/with-saleor-app.ts +++ b/src/middleware/with-saleor-app.ts @@ -5,6 +5,9 @@ import { createMiddlewareDebug } from "./middleware-debug"; const debug = createMiddlewareDebug("withSaleorApp"); +/** + * @deprecated Use `/handlers` instead of middlewares + * */ export const withSaleorApp = (saleorApp: SaleorApp): Middleware => (handler) => diff --git a/src/middleware/with-saleor-domain-present.test.ts b/src/middleware/with-saleor-domain-present.test.ts deleted file mode 100644 index 15aa579d..00000000 --- a/src/middleware/with-saleor-domain-present.test.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Handler, Request } from "retes"; -import { Response } from "retes/response"; -import { beforeEach, describe, expect, it, vi } from "vitest"; - -import { SALEOR_DOMAIN_HEADER } from "../const"; -import { withSaleorDomainPresent } from "./with-saleor-domain-present"; - -const getMockSuccessResponse = async () => Response.OK({}); - -describe("middleware", () => { - describe("withSaleorDomainPresent", () => { - let mockHandlerFn: Handler = vi.fn(getMockSuccessResponse); - - beforeEach(() => { - mockHandlerFn = vi.fn(getMockSuccessResponse); - }); - - it("Pass request when request has Saleor Domain header", async () => { - const mockRequest = { - context: {}, - headers: { - [SALEOR_DOMAIN_HEADER]: "example.com", - }, - } as unknown as Request; - - const response = await withSaleorDomainPresent(mockHandlerFn)(mockRequest); - - expect(response.status).toBe(200); - expect(mockHandlerFn).toHaveBeenCalledOnce(); - }); - - it("Reject request when Saleor domain header is not present", async () => { - const mockRequest = { - context: {}, - headers: {}, - } as unknown as Request; - - const response = await withSaleorDomainPresent(mockHandlerFn)(mockRequest); - expect(response.status).eq(400); - expect(mockHandlerFn).toBeCalledTimes(0); - }); - }); -}); diff --git a/src/middleware/with-saleor-domain-present.ts b/src/middleware/with-saleor-domain-present.ts deleted file mode 100644 index c4588249..00000000 --- a/src/middleware/with-saleor-domain-present.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Middleware } from "retes"; -import { Response } from "retes/response"; - -import { getSaleorHeaders } from "../headers"; -import { createMiddlewareDebug } from "./middleware-debug"; - -const debug = createMiddlewareDebug("withSaleorDomainPresent"); - -export const withSaleorDomainPresent: Middleware = (handler) => async (request) => { - const { domain } = getSaleorHeaders(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.BadRequest({ - success: false, - message: "Missing Saleor domain header.", - }); - } - - return handler(request); -}; diff --git a/src/middleware/with-saleor-event-match.ts b/src/middleware/with-saleor-event-match.ts index 6cdae58e..4705e32a 100644 --- a/src/middleware/with-saleor-event-match.ts +++ b/src/middleware/with-saleor-event-match.ts @@ -6,6 +6,9 @@ import { createMiddlewareDebug } from "./middleware-debug"; const debug = createMiddlewareDebug("withSaleorEventMatch"); +/** + * @deprecated Use `/handlers` instead of middlewares + * */ export const withSaleorEventMatch = (expectedEvent: `${Lowercase}`): Middleware => (handler) => diff --git a/src/middleware/with-webhook-signature-verified.ts b/src/middleware/with-webhook-signature-verified.ts index 516eea6e..1ed82d83 100644 --- a/src/middleware/with-webhook-signature-verified.ts +++ b/src/middleware/with-webhook-signature-verified.ts @@ -12,8 +12,8 @@ const debug = createMiddlewareDebug("withWebhookSignatureVerified"); const ERROR_MESSAGE = "Webhook signature verification failed:"; /** - * TODO: Add test - */ + * @deprecated Use `/handlers` instead of middlewares + * */ export const withWebhookSignatureVerified = (secretKey: string | undefined = undefined): Middleware => (handler) => diff --git a/src/settings-manager/metadata-manager.ts b/src/settings-manager/metadata-manager.ts index d2f74f3f..439749ac 100644 --- a/src/settings-manager/metadata-manager.ts +++ b/src/settings-manager/metadata-manager.ts @@ -23,7 +23,8 @@ const deserializeMetadata = ({ key, value }: MetadataEntry): SettingsValue => { }; }; -const constructDomainSpecificKey = (key: string, domain: string) => [key, domain].join("__"); +const constructDomainSpecificKey = (key: string, saleorApiUrl: string) => + [key, saleorApiUrl].join("__"); const serializeSettingsToMetadata = ({ key, value, domain }: SettingsValue): MetadataEntry => { // domain specific metadata use convention key__domain, e.g. `secret_key__example.com` diff --git a/src/test-utils/mock-apl.ts b/src/test-utils/mock-apl.ts index 28a84c87..6cde6d5f 100644 --- a/src/test-utils/mock-apl.ts +++ b/src/test-utils/mock-apl.ts @@ -43,7 +43,6 @@ export class MockAPL implements APL { async get(saleorApiUrl: string) { if (saleorApiUrl === this.workingSaleorApiUrl) { return { - domain: this.resolveDomainFromApiUrl(saleorApiUrl), token: this.mockToken, saleorApiUrl, appId: this.mockAppId,