Skip to content

update @clevercloud/client and use it instead of local declarations #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"typecheck:stats": "node tasks/typechecking-stats.js"
},
"dependencies": {
"@clevercloud/client": "^9.2.0",
"@clevercloud/client": "^9.2.1",
"@lit-labs/motion": "^1.0.7",
"@lit-labs/virtualizer": "^2.0.14",
"@shoelace-style/shoelace": "^2.18.0",
Expand Down
34 changes: 11 additions & 23 deletions src/components/cc-domain-management/cc-domain-management.smart.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { defineSmartComponent } from '../../lib/smart/define-smart-component.js'
import { i18n } from '../../translations/translation.js';
import '../cc-smart-container/cc-smart-container.js';
import { CcDomainManagement } from './cc-domain-management.js';
// @ts-expect-error FIXME: remove when clever-client exports types
import { getDefaultLoadBalancersDnsInfo } from '@clevercloud/client/esm/api/v4/load-balancers.js';

/**
* @typedef {import('./cc-domain-management.types.js').DomainManagementFormState} DomainManagementFormState
Expand Down Expand Up @@ -317,29 +319,15 @@ function markAsPrimaryDomain({ apiConfig, ownerId, appId, id }) {
function fetchDnsInfo({ apiConfig, ownerId, appId, signal }) {
return getDefaultLoadBalancersDnsInfo({ appId, ownerId })
.then(sendToApi({ apiConfig, signal }))
.then((defaultLoadBalancers) => {
const defaultLoadBalancerData = defaultLoadBalancers[0];
return {
cnameRecord: defaultLoadBalancerData?.dns?.cname,
aRecords: defaultLoadBalancerData?.dns?.a,
};
});
}

/**
* TODO: move this to Clever Client
*
* @param {Object} params
* @param {string} params.ownerId
* @param {string} params.appId
* @returns {Promise<any>}
*/
function getDefaultLoadBalancersDnsInfo({ appId, ownerId }) {
return Promise.resolve({
method: 'get',
url: `/v4/load-balancers/organisations/${ownerId}/applications/${appId}/load-balancers/default`,
headers: { Accept: 'application/json' },
});
.then(
/** @param {Array<{dns?: {cname: string, a: string[]}}>} defaultLoadBalancers */ (defaultLoadBalancers) => {
const defaultLoadBalancerData = defaultLoadBalancers[0];
return {
cnameRecord: defaultLoadBalancerData?.dns?.cname,
aRecords: defaultLoadBalancerData?.dns?.a,
};
},
);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/components/cc-email-list/cc-email-list.smart.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// prettier-ignore
// @ts-expect-error FIXME: remove when clever-client exports types
import { todo_addEmailAddress as addEmailAddress,todo_getEmailAddresses as getEmailAddresses,todo_removeEmailAddress as removeEmailAddress,todo_getConfirmationEmail as sendConfirmationEmail,} from '@clevercloud/client/esm/api/v2/user.js';
// @ts-expect-error FIXME: remove when clever-client exports types
import { get as getSelf } from '@clevercloud/client/esm/api/v2/organisation.js';
import { notify, notifyError, notifySuccess } from '../../lib/notifications.js';
import { sendToApi } from '../../lib/send-to-api.js';
import { defineSmartComponent } from '../../lib/smart/define-smart-component.js';
Expand Down Expand Up @@ -281,11 +283,7 @@ function getApi(apiConfig, signal) {
* @return {Promise<{email: string, emailValidated: boolean}>}
*/
fetchPrimaryEmailAddress() {
return Promise.resolve({
method: 'get',
url: `/v2/self`,
headers: { Accept: 'application/json' },
}).then(sendToApi({ apiConfig, signal }));
return getSelf({}).then(sendToApi({ apiConfig, signal }));
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { defineSmartComponent } from '../../lib/smart/define-smart-component.js'
import { i18n } from '../../translations/translation.js';
import '../cc-smart-container/cc-smart-container.js';
import './cc-env-var-form.js';
// @ts-expect-error FIXME: remove when clever-client exports types
import { getConfigProviderEnv, updateConfigProviderEnv } from '@clevercloud/client/esm/api/v4/addon.js';

/**
* @typedef {import('./cc-env-var-form.js').CcEnvVarForm} CcEnvVarForm
Expand Down Expand Up @@ -112,7 +114,7 @@ function fetchAddon({ apiConfig, signal, ownerId, addonId }) {
* @returns {Promise<Array<EnvVar>>}
*/
async function fetchVariables({ apiConfig, signal, realAddonId }) {
return getConfigProviderEnv({ realAddonId }).then(sendToApi({ apiConfig, signal }));
return getConfigProviderEnv({ configurationProviderId: realAddonId }).then(sendToApi({ apiConfig, signal }));
}

/**
Expand All @@ -123,40 +125,5 @@ async function fetchVariables({ apiConfig, signal, realAddonId }) {
* @returns {Promise<Addon>}
*/
async function updateVariables({ apiConfig, realAddonId, variables }) {
return updateConfigProviderEnv({ realAddonId }, variables).then(sendToApi({ apiConfig }));
}

/**
* TODO: clever-client
*
* @param {object} params
* @param {string} params.realAddonId
* @returns {Promise<{ method: 'get', url: string, headers: { Accept: 'application/json' }}>}
*/
export function getConfigProviderEnv({ realAddonId }) {
return Promise.resolve({
method: 'get',
url: `/v4/addon-providers/config-provider/addons/${realAddonId}/env`,
headers: { Accept: 'application/json' },
// no query params
// no body
});
}

/**
* TODO: clever-client
*
* @param {object} params
* @param {string} params.realAddonId
* @param {Array<EnvVar>} body
* @returns {Promise<{ method: 'put', url: string, headers: { Accept: 'application/json' }, body: Array<EnvVar> }>}
*/
export function updateConfigProviderEnv({ realAddonId }, body) {
return Promise.resolve({
method: 'put',
url: `/v4/addon-providers/config-provider/addons/${realAddonId}/env`,
headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
// no query params
body,
});
return updateConfigProviderEnv({ configurationProviderId: realAddonId }, variables).then(sendToApi({ apiConfig }));
}
45 changes: 25 additions & 20 deletions src/components/cc-grafana-info/cc-grafana-info.smart.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
createGrafanaOrganisation,
deleteGrafanaOrganisation,
getGrafanaOrganisation,
resetGrafanaOrganisation,
} from '../../lib/api-helpers.js';
import { notifyError, notifySuccess } from '../../lib/notifications.js';
import { sendToApi } from '../../lib/send-to-api.js';
import { defineSmartComponent } from '../../lib/smart/define-smart-component.js';
import { i18n } from '../../translations/translation.js';
import '../cc-smart-container/cc-smart-container.js';
import './cc-grafana-info.js';
import {
createGrafanaOrganisation,
deleteGrafanaOrganisation,
getGrafanaOrganisation,
resetGrafanaOrganisation,
// @ts-expect-error FIXME: remove when clever-client exports types
} from '@clevercloud/client/esm/api/v4/saas.js';

/**
* @typedef {import('./cc-grafana-info.js').CcGrafanaInfo} CcGrafanaInfo
Expand Down Expand Up @@ -152,20 +153,24 @@ defineSmartComponent({
function fetchGrafanaOrganisation({ apiConfig, signal, ownerId, grafanaBaseLink }) {
return getGrafanaOrganisation({ id: ownerId })
.then(sendToApi({ apiConfig, signal }))
.then((exposedVarsObject) => {
const grafanaLink = new URL('/d/home/clever-cloud-metrics-home', grafanaBaseLink);
grafanaLink.searchParams.set('orgId', exposedVarsObject.id);
/** @type {GrafanaInfoEnabled} */
const grafanaInfo = { status: 'enabled', link: grafanaLink.toString() };
return grafanaInfo;
})
.catch((error) => {
if (error.response?.status === 404 && error.toString().startsWith('Error: Grafana organization not found')) {
return { status: 'disabled' };
} else {
throw error;
}
});
.then(
/** @param {{id: string}} exposedVarsObject */ (exposedVarsObject) => {
const grafanaLink = new URL('/d/home/clever-cloud-metrics-home', grafanaBaseLink);
grafanaLink.searchParams.set('orgId', exposedVarsObject.id);
/** @type {GrafanaInfoEnabled} */
const grafanaInfo = { status: 'enabled', link: grafanaLink.toString() };
return grafanaInfo;
},
)
.catch(
/** @param {{response?: {status: number}}} error */ (error) => {
if (error.response?.status === 404 && error.toString().startsWith('Error: Grafana organization not found')) {
return { status: 'disabled' };
} else {
throw error;
}
},
);
}

