Skip to content

Commit

Permalink
Simplified constructors and xray configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
me-matt committed Sep 26, 2024
1 parent b05128e commit ca838a3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 48 deletions.
21 changes: 21 additions & 0 deletions src/config/DependencyInjection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LambdaClient } from '@aws-sdk/client-lambda';
import { S3Client } from '@aws-sdk/client-s3';
import * as AWSXRay from 'aws-xray-sdk';
import { Container, Service } from 'typedi';
import { IInvokeConfig } from '../models';
import { Configuration } from '../utils/Configuration';

@Service()
export class DependencyInjection {
public static register() {
const config: IInvokeConfig = Configuration.getInstance().getInvokeConfig();

Container.set(LambdaClient, AWSXRay.captureAWSv3Client(new LambdaClient(config.params)));
Container.set(S3Client, AWSXRay.captureAWSv3Client(new S3Client(config)));

const isOffline = (process.env.IS_OFFLINE ?? false) as boolean;
if (isOffline) {
// Needed for later.
}
}
}
13 changes: 2 additions & 11 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { DependencyInjection } from './config/DependencyInjection';
import { certGen } from './functions/certGen';

const isOffline: boolean = !process.env.BRANCH || process.env.BRANCH === 'local';
DependencyInjection.register();

let credentials = {};

if (isOffline) {
credentials = {
accessKeyId: 'accessKey1',
secretAccessKey: 'verySecretKey1',
};
}

export { credentials };
export { certGen as handler };
17 changes: 7 additions & 10 deletions src/services/CertificateGenerationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GetObjectOutput } from '@aws-sdk/client-s3';
import { getProfile } from '@dvsa/cvs-feature-flags/profiles/vtx';
import { toUint8Array } from '@smithy/util-utf8';
import moment from 'moment';
import { Inject, Service } from 'typedi';
import { Service } from 'typedi';
import {
ICertificatePayload,
ICustomDefect,
Expand Down Expand Up @@ -51,15 +51,12 @@ import { S3BucketService } from './S3BucketService';
*/
@Service()
class CertificateGenerationService {
private readonly s3Client: S3BucketService;
private readonly config: Configuration;
private readonly lambdaClient: LambdaService;

constructor(@Inject() s3Client: S3BucketService, @Inject() lambdaClient: LambdaService) {
this.s3Client = s3Client;
this.config = Configuration.getInstance();
this.lambdaClient = lambdaClient;
}
private readonly config: Configuration = Configuration.getInstance();

constructor(
private s3Client: S3BucketService,
private lambdaClient: LambdaService
) {}

/**
* Generates MOT certificate for a given test result
Expand Down
8 changes: 2 additions & 6 deletions src/services/CertificateUploadService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PutObjectCommandOutput } from '@aws-sdk/client-s3';
import { Inject, Service } from 'typedi';
import { Service } from 'typedi';
import { IGeneratedCertificateResponse } from '../models';
import { S3BucketService } from './S3BucketService';

Expand All @@ -8,11 +8,7 @@ import { S3BucketService } from './S3BucketService';
*/
@Service()
class CertificateUploadService {
private readonly s3BucketService: S3BucketService;

constructor(@Inject() s3BucketService: S3BucketService) {
this.s3BucketService = s3BucketService;
}
constructor(private s3BucketService: S3BucketService) {}

/**
* Uploads a generated certificate to S3 bucket
Expand Down
13 changes: 2 additions & 11 deletions src/services/LambdaService.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import { InvocationRequest, InvocationResponse, InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda';
import { Inject, Service } from 'typedi';
import { IInvokeConfig } from '../models';
import { Service } from 'typedi';
import { ERRORS } from '../models/Enums';
import { HTTPError } from '../models/HTTPError';
import { Configuration } from '../utils/Configuration';

import AWSXRay from 'aws-xray-sdk';

/**
* Service class for invoking external lambda functions
*/
@Service()
class LambdaService {
public readonly lambdaClient: LambdaClient;

constructor(@Inject() lambdaClient: LambdaClient) {
const config: IInvokeConfig = Configuration.getInstance().getInvokeConfig();
this.lambdaClient = AWSXRay.captureAWSv3Client(new LambdaClient({ ...lambdaClient, ...config.params }));
}
constructor(private lambdaClient: LambdaClient) {}

/**
* Invokes a lambda function based on the given parameters
Expand Down
12 changes: 2 additions & 10 deletions src/services/S3BucketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ import {
PutObjectCommandOutput,
S3Client,
} from '@aws-sdk/client-s3';
import AWSXRay from 'aws-xray-sdk';
import { Inject, Service } from 'typedi';
import { IS3Config } from '../models';
import { Configuration } from '../utils/Configuration';
import { Service } from 'typedi';

/**
* Service class for communicating with Simple Storage Service
*/
@Service()
class S3BucketService {
public readonly s3Client: S3Client;

constructor(@Inject() s3Client: S3Client) {
const config: IS3Config = Configuration.getInstance().getS3Config();
this.s3Client = AWSXRay.captureAWSv3Client(new S3Client({ ...s3Client, ...config }));
}
constructor(private s3Client: S3Client) {}

/**
* Uploads a file to an S3 bucket
Expand Down

0 comments on commit ca838a3

Please sign in to comment.