Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add explicit return type for most functions #138

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions serverless/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export function getConfigFromCfnParams(params: CfnParams) {
};
}

export function validateParameters(config: Configuration) {
export function validateParameters(config: Configuration): string[] {
log.debug("Validating parameters...");
const errors: string[] = [];

Expand Down Expand Up @@ -278,7 +278,7 @@ export function validateParameters(config: Configuration) {
return errors;
}

export function checkForMultipleApiKeys(config: Configuration) {
export function checkForMultipleApiKeys(config: Configuration): string | undefined {
let multipleApiKeysMessage;
if (config.apiKey !== undefined && config.apiKMSKey !== undefined && config.apiKeySecretArn !== undefined) {
multipleApiKeysMessage = "`apiKey`, `apiKMSKey`, and `apiKeySecretArn`";
Expand All @@ -293,7 +293,7 @@ export function checkForMultipleApiKeys(config: Configuration) {
return multipleApiKeysMessage;
}

export function setEnvConfiguration(config: Configuration, lambdas: LambdaFunction[]) {
export function setEnvConfiguration(config: Configuration, lambdas: LambdaFunction[]): void {
lambdas.forEach((lambda) => {
const environment = lambda.properties.Environment ?? {};
const envVariables = environment.Variables ?? {};
Expand Down
32 changes: 23 additions & 9 deletions serverless/src/forwarder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function addCloudWatchForwarderSubscriptions(
stackName: string | undefined,
forwarderArn: string,
cloudWatchLogs: CloudWatchLogs,
) {
): Promise<void> {
let logGroupsOnStack: CloudWatchLogs.LogGroups | undefined;
let logGroupsInTemplate: LogGroupDefinition[] | undefined;
let subscriptionsInTemplate: SubscriptionDefinition[] | undefined;
Expand Down Expand Up @@ -203,7 +203,10 @@ export async function addCloudWatchForwarderSubscriptions(
}
}

export async function findExistingLogGroupWithFunctionName(cloudWatchLogs: CloudWatchLogs, functionName: string) {
export async function findExistingLogGroupWithFunctionName(
cloudWatchLogs: CloudWatchLogs,
functionName: string,
): Promise<any> {
const logGroupName = `${LAMBDA_LOG_GROUP_PREFIX}${functionName}`;
const args = {
logGroupNamePrefix: logGroupName,
Expand All @@ -216,7 +219,10 @@ export async function findExistingLogGroupWithFunctionName(cloudWatchLogs: Cloud
return logGroups.find((lg) => lg.logGroupName === logGroupName);
}

export async function getExistingLambdaLogGroupsOnStack(cloudWatchLogs: CloudWatchLogs, stackName: string) {
export async function getExistingLambdaLogGroupsOnStack(
cloudWatchLogs: CloudWatchLogs,
stackName: string,
): Promise<any> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Unexpected any. Specify a different type. (...read more)

Do not use the any type, as it is too broad and can lead to unexpected behavior.

View in Datadog  Leave us feedback  Documentation

const logGroupNamePrefix = `${LAMBDA_LOG_GROUP_PREFIX}${stackName}-`;
const args = { logGroupNamePrefix };
const response = await cloudWatchLogs.describeLogGroups(args).promise();
Expand All @@ -225,7 +231,7 @@ export async function getExistingLambdaLogGroupsOnStack(cloudWatchLogs: CloudWat
return logGroups ?? [];
}

export async function shouldSubscribeLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string) {
export async function shouldSubscribeLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string): Promise<boolean> {
const subscriptionFilters = await describeSubscriptionFilters(cloudWatchLogs, logGroupName);
const numberOfActiveSubscriptionFilters = subscriptionFilters.length;
if (numberOfActiveSubscriptionFilters >= MAX_ALLOWABLE_LOG_GROUP_SUBSCRIPTIONS) {
Expand All @@ -243,7 +249,7 @@ export async function shouldSubscribeLogGroup(cloudWatchLogs: CloudWatchLogs, lo
return true;
}

async function describeSubscriptionFilters(cloudWatchLogs: CloudWatchLogs, logGroupName: string) {
async function describeSubscriptionFilters(cloudWatchLogs: CloudWatchLogs, logGroupName: string): Promise<any> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Unexpected any. Specify a different type. (...read more)

Do not use the any type, as it is too broad and can lead to unexpected behavior.

View in Datadog  Leave us feedback  Documentation

const request = { logGroupName };
const response = await cloudWatchLogs.describeSubscriptionFilters(request).promise();
return response.subscriptionFilters ?? [];
Expand All @@ -257,7 +263,11 @@ function findLogGroupsInTemplate(resources: Resources) {
});
}

export function findDeclaredLogGroup(logGroups: LogGroupDefinition[], functionKey: string, functionName?: string) {
export function findDeclaredLogGroup(
logGroups: LogGroupDefinition[],
functionKey: string,
functionName?: string,
): LogGroupDefinition | undefined {
for (const resource of Object.values(logGroups)) {
const logGroupName = resource.logGroupResource.Properties.LogGroupName;

Expand Down Expand Up @@ -298,7 +308,7 @@ function findDeclaredSub(
subscriptions: SubscriptionDefinition[],
logGroupName: string | { [fn: string]: any },
logGroupKey: string,
) {
): SubscriptionDefinition | undefined {
for (const subscription of subscriptions) {
const subscribedLogGroupName = subscription.subscriptionResource.Properties.LogGroupName;
if (typeof subscribedLogGroupName === "string" && subscribedLogGroupName.includes(logGroupKey)) {
Expand All @@ -310,7 +320,11 @@ function findDeclaredSub(
}
}

async function putSubscriptionFilter(cloudWatchLogs: CloudWatchLogs, forwarderArn: string, logGroupName: string) {
async function putSubscriptionFilter(
cloudWatchLogs: CloudWatchLogs,
forwarderArn: string,
logGroupName: string,
): Promise<void> {
const args = {
destinationArn: forwarderArn,
filterName: SUBSCRIPTION_FILTER_NAME,
Expand All @@ -320,7 +334,7 @@ async function putSubscriptionFilter(cloudWatchLogs: CloudWatchLogs, forwarderAr
await cloudWatchLogs.putSubscriptionFilter(args).promise();
}

async function createLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string) {
async function createLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string): Promise<void> {
const args = { logGroupName };
await cloudWatchLogs.createLogGroup(args).promise();
}
2 changes: 1 addition & 1 deletion serverless/src/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const filterSensitiveInfoFromRepository = (repositoryUrl: string | undefined) => {
export const filterSensitiveInfoFromRepository = (repositoryUrl: string | undefined): string | undefined => {
try {
if (!repositoryUrl) {
return repositoryUrl;
Expand Down
4 changes: 2 additions & 2 deletions serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const handler = async (event: InputEvent, _: any) => {
* the 'FunctionName' property. In this case, it means at least one of the names
* will be dynamically generated.
*/
function lambdaHasDynamicallyGeneratedName(lambdas: LambdaFunction[]) {
function lambdaHasDynamicallyGeneratedName(lambdas: LambdaFunction[]): LambdaFunction[] {
const dynmicallyNamedLambdas: LambdaFunction[] = [];
for (const lambda of lambdas) {
if (lambda.properties.FunctionName === undefined) {
Expand All @@ -223,7 +223,7 @@ function lambdaHasDynamicallyGeneratedName(lambdas: LambdaFunction[]) {
return dynmicallyNamedLambdas;
}

export function getMissingStackNameErrorMsg(lambdaKeys: string[]) {
export function getMissingStackNameErrorMsg(lambdaKeys: string[]): string {
return (
"A forwarder ARN was provided with one or more dynamically named lambda function resources," +
" but the stack name was not provided. Without the stack name, the dynamically generated" +
Expand Down
29 changes: 21 additions & 8 deletions serverless/src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const layerNameLookup: { [key in ArchitectureType]: { [key: string]: stri
* Also assigns a general runtime type to the output lambdas. This helps to determine which lambda
* layers to add & which handler to redirect to later on in the macro.
*/
export function findLambdas(resources: Resources, templateParameterValues: Parameters) {
export function findLambdas(resources: Resources, templateParameterValues: Parameters): LambdaFunction[] {
return Object.entries(resources)
.map(([key, resource]) => {
if (resource.Type !== LAMBDA_FUNCTION_RESOURCE_TYPE) {
Expand Down Expand Up @@ -176,7 +176,7 @@ export function applyLayers(
nodeLayerVersion?: number,
dotnetLayerVersion?: number,
javaLayerVersion?: number,
) {
): string[] {
const errors: string[] = [];
lambdas.forEach((lambda) => {
if (lambda.runtimeType === RuntimeType.UNSUPPORTED) {
Expand Down Expand Up @@ -233,7 +233,11 @@ export function applyLayers(
return errors;
}

export function applyExtensionLayer(region: string, lambdas: LambdaFunction[], extensionLayerVersion?: number) {
export function applyExtensionLayer(
region: string,
lambdas: LambdaFunction[],
extensionLayerVersion?: number,
): string[] {
const errors: string[] = [];
lambdas.forEach((lambda) => {
let lambdaExtensionLayerArn;
Expand All @@ -250,7 +254,7 @@ export function applyExtensionLayer(region: string, lambdas: LambdaFunction[], e
return errors;
}

function addLayer(layerArn: string, lambda: LambdaFunction) {
function addLayer(layerArn: string, lambda: LambdaFunction): void {
if (layerArn === undefined) {
return;
}
Expand Down Expand Up @@ -292,7 +296,12 @@ export function getNewLayers(layerArn: string, currentLayers: LambdaLayersProper
return { [CFN_IF_FUNCTION_STRING]: [conditionalName, newLayersIfTrue, newLayersIfFalse] };
}

export function getLambdaLibraryLayerArn(region: string, version: number, runtime: string, architecture: string) {
export function getLambdaLibraryLayerArn(
region: string,
version: number,
runtime: string,
architecture: string,
): string {
const layerName = layerNameLookup[architectureLookup[architecture]][runtime];
const isGovCloud = region === "us-gov-east-1" || region === "us-gov-west-1";

Expand All @@ -304,7 +313,7 @@ export function getLambdaLibraryLayerArn(region: string, version: number, runtim
return `arn:aws:lambda:${region}:${DD_ACCOUNT_ID}:layer:${layerName}:${version}`;
}

export function getExtensionLayerArn(region: string, version: number, architecture: string) {
export function getExtensionLayerArn(region: string, version: number, architecture: string): string {
const layerName = architectureToExtensionLayerName[architecture];

const isGovCloud = region === "us-gov-east-1" || region === "us-gov-west-1";
Expand All @@ -317,14 +326,18 @@ export function getExtensionLayerArn(region: string, version: number, architectu
return `arn:aws:lambda:${region}:${DD_ACCOUNT_ID}:layer:${layerName}:${version}`;
}

export function getMissingLayerVersionErrorMsg(functionKey: string, formalRuntime: string, paramRuntime: string) {
export function getMissingLayerVersionErrorMsg(
functionKey: string,
formalRuntime: string,
paramRuntime: string,
): string {
return (
`Resource ${functionKey} has a ${formalRuntime} runtime, but no ${formalRuntime} Lambda Library version was provided. ` +
`Please add the '${paramRuntime}LayerVersion' parameter for the Datadog serverless macro.`
);
}

export function getMissingExtensionLayerVersionErrorMsg(functionKey: string) {
export function getMissingExtensionLayerVersionErrorMsg(functionKey: string): string {
return (
`Resource ${functionKey} has been configured to apply the extension laybe but no extension version was provided. ` +
`Please add the 'extensionLayerVersion' parameter for the Datadog serverless macro`
Expand Down
6 changes: 3 additions & 3 deletions serverless/src/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const JS_HANDLER = "node_modules/datadog-lambda-js/dist/handler.handler";
* 'DD_LAMBDA_HANDLER' is set to the original handler in the lambda's environment for the
* replacement handler to find.
*/
export function redirectHandlers(lambdas: LambdaFunction[], addLayers: boolean) {
export function redirectHandlers(lambdas: LambdaFunction[], addLayers: boolean): void {
lambdas.forEach((lambda) => {
if (lambda.runtimeType == RuntimeType.DOTNET || lambda.runtimeType == RuntimeType.JAVA) {
setEnvDatadogWrapper(lambda);
Expand Down Expand Up @@ -41,7 +41,7 @@ function getDDHandler(lambdaRuntime: RuntimeType | undefined, addLayers: boolean
}
}

function setEnvDatadogHandler(lambda: LambdaFunction) {
function setEnvDatadogHandler(lambda: LambdaFunction): void {
const environment = lambda.properties.Environment ?? {};
const environmentVariables = environment.Variables ?? {};

Expand All @@ -52,7 +52,7 @@ function setEnvDatadogHandler(lambda: LambdaFunction) {
lambda.properties.Environment = environment;
}

function setEnvDatadogWrapper(lambda: LambdaFunction) {
function setEnvDatadogWrapper(lambda: LambdaFunction): void {
const environment = lambda.properties.Environment ?? {};
const environmentVariables = environment.Variables ?? {};

Expand Down
8 changes: 4 additions & 4 deletions serverless/src/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MACRO_VERSION = "dd_sls_macro";
// Following the same pattern from SAM
const MACRO_BY = "dd_sls_macro_by";

export function addDDTags(lambdas: LambdaFunction[], config: Configuration) {
export function addDDTags(lambdas: LambdaFunction[], config: Configuration): void {
// Add the tags for each function, unless a tag already exists.
lambdas.forEach((lambda) => {
const tags = lambda.properties.Tags ?? [];
Expand Down Expand Up @@ -41,7 +41,7 @@ export function addDDTags(lambdas: LambdaFunction[], config: Configuration) {
});
}

export function addMacroTag(lambdas: LambdaFunction[], version: string | undefined) {
export function addMacroTag(lambdas: LambdaFunction[], version: string | undefined): void {
if (!version) return;

lambdas.forEach((lambda) => {
Expand All @@ -52,7 +52,7 @@ export function addMacroTag(lambdas: LambdaFunction[], version: string | undefin
});
}

export function addCDKTag(lambdas: LambdaFunction[]) {
export function addCDKTag(lambdas: LambdaFunction[]): void {
lambdas.forEach((lambda) => {
const tags = lambda.properties.Tags ?? [];
tags.push({ Key: MACRO_BY, Value: "CDK" });
Expand All @@ -61,7 +61,7 @@ export function addCDKTag(lambdas: LambdaFunction[]) {
});
}

export function addSAMTag(lambdas: LambdaFunction[]) {
export function addSAMTag(lambdas: LambdaFunction[]): void {
lambdas.forEach((lambda) => {
const tags = lambda.properties.Tags ?? [];

Expand Down
6 changes: 3 additions & 3 deletions serverless/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export enum TracingMode {
NONE,
}

function findIamRole(resources: Resources, lambda: LambdaFunction) {
function findIamRole(resources: Resources, lambda: LambdaFunction): IamRoleProperties | undefined {
const role = lambda.properties.Role;
let roleKey;
if (typeof role !== "string") {
Expand All @@ -66,7 +66,7 @@ function findIamRole(resources: Resources, lambda: LambdaFunction) {
}
}

export function getTracingMode(config: Configuration) {
export function getTracingMode(config: Configuration): TracingMode {
if (config.enableXrayTracing && config.enableDDTracing) {
return TracingMode.HYBRID;
} else if (config.enableDDTracing) {
Expand All @@ -77,7 +77,7 @@ export function getTracingMode(config: Configuration) {
return TracingMode.NONE;
}

export function enableTracing(tracingMode: TracingMode, lambdas: LambdaFunction[], resources: Resources) {
export function enableTracing(tracingMode: TracingMode, lambdas: LambdaFunction[], resources: Resources): void {
if (tracingMode === TracingMode.XRAY || tracingMode === TracingMode.HYBRID) {
log.debug("Enabling Xray tracing...");
const xrayPolicies = {
Expand Down
Loading