LZLD (Pronounced 'lazy load') 🦥
!npm packagenpm-imgnpm-url
!Build Statusbuild-imgbuild-url
!Downloadsdownloads-imgdownloads-url
!Issuesissues-imgissues-url
!Commitizen Friendlycommitizen-imgcommitizen-url
!Semantic Releasesemantic-release-imgsemantic-release-url0 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 class
- Lazy 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 templateliteral 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'
...