refactor types

This commit is contained in:
Samira Mokaram
2020-11-13 11:41:06 +01:00
parent 2be408a226
commit ab0cceaa08
4 changed files with 58 additions and 47 deletions
+11 -8
View File
@@ -18,10 +18,10 @@ import { createApiRef } from '@backstage/core';
import {
Service,
Incident,
Options,
PagerDutyClientConfig,
RequestOptions,
ClientApiConfig,
ServicesResponse,
IncidentResponse,
IncidentsResponse,
OnCallsResponse,
OnCall,
} from '../components/types';
@@ -41,7 +41,7 @@ export class PagerDutyClientApi implements PagerDutyClient {
private API_URL = 'https://api.pagerduty.com';
private EVENTS_API_URL = 'https://events.pagerduty.com/v2';
constructor(private readonly config?: PagerDutyClientConfig) {}
constructor(private readonly config?: ClientApiConfig) {}
async getServiceByIntegrationKey(integrationKey: string): Promise<Service[]> {
if (!this.config?.token) {
@@ -62,7 +62,7 @@ export class PagerDutyClientApi implements PagerDutyClient {
const params = `service_ids[]=${serviceId}`;
const url = `${this.API_URL}/incidents?${params}`;
const { incidents } = await this.getByUrl<IncidentResponse>(url);
const { incidents } = await this.getByUrl<IncidentsResponse>(url);
return incidents;
}
@@ -74,9 +74,9 @@ export class PagerDutyClientApi implements PagerDutyClient {
const params = `include[]=users&escalation_policy_ids[]=${policyId}`;
const url = `${this.API_URL}/oncalls?${params}`;
const { onCalls } = await this.getByUrl<OnCallsResponse>(url);
const { oncalls } = await this.getByUrl<OnCallsResponse>(url);
return onCalls;
return oncalls;
}
triggerPagerDutyAlarm(
@@ -127,7 +127,10 @@ export class PagerDutyClientApi implements PagerDutyClient {
return response.json();
}
private async request(url: string, options: Options): Promise<Response> {
private async request(
url: string,
options: RequestOptions,
): Promise<Response> {
try {
const response = await fetch(url, options);
if (!response.ok) {
@@ -45,13 +45,13 @@ export const PagerDutyServiceCard = ({ entity }: Props) => {
const services = await api.getServiceByIntegrationKey(integrationKey);
// TODO check services length
const service = services[0];
const incidents = await api.getIncidentsByServiceId(service.id);
const oncalls = await api.getOnCallByPolicyId(service.escalation_policy.id);
const users = oncalls.map(item => item.user);
return {
incidents,
oncalls,
users,
id: service.id,
name: service.name,
homepageUrl: service.html_url,
@@ -89,9 +89,9 @@ export const PagerDutyServiceCard = ({ entity }: Props) => {
content={
<>
<Incidents incidents={value!.incidents} />
<EscalationPolicy escalation={value!.oncalls} />
<EscalationPolicy users={value!.users} />
</>
}
></PagerdutyCard>
/>
);
};
+16
View File
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import SvgIcon, { SvgIconProps } from '@material-ui/core/SvgIcon';
+27 -35
View File
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export type Incident = {
id: string;
title: string;
@@ -32,41 +33,14 @@ export type Service = {
name: string;
html_url: string;
integrationKey: string;
escalation_policy: OnCall;
escalation_policy: {
id: string;
user: User;
};
};
export type ResponseService = {
services: Service[];
more: boolean;
};
export type ResponseOnCall = {
id: string;
name: string;
email: string;
homepageUrl: string;
};
export type Options = {
method: string;
headers: HeadersInit;
body?: BodyInit;
};
export type PagerDutyClientConfig = {
token?: string;
};
export type ServicesResponse = {
services: Service[];
};
export type IncidentResponse = {
incidents: Incident[];
};
export type OnCallsResponse = {
onCalls: OnCall[];
export type OnCall = {
user: User;
};
export type User = {
@@ -77,6 +51,24 @@ export type User = {
name: string;
};
export type OnCall = {
user: User;
export type ClientApiConfig = {
token?: string;
};
export type ServicesResponse = {
services: Service[];
};
export type IncidentsResponse = {
incidents: Incident[];
};
export type OnCallsResponse = {
oncalls: OnCall[];
};
export type RequestOptions = {
method: string;
headers: HeadersInit;
body?: BodyInit;
};