Skip to content

Commit

Permalink
split up rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Dec 9, 2021
1 parent eecc65a commit 0095e51
Show file tree
Hide file tree
Showing 6 changed files with 1,314 additions and 1,299 deletions.
52 changes: 32 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const path = require("path");
const ts = require("typescript");
const type = require("./type");
const descriptor = require("./descriptor");
const rpc = require("./rpc");
const node = require("./rpc/node");
const server = require("./rpc/server");


function createImport(identifier, moduleSpecifier) {
return ts.factory.createImportDeclaration(
Expand All @@ -20,27 +22,28 @@ function replaceExtension(filename, extension = ".ts") {
}

/**
* @typedef {{ unary_rpc_promise: boolean, grpc_package: string }} ConfigParameters
* @typedef {{ unary_rpc_promise: boolean, grpc_package: string, target: "node" | "web" }} ConfigParameters
*/

/**
* @param {string | undefined | null} parameters
* @return{ConfigParameters}
* @returns {ConfigParameters}
*/
function parseParameters(parameters) {
/** @type{ConfigParameters} */
/** @type {ConfigParameters} */
const defaultValues = {
unary_rpc_promise: false,
grpc_package: "@grpc/grpc-js",
target: "node"
};

/** @type{{ [K keyof ConfigParameters]: (value: string) => ConfigParameters[K] }} */
/** @type {{ [K keyof ConfigParameters]: (value: string) => ConfigParameters[K] }} */
const parsers = {
unary_rpc_promise: (value) => value === "true",
grpc_package: (value) => value,
};

/** @type{Partial<ConfigParameters>} */
/** @type {Partial<ConfigParameters>} */
const inputParams = {};

// comma separated
Expand Down Expand Up @@ -76,6 +79,7 @@ for (const fileDescriptor of request.proto_file) {
const name = replaceExtension(fileDescriptor.name);
const pbIdentifier = ts.factory.createUniqueName("pb");
const grpcIdentifier = ts.factory.createUniqueName("grpc");
const grpcWeb = ts.factory.createUniqueName("web");

// Will keep track of import statements
const importStatements = [];
Expand Down Expand Up @@ -116,34 +120,42 @@ for (const fileDescriptor of request.proto_file) {
configParams.grpc_package,
)
);
statements.push(
...rpc.createGrpcInterfaceType(fileDescriptor, grpcIdentifier, configParams)
);

if (configParams.target != "web") {
statements.push(
...node.createServiceInterface(grpcIdentifier)
);
}

// Create all services and clients
for (const serviceDescriptor of fileDescriptor.service) {
statements.push(
rpc.createUnimplementedServer(
server.createUnimplementedService(
fileDescriptor,
serviceDescriptor,
grpcIdentifier
)
);
statements.push(
rpc.createServiceClient(
fileDescriptor,
serviceDescriptor,
grpcIdentifier,
configParams,
)
);
if (configParams.target == "web") {

} else {
statements.push(
node.createServiceClient(
fileDescriptor,
serviceDescriptor,
grpcIdentifier,
configParams,
)
);
}
}
}

const {major, minor, patch} = request.compiler_version || {major: 0, minor: 0, patch: 0};
const { major, minor, patch } = request.compiler_version || { major: 0, minor: 0, patch: 0 };

const doNotEditComment = ts.factory.createJSDocComment(
`Generated by the protoc-gen-ts. DO NOT EDIT!\n` +
`compiler version: ${major}.${minor}.${patch}\n` +
`compiler version: ${major}.${minor}.${patch}\n` +
`source: ${fileDescriptor.name}\n` +
`git: https://github.com/thesayyn/protoc-gen-ts\n`
);
Expand Down
Loading

0 comments on commit 0095e51

Please sign in to comment.