-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
861 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.turbo | ||
src | ||
tsconfig.json | ||
rollup.config.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
NestJS's Dynamic Module for Client. | ||
|
||
# Install | ||
|
||
```sh | ||
npm install --save @grpc.ts/nestjs-client | ||
|
||
# or | ||
|
||
yarn add @grpc.ts/nestjs-client | ||
|
||
# or | ||
|
||
pnpm add @grpc.ts/nestjs-client | ||
``` | ||
|
||
# Usage | ||
|
||
```proto | ||
// example.proto | ||
syntax = "proto3"; | ||
import "google/protobuf/timestamp.proto"; | ||
package example.v1; | ||
message Message { | ||
string message = 1; | ||
google.protobuf.Timestamp created_at = 2; | ||
} | ||
message SendMessageRequest { | ||
string message = 1; | ||
google.protobuf.Timestamp created_at = 2; | ||
} | ||
message GetMessageResponse { Message message = 1; } | ||
service ExampleService { | ||
rpc SendMessage(SendMessageRequest) returns (GetMessageResponse); | ||
} | ||
``` | ||
|
||
In NestJS | ||
|
||
```ts | ||
// client.module.ts | ||
import { Module } from '@nestjs/common'; | ||
import { GrpcClient } from '@grpc.ts/nestjs-client'; | ||
|
||
import { ClientController } from './client.controller'; | ||
|
||
@Module({ | ||
imports: [ | ||
GrpcClient.register([ | ||
{ | ||
url: 'localhost:3010', | ||
package: [ | ||
{ | ||
packageName: 'example.v1', | ||
protoPath: '../proto/example.proto', | ||
}, | ||
{ | ||
packageName: 'example2.v1', | ||
protoPath: '../proto/example2.proto', | ||
}, | ||
{ | ||
protoPath: '../proto/example3.proto', | ||
}, | ||
], | ||
packageDefinitionOptions: { | ||
oneofs: true, | ||
longs: String, | ||
enums: String, | ||
defaults: true, | ||
}, | ||
options: { | ||
keepaliveTimeMs: 5_000, | ||
}, | ||
}, | ||
]), | ||
], | ||
controllers: [ClientController], | ||
}) | ||
export class ClientModule {} | ||
``` | ||
|
||
```ts | ||
// client.controller.ts | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { | ||
GrpcService, | ||
createMetadata, | ||
dateToGrpcTimestamp, | ||
} from '@grpc.ts/nestjs-client'; | ||
|
||
@Controller('/client') | ||
export class ClientController { | ||
constructor( | ||
@GrpcService({ | ||
serviceName: 'ExampleService', | ||
}) | ||
private readonly exampleService: any, | ||
) {} | ||
|
||
@Get() | ||
public async sendMessage(): Promise<string> { | ||
this.exampleService | ||
.sendMessage( | ||
{ message: 'hello', createdAt: dateToGrpcTimestamp(new Date()) }, | ||
createMetadata({ | ||
meta: 'test', | ||
}), | ||
) | ||
.then((data) => { | ||
console.log(data); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
|
||
return 'Ok!'; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@grpc.ts/nestjs-client", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"directories": { | ||
"lib": "lib" | ||
}, | ||
"author": "Alpha", | ||
"description": "NestJS package for client", | ||
"homepage": "https://github.com/zgid123/grpc-ts", | ||
"keywords": [ | ||
"grpc", | ||
"grpc-ts", | ||
"grpc-js", | ||
"grpc-typescript", | ||
"grpc-javascript", | ||
"grpc-nodejs", | ||
"grpc-nestjs", | ||
"grpc-client" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/zgid123/grpc-ts" | ||
}, | ||
"main": "./lib/index.cjs", | ||
"module": "./lib/index.mjs", | ||
"types": "./lib/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./lib/index.mjs", | ||
"require": "./lib/index.cjs", | ||
"types": "./lib/index.d.ts" | ||
} | ||
}, | ||
"scripts": { | ||
"prepublish": "pnpm build", | ||
"build": "rollup --config rollup.config.ts --configPlugin typescript" | ||
}, | ||
"dependencies": { | ||
"@grpc.ts/core": "workspace:*", | ||
"@nestjs/common": "^9.4.0" | ||
}, | ||
"devDependencies": { | ||
"@grpc.ts/client-commons": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { defineConfig } from 'rollup'; | ||
import json from '@rollup/plugin-json'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import resolve from '@rollup/plugin-node-resolve'; | ||
import typescript from '@rollup/plugin-typescript'; | ||
|
||
export default defineConfig({ | ||
input: 'src/index.ts', | ||
plugins: [json(), resolve(), commonjs(), typescript()], | ||
external: ['@nestjs/common', '@grpc.ts/core'], | ||
output: [ | ||
{ | ||
file: './lib/index.cjs', | ||
format: 'cjs', | ||
}, | ||
{ | ||
file: './lib/index.mjs', | ||
format: 'es', | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createClients } from '@grpc.ts/client-commons'; | ||
import { | ||
Module, | ||
type Provider, | ||
type DynamicModule, | ||
type OnApplicationShutdown, | ||
} from '@nestjs/common'; | ||
|
||
import type { | ||
IGrpcClientProps, | ||
IGrpcClientListProps, | ||
} from '@grpc.ts/core/lib/interface'; | ||
|
||
import { normalizePattern } from './utils'; | ||
|
||
@Module({}) | ||
export class GrpcClient { | ||
public static async register( | ||
options: IGrpcClientProps | IGrpcClientProps[], | ||
): Promise<DynamicModule> { | ||
const clients = await createClients(options); | ||
|
||
const providers = this._createProviders(clients); | ||
|
||
return { | ||
module: GrpcClient, | ||
providers, | ||
exports: providers, | ||
}; | ||
} | ||
|
||
protected static _createProviders(clients: IGrpcClientListProps): Provider[] { | ||
return Object.entries(clients).reduce<Provider[]>( | ||
(result, [clientName, clientWrapper]) => { | ||
Object.entries(clientWrapper.getPackages()).forEach( | ||
([packageName, serviceClientWrapper]) => { | ||
Object.entries(serviceClientWrapper).forEach( | ||
([serviceName, serviceClient]) => { | ||
( | ||
serviceClient as unknown as OnApplicationShutdown | ||
).onApplicationShutdown = serviceClient.close; | ||
|
||
result.push({ | ||
provide: normalizePattern({ | ||
serviceName, | ||
clientName: clientName || '', | ||
packageName: packageName || '', | ||
}), | ||
useValue: serviceClient, | ||
}); | ||
}, | ||
); | ||
}, | ||
); | ||
|
||
return result; | ||
}, | ||
[], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Inject } from '@nestjs/common'; | ||
|
||
import { normalizePattern } from './utils'; | ||
|
||
import type { IGrpcServiceProps } from './interface'; | ||
|
||
export const GrpcService = (props: IGrpcServiceProps) => { | ||
return Inject(normalizePattern(props)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export { | ||
Metadata, | ||
createMetadata, | ||
grpcTimestampToDate, | ||
dateToGrpcTimestamp, | ||
} from '@grpc.ts/core'; | ||
|
||
export * from './decorators'; | ||
export * from './GrpcClient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface IGrpcServiceProps { | ||
clientName?: string; | ||
serviceName: string; | ||
packageName?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { IGrpcServiceProps } from './interface'; | ||
|
||
function combine(separator: string, ...data: string[]): string { | ||
return data.filter((s) => !!s).join(separator); | ||
} | ||
|
||
export function normalizePattern({ | ||
serviceName, | ||
clientName = '', | ||
packageName = '', | ||
}: IGrpcServiceProps): string { | ||
return combine( | ||
'::', | ||
'grpc', | ||
combine('::', clientName, packageName), | ||
serviceName, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "./src", | ||
"outDir": "./lib", | ||
}, | ||
"include": [ | ||
"./src" | ||
], | ||
"exclude": [ | ||
"./lib", | ||
"./node_modules" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.turbo | ||
src | ||
tsconfig.json | ||
rollup.config.ts |
Oops, something went wrong.