1.0.18 • Published 6 years ago

super-matcher-sdk v1.0.18

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

Getting started

You must init the library before to use any other function. Exemple with ES2015 syntax:

import { init } from 'super-matcher-sdk';

init({
    host: 'http://localhost:8000 // Don't add a "/" at the end of the host
});

If you already have a refresh token (e.g from a cookie or the localstorage), you can also pass this value to your init call. The refresh token will be exchange before the first API call to obtain a new access token. This new access token will be internally store and use for each call until it expires.

import { init } from 'super-matcher-sdk';

init({
    host: 'http://localhost:8000,
    refreshToken: localStorage.getItem('refreshToken')
});

Error handling

The library uses fetch web api to consume Rest API. Every library function return a promise. If the http status code is in range [200, 300`[, the promise will be resolve and you will obtain the response body as the promise parameter. Otherwise, the promise is rejected and you will be able to handle your error in your way with the Response object. Exemple :

    import { signIn } from 'super-matcher-sdk';

    signIn('username', 'password')
    .then(res => {
        // Handle your sucessfull response
        // Here res is a SignInResponse object
    })
    .catch(res => {
        // Handle your failed call
        // Here res is a Response object

        // res.json() will return a promise of ErrorResponse parameter
    })

Default Reponse and Error

Two default objets are used :

interface SuccessResponse {
    message: string;
}

interface ErrorResponse {
    code: string;
    error: string;
}

SuccessReponse represents a default success response. ErrorResponse reprensents the common way to format error.

Locales

Every sentences are translated. The default accepted locate is en_US. The current available locales are: en_US, fr_FR

There is two ways to set the locale:

  • globally
    import { setLocale } from 'super-matcher-sdk';

    setLocale('fr_FR');

    // Every function will now return sentences in French
  • locally You can pass the locale to your function when accepted:
    import { getAllRarity } from 'super-matcher-sdk';

    getAllRarity('fr_FR').then(rarities => {
        for (const rarity of rarities) {
            // here rarity.name is french
        }
    })

Get all locales

  • Role: user

function getAllLocales(): Promise<Locale[]>

export interface Locale {
    id: string;
    descriptions: string;
}

Token

Initialize the library

function init(config: InitConfig)

Retrive the access token

function getAccessToken(): string

Does the user have an admin role ?

function isAdmin(): boolean

Does the user have a user role ?

function isUser(): boolean

Users

Sign in

signIn(email: string, password: string): Promise<SignInResponse>

interface SignInResponse {
    expiredIn: number;
    tokenType: string;
    accessToken: string;
    refreshToken: string;
}

Create user

createUser(userData: { username: string, password: string, email: string }): Promise<void>

Logout user

disconnect(): Promise<SuccessResponse>

Cards

Get all cards

  • Role: user

getAllCards(): Promise<Card>

interface Card {
    id: number;
    attack: number;
    cost: number;
    hp: number;
    image: string;
    name: string;
    rarity: string;
}

Rarity

Get all rarities

  • Role: user

function getAllRarities(locale?: string): Promise<Rarity[]>

export interface Rarity {
    id: number;
    name: string;
}

Create rarity

  • Role: admin

function createRarity(uniqueName: string, name: {[localeId: string]: string})

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago