lzld
v1.5.0
Published
Lazy load API handlers, interactors and factories easily
Downloads
6
Readme
LZLD (Pronounced 'lazy load') 🦥
0 Dependency code splitting, lazy loading, and active code generation without transpilers for Typescript and NodeJS backends.
Read the Docs
Install
npm install lzld
TLDR;
Lazy Loading (for an HTTP API)
- Define a base class all your entrypoints will extend (Plain Old Javascript Class)
export class APIHandler {
static http: {
method: 'POST' | 'PUT' | 'GET'
path: string
}
async handle(): Promise<unknown> {
throw new Error('Not implemented')
}
}
- Define an entrypoint with that base class
const myHandlers = Entrypoint.init(APIHandler, {
__filename,
metadataFilepath: './metadata/handlers.generated.json',
match: /([a-zA-Z0-9]+).handler.ts$/,
getMetadata: (target, { result: [, name] }) => ({
name,
path: target.http.path,
method: target.http.method,
}),
});
Write a bunch of handlers in any nested path like
./features/api/users/GetUser.handler.ts
that implement your base classLazy load a handler with express routing or anything that returns a boolean
const Handler = myHandlers.find(({ path }) => matchPath(path, somePassedPath))
Code Generation
Generate some random .yaml
file for serverless/ansible/etc with template
literal syntax
myHandlers.codegen('./all_routes.yaml')
`routes:
${({entries}) => entries.map(({ meta }) => `${meta.name}: '${meta.method} ${method.path}'`)}
`
Generates ./all_routes.yaml
:
routes:
GetUsers: 'GET /users/:userId'
GeneratePDF: 'POST /pdf/generate'
...