npm package: @league-of-legends-devs/instabuilds
Description: Official NPM module for the website https://instabuilds.gg
Author: Ilshidur
Latest version: 0.0.10
Deprecated: Discontinued

# instabuilds

> Official NPM module for the website https://instabuilds.gg<br/>
Works server-side & browser-side.

## Installation

`npm install --save @league-of-legends-devs/instabuilds`

## TODO

- Replace Axios with lightweight client
- Docs

- *Methods*
  - `auth.authenticate()`
  - `auth.logout()`
  - `itemSets.upload()`
  - `itemSets.findById()`
  - `itemSets.findBySlug()`
  - `itemSets.edit()`
  - `itemSets.delete()`
  - `itemSets.rate()`
  - `itemSets.hash()`
  - `_setDefaultItemSetDownloadRoute()`
  - `download.link.itemSet()`
  - `users.create()`
  - `users.me()`
  - `users.findById()`
  - `search.itemSets()`
  - `generations.getAll()`
  - `generations.getLast()`
  - `generations.findById()`
  - `static.getChampions()`
  - `static.getItems()`
  - `static.getRoles()`
  - `static.getMaps()`
  - `static.getRegions()`
  - `static.getLastPatchVersion()`
  - `news.getLast()`
  - `thirdPartyCode.getOne()`
  - `recaptcha.getSiteKey()`
  - `startListeners()`
  - `stopListeners()`
  - `isAuthenticated()`
- *Events*
  - [ ] `events.onDownloadBuildRequest()`
  - [ ] `events.onNewGeneration()`
* *Constants*
  - `CHAMPION_ROLE`
  - `ITEM_SET_TYPE`

## Examples

```javascript
const InstaBuilds = require('@league-of-legends-devs/instabuilds');

const client = new InstaBuilds('Application name');
// Using an API auth key
await client.auth.authenticateWithKey('AUTH_KEY');
// or ...
// Credentials that will be used to retrieve a JSON Web Token internally
await client.auth.authenticate('USERNAME', 'PASSWORD');

// Using ReCAPTCHA :
const reCaptchaSiteKey = await client.recaptcha.getSiteKey();
// => Use it to render a ReCAPTCHA checkbox in a login form, then use the obtained auth token to authenticate

// Register a user (ReCAPTCHA auth token is required)
await client.users.create('RECAPTCHA_AUTH_TOKEN', {
  email: 'bestteemona@riotgames.com',
  region: 'NA',
  login: 'BestTeemoNA',
  password: 'SHR0om$_',
  thirdPartyCode: 'YOLO',
});

const patchVersion = await client.static.getLastPatchVersion();

// Upload an item set to the website
const itemSet = {
  title: 'My dank Ahri waifu build',
  champion: { id: 103 },
  role: { id: InstaBuilds.CHAMPION_ROLE.MIDDLE },
  maps: [11]
  comment: 'Easy build',
  content: {
    blocks: [{
      type: 'First block'
      items: [
        { id: 4004, count: 2 }
      ]
    }]
  }
};
const uploadedItemSet = await client.itemSets.upload(itemSet);

// Listen to the Web Sockets events sent from the server
// This step is required in order to use the 'client.onMessage()' event listeners
client.startListeners();

// Listen to real time events using Web Sockets, like actions on the website
client.events.onDownloadRequest(data => {
  // data.itemSets
  // The user called a download from the website
});
```

## API (TODO: documentation in active redaction)

### `InstaBuilds.init(applicationName [, opts])`

> Initialize and returns a new client.

Params :
* `applicationName`: _`String`_. The name of your application (e.g.: "My InstaBuilds client").
* `opts`: _optional `Object`_. Options (details coming soon).

Returns :
* the new `client`

### `client.login(authKey)` or `client.login(userName, password)`

> Log in to the https://instabuilds.gg server with either a auth key or an username & password.
This step is required for some features related to user accounts.<br/>
`client.login()` also starts a micro web sockets client ensuring the server that this client is alive and that it can send signals to it.

Returns :
* a `Promise` :
  * _`Object`_. `null` if failed authentication.
    * `text`: _`String`_. The server response.

### `client.logout()`

> Log out the client. The same client can log in again with the `client.login()` function.<br/>
As `client.login()` starts a micro web sockets client ensuring the server that this client is alive, this function stops it.

Returns :
* a `Promise` :
  * _`Object`_. `null` if the client failed to log out.
    * `text`: _`String`_. The server response.

### `client._setDefaultItemSetsDownloadRoute` (undocumented)

> Override the `/downloads/sets-from-lib` to `/downloads/sets-from-app`. Must be undocumented.

### `client.startListeners()`

> Start the Web Socket listeners, which are not activated by default.
This step is required in order to use the `client.onMessage()` event listeners.

### `client.stopListeners()`

