Uncharted
03/18/2022, 1:41 PMexport interface IService {
handle(event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult>;
}
@injectable()
export class TestInject implements IService {
public async handle(event: APIGatewayProxyEvent) {
console.log('test inject');
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ success: true }),
};
}
}
const service = myContainer.get<IService>(TYPES.IService);
export const handle = service.handle;
Result:
Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read property 'name' of undefined","reason":"TypeError: Cannot read property 'name' of undefined","promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'name' of undefined"," at process.<anonymous> (file:///home/uncharted/projects/my-api/node_modules/@serverless-stack/aws-lambda-ric/lib/index.js:34:23)"," at process.emit (events.js:376:20)"," at processPromiseRejections (internal/process/promises.js:245:33)"," at processTicksAndRejections (internal/process/task_queues.js:96:32)"]}
Adam Fanello
03/18/2022, 3:17 PMAdam Fanello
03/18/2022, 3:19 PMUncharted
03/18/2022, 3:19 PMAdam Fanello
03/18/2022, 3:20 PMAdam Fanello
03/18/2022, 3:21 PMAdam Fanello
03/18/2022, 3:22 PMArtemiy Davydov
03/18/2022, 7:25 PM...
const api = new sst.Api(this, "Api", {
defaultFunctionProps: {
bundle: {
esbuildConfig: {
plugins: "esbuild-decorators-plugin.js",
},
},
...
esbuild-decorators-plugin.js
/* eslint-disable @typescript-eslint/no-var-requires */
const { esbuildDecorators } = require("@anatine/esbuild-decorators");
module.exports = [esbuildDecorators()];
tsconfig.json
...
"compilerOptions": {
...
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
Adam Fanello
03/18/2022, 7:29 PMArtemiy Davydov
03/19/2022, 7:40 AMUncharted
03/19/2022, 9:46 AMimport { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { injectable } from 'inversify';
import 'reflect-metadata';
import { myContainer } from './inversify.config';
import { TYPES } from './types';
export interface IService {
handle(event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult>;
}
@injectable()
export class TestInject implements IService {
public async handle(
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> {
console.log('test inject');
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ success: true }),
};
}
}
const service = myContainer.get<IService>(TYPES.IService);
// const service = new TestInject()
export const handle = service.handle;
Uncharted
03/19/2022, 9:48 AMconst TYPES = {
IService: Symbol.for('IService'),
};
export { TYPES };
inversify.config.ts
import { Container } from 'inversify';
import { TYPES } from './types';
import { IService, TestInject } from './testinject';
const myContainer = new Container();
myContainer.bind<IService>(TYPES.IService).to(TestInject);
export { myContainer };
stack
new sst.Api(this, 'Api', {
routes: {
'POST /testinject': {
function: {
bundle: {
esbuildConfig: {
plugins: 'src/esbuild-decorators-plugin.js',
},
},
srcPath: 'src/',
handler: 'testinject.handle',
},
},
},
});
Uncharted
03/19/2022, 9:49 AM{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": ["es2018"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"strictPropertyInitialization": false,
"typeRoots": ["./node_modules/@types"],
"esModuleInterop": true,
"types": ["reflect-metadata"],
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
}
Uncharted
03/19/2022, 9:57 AMUncharted
03/19/2022, 10:20 AMUncharted
03/19/2022, 3:42 PMFrank
Frank
Uncharted
03/21/2022, 11:01 AMUncharted
03/21/2022, 11:03 AMUncharted
03/21/2022, 11:05 AMAdam Fanello
03/21/2022, 2:38 PMUncharted
03/21/2022, 2:40 PM