Home Manual Reference Source Test Repository

Function

Static Public Summary
public

Gets the instance of the currently running application.

public

createObject(type: string | object, params: ...*): *

Creates a new object using the dependency injection (DI) container of the application.

public

async errorAction(ctx: Context, next: function)

Manages the application errors.

public

Creates a middleware function managing the application errors.

public

getAlias(alias: string, throwError: boolean): string

Translates a path alias into an actual path.

public

registerType(alias: string): *

Registers a type in the dependency injection (DI) container of the application.

public

Creates a middleware function setting some security-related headers.

public

setAlias(name: string, path: string)

Registers a path alias.

public

statusAction(ctx: Context)

Sends the application status.

public

Creates a middleware function sending the application status.

Static Public

public app(): Application source

Gets the instance of the currently running application.

Return:

Application

The application instance.

Test:

public createObject(type: string | object, params: ...*): * source

import {createObject} from '@cedx/koa-core/src/di.js'

Creates a new object using the dependency injection (DI) container of the application.

You may view this function as an enhanced version of the new operator. The function supports creating an object based on one of the following forms:

  • a string: a class alias representing the location and the class name of the object to be created.
  • a configuration object: the object must contain a class element which is treated as the class alias, and the rest of the name-value pairs will be used to initialize the corresponding object properties.

Params:

NameTypeAttributeDescription
type string | object

The object type: a class alias or a configuration object.

params ...*

The constructor parameters.

Return:

*

The newly created object.

Throw:

TypeError

The object type is not supported.

Test:

public async errorAction(ctx: Context, next: function) source

import {errorAction} from '@cedx/koa-core/src/error_action.js'

Manages the application errors.

Params:

NameTypeAttributeDescription
ctx Context

The context encapsulating the client request and the server response.

next function

TODO

public errorHandler(): function source

import {errorHandler} from '@cedx/koa-core/src/middleware.js'

Creates a middleware function managing the application errors.

Return:

function

The created middleware function.

public getAlias(alias: string, throwError: boolean): string source

import {getAlias} from '@cedx/koa-core/src/alias.js'

Translates a path alias into an actual path.

The translation is done according to the following procedure:

  • If the given alias does not start with @, it is returned back without change.
  • Otherwise, look for the registered alias that matches the beginning part of the given alias. If it exists, replace the matching part of the given alias with the corresponding registered path.
  • Throw an error or return an empty string, depending on the throwError parameter.

Params:

NameTypeAttributeDescription
alias string

The alias to be translated.

throwError boolean
  • optional

Value indicating whether to throw an error if the given alias is invalid.

Return:

string

The path corresponding to the alias, or an empty string if the root alias is not previously registered.

Throw:

TypeError

The path alias is invalid while throwError is true.

Test:

public registerType(alias: string): * source

import {registerType} from '@cedx/koa-core/src/di.js'

Registers a type in the dependency injection (DI) container of the application.

A type is registered using a class alias specifying the location and the name of the type to be registered. For example:

  • @app/components/FooBar: will be resolved to the FooBar class exported by the @app/components.js module, or the @app/components/index.js module.
  • @core/FooBar: will be resolved to the FooBar class exported by this package.
  • @npm/foo/FooBar: will be resolved to the FooBar class exported by the foo package located in the node_modules folder.
  • @npm/@foo/bar/FooBar: will be resolved to the FooBar class exported by the @foo/bar package located in the node_modules folder.

Params:

NameTypeAttributeDescription
alias string

The class alias representing the location and the name of the type to be registered.

Return:

*

The resolved type.

Throw:

TypeError

The specified class alias is invalid, or the corresponding type is not found.

Test:

public securityHandler(): function source

import {securityHandler} from '@cedx/koa-core/src/middleware.js'

Creates a middleware function setting some security-related headers.

Return:

function

The created middleware function.

public setAlias(name: string, path: string) source

import {setAlias} from '@cedx/koa-core/src/alias.js'

Registers a path alias.

It must start with the character @ so that it can be easily differentiated from non-alias paths. Alias defined without leading @ will be prefixed with @ character. Any trailing / and \ characters in the given path will be trimmed.

Params:

NameTypeAttributeDescription
name string

The alias name (e.g. "@alias").

path string

The path corresponding to the alias. This can be: a directory or a file path, a URL or a path alias.

Throw:

TypeError

The specified name is empty.

Test:

public statusAction(ctx: Context) source

import {statusAction} from '@cedx/koa-core/src/status_action.js'

Sends the application status.

Params:

NameTypeAttributeDescription
ctx Context

The context encapsulating the client request and the server response.

public statusHandler(): function source

import {statusHandler} from '@cedx/koa-core/src/middleware.js'

Creates a middleware function sending the application status.

Return:

function

The created middleware function.