@adobe/openwhisk-action-utils 中文文档教程

发布于 3 年前 浏览 1 项目主页 更新于 2 年前

Openwhisk Action Utilities

用于 OpenWhisk 操作的实用程序。

Status

GitHub 许可证GitHub 问题CircleCIcodecovLGTM Code Quality Grade: JavaScript

API Reference

Modules

expressify

将 OpenWhisk 网络操作转换为可通过正常方式处理的快速请求的帮助程序 快递员。

Expressify 将查询和大部分操作参数映射到 req.query。 原始操作参数在 req.owActionParams 下可用。

用法

const { expressify, errorHandler } = require('@adobe/openwhisk-action-utils');

async function main(params) {
  const app = express();
  app.use(cookieParser());
  app.use(express.static('static'));
  app.get('/', homepageHandler);
  app.get('/ping', pingHandler);
  app.use(errorHandler(log));

  return expressify(app)(params);
}
middleware

表达动作的辅助函数。

用法:

const {
  expressify, logRequest, errorHandler, asyncHandler, cacheControl, createBunyanLogger,
} = require('@adobe/openwhisk-action-utils');

async function startHandler(params, req, res) {
   res.send('Hello, world.');
}
async function main(params) {
  const log = createBunyanLogger();
  const app = express();
  app.use(logRequest(log));
  app.use(cacheControl());
  app.get('/', asyncHandler(startHandler));
  app.get('/ping', asyncHandler(pingHandler));
  app.use(errorHandler(log));

  return expressify(app)(params);
}
wrap

帮助函数轻松链接 OpenWhisk 操作。

用法

const { wrap } = require('@adobe/openwhisk-action-utils');

async main(params) {
  // …my action code…
}

module.exports.main = wrap(main)
  .with(epsagon)
  .with(status)
  .with(logger);

Classes

VersionLock

帮助程序类使用x-ow-version-lock 标头中的信息来锁定版本 一个 openwhisk 动作。

Functions

createBunyanLogger([logger])BunyanLogger

设置适合与 openwhisk 操作一起使用的 bunyan 记录器。 bunyan 记录器将 流到给定的螺旋记录器。

expressify

帮助程序将 OpenWhisk 网络操作转换为可以用正常方式处理的快速请求 快递员。

Expressify 将查询和大部分操作参数映射到 req.query。 原始操作参数在 req.owActionParams 下可用。

用法:

const { expressify, errorHandler } = require('@adobe/openwhisk-action-utils');

async function main(params) {
  const app = express();
  app.use(cookieParser());
  app.use(express.static('static'));
  app.get('/', homepageHandler);
  app.get('/ping', pingHandler);
  app.use(errorHandler(log));

  return expressify(app)(params);
}

expressify~expressify(app) ⇒ ActionFunction

创建一个使用 express 框架处理调用的 OpenWhisk 操作函数。

Kindexpressify
的内部方法 返回ActionFunction - 动作函数。
参见:https:

ParamTypeDescription
appExpressAppThe express application

//expressjs.com/en/4x/api.html#app

middleware

表达操作的辅助函数。

用法:

const {
  expressify, logRequest, errorHandler, asyncHandler, cacheControl, createBunyanLogger,
} = require('@adobe/openwhisk-action-utils');

async function startHandler(params, req, res) {
   res.send('Hello, world.');
}
async function main(params) {
  const log = createBunyanLogger();
  const app = express();
  app.use(logRequest(log));
  app.use(cacheControl());
  app.get('/', asyncHandler(startHandler));
  app.get('/ping', asyncHandler(pingHandler));
  app.use(errorHandler(log));

  return expressify(app)(params);
}

middleware~errorHandler(log) ⇒ ExpressMiddleware

错误处理程序。 报告请求处理期间发生的错误并做出响应 如果尚未设置,则带有 500

种类中间件
的内部方法 返回ExpressMiddleware - 一个快速中间件函数。

ParamTypeDescription
logBunyanLoggerThe logger to use for reporting errors.

示例

// install last
app.use(errorHandler(log));

middleware~cacheControl([value]) ⇒ ExpressMiddleware

确保缓存控制。 设置缓存控制标头。

种类中间件
的内部方法 返回ExpressMiddleware - 一个快速中间件函数。

ParamTypeDefaultDescription
[value]string"no-store, private, must-revalidate"Cache control header value.

示例

app.use(cacheControl());

middleware~hideHeaders(headerNames) ⇒ ExpressMiddleware

从枚举中隐藏标题。

