{"id":18974979,"url":"https://github.com/bufferapp/micro-rpc","last_synced_at":"2025-04-19T16:44:16.389Z","repository":{"id":65840776,"uuid":"81604743","full_name":"bufferapp/micro-rpc","owner":"bufferapp","description":"Async RPC microservices made easy","archived":false,"fork":false,"pushed_at":"2019-10-23T21:18:28.000Z","size":21,"stargazers_count":8,"open_issues_count":2,"forks_count":4,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-10T20:51:03.127Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bufferapp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-10T20:38:16.000Z","updated_at":"2024-02-08T19:42:45.000Z","dependencies_parsed_at":"2023-02-13T14:10:12.048Z","dependency_job_id":null,"html_url":"https://github.com/bufferapp/micro-rpc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fmicro-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fmicro-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fmicro-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bufferapp%2Fmicro-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bufferapp","download_url":"https://codeload.github.com/bufferapp/micro-rpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249223960,"owners_count":21232840,"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":[],"created_at":"2024-11-08T15:16:52.433Z","updated_at":"2025-04-16T09:34:24.713Z","avatar_url":"https://github.com/bufferapp.png","language":"JavaScript","readme":"# micro-rpc\n\nAsync RPC microservices made easy\n\nMade with [Micro](https://github.com/zeit/micro)\n\n## Quickstart\n\nCreate a RPC method to add 2 numbers:\n\n```js\n// index.js\nconst { rpc, method, createError } = require('@bufferapp/micro-rpc');\nmodule.exports = rpc(\n  method('add', (a, b) =\u003e a + b)\n);\n```\n\nStart the server\n\n```sh\nmicro\n```\n\nUse the Micro RPC Client: https://github.com/bufferapp/micro-rpc-client to run the `add` method\n\n```js\nconst RPCClient = require('@bufferapp/micro-rpc-client');\n\nconst rpc = new RPCClient({\n  serverUrl: 'https://localhost:3000',\n});\n\nrpc.call('add', 2, 2)\n  .then(result =\u003e console.log(result)); // output: 4\n```\n\nOr you can use curl to call the `add` method:\n\n```sh\ncurl -H \"Content-Type: application/json\" -X POST -d '{\"name\": \"add\", \"args\": \"[2, 3]\"}' localhost:3000 | python -m json.tool\n\n# {\n#    \"result\": 5\n# }\n```\n\nTo see a list of all available methods use the `methods` call:\n\n```sh\ncurl -H \"Content-Type: application/json\" -X POST -d '{\"name\": \"methods\"}' localhost:3000 | python -m json.tool\n\n# {\n#   result: [\n#     {\n#       \"docs\": \"add two numbers\"\n#       \"name\": \"add\"\n#     },\n#     {\n#       \"docs\": \"list all available methods\",\n#       \"name\": \"methods\"\n#     }\n#   ]\n# }\n```\n\n## Usage\n\nHere's a few examples of how to hook up the handler methods:\n\n```js\nconst { rpc, method, createError } = require('@bufferapp/micro-rpc');\nmodule.exports = rpc(\n  method('add', (a, b) =\u003e a + b),\n  method('addAsync', (a, b) =\u003e new Promise((resolve) =\u003e {\n    resolve(a + b);\n  })),\n  method('addItems', ({ a, b }) =\u003e a + b),\n  method('addItemsAsync', ({ a, b }) =\u003e new Promise((resolve) =\u003e {\n    resolve(a + b);\n  })),\n  method('throwError', () =\u003e {\n    throw createError({ message: 'I\\'m sorry I can\\'t do that'});\n  }),\n\n  method('throwErrorAsync', () =\u003e new Promise((resolve, reject) =\u003e {\n    reject(createError({ message: 'Something is broke internally', statusCode: 500 }));\n  })),\n  method('documentation',\n  `\n  # documentation\n\n  Document what a method does.\n  `,\n  () =\u003e new Promise((resolve, reject) =\u003e {\n    reject(createError({ message: 'Something is broke internally', statusCode: 500 }));\n  }))\n);\n```\n\n## API\n\n### rpc\n\nAn async function that can be served by [Micro](https://github.com/zeit/micro), takes a bunch of methods as arguments.\n\n```js\nrpc(...methods)\n```\n\n**...methods** - _method_ - micro rpc method (see below)\n\n### method\n\nadd a remote method\n\n```js\nmethod(name, [docs], fn)\n```\n\n**name** - _string_ - the name of the method  \n**docs** - _string_ - documentation about a method  \n**fn** - _function_ - the function to call and apply parameters the method is requested\n\n### createError\n\ncreate an error to be thrown, optionally set the status code\n\n```js\ncreateError({ message, statusCode = 400})\n```\n\n**message** - _string_ - error message to return  \n**statusCode** - _string_ - optional HTTP status code (default to 400)\n\n## Request and Response Objects\n\nRequest and response objects are always passed along as the last two arguments in case they're needed.\n\n```js\nmethod('addWithSession', (a, b, req, res) =\u003e {\n  if (!req.session) {\n    throw createError({ message: 'a session is needed to add numbers', statusCode: 401});\n  }\n  return a + b;\n};\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbufferapp%2Fmicro-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbufferapp%2Fmicro-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbufferapp%2Fmicro-rpc/lists"}