@themainstack/communication 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +194 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,194 @@
1
+ # @themainstack/communication
2
+
3
+ A unified gRPC framework for inter-service communication at Mainstack.
4
+
5
+ ## Table of Contents
6
+ 1. [Overview](#overview)
7
+ 2. [Installation](#installation)
8
+ 3. [Proto Generation](#proto-generation)
9
+ 4. [Creating a gRPC Server](#creating-a-grpc-server)
10
+ 5. [Creating a gRPC Client](#creating-a-grpc-client)
11
+ 6. [Error Handling](#error-handling)
12
+ 7. [Full Example](#full-example)
13
+
14
+ ---
15
+
16
+ ## Overview
17
+
18
+ The workflow is simple:
19
+
20
+ ```
21
+ Developer writes TypeScript function
22
+
23
+ Package auto-generates .proto file
24
+
25
+ Server Factory exposes function as gRPC
26
+
27
+ Client Factory in another service calls it
28
+
29
+ Error Handler normalizes any errors
30
+ ```
31
+
32
+ ---
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ npm install @themainstack/communication
38
+ # or
39
+ yarn add @themainstack/communication
40
+ ```
41
+
42
+ ---
43
+
44
+ ## Proto Generation
45
+
46
+ Auto-generate `.proto` files from your TypeScript types.
47
+
48
+ ```typescript
49
+ import { generateProtoFromMethods } from '@themainstack/communication';
50
+
51
+ generateProtoFromMethods([
52
+ {
53
+ name: 'CalculateFee',
54
+ requestSample: () => ({ merchantId: '', amount: 0, currency: '' }),
55
+ responseSample: () => ({ fee: 0, total: 0 }),
56
+ }
57
+ ], {
58
+ packageName: 'fee.v1',
59
+ serviceName: 'FeeService',
60
+ outputDir: './src/grpc',
61
+ });
62
+ ```
63
+
64
+ **Output:** `./src/grpc/feeservice.proto` is auto-generated!
65
+
66
+ ---
67
+
68
+ ## Creating a gRPC Server
69
+
70
+ Expose existing functions as gRPC endpoints.
71
+
72
+ ```typescript
73
+ import { GrpcServerFactory } from '@themainstack/communication';
74
+
75
+ // Your existing business function
76
+ async function calculateFee(request) {
77
+ return { fee: request.amount * 0.02, total: request.amount * 1.02 };
78
+ }
79
+
80
+ // Create and start the server
81
+ const server = await GrpcServerFactory.createServer({
82
+ packageName: 'fee.v1',
83
+ serviceName: 'FeeService',
84
+ port: 50053,
85
+ }, [
86
+ {
87
+ name: 'CalculateFee',
88
+ handler: calculateFee, // Your existing function!
89
+ requestSample: () => ({ merchantId: '', amount: 0, currency: '' }),
90
+ responseSample: () => ({ fee: 0, total: 0 }),
91
+ }
92
+ ]);
93
+
94
+ await server.start();
95
+ // 🚀 gRPC Server running on 0.0.0.0:50053
96
+ ```
97
+
98
+ ### Quick Expose (One-liner)
99
+
100
+ ```typescript
101
+ import { exposeAsGrpc } from '@themainstack/communication';
102
+
103
+ const server = await exposeAsGrpc(
104
+ 'CalculateFee',
105
+ calculateFee,
106
+ { requestSample: () => ({...}), responseSample: () => ({...}) },
107
+ { packageName: 'fee.v1', serviceName: 'FeeService', port: 50053 }
108
+ );
109
+ ```
110
+
111
+ ---
112
+
113
+ ## Creating a gRPC Client
114
+
115
+ Call gRPC services from other services.
116
+
117
+ ```typescript
118
+ import { GrpcClientFactory } from '@themainstack/communication';
119
+
120
+ const client = GrpcClientFactory.createClient({
121
+ serviceName: 'FeeService',
122
+ packageName: 'fee.v1',
123
+ protoPath: './src/grpc/fee.proto',
124
+ url: process.env.FEE_SERVICE_URL || 'localhost:50053',
125
+ });
126
+
127
+ // Make a call
128
+ client.CalculateFee(
129
+ { merchantId: 'merchant_123', amount: 1000, currency: 'USD' },
130
+ (err, response) => {
131
+ if (err) {
132
+ handleGrpcError(err);
133
+ return;
134
+ }
135
+ console.log('Fee:', response.fee);
136
+ }
137
+ );
138
+ ```
139
+
140
+ ---
141
+
142
+ ## Error Handling
143
+
144
+ Standardized gRPC error translation.
145
+
146
+ ```typescript
147
+ import { handleGrpcError } from '@themainstack/communication';
148
+
149
+ client.SomeMethod(request, (err, response) => {
150
+ if (err) {
151
+ try {
152
+ handleGrpcError(err); // Throws normalized error
153
+ } catch (normalizedError) {
154
+ console.error(normalizedError.message);
155
+ // Handle based on error type
156
+ }
157
+ return;
158
+ }
159
+ // Process response
160
+ });
161
+ ```
162
+
163
+ ---
164
+
165
+ ## Full Example
166
+
167
+ See `examples/full-grpc-demo.ts` in the repository for a complete working example.
168
+
169
+ ---
170
+
171
+ ## Environment Variables
172
+
173
+ | Variable | Description | Default |
174
+ |----------|-------------|---------|
175
+ | `GRPC_PORT` | Port for gRPC server | `50051` |
176
+ | `FEE_SERVICE_URL` | Fee service gRPC address | `localhost:50053` |
177
+
178
+ ---
179
+
180
+ ## API Reference
181
+
182
+ ### Proto Generation
183
+ - `generateProtoFromMethods(methods, options)` - Generate proto with service definition
184
+ - `generateProtoFromFunction(fn, name)` - Generate proto for a single message
185
+
186
+ ### Server
187
+ - `GrpcServerFactory.createServer(options, handlers)` - Create a gRPC server
188
+ - `exposeAsGrpc(name, handler, samples, options)` - Quick one-liner
189
+
190
+ ### Client
191
+ - `GrpcClientFactory.createClient(options)` - Create a gRPC client
192
+
193
+ ### Error Handling
194
+ - `handleGrpcError(err)` - Translate gRPC errors to application errors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@themainstack/communication",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "private": false,
5
5
  "description": "Unified gRPC framework for inter-service communication - auto-generates protos, creates servers, and provides type-safe clients",
6
6
  "main": "dist/index.js",