Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark middlewares as deprecated #395

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-socks-tease.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changeset/witty-carrots-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saleor/app-sdk": patch
---

Marked `/middleware`s as deprecated in favor of `/handlers`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are merging this to main branch, which is 1.0.0 pre and will have breaking change. You can just remove them now, no need to deprecation.

Deprecation makes sense in v0.x branch

Copy link
Member Author

@witoszekdev witoszekdev Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, I set wrong base branch 👍🏻

1 change: 1 addition & 0 deletions .github/workflows/prepare-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- "v[0-9]+.x"

concurrency: ${{ github.workflow }}-${{ github.ref }}

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
patch:
default:
informational: true
project:
default:
informational: true
1 change: 0 additions & 1 deletion src/APL/apl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface AuthData {
domain?: string;
token: string;
saleorApiUrl: string;
appId: string;
Expand Down
3 changes: 1 addition & 2 deletions src/APL/auth-data-from-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
4 changes: 1 addition & 3 deletions src/APL/env-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const getMockAuthData = (): AuthData => ({
appId: "app-id",
token: "some-token",
jwks: "{}",
domain: "my-saleor-instance.cloud",
});

describe("EnvAPL", () => {
Expand Down Expand Up @@ -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": "{}"
}`
);
});
Expand Down
1 change: 0 additions & 1 deletion src/APL/file-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 2 additions & 6 deletions src/APL/file-apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,17 @@ 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 };

if (jwks) {
authData.jwks = jwks;
}

if (domain) {
authData.domain = domain;
}

return authData;
}

Expand Down
2 changes: 0 additions & 2 deletions src/APL/has-auth-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") &&
Expand Down
12 changes: 2 additions & 10 deletions src/APL/saleor-cloud/saleor-cloud-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const aplConfig: SaleorCloudAPLConfig = {
};

const stubAuthData: AuthData = {
domain: "example.com",
token: "example-token",
saleorApiUrl: "https://example.com/graphql/",
appId: "42",
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
}),
});
Expand Down Expand Up @@ -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,
}),
});
Expand Down Expand Up @@ -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/",
Expand All @@ -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",
Expand Down Expand Up @@ -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/",
Expand All @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions src/APL/saleor-cloud/saleor-cloud-apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/APL/upstash-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const aplConfig: UpstashAPLConfig = {
};

const stubAuthData: AuthData = {
domain: "example.com",
token: "example-token",
saleorApiUrl: "https://example.com/graphql/",
appId: "42",
Expand Down
1 change: 0 additions & 1 deletion src/APL/vercel-kv/vercel-kv-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const getMockAuthData = (saleorApiUrl = "https://demo.saleor.io/graphql"): AuthD
appId: "foobar",
saleorApiUrl,
token: "token",
domain: "domain",
jwks: "{}",
});

Expand Down
8 changes: 1 addition & 7 deletions src/app-bridge/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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");
});

Expand All @@ -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");
});
Expand Down
9 changes: 2 additions & 7 deletions src/app-bridge/fetch.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -16,11 +12,10 @@ type HasAppBridgeState = Pick<AppBridge, "getState">;
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 ?? "");

Expand Down
1 change: 0 additions & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 0 additions & 4 deletions src/handlers/next/create-app-register-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "{}",
Expand Down Expand Up @@ -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,
Expand All @@ -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/",
})
);
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 3 additions & 11 deletions src/handlers/next/create-app-register-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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) {
Expand All @@ -144,7 +143,6 @@ export const createAppRegisterHandler = ({
await onRequestStart(request, {
authToken,
saleorApiUrl,
saleorDomain,
respondWithError: createCallbackError,
});
} catch (e: RegisterCallbackError | unknown) {
Expand Down Expand Up @@ -218,7 +216,6 @@ export const createAppRegisterHandler = ({
}

const authData = {
domain: saleorDomain,
token: authToken,
saleorApiUrl,
appId,
Expand Down Expand Up @@ -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]);
};
1 change: 0 additions & 1 deletion src/handlers/next/process-protected-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading