This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MEDIA-2230 Added client implementation
- Loading branch information
Showing
12 changed files
with
287 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
es/ | ||
lib/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "standard" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package-lock = false | ||
progress = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |