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

mage-sdk-node

v1.0.0

Published

Mage client for Node.js

Downloads

4

Readme

Mage Client

This is a MAGE client written in pure ES6, it should be compatible with Node 5.x and should also run in the browser if built through a tool like Babel.

Installation

npm i mage-sdk-node --save

Requirements

At least NodeJS 6.x

Usage

Promise-based

const Mage = require('mage-sdk-node')
const config = require('./my-mage-config')


const client = new Mage(config)

// mage commands are now promises so they can be chained
client.user.register()
    .then(() => {
        // do game/app logic
    })
    .catch(error => {
        console.error(error);
        process.exit(1);
    });

Async/Await

If async/await is available, the code above can be rewritten as follow:

const Mage = require('mage-sdk-node')
const config = require('./my-mage-config')

try {
    // create the mage client
    const client = new Mage(config)

    // register a guest user and join a game
    await client.user.register();

    const game = await client.game.join();
    doSomething(game);
} catch (error) {
    console.error(error);
}

Configuration

You will need to extract your client configuration from your mage project, to do so create a javascript file at the root of your project (ie. config.js) with the following content:

const mage = require('./lib');
console.log(JSON.stringify(mage.getClientConfig('api'), null, 4));

Then run it with node, a JSON object should get output in your console, this is your node config and can be used to instanciate the Mage class.

It could for example be saved in a file called config/production.js in your client project using this format:

module.exports = {
    /* json configuration */
}

That way you can easily require it in your project.

API

Mage (EventEmitter)

When the mage client finish initializing, you should be able to run client.{module}.{command} for each user command set on the server, those calls will return a promise.

constructor(config: MageClientConfig)

The Mage client config can be extracted from your server by running the command described above.

get archivist

The archivist instance.

get commands

Returns the Command Center.

get session

Returns the current session.

set session

Sets the current session, must be a Session object.

get stream

Returns the message stream.

close() -> void

Closes all connections to the server.

{module}.{command}(any ...params) -> Promise(any)

Wrappers for CommandCenter.send.

Archivist (EventEmitter)

get cache

Returns the underlying cache (defaults to Memory).

exists(string topic, Object index[, Object options]) -> Promise(boolean)

Checks if the given value exists.

get(string topic, Object index[, Object options]) -> Promise(any)

Retrieve a value from the server, unless cached locally.

list(string topic, Object partialIndex[, Object options]) -> Promise(any[])

List objects on the server using a partial index.

mget(Object|Object[] queries[, Object options]) -> Promise(Object|any[])

Run multiple gets on the server in one go.

event {topic}(string operation, Object index[, any value])

Whenether set, applyDiff or del operations are run, emit an event for the topic.

CommandCenter

set cookies

Set a cookie jar for use with the command center.

registerHook(string name, Function hook)

Register a hook with the command center, when hooks are called they should return an object that will then be set as a header to the current call.

unregisterHook(string name)

Remove a hook.

send(string cmdName, any[] params) -> Promise(any)

Call the command center, returns a promise and queue the command, the promise only resolves once the queue goes through.

License

MIT