Git Product home page Git Product 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

asyncWrap 针对普通的函数

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

'use strict';

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

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

测试支持

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

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

var handle = asyncWrap(async function (ctx) {
  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

jacksontian avatar cncolder avatar raininfall avatar

Watchers

James Cloos avatar leeyoung avatar

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.