Skip to content

Commit

Permalink
Move interfaces to their own file and inforce type imports
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbarthelet committed Aug 17, 2021
1 parent 1d78b0a commit 6653909
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 126 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/switch-exhaustiveness-check": "error",
// We intentionally use this so that constructs can declare their command handlers
"@typescript-eslint/unbound-method": "off"
"@typescript-eslint/unbound-method": "off",
"@typescript-eslint/consistent-type-imports": "error"
}
}
]
Expand Down
7 changes: 4 additions & 3 deletions src/CloudFormation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DescribeStacksInput, DescribeStacksOutput } from "aws-sdk/clients/cloudformation";
import { CfnOutput, Stack } from "@aws-cdk/core";
import { AwsProvider } from "@lift/providers";
import type { DescribeStacksInput, DescribeStacksOutput } from "aws-sdk/clients/cloudformation";
import type { CfnOutput } from "@aws-cdk/core";
import { Stack } from "@aws-cdk/core";
import type { AwsProvider } from "@lift/providers";
import { debug } from "./utils/logger";

export async function getStackOutput(aws: AwsProvider, output: CfnOutput): Promise<string | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion src/classes/aws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provider as LegacyAwsProvider } from "../types/serverless";
import type { Provider as LegacyAwsProvider } from "../types/serverless";

// This is defined as a separate function to allow mocking in tests
export async function awsRequest<Input, Output>(
Expand Down
31 changes: 31 additions & 0 deletions src/constructs/ConstructInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { PolicyStatement } from "../CloudFormation";

/**
* Defines which methods a Lift construct must expose.
*/
export interface ConstructInterface {
/**
* Values shown in the CLI output.
*/
outputs?(): Record<string, () => Promise<string | undefined>>;

/**
* serverless.yml variables
*/
variables?(): Record<string, unknown>;

/**
* Post-CloudFormation deployment
*/
postDeploy?(): Promise<void>;

/**
* Pre-CloudFormation deletion
*/
preRemove?(): Promise<void>;

/**
* IAM permissions to add to Lambda functions of the stack
*/
permissions?(): PolicyStatement[];
}
30 changes: 30 additions & 0 deletions src/constructs/StaticConstructInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ConstructInterface } from "@lift/constructs";
import type { ProviderInterface } from "@lift/providers";
import type { CliOptions } from "../types/serverless";

/**
* Defines which static properties and methods a Lift construct must expose.
*/
export interface StaticConstructInterface {
type: string;
schema: {
type: "object";
[k: string]: unknown;
};
commands?: ConstructCommands;
create(provider: ProviderInterface, id: string, configuration: Record<string, unknown>): ConstructInterface;
}

export type ConstructCommands = Record<string, ConstructCommandDefinition>;
type ConstructCommandDefinition = {
usage: string;
handler: (options: CliOptions) => void | Promise<void>;
options?: {
[name: string]: {
usage: string;
type: string;
required?: boolean;
shortcut?: string;
};
};
};
4 changes: 2 additions & 2 deletions src/constructs/abstracts/AwsConstruct.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct as CdkConstruct } from "@aws-cdk/core";
import { AwsProvider } from "@lift/providers";
import { ConstructInterface } from "@lift/constructs";
import type { AwsProvider } from "@lift/providers";
import type { ConstructInterface } from "@lift/constructs";

