Fix data serialization in the UpstashAPL (#228)

This commit is contained in:
Krzysztof Wolski 2023-04-12 21:21:19 +02:00 committed by GitHub
parent 5057d3491e
commit 5a68bec557
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 8 deletions

View file

@ -0,0 +1,5 @@
---
"@saleor/app-sdk": patch
---
Fix serialization of the nested values in the UpstashAPL.

View file

@ -57,8 +57,7 @@ describe("APL", () => {
"https://example.com", "https://example.com",
{ {
// eslint-disable-next-line quotes body: JSON.stringify(["SET", stubAuthData.saleorApiUrl, JSON.stringify(stubAuthData)]),
body: `["SET", "${stubAuthData.saleorApiUrl}", "${JSON.stringify(stubAuthData)}"]`,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: "Bearer token", Authorization: "Bearer token",

View file

@ -54,7 +54,7 @@ export class UpstashAPL implements APL {
this.restToken = restToken; this.restToken = restToken;
} }
private async upstashRequest(requestBody: string) { private async upstashRequest(request: string[]) {
debug("Sending request to Upstash"); debug("Sending request to Upstash");
if (!this.restURL || !this.restToken) { if (!this.restURL || !this.restToken) {
throw new Error( throw new Error(
@ -66,7 +66,7 @@ export class UpstashAPL implements APL {
response = await fetch(this.restURL, { response = await fetch(this.restURL, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.restToken}` }, headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.restToken}` },
body: requestBody, body: JSON.stringify(request),
}); });
} catch (error) { } catch (error) {
debug("Error during sending the data:", error); debug("Error during sending the data:", error);
@ -97,17 +97,17 @@ export class UpstashAPL implements APL {
}); });
const data = JSON.stringify(authData); const data = JSON.stringify(authData);
await this.upstashRequest(`["SET", "${authData.saleorApiUrl}", "${data}"]`); await this.upstashRequest(["SET", authData.saleorApiUrl, data]);
} }
private async deleteDataFromUpstash(saleorApiUrl: string) { private async deleteDataFromUpstash(saleorApiUrl: string) {
await this.upstashRequest(`["DEL", "${saleorApiUrl}"]`); await this.upstashRequest(["DEL", saleorApiUrl]);
} }
private async fetchDataFromUpstash(saleorApiUrl: string) { private async fetchDataFromUpstash(saleorApiUrl: string) {
const result = await this.upstashRequest(`["GET", "${saleorApiUrl}"]`); const result = await this.upstashRequest(["GET", saleorApiUrl]);
if (result) { if (result) {
const authData = JSON.parse(result); const authData = JSON.parse(result) as AuthData;
return authData; return authData;
} }
return undefined; return undefined;