Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/feedbackFix2 #259

Merged
merged 8 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@equinor/amplify-components",
"version": "4.2.2",
"version": "3.9.1",
"description": "Frontend Typescript components for the Amplify team",
"main": "dist/amplify-components.cjs",
"module": "dist/esm/index.js",
Expand Down
76 changes: 73 additions & 3 deletions src/api/core/OpenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
/* eslint-disable */
import type { ApiRequestOptions } from './ApiRequestOptions';
import { environment, auth } from 'src/utils';
import { CancelablePromise, TokenService } from 'src/api';
import {
getLocalStorage,
updateLocalStorage,
} from 'src/hooks/useLocalStorage';
import { JwtPayload } from 'jwt-decode';
import jwtDecode from 'jwt-decode';

const { getApiUrl } = environment;
const { getApiUrl, getEnvironmentName } = environment;
const { GRAPH_REQUESTS_BACKEND, acquireToken, msalApp } = auth;

type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
type Headers = Record<string, string>;

const environmentName = getEnvironmentName(
import.meta.env.VITE_ENVIRONMENT_NAME
);
const noLocalhostEnvironmentName =
environmentName === 'localhost' ? 'development' : environmentName;

export type OpenAPIConfig = {
BASE: string;
VERSION: string;
Expand All @@ -22,7 +35,7 @@ export type OpenAPIConfig = {
ENCODE_PATH?: (path: string) => string;
};

const getToken = async () => {
const getApplicationToken = async () => {
return (
await acquireToken(
msalApp(environment.getClientId(import.meta.env.VITE_CLIENT_ID)),
Expand All @@ -33,12 +46,69 @@ const getToken = async () => {
).accessToken;
};

const isJwtTokenExpired = (token: string) => {
const decodedToken: JwtPayload = jwtDecode(token);
const todayInSecUnix = new Date().getTime() / 1000;
return decodedToken.exp && todayInSecUnix < decodedToken.exp;
};

const getToken = async (
localStorageKey: string,
tokenRequest: () => CancelablePromise<string>
) => {
const localStorageToken = getLocalStorage(localStorageKey, '');
if (localStorageToken.length !== 0 && isJwtTokenExpired(localStorageToken)) {
return localStorageToken;
} else {
const requestToken = await tokenRequest();
updateLocalStorage(localStorageKey, requestToken);
return requestToken;
}
};

const getPortalToken = async () => {
return getToken(
`amplify-portal-${environmentName}`,
TokenService.getAmplifyPortalToken
);
};

const getPortalProdToken = async () => {
return getToken(
`amplify-portal-production`,
TokenService.getAmplifyPortalProductionToken
);
};

export const OpenAPI: OpenAPIConfig = {
BASE: getApiUrl(import.meta.env.VITE_API_URL),
VERSION: '1.0',
WITH_CREDENTIALS: false,
CREDENTIALS: 'include',
TOKEN: getToken,
TOKEN: getApplicationToken,
USERNAME: undefined,
PASSWORD: undefined,
HEADERS: undefined,
ENCODE_PATH: undefined,
};

export const OpenAPI_Portal: OpenAPIConfig = {
BASE: `https://api-amplify-portal-${noLocalhostEnvironmentName}.radix.equinor.com`,
VERSION: '1.0',
WITH_CREDENTIALS: false,
CREDENTIALS: 'include',
TOKEN: getPortalToken,
USERNAME: undefined,
PASSWORD: undefined,
HEADERS: undefined,
ENCODE_PATH: undefined,
};
export const OpenAPI_Portal_Prod: OpenAPIConfig = {
BASE: `https://api-amplify-portal-production.radix.equinor.com`,
VERSION: '1.0',
WITH_CREDENTIALS: false,
CREDENTIALS: 'include',
TOKEN: getPortalProdToken,
USERNAME: undefined,
PASSWORD: undefined,
HEADERS: undefined,
Expand Down
9 changes: 8 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
/* eslint-disable */
export { ApiError } from './core/ApiError';
export { CancelablePromise, CancelError } from './core/CancelablePromise';
export { OpenAPI } from './core/OpenAPI';
export { OpenAPI, OpenAPI_Portal, OpenAPI_Portal_Prod } from './core/OpenAPI';
export { request } from './core/request';
export type { OpenAPIConfig } from './core/OpenAPI';

export { TokenService } from './services/TokenService';
export { PortalService } from './services/PortalService';

export type { Feature } from './models/Feature';
export type { FeatureToggleDto } from './models/FeatureToggleDto';
export type { GraphUser } from './models/GraphUser';
export type { ServiceNowIncidentRequestDto } from './models/ServiceNowIncidentRequestDto';
13 changes: 13 additions & 0 deletions src/api/models/Feature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

import type { GraphUser } from './GraphUser';

export type Feature = {
uuid?: string | null;
featureKey?: string | null;
description?: string | null;
activeUsers?: Array<GraphUser> | null;
activeEnvironments?: Array<string> | null;
};
10 changes: 10 additions & 0 deletions src/api/models/FeatureToggleDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

import type { Feature } from './Feature';

export type FeatureToggleDto = {
applicationName?: string | null;
features?: Array<Feature> | null;
};
10 changes: 10 additions & 0 deletions src/api/models/GraphUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

export type GraphUser = {
id?: string | null;
displayName?: string | null;
mail?: string | null;
userPrincipalName?: string | null;
};
10 changes: 10 additions & 0 deletions src/api/models/ServiceNowIncidentRequestDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

export type ServiceNowIncidentRequestDto = {
configurationItem: string;
title: string;
description: string;
callerEmail: string;
};
66 changes: 66 additions & 0 deletions src/api/services/PortalService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { CancelablePromise } from '../.';
import {
OpenAPI_Portal,
request as __request,
ServiceNowIncidentRequestDto,
FeatureToggleDto,
OpenAPI_Portal_Prod,
} from '../.';
export class PortalService {
/**
* @param requestBody
* @returns any Success
* @throws ApiError
*/
public static createIncident(
requestBody?: ServiceNowIncidentRequestDto
): CancelablePromise<any> {
return __request(OpenAPI_Portal, {
method: 'POST',
url: '/api/v1/ServiceNow/incident',
body: requestBody,
mediaType: 'application/json-patch+json',
});
}

/**
* Uploads file to slack and links it to a channel defined in config
* @param formData
* @returns any Success
* @throws ApiError
*/

public static fileUpload(formData?: FormData): CancelablePromise<any> {
return __request(OpenAPI_Portal, {
method: 'POST',
url: '/api/v1/Slack/fileUpload',
body: formData,
});
}

/**
* Gets a Feature Toggle from Application name
* @param applicationName name
* @returns FeatureToggleDto Success
* @throws ApiError
*/
public static getFeatureToggleFromApplicationName(
applicationName: string
): CancelablePromise<FeatureToggleDto> {
return __request(OpenAPI_Portal_Prod, {
method: 'GET',
url: '/api/v1/FeatureToggle/{applicationName}',
path: {
applicationName: applicationName,
},
errors: {
400: `Bad Request`,
404: `Not Found`,
500: `Server Error`,
},
});
}
}
18 changes: 0 additions & 18 deletions src/api/services/TokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ import { request as __request } from '../core/request';

export class TokenService {
/**
* Gets a token for the application with the provided clientId
* @param clientId
* @returns string Success
* @throws ApiError
*/
public static getToken(clientId: string): CancelablePromise<string> {
return __request(OpenAPI, {
method: 'GET',
url: '/api/v1/Token/{clientId}',
path: {
clientId: clientId,
},
});
}

/**
* Gets a Token for Amplify Portal for the Current Radix Environemnt (development/staging/production)
* @returns string Success
* @throws ApiError
*/
Expand All @@ -35,7 +18,6 @@ export class TokenService {
}

/**
* Gets a token for Amplify Portal in Production
* @returns string Success
* @throws ApiError
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FileWithPath } from 'react-dropzone';
import { Checkbox, Typography } from '@equinor/eds-core-react';
import { tokens } from '@equinor/eds-tokens';

import { SeverityOption } from './FeedbackDetails';
import { FeedbackContentType, FeedbackEnum } from './FeedbackForm';
import { SeverityOption } from './FeedbackFormInner';

import styled from 'styled-components';

Expand Down
Loading
Loading