Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow grpc-web service #14

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = function(api) {
// https://babeljs.io/docs/en/config-files#apicache
api.cache.forever()
return {
presets: ["@babel/preset-env", "@babel/preset-flow"]
presets: ["@babel/preset-env", "@babel/preset-flow"],
plugins: [
"@babel/plugin-proposal-class-properties"
]
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/annotations.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/annotations.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/empty_message_no_service.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/empty_message_no_service.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/enum_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/enum_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/map_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/map_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/oneof_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/oneof_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/parent_message_v2.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/parent_message_v2.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/parent_message_v3.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/parent_message_v3.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/primitive_message_v2.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/primitive_message_v2.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/primitive_message_v3.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/primitive_message_v3.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/repeated_primitive_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/repeated_primitive_message.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/reserved_words.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/reserved_words.proto

3 changes: 3 additions & 0 deletions examples/flow/generated/proto/examplecom/simple_pb_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package: examplecom
// file: proto/examplecom/simple.proto

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
// package: examplecom
// file: proto/examplecom/simple.proto

255 changes: 255 additions & 0 deletions examples/flow/generated/proto/examplecom/simple_service_pb_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
// package: examplecom
// file: proto/examplecom/simple_service.proto

var proto_examplecom_simple_service_pb = require("../../proto/examplecom/simple_service_pb");
var proto_othercom_external_child_message_pb = require("../../proto/othercom/external_child_message_pb");
var google_protobuf_empty_pb = require("google-protobuf/google/protobuf/empty_pb");
var grpc = require("grpc-web-client").grpc;

var SimpleService = (function () {
function SimpleService() {}
SimpleService.serviceName = "examplecom.SimpleService";
return SimpleService;
}());

SimpleService.DoUnary = {
methodName: "DoUnary",
service: SimpleService,
requestStream: false,
responseStream: false,
requestType: proto_examplecom_simple_service_pb.UnaryRequest,
responseType: proto_othercom_external_child_message_pb.ExternalChildMessage
};

SimpleService.DoServerStream = {
methodName: "DoServerStream",
service: SimpleService,
requestStream: false,
responseStream: true,
requestType: proto_examplecom_simple_service_pb.StreamRequest,
responseType: proto_othercom_external_child_message_pb.ExternalChildMessage
};

SimpleService.DoClientStream = {
methodName: "DoClientStream",
service: SimpleService,
requestStream: true,
responseStream: false,
requestType: proto_examplecom_simple_service_pb.StreamRequest,
responseType: google_protobuf_empty_pb.Empty
};

SimpleService.DoBidiStream = {
methodName: "DoBidiStream",
service: SimpleService,
requestStream: true,
responseStream: true,
requestType: proto_examplecom_simple_service_pb.StreamRequest,
responseType: proto_othercom_external_child_message_pb.ExternalChildMessage
};

SimpleService.Delete = {
methodName: "Delete",
service: SimpleService,
requestStream: false,
responseStream: false,
requestType: proto_examplecom_simple_service_pb.UnaryRequest,
responseType: proto_examplecom_simple_service_pb.UnaryResponse
};

exports.SimpleService = SimpleService;

function SimpleServiceClient(serviceHost, options) {
this.serviceHost = serviceHost;
this.options = options || {};
}

SimpleServiceClient.prototype.doUnary = function doUnary(requestMessage, metadata, callback) {
if (arguments.length === 2) {
callback = arguments[1];
}
var client = grpc.unary(SimpleService.DoUnary, {
request: requestMessage,
host: this.serviceHost,
metadata: metadata,
transport: this.options.transport,
debug: this.options.debug,
onEnd: function (response) {
if (callback) {
if (response.status !== grpc.Code.OK) {
var err = new Error(response.statusMessage);
err.code = response.status;
err.metadata = response.trailers;
callback(err, null);
} else {
callback(null, response.message);
}
}
}
});
return {
cancel: function () {
callback = null;
client.close();
}
};
};

SimpleServiceClient.prototype.doServerStream = function doServerStream(requestMessage, metadata) {
var listeners = {
data: [],
end: [],
status: []
};
var client = grpc.invoke(SimpleService.DoServerStream, {
request: requestMessage,
host: this.serviceHost,
metadata: metadata,
transport: this.options.transport,
debug: this.options.debug,
onMessage: function (responseMessage) {
listeners.data.forEach(function (handler) {
handler(responseMessage);
});
},
onEnd: function (status, statusMessage, trailers) {
listeners.end.forEach(function (handler) {
handler();
});
listeners.status.forEach(function (handler) {
handler({ code: status, details: statusMessage, metadata: trailers });
});
listeners = null;
}
});
return {
on: function (type, handler) {
listeners[type].push(handler);
return this;
},
cancel: function () {
listeners = null;
client.close();
}
};
};

SimpleServiceClient.prototype.doClientStream = function doClientStream(metadata) {
var listeners = {
end: [],
status: []
};
var client = grpc.client(SimpleService.DoClientStream, {
host: this.serviceHost,
metadata: metadata,
transport: this.options.transport
});
client.onEnd(function (status, statusMessage, trailers) {
listeners.end.forEach(function (handler) {
handler();
});
listeners.status.forEach(function (handler) {
handler({ code: status, details: statusMessage, metadata: trailers });
});
listeners = null;
});
return {
on: function (type, handler) {
listeners[type].push(handler);
return this;
},
write: function (requestMessage) {
if (!client.started) {
client.start(metadata);
}
client.send(requestMessage);
return this;
},
end: function () {
client.finishSend();
},
cancel: function () {
listeners = null;
client.close();
}
};
};

SimpleServiceClient.prototype.doBidiStream = function doBidiStream(metadata) {
var listeners = {
data: [],
end: [],
status: []
};
var client = grpc.client(SimpleService.DoBidiStream, {
host: this.serviceHost,
metadata: metadata,
transport: this.options.transport
});
client.onEnd(function (status, statusMessage, trailers) {
listeners.end.forEach(function (handler) {
handler();
});
listeners.status.forEach(function (handler) {
handler({ code: status, details: statusMessage, metadata: trailers });
});
listeners = null;
});
client.onMessage(function (message) {
listeners.data.forEach(function (handler) {
handler(message);
})
});
client.start(metadata);
return {
on: function (type, handler) {
listeners[type].push(handler);
return this;
},
write: function (requestMessage) {
client.send(requestMessage);
return this;
},
end: function () {
client.finishSend();
},
cancel: function () {
listeners = null;
client.close();
}
};
};

SimpleServiceClient.prototype.delete = function pb_delete(requestMessage, metadata, callback) {
if (arguments.length === 2) {
callback = arguments[1];
}
var client = grpc.unary(SimpleService.Delete, {
request: requestMessage,
host: this.serviceHost,
metadata: metadata,
transport: this.options.transport,
debug: this.options.debug,
onEnd: function (response) {
if (callback) {
if (response.status !== grpc.Code.OK) {
var err = new Error(response.statusMessage);
err.code = response.status;
err.metadata = response.trailers;
callback(err, null);
} else {
callback(null, response.message);
}
}
}
});
return {
cancel: function () {
callback = null;
client.close();
}
};
};

exports.SimpleServiceClient = SimpleServiceClient;

Loading