app-sdk
app-sdk copied to clipboard
Redis APL provided by app-sdk
What I'm trying to achieve
Use an APL that I don't have to pay another party for . For simple KV storage it should be enough to just use the Redis db that is inlcuded in saleor-platform.
Describe a proposed solution
Add Redis APL as another possible import, and also add an .env switch for it inside apps.
Screenshots or mockups
As per https://docs.saleor.io/docs/3.x/developer/extending/apps/developing-apps/app-sdk/apl#example-implementation someone already wrote how to make it, so just use that in app-sdk :) Code snippet from the docs:
import { createClient } from "redis";
import { APL, AuthData } from "@saleor/app-sdk/apl";
const client = createClient();
await client.connect();
/**
* The APL uses API URL as a key to store and retrieve AuthData.
* If you intend to use the same Redis instance for multiple Apps,
* add prefix to the keys to avoid overwriting the data by different apps.
* Keys will be formatted as below:
* - `APPID1:http://staging.saleor.io/graphql`
* - `APPID1:http://demo.saleor.io/graphql`
* - `APPID2:http://demo.saleor.io/graphql`
**/
const prepareAuthDataKey = (apiUrl: string) => `${APP_ID}:${apiUrl}`;
const redisAPL: APL = {
get: async (saleorApiUrl: string) => {
const response = await client.get(prepareAuthDataKey(saleorApiUrl));
if (response) {
return JSON.parse(response);
}
return;
},
set: async (authData: AuthData) => {
await client.set(
prepareAuthDataKey(authData.saleorApiUrl),
JSON.stringify(authData)
);
},
delete: async (saleorApiUrl: string) => {
await client.del(prepareAuthDataKey(saleorApiUrl));
},
getAll: async () => {
throw new Exception("Not implemented.");
},
};