https://github.com/zephyrpersonal/aliyun-serverless-wrapper
Wrapper for aliyun serverless (node)
https://github.com/zephyrpersonal/aliyun-serverless-wrapper
aliyun async http-trigger node serverless type typescript
Last synced: 11 months ago
JSON representation
Wrapper for aliyun serverless (node)
- Host: GitHub
- URL: https://github.com/zephyrpersonal/aliyun-serverless-wrapper
- Owner: zephyrpersonal
- Created: 2019-03-03T04:24:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T04:28:12.000Z (about 3 years ago)
- Last Synced: 2025-04-15T16:07:46.182Z (12 months ago)
- Topics: aliyun, async, http-trigger, node, serverless, type, typescript
- Language: TypeScript
- Size: 813 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Aliyun-serverless-wrapper

[](https://app.codacy.com/app/zephyrpersonal/aliyun-serverless-wrapper?utm_source=github.com&utm_medium=referral&utm_content=zephyrpersonal/aliyun-serverless-wrapper&utm_campaign=Badge_Grade_Dashboard)
[](https://circleci.com/gh/zephyrpersonal/aliyun-serverless-wrapper/tree/master)
## This lib only works when using [http-trigger](https://help.aliyun.com/document_detail/74757.html?spm=a2c4g.11186623.6.558.1d555ed64YgYv8#HTTP-trigger-interface)
## 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](src/request.ts)
### WrappedContext.Response
see [response.ts](src/response.ts)
## Example
Simply set the context's body, it will automatically send the response with proper status and header
```js
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 = "
hello wordl
"
})
/* response shall be
HTTP/1.1 200 OK
Content-Type: text/html
"
hello wordl
"
*/
```
Or throw Error
```js
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" }
*/
```