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

113 lines
2.9 KiB
TypeScript
Raw Normal View History

import API, { HttpMethod } from "./Api";
2019-09-16 10:37:38 +00:00
import { ApiResponse } from "./ApiResponses";
import { APIRequest } from "./ApiRequests";
import { mapToPropList } from "../utils/AppsmithUtils";
2019-09-12 11:44:18 +00:00
export interface CreateActionRequest<T> extends APIRequest {
2019-09-16 10:37:38 +00:00
resourceId: string;
actionName: 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;
actionName: string;
2019-09-16 10:37:38 +00:00
requestHeaders: Record<string, string>;
method: HttpMethod;
path: string;
body: JSON;
queryParams: Record<string, string>;
actionId: string;
}
export interface Property {
key: string;
value: string;
}
export interface APIConfigRequest {
headers: Property[];
httpMethod: HttpMethod;
path: string;
body: JSON;
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 {
actionId: string;
dynamicBindingMap: Record<string, string>;
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;
dynamicBindingList?: 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
}
class ActionAPI extends API {
static url = "v1/actions";
2019-09-12 11:44:18 +00:00
static createAPI(apiConfig: APIConfig): Promise<ActionCreateUpdateResponse> {
const createAPI: CreateActionRequest<APIConfigRequest> = {
resourceId: apiConfig.resourceId,
actionName: apiConfig.actionName,
actionConfiguration: {
httpMethod: apiConfig.method,
path: apiConfig.path,
body: apiConfig.body,
headers: mapToPropList(apiConfig.requestHeaders),
queryParameters: mapToPropList(apiConfig.queryParams),
},
};
return API.post(ActionAPI.url, createAPI);
2019-09-12 11:44:18 +00:00
}
static updateAPI(apiConfig: APIConfig): Promise<ActionCreateUpdateResponse> {
const updateAPI: UpdateActionRequest<APIConfigRequest> = {
resourceId: apiConfig.resourceId,
actionName: apiConfig.actionName,
actionId: apiConfig.actionId,
actionConfiguration: {
httpMethod: apiConfig.method,
path: apiConfig.path,
body: apiConfig.body,
headers: mapToPropList(apiConfig.requestHeaders),
queryParameters: mapToPropList(apiConfig.queryParams),
},
};
return API.post(ActionAPI.url, updateAPI);
2019-09-12 11:44:18 +00:00
}
2019-09-16 10:37:38 +00:00
static createQuery(
createQuery: CreateActionRequest<QueryConfig>,
): Promise<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>,
): Promise<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,
): Promise<ActionCreateUpdateResponse> {
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;