种类中间件
的内部方法 返回ExpressMiddleware - 一个快速中间件函数。

ParamTypeDescription
headerNamesArray.<string>Names of headers to make un-enumerable

示例

// install first
app.use(hideHeaders(['x-token', 'authentication'));
app.use(logRequest(log));

middleware~logRequest(logger, [level]) ⇒ ExpressMiddleware

为请求创建一个 bunyan 子记录器并将其添加到请求中。 这确保了 重要的标头值,如 x-request-id 包含在每个日志条目中。 它也是 记录请求和响应行。

种类中间件
的内部方法 返回ExpressMiddleware - 一个快速中间件函数。

ParamTypeDefaultDescription
loggerBunyanLoggerthe bunyan logger
[level]string"debug"the log level to use for logging the request information.

示例

// install first
app.use(logRequest(log));

middleware~asyncHandler(fn) ⇒ ExpressMiddleware

包装路由中间件,以便它可以捕获潜在的承诺拒绝 在异步调用期间。

种类中间件
的内部方法 返回ExpressMiddleware - 一个快速中间件函数。

ParamTypeDescription
fnExpressMiddlewarean extended express middleware function

middleware~ActionMiddlewareFunction : function

asyncHandler 一起使用的扩展中间件函数。

种类中间件
的内部类型定义 参见:https:

ParamTypeDescription
params\*The action params
reqExpressRequestThe express request
resExpressResponseThe express response
nextExpressMiddlewareThe next handler in chain.

//expressjs.com/en/4x/api.html#middleware-callback-function-examples

wrap

轻松链接 OpenWhisk 的辅助函数动作。

用法:

const { wrap } = require('@adobe/openwhisk-action-utils');

async main(params) {
  // …my action code…
}

module.exports.main = wrap(main)
  .with(epsagon)
  .with(status)
  .with(logger);

wrap~wrap(main) ⇒ WrappableActionFunction

使您的操作函数(即 main)可包装的函数, 以便使用 with 可以应用许多包装器。 这允许 您将结果导出为新函数。

Kindwrap
的内部方法 返回WrappableActionFunction - 相同的主函数,现在包含一个with方法

ParamTypeDescription
mainActionFunctionthe main function to prepare for wrapping

Example

async main(params) {
  //…my action code…
}

module.exports.main = wrap(main)
.with(epsagon)
.with(status)
.with(logger);

注意:执行顺序是最后添加的包装器将首先执行。

wrap~ActionFunction ⇒ object

OpenWhisk 操作的 main 函数。

种类wrap
的内部类型定义 返回object - 结果

ParamTypeDescription
paramsobjectthe parameters of the action function

wrap~WrappableActionFunction ⇒ object

一个 ActionFunction 已被扩充为可使用 with 方法进行包装。

种类wrap
的内部类型定义 返回object - 结果

ParamTypeDescription
paramsobjectthe parameters of the action function

wrap~WrapFunction ⇒ ActionFunction

包装(并调用您的主函数)的函数。 可以用 装饰输入或输出,或提供额外的功能 例如日志记录、跟踪、调试等。

种类wrap
的内部类型定义 返回ActionFunction - 一个与原始主函数具有相同签名的新函数

ParamTypeDescription
mainActionFunctionyour main function
…opts\*configuration options for the wrapping function

示例

function tracer(fn, level) {
  return (params) => {
    log[level]('enter');
    const ret = fn(params);
    log[level]('exit');
    return ret;
  }
}

VersionLock

使用 x-ow-version-lock 标头中的信息来锁定版本的帮助程序类 一个 openwhisk 动作。

Kind: global class

new VersionLock(params, defaults)

创建一个版本锁类。

ParamTypeDescription
paramsobjectthe openwhisk action params
defaultsobjectthe action name defaults.

versionLock.transformActionURL(url)

根据锁信息转换一个action url。

KindVersionLock 的实例方法

ParamTypeDescription
urlstringthe action url.

versionLock.wrapOpenwhisk(ow) ⇒ OpenWhisk

增强 openwhisk客户端通过包装自动操作名称的 invoke 方法 替代品。

KindVersionLock
的实例方法 返回OpenWhisk - 包装的客户端

ParamTypeDescription
owOpenWhiskopenwhisk client

VersionLock.X_OW_VERSION_LOCK ⇒ string

版本锁标头的名称。

种类VersionLock
的静态属性 返回string - 'x-ow-version-lock'

createBunyanLogger([logger]) ⇒ BunyanLogger

设置适合与 openwhisk 操作一起使用的 bunyan 记录器。 bunyan 记录器将 流到给定的螺旋记录器。

种类:全局函数
返回BunyanLogger - 一个 bunyan 记录器

ParamTypeDefaultDescription
[logger]LoggerrootLoggera helix multi logger. defaults to the helix rootLogger.

Openwhisk Action Utilities

Utilities for OpenWhisk actions.

Status

GitHub licenseGitHub issuesCircleCIcodecovLGTM Code Quality Grade: JavaScript

API Reference

Modules

expressify

Helper to turn a OpenWhisk web action into a express request which can be handled with normal express handlers.

Expressify maps the query and most of action params to req.query. The original action params are available under req.owActionParams.

Usage:

const { expressify, errorHandler } = require('@adobe/openwhisk-action-utils');

async function main(params) {
  const app = express();
  app.use(cookieParser());
  app.use(express.static('static'));
  app.get('/', homepageHandler);
  app.get('/ping', pingHandler);
  app.use(errorHandler(log));

  return expressify(app)(params);
}
middleware

Helper functions for expressified actions.

Usage:

const {
  expressify, logRequest, errorHandler, asyncHandler, cacheControl, createBunyanLogger,
} = require('@adobe/openwhisk-action-utils');

async function startHandler(params, req, res) {
   res.send('Hello, world.');
}
async function main(params) {
  const log = createBunyanLogger();
  const app = express();
  app.use(logRequest(log));
  app.use(cacheControl());
  app.get('/', asyncHandler(startHandler));
  app.get('/ping', asyncHandler(pingHandler));
  app.use(errorHandler(log));

  return expressify(app)(params);
}
wrap

Helper function to easily chain OpenWhisk actions.

Usage:

const { wrap } = require('@adobe/openwhisk-action-utils');

async main(params) {
  // …my action code…
}

module.exports.main = wrap(main)
  .with(epsagon)
  .with(status)
  .with(logger);

Classes

VersionLock

Helper class that uses the information in the x-ow-version-lock header to lock the version of an openwhisk action.

Functions

createBunyanLogger([logger])BunyanLogger

Sets up a bunyan logger suitable to use with an openwhisk action. The bunyan logger will stream to the given helix logger.

expressify

Helper to turn a OpenWhisk web action into a express request which can be handled with normal express handlers.

Expressify maps the query and most of action params to req.query. The original action params are available under req.owActionParams.

Usage:

const { expressify, errorHandler } = require('@adobe/openwhisk-action-utils');

async function main(params) {
  const app = express();
  app.use(cookieParser());
  app.use(express.static('static'));
  app.get('/', homepageHandler);
  app.get('/ping', pingHandler);
  app.use(errorHandler(log));

  return expressify(app)(params);
}

expressify~expressify(app) ⇒ ActionFunction

Creates an OpenWhisk action function that uses the express framework to handle the invocation.

Kind: inner method of expressify
Returns: ActionFunction - An action function.
See: https://expressjs.com/en/4x/api.html#app

ParamTypeDescription
appExpressAppThe express application

middleware

Helper functions for expressified actions.

Usage:

const {
  expressify, logRequest, errorHandler, asyncHandler, cacheControl, createBunyanLogger,
} = require('@adobe/openwhisk-action-utils');

async function startHandler(params, req, res) {
   res.send('Hello, world.');
}
async function main(params) {
  const log = createBunyanLogger();
  const app = express();
  app.use(logRequest(log));
  app.use(cacheControl());
  app.get('/', asyncHandler(startHandler));
  app.get('/ping', asyncHandler(pingHandler));
  app.use(errorHandler(log));

  return expressify(app)(params);
}

middleware~errorHandler(log) ⇒ ExpressMiddleware

Error handler. Reports errors that happen during the request processing and responds with a 500 if not already set.

Kind: inner method of middleware
Returns: ExpressMiddleware - an express middleware function.

ParamTypeDescription
logBunyanLoggerThe logger to use for reporting errors.

Example

// install last
app.use(errorHandler(log));

middleware~cacheControl([value]) ⇒ ExpressMiddleware

Ensures cache control. Sets cache control headers.

Kind: inner method of middleware
Returns: ExpressMiddleware - an express middleware function.

ParamTypeDefaultDescription
[value]string"no-store, private, must-revalidate"Cache control header value.

Example

app.use(cacheControl());

middleware~hideHeaders(headerNames) ⇒ ExpressMiddleware

Hides headers from enumeration.

Kind: inner method of middleware
Returns: ExpressMiddleware - an express middleware function.

ParamTypeDescription
headerNamesArray.<string>Names of headers to make un-enumerable

Example

// install first
app.use(hideHeaders(['x-token', 'authentication'));
app.use(logRequest(log));

middleware~logRequest(logger, [level]) ⇒ ExpressMiddleware

Creates a bunyan child logger for the request and adds it to the request. This ensures that important header values, like x-request-id are included in every log entry. It also logs the request and response lines.

Kind: inner method of middleware
Returns: ExpressMiddleware - an express middleware function.

ParamTypeDefaultDescription
loggerBunyanLoggerthe bunyan logger
[level]string"debug"the log level to use for logging the request information.

Example

// install first
app.use(logRequest(log));

middleware~asyncHandler(fn) ⇒ ExpressMiddleware

Wraps the route middleware so it can catch potential promise rejections during the async invocation.

Kind: inner method of middleware
Returns: ExpressMiddleware - an express middleware function.

ParamTypeDescription
fnExpressMiddlewarean extended express middleware function

middleware~ActionMiddlewareFunction : function

Extended middleware function to be use with the asyncHandler.

Kind: inner typedef of middleware
See: https://expressjs.com/en/4x/api.html#middleware-callback-function-examples

ParamTypeDescription
params\*The action params
reqExpressRequestThe express request
resExpressResponseThe express response
nextExpressMiddlewareThe next handler in chain.

wrap

Helper function to easily chain OpenWhisk actions.

Usage:

const { wrap } = require('@adobe/openwhisk-action-utils');

async main(params) {
  // …my action code…
}

module.exports.main = wrap(main)
  .with(epsagon)
  .with(status)
  .with(logger);

wrap~wrap(main) ⇒ WrappableActionFunction

A function that makes your action function (i.e. main) wrappable, so that using with a number of wrappers can be applied. This allows you to export the result as a new function.

Kind: inner method of wrap
Returns: WrappableActionFunction - the same main function, now including a with method

ParamTypeDescription
mainActionFunctionthe main function to prepare for wrapping

Example

async main(params) {
  //…my action code…
}

module.exports.main = wrap(main)
.with(epsagon)
.with(status)
.with(logger);

Note: the execution order is that the last wrapper added will be executed first.

wrap~ActionFunction ⇒ object

The main function of an OpenWhisk action.

Kind: inner typedef of wrap
Returns: object - a result

ParamTypeDescription
paramsobjectthe parameters of the action function

wrap~WrappableActionFunction ⇒ object

An ActionFunction that has been augmented to become wrappable using the with method.

Kind: inner typedef of wrap
Returns: object - a result

ParamTypeDescription
paramsobjectthe parameters of the action function

wrap~WrapFunction ⇒ ActionFunction

A function that wraps (and invokes your main function). It can be used to decorate inputs or outputs, or to provide additional functionality like logging, tracing, debugging, etc.

Kind: inner typedef of wrap
Returns: ActionFunction - a new function with the same signature as your original main function

ParamTypeDescription
mainActionFunctionyour main function
…opts\*configuration options for the wrapping function

Example

function tracer(fn, level) {
  return (params) => {
    log[level]('enter');
    const ret = fn(params);
    log[level]('exit');
    return ret;
  }
}

VersionLock

Helper class that uses the information in the x-ow-version-lock header to lock the version of an openwhisk action.

Kind: global class

new VersionLock(params, defaults)

Creates a version lock class.

ParamTypeDescription
paramsobjectthe openwhisk action params
defaultsobjectthe action name defaults.

versionLock.transformActionURL(url)

Transforms an action url according to the lock information.

Kind: instance method of VersionLock

ParamTypeDescription
urlstringthe action url.

versionLock.wrapOpenwhisk(ow) ⇒ OpenWhisk

Enhances an openwhisk client by wrapping the invoke method for automatic action name replacement.

Kind: instance method of VersionLock
Returns: OpenWhisk - the wrapped client

ParamTypeDescription
owOpenWhiskopenwhisk client

VersionLock.X_OW_VERSION_LOCK ⇒ string

The name of the version lock header.

Kind: static property of VersionLock
Returns: string - 'x-ow-version-lock'

createBunyanLogger([logger]) ⇒ BunyanLogger

Sets up a bunyan logger suitable to use with an openwhisk action. The bunyan logger will stream to the given helix logger.

Kind: global function
Returns: BunyanLogger - A bunyan logger

ParamTypeDefaultDescription
[logger]LoggerrootLoggera helix multi logger. defaults to the helix rootLogger.
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文