diff --git a/apps/web-graphpolaris/src/app/app.tsx b/apps/web-graphpolaris/src/app/app.tsx index 79360ad5071c4205bd860ccb279ed657a58760e9..5490311a6cd81a64417e6545b8fa255794f1d4ec 100644 --- a/apps/web-graphpolaris/src/app/app.tsx +++ b/apps/web-graphpolaris/src/app/app.tsx @@ -11,12 +11,18 @@ import { RawJSONVis } from '../components/visualisations/rawjsonvis/rawjsonvis'; import SemanticSubstrates from '../components/visualisations/semanticsubstrates/semanticsubstrates'; import LoginScreen from '../components/login/loginScreen'; import Schema from '../components/schema/schema'; +import { OurThemeProvider } from '@graphpolaris/shared/data-access/theme'; +import { GetUserInfo } from '@graphpolaris/shared/data-access/api'; function useIsAuthorized() { const [userAuthorized, setUserAuthorized] = useState(false); - const authCallback = () => { + const authCallback = async () => { setUserAuthorized(true); + + // Print the user that is currently logged in + const user = await GetUserInfo(); + console.log(user); }; AuthorizationHandler.instance().setCallback(authCallback); diff --git a/libs/shared/data-access/api/.babelrc b/libs/shared/data-access/api/.babelrc new file mode 100644 index 0000000000000000000000000000000000000000..cf7ddd99c615a064ac18eb3109eee4f394ab1faf --- /dev/null +++ b/libs/shared/data-access/api/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]] +} diff --git a/libs/shared/data-access/api/.eslintrc.json b/libs/shared/data-access/api/.eslintrc.json new file mode 100644 index 0000000000000000000000000000000000000000..632e9b0e22253922989d1153e06f7ba996c72d38 --- /dev/null +++ b/libs/shared/data-access/api/.eslintrc.json @@ -0,0 +1,18 @@ +{ + "extends": ["../../../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.ts", "*.tsx"], + "rules": {} + }, + { + "files": ["*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/libs/shared/data-access/api/README.md b/libs/shared/data-access/api/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3156f01c6b6befd68fe400d0c03a01d52ac3b5e7 --- /dev/null +++ b/libs/shared/data-access/api/README.md @@ -0,0 +1,7 @@ +# shared-data-access-api + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test shared-data-access-api` to execute the unit tests via [Jest](https://jestjs.io). diff --git a/libs/shared/data-access/api/jest.config.js b/libs/shared/data-access/api/jest.config.js new file mode 100644 index 0000000000000000000000000000000000000000..d6f5553b2f38e80cf671a056bd731ff218828fc7 --- /dev/null +++ b/libs/shared/data-access/api/jest.config.js @@ -0,0 +1,14 @@ +module.exports = { + displayName: 'shared-data-access-api', + preset: '../../../../jest.preset.js', + globals: { + 'ts-jest': { + tsconfig: '<rootDir>/tsconfig.spec.json', + }, + }, + transform: { + '^.+\\.[tj]sx?$': 'ts-jest', + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../../../coverage/libs/shared/data-access/api', +}; diff --git a/libs/shared/data-access/api/project.json b/libs/shared/data-access/api/project.json new file mode 100644 index 0000000000000000000000000000000000000000..362413c86100b967aa87d647e7a416bfd1b1d8c6 --- /dev/null +++ b/libs/shared/data-access/api/project.json @@ -0,0 +1,23 @@ +{ + "root": "libs/shared/data-access/api", + "sourceRoot": "libs/shared/data-access/api/src", + "projectType": "library", + "targets": { + "lint": { + "executor": "@nrwl/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["libs/shared/data-access/api/**/*.ts"] + } + }, + "test": { + "executor": "@nrwl/jest:jest", + "outputs": ["coverage/libs/shared/data-access/api"], + "options": { + "jestConfig": "libs/shared/data-access/api/jest.config.js", + "passWithNoTests": true + } + } + }, + "tags": [] +} diff --git a/libs/shared/data-access/api/src/index.ts b/libs/shared/data-access/api/src/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..136d9ebb4b0d0d0dc89d1ac6330a62d19d681908 --- /dev/null +++ b/libs/shared/data-access/api/src/index.ts @@ -0,0 +1 @@ +export { User, GetUserInfo } from './lib/user'; diff --git a/libs/shared/data-access/api/src/lib/database.ts b/libs/shared/data-access/api/src/lib/database.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d6700de6f55dde670227d1cedf1080c44637e28 --- /dev/null +++ b/libs/shared/data-access/api/src/lib/database.ts @@ -0,0 +1,75 @@ +// All database related API calls + +import { AuthorizationHandler } from '@graphpolaris/shared/data-access/authorization'; + +export type AddDatabaseRequest = { + name: string; + internal_database_name: string; + url: string; + port: number; + username: string; + password: string; + type: number; // Database type. 0 = ArangoDB, 1 = Neo4j +}; + +export function AddDatabase(request: AddDatabaseRequest): Promise<void> { + return new Promise((resolve, reject) => { + fetch('https://datastrophe.science.uu.nl/user/database', { + method: 'POST', + credentials: 'same-origin', + headers: new Headers({ + Authorization: + 'Bearer ' + AuthorizationHandler.instance().AccessToken(), + }), + body: JSON.stringify(request), + }).then((response: Response) => { + if (!response.ok) { + reject(response.statusText); + } + + resolve(); + }); + }); +} + +export function GetAllDatabases(): Promise<Array<string>> { + return new Promise<Array<string>>((resolve, reject) => { + fetch('https://datastrophe.science.uu.nl/user/database', { + method: 'GET', + credentials: 'same-origin', + headers: new Headers({ + Authorization: + 'Bearer ' + AuthorizationHandler.instance().AccessToken(), + }), + }) + .then((response: Response) => { + if (!response.ok) { + reject(response.statusText); + } + + return response.json(); + }) + .then((json: any) => { + resolve(json.databases); + }); + }); +} + +export function DeleteDatabase(name: string): Promise<void> { + return new Promise((resolve, reject) => { + fetch('https://datastrophe.science.uu.nl/user/database/' + name, { + method: 'DELETE', + credentials: 'same-origin', + headers: new Headers({ + Authorization: + 'Bearer ' + AuthorizationHandler.instance().AccessToken(), + }), + }).then((response: Response) => { + if (!response.ok) { + reject(response.statusText); + } + + resolve(); + }); + }); +} diff --git a/libs/shared/data-access/api/src/lib/user.ts b/libs/shared/data-access/api/src/lib/user.ts new file mode 100644 index 0000000000000000000000000000000000000000..457ce20f6af6c8be31727f2b9757134827b4fc70 --- /dev/null +++ b/libs/shared/data-access/api/src/lib/user.ts @@ -0,0 +1,36 @@ +// All user related API calls + +import { AuthorizationHandler } from '@graphpolaris/shared/data-access/authorization'; + +export type User = { + Name: string; + Email: string; + SignInProvider: number; +}; + +export function GetUserInfo(): Promise<User> { + return new Promise<User>((resolve, reject) => { + fetch('https://datastrophe.science.uu.nl/user/', { + method: 'GET', + credentials: 'same-origin', + headers: new Headers({ + Authorization: + 'Bearer ' + AuthorizationHandler.instance().AccessToken(), + }), + }) + .then((response: Response) => { + if (!response.ok) { + reject(response.statusText); + } + + return response.json(); + }) + .then((json: any) => { + resolve({ + Name: json.name, + Email: json.email, + SignInProvider: json.sign_in_provider, + }); + }); + }); +} diff --git a/libs/shared/data-access/api/tsconfig.json b/libs/shared/data-access/api/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..d0953a0f8b8084fd7ca99de4a00f82435f8679cd --- /dev/null +++ b/libs/shared/data-access/api/tsconfig.json @@ -0,0 +1,19 @@ +{ + "extends": "../../../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ], + "compilerOptions": { + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + } +} diff --git a/libs/shared/data-access/api/tsconfig.lib.json b/libs/shared/data-access/api/tsconfig.lib.json new file mode 100644 index 0000000000000000000000000000000000000000..2ef844c42b4d526dd97ef3b18591cc5c652781e5 --- /dev/null +++ b/libs/shared/data-access/api/tsconfig.lib.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../../dist/out-tsc", + "declaration": true, + "types": [] + }, + "include": ["**/*.ts"], + "exclude": ["**/*.spec.ts"] +} diff --git a/libs/shared/data-access/api/tsconfig.spec.json b/libs/shared/data-access/api/tsconfig.spec.json new file mode 100644 index 0000000000000000000000000000000000000000..315a5b0bbebaca96617a8dd5353901287ebd8e68 --- /dev/null +++ b/libs/shared/data-access/api/tsconfig.spec.json @@ -0,0 +1,19 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "**/*.test.ts", + "**/*.spec.ts", + "**/*.test.tsx", + "**/*.spec.tsx", + "**/*.test.js", + "**/*.spec.js", + "**/*.test.jsx", + "**/*.spec.jsx", + "**/*.d.ts" + ] +} diff --git a/libs/shared/data-access/authorization/src/lib/authorizationHandler.ts b/libs/shared/data-access/authorization/src/lib/authorizationHandler.ts index f3e4319a9525521d43ceee6b92e7d18b52defea5..fc92922419ad609e85767e55dd35b06b2a581012 100644 --- a/libs/shared/data-access/authorization/src/lib/authorizationHandler.ts +++ b/libs/shared/data-access/authorization/src/lib/authorizationHandler.ts @@ -157,7 +157,7 @@ export class AuthorizationHandler { * SetAccessToken sets the current access token (should only be called by the sign-in component) * @param accessToken */ - SetAccessToken(accessToken: string) { + async SetAccessToken(accessToken: string) { this.accessToken = accessToken; console.log(this.accessToken); @@ -170,7 +170,6 @@ export class AuthorizationHandler { this.refreshTokens(); }, 10 * 60 * 1000); - // TODO: Change auth state if (this.callback) { this.callback(); } diff --git a/tsconfig.base.json b/tsconfig.base.json index b0ae10a05dbbb715dadd01b8cc54aef7018bc5ac..cd9c7fc960effe1a638e83bd4e1dd03b2bcc53c0 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -16,6 +16,9 @@ "baseUrl": ".", "paths": { "@graphpolaris/schema-usecases": ["libs/schema/usecases/src/index.ts"], + "@graphpolaris/shared/data-access/api": [ + "libs/shared/data-access/api/src/index.ts" + ], "@graphpolaris/shared/data-access/authorization": [ "libs/shared/data-access/authorization/src/index.ts" ], diff --git a/workspace.json b/workspace.json index d66d6e6672063525132bd6b6e3aedb7322d30e23..1e30cdac60853923072853d35375bf9d10055196 100644 --- a/workspace.json +++ b/workspace.json @@ -2,6 +2,7 @@ "version": 2, "projects": { "schema-usecases": "libs/schema/usecases", + "shared-data-access-api": "libs/shared/data-access/api", "shared-data-access-authorization": "libs/shared/data-access/authorization", "shared-data-access-store": "libs/shared/data-access/store", "shared-data-access-theme": "libs/shared/data-access/theme", @@ -9,4 +10,4 @@ "web-graphpolaris": "apps/web-graphpolaris", "web-graphpolaris-e2e": "apps/web-graphpolaris-e2e" } -} +} \ No newline at end of file