Fix checking APL configuration state (#79)

This commit is contained in:
Lukasz Ostrowski 2022-10-12 10:08:32 +02:00 committed by GitHub
parent d81e61d3f6
commit bb0786a544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 8 deletions

View file

@ -12,6 +12,15 @@ export type AplReadyResult =
error: Error; error: Error;
}; };
export type AplConfiguredResult =
| {
configured: true;
}
| {
configured: false;
error: Error;
};
export interface APL { export interface APL {
get: (domain: string) => Promise<AuthData | undefined>; get: (domain: string) => Promise<AuthData | undefined>;
set: (authData: AuthData) => Promise<void>; set: (authData: AuthData) => Promise<void>;
@ -21,4 +30,5 @@ export interface APL {
* Inform that configuration is finished and correct * Inform that configuration is finished and correct
*/ */
isReady: () => Promise<AplReadyResult>; isReady: () => Promise<AplReadyResult>;
isConfigured: () => Promise<AplConfiguredResult>;
} }

View file

@ -1,6 +1,6 @@
import { promises as fsPromises } from "fs"; import { promises as fsPromises } from "fs";
import { APL, AplReadyResult, AuthData } from "./apl"; import { APL, AplConfiguredResult, AplReadyResult, AuthData } from "./apl";
import { createAPLDebug } from "./apl-debug"; import { createAPLDebug } from "./apl-debug";
const debug = createAPLDebug("FileAPL"); const debug = createAPLDebug("FileAPL");
@ -109,4 +109,15 @@ export class FileAPL implements APL {
ready: true, ready: true,
}; };
} }
// eslint-disable-next-line class-methods-use-this
async isConfigured(): Promise<AplConfiguredResult> {
/**
* Assume FileAPL is just ready to use.
* Consider checking if directory is writable
*/
return {
configured: true,
};
}
} }

View file

@ -2,7 +2,7 @@
// eslint-disable-next-line max-classes-per-file // eslint-disable-next-line max-classes-per-file
import fetch, { Response } from "node-fetch"; import fetch, { Response } from "node-fetch";
import { APL, AplReadyResult, AuthData } from "./apl"; import { APL, AplConfiguredResult, AplReadyResult, AuthData } from "./apl";
import { createAPLDebug } from "./apl-debug"; import { createAPLDebug } from "./apl-debug";
const debug = createAPLDebug("VercelAPL"); const debug = createAPLDebug("VercelAPL");
@ -14,7 +14,7 @@ export const VercelAPLVariables = {
SALEOR_DEPLOYMENT_TOKEN: "SALEOR_DEPLOYMENT_TOKEN", SALEOR_DEPLOYMENT_TOKEN: "SALEOR_DEPLOYMENT_TOKEN",
}; };
export class VercelAplMisconfiguredError extends Error { export class VercelAplNotReadyError extends Error {
constructor(public missingEnvVars: string[]) { constructor(public missingEnvVars: string[]) {
super( super(
`Env variables: ${missingEnvVars `Env variables: ${missingEnvVars
@ -24,6 +24,8 @@ export class VercelAplMisconfiguredError extends Error {
} }
} }
export class VercelAplNotConfiguredError extends Error {}
const getEnvAuth = (): AuthData | undefined => { const getEnvAuth = (): AuthData | undefined => {
const token = process.env[VercelAPLVariables.TOKEN_VARIABLE_NAME]; const token = process.env[VercelAPLVariables.TOKEN_VARIABLE_NAME];
const domain = process.env[VercelAPLVariables.DOMAIN_VARIABLE_NAME]; const domain = process.env[VercelAPLVariables.DOMAIN_VARIABLE_NAME];
@ -146,7 +148,7 @@ export class VercelAPL implements APL {
if (invalidEnvKeys.length > 0) { if (invalidEnvKeys.length > 0) {
return { return {
ready: false, ready: false,
error: new VercelAplMisconfiguredError(invalidEnvKeys), error: new VercelAplNotReadyError(invalidEnvKeys),
}; };
} }
@ -154,4 +156,17 @@ export class VercelAPL implements APL {
ready: true, ready: true,
}; };
} }
async isConfigured(): Promise<AplConfiguredResult> {
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 "
),
};
}
} }

View file

@ -14,6 +14,9 @@ describe("create-app-register-handler", () => {
isReady: vi.fn().mockImplementation(async () => ({ isReady: vi.fn().mockImplementation(async () => ({
ready: true, ready: true,
})), })),
isConfigured: vi.fn().mockImplementation(async () => ({
configured: true,
})),
}; };
const { res, req } = createMocks({ const { res, req } = createMocks({

View file

@ -19,15 +19,15 @@ export const createAppRegisterHandler = ({ apl }: CreateAppRegisterHandlerOption
const authToken = request.params.auth_token; const authToken = request.params.auth_token;
const saleorDomain = request.headers[SALEOR_DOMAIN_HEADER] as string; 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( return new Response(
{ {
success: false, success: false,
error: { error: {
code: "APL_NOT_READY", code: "APL_NOT_CONFIGURED",
message: "App is not ready yet", message: "APL_NOT_CONFIGURED. App is configured properly. Check APL docs for help.",
}, },
}, },
{ {