PromucFlow_constructor/app/client/src/api/DatasourcesApi.ts
Ilia d6f249b42d
chore: add blank line eslint rule (#36369)
## Description
Added ESLint rule to force blank lines between statements. 


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!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  -->
> [!CAUTION]
> 🔴 🔴 🔴 Some tests have failed.
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/10924926728>
> Commit: 34f57714a1575ee04e94e03cbcaf95e57a96c86c
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10924926728&attempt=1&selectiontype=test&testsstatus=failed&specsstatus=fail"
target="_blank">Cypress dashboard</a>.
> Tags: @tag.All
> Spec: 
> The following are new failures, please fix them before merging the PR:
<ol>
> <li>cypress/e2e/Regression/ClientSide/Anvil/AnvilModal_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCheckboxGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCurrencyInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilIconButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInlineButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilParagraphWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilPhoneInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilStatsWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilTableWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilToolbarButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilZoneSectionWidgetSnapshot_spec.ts</ol>
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/identified-flaky-tests-65890b3c81d7400d08fa9ee3?branch=master"
target="_blank">List of identified flaky tests</a>.
> <hr>Wed, 18 Sep 2024 16:33:36 UTC
<!-- end of auto-generated comment: Cypress test results  -->


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

---------

Co-authored-by: Valera Melnikov <valera@appsmith.com>
2024-09-18 19:35:28 +03:00

266 lines
9.0 KiB
TypeScript

import { DEFAULT_TEST_DATA_SOURCE_TIMEOUT_MS } from "ee/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 }>;
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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>,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): 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,
connection: storage.datasourceConfiguration.connection && {
...storage.datasourceConfiguration.connection,
ssl: {
...storage.datasourceConfiguration.connection.ssl,
authTypeControl: undefined,
certificateType: undefined,
},
},
},
},
},
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
}
return API.post(DatasourcesApi.url, datasourceConfig);
}
// Need for when we add strict type checking back on server
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static cleanAuthenticationObject(authentication: any): any {
if (!authentication) {
return undefined;
}
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> {
const payload = {
...datasourceConfig,
pluginId,
workspaceId,
isValid: undefined,
toastMessage: undefined,
datasourceConfiguration: datasourceConfig.datasourceConfiguration && {
...datasourceConfig.datasourceConfiguration,
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,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> {
return API.put(DatasourcesApi.url + `/${id}`, datasourceConfig);
}
// Api to update specific datasource storage/environment configuration
static async updateDatasourceStorage(
datasourceStorage: Partial<DatasourceStorage>,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> {
const payload = {
...datasourceStorage,
isValid: undefined,
toastMessage: undefined,
datasourceConfiguration: datasourceStorage.datasourceConfiguration && {
...datasourceStorage.datasourceConfiguration,
connection: datasourceStorage.datasourceConfiguration.connection && {
...datasourceStorage.datasourceConfiguration.connection,
ssl: {
...datasourceStorage.datasourceConfiguration.connection.ssl,
authTypeControl: undefined,
certificateType: undefined,
},
},
},
};
return API.put(DatasourcesApi.url + `/datasource-storages`, payload);
}
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static async deleteDatasource(id: string): Promise<any> {
return API.delete(DatasourcesApi.url + `/${id}`);
}
static async fetchDatasourceStructure(
id: string,
ignoreCache = false,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): 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,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): 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;