Skip to content

Commit

Permalink
Improve API helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 30, 2020
1 parent 9c54ea4 commit 1a64bf6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
16 changes: 6 additions & 10 deletions x-pack/plugins/actions/server/builtin_action_types/case/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,34 +145,30 @@ export const throwIfNotAlive = (
}
};

export const request = async ({
export const request = async <T = unknown>({
axios,
url,
method = 'get',
data = {},
data,
}: {
axios: AxiosInstance;
url: string;
method?: Method;
// This will have to remain `any` until we can extend connectors with generics
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data?: any;
data?: T;
}): Promise<AxiosResponse> => {
const res = await axios(url, { method, data });
const res = await axios(url, { method, data: data ?? {} });
throwIfNotAlive(res.status, res.headers['content-type']);
return res;
};

export const patch = ({
export const patch = async <T = unknown>({
axios,
url,
data,
}: {
axios: AxiosInstance;
url: string;
// This will have to remain `any` until we can extend connectors with generics
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any;
data: T;
}): Promise<AxiosResponse> => {
return request({
axios,
Expand Down
14 changes: 10 additions & 4 deletions x-pack/plugins/actions/server/builtin_action_types/jira/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import axios from 'axios';

import { ExternalServiceCredentials, ExternalService, ExternalServiceParams } from '../case/types';
import { JiraPublicConfigurationType, JiraSecretConfigurationType } from './types';
import {
JiraPublicConfigurationType,
JiraSecretConfigurationType,
CreateIncidentRequest,
UpdateIncidentRequest,
CreateCommentRequest,
} from './types';

import * as i18n from './translations';
import { getErrorMessage, request } from '../case/utils';
Expand Down Expand Up @@ -66,7 +72,7 @@ export const createExternalService = ({
// The function makes two calls when creating an issue. One to create the issue and one to get
// the created issue with all the necessary fields.
try {
const res = await request({
const res = await request<CreateIncidentRequest>({
axios: axiosInstance,
url: `${incidentUrl}`,
method: 'post',
Expand All @@ -92,7 +98,7 @@ export const createExternalService = ({

const updateIncident = async ({ incidentId, incident }: ExternalServiceParams) => {
try {
await request({
await request<UpdateIncidentRequest>({
axios: axiosInstance,
method: 'put',
url: `${incidentUrl}/${incidentId}`,
Expand All @@ -119,7 +125,7 @@ export const createExternalService = ({

const createComment = async ({ incidentId, comment, field }: ExternalServiceParams) => {
try {
const res = await request({
const res = await request<CreateCommentRequest>({
axios: axiosInstance,
method: 'post',
url: getCommentsURL(incidentId),
Expand Down
21 changes: 21 additions & 0 deletions x-pack/plugins/actions/server/builtin_action_types/jira/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@ import { JiraPublicConfigurationSchema, JiraSecretConfigurationSchema } from './

export type JiraPublicConfigurationType = TypeOf<typeof JiraPublicConfigurationSchema>;
export type JiraSecretConfigurationType = TypeOf<typeof JiraSecretConfigurationSchema>;

interface CreateIncidentBasicRequestArgs {
summary: string;
description: string;
}
interface CreateIncidentRequestArgs extends CreateIncidentBasicRequestArgs {
project: { key: string };
issuetype: { name: string };
}

export interface CreateIncidentRequest {
fields: CreateIncidentRequestArgs;
}

export interface UpdateIncidentRequest {
fields: Partial<CreateIncidentBasicRequestArgs>;
}

export interface CreateCommentRequest {
body: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { ExternalServiceCredentials, ExternalService, ExternalServiceParams } fr
import { addTimeZoneToDate, patch, request, getErrorMessage } from '../case/utils';

import * as i18n from './translations';
import { ServiceNowPublicConfigurationType, ServiceNowSecretConfigurationType } from './types';
import {
ServiceNowPublicConfigurationType,
ServiceNowSecretConfigurationType,
CreateIncidentRequest,
UpdateIncidentRequest,
CreateCommentRequest,
} from './types';

const API_VERSION = 'v2';
const INCIDENT_URL = `api/now/${API_VERSION}/table/incident`;
Expand Down Expand Up @@ -57,7 +63,7 @@ export const createExternalService = ({

const createIncident = async ({ incident }: ExternalServiceParams) => {
try {
const res = await request({
const res = await request<CreateIncidentRequest>({
axios: axiosInstance,
url: `${incidentUrl}`,
method: 'post',
Expand All @@ -79,7 +85,7 @@ export const createExternalService = ({

const updateIncident = async ({ incidentId, incident }: ExternalServiceParams) => {
try {
const res = await patch({
const res = await patch<UpdateIncidentRequest>({
axios: axiosInstance,
url: `${incidentUrl}/${incidentId}`,
data: { ...incident },
Expand All @@ -103,7 +109,7 @@ export const createExternalService = ({

const createComment = async ({ incidentId, comment, field }: ExternalServiceParams) => {
try {
const res = await patch({
const res = await patch<CreateCommentRequest>({
axios: axiosInstance,
url: `${commentUrl}/${incidentId}`,
data: { [field]: comment.comment },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ export {
ExternalIncidentServiceConfiguration as ServiceNowPublicConfigurationType,
ExternalIncidentServiceSecretConfiguration as ServiceNowSecretConfigurationType,
} from '../case/types';

export interface CreateIncidentRequest {
summary: string;
description: string;
}

export type UpdateIncidentRequest = Partial<CreateIncidentRequest>;

export interface CreateCommentRequest {
[key: string]: string;
}

0 comments on commit 1a64bf6

Please sign in to comment.