Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @glacierjs/core

@glacierjs/core

作为 GlacierJS 的核心模块,它提供:

  1. MiddlewareQueue:支撑函数的插件化。
  2. Logger:统一管理 GlacierJS 的日志。
  3. Const:统一管理静态变量

MiddlewareQueue

一个基于洋葱模型的中间件队列。

Quick Usage:

const queue = new MiddlewareQueue();

queue.push(async (context, next) => {
console.log('A');
await next();
console.log('B');
context.name = 'abc';
});

queue.push(async (context, next) => {
console.log('C');
await next();
console.log('D');
});

const context = {};
await queue.runAll(context); // print: A C D B
consle.log(context); // { name: 'abc' }

Logger

Quick Usage:

const logger = new Logger({ prefix: 'glacierjs' });
logger.info('hello'); // print: glacierjs [info] hello
logger.debug('hello'); // print: glacierjs [debug] hello
logger.warn('hello'); // print: glacierjs [warn] hello
logger.error('hello'); // print: glacierjs [error] hello

Nest Logger:

const myPluginLogger = logger.extends({ prefix: 'my-plugin' }); 
const myPluginModuleALogger = myPluginLogger.extends({ prefix: 'module-a' });

myPluginLogger.info('hello'); // print: glacierjs-my-plugin [info] hello
myPluginModuleALogger.info('hello'); // print: glacierjs-my-plugin-module-a [info] hello

Level Controller:

Logger.level = Level.ERROR;
logger.info('hello'); // print nothing
logger.debug('hello'); // print nothing
logger.warn('hello'); // print nothing
logger.error('hello'); // print: glacierjs [error] hello

Index

Logger

logger: Logger = ...

The instance of Class Logger in GlacierJS

Other

Renames and re-exports LoggerLevel
HookFn<ContextType>: (context: ContextType, next: NextFn) => Promise<void>

Type parameters

  • ContextType

Type declaration

    • (context: ContextType, next: NextFn): Promise<void>
    • Parameters

      • context: ContextType
      • next: NextFn

      Returns Promise<void>

Interceptor: (middleware: Middleware) => Middleware

Type declaration

Middleware: (context: any, next?: NextFn) => Promise<any>

Type declaration

    • (context: any, next?: NextFn): Promise<any>
      1. Middleware 不关心 context 类型 (context:any),只负责传递。
      2. Middleware 不关心返回内容 (Promise),只保证是异步任务。
      3. NextFn 不关心入参和返回,只保证是异步任务。

      Parameters

      • context: any
      • Optional next: NextFn

      Returns Promise<any>

NextFn: (...arg: any[]) => Promise<any>

Type declaration

    • (...arg: any[]): Promise<any>
    • Parameters

      • Rest ...arg: any[]

      Returns Promise<any>

Generated using TypeDoc