Skip to content

Commit

Permalink
refactor: env-helpers (#249)
Browse files Browse the repository at this point in the history
* refactor: env-helpers

* fix: imports
  • Loading branch information
mattzcarey authored Sep 6, 2023
1 parent 4ee9a5c commit 98b0d6a
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 145 deletions.
12 changes: 7 additions & 5 deletions services/core/functions/demo-review-lambda/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import {
LANGCHAIN_API_KEY_PARAM_NAME,
OPENAI_API_KEY_PARAM_NAME,
} from "../../constants";
import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import { reviewLambdaEnvironment } from "../helpers/reviewLambdaEnvironment";
import {
buildResourceName,
commonLambdaEnvironment,
commonLambdaProps,
} from "../../helpers";
import { reviewLambdaEnvironment } from "../utils/reviewLambdaEnvironment";

export type DemoReviewLambdaProps = {
table: Table;
bucket: Bucket;
}
};

export class DemoReviewLambda extends NodejsFunction {
constructor(scope: Construct, id: string, props: DemoReviewLambdaProps) {
Expand Down
6 changes: 3 additions & 3 deletions services/core/functions/demo-review-lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { S3Client } from "@aws-sdk/client-s3";
import { APIGatewayProxyEvent } from "aws-lambda";
import { v4 as uuidv4 } from "uuid";

import { saveInputAndResponseToS3 } from "./saveInputAndResponseToS3";
import { getMaxPromptLength } from "../../../../src/common/model/getMaxPromptLength";
import { logger } from "../../../../src/common/utils/logger";
import { askAI } from "../../../../src/review/llm/askAI";
import { demoPrompt } from "../../../../src/review/prompt/prompts";
import { ReviewDemoCounterEntity } from "../../entities";
import { getEnvVariable, getVariableFromSSM } from "../helpers/getVariable";
import { getEnvVariable, getVariableFromSSM } from "../utils/getVariable";
import { saveInputAndResponseToS3 } from "./saveInputAndResponseToS3";

const DEFAULT_DEMO_MODEL = "gpt-3.5-turbo";

Expand All @@ -27,7 +27,7 @@ type DemoReviewLambdaResponse = {

type ReviewLambdaInput = {
code: string;
}
};

const isReviewLambdaInput = (input: unknown): input is ReviewLambdaInput =>
typeof input === "object" && input !== null && "code" in input;
Expand Down
10 changes: 6 additions & 4 deletions services/core/functions/get-user/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";
import { join } from "path";

import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import {
buildResourceName,
commonLambdaEnvironment,
commonLambdaProps,
} from "../../helpers";

type GetUserLambdaProps = {
table: Table;
}
};

export class GetUserLambda extends NodejsFunction {
constructor(scope: Construct, id: string, props: GetUserLambdaProps) {
Expand Down
6 changes: 0 additions & 6 deletions services/core/functions/helpers/commonLambdaEnvironment.ts

This file was deleted.

14 changes: 0 additions & 14 deletions services/core/functions/helpers/commonLambdaProps.ts

This file was deleted.

17 changes: 0 additions & 17 deletions services/core/functions/helpers/sharedCdkEsbuildconfig.ts

This file was deleted.

6 changes: 3 additions & 3 deletions services/core/functions/review-lambda/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { subtle } from "crypto";

import { getVariableFromSSM } from "../helpers/getVariable";
import { getVariableFromSSM } from "../utils/getVariable";

export const authenticate = async (
header: string,
Expand Down Expand Up @@ -30,7 +30,7 @@ export const authenticate = async (
return equal;
};

function hexToBytes(hex: string) {
const hexToBytes = (hex: string) => {
const len = hex.length / 2;
const bytes = new Uint8Array(len);

Expand All @@ -43,4 +43,4 @@ function hexToBytes(hex: string) {
}

return bytes;
}
};
10 changes: 6 additions & 4 deletions services/core/functions/review-lambda/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
LANGCHAIN_API_KEY_PARAM_NAME,
OPENAI_API_KEY_PARAM_NAME,
} from "../../constants";
import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import { reviewLambdaEnvironment } from "../helpers/reviewLambdaEnvironment";
import {
buildResourceName,
commonLambdaEnvironment,
commonLambdaProps,
} from "../../helpers";
import { reviewLambdaEnvironment } from "../utils/reviewLambdaEnvironment";

export class ReviewLambda extends NodejsFunction {
constructor(scope: Construct, id: string) {
Expand Down
2 changes: 1 addition & 1 deletion services/core/functions/review-lambda/decryptKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecryptCommand, KMSClient } from "@aws-sdk/client-kms";

import { getEnvVariable } from "../helpers/getVariable";
import { getEnvVariable } from "../utils/getVariable";

const kmsClient = new KMSClient({});

Expand Down
4 changes: 2 additions & 2 deletions services/core/functions/review-lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { ReviewArgs, ReviewFile } from "../../../../src/common/types";
import { logger } from "../../../../src/common/utils/logger";
import { review } from "../../../../src/review/index";
import { GITHUB_SIGNATURE_HEADER_KEY } from "../../constants";
import { getVariableFromSSM } from "../helpers/getVariable";
import { getVariableFromSSM } from "../utils/getVariable";

type ReviewLambdasBody = {
args: ReviewArgs;
files: ReviewFile[];
}
};

logger.settings.minLevel = 4;

Expand Down
10 changes: 6 additions & 4 deletions services/core/functions/update-user/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";
import { join } from "path";

import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import {
buildResourceName,
commonLambdaEnvironment,
commonLambdaProps,
} from "../../helpers";

type UpdateUserLambdaProps = {
table: Table;
kmsKey: Key;
}
};

export class UpdateUserLambda extends NodejsFunction {
constructor(scope: Construct, id: string, props: UpdateUserLambdaProps) {
Expand Down
2 changes: 1 addition & 1 deletion services/core/functions/update-user/encryptKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EncryptCommand, KMSClient } from "@aws-sdk/client-kms";

import { getEnvVariable } from "../helpers/getVariable";
import { getEnvVariable } from "../utils/getVariable";

const kmsClient = new KMSClient({});

Expand Down
2 changes: 1 addition & 1 deletion services/core/helpers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StringParameter } from "aws-cdk-lib/aws-ssm";
import { Construct } from "constructs";

import { getStage } from "./env-helpers";
import { isDev } from "./environment";
import { isDev } from "./env-helpers/environment";

export const getCertificateArn = (
scope: Construct,
Expand Down
77 changes: 0 additions & 77 deletions services/core/helpers/env-helpers.ts

This file was deleted.

4 changes: 4 additions & 0 deletions services/core/helpers/env-helpers/buildResourceName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getStage } from "./getStage";

export const buildResourceName = (resourceName: string): string =>
`${getStage()}-${resourceName}`;
3 changes: 3 additions & 0 deletions services/core/helpers/env-helpers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const defaultStage = "dev";
export const defaultRegion = "eu-west-2";
export const projectName = "crgpt";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getStage } from "./env-helpers";
import { getStage } from "./getStage";

export const isProduction = (): boolean => {
const stage = getStage();
Expand Down
47 changes: 47 additions & 0 deletions services/core/helpers/env-helpers/getArg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export type GetArgProps = {
cliArg: string;
processEnvName: string;
defaultValue?: string;
};

export const getArg = ({
cliArg,
processEnvName,
defaultValue,
}: GetArgProps): string => {
const isNonEmptyString = (arg: unknown): arg is string =>
typeof arg === "string" && arg !== "";

const getCdkContext = (): Record<string, unknown> | undefined => {
const cdkContextEnv = process.env.CDK_CONTEXT_JSON;

return cdkContextEnv != null
? (JSON.parse(cdkContextEnv) as Record<string, unknown>)
: undefined;
};

const findCliArg = (key: string): string | undefined => {
const index = process.argv.findIndex((arg) => arg === `--${key}`);

return index !== -1 && index + 1 < process.argv.length
? process.argv[index + 1]
: undefined;
};

const argSources = [
findCliArg(cliArg),
getCdkContext()?.[cliArg] as string | undefined,
process.env[processEnvName],
defaultValue,
];

for (const arg of argSources) {
if (isNonEmptyString(arg)) {
return arg;
}
}

throw new Error(
`--${cliArg} CLI argument or ${processEnvName} env var required.`
);
};
9 changes: 9 additions & 0 deletions services/core/helpers/env-helpers/getRegion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defaultRegion } from "./config";
import { getArg } from "./getArg";

export const getRegion = (): string =>
getArg({
cliArg: "region",
processEnvName: "REGION",
defaultValue: defaultRegion,
});
10 changes: 10 additions & 0 deletions services/core/helpers/env-helpers/getStage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defaultStage } from "./config";
import { getArg } from "./getArg";

export const getStage = (): string => {
return getArg({
cliArg: "stage",
processEnvName: "STAGE",
defaultValue: defaultStage,
});
};
6 changes: 6 additions & 0 deletions services/core/helpers/env-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./buildResourceName";
export * from "./config";
export * from "./environment";
export * from "./getArg";
export * from "./getRegion";
export * from "./getStage";
3 changes: 2 additions & 1 deletion services/core/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./certificates";
export * from "./domains";
export * from "./env-helpers";
export * from "./env-helpers/environment";
export * from "./format-response";
export * from "./environment";
export * from "./lambda";
Loading

0 comments on commit 98b0d6a

Please sign in to comment.