Skip to content

Commit

Permalink
Fix checking APL configuration state (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski authored Oct 12, 2022
1 parent d81e61d commit bb0786a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/APL/apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand All @@ -21,4 +30,5 @@ export interface APL {
* Inform that configuration is finished and correct
*/
isReady: () => Promise<AplReadyResult>;
isConfigured: () => Promise<AplConfiguredResult>;
}
13 changes: 12 additions & 1 deletion src/APL/file-apl.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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,
};
}
}
21 changes: 18 additions & 3 deletions src/APL/vercel-apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -146,12 +148,25 @@ export class VercelAPL implements APL {
if (invalidEnvKeys.length > 0) {
return {
ready: false,
error: new VercelAplMisconfiguredError(invalidEnvKeys),
error: new VercelAplNotReadyError(invalidEnvKeys),
};
}

return {
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 "
),
};
}
}
3 changes: 3 additions & 0 deletions src/handlers/next/create-app-register-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/next/create-app-register-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
},
{
Expand Down

0 comments on commit bb0786a

Please sign in to comment.