/**
Expand Down
85 changes: 18 additions & 67 deletions src/components/cc-logs-app-runtime/cc-logs-app-runtime.smart.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// @ts-expect-error FIXME: remove when clever-client exports types
import { getAllInstances, getDeployment } from '@clevercloud/client/esm/api/v2/application.js';
// @ts-expect-error FIXME: remove when clever-client exports types
import { pickNonNull } from '@clevercloud/client/esm/pick-non-null.js';
import { getDeployment as getDeploymentV2 } from '@clevercloud/client/esm/api/v2/application.js';
// @ts-expect-error FIXME: remove when clever-client exports types
import { ApplicationLogStream } from '@clevercloud/client/esm/streams/application-logs.js';
import {
getAllApplicationInstances as getApplicationInstancesV4,
getInstance as getInstanceV4,
// @ts-expect-error FIXME: remove when clever-client exports types
} from '@clevercloud/client/esm/api/v4/instance.js';
// @ts-expect-error FIXME: remove when clever-client exports types
import { getApplicationDeployment as getDeploymentV4 } from '@clevercloud/client/esm/api/v4/deployment.js';
import { isLive, lastXDays } from '../../lib/date/date-range-utils.js';
import { LogsStream } from '../../lib/logs/logs-stream.js';
import { sendToApi } from '../../lib/send-to-api.js';
Expand Down Expand Up @@ -880,6 +885,7 @@ class Api {
*/
constructor(apiConfig, ownerId, appId) {
this._apiConfig = apiConfig;
this._applicationRef = { ownerId, applicationId: appId };
this._commonApiPrams = { id: ownerId, appId };
}

