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

aliyun-serverless-wrapper

v2.0.0

Published

wrapper for aliyun serverless

Downloads

5

Readme

Aliyun-serverless-wrapper

npm Codacy Badge CircleCI

This lib only works when using http-trigger

Yet this lib doesn't support multipart form data

By using this wrapper you shall write your handler function in a koa-like way.

Typescript friendly.

API

wrapper(handler: (ctx: WrappedContext, options?: WrapperOptions) => void) => void

The original request, response, context object will be merged into a more powerful WrappedContext object and passed into handler function.

WrapperOptions

  • timeout: set a timeout (ms) to limit the time range running handler

  • onError: you can set a callback like (err:Error, ctx: WrappedContext) => void for this field and do some error handler

WrappedContext

  • req: Request Request object

  • res: Response Response object

    also fields inherit from aliyun runtime context

  • credentials: AliyunContextCredentials

  • service: AliyunContextService

  • requestId: string

  • accountId: string

  • function: AliyunContextFunction

  • region: string

    and short hands for ctx.res

  • setHeader(field: string, value: string): void

  • removeHeader(field: string): void

  • get header: Headers

  • get headers: Headers

  • get status: number

  • set status(code: number): void

  • get body: any

  • set body(value: any): void

WrappedContext.Request

see request.ts

WrappedContext.Response

see response.ts

Example

Simply set the context's body, it will automatically send the response with proper status and header

const { wrapper } = require("aliyun-serverless-wrapper")

exports.someFunction = wrapper(async (ctx) => {
  ctx.body = { hello: "world" }
})

/* response shall be

  HTTP/1.1 200 OK
  Content-Type: application/json

  { "hello": "world" }

*/

exports.someFunction = wrapper(async (ctx) => {
  ctx.body = "hello world"
})

/* response shall be

  HTTP/1.1 200 OK
  Content-Type: text/plain

  "hello world"

*/

exports.someFunction = wrapper(async (ctx) => {
  ctx.body = "<html><h1>hello wordl</h1></html>"
})

/* response shall be

  HTTP/1.1 200 OK
  Content-Type: text/html

  "<html><h1>hello wordl</h1></html>"

*/

Or throw Error

exports.someFunction = wrapper(async (ctx) => {
  throw new Error("oops")
})

/* response shall be

  HTTP/1.1 500 Internal Error

*/

exports.someFunction = wrapper(
  async (ctx) => {
    await checkAuth(ctx.req) // and this will throw a Error
  },
  {
    onError: (e, ctx) => {
      ctx.status = 401
      ctx.body = { errorMessage: e.message }
    }
  }
)

/* response shall be

  HTTP/1.1 404 Not Found
  Content-Type: application/json

  { "errorMessage": "Not Authorized" }

*/