super-matcher-sdk v1.0.18
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})
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago