From a040d1e7d32071527012e2f325440a24b9fe50d4 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Fri, 10 Feb 2023 01:27:38 +0200 Subject: [PATCH] added tuitio client --- package-lock.json | 165 +++++++++++++++++++++++++++++++++++ package.json | 4 + src/__tests__/Tuitio.test.ts | 23 +++-- src/client.ts | 66 ++++++++++++++ src/config.ts | 11 +++ src/constants.ts | 4 + src/utils.ts | 7 ++ 7 files changed, 273 insertions(+), 7 deletions(-) create mode 100644 src/client.ts create mode 100644 src/config.ts create mode 100644 src/constants.ts create mode 100644 src/utils.ts diff --git a/package-lock.json b/package-lock.json index 8486562..e0d6a14 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,10 @@ "name": "@flare/tuitio-client", "version": "1.0.0", "license": "ISC", + "dependencies": { + "@flare/js-utils": "^1.0.3", + "axios": "^1.3.2" + }, "devDependencies": { "@types/jest": "^29.4.0", "jest": "^29.4.1", @@ -603,6 +607,11 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "node_modules/@flare/js-utils": { + "version": "1.0.3", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.3.tgz", + "integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" + }, "node_modules/@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -1155,6 +1164,21 @@ "sprintf-js": "~1.0.2" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.2.tgz", + "integrity": "sha512-1M3O703bYqYuPhbHeya5bnhpYVsDDRyQSabNja04mZtboLNSuZ4YrltestrLXfHgmzua4TpUqRiVKbiQuo2epw==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/babel-jest": { "version": "29.4.1", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.4.1.tgz", @@ -1466,6 +1490,17 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", @@ -1530,6 +1565,14 @@ "node": ">=0.10.0" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/detect-newline": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz", @@ -1709,6 +1752,38 @@ "node": ">=8" } }, + "node_modules/follow-redirects": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -2792,6 +2867,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -3098,6 +3192,11 @@ "node": ">= 6" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/react-is": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz", @@ -4238,6 +4337,11 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "@flare/js-utils": { + "version": "1.0.3", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.3.tgz", + "integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" + }, "@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -4699,6 +4803,21 @@ "sprintf-js": "~1.0.2" } }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "axios": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.2.tgz", + "integrity": "sha512-1M3O703bYqYuPhbHeya5bnhpYVsDDRyQSabNja04mZtboLNSuZ4YrltestrLXfHgmzua4TpUqRiVKbiQuo2epw==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "babel-jest": { "version": "29.4.1", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.4.1.tgz", @@ -4920,6 +5039,14 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, "commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", @@ -4970,6 +5097,11 @@ "integrity": "sha512-z2wJZXrmeHdvYJp/Ux55wIjqo81G5Bp4c+oELTW+7ar6SogWHajt5a9gO3s3IDaGSAXjDk0vlQKN3rms8ab3og==", "dev": true }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" + }, "detect-newline": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz", @@ -5103,6 +5235,21 @@ "path-exists": "^4.0.0" } }, + "follow-redirects": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -5925,6 +6072,19 @@ "picomatch": "^2.3.1" } }, + "mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, "mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -6145,6 +6305,11 @@ "sisteransi": "^1.0.5" } }, + "proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "react-is": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz", diff --git a/package.json b/package.json index 5ffddce..4615c37 100644 --- a/package.json +++ b/package.json @@ -50,5 +50,9 @@ "tslint": "^6.1.3", "tslint-config-prettier": "^1.18.0", "typescript": "^4.9.5" + }, + "dependencies": { + "@flare/js-utils": "^1.0.3", + "axios": "^1.3.2" } } diff --git a/src/__tests__/Tuitio.test.ts b/src/__tests__/Tuitio.test.ts index 7d42a44..b689dba 100644 --- a/src/__tests__/Tuitio.test.ts +++ b/src/__tests__/Tuitio.test.ts @@ -1,11 +1,20 @@ -import { sum } from "../index"; +import { getUrlTemplates } from "../config"; +import { combineUrls } from "../utils"; -test("Test 1", () => { - const result = sum(1, 1); - expect(result).toBe(2); +test("Combine urls with trailing slash", () => { + const result = combineUrls("https://test.com/api/", "/test"); + expect(result).toBe("https://test.com/api/test"); }); -test("Test 2", () => { - const result = sum(99, 101); - expect(result).toBe(200); +test("Combine urls without trailing slash", () => { + const result = combineUrls("https://test.com/api", "/test"); + expect(result).toBe("https://test.com/api/test"); +}); + +test("Get url templates", () => { + const result = getUrlTemplates("https://test.com/api"); + expect(result).toHaveProperty("authentication"); + expect(result.authentication).toContain("https://test.com/api"); + expect(result.authentication).toContain("{username}"); + expect(result.authentication).toContain("{password}"); }); diff --git a/src/client.ts b/src/client.ts new file mode 100644 index 0000000..eb86d45 --- /dev/null +++ b/src/client.ts @@ -0,0 +1,66 @@ +import axios from "axios"; +import { localStorage } from "@flare/js-utils"; +import { storageKeys } from "./constants"; +import { getUrlTemplates } from "./config"; + +const { setItem, getItem, removeItem } = localStorage; + +async function request(url: string, method: string) { + try { + const res = await axios.request({ url, method }); + return res.data; + } catch (error: any) { + if (error.response && error.response.data) { + throw ( + { + ...error.response.data, + message: error.response.data.detail || error.response.data.title + } || error + ); + } + throw error; + } +} + +class TuitioConnector { + baseUrl: string; + + constructor(baseUrl: string) { + this.baseUrl = baseUrl; + } + + async authenticate(userName: string, password: string) { + const templates = getUrlTemplates(this.baseUrl); + const url = templates.authentication.replace("{username}", userName).replace("{password}", password); + + const response = await request(url, "post"); + if (response.token) { + setItem(storageKeys.TOKEN, response.token); + setItem(storageKeys.USER, userName); + } + + return response; + } +} + +const invalidate = (): void => { + const token = getItem(storageKeys.TOKEN); + if (token) { + removeItem(storageKeys.TOKEN); + removeItem(storageKeys.USER); + } +}; + +type TuitioState = { token: string; userName: string }; + +const fetch = (): TuitioState => { + const data = { + token: getItem(storageKeys.TOKEN), + userName: getItem(storageKeys.USER) + }; + + return data; +}; + +export { TuitioConnector, invalidate, fetch }; +export default TuitioConnector; diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..e4d7690 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,11 @@ +import { combineUrls } from "./utils"; + +type UrlTemplates = { + authentication: string; +}; + +const getUrlTemplates = (tuitioUrl: string): UrlTemplates => ({ + authentication: combineUrls(tuitioUrl, "/identity/authenticate?UserName={username}&Password={password}") +}); + +export { getUrlTemplates }; diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..b4bf3da --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,4 @@ +export const storageKeys: { TOKEN: string; USER: string } = { + TOKEN: "TUITIO_AUTHORIZATION_TOKEN", + USER: "TUITIO_USER_NAME" +}; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..2551ccf --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,7 @@ +const combineUrls = (piece1: string, piece2: string): string => { + let baseElement = piece1; + if (baseElement.endsWith("/")) baseElement = baseElement.substring(0, baseElement.length - 1); + return baseElement + piece2; +}; + +export { combineUrls };