Expand All @@ -888,15 +894,15 @@ class Api {
* @returns {Promise<any>}
*/
fetchDeployment(deploymentId) {
return v4.getDeployment({ ...this._commonApiPrams, deploymentId }).then(sendToApi({ apiConfig: this._apiConfig }));
return getDeploymentV4({ ...this._applicationRef, deploymentId }).then(sendToApi({ apiConfig: this._apiConfig }));
}

/**
* @param {string} deploymentId
* @returns {Promise<any>}
*/
fetchDeploymentV2(deploymentId) {
return v2.getDeployment({ ...this._commonApiPrams, deploymentId }).then(sendToApi({ apiConfig: this._apiConfig }));
return getDeploymentV2({ ...this._commonApiPrams, deploymentId }).then(sendToApi({ apiConfig: this._apiConfig }));
}

/**
Expand All @@ -905,71 +911,30 @@ class Api {
* @returns {Promise<Array<any>>}
*/
fetchInstances(since, until) {
return v4
.getInstances({ ...this._commonApiPrams, limit: 100, since, until })
.then(sendToApi({ apiConfig: this._apiConfig }));
return getApplicationInstancesV4({ ...this._applicationRef, limit: 100, since, until }).then(
sendToApi({ apiConfig: this._apiConfig }),
);
}

/**
* @param {string} deploymentId
* @returns {Promise<Array<any>>}
*/
fetchInstancesByDeployment(deploymentId) {
return v4
.getInstances({ ...this._commonApiPrams, limit: 100, deploymentId })
.then(sendToApi({ apiConfig: this._apiConfig }));
return getApplicationInstancesV4({ ...this._applicationRef, limit: 100, deploymentId }).then(
sendToApi({ apiConfig: this._apiConfig }),
);
}

/**
* @param {string} instanceId
* @returns {Promise<Array<any>>}
*/
fetchInstance(instanceId) {
return v4.getInstance({ ...this._commonApiPrams, instanceId }).then(sendToApi({ apiConfig: this._apiConfig }));
return getInstanceV4({ ...this._applicationRef, instanceId }).then(sendToApi({ apiConfig: this._apiConfig }));
}
}

// --- APIs ------

const v4 = {
/**
* @param {{id: string, appId: string, limit?: number, since?: string, until?: string, deploymentId?: string, includeState?: boolean}} params
*/
getInstances(params) {
return Promise.resolve({
method: 'get',
url: `/v4/orchestration/organisations/${params.id}/applications/${params.appId}/instances`,
headers: { Accept: 'application/json' },
queryParams: pickNonNull(params, ['limit', 'since', 'until', 'deploymentId', 'includeState']),
});
},
/**
* @param {{id: string, appId: string, instanceId?: string}} params
*/
getInstance(params) {
return Promise.resolve({
method: 'get',
url: `/v4/orchestration/organisations/${params.id}/applications/${params.appId}/instances/${params.instanceId}`,
headers: { Accept: 'application/json' },
});
},
/**
* @param {{id: string, appId: string, deploymentId?: string}} params
*/
getDeployment(params) {
return Promise.resolve({
method: 'get',
url: `/v4/orchestration/organisations/${params.id}/applications/${params.appId}/deployments/${params.deploymentId}`,
headers: { Accept: 'application/json' },
});
},
};

const v2 = {
getDeployment: getDeployment,
getAllInstances: getAllInstances,
};

// --- utils ------

/**
Expand Down Expand Up @@ -1008,17 +973,3 @@ function isCurrentDeployment(deployment) {
function getErrorStatusCode(e) {
return e.response?.status;
}

//
// function getDeployments (params) {
// // "https://api.clever-cloud.com/v4/orchestration/organisations/orga_8b852f0a-1135-4b4f-8eee-a538f538b640/applications/app_4bc68315-c93e-49eb-8086-430314475568/deployments?limit=1" | jq
// const urlBase = `/orchestration/organisations/${params.id}/applications/${params.appId}/deployments`;
// return Promise.resolve({
// method: 'get',
// url: `/v4${urlBase}/applications/${params.appId}/deployments`,
// headers: { Accept: 'application/json' },
// queryParams: pickNonNull(params, ['limit', 'offset', 'action']),
// // no body
// });
//
// }
Loading