Skip to content

Commit

Permalink
[cli]: exports enum mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
zgid123 committed Mar 15, 2024
1 parent 9935df9 commit 53d6279
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 157 deletions.
4 changes: 2 additions & 2 deletions examples/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"@grpc.ts/fastify-server": "workspace:*",
"detect-port": "^1.5.1",
"fastify": "^4.26.2",
"vite": "^5.1.5"
"vite": "^5.1.6"
},
"devDependencies": {
"@fastify/vite": "^6.0.6",
"@grpc.ts/cli": "workspace:*",
"@grpc.ts/core": "workspace:^",
"@types/detect-port": "^1.3.5",
"fastify-cli": "^6.1.1",
"vite-node": "^1.3.1"
"vite-node": "^1.4.0"
}
}
14 changes: 14 additions & 0 deletions examples/fastify/src/protobufTypings/example.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

export const PACKAGE_NAME = 'example.v1';

export const messageEnum = {
unknown: 0,
info: 1,
};

export const messageEnumMapper = {
0: 'unknown',
1: 'info',
};

export type TMessageEnum = 'unknown' | 'info';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
Expand All @@ -16,6 +28,8 @@ export interface IGetMessageResponse {
message: IMessage;
}

export const EXAMPLE_SERVICE = 'ExampleService';

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
Expand Down
2 changes: 2 additions & 0 deletions examples/fastify/src/protobufTypings/example2.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface IGetMessageResponse {
message: IMessage;
}

export const EXAMPLE_SERVICE = 'ExampleService';

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
Expand Down
23 changes: 23 additions & 0 deletions examples/fastify/src/protobufTypings/example3.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface IGetMessageResponse {
message: IMessage;
}

export const EXAMPLE_SERVICE = 'ExampleService';

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
Expand All @@ -33,3 +35,24 @@ export interface IExampleService extends ServiceClient {
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}

export const ANOTHER_SERVICE = 'AnotherService';

export interface IAnotherService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
SendEmptyMessage(
params: {},
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendEmptyMessage(
params: {},
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
2 changes: 1 addition & 1 deletion examples/nestjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@nestjs/schematics": "^10.1.1",
"@types/detect-port": "^1.3.5",
"@types/express": "^4.17.21",
"@types/node": "20.11.25",
"@types/node": "20.11.28",
"source-map-support": "^0.5.21",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
Expand Down
12 changes: 12 additions & 0 deletions examples/nestjs/protobufTypings/example.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import type { Metadata, GrpcTimestamp, ServiceClient } from '@grpc.ts/core';

export const PACKAGE_NAME = 'example.v1';

export const messageEnum = {
unknown: 0,
info: 1,
};

export const messageEnumMapper = {
0: 'unknown',
1: 'info',
};

export type TMessageEnum = 'unknown' | 'info';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
Expand Down
5 changes: 5 additions & 0 deletions examples/proto/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import "google/protobuf/timestamp.proto";

package example.v1;

enum MessageEnum {
unknown = 0;
info = 1;
}

message Message {
string message = 1;
google.protobuf.Timestamp created_at = 2;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"rollup": "^4.12.1",
"rollup": "^4.13.0",
"turbo": "^1.12.5",
"typescript": "^5.4.2"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grpc.ts/cli",
"version": "1.0.2",
"version": "1.0.3",
"license": "MIT",
"directories": {
"lib": "lib"
Expand Down Expand Up @@ -46,7 +46,7 @@
},
"devDependencies": {
"@types/glob": "^8.1.0",
"@types/node": "^20.11.25",
"@types/node": "^20.11.28",
"@types/prettier": "^3.0.0"
}
}
9 changes: 7 additions & 2 deletions packages/cli/src/utils/contentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,30 @@ export function createExportEnums(
const [enumName, values] = enu;
cachedEnums[enumName] = true;

const valuesAsStrings = values.reduce<[string, string]>(
const valuesAsStrings = values.reduce<[string, string, string]>(
(result, vals) => {
Object.entries(vals).forEach(([k, v]) => {
const camelizedKey = camelize(k);

result[0] += `${camelizedKey}: ${v},\n`;
result[1] += `| '${camelizedKey}'\n`;
result[2] += `${v}: '${camelizedKey}',\n`;
});

return result;
},
['', ''],
['', '', ''],
);

content += `
export const ${lowerFirstChar(enumName)} = {
${valuesAsStrings[0]}
};
export const ${lowerFirstChar(enumName)}Mapper = {
${valuesAsStrings[2]}
};
export type T${enumName} = ${valuesAsStrings[1]};
`;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"build": "rollup --config rollup.config.ts --configPlugin typescript"
},
"dependencies": {
"@grpc/grpc-js": "^1.10.1",
"@grpc/grpc-js": "^1.10.3",
"@grpc/proto-loader": "^0.7.10",
"google-protobuf": "^3.21.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/fastify/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"devDependencies": {
"@grpc.ts/client-commons": "workspace:*",
"@types/node": "^20.11.25",
"@types/node": "^20.11.28",
"fastify": "^4.26.2",
"fastify-tsconfig": "^2.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/fastify/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"devDependencies": {
"@grpc.ts/server-commons": "workspace:*",
"@types/node": "^20.11.25",
"@types/node": "^20.11.28",
"fastify": "^4.26.2",
"fastify-tsconfig": "^2.0.0"
},
Expand Down
Loading

0 comments on commit 53d6279

Please sign in to comment.