Skip to content

Commit 0747b7b

Browse files
authored
parse stage variable rather than using "as" (#3303)
1 parent 8301885 commit 0747b7b

File tree

19 files changed

+91
-89
lines changed

19 files changed

+91
-89
lines changed

handlers/discount-api/src/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { Router } from '@modules/routing/router';
44
import { withMMAIdentityCheck } from '@modules/routing/withMMAIdentityCheck';
55
import { withBodyParser } from '@modules/routing/withParsers';
66
import type { Stage } from '@modules/stage';
7+
import { stageFromEnvironment } from '@modules/stage';
78
import type {
89
ZuoraAccount,
910
ZuoraSubscription,
1011
} from '@modules/zuora/types/objects';
1112
import type { ZuoraClient } from '@modules/zuora/zuoraClient';
1213
import type { APIGatewayProxyResult, Handler } from 'aws-lambda';
1314
import dayjs from 'dayjs';
14-
import type { ZodType } from 'zod';
1515
import {
1616
applyDiscountEndpoint,
1717
previewDiscountEndpoint,
@@ -26,9 +26,9 @@ import {
2626
applyDiscountResponseSchema,
2727
previewDiscountResponseSchema,
2828
} from './responseSchema';
29+
import { stringify } from './stringify';
2930

30-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- todo fix in next refactor
31-
const stage = process.env.STAGE as Stage;
31+
const stage = stageFromEnvironment();
3232

3333
// main entry point from AWS
3434
export const handler: Handler = Router([
@@ -108,7 +108,3 @@ export function previewDiscountHandler(stage: Stage) {
108108
};
109109
};
110110
}
111-
112-
// this is a type safe version of stringify
113-
export const stringify = <T>(t: T, type: ZodType<T>): string =>
114-
JSON.stringify(type.parse(t));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { z } from 'zod';
2+
3+
// this is a type safe version of stringify
4+
export const stringify = <T>(t: T, type: z.ZodType<T>): string =>
5+
JSON.stringify(type.parse(t));

handlers/discount-api/test/stringify.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from 'zod';
2-
import { stringify } from '../src/index';
2+
import { stringify } from '../src/stringify';
33

44
test('stringify should not serialise any stray values in the object, for security reasons', () => {
55
const testSchema = z.object({

handlers/generate-product-catalog/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const writeProductCatalogToS3 = async (stage: Stage) => {
2929
'The generated product catalog did not pass validation in the current Zod schema: ',
3030
parseResult.error,
3131
);
32-
await putMetric(failedSchemaValidationMetricName);
32+
await putMetric(failedSchemaValidationMetricName, stage);
3333
} else {
3434
console.log(
3535
'The generated product catalog passed validation, writing to S3',

handlers/metric-push-api/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { putMetric } from '@modules/aws/cloudwatch';
22
import { Router } from '@modules/routing/router';
3+
import { stageFromEnvironment } from '@modules/stage';
34
import type { APIGatewayProxyEvent } from 'aws-lambda';
45

6+
const stage = stageFromEnvironment();
7+
58
const validReferers = [
69
'https://support.thegulocal.com/',
710
'https://support.code.dev-theguardian.com/',
@@ -22,7 +25,7 @@ export const handler = Router([
2225
const referer = event.headers.referer ?? event.headers.Referer;
2326

2427
if (referer && validReferers.includes(referer)) {
25-
await putMetric('metric-push-api-client-side-error');
28+
await putMetric('metric-push-api-client-side-error', stage);
2629
return Promise.resolve(buildResponse(201));
2730
} else {
2831
return Promise.resolve(buildResponse(204));

handlers/metric-push-api/test/index.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { putMetric } from '@modules/aws/cloudwatch';
22
import type { APIGatewayProxyEvent } from 'aws-lambda';
33
import { handler } from '../src/index';
44

5+
jest.mock('@modules/stage', () => ({
6+
stageFromEnvironment: jest.fn(() => 'CODE'),
7+
}));
58
jest.mock('@modules/aws/cloudwatch', () => ({
69
putMetric: jest.fn(),
710
}));
@@ -60,6 +63,7 @@ describe('handler', () => {
6063

6164
expect(putMetric).toHaveBeenCalledWith(
6265
'metric-push-api-client-side-error',
66+
'CODE',
6367
);
6468
});
6569

handlers/product-switch-api/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { Router } from '@modules/routing/router';
22
import { withMMAIdentityCheck } from '@modules/routing/withMMAIdentityCheck';
33
import { withParsers } from '@modules/routing/withParsers';
4-
import type { Stage } from '@modules/stage';
4+
import { stageFromEnvironment } from '@modules/stage';
55
import type { Handler } from 'aws-lambda';
66
import dayjs from 'dayjs';
77
import { z } from 'zod';
88
import { contributionToSupporterPlusEndpoint } from './productSwitchEndpoint';
99
import { productSwitchRequestSchema } from './schemas';
1010

11-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- todo fix in next refactor
12-
const stage = process.env.STAGE as Stage;
11+
const stage = stageFromEnvironment();
1312

1413
const pathParserSchema = z.object({
1514
subscriptionNumber: z

handlers/promotions-lambdas/src/lib/syncHandler.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AttributeValue as SDKAttributeValue } from '@aws-sdk/client-dynamo
22
import { unmarshall } from '@aws-sdk/util-dynamodb';
33
import { logger } from '@modules/routing/logger';
44
import type { Stage } from '@modules/stage';
5+
import { stageFromEnvironment } from '@modules/stage';
56
import type {
67
DynamoDBBatchResponse,
78
DynamoDBRecord,
@@ -65,10 +66,7 @@ export const createSyncHandler = <TSource, TTarget extends object>(
6566

6667
// build the handler
6768
return async (event: DynamoDBStreamEvent): Promise<DynamoDBBatchResponse> => {
68-
const stage = process.env.STAGE;
69-
if (!(stage === 'CODE' || stage === 'PROD')) {
70-
throw new Error('Invalid STAGE');
71-
}
69+
const stage = stageFromEnvironment();
7270

7371
const batchItemFailures: DynamoDBBatchResponse['batchItemFailures'] = [];
7472

handlers/salesforce-disaster-recovery-health-check/src/handlers/salesforceDisasterRecoveryHealthCheck.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getIfDefined } from '@modules/nullAndUndefined';
2+
import { stageFromEnvironment } from '@modules/stage';
23
import { publishSnsMessage } from '../services/sns';
34
import { describeExecution, startExecution } from '../services/step-functions';
45

@@ -10,10 +11,7 @@ export const handler = async (): Promise<
1011
'APP environment variable not set',
1112
);
1213

13-
const stage = getIfDefined<string>(
14-
process.env.STAGE,
15-
'STAGE environment variable not set',
16-
);
14+
const stage = stageFromEnvironment();
1715

1816
const region = getIfDefined<string>(
1917
process.env.REGION,

handlers/salesforce-disaster-recovery-health-check/test/handlers/salesforceDisasterRecoveryHealthCheck.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('Handler', () => {
6666
delete process.env.STAGE;
6767

6868
await expect(handler()).rejects.toThrow(
69-
'STAGE environment variable not set',
69+
'Stage environment variable is invalid',
7070
);
7171
});
7272
});

0 commit comments

Comments
 (0)