Skip to content

Commit

Permalink
Removed index files from ignore and add them to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
alimnios72 committed Aug 9, 2023
1 parent c67607a commit e5aba76
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ node_modules
coverage
.idea
*.iml
lib/*.d.ts
lib/*.js
index.js
index.d.ts
18 changes: 18 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LegacyProxyIntegrationConfig, LegacyProxyIntegrationEvent } from './lib/legacyProxyIntegration';
import { ProxyIntegrationConfig, ProxyIntegrationEvent } from './lib/proxyIntegration';
import { SnsConfig, SnsEvent } from './lib/sns';
import { SqsConfig, SqsEvent } from './lib/sqs';
import { S3Config, S3Event } from './lib/s3';
import { Context } from 'aws-lambda';
export interface RouteConfig {
legacyProxyIntegration?: LegacyProxyIntegrationConfig;
proxyIntegration?: ProxyIntegrationConfig;
sns?: SnsConfig;
sqs?: SqsConfig;
s3?: S3Config;
debug?: boolean;
onError?: ErrorHandler;
}
export declare type ErrorHandler<TContext extends Context = Context> = (error?: Error, event?: RouterEvent, context?: TContext) => Promise<any> | any | void;
export declare type RouterEvent = LegacyProxyIntegrationEvent | ProxyIntegrationEvent | SnsEvent | SqsEvent | S3Event;
export declare const handler: (routeConfig: RouteConfig) => <TContext extends Context>(event: RouterEvent, context: TContext) => Promise<any>;
62 changes: 62 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = void 0;
const handler = (routeConfig) => {
const eventProcessorMapping = extractEventProcessorMapping(routeConfig);
return async (event, context) => {
if (routeConfig.debug) {
console.log('Lambda invoked with request:', event);
console.log('Lambda invoked with context:', context);
}
for (const [eventProcessorName, eventProcessor] of eventProcessorMapping.entries()) {
try {
// the contract of 'processors' is as follows:
// - their method 'process' is called with (config, event)
// - the method...
// - returns null: the processor does not feel responsible for the event
// - throws Error: the 'error.toString()' is taken as the error message of processing the event
// - returns object: this is taken as the result of processing the event
// - returns promise: when the promise is resolved, this is taken as the result of processing the event
const result = eventProcessor.process(routeConfig[eventProcessorName], event, context);
if (result) {
// be resilient against a processor returning a value instead of a promise:
return await result;
}
else {
if (routeConfig.debug) {
console.log("Event processor couldn't handle request.");
}
}
}
catch (error) {
if (error.stack) {
console.log(error.stack);
}
if (routeConfig.onError) {
const result = await routeConfig.onError(error, event, context);
if (result) {
return result;
}
}
throw error.toString();
}
}
throw 'No event processor found to handle this kind of event!';
};
};
exports.handler = handler;
const extractEventProcessorMapping = (routeConfig) => {
const processorMap = new Map();
for (const key of Object.keys(routeConfig)) {
if (key === 'debug' || key === 'onError') {
continue;
}
try {
processorMap.set(key, require(`./lib/${key}`));
}
catch (error) {
throw new Error(`The event processor '${key}', that is mentioned in the routerConfig, cannot be instantiated (${error.toString()})`);
}
}
return processorMap;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "AWS lambda router",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": ["dist/index.*", "dist/**/*"],
"files": ["dist/**"],
"scripts": {
"test": "yarn clean && jest --coverage",
"build": "rimraf .dist/ && tsc"
Expand Down

0 comments on commit e5aba76

Please sign in to comment.