-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move hackaton files to this repository
- Loading branch information
Showing
22 changed files
with
594 additions
and
17 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,62 @@ | ||
# 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 | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# custom | ||
.idea | ||
dist |
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 @@ | ||
**/node_modules/ | ||
**/dist/ |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Germain Bergeron | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
automock: false, | ||
setupFiles: ['./jest.setup.ts'], | ||
}; |
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,5 @@ | ||
import {GlobalWithFetchMock} from 'jest-fetch-mock'; | ||
|
||
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock; | ||
customGlobal.fetch = require('jest-fetch-mock'); | ||
customGlobal.fetchMock = customGlobal.fetch; |
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,19 +1,53 @@ | ||
{ | ||
"name": "@coveord/platform-client", | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/coveo/platform-client.git" | ||
}, | ||
"author": "Coveo", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/coveo/platform-client/issues" | ||
}, | ||
"homepage": "https://github.com/coveo/platform-client#readme" | ||
"name": "@coveord/platform-client", | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"types": "dist/definitions/CoveoPlatform.d.ts", | ||
"scripts": { | ||
"start": "webpack --watch", | ||
"build": "webpack", | ||
"test": "jest", | ||
"test:changed": "jest --watch", | ||
"test:watch": "jest --watchAll" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.18", | ||
"husky": "^3.0.5", | ||
"jest": "^24.9.0", | ||
"jest-fetch-mock": "^2.1.2", | ||
"lint-staged": "^9.2.5", | ||
"prettier": "^1.18.2", | ||
"ts-jest": "^24.0.2", | ||
"ts-loader": "^6.0.4", | ||
"tsjs": "^1.0.1", | ||
"tslint": "^5.19.0", | ||
"typescript": "^3.6.2", | ||
"webpack": "^4.39.3", | ||
"webpack-cli": "^3.3.7" | ||
}, | ||
"prettier": "tsjs/prettier-config", | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"**/*.{ts,tsx}": [ | ||
"prettier --write", | ||
"tslint -c ./tslint.json -p ./tsconfig.json --fix", | ||
"git add" | ||
], | ||
"**/*.{js,jsx,json,md,yml}": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/coveo/platform-client.git" | ||
}, | ||
"author": "Coveo", | ||
"license": "Apache-2.0", | ||
"homepage": "https://github.com/coveo/platform-client#readme" | ||
} |
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,67 @@ | ||
import {IRestResponse} from './handlers/HandlerConstants'; | ||
import {Handlers} from './handlers/Handlers'; | ||
|
||
const removeEmptyEntries = (obj) => | ||
Object.keys(obj).reduce((memo, key) => { | ||
const val = obj[key]; | ||
if (val && typeof val === 'object') { | ||
memo[key] = removeEmptyEntries(obj); | ||
} else if (val != null || val !== '') { | ||
memo[key] = obj[key]; | ||
} | ||
return memo; | ||
}, {}); | ||
|
||
export default class API { | ||
static orgPlaceholder = '{organizationName}'; | ||
|
||
constructor(private host: string, private orgId: string, private accessTokenRetriever: () => string) {} | ||
|
||
static toQueryString(parameters: any) { | ||
return parameters ? `?${new URLSearchParams(Object.entries(removeEmptyEntries(parameters))).toString()}` : ''; | ||
} | ||
|
||
async get<T>(url: string, queryParams?: any, args: RequestInit = {method: 'get'}): Promise<IRestResponse<T>> { | ||
return await this.request<T>(url + API.toQueryString(queryParams), args); | ||
} | ||
|
||
async post<T>( | ||
url: string, | ||
body: any, | ||
args: RequestInit = {method: 'post', body: JSON.stringify(body)} | ||
): Promise<IRestResponse<T>> { | ||
return await this.request<T>(url, args); | ||
} | ||
|
||
async put<T>( | ||
url: string, | ||
body: any, | ||
args: RequestInit = {method: 'put', body: JSON.stringify(body)} | ||
): Promise<IRestResponse<T>> { | ||
return await this.request<T>(url, args); | ||
} | ||
|
||
async delete<T>(url: string, args: RequestInit = {method: 'delete'}): Promise<IRestResponse<T>> { | ||
return await this.request<T>(url, args); | ||
} | ||
|
||
private handleResponse<T>(response: Response) { | ||
const canProcess = Handlers.filter((handler) => handler.canProcess(response)); | ||
return canProcess[0].process<T>(response); | ||
} | ||
|
||
private async request<T>(urlWithOrg: string, args: RequestInit): Promise<IRestResponse<T>> { | ||
const init: RequestInit = { | ||
...args, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
authorization: `Bearer ${this.accessTokenRetriever()}`, | ||
...(args.headers || {}), | ||
}, | ||
}; | ||
const url = `${this.host}${urlWithOrg}`.replace(API.orgPlaceholder, this.orgId); | ||
|
||
const response = await fetch(url, init); | ||
return this.handleResponse(response); | ||
} | ||
} |
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,63 @@ | ||
import API from './APICore'; | ||
import {CoveoPlatformResources, Resources} from './resources/Resources'; | ||
|
||
export interface CoveoPlatformOptions { | ||
organizationId: string; | ||
accessTokenRetreiver: () => string; | ||
environment?: string; | ||
host?: string; | ||
} | ||
|
||
const Environments = { | ||
local: 'local', | ||
dev: 'development', | ||
staging: 'staging', | ||
prod: 'production', | ||
hipaa: 'hipaa', | ||
}; | ||
|
||
const Hosts = { | ||
[Environments.dev]: 'https://platformdev.cloud.coveo.com', | ||
[Environments.staging]: 'https://platformqa.cloud.coveo.com', | ||
[Environments.prod]: 'https://platform.cloud.coveo.com', | ||
[Environments.hipaa]: 'https://platformhipaa.cloud.coveo.com', | ||
}; | ||
|
||
export default class CoveoPlatform extends CoveoPlatformResources { | ||
static defaultOptions: Partial<CoveoPlatformOptions> = { | ||
environment: Environments.prod, | ||
}; | ||
|
||
private options: CoveoPlatformOptions; | ||
private tokenInfo: any; // define a better type | ||
private readonly API: API; | ||
|
||
constructor(options: CoveoPlatformOptions) { | ||
super(); | ||
|
||
this.options = { | ||
...CoveoPlatform.defaultOptions, | ||
...options, | ||
}; | ||
|
||
const host = this.options.host || Hosts[this.options.environment]; | ||
if (!host) { | ||
throw new Error(`CoveoPlatform's host is undefined.`); | ||
} | ||
|
||
this.API = new API(host, this.options.organizationId, this.options.accessTokenRetreiver); | ||
Resources.registerAll(this, this.API); | ||
} | ||
|
||
async initialize() { | ||
try { | ||
this.tokenInfo = await this.checkToken(); | ||
} catch (err) { | ||
throw new Error(err.message); | ||
} | ||
} | ||
|
||
private async checkToken() { | ||
return this.API.post('/oauth/check_token', {token: this.options.accessTokenRetreiver()}); | ||
} | ||
} |
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,13 @@ | ||
import {IRestResponse} from './HandlerConstants'; | ||
import {IRestResponseHandler} from './Handlers'; | ||
|
||
export class ErrorResponseHandler implements IRestResponseHandler { | ||
canProcess = () => true; | ||
process = async <T>(response: Response): Promise<IRestResponse<T>> => { | ||
const errorResponse = { | ||
error: await response.json(), | ||
}; | ||
console.error(errorResponse); | ||
return errorResponse; | ||
}; | ||
} |
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 @@ | ||
export interface IRestError { | ||
code: string; | ||
message: string; | ||
} | ||
|
||
export interface IRestResponse<T> { | ||
error?: IRestError; | ||
data?: T; | ||
} |
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,17 @@ | ||
import {ErrorResponseHandler} from './ErrorResponseHandler'; | ||
import {IRestResponse} from './HandlerConstants'; | ||
import {NoContentResponseHandler} from './NoContentResponseHandler'; | ||
import {SuccessResponseHandler} from './SuccessResponseHandler'; | ||
import {UnauthorizedResponseHandler} from './UnauthorizedResponseHandler'; | ||
|
||
export interface IRestResponseHandler { | ||
canProcess(response: Response): boolean; | ||
process<T>(response: Response): Promise<IRestResponse<T>>; | ||
} | ||
|
||
export const Handlers: IRestResponseHandler[] = [ | ||
new UnauthorizedResponseHandler(), | ||
new NoContentResponseHandler(), | ||
new SuccessResponseHandler(), | ||
new ErrorResponseHandler(), | ||
]; |
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,7 @@ | ||
import {IRestResponse} from './HandlerConstants'; | ||
import {IRestResponseHandler} from './Handlers'; | ||
|
||
export class NoContentResponseHandler implements IRestResponseHandler { | ||
canProcess = (response: Response): boolean => response.status === 204; | ||
process = async <T>(): Promise<IRestResponse<T>> => ({}); | ||
} |
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,7 @@ | ||
import {IRestResponse} from './HandlerConstants'; | ||
import {IRestResponseHandler} from './Handlers'; | ||
|
||
export class SuccessResponseHandler implements IRestResponseHandler { | ||
canProcess = (response: Response): boolean => response.status >= 200 && response.status < 300; | ||
process = async <T>(response: Response): Promise<IRestResponse<T>> => await response.json(); | ||
} |
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,17 @@ | ||
import {IRestResponse} from './HandlerConstants'; | ||
import {IRestResponseHandler} from './Handlers'; | ||
|
||
export class UnauthorizedResponseError extends Error { | ||
constructor() { | ||
super( | ||
'The request was aborted because the current user does not have access to the service. You will be redirected soon.' | ||
); | ||
} | ||
} | ||
|
||
export class UnauthorizedResponseHandler implements IRestResponseHandler { | ||
canProcess = (response: Response): boolean => response.status === 403; | ||
process = async <T>(response: Response): Promise<IRestResponse<T>> => { | ||
throw new UnauthorizedResponseError(); | ||
}; | ||
} |
Oops, something went wrong.