Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
MEDIA-2230 Added client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkonst committed Apr 17, 2019
1 parent 7ff11a6 commit 6e1e9bd
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 44 deletions.
28 changes: 28 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"env": {
"lib": {
"presets": [
"@babel/preset-env"
]
},
"es": {
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"targets": {
"chrome": 43,
"firefox": 43,
"safari": 10,
"ios": 10,
"opera": 40,
"edge": 13,
"ie": 9
}
}
]
]
}
}
}
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
es/
lib/
node_modules/
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
56 changes: 12 additions & 44 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,61 +1,29 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz
# Lock files
package-lock.json
yarn.lock

# Yarn Integrity file
.yarn-integrity
# IDE specific files
.idea/*

# dotenv environment variables file
.env
# OSX
.DS_Store
._*
.Spotlight-V100
.Trashes

# next.js build output
.next
es/
lib/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock = false
progress = false
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@ulms/events",
"version": "0.1.0",
"description": "JavaScript API-client for uLMS Events service",
"files": [
"es",
"lib"
],
"main": "lib/index.js",
"module": "es/index.js",
"scripts": {
"build": "npm run build:es && npm run build:lib",
"build:es": "BABEL_ENV=es babel src -d es",
"build:lib": "BABEL_ENV=lib babel src -d lib",
"lint": "eslint .",
"prebuild": "rm -rf es lib",
"prepublishOnly": "npm run test && npm run build",
"test": "eslint ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/netology-group/ulms-events-js.git"
},
"author": "netology-group",
"license": "MIT",
"bugs": {
"url": "https://github.com/netology-group/ulms-events-js/issues"
},
"homepage": "https://github.com/netology-group/ulms-events-js#readme",
"devDependencies": {
"@babel/cli": "~7.2.3",
"@babel/core": "~7.2.2",
"@babel/preset-env": "~7.3.1",
"eslint": "~5.12.1",
"eslint-config-standard": "~12.0.0",
"eslint-plugin-import": "~2.15.0",
"eslint-plugin-node": "~8.0.1",
"eslint-plugin-promise": "~4.0.1",
"eslint-plugin-standard": "~4.0.0"
},
"dependencies": {}
}
135 changes: 135 additions & 0 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
export class HttpEventsResource {
constructor (
host,
endpoint,
httpClient,
tokenProvider
) {
this.baseUrl = `${host}/${endpoint}`
this.httpClient = httpClient
this.tokenProvider = tokenProvider
}
static _headers (token, params = {}) {
const { randomId } = params
const additionalHeaders = {}

if (randomId) {
additionalHeaders['X-Random-Id'] = randomId
}

return {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
...additionalHeaders
}
}
getState (audience, roomId, params = {}) {
const { offset } = params
let qs = ''

if (offset) {
qs = `?offset=${offset}`
}

return this.tokenProvider.getToken()
.then((token) =>
this.httpClient.get(
`${this.baseUrl}/${audience}/rooms/${roomId}/state${qs}`,
{
headers: HttpEventsResource._headers(token)
}
)
)
}
getEvents (audience, roomId, direction, params = {}) {
const { after, before, lastId, type } = params
const qsParts = []
let qs = ''

qsParts.push(`direction=${direction}`)

if (after) {
qsParts.push(`after=${after}`)
}

if (before) {
qsParts.push(`before=${before}`)
}

if (lastId) {
qsParts.push(`last_id=${lastId}`)
}

if (type) {
qsParts.push(`type=${type}`)
}

qs = `?${qsParts.join('&')}`

return this.tokenProvider.getToken()
.then((token) =>
this.httpClient.get(
`${this.baseUrl}/${audience}/rooms/${roomId}/events${qs}`,
{
headers: HttpEventsResource._headers(token)
}
)
)
}
createEvent (audience, roomId, type, data, params = {}) {
const { randomId } = params

return this.tokenProvider.getToken()
.then((token) =>
this.httpClient.post(
`${this.baseUrl}/${audience}/rooms/${roomId}/events/${type}`,
data,
{
headers: HttpEventsResource._headers(token, { randomId })
}
)
)
}
updateEvent (audience, roomId, type, eventId, data, params = {}) {
const { randomId } = params

return this.tokenProvider.getToken()
.then((token) =>
this.httpClient.patch(
`${this.baseUrl}/${audience}/rooms/${roomId}/events/${type}/${eventId}`,
data,
{
headers: HttpEventsResource._headers(token, { randomId })
}
)
)
}
deleteEvent (audience, roomId, type, eventId, data, params = {}) {
const { randomId } = params

return this.tokenProvider.getToken()
.then((token) =>
this.httpClient.delete(
`${this.baseUrl}/${audience}/rooms/${roomId}/events/${type}/${eventId}`,
data,
{
headers: HttpEventsResource._headers(token, { randomId })
}
)
)
}
createNotification (audience, roomId, type, data, params = {}) {
const { randomId } = params

return this.tokenProvider.getToken()
.then((token) =>
this.httpClient.post(
`${this.baseUrl}/${audience}/rooms/${roomId}/notifications/${type}`,
data,
{
headers: HttpEventsResource._headers(token, { randomId })
}
)
)
}
}
41 changes: 41 additions & 0 deletions src/http-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* global fetch */

export class FetchHttpClient {
static _processResponse (response) {
if (response.status === 204) {
return null
}

return response.json()
}
get (url, config) {
return fetch(url, {
method: 'GET',
headers: config.headers
})
.then(FetchHttpClient._processResponse)
}
post (url, data, config) {
return fetch(url, {
method: 'POST',
headers: config.headers,
body: JSON.stringify(data)
})
.then(FetchHttpClient._processResponse)
}
patch (url, data, config) {
return fetch(url, {
method: 'PATCH',
headers: config.headers,
body: JSON.stringify(data)
})
.then(FetchHttpClient._processResponse)
}
delete (url, config) {
return fetch(url, {
method: 'DELETE',
headers: config.headers
})
.then(FetchHttpClient._processResponse)
}
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { FetchHttpClient } from './http-client'
export { SimpleTokenProvider } from './token-provider'
export { HttpEventsResource } from './events'
8 changes: 8 additions & 0 deletions src/token-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class SimpleTokenProvider {
constructor (token) {
this.token = token
}
getToken () {
return Promise.resolve(this.token)
}
}

0 comments on commit 6e1e9bd

Please sign in to comment.