This package has been deprecated

Author message:

Package no longer supported. Use at your own risk

@yr/framework

5.1.0 • Public • Published

NPM Version Build Status

A universal (backend and frontend) application framework based on Express.js.

Usage

Server:

const framework = require('@yr/framework');

const app = framework('myapp', process.env.PORT, process.cwd(), {
  locales: {
    /* store locales files */
    load(dirpath)
  },
  templates: {
    /* store precompiled template files */
    load(dirpath)
  },
  middleware: {
    /* register middleware */
    register(app),
    /* register error middleware */
    registerError(app)
  },
  params: {
    /* register param validators */
    register(app)
  },
  pages: {
    home: {
      dir: 'pages/home',
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale']
    },
    blog: {
      dir: 'pages/blog',
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale/blog']
    }
  },
  settings: {
    /* store page specific settings */
    set(key, value)
  }
});

app.listen();

Client:

const framework = require('@yr/framework');

const app = framework('myapp', {
  middleware: {
    /* register middleware */
    register(app),
    /* register error middleware */
    registerError(app)
  },
  params: {
    /* register param validators */
    register(app)
  },
  pages: {
    home: {
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale']
    },
    blog: {
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale/blog']
    }
  },
  settings: {
    /* store page specific settings */
    set(key, value)
  }
});

app.listen();

API - Server

framework(id: String, port: Number, dir: String, options: Object): Express

Framework factory returning an Express application instance.

All key/value pairs in options will be stored as Express application settings (retrievable via app.get(key). If present, the following keys will be specifically handled:

  • pages: { [id: String]: { dir: String, pageFactory: (id: String, app: Express) => Page, routes: Array<String> }}: hash of pages to initialise. Page instances will be created via pageFactory, and registered with routes. Page directories at dir must contain a server.js file, and may optionally contain a locales subdirectory, a templates subdirectory, and a settings.js file.
  • locales: { load: (dirpath) => void }: if a locales directory exists in the application dir or in a page dir, the resolved path will be passed to the load method. The locales object can be retrieved via app.get('locales').
  • templates: { load: (dirpath) => void }: if a templates directory exists in the application dir or in a page dir, the resolved path will be passed to the load method. The templates object can be retrieved via app.get('templates').
  • settings: { set: (key: String, value: any) }: if a settings.js file exists in the application dir or in a page dir, the file content will be stored under the id key. The settings object can be retrieved via app.get('settings').
  • middleware: { register: (app) => void, registerError(app) => void }: middleware registration hooks. The register method will be invoked before, and registerError will be invoked after, page handlers are registered.
  • params: { register: (app) => void }: param validation hook

framework.Page: Page Reference to the Page class

framework.request: Object Reference to the Express request prototype

framework.response: Object Reference to the Express response prototype

framework.static: Object Reference to the Express static middleware

API - Client

framework(id: String, options: Object): Express

Framework factory returning an Express-client application instance.

All key/value pairs in options will be stored as Express application settings (retrievable via app.get(key). If present, the following keys will be specifically handled:

  • pages: { [id: String]: { pageFactory: (id: String, app: Express) => Page, routes: Array<String> }}: hash of pages to initialise. Page instances will be created via pageFactory, and registered with routes.
  • middleware: { register: (app) => void, registerError(app) => void }: middleware registration hooks. The register method will be invoked before, and registerError will be invoked after, page handlers are registered.
  • params: { register: (app) => void }: param validation hook

framework.Page: Page Reference to the Page class

framework.request: Object Reference to the Express-client request prototype

framework.response: Object Reference to the Express-client response prototype

Page

The Page class describes the default behaviour of site pages.

constructor(id: String, app: Express): Page Create a Page instance.

app: Express Reference to the Express application instance.

id: String The page id string.

debug: Function A debug instance, namespaced with id.

initialised: Boolean Flag indicating whether init() has been called.

Pages are subject to the following lifecycle: init -> handle -> render -> unrender -> unhandle

init(req: Request, res: Response, done: Function)

handle(req: Request, res: Response, done: Function)

render(req: Request, res: Response, done: Function)

unrender(req: Request, res: Response, done: Function)

unhandle(req: Request, res: Response, done: Function)

Middleware

Response extensions

Readme

Keywords

none

Package Sidebar

Install

npm i @yr/framework

Weekly Downloads

2

Version

5.1.0

License

MIT

Unpacked Size

58.8 kB

Total Files

38

Last publish

Collaborators

  • yr
  • saegrov