Skip to content

Commit

Permalink
Merge branch 'release/v2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilujD committed May 14, 2020
2 parents 89ebde8 + 6e7ae45 commit cc32f2b
Show file tree
Hide file tree
Showing 18 changed files with 383 additions and 86 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
docs/
docs/
localStorage/
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ npm i -s georide-js
Example :

```js
const Georide = require('georide-js').default
const { Client } = require('georide-js')


const EMAIL = process.env.EMAIL
const PASSWORD = process.env.PASSWORD

const main = async () => {
// Create the client
const client = new Georide(EMAIL, PASSWORD)
const client = new Client(EMAIL, PASSWORD)

// Authenticate the user
const token = await client.login()
Expand Down Expand Up @@ -101,4 +101,5 @@ docker build ./

### Tips

* Update tests if needed
* Update tests if needed
* Update the `JSDoc` part
3 changes: 2 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// To run it: `EMAIL=<your_georide_email_address> PASSWORD=<your_georide_password> node ./example.mjs

const Georide = require('./dist/cjs/georide').default
const Georide = require('./dist/cjs/georide').Client


const EMAIL = process.env.EMAIL
Expand Down Expand Up @@ -30,6 +30,7 @@ const main = async () => {
// Subscribe to the `position` event
client.onPosition(message => {
const { trackerId, latitude, longitude, moving } = message
console.log(message)
})

process.exit()
Expand Down
69 changes: 66 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "georide-js",
"version": "1.1.0",
"version": "2.0.0",
"description": "Georide JavaScript sdk",
"homepage": "https://neilujd.github.io/georide-js/",
"repository": {
"type": "git",
"url": "git://github.com/NeilujD/georide-js.git"
},
"main": "dist/cjs/georide.js",
"browser": "dist/esm/georide.js",
"module": "dist/esm/georide.js",
"files": [
"dist/**/*",
Expand All @@ -23,8 +24,8 @@
"build:esm": "tsc --module ES2015 --target ES2015 --outDir dist/esm",
"build:cjs": "tsc --module commonjs --target ES2015 --outDir dist/cjs",
"clean-and-build": "rm -r dist && npm run build",
"doc:build": "typedoc",
"doc:deploy": "gh-pages --message \"[skip ci] Updates\" --dist docs",
"doc:build": "typedoc --plugin typedoc-plugin-nojekyll",
"doc:deploy": "gh-pages --message \"[ci skip] update documentation\" -d docs",
"test": "mocha -r ts-node/register --extension ts"
},
"keywords": [
Expand All @@ -50,11 +51,13 @@
"nock": "^12.0.3",
"ts-node": "^8.10.1",
"typedoc": "^0.17.6",
"typedoc-plugin-nojekyll": "^1.0.1",
"typescript": "^3.8.3"
},
"dependencies": {
"cross-fetch": "^3.0.4",
"moment": "^2.25.3",
"node-fetch": "^2.6.0",
"node-localstorage": "^2.1.6",
"socket.io": "^2.3.0"
}
}
83 changes: 72 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Socket } from 'socket.io-client'

import { Token } from './request'
import { StorageFactory, MemoryStorageFactory } from './storage'


/**
Expand All @@ -10,16 +11,23 @@ import { Token } from './request'
* @property {string} host the Georide API host
* @property {string} protocol the Georide API protocol
* @property {string} authUri the Georide API authentication endpoint uri
* @property {string} newTokenUri the Georide API refresh token endpoint uri
* @property {Socket} socket the socket client
* @property {boolean} supportSocket define if client should support socket client connection
* @property {object} storage the storage used to store the token
* @property {string} storageTokenKey the storage key to the token
*/
export default class Config {
class Config {
email: string
password: string
host: string
protocol: string
authUri: string
newTokenUri: string
token: Token | null
supportSocket: boolean
socket!: typeof Socket
storage: StorageFactory | MemoryStorageFactory
storageTokenKey: string

/**
* Create a config instance
Expand All @@ -29,27 +37,78 @@ export default class Config {
* @param {string} options.host the Georide API host
* @param {string} options.protocol the Georide API protocol
* @param {string} options.authUri the Georide API authentication endpoint uri
* @param {string} options.newTokenUri the Georide API new token request uri
* @param {string} options.newTokenUri the Georide API new token endpoint uri
* @param {object} options.storage the storage strategy
* @param {string} options.storageTokenKey the storage key to store the token
* @param {boolean} options.supportSocket define if client should support socket client connection
*/
constructor (options: { email: string, password: string, host?: string, protocol?: string, auth_uri?: string, new_token_uri?: string }) {
const { email, password, host = 'api.georide.fr', protocol = 'https', auth_uri = '/user/login', new_token_uri = '/user/new-token'} = options
constructor (
options: {
email: string,
password: string,
host?: string,
protocol?: string,
authUri?: string,
newTokenUri?: string,
storage?: StorageFactory | MemoryStorageFactory,
storageTokenKey?: string,
supportSocket?: boolean
}
) {
const {
email,
password,
host = 'api.georide.fr',
protocol = 'https',
authUri = '/user/login',
newTokenUri = '/user/new-token',
storage = new StorageFactory(),
storageTokenKey = 'georide_token',
supportSocket = true
} = options

this.email = email
this.password = password

this.host = host
this.protocol = protocol
this.authUri = auth_uri
this.newTokenUri = new_token_uri
this.authUri = authUri
this.newTokenUri = newTokenUri

this.token = null
this.storage = storage
this.storageTokenKey = storageTokenKey

this.supportSocket = supportSocket
}

/** Setter for the token
/**
* Setter for the token
* @param {Token} token
*/
setToken (token: Token | null) {
this.token = token
// Update token in storage
if (!token) this.storage.delete(this.storageTokenKey)
else this.storage.set(this.storageTokenKey, JSON.stringify(token))

// Update token in socket client
if (this.socket)
this.socket.io.opts.transportOptions = {
...this.socket.io.opts.transportOptions,
polling: {
extraHeaders: {
token: token ? token.authToken : null
}
}
}
}

/**
* Getter for the token
* @return {Token | null}
*/
getToken (): Token | null {
const t = this.storage.get(this.storageTokenKey)
return t ? new Token(JSON.parse(t)) : null
}

/**
Expand All @@ -59,4 +118,6 @@ export default class Config {
setSocket (socket: typeof Socket) {
this.socket = socket
}
}
}
export default Config
export { Config }
6 changes: 4 additions & 2 deletions src/endpoints/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Request from '../request'
* @property {Request} request the request object
* @property {string} baseUri the endpoint base URI
*/
export default class BaseEndpoint {
class BaseEndpoint {
config: Config
request: Request
baseUri!: string
Expand All @@ -22,4 +22,6 @@ export default class BaseEndpoint {
this.config = config
this.request = new Request(config)
}
}
}
export default BaseEndpoint
export { BaseEndpoint }
Loading

0 comments on commit cc32f2b

Please sign in to comment.