PromucFlow_constructor/app/client/src/sagas/CurlImportSagas.ts
Nilesh Sarupriya c267965090
chore: refactor curl import API to support context type (#30934)
## Description

Support curl imports for different contexts. 

### Server changes

Previous API: 

`/api/v1/import?type=CURL&pageId={pageId}&name=Api2&workspaceId={workspaceId}`

New API: 
With context type, it will create for the specific context.

`/api/v1/import?type=CURL&contextId={contextId}&name=Api1&workspaceId={workspaceId}&contextType={contextType}`

Without context type, it will create for the page.

`/api/v1/import?type=CURL&contextId={contextId}&name=Api1&workspaceId={workspaceId}`

### Client changes
- Integrate api changes for curl import. Updated request params type and
interfaces for the saga functions
- Updated the form value types for the curl import editor



#### PR fixes following issue(s)
Fixes [[Task]: Curl Import isn't
working.](https://github.com/appsmithorg/appsmith/issues/30933)

#### Media
> N/A

#### Type of change
- Chore (housekeeping or task changes that don't impact user perception)

## Testing
>
#### How Has This Been Tested?
- [x] Manual (using postman)
- [x] JUnit (existing test cases should work)

#### Test Plan
> N/A

#### Issues raised during DP testing
> None

## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **New Features**
- Enhanced API import functionality with a new context-aware mechanism,
allowing for more flexible integration within different parts of the
application.
- Updated various components and services to support the new contextId
and contextType parameters for improved data handling and redirection
logic.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Nilesh Sarupriya <20905988+nsarupr@users.noreply.github.com>
Co-authored-by: Ayush Pahwa <ayushpahwa96@gmail.com>
2024-02-14 12:18:30 +05:30

66 lines
2.0 KiB
TypeScript

import { takeLatest, put, all, select } from "redux-saga/effects";
import type { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
import {
ReduxActionTypes,
ReduxActionErrorTypes,
} from "@appsmith/constants/ReduxActionConstants";
import { validateResponse } from "sagas/ErrorSagas";
import type { CurlImportRequest } from "api/ImportApi";
import CurlImportApi from "api/ImportApi";
import type { ApiResponse } from "api/ApiResponses";
import AnalyticsUtil from "utils/AnalyticsUtil";
import { getCurrentWorkspaceId } from "@appsmith/selectors/selectedWorkspaceSelectors";
import transformCurlImport from "transformers/CurlImportTransformer";
import history from "utils/history";
import { CURL } from "constants/AppsmithActionConstants/ActionConstants";
import { apiEditorIdURL } from "@appsmith/RouteBuilder";
export function* curlImportSaga(action: ReduxAction<CurlImportRequest>) {
const { contextId, contextType, name, type } = action.payload;
let { curl } = action.payload;
try {
curl = transformCurlImport(curl);
const workspaceId: string = yield select(getCurrentWorkspaceId);
const request: CurlImportRequest = {
type,
contextId,
name,
curl,
workspaceId,
contextType,
};
const response: ApiResponse = yield CurlImportApi.curlImport(request);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
AnalyticsUtil.logEvent("IMPORT_API", {
importSource: CURL,
});
yield put({
type: ReduxActionTypes.SUBMIT_CURL_FORM_SUCCESS,
payload: response.data,
});
history.push(
// @ts-expect-error: response.data is of type unknown
apiEditorIdURL({ parentEntityId: contextId, apiId: response.data.id }),
);
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.SUBMIT_CURL_FORM_ERROR,
payload: {
error,
},
});
}
}
export default function* curlImportSagas() {
yield all([
takeLatest(ReduxActionTypes.SUBMIT_CURL_FORM_INIT, curlImportSaga),
]);
}