Skip to content

Commit

Permalink
refactor: Improve typings (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyloJorgensen authored May 17, 2023
1 parent 074ef29 commit b2f5677
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
37 changes: 23 additions & 14 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,35 @@ import alexaSkillEventTemplate from "./events/aws/alexa-skill-event-template.jso
import cloudWatchEventTemplate from "./events/aws/cloud-watch-event-template.json";
import cognitoUserPoolEventTemplate from "./events/aws/cognito-user-pool-event-template.json";

type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>;
}
: T;

export const dictionary = {
"aws:sns": snsTemplate as SNSEvent,
"aws:sqs": sqsTemplate as SQSEvent,
"aws:apiGateway": apiGatewayTemplate as APIGatewayEvent,
"aws:scheduled": scheduledTemplate as ScheduledEvent,
"aws:s3": s3Template as S3Event,
"aws:kinesis": kinesisTemplate as KinesisStreamEvent,
"aws:dynamo": dynamoTemplate as DynamoDBStreamEvent,
"aws:cloudWatchLog": cloudwatchLogEventTemplate as CloudWatchLogsEvent,
"aws:alexaSmartHome": alexaSmartHomeEventTemplate as AlexaSmartHomeEvent,
"aws:alexaSkill": alexaSkillEventTemplate as AlexaSkillEvent,
"aws:cloudWatch": cloudWatchEventTemplate as CloudWatchEvent,
"aws:sns": snsTemplate as DeepPartial<SNSEvent>,
"aws:sqs": sqsTemplate as DeepPartial<SQSEvent>,
"aws:apiGateway": apiGatewayTemplate as DeepPartial<APIGatewayEvent>,
"aws:scheduled": scheduledTemplate as DeepPartial<ScheduledEvent>,
"aws:s3": s3Template as DeepPartial<S3Event>,
"aws:kinesis": kinesisTemplate as DeepPartial<KinesisStreamEvent>,
"aws:dynamo": dynamoTemplate as DeepPartial<DynamoDBStreamEvent>,
"aws:cloudWatchLog":
cloudwatchLogEventTemplate as DeepPartial<CloudWatchLogsEvent>,
"aws:alexaSmartHome":
alexaSmartHomeEventTemplate as DeepPartial<AlexaSmartHomeEvent>,
"aws:alexaSkill": alexaSkillEventTemplate as DeepPartial<AlexaSkillEvent>,
"aws:cloudWatch": cloudWatchEventTemplate as DeepPartial<CloudWatchEvent>,
"aws:iot": {} as any,
"aws:cognitoUserPool": cognitoUserPoolEventTemplate as CognitoUserPoolEvent,
"aws:websocket": apiGatewayTemplate as APIGatewayEvent, // Websockets are included in APIG typedef: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32855/files
"aws:cognitoUserPool":
cognitoUserPoolEventTemplate as DeepPartial<CognitoUserPoolEvent>,
"aws:websocket": apiGatewayTemplate as DeepPartial<APIGatewayEvent>, // Websockets are included in APIG typedef: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32855/files
};

export default function createEvent<T extends keyof typeof dictionary, B>(
eventType: T,
body: typeof dictionary[T],
body: typeof dictionary[T]
): typeof dictionary[T] {
const event = dictionary[eventType];
let generatedEvent = {};
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b2f5677

Please sign in to comment.