lzld

Lazy load API handlers, interactors and factories easily

  • lzld

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
lzld
1.5.02 years ago2 years agoMinified + gzip package size for lzld in KB

Readme

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-url
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)

  1. 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')
  }
}

  1. 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,
  }),
});

  1. Write a bunch of handlers in any nested path like
`./features/api/users/GetUser.handler.ts` that implement your base class
  1. 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 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'

...

Read the Docs