PromucFlow_constructor/app/client/src/api/ActionAPI.tsx

126 lines
2.9 KiB
TypeScript
Raw Normal View History

import API, { HttpMethod } from "./Api";
import { ApiResponse, GenericApiResponse, ResponseMeta } from "./ApiResponses";
2019-09-24 12:36:03 +00:00
import { APIRequest } from "../constants/ApiConstants";
import { AxiosPromise } from "axios";
2019-09-12 11:44:18 +00:00
export interface CreateActionRequest<T> extends APIRequest {
2019-09-16 10:37:38 +00:00
resourceId: string;
2019-10-21 15:12:45 +00:00
pageId: string;
name: string;
2019-09-16 10:37:38 +00:00
actionConfiguration: T;
2019-09-12 11:44:18 +00:00
}
export interface UpdateActionRequest<T> extends CreateActionRequest<T> {
2019-09-16 10:37:38 +00:00
actionId: string;
2019-09-12 11:44:18 +00:00
}
export interface APIConfig {
resourceId: string;
2019-10-21 15:12:45 +00:00
pageId: string;
name: string;
2019-09-16 10:37:38 +00:00
requestHeaders: Record<string, string>;
2019-10-21 15:12:45 +00:00
httpMethod: HttpMethod;
2019-09-16 10:37:38 +00:00
path: string;
body: JSON;
queryParams: Record<string, string>;
actionId: string;
}
export interface Property {
key: string;
value: string;
}
export interface APIConfigRequest {
headers: Property[];
2019-10-21 15:12:45 +00:00
httpMethod: string;
path: string;
body: JSON | string | Record<string, any> | null;
queryParameters: Property[];
2019-09-12 11:44:18 +00:00
}
export interface QueryConfig {
2019-09-16 10:37:38 +00:00
queryString: string;
2019-09-12 11:44:18 +00:00
}
2019-09-16 10:37:38 +00:00
export interface ActionCreateUpdateResponse extends ApiResponse {
2019-10-21 15:12:45 +00:00
id: string;
jsonPathKeys: Record<string, string>;
}
export interface RestAction {
id: string;
name: string;
resourceId: string;
pluginId: string;
pageId?: string;
2019-10-22 14:54:23 +00:00
actionConfiguration: Partial<APIConfigRequest>;
2019-09-12 11:44:18 +00:00
}
2019-09-12 13:44:25 +00:00
export interface ExecuteActionRequest extends APIRequest {
2019-09-16 10:37:38 +00:00
actionId: string;
2019-11-01 07:11:32 +00:00
params?: Property[];
2019-09-12 13:44:25 +00:00
}
export interface ExecuteActionResponse extends ApiResponse {
2019-09-16 10:37:38 +00:00
actionId: string;
data: any;
2019-09-12 13:44:25 +00:00
}
export interface ActionApiResponse {
responseMeta?: ResponseMeta;
body: JSON;
headers: Record<string, string[]>;
statusCode: string;
duration: string;
size: string;
}
class ActionAPI extends API {
static url = "v1/actions";
2019-09-12 11:44:18 +00:00
static fetchAPI(id: string): AxiosPromise<GenericApiResponse<RestAction>> {
2019-10-21 15:12:45 +00:00
return API.get(`${ActionAPI.url}/${id}`);
}
static createAPI(
apiConfig: RestAction,
): AxiosPromise<ActionCreateUpdateResponse> {
2019-10-21 15:12:45 +00:00
return API.post(ActionAPI.url, apiConfig);
}
static fetchActions(): AxiosPromise<GenericApiResponse<RestAction[]>> {
2019-10-21 15:12:45 +00:00
return API.get(ActionAPI.url);
}
static updateAPI(
apiConfig: Partial<RestAction>,
): AxiosPromise<ActionCreateUpdateResponse> {
2019-10-21 15:12:45 +00:00
return API.put(`${ActionAPI.url}/${apiConfig.id}`, null, apiConfig);
2019-09-12 11:44:18 +00:00
}
2019-10-21 15:12:45 +00:00
static deleteAction(id: string) {
return API.delete(`${ActionAPI.url}/${id}`);
2019-09-12 11:44:18 +00:00
}
2019-09-16 10:37:38 +00:00
static createQuery(
createQuery: CreateActionRequest<QueryConfig>,
): AxiosPromise<ActionCreateUpdateResponse> {
return API.post(ActionAPI.url, createQuery);
2019-09-12 11:44:18 +00:00
}
2019-09-16 10:37:38 +00:00
static updateQuery(
updateQuery: UpdateActionRequest<QueryConfig>,
): AxiosPromise<ActionCreateUpdateResponse> {
return API.post(ActionAPI.url, updateQuery);
2019-09-12 13:44:25 +00:00
}
2019-09-16 10:37:38 +00:00
static executeAction(
executeAction: ExecuteActionRequest,
): AxiosPromise<ActionApiResponse> {
return API.post(ActionAPI.url + "/execute", executeAction);
2019-09-16 10:37:38 +00:00
}
2019-09-12 12:19:46 +00:00
}
2019-09-12 11:44:18 +00:00
2019-09-16 10:37:38 +00:00
export default ActionAPI;