PromucFlow_constructor/app/client/src/api/DatasourcesApi.ts
sneha122 daeeb21fdb
fix: selected gsheets issue fixed (#34659)
## Description
Fixes release blocker issue:
https://github.com/appsmithorg/appsmith/issues/34654


Fixes #`Issue Number`  
_or_  
Fixes [`Issue
URL`](https://github.com/appsmithorg/appsmith/issues/34654)
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/9773245154>
> Commit: d21b7eeb4724d584c65c88bfd029da200c893360
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=9773245154&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.All`
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


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

## Summary by CodeRabbit

- **New Features**
- Improved handling of authentication status in data source management.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: “sneha122” <“sneha@appsmith.com”>
2024-07-03 13:56:26 +05:30

250 lines
8.1 KiB
TypeScript

import { DEFAULT_TEST_DATA_SOURCE_TIMEOUT_MS } from "@appsmith/constants/ApiConstants";
import API from "api/Api";
import type { ApiResponse } from "./ApiResponses";
import type { AxiosPromise } from "axios";
import type { Datasource, DatasourceStorage } from "entities/Datasource";
export interface CreateDatasourceConfig {
name: string;
pluginId: string;
type?: string;
// key in the map representation of environment id of type string
datasourceStorages: Record<string, DatasourceStorage>;
//Passed for logging purposes.
appName?: string;
}
// type executeQueryData = Array<{ key?: string; value?: string }>;
type executeQueryData = Record<string, any>;
interface executeDatasourceQueryRequest {
datasourceId: string;
data?: executeQueryData;
}
class DatasourcesApi extends API {
static url = "v1/datasources";
static async fetchDatasources(
workspaceId: string,
): Promise<AxiosPromise<ApiResponse<Datasource[]>>> {
return API.get(DatasourcesApi.url + `?workspaceId=${workspaceId}`);
}
static async createDatasource(
datasourceConfig: Partial<Datasource>,
): Promise<any> {
// This here abomination is to remove several fields that are not accepted by the server.
for (const [name, storage] of Object.entries(
datasourceConfig.datasourceStorages || {},
)) {
datasourceConfig = {
...datasourceConfig,
isValid: undefined,
datasourceStorages: {
...datasourceConfig.datasourceStorages,
[name]: {
...storage,
isValid: undefined,
toastMessage: undefined,
datasourceConfiguration: {
...storage.datasourceConfiguration,
isValid: undefined,
authentication: DatasourcesApi.cleanAuthenticationObject(
storage.datasourceConfiguration.authentication,
),
connection: storage.datasourceConfiguration.connection && {
...storage.datasourceConfiguration.connection,
ssl: {
...storage.datasourceConfiguration.connection.ssl,
authTypeControl: undefined,
certificateType: undefined,
},
},
},
},
},
} as any;
}
return API.post(DatasourcesApi.url, datasourceConfig);
}
static cleanAuthenticationObject(authentication: any): any {
if (!authentication) {
return undefined;
}
const clean: any = {
authenticationType: authentication.authenticationType ?? "dbAuth",
};
switch (clean.authenticationType) {
case "dbAuth":
clean.authType = authentication.authType;
clean.username = authentication.username;
clean.password = authentication.password;
clean.databaseName = authentication.databaseName;
break;
case "oAuth2":
clean.grantType = authentication.grantType;
clean.isTokenHeader = authentication.isTokenHeader;
clean.isAuthorizationHeader = authentication.isAuthorizationHeader;
clean.clientId = authentication.clientId;
clean.clientSecret = authentication.clientSecret;
clean.authorizationUrl = authentication.authorizationUrl;
clean.expiresIn = authentication.expiresIn;
clean.accessTokenUrl = authentication.accessTokenUrl;
clean.scopeString = authentication.scopeString;
clean.scope = authentication.scope;
clean.sendScopeWithRefreshToken =
authentication.sendScopeWithRefreshToken;
clean.refreshTokenClientCredentialsLocation =
authentication.refreshTokenClientCredentialsLocation;
clean.headerPrefix = authentication.headerPrefix;
clean.customTokenParameters = authentication.customTokenParameters;
clean.audience = authentication.audience;
clean.resource = authentication.resource;
clean.useSelfSignedCert = authentication.useSelfSignedCert;
clean.authenticationStatus = authentication.authenticationStatus;
break;
case "basic":
clean.username = authentication.username;
clean.password = authentication.password;
break;
case "apiKey":
clean.addTo = authentication.addTo;
clean.label = authentication.label;
clean.headerPrefix = authentication.headerPrefix;
clean.value = authentication.value;
break;
case "bearerToken":
clean.bearerToken = authentication.bearerToken;
break;
case "snowflakeKeyPairAuth":
clean.username = authentication.username;
clean.privateKey = authentication.privateKey;
clean.passphrase = authentication.passphrase;
}
return clean;
}
// Api to test current environment datasource
static async testDatasource(
datasourceConfig: Partial<DatasourceStorage>,
pluginId: string,
workspaceId: string,
): Promise<any> {
const payload = {
...datasourceConfig,
pluginId,
workspaceId,
isValid: undefined,
toastMessage: undefined,
datasourceConfiguration: datasourceConfig.datasourceConfiguration && {
...datasourceConfig.datasourceConfiguration,
authentication: DatasourcesApi.cleanAuthenticationObject(
datasourceConfig.datasourceConfiguration.authentication,
),
connection: datasourceConfig.datasourceConfiguration.connection && {
...datasourceConfig.datasourceConfiguration.connection,
ssl: {
...datasourceConfig.datasourceConfiguration.connection.ssl,
certificateType: undefined,
},
},
},
};
return API.post(`${DatasourcesApi.url}/test`, payload, undefined, {
timeout: DEFAULT_TEST_DATA_SOURCE_TIMEOUT_MS,
});
}
// Api to update datasource name.
static async updateDatasource(
datasourceConfig: Partial<Datasource>,
id: string,
): Promise<any> {
return API.put(DatasourcesApi.url + `/${id}`, datasourceConfig);
}
// Api to update specific datasource storage/environment configuration
static async updateDatasourceStorage(
datasourceStorage: Partial<DatasourceStorage>,
): Promise<any> {
const payload = {
...datasourceStorage,
isValid: undefined,
toastMessage: undefined,
datasourceConfiguration: datasourceStorage.datasourceConfiguration && {
...datasourceStorage.datasourceConfiguration,
authentication: DatasourcesApi.cleanAuthenticationObject(
datasourceStorage.datasourceConfiguration.authentication,
),
connection: datasourceStorage.datasourceConfiguration.connection && {
...datasourceStorage.datasourceConfiguration.connection,
ssl: {
...datasourceStorage.datasourceConfiguration.connection.ssl,
authTypeControl: undefined,
certificateType: undefined,
},
},
},
};
return API.put(DatasourcesApi.url + `/datasource-storages`, payload);
}
static async deleteDatasource(id: string): Promise<any> {
return API.delete(DatasourcesApi.url + `/${id}`);
}
static async fetchDatasourceStructure(
id: string,
ignoreCache = false,
): Promise<any> {
return API.get(
DatasourcesApi.url + `/${id}/structure?ignoreCache=${ignoreCache}`,
);
}
static async fetchMockDatasources(): Promise<
AxiosPromise<ApiResponse<Datasource[]>>
> {
return API.get(DatasourcesApi.url + "/mocks");
}
static async addMockDbToDatasources(
name: string,
workspaceId: string,
pluginId: string,
packageName: string,
): Promise<any> {
return API.post(DatasourcesApi.url + `/mocks`, {
name,
workspaceId,
pluginId,
packageName,
});
}
static async executeDatasourceQuery({
data,
datasourceId,
}: executeDatasourceQueryRequest) {
return API.post(
DatasourcesApi.url + `/${datasourceId}` + `/schema-preview`,
data,
);
}
static async executeGoogleSheetsDatasourceQuery({
data,
datasourceId,
}: executeDatasourceQueryRequest) {
return API.post(DatasourcesApi.url + `/${datasourceId}` + `/trigger`, data);
}
}
export default DatasourcesApi;