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 12, 2024
1 parent 0a04749 commit a4fed20
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 47 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 { Container, Service } from 'typedi';
import { LambdaClient } from '@aws-sdk/client-lambda';
import * as AWSXRay from 'aws-xray-sdk';
import { S3Client } from '@aws-sdk/client-s3';
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.
}
}
}
14 changes: 2 additions & 12 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { certGen } from "./functions/certGen";
import { DependencyInjection } from "./config/DependencyInjection";

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 };
13 changes: 4 additions & 9 deletions src/services/CertificateGenerationService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Service } from 'typedi';
import { Service } from "typedi";
import { InvocationRequest, InvocationResponse, ServiceException } from "@aws-sdk/client-lambda";
import { GetObjectOutput } from "@aws-sdk/client-s3";
import { getProfile } from "@dvsa/cvs-feature-flags/profiles/vtx";
Expand Down Expand Up @@ -51,14 +51,9 @@ 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) {
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/services/CertificateUploadService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Service } from 'typedi';
import { Service } from "typedi";
import { PutObjectCommandOutput } from "@aws-sdk/client-s3";
import { IGeneratedCertificateResponse } from "../models";
import { S3BucketService } from "./S3BucketService";
Expand All @@ -8,10 +8,7 @@ import { S3BucketService } from "./S3BucketService";
*/
@Service()
class CertificateUploadService {
private readonly s3BucketService: S3BucketService;

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

/**
Expand Down
12 changes: 2 additions & 10 deletions src/services/LambdaService.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { Inject, Service } from 'typedi';
import { IInvokeConfig } from "../models";
import { Configuration } from "../utils/Configuration";
import { Service } from "typedi";
import { InvocationRequest, InvocationResponse, LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
import { HTTPError } from "../models/HTTPError";
import { ERRORS } from "../models/Enums";

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) {
}

/**
Expand Down
15 changes: 4 additions & 11 deletions src/services/S3BucketService.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { Inject, Service } from 'typedi';
import {Readable} from "stream";
import {Configuration} from "../utils/Configuration";
import {IS3Config} from "../models";
import AWSXRay from "aws-xray-sdk";
import {DeleteObjectCommand, DeleteObjectCommandOutput, GetObjectCommand, GetObjectCommandOutput, PutObjectCommand, PutObjectCommandOutput, S3Client} from "@aws-sdk/client-s3";
import { Service } from "typedi";
import { Readable } from "stream";
import { DeleteObjectCommand, DeleteObjectCommandOutput, GetObjectCommand, GetObjectCommandOutput, PutObjectCommand, PutObjectCommandOutput, S3Client } from "@aws-sdk/client-s3";

/**
* 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) {
}

/**
Expand Down

0 comments on commit a4fed20

Please sign in to comment.