RICADO Gen 4 API JS Client

RICADO Gen 4 API JS Client

source

Controllers/TokenController.js

/**
 * File Auto-Generated by the RICADO Gen 4 PHP API Project
 * 
 * Do Not Edit this File Manually!
 */

import RequestHelper from '../RequestHelper';
import TokenModel from '../Models/TokenModel';

/**
 * Controller Class for Tokens
 * 
 * @class
 */
class TokenController
{
    /**
     * Generate a new Token [POST /token/new]
     * 
     * This method is used to generate a new JWT for User or API Key based authentication. You can think of this as the 'Login Process'.
     * 
     * **User Authentication**
     * The user must have an account that has been created on RICADO. A valid Email Address and Password will be required.
     * 
     * **API Key Authentication**
     * A valid API Key and API Secret will be required. These can be created in the RICADO Admin App at admin.ricado.co.nz.
     * 
     * @static
     * @public
     * @param {{email: string, password: string, providerId: string}|{key: string, secret: string, providerId: string}} requestData The Request Data
     * @return {Promise<{token: string, expires: number}>}
     */
    static create(requestData)
    {
        return new Promise((resolve, reject) => {
            RequestHelper.postRequest(`/token/new`, requestData)
            .then((result) => {
                let resolveValue = (function(){
                    let resultObject = {};
                    
                    if(typeof result === 'object' && 'token' in result)
                    {
                        resultObject.token = (function(){
                            if(typeof result.token !== 'string')
                            {
                                return String(result.token);
                            }
                
                            return result.token;
                        }());
                    }
                    else
                    {
                        resultObject.token = "";
                    }
                    
                    if(typeof result === 'object' && 'expires' in result)
                    {
                        resultObject.expires = (function(){
                            if(typeof result.expires !== 'number')
                            {
                                return Number.isInteger(Number(result.expires)) ? Number(result.expires) : Math.floor(Number(result.expires));
                            }
                
                            return Number.isInteger(result.expires) ? result.expires : Math.floor(result.expires);
                        }());
                    }
                    else
                    {
                        resultObject.expires = 0;
                    }
                
                    return resultObject;
                }());
                
                resolve(resolveValue);
            })
            .catch(error => reject(error));
        });
    }

    /**
     * Unlock the current Token [POST /token/unlock]
     * 
     * This method is used to unlock an existing JWT. Tokens can be locked by the server or via an API call.
     * The user's pin code or password is used as authentication to unlock the token.
     * 
     * @static
     * @public
     * @param {TokenController.PinCodeUnlock|TokenController.PasswordUnlock} requestData The Request Data
     * @return {Promise<boolean>}
     */
    static unlock(requestData)
    {
        return new Promise((resolve, reject) => {
            RequestHelper.postRequest(`/token/unlock`, requestData)
            .then((result) => {
                resolve(result ?? true);
            })
            .catch(error => reject(error));
        });
    }

    /**
     * Lock the current Token [POST /token/lock]
     * 
     * This method is used to lock an existing JWT. Once the Token has been locked, any further API calls will be denied until the Token has been unlocked.
     * 
     * @static
     * @public
     * @return {Promise<boolean>}
     */
    static lock()
    {
        return new Promise((resolve, reject) => {
            RequestHelper.postRequest(`/token/lock`)
            .then((result) => {
                resolve(result ?? true);
            })
            .catch(error => reject(error));
        });
    }

    /**
     * Destroy the current Token [POST /token/destroy]
     * 
     * This method is used to destroy an existing JWT. This method should be used to effectively 'logout' a User or API Key.
     * Once this method has been successfully called, the JWT can no longer be used for any API calls.
     * 
     * @static
     * @public
     * @return {Promise<boolean>}
     */
    static destroy()
    {
        return new Promise((resolve, reject) => {
            RequestHelper.postRequest(`/token/destroy`)
            .then((result) => {
                resolve(result ?? true);
            })
            .catch(error => reject(error));
        });
    }

    /**
     * Retrieve the current Token [GET /token]
     * 
     * Retrieves the Token that is currently being used.
     * 
     * @static
     * @public
     * @return {Promise<TokenModel>}
     */
    static getCurrent()
    {
        return new Promise((resolve, reject) => {
            RequestHelper.getRequest(`/token`)
            .then((result) => {
                let resolveValue = (function(){
                    return TokenModel.fromJSON(result);
                }());
                
                resolve(resolveValue);
            })
            .catch(error => reject(error));
        });
    }

    /**
     * Retrieve a Token [GET /tokens/{id}]
     * 
     * @static
     * @public
     * @param {string} id The Token ID
     * @return {Promise<TokenModel>}
     */
    static getOne(id)
    {
        return new Promise((resolve, reject) => {
            RequestHelper.getRequest(`/tokens/${id}`)
            .then((result) => {
                let resolveValue = (function(){
                    return TokenModel.fromJSON(result);
                }());
                
                resolve(resolveValue);
            })
            .catch(error => reject(error));
        });
    }

    /**
     * Delete a Token [DELETE /tokens/{id}]
     * 
     * @static
     * @public
     * @param {string} id The Token ID
     * @return {Promise<boolean>}
     */
    static delete(id)
    {
        return new Promise((resolve, reject) => {
            RequestHelper.deleteRequest(`/tokens/${id}`)
            .then((result) => {
                resolve(result ?? true);
            })
            .catch(error => reject(error));
        });
    }

    /**
     * List all Tokens [GET /tokens]
     * 
     * @static
     * @public
     * @param {TokenController.GetAllQueryParameters} [queryParameters] The Optional Query Parameters
     * @return {Promise<TokenModel[]>}
     */
    static getAll(queryParameters = {})
    {
        return new Promise((resolve, reject) => {
            RequestHelper.getRequest(`/tokens`, queryParameters)
            .then((result) => {
                let resolveValue = (function(){
                    if(Array.isArray(result) !== true)
                    {
                        return [];
                    }
                
                    return result.map((resultItem) => {
                        return (function(){
                            return TokenModel.fromJSON(resultItem);
                        }());
                    });
                }());
                
                resolve(resolveValue);
            })
            .catch(error => reject(error));
        });
    }
}

export default TokenController;

/**
 * The Optional Query Parameters for the getAll Function
 * 
 * @typedef {Object} TokenController.GetAllQueryParameters
 * @property {string} [accountId] The Account this Token belongs to
 * @property {string} [accountType] The Account Type
 * @property {Date} [issueTimestamp] When the Token was issued
 * @property {Date} [expireTimestamp] When the Token will expire
 * @property {?Date} [activityTimestamp] When the last API call using this Token was made
 * @memberof Controllers
 */

/**
 * A **PinCodeUnlock** Type
 * 
 * @typedef {Object} TokenController.PinCodeUnlock
 * @property {string} pinCode The User's Pin Code
 * @memberof Controllers
 */

/**
 * A **PasswordUnlock** Type
 * 
 * @typedef {Object} TokenController.PasswordUnlock
 * @property {string} password The User's Password
 * @memberof Controllers
 */