@mapify/sdk
v0.0.34
Published
Mapify javascript SDK
Downloads
1
Readme
Mapify JS SDK
The Mapify JS SDK library is a wrapper for the Mapify APIs, such as the Authorization API, allowing you to develop your Location Intelligence application and services using the javascript programming languages.
Mapify Platform
Mapify is a Location Intelligence Platform to prototype and develop large scalable enterprise solutions. It aggregates data from IOT sensing devices, modern and legacy information systems, enriching its value through location intelligence and machine learning layers to assist human decision on every imaginable use case while allowing virtually infinite platform extensibility.
Requirements
Installing
Via npm
npm install @mapify/sdk
Via yarn.
yarn add @mapify/sdk
Basic usage examples
Instantiate Authentication Client
const { AuthenticationClient } = require(`@mapify/sdk`)
const authenticationClient = new AuthenticationClient({
publicKey: "/path/to/key.pub"
})
Sign with a API key
Note: Before you sign an Api Key, you must create one on Mapify Console
const { AuthenticationClient, Token } = require(`@mapify/sdk`)
const authenticationClient = new AuthenticationClient()
try{
const authorization = await authenticationClient.sign('apikey')
// Authentication token
authorization.authenticationToken
// Refresh token
authorization.refreshToken
// Authentication token expire date (UTC in seconds)
authorization.expires
// Decoded payload
tokenPayload = authenticationClient.decode(authorization.authenticationToken)
tokenPayload.apis //the apis
tokenPayload.payload //the custom payload
// List of Claims
const decodedToken = new Token(tokenPayload)
claims = token.getClaimsByApi('api')
}catch(e){
// * `HTTPException` its thrown wherever is a problem with a Sign.
}
Sign with a API key and a custom payload
const { AuthenticationClient } = require(`@mapify/sdk`)
const authenticationClient = new AuthenticationClient()
try{
const customPayload = {
example: "example"
}
const token = await authenticationClient.sign('apikey', customPayload)
}catch(e){
// * `HTTPException` its thrown wherever is a problem with a Sign.
}
Sign with a API key and a Handler
const { Handler, AuthenticationClient } = require(`@mapify/sdk`)
class AuthenticationHandler implements Handler {
construct(private readonly username, private readonly password) { }
execute(payload) {
return {
...payload,
basic_auth: new Buffer(`${this.username}:${this.password}`).toString('base64')
};
}
}
const authenticationClient = new AuthenticationClient()
authenticationClient.withHandler(new AuthenticationHandler(`user`, `password`))
try{
const authorization = await authenticationClient.sign('apikey')
// authorization code
}catch(e){
// * `HTTPException` its thrown wherever is a problem with a Sign.
}
Verify token
const fs = require('fs')
const { Handler, AuthenticationClient } = require(`@mapify/sdk`)
const authenticationClient = new AuthenticationClient({
publicKey: fs.readFileSync(`${__dirname}/key.pub`, { encoding: 'utf8' })
})
try{
const isValid = authenticationClient.verify(`token`)
}catch(e){
// * `TokenExpiredError` Token is expired
// * `InvalidToken` Token is invalid with he public key
}
// Decode payload
tokenPayload = authenticationClient.decode(`token`)
tokenPayload.apis //the apis
tokenPayload.payload //the custom payload
// List of Claims
const decodedToken = new Token(tokenPayload)
claims = token.getClaimsByApi('api')
OR
const fs = require('fs')
const { AuthenticationClient } = require(`@mapify/sdk`)
try{
const isValid = AuthenticationClient.verify(`token`, fs.readFileSync(`${__dirname}/key.pub`, { encoding: 'utf8' }))
}catch(e){
// * `TokenExpiredError` Token is expired
// * `InvalidToken` Token is invalid with he public key
}
Refresh Token
const { AuthenticationClient } = require(`@mapify/sdk`)
try{
const authenticationClient = new AuthenticationClient()
const authorization = await authenticationClient.refresh(refreshToken)
}catch(e){
// * `HTTPException` its thrown wherever is a problem with a Sign.
}
Exceptions
This library throws exceptions to indicate problems:
HTTPException
its thrown wherever is a problem with a Sign.TokenExpiredError
Token is expiredInvalidToken
Token is invalid with he public key
Testing
NPM
npm test
Yarn
yarn install && yarn build && yarn test
Docker
docker build . -t mapify-js-sdk && \
docker run -it mapify-js-sdk sh -c "yarn test"
Contributing
Please follow our contributor guide
License
This project is licensed under the terms of the Apache License Version 2.0, January 2004.