{"id":20854009,"url":"https://github.com/zephyrpersonal/aliyun-serverless-wrapper","last_synced_at":"2025-05-12T05:31:47.259Z","repository":{"id":34247078,"uuid":"173530336","full_name":"zephyrpersonal/aliyun-serverless-wrapper","owner":"zephyrpersonal","description":"Wrapper for aliyun serverless (node)","archived":false,"fork":false,"pushed_at":"2023-01-07T04:28:12.000Z","size":833,"stargazers_count":3,"open_issues_count":19,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T16:07:46.182Z","etag":null,"topics":["aliyun","async","http-trigger","node","serverless","type","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zephyrpersonal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-03T04:24:53.000Z","updated_at":"2021-09-08T06:09:32.000Z","dependencies_parsed_at":"2023-01-15T05:45:22.801Z","dependency_job_id":null,"html_url":"https://github.com/zephyrpersonal/aliyun-serverless-wrapper","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zephyrpersonal%2Faliyun-serverless-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zephyrpersonal%2Faliyun-serverless-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zephyrpersonal%2Faliyun-serverless-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zephyrpersonal%2Faliyun-serverless-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zephyrpersonal","download_url":"https://codeload.github.com/zephyrpersonal/aliyun-serverless-wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253682309,"owners_count":21946912,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aliyun","async","http-trigger","node","serverless","type","typescript"],"created_at":"2024-11-18T03:24:06.400Z","updated_at":"2025-05-12T05:31:46.865Z","avatar_url":"https://github.com/zephyrpersonal.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Aliyun-serverless-wrapper\n\n![npm](https://img.shields.io/npm/v/aliyun-serverless-wrapper.svg)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b283383048264aca84abcaa8e02e01ac)](https://app.codacy.com/app/zephyrpersonal/aliyun-serverless-wrapper?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=zephyrpersonal/aliyun-serverless-wrapper\u0026utm_campaign=Badge_Grade_Dashboard)\n[![CircleCI](https://circleci.com/gh/zephyrpersonal/aliyun-serverless-wrapper/tree/master.svg?style=svg)](https://circleci.com/gh/zephyrpersonal/aliyun-serverless-wrapper/tree/master)\n\n## 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)\n\n## Yet this lib doesn't support multipart form data\n\nBy using this wrapper you shall write your handler function in a koa-like way.\n\nTypescript friendly.\n\n## API\n\n`wrapper(handler: (ctx: WrappedContext, options?: WrapperOptions) =\u003e void) =\u003e void`\n\nThe original `request`, `response`, `context` object will be merged into a more powerful `WrappedContext` object and passed into handler function.\n\n### WrapperOptions\n\n- `timeout:` set a timeout (ms) to limit the time range running handler\n\n- `onError:` you can set a callback like `(err:Error, ctx: WrappedContext) =\u003e void` for this field and do some error handler\n\n### WrappedContext\n\n- `req: Request` Request object\n\n- `res: Response` Response object\n\n  also fields inherit from aliyun runtime context\n\n- `credentials: AliyunContextCredentials`\n\n- `service: AliyunContextService`\n\n- `requestId: string`\n\n- `accountId: string`\n\n- `function: AliyunContextFunction`\n\n- `region: string`\n\n  and short hands for ctx.res\n\n- `setHeader(field: string, value: string): void`\n\n- `removeHeader(field: string): void`\n\n- `get header: Headers`\n\n- `get headers: Headers`\n\n- `get status: number`\n\n- `set status(code: number): void`\n\n- `get body: any`\n\n- `set body(value: any): void`\n\n### WrappedContext.Request\n\nsee [request.ts](src/request.ts)\n\n### WrappedContext.Response\n\nsee [response.ts](src/response.ts)\n\n## Example\n\nSimply set the context's body, it will automatically send the response with proper status and header\n\n```js\nconst { wrapper } = require(\"aliyun-serverless-wrapper\")\n\nexports.someFunction = wrapper(async (ctx) =\u003e {\n  ctx.body = { hello: \"world\" }\n})\n\n/* response shall be\n\n  HTTP/1.1 200 OK\n  Content-Type: application/json\n\n  { \"hello\": \"world\" }\n\n*/\n\nexports.someFunction = wrapper(async (ctx) =\u003e {\n  ctx.body = \"hello world\"\n})\n\n/* response shall be\n\n  HTTP/1.1 200 OK\n  Content-Type: text/plain\n\n  \"hello world\"\n\n*/\n\nexports.someFunction = wrapper(async (ctx) =\u003e {\n  ctx.body = \"\u003chtml\u003e\u003ch1\u003ehello wordl\u003c/h1\u003e\u003c/html\u003e\"\n})\n\n/* response shall be\n\n  HTTP/1.1 200 OK\n  Content-Type: text/html\n\n  \"\u003chtml\u003e\u003ch1\u003ehello wordl\u003c/h1\u003e\u003c/html\u003e\"\n\n*/\n```\n\nOr throw Error\n\n```js\nexports.someFunction = wrapper(async (ctx) =\u003e {\n  throw new Error(\"oops\")\n})\n\n/* response shall be\n\n  HTTP/1.1 500 Internal Error\n\n*/\n\nexports.someFunction = wrapper(\n  async (ctx) =\u003e {\n    await checkAuth(ctx.req) // and this will throw a Error\n  },\n  {\n    onError: (e, ctx) =\u003e {\n      ctx.status = 401\n      ctx.body = { errorMessage: e.message }\n    }\n  }\n)\n\n/* response shall be\n\n  HTTP/1.1 404 Not Found\n  Content-Type: application/json\n\n  { \"errorMessage\": \"Not Authorized\" }\n\n*/\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzephyrpersonal%2Faliyun-serverless-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzephyrpersonal%2Faliyun-serverless-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzephyrpersonal%2Faliyun-serverless-wrapper/lists"}