GithubHelp home page GithubHelp logo

fc-helper's Introduction

fc-helper

函数计算 & API 网关辅助库

  • Build Status

原始形式

exports.handle = function(event, context, callback) {
  // 处理 event
  callback(null, {
    isBase64Encoded: false,
    statusCode: statusCode,
    headers: headers,
    body: body
  });
}

新形式

'use strict';

const { hook } = require('fc-helper');

exports.handle = hook(async function (ctx) {
  ctx.body = 'Hello world!\n';
});

会更接近于常见 Web 开发的体验。

API

hook 针对 API Gateway 作为前端

将一个 Web 形式的处理单元转化为原始形式。使用方法:

const { hook } = require('fc-helper');

exports.handle = hook(async function(ctx) {
  ctx.body = 'Hello world!\n';
});

Request

包装后的 Request 对象:具有 path、method、headers、query、params、body只读属性。

可以通过 ctx.path、ctx.method、ctx.

Response

包装后的 Response 对象:

可以通过 ctx.type= "" 的方式设置 content-type。

可以通过 ctx.status= 200 的方式设置 statusCode。

可以通过 ctx.set('key', 'value') 的方式设置任意用于响应的 headers。

可以通过 ctx.body= 的方式设置 body。如果没有设置 content-type,会根据 body 类型自动设置 content-type。

  • string,会自动填充类型为 text/plain
  • object,会自动填充类型为 application/json

Context

即上文中的 ctx。ctx 除了上述的几个字段外,还包含 requestId、credentials、function 字段,即原始的 context 对象的属性。

asyncWrap 针对普通的函数

调用 return 返回的值,即结果。开发者可以编写顺序式的业务逻辑,远离回调。

'use strict';

const { asyncWrap } = require('fc-helper');

exports.handle = asyncWrap(async function (event, context) {
  return 'hello world!';
});

测试支持

提供 test 方法,将一个函数变成一个可执行的 case,然后通过 run 方法执行。返回一个 Promise 对象。

const { test, asyncWrap } = require('fc-helper');

var handle = asyncWrap(async function (event, context) {
  return 'hello world!';
});
const res = await test(handle).run('', '');
assert.equal(res, 'hello world!');

run(event, contenxt) 方法接受两个参数,event 和 contentx。我们通过修改这两个值来 mock 真实环境的输入。

License

The MIT license

fc-helper's People

Contributors

cncolder avatar jacksontian avatar raininfall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fc-helper's Issues

[RFC] 辅助库的使用方式建议

背景

新的形式相较于函数原有模式已经比较简单,但是和 web 的使用方式还是有点差异,感觉可以进一步改变形式,进而提供更贴近 web 的开发体验。

示例

// api/hello.js
module.exports = async ctx => {
  ctx.body = 'Hello world!\n';
}
// index.js
const { router } = require('fc-helper');
const helloController = require('./api/hello');

module.exports = () => {
  // self register
  router.add('/hello', helloController);

  // auto register
  router.load('api');

  return router.routes;
}

拓展

  • api 路由注册(index.js 也可以不要)
  • event 自动绑定(如手动指定了非 http event,则将其与 api 绑定)

[咨询]函数方法复用

比如正常我们传统项目里会抽出一些 helper/utils 方法,model 模型之类的,在不同的路由、事件触发器等地方复用(通过代码文件拆分,然后 require)。

那么在云函数中,如何把一个个独立的函数也工程化,抽象封装的复用组件、方法该放到哪里,怎么调用?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.