refactoring some codes, names and dependency

Signed-off-by: Alisson Fabiano <afabiano@eshopworld.com>
This commit is contained in:
Alisson Fabiano
2022-09-22 15:07:48 +01:00
parent 3e32883ad8
commit b102cdbfc6
7 changed files with 42 additions and 79 deletions
+4 -22
View File
@@ -10,7 +10,6 @@ import { BackstagePlugin } from '@backstage/core-plugin-api';
import { BulkCheckResponse } from '@backstage/plugin-tech-insights-common';
import { CheckResult } from '@backstage/plugin-tech-insights-common';
import { CompoundEntityRef } from '@backstage/catalog-model';
import { DateTime } from 'luxon';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { IdentityApi } from '@backstage/core-plugin-api';
import { JsonValue } from '@backstage/types';
@@ -54,23 +53,12 @@ export const EntityTechInsightsScorecardContent: (props: {
}) => JSX.Element;
// @public
export interface InsightFact {
export interface InsightFacts {
// (undocumented)
[factId: string]: {
timestamp: string;
version: string;
facts: Record<
string,
| number
| string
| boolean
| DateTime
| number[]
| string[]
| boolean[]
| DateTime[]
| JsonValue
>;
facts: Record<string, JsonValue>;
};
}
@@ -84,10 +72,7 @@ export interface TechInsightsApi {
// (undocumented)
getCheckResultRenderers: (types: string[]) => CheckResultRenderer[];
// (undocumented)
getLatestFacts(
entity: CompoundEntityRef,
facts: string[],
): Promise<InsightFact>;
getFacts(entity: CompoundEntityRef, facts: string[]): Promise<InsightFacts>;
// (undocumented)
runBulkChecks(
entities: CompoundEntityRef[],
@@ -115,10 +100,7 @@ export class TechInsightsClient implements TechInsightsApi {
// (undocumented)
getCheckResultRenderers(types: string[]): CheckResultRenderer[];
// (undocumented)
getLatestFacts(
entity: CompoundEntityRef,
facts: string[],
): Promise<InsightFact>;
getFacts(entity: CompoundEntityRef, facts: string[]): Promise<InsightFacts>;
// (undocumented)
runBulkChecks(
entities: CompoundEntityRef[],
+1 -2
View File
@@ -38,8 +38,7 @@
"@material-ui/core": "^4.12.2",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "4.0.0-alpha.57",
"@types/luxon": "^3.0.1",
"luxon": "^3.0.3",
"qs": "^6.11.0",
"react-use": "^17.2.4"
},
"peerDependencies": {
@@ -19,7 +19,7 @@ import {
CheckResult,
BulkCheckResponse,
} from '@backstage/plugin-tech-insights-common';
import { Check, InsightFact } from './types';
import { Check, InsightFacts } from './types';
import { CheckResultRenderer } from '../components/CheckResultRenderer';
import { CompoundEntityRef } from '@backstage/catalog-model';
@@ -48,8 +48,5 @@ export interface TechInsightsApi {
entities: CompoundEntityRef[],
checks?: Check[],
): Promise<BulkCheckResponse>;
getLatestFacts(
entity: CompoundEntityRef,
facts: string[],
): Promise<InsightFact>;
getFacts(entity: CompoundEntityRef, facts: string[]): Promise<InsightFacts>;
}
@@ -19,15 +19,19 @@ import {
BulkCheckResponse,
CheckResult,
} from '@backstage/plugin-tech-insights-common';
import { Check, InsightFact } from './types';
import { Check, InsightFacts } from './types';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import { ResponseError } from '@backstage/errors';
import { CompoundEntityRef } from '@backstage/catalog-model';
import {
CompoundEntityRef,
stringifyEntityRef,
} from '@backstage/catalog-model';
import {
CheckResultRenderer,
jsonRulesEngineCheckResultRenderer,
} from '../components/CheckResultRenderer';
import qs from 'qs';
/** @public */
export class TechInsightsClient implements TechInsightsApi {
@@ -45,18 +49,15 @@ export class TechInsightsClient implements TechInsightsApi {
this.renderers = options.renderers;
}
async getLatestFacts(
async getFacts(
entity: CompoundEntityRef,
facts: string[],
): Promise<InsightFact> {
const { namespace, kind, name } = entity;
const entityQuery = `entity=${encodeURIComponent(
kind.toLocaleLowerCase('en-US'),
)}:${encodeURIComponent(namespace)}/${encodeURIComponent(name)}`;
const idsQuery = facts.map(id => `ids[]=${id}`).join('&');
return await this.api<InsightFact>(
`/facts/latest?${entityQuery}&${idsQuery}`,
);
): Promise<InsightFacts> {
const query = qs.stringify({
entity: stringifyEntityRef(entity),
ids: facts,
});
return await this.api<InsightFacts>(`/facts/latest?${query}`);
}
getCheckResultRenderers(types: string[]): CheckResultRenderer[] {
@@ -104,13 +105,15 @@ export class TechInsightsClient implements TechInsightsApi {
const url = await this.discoveryApi.getBaseUrl('tech-insights');
const { token } = await this.identityApi.getCredentials();
return fetch(`${url}${path}`, {
...init,
headers: {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
},
}).then(async response => {
const request = new Request(`${url}${path}`, init);
if (!request.headers.has('content-type')) {
request.headers.set('content-type', 'application/json');
}
if (token && !request.headers.has('authorization')) {
request.headers.set('authorization', `Bearer ${token}`);
}
return fetch(request).then(async response => {
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
+2 -14
View File
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import { DateTime } from 'luxon';
import { JsonValue } from '@backstage/types';
/**
@@ -35,21 +34,10 @@ export type Check = {
*
* @public
*/
export interface InsightFact {
export interface InsightFacts {
[factId: string]: {
timestamp: string;
version: string;
facts: Record<
string,
| number
| string
| boolean
| DateTime
| number[]
| string[]
| boolean[]
| DateTime[]
| JsonValue
>;
facts: Record<string, JsonValue>;
};
}
+1 -1
View File
@@ -20,7 +20,7 @@ export {
} from './plugin';
export { techInsightsApiRef, TechInsightsClient } from './api';
export type { TechInsightsApi, Check, InsightFact } from './api';
export type { TechInsightsApi, Check, InsightFacts } from './api';
export { BooleanCheck } from './components/BooleanCheck';
export { jsonRulesEngineCheckResultRenderer } from './components/CheckResultRenderer';
export type { CheckResultRenderer } from './components/CheckResultRenderer';
+10 -16
View File
@@ -6934,11 +6934,10 @@ __metadata:
"@testing-library/jest-dom": ^5.10.1
"@testing-library/react": ^12.1.3
"@testing-library/user-event": ^14.0.0
"@types/luxon": ^3.0.1
"@types/node": ^16.11.26
cross-fetch: ^3.1.5
luxon: ^3.0.3
msw: ^0.47.0
qs: ^6.11.0
react-use: ^17.2.4
peerDependencies:
"@types/react": ^16.13.1 || ^17.0.0
@@ -14476,13 +14475,6 @@ __metadata:
languageName: node
linkType: hard
"@types/luxon@npm:^3.0.1":
version: 3.0.1
resolution: "@types/luxon@npm:3.0.1"
checksum: a81444f9b474ea9b3063ab4cc68b917a2634e38b4e229f86c78c35023a32bf5e8d1044d7c229c011291662c976cb6c4cf109dc3d2077c571790a31779a554178
languageName: node
linkType: hard
"@types/markdown-it@npm:^12.2.3":
version: 12.2.3
resolution: "@types/markdown-it@npm:12.2.3"
@@ -29349,13 +29341,6 @@ __metadata:
languageName: node
linkType: hard
"luxon@npm:^3.0.3":
version: 3.0.3
resolution: "luxon@npm:3.0.3"
checksum: 67d143f102f520761c4202086579a5cf30b230ea299da27cacb31e9f9d372cadef90f14cbe7e2621b70825b267fe00128b8176ed8b7172b48f77a403a662d5aa
languageName: node
linkType: hard
"lz-string@npm:^1.4.4":
version: 1.4.4
resolution: "lz-string@npm:1.4.4"
@@ -34244,6 +34229,15 @@ __metadata:
languageName: node
linkType: hard
"qs@npm:^6.11.0":
version: 6.11.0
resolution: "qs@npm:6.11.0"
dependencies:
side-channel: ^1.0.4
checksum: 6e1f29dd5385f7488ec74ac7b6c92f4d09a90408882d0c208414a34dd33badc1a621019d4c799a3df15ab9b1d0292f97c1dd71dc7c045e69f81a8064e5af7297
languageName: node
linkType: hard
"qs@npm:~6.5.2":
version: 6.5.2
resolution: "qs@npm:6.5.2"