> Stop the Web Socket listeners. These can be restarted later using `client.startListeners()`.

### **TODO: Rewrite** `client.get(data, [, params], callback)`

> Requests some datas to the server.

Params :

* `data`: _String_. The type of data to request. Refer to the "Available `data` inputs" section below.
* `params`: _optional `Object`_. Parameters to give to the data request. Depends on the `data` value.
* `callback`: _Optional callback function_. Executed when the server sent a response. Follows the current schema : `function(err, response) {}`. If not set, the `get()` method returns a `Promise`.
    * `err`: _`Error`_. `null` if successful request
    * `response`: _`Object`_. The retrieved informations, depending on the `data` value. `null` if failed request.

Available `data` inputs ; each different value will have its own associated response (in the `response` argument of the callback) :

* `'patchVersion'`: Gets the current patch version of the server.
    * `response`: _`Object`_.
        * `version`: _`String`_. Patch version (format : 'X.X.X') of the latest item sets generation. This value can differ from the official patch version, as the server takes a little time to generate the datas for the current patch.
        * `generationDate`: _`Date`_. Date of the latest item sets generation.
* `'news'`: Gets the "news" section : announcements, warnings ...
    * `response`: _`Object`_.
        * `text`: _`String`_. The content of the "news" section.
        * `date`: _`Date`_. Date of broadcast.
* `'itemSets'`: Gets a list of every item sets in a generation. This counts as a download.
    * `params`: _`Object`_.<br/>
        * `id`: _optional `String`_. The id of the item sets generaton (format : 'XXXXXXXXXXXXXXXXXXXXXXXX', see [the sets page](https://instabuilds.gg/sets)). If `null`, get the latest generation.
    * `response`: _`Object`_.
        * `date`: _`Date`_. The generation date of the item sets.
        * `itemSets`: _`[Object]`_. List of the item sets, as JSON objects.
* `'itemSet'`: Gets a single item set. This counts as a download.
    * `params`: _`Object`_.<br/>
        * Either :
            * `generationId`: _`String`_. The id of the generation (format : 'XXXXXXXXXXXXXXXXXXXXXXXX', see [the sets page](https://instabuilds.gg/sets)).
            * `itemSetNumber`: _`String` or `Number`_<br/>
        * Or :
            * `generationId`: _optional `String`_. The id of the generation (format : 'XXXXXXXXXXXXXXXXXXXXXXXX', see [the sets page](https://instabuilds.gg/sets)). If `null`, get the latest generation.
            * `championKey`: _`String`_. The "key" of the champion (e.g.: "MonkeyKing" for _Wukong_, "AurelionSol" for _Aurelion Sol_).
    * `response`: _`Object`_.
        * `date`: _`Date`_. The generation date of the item set.
        * `itemSet`: _`Object`_. The item set, as JSON object.
        * `type`: _`String`_. The item sets type. Values : `'generated-riot'`, `'generated-probuilts'`, `'custom'`.
        * `author`: _`String`_. The author's name of this item set. `null` if generated by the server.
* `'userDatas'`: Gets informations of the current logged in user.
    * `response`: _`Object`_.
        * `username`: _`String`_. User name.
        * `lastLogin`: _`Date`_. Date of last login.
        * `itemSets`: _`[Object]]`_. List of the user item sets, as JSON objects. Contains all the item sets of the user : generated and custom builds.

Returns :
* if no `callback` provided, returns a `Promise` :
  * _`Object`_. The retrieved informations, depending on the `data` value.

### `client.downloadItemSets(data, [, params], callback)`

> // TODO

### `client.send(data, [, params], callback)`

> // TODO

### `client.onMessage(data, [, params], action)`

> // TODO

### `client.on(data, action)`

> // TODO

### `client.submitBug(data, callback)`

Params :
* `data`: _Object_.
    * `platform`: _`Integer`_. ID of the platform where the bug is located ('Website' : 1, 'Application' : 2 ...).
    * `title`: _`String`_. Short description of the bug.
    * `content`: _`String`_. Detailed description of the bug.
    * `sendAdditionalDatas`: _`Boolean`_. Set to `true` to send additional datas to help fix the bug (operation system, default browser and screen resolution).
* `callback`: _Callback function_. Executed when the server sent a response. Follows the current schema : `function(err) {}`. If not set, the `submitBug()` method returns a `Promise`.
    * `err`:  _`Error`_. `null` if failed to submit the bug.

Returns :
* if no `callback` provided, returns a `Promise`.

## Privacy and conditions

By using this module, you agree to the [terms and conditions](https://instabuilds.gg/terms) and [privacy policy ](https://instabuilds.gg/privacy) of https://instabuilds.gg.

## License

The MIT License (MIT)

Copyright (c) 2018 **Nicolas COUTIN**

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Anyway, if you earn money on my open source work, I will fucking end you :)