From 19f4621c30b8a0848c5451c75ea57eb97164ae7f Mon Sep 17 00:00:00 2001 From: Harold Hunt Date: Thu, 25 Apr 2024 19:27:49 -0400 Subject: [PATCH] Fix lint warnings (#367) --- packages/demo-app/src/index.ts | 3 -- .../src/controllers/version/DeleteVersion.ts | 16 +++++----- .../src/controllers/version/DeployVersion.ts | 32 ++++++++++++------- .../controllers/version/GetVersion.spec.ts | 8 +---- .../src/controllers/version/LambdaAlias.ts | 29 ++++++++++------- packages/microapps-deployer/src/index.ts | 11 ++++--- .../src/index.route.spec.ts | 14 +------- .../microapps-edge-to-origin/src/index.ts | 3 -- .../src/get-app-info.spec.ts | 2 +- .../microapps-router-lib/src/get-route.ts | 25 ++++++++++++--- packages/microapps-router/src/index.ts | 10 ++++-- 11 files changed, 83 insertions(+), 70 deletions(-) diff --git a/packages/demo-app/src/index.ts b/packages/demo-app/src/index.ts index 700a68c6..9c24da61 100644 --- a/packages/demo-app/src/index.ts +++ b/packages/demo-app/src/index.ts @@ -5,9 +5,6 @@ import { readFileSync } from 'fs'; const html = readFileSync(`./index.html`, 'utf8'); const file = readFileSync(`./file.html`, 'utf8'); -const buildTrigger = '2023-01-24-01'; -console.info('Demo-app build trigger', { buildTrigger }); - // eslint-disable-next-line @typescript-eslint/require-await export async function handler( // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/packages/microapps-deployer/src/controllers/version/DeleteVersion.ts b/packages/microapps-deployer/src/controllers/version/DeleteVersion.ts index 4b541406..b7a76c42 100644 --- a/packages/microapps-deployer/src/controllers/version/DeleteVersion.ts +++ b/packages/microapps-deployer/src/controllers/version/DeleteVersion.ts @@ -80,8 +80,8 @@ export async function DeleteVersion(opts: { RouteId: routeId, }), ); - } catch (err: any) { - if (err.name === 'AccessDeniedException') { + } catch (err) { + if (err instanceof Error && err.name === 'AccessDeniedException') { Log.Instance.error('AccessDeniedException removing route from API Gateway', { error: err, apiId, @@ -110,8 +110,8 @@ export async function DeleteVersion(opts: { IntegrationId: record.IntegrationID, }), ); - } catch (error: any) { - if (error.name === 'AccessDeniedException') { + } catch (error) { + if (error instanceof Error && error.name === 'AccessDeniedException') { Log.Instance.error('AccessDeniedException removing integration from API Gateway', { error, apiId, @@ -208,17 +208,17 @@ export async function DeleteVersion(opts: { Qualifier: aliasInfo.FunctionVersion, }), ); - } catch (error: any) { - if (error.name !== 'ResourceConflictException') { + } catch (error) { + if (!(error instanceof Error && error.name === 'ResourceConflictException')) { throw error; } Log.Instance.info('Version is still in use by another alias, not deleting'); } } - } catch (error: any) { + } catch (error) { // It's ok if the Alias or Version is already gone - if (error.name !== 'ResourceNotFoundException') { + if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) { throw error; } } diff --git a/packages/microapps-deployer/src/controllers/version/DeployVersion.ts b/packages/microapps-deployer/src/controllers/version/DeployVersion.ts index 8be9c4b0..ca4df05d 100644 --- a/packages/microapps-deployer/src/controllers/version/DeployVersion.ts +++ b/packages/microapps-deployer/src/controllers/version/DeployVersion.ts @@ -152,8 +152,8 @@ export async function DeployVersion(opts: { } } } - } catch (error: any) { - if (error.name !== 'ResourceNotFoundException') { + } catch (error) { + if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) { throw error; } } @@ -207,8 +207,8 @@ export async function DeployVersion(opts: { ); integrationId = integration.IntegrationId as string; - } catch (error: any) { - if (error.name === 'AccessDeniedException') { + } catch (error) { + if (error instanceof Error && error.name === 'AccessDeniedException') { Log.Instance.error('AccessDeniedException adding integration to API Gateway', { error, }); @@ -245,8 +245,8 @@ export async function DeployVersion(opts: { ); Log.Instance.info('created RouteIDAppVersion', { result }); record.RouteIDAppVersion = `${result.RouteId}`; - } catch (err: any) { - if (err.name === 'AccessDeniedException') { + } catch (err) { + if (err instanceof Error && err.name === 'AccessDeniedException') { Log.Instance.error('AccessDeniedException adding route to API Gateway', { error: err, }); @@ -255,7 +255,11 @@ export async function DeployVersion(opts: { // Don't care Log.Instance.error('Caught unexpected error on app/ver route add'); - Log.Instance.error(err); + if (typeof err === 'string' || err instanceof Error) { + Log.Instance.error(err); + } else { + Log.Instance.error('An unknown error occurred'); + } } } // Add the route to API Gateway for appName/version/{proxy+} @@ -279,8 +283,8 @@ export async function DeployVersion(opts: { ); Log.Instance.info('created RouteIDAppVersionSplat', { result }); record.RouteIDAppVersionSplat = `${result.RouteId}`; - } catch (err: any) { - if (err.name === 'AccessDeniedException') { + } catch (err) { + if (err instanceof Error && err.name === 'AccessDeniedException') { Log.Instance.error('AccessDeniedException adding route to API Gateway', { error: err, }); @@ -289,7 +293,11 @@ export async function DeployVersion(opts: { // Don't care Log.Instance.error('Caught unexpected error on {proxy+} route add'); - Log.Instance.error(err); + if (typeof err === 'string' || err instanceof Error) { + Log.Instance.error(err); + } else { + Log.Instance.error('An unknown error occurred'); + } } } @@ -345,8 +353,8 @@ export async function DeployVersion(opts: { if (functionUrl.FunctionUrl) { url = functionUrl.FunctionUrl; } - } catch (error: any) { - if (error.name !== 'ResourceNotFoundException') { + } catch (error) { + if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) { throw error; } } diff --git a/packages/microapps-deployer/src/controllers/version/GetVersion.spec.ts b/packages/microapps-deployer/src/controllers/version/GetVersion.spec.ts index 3213c19d..d4957f11 100644 --- a/packages/microapps-deployer/src/controllers/version/GetVersion.spec.ts +++ b/packages/microapps-deployer/src/controllers/version/GetVersion.spec.ts @@ -21,7 +21,7 @@ const theConfig: Writeable = { rootPathPrefix: 'dev', requireIAMAuthorization: true, parentDeployerLambdaARN: '', - edgeToOriginRoleARN: '', + edgeToOriginRoleARN: [], }; const origConfig = { ...theConfig }; Object.defineProperty(Config, 'instance', { @@ -31,10 +31,7 @@ Object.defineProperty(Config, 'instance', { return theConfig; }), }); -import * as apigwy from '@aws-sdk/client-apigatewayv2'; import * as dynamodb from '@aws-sdk/client-dynamodb'; -import * as lambda from '@aws-sdk/client-lambda'; -import * as s3 from '@aws-sdk/client-s3'; import type { ICreateApplicationRequest, @@ -43,7 +40,6 @@ import type { } from '@pwrdrvr/microapps-deployer-lib'; import { DBManager, Version } from '@pwrdrvr/microapps-datalib'; import type * as lambdaTypes from 'aws-lambda'; -import { mockClient, AwsClientStub } from 'aws-sdk-client-mock'; import sinon from 'sinon'; import { handler, overrideDBManager } from '../../index'; @@ -53,9 +49,7 @@ let dbManager: DBManager; const TEST_TABLE_NAME = 'microapps'; describe('GetVersion', () => { - const config = Config.instance; let sandbox: sinon.SinonSandbox; - const pathPrefix = `${config.rootPathPrefix}/`; beforeAll(() => { dynamoClient = new dynamodb.DynamoDBClient({ diff --git a/packages/microapps-deployer/src/controllers/version/LambdaAlias.ts b/packages/microapps-deployer/src/controllers/version/LambdaAlias.ts index 37ca3e6e..d3ad4de6 100644 --- a/packages/microapps-deployer/src/controllers/version/LambdaAlias.ts +++ b/packages/microapps-deployer/src/controllers/version/LambdaAlias.ts @@ -148,13 +148,20 @@ export async function LambdaAlias(opts: { errorMessage: `Lambda version not found: ${lambdaVersion}, for lambda: ${lambdaARN}`, }; } - } catch (error: any) { - Log.Instance.error('Error getting Lambda version', error); - - if (error.name === 'ResourceNotFoundException') { + } catch (error) { + if (error instanceof Error && error.name === 'ResourceNotFoundException') { return { statusCode: 404, errorMessage: error.message }; } else { - return { statusCode: 500, errorMessage: error.message }; + if (error instanceof Error) { + Log.Instance.error('Error getting Lambda version', error); + return { statusCode: 500, errorMessage: error.message }; + } else if (typeof error === 'string') { + Log.Instance.error(`Error getting Lambda version: ${error}`); + return { statusCode: 500, errorMessage: error }; + } else { + Log.Instance.error('Error getting Lambda version: unknown error'); + return { statusCode: 500, errorMessage: 'An unknown error occurred' }; + } } } @@ -230,8 +237,8 @@ async function AddOrUpdateFunctionUrl({ if (functionUrl.FunctionUrl) { url = functionUrl.FunctionUrl; } - } catch (error: any) { - if (error.name !== 'ResourceNotFoundException') { + } catch (error) { + if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) { throw error; } } @@ -321,8 +328,8 @@ async function CreateOrUpdateLambdaAlias(opts: { lambdaAliasARN: resultLambdaAlias.AliasArn, actionTaken: 'updated', }; - } catch (error: any) { - if (error.name !== 'ResourceNotFoundException') { + } catch (error) { + if (!(error instanceof Error && error.name == 'ResourceNotFoundException')) { throw error; } @@ -453,8 +460,8 @@ async function AddCrossAccountPermissionsToAlias({ if (existingPolicy.Policy) { policyDoc = JSON.parse(existingPolicy.Policy) as IPolicyDocument; } - } catch (error: any) { - if (error.name !== 'ResourceNotFoundException') { + } catch (error) { + if (!(error instanceof Error && error.name === 'ResourceNotFoundException')) { throw error; } diff --git a/packages/microapps-deployer/src/index.ts b/packages/microapps-deployer/src/index.ts index d32c6fca..9333e7b6 100644 --- a/packages/microapps-deployer/src/index.ts +++ b/packages/microapps-deployer/src/index.ts @@ -37,9 +37,6 @@ let dynamoClient = new DynamoDBClient({ maxAttempts: 8, }); -const buildTrigger = '2023-01-31-01'; -Log.Instance.info('Deployer build trigger', { buildTrigger }); - export function overrideDBManager(opts: { dbManager: DBManager; dynamoClient: DynamoDBClient; @@ -167,9 +164,13 @@ export async function handler( default: return { statusCode: 400 }; } - } catch (err: any) { + } catch (err) { Log.Instance.error('Caught unexpected exception in handler'); - Log.Instance.error(err); + if (typeof err === 'string' || err instanceof Error) { + Log.Instance.error(err); + } else { + Log.Instance.error('An unknown error occurred'); + } return { statusCode: 500 }; } } diff --git a/packages/microapps-edge-to-origin/src/index.route.spec.ts b/packages/microapps-edge-to-origin/src/index.route.spec.ts index a3bb56b4..4ac82a31 100644 --- a/packages/microapps-edge-to-origin/src/index.route.spec.ts +++ b/packages/microapps-edge-to-origin/src/index.route.spec.ts @@ -492,7 +492,6 @@ describe('edge-to-origin - routing - without prefix', () => { SemVer2: '1.2.1-beta.2', Locales: [], RawPath: '/_next/data/1.2.1-beta.2/batdirectnobasenextdata/route.json', - SuffixPath: 'batdirectnobasenextdata/route.json', }, { AppName: 'BatDirectNoBaseNextDataRootRoute', @@ -502,7 +501,6 @@ describe('edge-to-origin - routing - without prefix', () => { SemVer2: '1.2.1-beta.4', Locales: [], RawPath: '/_next/data/1.2.1-beta.4/batdirectnobasenextdatarootroute.json', - SuffixPath: 'batdirectnobasenextdatarootroute.json', }, { AppName: 'BatDirectNoBaseNextDataRootRouteLocale', @@ -512,22 +510,12 @@ describe('edge-to-origin - routing - without prefix', () => { SemVer2: '1.2.1-beta.4', Locales: ['en', 'sv'], RawPath: '/_next/data/1.2.1-beta.4/sv/batdirectnobasenextdatarootroutelocale.json', - SuffixPath: 'batdirectnobasenextdatarootroutelocale.json', }, ]; it.each(testCases)( 'should route `direct` /_next/data/[$SemVer2]/[$AppName] request to [$AppName] when it exists but $SemVer1 is the default', - async ({ - AppName, - LambdaURL1, - SemVer1, - LambdaURL2, - SemVer2, - RawPath, - Locales, - SuffixPath, - }) => { + async ({ AppName, LambdaURL1, SemVer1, LambdaURL2, SemVer2, RawPath, Locales }) => { theConfig.replaceHostHeader = true; theConfig.locales = Locales; diff --git a/packages/microapps-edge-to-origin/src/index.ts b/packages/microapps-edge-to-origin/src/index.ts index dbb1ab96..047e6b7e 100644 --- a/packages/microapps-edge-to-origin/src/index.ts +++ b/packages/microapps-edge-to-origin/src/index.ts @@ -20,9 +20,6 @@ const appFrame = loadAppFrame({ basePath: __dirname }); log.info('loaded config', { config }); -const buildTrigger = '2023-02-01-01'; -log.info('Edge-to-origin build trigger', { buildTrigger }); - let dbManager: DBManager | undefined; let dynamoClient = new DynamoDBClient({ maxAttempts: 8, diff --git a/packages/microapps-router-lib/src/get-app-info.spec.ts b/packages/microapps-router-lib/src/get-app-info.spec.ts index a3298db5..d51f756b 100644 --- a/packages/microapps-router-lib/src/get-app-info.spec.ts +++ b/packages/microapps-router-lib/src/get-app-info.spec.ts @@ -57,7 +57,7 @@ describe('GetAppInfo', () => { expected: '[root]', }, ]; - + // eslint-disable-next-line @typescript-eslint/no-unused-vars it.each(testCases)('$caseName', async ({ caseName, appName, mockRules, expected }) => { getAppVersionCacheSpy.mockImplementation(() => { return { diff --git a/packages/microapps-router-lib/src/get-route.ts b/packages/microapps-router-lib/src/get-route.ts index 51ce046f..6a7dff15 100644 --- a/packages/microapps-router-lib/src/get-route.ts +++ b/packages/microapps-router-lib/src/get-route.ts @@ -257,11 +257,26 @@ export async function GetRoute(event: IGetRouteEvent): Promise statusCode: 599, errorMessage: `Router - Could not route: ${event.rawPath}, no matching route`, }; - } catch (error: any) { + } catch (error) { log.error('unexpected exception - returning 599', { statusCode: 599, error }); - return { - statusCode: 599, - errorMessage: `Router - Could not route: ${event.rawPath}, ${error.message}`, - }; + if (error instanceof Error) { + log.error('unexpected exception - returning 599', { statusCode: 599, error }); + return { + statusCode: 599, + errorMessage: `Router - Could not route: ${event.rawPath}, ${error.message}`, + }; + } else if (typeof error === 'string') { + log.error('unexpected exception - returning 599', { statusCode: 599, error }); + return { + statusCode: 599, + errorMessage: `Router - Could not route: ${event.rawPath}, ${error}`, + }; + } else { + log.error('unexpected exception - returning 599', { statusCode: 599, error }); + return { + statusCode: 599, + errorMessage: `Router - Could not route: ${event.rawPath}, unknown error`, + }; + } } } diff --git a/packages/microapps-router/src/index.ts b/packages/microapps-router/src/index.ts index 57cd6a63..a91cbb00 100644 --- a/packages/microapps-router/src/index.ts +++ b/packages/microapps-router/src/index.ts @@ -104,12 +104,18 @@ export async function handler( } else { throw new Error('Unmatched route'); } - } catch (error: any) { + } catch (error) { log.error('unexpected exception - returning 599', { statusCode: 599, error }); response.statusCode = 599; response.headers = {}; response.headers['Content-Type'] = 'text/plain'; - response.body = `Router - Could not route: ${event.rawPath}, ${error.message}`; + if (error instanceof Error) { + response.body = `Router - Could not route: ${event.rawPath}, ${error.message}`; + } else if (typeof error === 'string') { + response.body = `Router - Could not route: ${event.rawPath}, ${error}`; + } else { + response.body = `Router - Could not route: ${event.rawPath}, unknown error`; + } } return response;