From 7163c1dc6740af916c74b07831f3ddd38ba55939 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sun, 14 Apr 2024 19:01:25 +0200 Subject: [PATCH] update documentation --- npmextra.json | 17 ++++- package.json | 12 +++- readme.hints.md | 1 + readme.md | 174 ++++++++++++++++++++++++++++++++++++++++-------- tsconfig.json | 12 ++-- 5 files changed, 179 insertions(+), 37 deletions(-) create mode 100644 readme.hints.md diff --git a/npmextra.json b/npmextra.json index 0c0bdc8..733066f 100644 --- a/npmextra.json +++ b/npmextra.json @@ -2,17 +2,28 @@ "gitzone": { "projectType": "npm", "module": { - "githost": "gitlab.com", + "githost": "code.foss.global", "gitscope": "api.global", "gitrepo": "typedsocket", - "description": "a typedrequest extension supporting websockets", + "description": "A library for creating typed WebSocket connections, supporting bi-directional communication with type safety.", "npmPackagename": "@api.global/typedsocket", "license": "MIT", - "projectDomain": "api.global" + "projectDomain": "api.global", + "keywords": [ + "WebSocket", + "Type Safety", + "Real-time Communication", + "Client-Server Architecture", + "TypeScript", + "Networking" + ] } }, "npmci": { "npmGlobalTools": [], "npmAccessLevel": "public" + }, + "tsdoc": { + "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n" } } \ No newline at end of file diff --git a/package.json b/package.json index e2bb15e..c352263 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@api.global/typedsocket", "version": "3.0.0", "private": false, - "description": "a typedrequest extension supporting websockets", + "description": "A library for creating typed WebSocket connections, supporting bi-directional communication with type safety.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "type": "module", @@ -46,5 +46,13 @@ "cli.js", "npmextra.json", "readme.md" + ], + "keywords": [ + "WebSocket", + "Type Safety", + "Real-time Communication", + "Client-Server Architecture", + "TypeScript", + "Networking" ] -} +} \ No newline at end of file diff --git a/readme.hints.md b/readme.hints.md new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/readme.hints.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/readme.md b/readme.md index 0535b48..2aef3b0 100644 --- a/readme.md +++ b/readme.md @@ -1,41 +1,159 @@ -# @apiglobal/typedsocket +# @api.global/typedsocket a typedrequest extension supporting websockets -## Availabililty and Links -* [npmjs.org (npm package)](https://www.npmjs.com/package/@apiglobal/typedsocket) -* [gitlab.com (source)](https://gitlab.com/apiglobal/typedsocket) -* [github.com (source mirror)](https://github.com/apiglobal/typedsocket) -* [docs (typedoc)](https://apiglobal.gitlab.io/typedsocket/) +## Install +To install `@api.global/typedsocket` in your project, run the following command using npm: -## Status for master +```bash +npm install @api.global/typedsocket --save +``` -Status Category | Status Badge --- | -- -GitLab Pipelines | [![pipeline status](https://gitlab.com/apiglobal/typedsocket/badges/master/pipeline.svg)](https://lossless.cloud) -GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/apiglobal/typedsocket/badges/master/coverage.svg)](https://lossless.cloud) -npm | [![npm downloads per month](https://badgen.net/npm/dy/@apiglobal/typedsocket)](https://lossless.cloud) -Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/apiglobal/typedsocket)](https://lossless.cloud) -TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud) -node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) -Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud) -PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@apiglobal/typedsocket)](https://lossless.cloud) -PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@apiglobal/typedsocket)](https://lossless.cloud) -BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@apiglobal/typedsocket)](https://lossless.cloud) +For Yarn users: + +```bash +yarn add @api.global/typedsocket +``` ## Usage -Use TypeScript for best in class intellisense. +To utilize `@api.global/typedsocket` effectively, let's explore how to integrate it into a project with TypeScript. This guide provides comprehensive usage scenarios, highlighting all features of the module. `@api.global/typedsocket` extends the capabilities of `typedrequest` by integrating websocket support, allowing for real-time, bidirectional communication between a client and server. -TypedSocket is the typedrequest you love, just in a bidirectional way. +### Getting Started -See the following examples from our test for reference: +#### Prerequisites -## Contribution +Before diving into examples, ensure you have the following: -We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) +- A basic understanding of TypeScript and Node.js. +- An existing project set up with TypeScript. +- `@api.global/typedrequest` and its dependencies installed in your project. -For further information read the linked docs at the top of this readme. +#### Initialization -## Legal -> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc) -| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) +First, let's import the required modules and initialize our server and client instances. + +**Server Side:** + +Within your server-side code, import and set up `TypedSocket` to create a websocket server. + +```typescript +import { TypedSocket } from '@api.global/typedsocket'; +import * as typedrequest from '@api.global/typedrequest'; + +// Initialize your TypedRouter +const typedRouter = new typedrequest.TypedRouter(); + +// Create the TypedSocket server +const server = await TypedSocket.createServer(typedRouter); + +// Optionally, you can provide a smartexpressServerArg if you have an existing SmartExpress server +``` + +**Client Side:** + +On the client side, initialize `TypedSocket` to connect to the websocket server. + +```typescript +import { TypedSocket } from '@api.global/typedsocket'; + +// Assuming server is at http://localhost:3000 +const client = await TypedSocket.createClient(typedRouter, 'http://localhost:3000'); +``` + +### Defining Typed Requests + +`@api.global/typedsocket` leverages `typedrequest` for type-safe requests and responses. Here's how you can define and handle typed requests: + +**Defining a Typed Request Interface:** + +Define an interface for the request and its corresponding response. + +```typescript +interface ExampleRequest extends typedrequestInterfaces.ITypedRequest { + method: 'exampleMethod'; + request: { + message: string; + }; + response: { + reply: string; + }; +} +``` + +**Handling Requests on the Server:** + +Add a typed handler for the request in your `TypedRouter`. + +```typescript +typedRouter.addTypedHandler( + new typedrequest.TypedHandler('exampleMethod', async (requestData) => { + return { + reply: `Server received: ${requestData.message}`, + }; + }) +); +``` + +### Sending Requests from the Client + +Use the client instance to send a request to the server and await a response. + +```typescript +const exampleRequest = client.createTypedRequest('exampleMethod'); +const response = await exampleRequest.fire({ + message: 'Hello from client!', +}); + +console.log(response.reply); // "Server received: Hello from client!" +``` + +### Advanced Usage + +#### Tagging Connections + +For more refined control, especially in scenarios with multiple clients, you can tag connections for targeted communication. + +```typescript +// Add a tag to a client +client.addTag('role', 'admin'); + +// On the server, find connections with the tag +const adminConnections = await server.findAllTargetConnectionsByTag('role', 'admin'); + +// Send a message to all admin connections +for (const adminConnection of adminConnections) { + const request = server.createTypedRequest('exampleMethod', adminConnection); + await request.fire({ + message: 'Message to admins', + }); +} +``` + +#### Handling Reconnections + +`TypedSocket` automatically attempts to reconnect in the event of connection loss. Ensure your server and client logic accounts for reconnection, especially for maintaining state or re-subscribing to necessary events. + +### Conclusion + +Implementing `@api.global/typedsocket` provides a type-safe, real-time communication layer for your TypeScript applications, extending the functionality of `typedrequest` into the realm of websockets. This guide covered initialization, defining typed requests, sending requests, and advanced usage patterns like tagging connections and handling reconnections. + +Integrating `@api.global/typedsocket` into your project harnesses the power of websockets with the benefits of type safety and structured request/response patterns, enabling more reliable and maintainable real-time applications. + +## License and Legal Information + +This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. + +**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. + +### Trademarks + +This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH. + +### Company Information + +Task Venture Capital GmbH +Registered at District court Bremen HRB 35230 HB, Germany + +For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. + +By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works. diff --git a/tsconfig.json b/tsconfig.json index 0e331cc..dfe5a55 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,8 +3,12 @@ "experimentalDecorators": true, "useDefineForClassFields": false, "target": "ES2022", - "module": "ES2022", - "moduleResolution": "nodenext", - "esModuleInterop": true - } + "module": "NodeNext", + "moduleResolution": "NodeNext", + "esModuleInterop": true, + "verbatimModuleSyntax": true + }, + "exclude": [ + "dist_*/**/*.d.ts" + ] }