npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

soracom

v0.0.3

Published

SORACOM API client for Node.js

Downloads

60

Readme

soracom

SORACOM API client for Node.js.

Install

$ npm install soracom

Usage

Authenticate with a pair of email and password, then retrieve your operator information.

var Soracom = require('soracom');
var soracom = new Soracom({ email: 'email', password: 'password' });
soracom.post('/auth', function(err, res, auth) {
  if (!error) {
  	console.log(auth); // {apiKey: "api_key", token: "token", operatorId: "operator_id"}
  	soracom.defaults(auth);
    soracom.get('/operators/:operatorId', function(err, res, operator) {});
  }
});

You can also specify your credentials directly on instantiation.

var Soracom = require('soracom');
var soracom = new Soracom({
  apiKey: 'api_key',
  token: 'token',
  operatorId: 'operator_id'
});
soracom.get('/operators/:operatorId', function(err, res, operator) {})

The given apiKey and token are automatically set to HTTP header on subsequent requests. The rest of properties, in the above case operatorId, are used as any of default request path, query or request body.

Examples

All examples below assume that you already set the following credentials to the soracom object.

var Soracom = require('soracom');
soracom = new Soracom();
soracom.defaults({
  apiKey: 'api_key',
  token: 'token',
  operatorId: 'operator_id'
});

Operator

Get operator.

soracom.get('/operators/:operatorId', function(err, res, body) {});

Create operator.

soracom.post('/operators', function(err, res, body) {});

Subscriber

List subscribers with speed class s1.minimum.

soracom.get('/subscribers', { speed_class_filter: 's1.minimum' }, function(err, res, body) {});

Get subscriber.

soracom.get('/subscriber/:imsi', { ismi: '123456789012345' }, function(err, res, body) {});

Activate subscriber.

soracom.post('/subscribers/:imsi/activate', { imsi: '123456789012345' }, function(err, res, body) {});

Deactivate subscriber.

soracom.post('/subscribers/:imsi/deactivate', { imsi: '123456789012345' }, function(err, res, body) {});

Insert tags to subscriber.

Note if the type of request body is an array, you cannot pass any other properties (i.e. imsi) on the same request. To workaround this, you need to call soracom.defaults({ imsi: '123456789012345'}) beforehand, or you can just call function with static path soracom.put('/subscribers/123456789012345/tags')).

soracom.defaults({ imsi: '123456789012345' });
soracom.put('/subscribers/:imsi/tags', [{ tagName: 'foo', tagValue: 'bar'}], function(err, res, body) {});

Delete tag from subscriber.

soracom.delete('/subscribers/:imsi/tags/:tagName', { imsi: '123456789012345', tagName: 'foo' }, function(err, res, body) {});

API

var soracom = new Soracom(obj);

Create a Soracom instance that can be used to make reqeusts to SORACOM's APIs.

If authenticating with user account, obj should look like:

new Soracom({ email: '[email protected]', password: 'superStrongP@ssw0rd' });

If authenticating with credentials, obj should look like:

new Soracom({ apiKey: 'api_key', token: 'very_long_string_token' });

soracom.defaults(obj);

Set given object properties to either default http headers or default request path, query or request body.

If the key of property is either apiKey or token, then its value is set to default headers and deleted from original object. The headers are mapped like below:

headers = {
  'X-Soracom-API-Key': apiKey,
  'X-Soracom-Token': token
};

soracom.get(path, [params], callback);

GET any of the REST API endpints.

path

The endpoint to hit. When path contains special tag starts with :, then that tag should be treated as variable. If there is property with same name as that variable existed in the given param, then the tag is replaced with that value.

Example 1:

var soracom = new Soracom({ apiKey: 'api_key', token: 'token', operatorId: 'OP1234567890' });
soracom.get('/operators/:operatorId', callback);
// The operatorId is exracted from default params, so the endpoint will be "/operators/OP1234567890"

Example 2:

var soracom = new Soracom({ apiKey: 'api_key', token: 'token' });
soracom.get('/subscriber/:imsi', { imsi: '123456789012345' }, callback);
// The imsi is exracted from given params in a same function call, so the endpoint will be "/subscribers/123456789012345"

params

(Optional) parameters for request.

callback

The callback argument gets 3 arguments function (error, response, body):

  • error: An error object when applicable
  • response: An http.IncomingMessage object
  • body: JSON.parse()ed response body

soracom.post(path, [params], callback);

Same as get(), but set method to POST.

soracom.put(path, [params], callback);

Same as get(), but set method to PUT.

soracom.delete(path, [params], callback);

Same as get(), but set method to DELETE.

Testing

Currently we have two test scripts, helper.js and e2e.js. helper.js is an unit test for lib/helper.js while e2e.js is an integration test using lib/soracom.js to execute real API access. By default e2e.js is disabled (it's simply marked as skip by mocha).

To run the unit test:

$ npm test

You can also run integration test by modifying the line in e2e.js.

describe.skip('e2e', function() { // remove .skip from this line.

Then add your accout info to test/account.json.

$ echo '{"email": "email_address", "password": "password"}' > test/account.json

Finally run the test command, same as unit test.

$ npm test

License

MIT