npm package: @sagebox-be/proto-contracts
Description: Sagebox gRPC Protocol Buffer contracts and TypeScript definitions
Author: Sagebox Team
Latest version: 1.0.19

# @sagebox-be/proto-contracts

Sagebox gRPC Protocol Buffer contracts and TypeScript type definitions.

## 📦 Installation

```bash
npm install @sagebox-be/proto-contracts
# or
yarn add @sagebox-be/proto-contracts
# or
pnpm add @sagebox-be/proto-contracts
```

## 🚀 Usage

### Import TypeScript Types

```typescript
import { ProductServiceClient, ProductServiceController, GetProductByIdRequest, ProductResponse, getProtoPath, getAllProtosPaths } from '@sagebox-be/proto-contracts';
```

### Use in NestJS gRPC Microservice

```typescript
import { Module } from '@nestjs/common';
import { Transport, ClientsModule } from '@nestjs/microservices';
import { getProtoPath } from '@sagebox-be/proto-contracts';

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'PRODUCT_SERVICE',
        transport: Transport.GRPC,
        options: {
          package: 'product',
          protoPath: getProtoPath('product.proto'),
          url: 'localhost:5000',
        },
      },
    ]),
  ],
})
export class AppModule {}
```

### Use in Node.js gRPC Client

```typescript
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { getProtoPath } from '@sagebox-be/proto-contracts';
import type { ProductServiceClient } from '@sagebox-be/proto-contracts';

const PROTO_PATH = getProtoPath('product.proto');

const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
});

const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const productProto = protoDescriptor.product as any;

const client: ProductServiceClient = new productProto.ProductService('localhost:5000', grpc.credentials.createInsecure());

// Make gRPC calls
client.getProductById({ id: 'product-123' }, (error, response) => {
  if (error) {
    console.error(error);
  } else {
    console.log(response);
  }
});
```

### Use Proto Files Directly

```typescript
import { getAllProtosPaths } from '@sagebox-be/proto-contracts';

const protoPaths = getAllProtosPaths();
console.log(protoPaths.product); // Path to product.proto
```

## 📁 Package Contents

- **TypeScript Types**: Generated from proto files with full type safety
- **Proto Files**: Original `.proto` files for gRPC code generation in any language
- **Helper Functions**: Utilities to get proto file paths

## 🔧 Development

### Generate Types from Proto

```bash
npm run proto:generate
```

### Build Package

```bash
npm run build
```

### Publish

```bash
npm publish
```

## 📖 Available Services

### ProductService

#### Methods

- `GetProductsByCategoryId(GetProductsByCategoryIdRequest): GetProductsResponse`
- `GetProductById(GetProductByIdRequest): ProductResponse`
- `CreateProduct(CreateProductRequest): ProductResponse`

## 🤝 Contributing

When adding new proto files:

1. Add the `.proto` file to `proto/` directory
2. Run `npm run proto:generate` to generate TypeScript types
3. Export types in `src/index.ts`
4. Update `getAllProtosPaths()` in `src/utils.ts`
5. Build and publish

## 📝 License

MIT