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

feat: adding redis APL #374

Merged
merged 17 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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/ninety-otters-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saleor/app-sdk": minor
---

Adding REDIS as APL provider
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"graphql": ">=16.6.0",
"next": ">=12",
"react": ">=17",
"react-dom": ">=17"
"react-dom": ">=17",
"redis": ">=4"
},
"dependencies": {
"@opentelemetry/api": "^1.7.0",
Expand Down Expand Up @@ -70,6 +71,7 @@
"prettier": "2.7.1",
"react": "^18.2.0",
"react-dom": "18.2.0",
"redis": "^4.7.0",
"tsm": "^2.2.2",
"tsup": "^6.2.3",
"typescript": "4.9.5",
Expand All @@ -80,6 +82,9 @@
"peerDependenciesMeta": {
"@vercel/kv": {
"optional": true
},
"redis": {
"optional": true
}
},
"lint-staged": {
Expand Down
82 changes: 82 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/APL/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./apl";
export * from "./env-apl";
export * from "./file-apl";
export * from "./redis";
Copy link
Member

Choose a reason for hiding this comment

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

@JannikZed let's avoid that, now importing any other APL will evaluate redis, this causes issues, e.g. next is poor at tree shaking, possibly fail because it can't import redis sdk

Please check package.json and add exports key with new path, like vercel-kv. For example

    "./APL/redis": {
      "types": "./APL/redis/index.d.ts",
      "import": "./APL/redis/index.mjs",
      "require": "./APL/redis/index.js"
    },

also new entry point will be needed

https://github.com/saleor/app-sdk/blob/main/tsup.config.ts#L13

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@IKarbowiak alright, check it out, if that works for you.
Is there a very easy way to test also if the package got build correctly and we can actually use the different APLs? are you spinning up an example saleor app and installing it locally or how are you doing that?

Copy link
Member

@lkostrowski lkostrowski Jan 17, 2025

Choose a reason for hiding this comment

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

I would do this

  1. build sdk
  2. set up app-template
  3. add sdk locally like pnpm add ../saleor-app-sdk
  4. pnpm install
  5. try it

of course other APLs must work if redis package is not installed

export * from "./saleor-cloud";
export * from "./upstash-apl";
3 changes: 3 additions & 0 deletions src/APL/redis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { RedisAPL } from "./redis-apl";

export { RedisAPL };
197 changes: 197 additions & 0 deletions src/APL/redis/redis-apl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import { createClient } from "redis";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { AuthData } from "../apl";
import { RedisAPL } from "./redis-apl";

// Create a variable to control the connection state
let isRedisOpen = false;

// Create properly typed mock functions
const mockHGet = vi.fn().mockResolvedValue(undefined);
const mockHSet = vi.fn().mockResolvedValue(1);
const mockHDel = vi.fn().mockResolvedValue(1);
const mockHGetAll = vi.fn().mockResolvedValue({});
const mockPing = vi.fn().mockResolvedValue("PONG");
const mockConnect = vi.fn().mockImplementation(async () => {
isRedisOpen = true;
});
const mockDisconnect = vi.fn().mockImplementation(async () => {
isRedisOpen = false;
});

const mockRedisClient = {
connect: mockConnect,
ping: mockPing,
hGet: mockHGet,
hSet: mockHSet,
hDel: mockHDel,
hGetAll: mockHGetAll,
get isOpen() {
return isRedisOpen;
},
disconnect: mockDisconnect,
isReady: false,
};

vi.mock("redis", () => ({
createClient: vi.fn(() => mockRedisClient),
}));

describe("RedisAPL", () => {
const mockHashKey = "test_hash_key";
const mockAuthData: AuthData = {
token: "test-token",
saleorApiUrl: "https://test-store.saleor.cloud/graphql/",
appId: "test-app-id",
};

let apl: RedisAPL;

beforeEach(() => {
apl = new RedisAPL({
client: mockRedisClient,
hashCollectionKey: mockHashKey,
});
vi.clearAllMocks();
isRedisOpen = false;
});

afterEach(async () => {
if (mockRedisClient.isOpen) {
await mockRedisClient.disconnect();
}
vi.clearAllMocks();
});

describe("constructor", () => {
it("uses provided hash key", async () => {
const customHashKey = "custom_hash";
const customApl = new RedisAPL({
client: mockRedisClient,
hashCollectionKey: customHashKey,
});

await customApl.get(mockAuthData.saleorApiUrl);
expect(mockRedisClient.hGet).toHaveBeenCalledWith(customHashKey, mockAuthData.saleorApiUrl);
});

it("uses default hash key when not provided", async () => {
const defaultApl = new RedisAPL({
client: mockRedisClient,
});

await defaultApl.get(mockAuthData.saleorApiUrl);
expect(mockRedisClient.hGet).toHaveBeenCalledWith(
"saleor_app_auth",
mockAuthData.saleorApiUrl
);
});
});

describe("get", () => {
it("returns undefined when no data found", async () => {
mockHGet.mockResolvedValueOnce(null);
const result = await apl.get(mockAuthData.saleorApiUrl);
expect(result).toBeUndefined();
expect(mockConnect).toHaveBeenCalled();
});

it("returns parsed auth data when found", async () => {
mockHGet.mockResolvedValueOnce(JSON.stringify(mockAuthData));
const result = await apl.get(mockAuthData.saleorApiUrl);
expect(result).toEqual(mockAuthData);
expect(mockHGet).toHaveBeenCalledWith(mockHashKey, mockAuthData.saleorApiUrl);
});

it("throws error when Redis operation fails", async () => {
mockHGet.mockRejectedValueOnce(new Error("Redis error"));
await expect(apl.get(mockAuthData.saleorApiUrl)).rejects.toThrow("Redis error");
});
});

describe("set", () => {
it("successfully sets auth data", async () => {
mockHSet.mockResolvedValueOnce(1);
await apl.set(mockAuthData);
expect(mockHSet).toHaveBeenCalledWith(
mockHashKey,
mockAuthData.saleorApiUrl,
JSON.stringify(mockAuthData)
);
});

it("throws error when Redis operation fails", async () => {
mockHSet.mockRejectedValueOnce(new Error("Redis error"));
await expect(apl.set(mockAuthData)).rejects.toThrow("Redis error");
});
});

describe("delete", () => {
it("successfully deletes auth data", async () => {
mockHDel.mockResolvedValueOnce(1);
await apl.delete(mockAuthData.saleorApiUrl);
expect(mockHDel).toHaveBeenCalledWith(mockHashKey, mockAuthData.saleorApiUrl);
});

it("throws error when Redis operation fails", async () => {
mockHDel.mockRejectedValueOnce(new Error("Redis error"));
await expect(apl.delete(mockAuthData.saleorApiUrl)).rejects.toThrow("Redis error");
});
});

describe("getAll", () => {
it("returns all auth data", async () => {
const mockAllData = {
[mockAuthData.saleorApiUrl]: JSON.stringify(mockAuthData),
};
mockHGetAll.mockResolvedValueOnce(mockAllData);
const result = await apl.getAll();
expect(result).toEqual([mockAuthData]);
});

it("throws error when Redis operation fails", async () => {
mockHGetAll.mockRejectedValueOnce(new Error("Redis error"));
await expect(apl.getAll()).rejects.toThrow("Redis error");
});
});

describe("isReady", () => {
it("returns ready true when Redis is connected", async () => {
mockPing.mockResolvedValueOnce("PONG");
const result = await apl.isReady();
expect(result).toEqual({ ready: true });
});

it("returns ready false with error when Redis is not connected", async () => {
mockPing.mockRejectedValueOnce(new Error("Connection failed"));
const result = await apl.isReady();
expect(result).toEqual({ ready: false, error: new Error("Connection failed") });
});
});

describe("isConfigured", () => {
it("returns configured true when Redis is connected", async () => {
mockPing.mockResolvedValueOnce("PONG");
const result = await apl.isConfigured();
expect(result).toEqual({ configured: true });
});

it("returns configured false with error when Redis is not connected", async () => {
mockPing.mockRejectedValueOnce(new Error("Connection failed"));
const result = await apl.isConfigured();
expect(result).toEqual({ configured: false, error: new Error("Connection failed") });
});
});

/**
* Type compatibility test with real Redis client
*/
describe("RedisAPL type compatibility", () => {
it("should accept Redis client type", () => {
const client = createClient();
// This test is just for TypeScript to verify the types
expect(() => new RedisAPL({ client, hashCollectionKey: "test" })).not.toThrow();
});
});
});
Loading
Loading