1.5.0 • Published 2 years ago

lzld v1.5.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

LZLD (Pronounced 'lazy load') 🦥

npm package Build Status Downloads Issues Commitizen Friendly Semantic Release

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

  2. 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

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago