I am using TSED.io for a API that runs in container, and also I am trying to use TSED for lambda functions subscribed on SNS topics (which don't need a WEB API).
Is there a way to use TSED.io Injectables/services on a Lambda function context?
Hello @amellovmware
Yes, it could be possible. @tsed/di can be used in standalone.
Declare your service:
import {Service} from "@tsed/di";
@Service() // or @Injectable
export class MyService {
$onInit() { // fired by DI
}
$customEvent() {
}
createSomething() {}
}
```typescript
import {InjectorService} from "@tsed/di";
import {MyService} from "./MyService";
async function bootstrap() {
const injector = new InjectorService()
// Load all providers registered via @Injectable/@Service decorators. You just have to import
// your class in this same module
await injector.load() // fire $onInit event
// Get a specific service
const myService = injector.get
myService.createSomething()
// Emit a custom event
await injector.emit("customEvent") // accept args
// And finally destroy injector and his instances (see injector hooks)
await injector.destroy()
}
bootstrap()
It also possible to use a container when you use injector.load().
```typescript
import {Container, InjectorService} from "@tsed/di";
import {MyService} from "./MyService";
const container = new Container()
container.addProvider(MyService);
async function bootstrap() {
const injector = new InjectorService()
await injector.load(container)
}
I hope this example will help you :)
See you
Romain
Hey!
You could put this example in the tsed.io as serverless example with lambda, I don't know if I'll really use it, but now it's very trendy, and I'm sure it will attract more people for your project.
Yes sure. I have to found an example with express and lambda expression before and write a short example page ;)
Ok I see, The goal is to use the entire tsed app without running / listen server :).
It's same things as run the unit test with SuperTest. I'll try to provide a complete example and maybe a package to wrap the server into a lambda function (if it's necessary)
Here a quick example with aws-serveless-express:
import awsServerlessExpress from 'aws-serverless-express'
import 聽{ExpressApplication, ServerLoader} from '@tsed/common'
import {Server} from './Server'
const server = awsServerlessExpress.createServer(app)
exports.handler = async (event, context) => {
const appServer = await ServerLoader.bootstrap(Server)
const server = awsServerlessExpress.createServer(server.injector.get(ExpressApplication))
awsServerlessExpress.proxy(server, event, context)
}
This solution require Ts.ED v5.26.0.
I don't know if this code works properly. Any help would be appreciate to test this code :)
See you
Romain
The tutorial about aws and tsed is up to date: http://tsed.io/tutorials/aws.html
and a dedicated example repository: https://github.com/TypedProject/tsed-example-aws
Impressive, I'll try to test it soon.