export abstract class AwsConstruct extends CdkConstruct implements ConstructInterface {
static create<C extends AwsConstruct = AwsConstruct>(
Expand Down
7 changes: 4 additions & 3 deletions src/constructs/aws/DatabaseDynamoDBSingleTable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Construct as CdkConstruct, CfnOutput, Fn, Stack } from "@aws-cdk/core";
import type { Construct as CdkConstruct } from "@aws-cdk/core";
import { CfnOutput, Fn, Stack } from "@aws-cdk/core";
import { AttributeType, BillingMode, StreamViewType, Table } from "@aws-cdk/aws-dynamodb";
import { FromSchema } from "json-schema-to-ts";
import { AwsProvider } from "@lift/providers";
import type { FromSchema } from "json-schema-to-ts";
import type { AwsProvider } from "@lift/providers";
import { AwsConstruct } from "@lift/constructs/abstracts";
import { PolicyStatement } from "../../CloudFormation";

Expand Down
15 changes: 8 additions & 7 deletions src/constructs/aws/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { Queue as CdkQueue } from "@aws-cdk/aws-sqs";
import { FromSchema } from "json-schema-to-ts";
import type { FromSchema } from "json-schema-to-ts";
import { Alarm, ComparisonOperator, Metric } from "@aws-cdk/aws-cloudwatch";
import { Subscription, SubscriptionProtocol, Topic } from "@aws-cdk/aws-sns";
import { AlarmActionConfig } from "@aws-cdk/aws-cloudwatch/lib/alarm-action";
import { Construct as CdkConstruct, CfnOutput, Duration } from "@aws-cdk/core";
import type { AlarmActionConfig } from "@aws-cdk/aws-cloudwatch/lib/alarm-action";
import type { Construct as CdkConstruct } from "@aws-cdk/core";
import { CfnOutput, Duration } from "@aws-cdk/core";
import chalk from "chalk";
import { PurgeQueueRequest, SendMessageRequest } from "aws-sdk/clients/sqs";
import type { PurgeQueueRequest, SendMessageRequest } from "aws-sdk/clients/sqs";
import ora from "ora";
import { spawnSync } from "child_process";
import * as inquirer from "inquirer";
import { AwsProvider } from "@lift/providers";
import type { AwsProvider } from "@lift/providers";
import { AwsConstruct } from "@lift/constructs/abstracts";
import { ConstructCommands } from "@lift/constructs";
import type { ConstructCommands } from "@lift/constructs";
import { pollMessages, retryMessages } from "./queue/sqs";
import { sleep } from "../../utils/sleep";
import { PolicyStatement } from "../../CloudFormation";
import { CliOptions } from "../../types/serverless";
import type { CliOptions } from "../../types/serverless";

const QUEUE_DEFINITION = {
type: "object",
Expand Down
15 changes: 8 additions & 7 deletions src/constructs/aws/StaticWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import {
ViewerProtocolPolicy,
} from "@aws-cdk/aws-cloudfront";
import * as cloudfront from "@aws-cdk/aws-cloudfront";
import { Construct as CdkConstruct, CfnOutput, Duration, RemovalPolicy } from "@aws-cdk/core";
import { FromSchema } from "json-schema-to-ts";
import {
import type { Construct as CdkConstruct } from "@aws-cdk/core";
import { CfnOutput, Duration, RemovalPolicy } from "@aws-cdk/core";
import type { FromSchema } from "json-schema-to-ts";
import type {
DeleteObjectsOutput,
DeleteObjectsRequest,
ListObjectsV2Output,
ListObjectsV2Request,
} from "aws-sdk/clients/s3";
import chalk from "chalk";
import { CreateInvalidationRequest, CreateInvalidationResult } from "aws-sdk/clients/cloudfront";
import type { CreateInvalidationRequest, CreateInvalidationResult } from "aws-sdk/clients/cloudfront";
import { S3Origin } from "@aws-cdk/aws-cloudfront-origins";
import * as acm from "@aws-cdk/aws-certificatemanager";
import { flatten } from "lodash";
import { ErrorResponse } from "@aws-cdk/aws-cloudfront/lib/distribution";
import { AwsProvider } from "@lift/providers";
import type { ErrorResponse } from "@aws-cdk/aws-cloudfront/lib/distribution";
import type { AwsProvider } from "@lift/providers";
import { AwsConstruct } from "@lift/constructs/abstracts";
import { ConstructCommands } from "@lift/constructs";
import type { ConstructCommands } from "@lift/constructs";
import { log } from "../../utils/logger";
import { s3Sync } from "../../utils/s3-sync";
import ServerlessError from "../../utils/error";
Expand Down
7 changes: 4 additions & 3 deletions src/constructs/aws/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BlockPublicAccess, Bucket, BucketEncryption, StorageClass } from "@aws-cdk/aws-s3";
import { Construct as CdkConstruct, CfnOutput, Duration, Fn, Stack } from "@aws-cdk/core";
import { FromSchema } from "json-schema-to-ts";
import { AwsProvider } from "@lift/providers";
import type { Construct as CdkConstruct } from "@aws-cdk/core";
import { CfnOutput, Duration, Fn, Stack } from "@aws-cdk/core";
import type { FromSchema } from "json-schema-to-ts";
import type { AwsProvider } from "@lift/providers";
import { AwsConstruct } from "@lift/constructs/abstracts";
import { PolicyStatement } from "../../CloudFormation";

Expand Down
8 changes: 4 additions & 4 deletions src/constructs/aws/Vpc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Vpc as CdkVpc, Peer, Port, SecurityGroup } from "@aws-cdk/aws-ec2";
import { Construct as CdkConstruct } from "@aws-cdk/core";
import { FromSchema } from "json-schema-to-ts";
import { AwsProvider } from "@lift/providers";
import { ConstructInterface } from "@lift/constructs";
import type { Construct as CdkConstruct } from "@aws-cdk/core";
import type { FromSchema } from "json-schema-to-ts";
import type { AwsProvider } from "@lift/providers";
import type { ConstructInterface } from "@lift/constructs";

const VPC_DEFINITION = {
type: "object",
Expand Down
7 changes: 4 additions & 3 deletions src/constructs/aws/Webhook.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Construct as CdkConstruct, CfnOutput, Fn } from "@aws-cdk/core";
import type { Construct as CdkConstruct } from "@aws-cdk/core";
import { CfnOutput, Fn } from "@aws-cdk/core";
import { CfnAuthorizer, CfnIntegration, CfnRoute, HttpApi } from "@aws-cdk/aws-apigatewayv2";
import { Function } from "@aws-cdk/aws-lambda";
import { EventBus } from "@aws-cdk/aws-events";
import { FromSchema } from "json-schema-to-ts";
import type { FromSchema } from "json-schema-to-ts";
import { PolicyDocument, PolicyStatement, Role, ServicePrincipal } from "@aws-cdk/aws-iam";
import { AwsProvider } from "@lift/providers";
import type { AwsProvider } from "@lift/providers";
import { AwsConstruct } from "@lift/constructs/abstracts";
import ServerlessError from "../../utils/error";

Expand Down
4 changes: 2 additions & 2 deletions src/constructs/aws/queue/sqs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
DeleteMessageBatchRequest,
DeleteMessageBatchResult,
Message,
Expand All @@ -7,7 +7,7 @@ import {
SendMessageBatchRequest,
SendMessageBatchResult,
} from "aws-sdk/clients/sqs";
import { AwsProvider } from "@lift/providers";
import type { AwsProvider } from "@lift/providers";
import { log } from "../../../utils/logger";
import { sleep } from "../../../utils/sleep";

Expand Down
62 changes: 2 additions & 60 deletions src/constructs/index.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,2 @@
import { ProviderInterface } from "@lift/providers";
import { PolicyStatement } from "../CloudFormation";
import { CliOptions } from "../types/serverless";

/**
* Defines which methods a Lift construct must expose.
*/
export interface ConstructInterface {
/**
* Values shown in the CLI output.
*/
outputs?(): Record<string, () => Promise<string | undefined>>;

/**
* serverless.yml variables
*/
variables?(): Record<string, unknown>;

/**
* Post-CloudFormation deployment
*/
postDeploy?(): Promise<void>;

/**
* Pre-CloudFormation deletion
*/
preRemove?(): Promise<void>;

/**
* IAM permissions to add to Lambda functions of the stack
*/
permissions?(): PolicyStatement[];
}

/**
* Defines which static properties and methods a Lift construct must expose.
*/
export interface StaticConstructInterface {
type: string;
schema: {
type: "object";
[k: string]: unknown;
};
commands?: ConstructCommands;
create(provider: ProviderInterface, id: string, configuration: Record<string, unknown>): ConstructInterface;
}

export type ConstructCommands = Record<string, ConstructCommandDefinition>;
type ConstructCommandDefinition = {
usage: string;
handler: (options: CliOptions) => void | Promise<void>;
options?: {
[name: string]: {
usage: string;
type: string;
required?: boolean;
shortcut?: string;
};
};
};
export type { ConstructInterface } from "./ConstructInterface";
export type { StaticConstructInterface, ConstructCommands } from "./StaticConstructInterface";
9 changes: 5 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { flatten, get, has, merge } from "lodash";
import chalk from "chalk";
import { AwsIamPolicyStatements } from "@serverless/typescript";
import type { AwsIamPolicyStatements } from "@serverless/typescript";
import * as path from "path";
import { readFileSync } from "fs";
import { dump } from "js-yaml";
import { DefaultTokenResolver, Lazy, StringConcat, Tokenization } from "@aws-cdk/core";
import { FromSchema } from "json-schema-to-ts";
import { AwsProvider, ProviderInterface, StaticProviderInterface, StripeProvider } from "@lift/providers";
import { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
import type { FromSchema } from "json-schema-to-ts";
import type { ProviderInterface, StaticProviderInterface } from "@lift/providers";
import { AwsProvider, StripeProvider } from "@lift/providers";
import type { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
import type {
CommandsDefinition,
DeprecatedVariableResolver,
Expand Down
11 changes: 6 additions & 5 deletions src/providers/AwsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { App, CfnOutput, Stack } from "@aws-cdk/core";
import type { CfnOutput } from "@aws-cdk/core";
import { App, Stack } from "@aws-cdk/core";
import { get, merge } from "lodash";
import { AwsCfInstruction, AwsLambdaVpcConfig } from "@serverless/typescript";
import { ProviderInterface } from "@lift/providers";
import { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
import type { AwsCfInstruction, AwsLambdaVpcConfig } from "@serverless/typescript";
import type { ProviderInterface } from "@lift/providers";
import type { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
import { DatabaseDynamoDBSingleTable, Queue, StaticWebsite, Storage, Vpc, Webhook } from "@lift/constructs/aws";
import { getStackOutput } from "../CloudFormation";
import { CloudformationTemplate, Provider as LegacyAwsProvider, Serverless } from "../types/serverless";
import type { CloudformationTemplate, Provider as LegacyAwsProvider, Serverless } from "../types/serverless";
import { awsRequest } from "../classes/aws";
import ServerlessError from "../utils/error";

Expand Down
5 changes: 5 additions & 0 deletions src/providers/ProviderInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ConstructInterface } from "@lift/constructs";

export interface ProviderInterface {
create(type: string, id: string): ConstructInterface;
}
6 changes: 6 additions & 0 deletions src/providers/StaticProviderInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { StaticConstructInterface } from "@lift/constructs";

export interface StaticProviderInterface {
getConstructClass(type: string): StaticConstructInterface | undefined;
getAllConstructClasses(): StaticConstructInterface[];
}
6 changes: 3 additions & 3 deletions src/providers/StripeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { resolve } from "path";
import { parse as tomlParse } from "toml";
import { get } from "lodash";
import { Stripe } from "stripe";
import { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
import { ProviderInterface } from "@lift/providers";
import { Serverless } from "../types/serverless";
import type { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
import type { ProviderInterface } from "@lift/providers";
import type { Serverless } from "../types/serverless";
import ServerlessError from "../utils/error";

type StripeConfiguration = {
Expand Down
12 changes: 2 additions & 10 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
export type { ProviderInterface } from "./ProviderInterface";
export type { StaticProviderInterface } from "./StaticProviderInterface";

export { AwsProvider } from "./AwsProvider";
export { StripeProvider } from "./StripeProvider";

export interface ProviderInterface {
create(type: string, id: string): ConstructInterface;
}

export interface StaticProviderInterface {
getConstructClass(type: string): StaticConstructInterface | undefined;
getAllConstructClasses(): StaticConstructInterface[];
}
4 changes: 2 additions & 2 deletions src/utils/s3-sync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
DeleteObjectsOutput,
DeleteObjectsRequest,
ListObjectsV2Output,
Expand All @@ -14,7 +14,7 @@ import * as crypto from "crypto";
import { lookup } from "mime-types";
import { chunk, flatten } from "lodash";
import chalk from "chalk";
import { AwsProvider } from "@lift/providers";
import type { AwsProvider } from "@lift/providers";

const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/queues.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { merge } from "lodash";
import * as sinon from "sinon";
import { DeleteMessageBatchResult, ReceiveMessageResult, SendMessageBatchResult } from "aws-sdk/clients/sqs";
import type { DeleteMessageBatchResult, ReceiveMessageResult, SendMessageBatchResult } from "aws-sdk/clients/sqs";
import * as CloudFormationHelpers from "../../src/CloudFormation";
import { pluginConfigExt, runServerless } from "../utils/runServerless";
import { mockAws } from "../utils/mockAws";
Expand Down
2 changes: 1 addition & 1 deletion test/unit/vpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cloneDeep, get, merge } from "lodash";
import { AwsCfInstruction } from "@serverless/typescript";
import type { AwsCfInstruction } from "@serverless/typescript";
import { baseConfig, pluginConfigExt, runServerless } from "../utils/runServerless";
import ServerlessError from "../../src/utils/error";

Expand Down
Loading

0 comments on commit 6653909

Please sign in to comment.