From bb0786a54452a9509dbfd6b0d66b3a4f6f4f2401 Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Wed, 12 Oct 2022 10:08:32 +0200 Subject: [PATCH] Fix checking APL configuration state (#79) --- src/APL/apl.ts | 10 +++++++++ src/APL/file-apl.ts | 13 +++++++++++- src/APL/vercel-apl.ts | 21 ++++++++++++++++--- .../next/create-app-register-handler.test.ts | 3 +++ .../next/create-app-register-handler.ts | 8 +++---- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/APL/apl.ts b/src/APL/apl.ts index 063aab4..da8b4e2 100644 --- a/src/APL/apl.ts +++ b/src/APL/apl.ts @@ -12,6 +12,15 @@ export type AplReadyResult = error: Error; }; +export type AplConfiguredResult = + | { + configured: true; + } + | { + configured: false; + error: Error; + }; + export interface APL { get: (domain: string) => Promise; set: (authData: AuthData) => Promise; @@ -21,4 +30,5 @@ export interface APL { * Inform that configuration is finished and correct */ isReady: () => Promise; + isConfigured: () => Promise; } diff --git a/src/APL/file-apl.ts b/src/APL/file-apl.ts index 9cfa5c5..f6f5220 100644 --- a/src/APL/file-apl.ts +++ b/src/APL/file-apl.ts @@ -1,6 +1,6 @@ import { promises as fsPromises } from "fs"; -import { APL, AplReadyResult, AuthData } from "./apl"; +import { APL, AplConfiguredResult, AplReadyResult, AuthData } from "./apl"; import { createAPLDebug } from "./apl-debug"; const debug = createAPLDebug("FileAPL"); @@ -109,4 +109,15 @@ export class FileAPL implements APL { ready: true, }; } + + // eslint-disable-next-line class-methods-use-this + async isConfigured(): Promise { + /** + * Assume FileAPL is just ready to use. + * Consider checking if directory is writable + */ + return { + configured: true, + }; + } } diff --git a/src/APL/vercel-apl.ts b/src/APL/vercel-apl.ts index b8ed744..78981dc 100644 --- a/src/APL/vercel-apl.ts +++ b/src/APL/vercel-apl.ts @@ -2,7 +2,7 @@ // eslint-disable-next-line max-classes-per-file import fetch, { Response } from "node-fetch"; -import { APL, AplReadyResult, AuthData } from "./apl"; +import { APL, AplConfiguredResult, AplReadyResult, AuthData } from "./apl"; import { createAPLDebug } from "./apl-debug"; const debug = createAPLDebug("VercelAPL"); @@ -14,7 +14,7 @@ export const VercelAPLVariables = { SALEOR_DEPLOYMENT_TOKEN: "SALEOR_DEPLOYMENT_TOKEN", }; -export class VercelAplMisconfiguredError extends Error { +export class VercelAplNotReadyError extends Error { constructor(public missingEnvVars: string[]) { super( `Env variables: ${missingEnvVars @@ -24,6 +24,8 @@ export class VercelAplMisconfiguredError extends Error { } } +export class VercelAplNotConfiguredError extends Error {} + const getEnvAuth = (): AuthData | undefined => { const token = process.env[VercelAPLVariables.TOKEN_VARIABLE_NAME]; const domain = process.env[VercelAPLVariables.DOMAIN_VARIABLE_NAME]; @@ -146,7 +148,7 @@ export class VercelAPL implements APL { if (invalidEnvKeys.length > 0) { return { ready: false, - error: new VercelAplMisconfiguredError(invalidEnvKeys), + error: new VercelAplNotReadyError(invalidEnvKeys), }; } @@ -154,4 +156,17 @@ export class VercelAPL implements APL { ready: true, }; } + + async isConfigured(): Promise { + return this.registerAppURL && this.deploymentToken + ? { + configured: true, + } + : { + configured: false, + error: new VercelAplNotConfiguredError( + "VercelAPL not configured. Check if register URL and deployment token provided in constructor or env " + ), + }; + } } diff --git a/src/handlers/next/create-app-register-handler.test.ts b/src/handlers/next/create-app-register-handler.test.ts index bd45a03..aed96c8 100644 --- a/src/handlers/next/create-app-register-handler.test.ts +++ b/src/handlers/next/create-app-register-handler.test.ts @@ -14,6 +14,9 @@ describe("create-app-register-handler", () => { isReady: vi.fn().mockImplementation(async () => ({ ready: true, })), + isConfigured: vi.fn().mockImplementation(async () => ({ + configured: true, + })), }; const { res, req } = createMocks({ diff --git a/src/handlers/next/create-app-register-handler.ts b/src/handlers/next/create-app-register-handler.ts index 4955dd8..5b356bf 100644 --- a/src/handlers/next/create-app-register-handler.ts +++ b/src/handlers/next/create-app-register-handler.ts @@ -19,15 +19,15 @@ export const createAppRegisterHandler = ({ apl }: CreateAppRegisterHandlerOption const authToken = request.params.auth_token; const saleorDomain = request.headers[SALEOR_DOMAIN_HEADER] as string; - const { ready: aplReady } = await apl.isReady(); + const { configured: aplConfigured } = await apl.isConfigured(); - if (!aplReady) { + if (!aplConfigured) { return new Response( { success: false, error: { - code: "APL_NOT_READY", - message: "App is not ready yet", + code: "APL_NOT_CONFIGURED", + message: "APL_NOT_CONFIGURED. App is configured properly. Check APL docs for help.", }, }, {