PromucFlow_constructor/app/client/src/api/ActionAPI.tsx
subratadeypappu 19e2e5785e
Fix/16994 refactor common datatype handling (#17429)
* fix:Add array datatype to execute request

* feat: Consume and store type of array elements in Param class (#16994)

* Append param instead of clientDataType in varargs (#16994)

* Refactor common data type handling w.r.t newer structure (#16994)

This commit takes care of the following items:
- It minimizes the number of usage to the older stringToKnownDataTypeConverter method
- Modifies the existing test cases to conform to the newer structure
- Marks stringToKnownDataTypeConverter method as deprecated to discourage further use

* Remove comma delimited numbers from valid test cases (#16994)

* Fix extracting clientDataType from varargs in MySQL (#16994)

* Pass param as a dedicated parameter in json smart replacement (#16994)

* Remove varargs from json smart replacement method (#16994)

* Move BsonType to mongoplugin module (#16994)

* Introduce NullArrayType and refactor BsonType test cases (#16994)

* Add new test cases on numeric string with leading zero (#16994)

* Refactor test case name (#16994)

* Add comment on the ordering of Json and Bson types (#16994)

* Add comment on the ordering of Json and Bson types (#16994)

* Add NullArrayType in Postgres and introduce postgres-specific types (#16994)

* Add data type test cases for Postgres and change as per review comments (#16994)

Co-authored-by: ChandanBalajiBP <chandan@appsmith.com>
2022-10-18 11:02:37 +05:30

199 lines
5.2 KiB
TypeScript

import API, { HttpMethod } from "api/Api";
import { ApiResponse } from "./ApiResponses";
import { DEFAULT_EXECUTE_ACTION_TIMEOUT_MS } from "@appsmith/constants/ApiConstants";
import axios, { AxiosPromise, CancelTokenSource } from "axios";
import { Action, ActionViewMode } from "entities/Action";
import { APIRequest } from "constants/AppsmithActionConstants/ActionConstants";
import { WidgetType } from "constants/WidgetConstants";
export interface CreateActionRequest<T> extends APIRequest {
datasourceId: string;
pageId: string;
name: string;
actionConfiguration: T;
}
export interface UpdateActionRequest<T> extends CreateActionRequest<T> {
actionId: string;
}
export interface Property {
key: string;
value?: string;
}
export interface BodyFormData {
editable: boolean;
mandatory: boolean;
description: string;
key: string;
value?: string;
type: string;
}
export interface QueryConfig {
queryString: string;
}
export type ActionCreateUpdateResponse = ApiResponse & {
id: string;
jsonPathKeys: Record<string, string>;
};
export type PaginationField = "PREV" | "NEXT";
export interface ExecuteActionRequest extends APIRequest {
actionId: string;
params?: Property[];
paginationField?: PaginationField;
viewMode: boolean;
paramProperties: Record<string, string | Record<string, string[]>>;
}
export type ExecuteActionResponse = ApiResponse & {
actionId: string;
};
export interface ActionApiResponseReq {
headers: Record<string, string[]>;
body: Record<string, unknown> | null;
httpMethod: HttpMethod | "";
url: string;
}
export type ActionExecutionResponse = ApiResponse<{
body: Record<string, unknown> | string;
headers: Record<string, string[]>;
statusCode: string;
isExecutionSuccess: boolean;
request: ActionApiResponseReq;
errorType?: string;
dataTypes: any[];
}> & {
clientMeta: {
duration: string;
size: string;
};
};
export interface SuggestedWidget {
type: WidgetType;
bindingQuery: string;
}
export interface ActionResponse {
body: unknown;
headers: Record<string, string[]>;
request?: ActionApiResponseReq;
statusCode: string;
dataTypes: Record<string, string>[];
duration: string;
size: string;
isExecutionSuccess?: boolean;
suggestedWidgets?: SuggestedWidget[];
messages?: Array<string>;
errorType?: string;
readableError?: string;
responseDisplayFormat?: string;
}
export interface MoveActionRequest {
action: Action;
destinationPageId: string;
}
export interface CopyActionRequest {
action: Action;
pageId: string;
}
export interface UpdateActionNameRequest {
pageId: string;
actionId: string;
layoutId: string;
newName: string;
oldName: string;
}
class ActionAPI extends API {
static url = "v1/actions";
static apiUpdateCancelTokenSource: CancelTokenSource;
static queryUpdateCancelTokenSource: CancelTokenSource;
static abortActionExecutionTokenSource: CancelTokenSource;
static createAction(
apiConfig: Partial<Action>,
): AxiosPromise<ActionCreateUpdateResponse> {
return API.post(ActionAPI.url, apiConfig);
}
static fetchActions(
applicationId: string,
): AxiosPromise<ApiResponse<Action[]>> {
return API.get(ActionAPI.url, { applicationId });
}
static fetchActionsForViewMode(
applicationId: string,
): AxiosPromise<ApiResponse<ActionViewMode[]>> {
return API.get(`${ActionAPI.url}/view`, { applicationId });
}
static fetchActionsByPageId(
pageId: string,
): AxiosPromise<ApiResponse<Action[]>> {
return API.get(ActionAPI.url, { pageId });
}
static updateAction(
apiConfig: Partial<Action>,
): AxiosPromise<ActionCreateUpdateResponse> {
if (ActionAPI.apiUpdateCancelTokenSource) {
ActionAPI.apiUpdateCancelTokenSource.cancel();
}
ActionAPI.apiUpdateCancelTokenSource = axios.CancelToken.source();
const action = Object.assign({}, apiConfig);
// While this line is not required, name can not be changed from this endpoint
delete action.name;
return API.put(`${ActionAPI.url}/${action.id}`, action, undefined, {
cancelToken: ActionAPI.apiUpdateCancelTokenSource.token,
});
}
static updateActionName(updateActionNameRequest: UpdateActionNameRequest) {
return API.put(ActionAPI.url + "/refactor", updateActionNameRequest);
}
static deleteAction(id: string) {
return API.delete(`${ActionAPI.url}/${id}`);
}
static executeAction(
executeAction: FormData,
timeout?: number,
): AxiosPromise<ActionExecutionResponse> {
ActionAPI.abortActionExecutionTokenSource = axios.CancelToken.source();
return API.post(ActionAPI.url + "/execute", executeAction, undefined, {
timeout: timeout || DEFAULT_EXECUTE_ACTION_TIMEOUT_MS,
headers: {
accept: "application/json",
"Content-Type": "multipart/form-data",
},
cancelToken: ActionAPI.abortActionExecutionTokenSource.token,
});
}
static moveAction(moveRequest: MoveActionRequest) {
return API.put(ActionAPI.url + "/move", moveRequest, undefined, {
timeout: DEFAULT_EXECUTE_ACTION_TIMEOUT_MS,
});
}
static toggleActionExecuteOnLoad(actionId: string, shouldExecute: boolean) {
return API.put(ActionAPI.url + `/executeOnLoad/${actionId}`, undefined, {
flag: shouldExecute.toString(),
});
}
}
export default ActionAPI;