npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shadyzoz/fc-helper

v3.1.0

Published

函数计算 & API 网关辅助库

Downloads

6

Readme

fc-helper

函数计算 & API 网关辅助库

Build Status codecov

原始形式

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