superagent-protobuf
Version:
Adds protobuf.js encoding and decoding support to superagent
99 lines (67 loc) • 2.42 kB
Markdown
> Adds [protobuf.js](https://github.com/dcodeIO/protobuf.js) encoding and
> decoding support to [superagent](http://visionmedia.github.io/superagent/)
[](https://travis-ci.com/bbridges/superagent-protobuf)
[](https://coveralls.io/github/bbridges/superagent-protobuf?branch=master)
[](https://www.npmjs.com/package/superagent-protobuf)
*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.
```js
// 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:
```js
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:
```js
// 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:
```js
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:
```js
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`).