{"id":16314240,"url":"https://github.com/rustamwin/rpc-controllers","last_synced_at":"2025-03-20T22:30:28.120Z","repository":{"id":34044725,"uuid":"165205874","full_name":"rustamwin/rpc-controllers","owner":"rustamwin","description":"Use class-based controllers to create JSON-RPC 2.0 server usage in Express / Koa and TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-11T22:45:07.000Z","size":716,"stargazers_count":10,"open_issues_count":28,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-11T21:53:51.322Z","etag":null,"topics":["decorators","express","json-rpc","koa","rpc","rpc-controllers","rpc-framework","rpc-server","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/rustamwin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":"rustamwin","open_collective":null,"ko_fi":null,"tidelift":null,"custom":null}},"created_at":"2019-01-11T08:17:09.000Z","updated_at":"2023-04-22T17:10:46.000Z","dependencies_parsed_at":"2023-01-15T04:11:10.569Z","dependency_job_id":null,"html_url":"https://github.com/rustamwin/rpc-controllers","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Frpc-controllers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Frpc-controllers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Frpc-controllers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Frpc-controllers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rustamwin","download_url":"https://codeload.github.com/rustamwin/rpc-controllers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221809108,"owners_count":16883856,"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":["decorators","express","json-rpc","koa","rpc","rpc-controllers","rpc-framework","rpc-server","typescript"],"created_at":"2024-10-10T21:53:28.617Z","updated_at":"2024-10-28T08:45:15.898Z","avatar_url":"https://github.com/rustamwin.png","language":"TypeScript","funding_links":["https://patreon.com/rustamwin"],"categories":[],"sub_categories":[],"readme":"# rpc-controllers\n\n[![Build Status](https://travis-ci.org/rustamwin/rpc-controllers.svg?branch=master)](https://travis-ci.org/rustamwin/rpc-controllers)\n[![npm version](https://badge.fury.io/js/rpc-controllers.svg)](https://badge.fury.io/js/rpc-controllers)\n[![Dependency Status](https://david-dm.org/rustamwin/rpc-controllers.svg)](https://david-dm.org/rustamwin/rpc-controllers)\n\nAllows to create controller classes with methods as JSON-RPC methods that handle [JSON-RPC 2.0][3] requests.\nYou can use rpc-controllers with [express.js][1] or [koa.js][2].\n\n# Table of contents\n\n * [Installation](#installation)\n * [Example of usage](#example-of-usage)\n * [More examples](#more-usage-examples)\n \n \n## Installation\n\n1. Install module:\n\n    `npm install rpc-controllers --save`\n\n2. `reflect-metadata` shim is required:\n\n    `npm install reflect-metadata --save`\n\n    and make sure to import it before you use rpc-controllers:\n\n    ```typescript\n    import \"reflect-metadata\";\n    ```\n\n3. Install framework:\n\n    **a. If you want to use rpc-controllers with *express.js*, then install it and all required dependencies:**\n\n    `npm install express body-parser --save`\n\n    Optionally you can also install their typings:\n\n    `npm install @types/express @types/body-parser --save`\n\n    **b. If you want to use rpc-controllers with *koa.js*, then install it and all required dependencies:**\n\n    `npm install koa koa-router koa-bodyparser --save`\n\n    Optionally you can also install their typings:\n\n    `npm install @types/koa @types/koa-router @types/koa-bodyparser --save`\n\n4. Its important to set these options in `tsconfig.json` file of your project:\n\n    ```json\n    {\n     \"emitDecoratorMetadata\": true,\n     \"experimentalDecorators\": true\n    }\n    ```\n    \n## Example of usage\n\n1. Create a file `MathController.ts`\n\n    ```typescript\n    import {Controller, Method, Params} from \"rpc-controllers\";\n    \n    @Controller(\"math\")\n    export class MathController {\n    \n        @Method(\"add\")\n        add(@Params() params: Array\u003cnumber\u003e) {\n            return params.reduce((prev, curr) =\u003e prev + curr);\n        }\n    \n        @Method(\"test\")\n        test(@Params() params: any[]) {\n            return params;\n        }\n    }\n    ```\n    \n    This class will register JSON-PRC methods specified in method decorators in your server framework (express.js or koa).\n    \n2. Create a file `app.ts`\n\n    ```typescript\n    import \"reflect-metadata\"; // don't forget import this shim!\n    import {createExpressServer} from \"rpc-controllers\";\n    import {MathController} from \"./MathController\";\n\n    // creates express app, registers all controller methods and returns you express app instance\n    const app = createExpressServer({\n       controllers: [MathController] // we specify controllers we want to use\n    });\n\n    // run express application on port 3000\n    app.listen(3000);\n    ```\n    \u003e if you are koa user you just need to use `createKoaServer` instead of `createExpressServer`\n    \n3. Send a JSON-RPC 2.0 request to `http://localhost:3000` using the method `math.add` and the params `[1, 1]`. You will get the result `2`.\n\n## More usage examples\n\n#### Load all controllers from the given directory\n\n```typescript\nimport \"reflect-metadata\"; // don't forget import this shim!\nimport {createExpressServer} from \"rpc-controllers\";\n\nconst app = createExpressServer({\n  controllers: [__dirname + \"/controllers/*.js\"] // registers all given controllers\n});\n\n// run express application on port 3000\napp.listen(3000);\n```\n\n#### Pre-configure express/koa\n\nIf you have, or if you want to create and configure express app separately,\nyou can use `useExpressServer` instead of `createExpressServer` function:\n\n```typescript\nimport \"reflect-metadata\";\nimport {useExpressServer} from \"rpc-controllers\";\nimport {MathController} from \"./MathController\";\n\nlet express = require(\"express\"); // or you can import it if you have installed typings\nlet app = express(); // your created express server\n// app.use() // you can configure it the way you want\nuseExpressServer(app, { // register created express server in rpc-controllers\n    controllers: [MathController] // and configure it the way you need (controllers, validation, etc.)\n});\napp.listen(3000); // run your express server\n```\n\n\u003e Note: Koa driver is experimental\n\n#### Using DI container\n\n`rpc-controllers` supports a DI container out of the box.\nYou can inject your services into your controllers. Container must be setup during application bootstrap.\nHere is example how to integrate rpc-controllers with [typedi][4]:\n\n```typescript\nimport \"reflect-metadata\";\nimport {createExpressServer, useContainer} from \"rpc-controllers\";\nimport {Container} from \"typedi\";\n\n// its important to set container before any operation you do with rpc-controllers,\n// including importing controllers\nuseContainer(Container);\n\n// create and run express server\nlet app = createExpressServer(3000, {\n    controllers: [__dirname + \"/controllers/*.js\"],\n});\n```\n\nThat's it, now you can inject your services into your controllers:\n\n```typescript\n@Controller()\nexport class MessageController {\n\n    constructor(private messageRepository: MessageRepository) {\n    }\n\n    // ... controller methods\n\n}\n```\n\n[1]: http://expressjs.com/\n[2]: http://koajs.com/\n[3]: https://www.jsonrpc.org/specification\n[4]: https://github.com/typestack/typedi\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frustamwin%2Frpc-controllers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frustamwin%2Frpc-controllers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frustamwin%2Frpc-controllers/lists"}