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

View file

@ -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<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
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<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 () => ({
ready: true,
})),
isConfigured: vi.fn().mockImplementation(async () => ({
configured: true,
})),
};
const { res, req } = createMocks({

View file

@ -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.",
},
},
{