Adds protobuf.js encoding and decoding support to superagent
Supports both superagent and supertest on Node.js.
Install using npm
:
$ npm install superagent-protobuf
superagent-protobuf
adds the proto(...)
and sendProto(...)
methods onto
an existing superagent or supertest object.
// Using require(...).
const request = require('superagent');
require('superagent-protobuf')(request);
// Using ES6 imports.
import request from 'superagent';
import addProtobuf from 'superagent-protobuf';
addProtobuf(request);
The same methods above work with supertest as well:
const request = require('supertest');
require('superagent-protobuf')(request);
Decodes a protobuf message with a specified message type.
Note that this method sets the Accept
header to application/x-protobuf
.
Example usage:
// Send a request and expect a MessageType protobuf.js message back.
let res = await request.get('.../some-endpoint')
.proto(MessageType);
// The resulting message is stored in the body property.
console.log(res.body);
Encodes a protobuf message with the Content-Type
application/x-protobuf
.
Example usage:
let res = await request.post('.../some-endpoint')
.sendProto(MessageType.create({
my_field: 1
}));
This can also be combined with request.proto(...)
to receive a protobuf
message as well:
let outbound = MessageType.create({
my_field: 1
});
// Sending a MessageType message and receiving an AnotherMessageType
// message.
let res = await request.post('.../some-endpoint')
.sendProto(outbound)
.proto(AnotherMessageType);
let inbound = res.body;
// Printing out the message.
console.log(inbound);
Released under the MIT License (see LICENSE
).