{"id":13716047,"url":"https://github.com/zhaoyao91/micro-action","last_synced_at":"2026-06-12T11:32:03.041Z","repository":{"id":57296375,"uuid":"97094302","full_name":"zhaoyao91/micro-action","owner":"zhaoyao91","description":"Help define actions for micro.","archived":false,"fork":false,"pushed_at":"2017-07-18T06:05:00.000Z","size":92,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-28T14:09:15.979Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/zhaoyao91.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-07-13T07:39:19.000Z","updated_at":"2019-07-05T19:21:44.000Z","dependencies_parsed_at":"2022-09-07T03:20:52.262Z","dependency_job_id":null,"html_url":"https://github.com/zhaoyao91/micro-action","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zhaoyao91/micro-action","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoyao91%2Fmicro-action","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoyao91%2Fmicro-action/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoyao91%2Fmicro-action/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoyao91%2Fmicro-action/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhaoyao91","download_url":"https://codeload.github.com/zhaoyao91/micro-action/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoyao91%2Fmicro-action/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34243051,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-12T02:00:06.859Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-08-03T00:01:06.488Z","updated_at":"2026-06-12T11:32:03.001Z","avatar_url":"https://github.com/zhaoyao91.png","language":"JavaScript","funding_links":[],"categories":["Modules"],"sub_categories":["Routing"],"readme":"# Micro Action\n\nTools to help build [Micro-Action][micro-action-protocol] server using [Zeit Micro][micro].\n\n## Usage\n\n### Install package\n\n```bash\nnpm i -S micro-action\n```\n\n### Basic usage\n\n```ecmascript 6\n// server.js\nconst {route} = require('micro-action')\n\nmodule.exports = route({\n  'add': ({a, b}) =\u003e a + b,\n  'add?async': async ({a, b}) =\u003e await Promise.resolve(a + b)\n})\n```\n\n```ecmascript 6\n// client.js\nconst fetch = require('node-fetch')\n\nconst res = await fetch('http://server-path', {\n  method: 'POST',\n  headers: {'content-type': 'application/json'},\n  body: JSON.stringify({cmd: 'add', input: {a: 1, b: 2}})\n})\nconst body = await res.json()\nconst sum = body.output // sum=3\n\nconst res = await fetch('http://server-path', {\n  method: 'POST',\n  headers: {'content-type': 'application/json'},\n  body: JSON.stringify({cmd: 'add?async', input: {a: 2, b: 3}})\n})\nconst body = await res.json()\nconst sum = body.output // sum=5\n```\n\n### Ok with code\n\nIf you have multiple possible success cases, use code to distinguish them.\n\n```ecmascript 6\n// server.js\nconst {route, ok, fail} = require('micro-action')\n\nmodule.exports = {\n  'magicNumber': (number) =\u003e {\n    if (number % 2 === 0) return ok('even', number / 2)\n    else return ok('odd', number * 2)\n  },\n}\n```\n\n```ecmascript 6\n// client.js\nconst fetch = require('node-fetch')\n\nconst res = await fetch('http://server-path', {\n  method: 'POST',\n  headers: {'content-type': 'application/json'},\n  body: JSON.stringify({cmd: 'magicNumber', input: 4})\n})\nconst body = await res.json() // body1={ok: true, code:'even', output:2}\n\nconst res = await fetch('http://server-path', {\n  method: 'POST',\n  headers: {'content-type': 'application/json'},\n  body: JSON.stringify({cmd: 'magicNumber', input: 5})\n})\nconst body = await res.json() // body={ok: true, code:'odd', output:10}\n```\n\n### Fail with code\n\nYou should always give a code for known failure cases.\n\nThe output is optional and can be used for further details of this failure case.\n\n```ecmascript 6\n// server.js\nconst {route, ok, fail} = require('micro-action')\n\nmodule.exports = {\n  'divide': ({a, b}) =\u003e {\n    if (b === 0) return fail('zero-denominator', {msg: 'b cannot be 0'})\n    else return a / b\n  },\n}\n```\n\n```ecmascript 6\n// client.js\nconst fetch = require('node-fetch')\n\nconst res = await fetch('http://server-path', {\n  method: 'POST',\n  headers: {'content-type': 'application/json'},\n  body: JSON.stringify({cmd: 'divide', input: {a: 1, b: 2}})\n})\nconst body = await res.json() // body={ok: true, output: 0.5}\n\nconst res = await fetch('http://server-path', {\n  method: 'POST',\n  headers: {'content-type': 'application/json'},\n  body: JSON.stringify({cmd: 'divide', input: {a: 1, b: 0}})\n})\nconst body = await res.json() // body={ok: false, code:'zero-denominator', output: {msg: 'b cannot be 0'}}\n```\n\n### Uncatched error\n\nYou can ignore if you don't care the unknown errors. It will be caught and properly composed into response.\n \n```ecmascript 6\n// server.js\nconst {route, ok, fail} = require('micro-action')\n\nmodule.exports = {\n  'parse/jsonString': (json) =\u003e {\n    return JSON.parse(json)\n  },\n}\n```\n\n```ecmascript 6\n// client.js\nconst fetch = require('node-fetch')\n\nconst res = await fetch('http://server-path', {\n  method: 'POST',\n  headers: {'content-type': 'application/json'},\n  body: JSON.stringify({cmd: 'parse/jsonString', input: '{invalidJsonString'})\n})\nconst body = await res.json() // body1={ok: false, error: {name: 'SyntaxError', message: 'Unexpected token i in JSON at position 1'}}\n```\n## APIs\n\n#### route\n\nfunc(handlers, options) =\u003e [microRequestHandler][[micro-request-handler]]\n\n- handlers - object, key is cmd pattern, value is the handler.\n- handler - async func(input) =\u003e any | handlerResult\n- options\n  - errorLogger - async func(err)\n  - errorHandler - async func(err, res, input) =\u003e any | handlerResult\n  - otherRequestHandler - [microRequestHandler][[micro-request-handler]]\n  - unmatchedCmdHandler - async func(cmd, input) =\u003e any | handlerResult\n\n#### ok\n\nasync func(code, output) =\u003e handlerResult\n\n#### fail\n\nasync func(code, output, err) =\u003e handlerResult\n\n## Related Projects\n\n- [micro-action-protocol][micro-action-protocol]\n- [micro-action-callers][micro-action-callers]\n\n## License\n\nISC\n\n[micro-request-handler]: https://github.com/zeit/micro#microfn\n[micro]: https://github.com/zeit/micro\n[micro-action-protocol]: https://github.com/zhaoyao91/micro-action-protocol\n[micro-action-callers]: https://github.com/zhaoyao91/micro-action-callers","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhaoyao91%2Fmicro-action","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhaoyao91%2Fmicro-action","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhaoyao91%2Fmicro-action/lists"}