|
| 1 | +import { apiCall } from "../core"; |
| 2 | +import { HttpMethod } from "../core/types"; |
| 3 | +import type { |
| 4 | + ActivateAddonAccountLevelResponse, |
| 5 | + DeactivateAnAddonResponse, |
| 6 | + AddonsListResponse, |
| 7 | + UpgradeAddonResponse, |
| 8 | + ElasticEmailDomainsResponse, |
| 9 | + verifyEmailDomainResponse, |
| 10 | +} from "./types"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Activates an add-on on a specified server, especially applicable for SMTP add-ons. |
| 14 | + * @param {number} serverId - The numeric ID of the server where the add-on will be activated. |
| 15 | + * @param {number} addonId - The numeric ID of the add-on to be activated. |
| 16 | + * @param {string} username - The username required for SMTP add-ons. |
| 17 | + * @param {string} password - The password required for SMTP add-ons. |
| 18 | + * @param {string} mode - The mode required for SMTP add-ons. Allowed values are 'enable' or 'update'. |
| 19 | + * @param {string} provider - The SMTP provider to be selected. |
| 20 | + * @param {string} host - The host required for SMTP add-ons. |
| 21 | + * @param {string} port - The port required for SMTP add-ons. |
| 22 | + * @returns {Promise<void>} A promise that resolves when the add-on is successfully activated. |
| 23 | + * @example |
| 24 | + * ``` |
| 25 | + * [] |
| 26 | + * |
| 27 | + * ``` |
| 28 | + */ |
| 29 | +export function activateAddOnServer( |
| 30 | + serverId: number, |
| 31 | + addonId: number, |
| 32 | + username: string, |
| 33 | + password: string, |
| 34 | + mode: string, |
| 35 | + provider: string, |
| 36 | + host: string, |
| 37 | + port: string |
| 38 | +): Promise<void> { |
| 39 | + const data = { |
| 40 | + server_id: serverId, |
| 41 | + addon_id: addonId, |
| 42 | + username: username, |
| 43 | + password: password, |
| 44 | + mode: mode, |
| 45 | + provider: provider, |
| 46 | + host: host, |
| 47 | + port: port, |
| 48 | + }; |
| 49 | + return apiCall("/addon/activateOnServer", HttpMethod.POST, data); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Activates an addon on the account level. |
| 54 | + * @param {number} addonId - The numeric ID of the addon to be activated. |
| 55 | + * @param {number} packageId - The package ID. Not necessary in case of SMTP addon. |
| 56 | + * @returns {Promise<ActivateAddonAccountLevelResponse>} A promise that resolves to the response containing information about the activated addon. |
| 57 | + * @example |
| 58 | + * { |
| 59 | + "message" : "Your Own SMTP has been successfully enabled.", |
| 60 | + "sub" : { |
| 61 | + "id_user_addons" : "1234", |
| 62 | + "status" : "1", |
| 63 | + "id_package" : null |
| 64 | + } |
| 65 | +} |
| 66 | + */ |
| 67 | +export function activateAddonOnAccountLevel( |
| 68 | + addonId: number, |
| 69 | + packageId: number |
| 70 | +): Promise<ActivateAddonAccountLevelResponse> { |
| 71 | + const data = { |
| 72 | + addon_id: addonId, |
| 73 | + package_id: packageId, |
| 74 | + }; |
| 75 | + |
| 76 | + return apiCall("/addon/activate", HttpMethod.POST, data).then((response) => { |
| 77 | + return { |
| 78 | + message: response.message, |
| 79 | + sub: { |
| 80 | + id_user_addons: response.sub.id_user_addons, |
| 81 | + status: response.sub.status, |
| 82 | + id_package: response.sub.id_package, |
| 83 | + }, |
| 84 | + }; |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Requests an addon for a specific application. |
| 90 | + * @param {number} addonId - The numeric ID of the addon to be requested. |
| 91 | + * @param {number} serverId - The numeric ID of the server where the application resides. |
| 92 | + * @param {number} appId - The numeric ID of the application to which the addon will be added. |
| 93 | + * @param {number} version - The desired version of the application. Required only for Application Upgradation Add-on. |
| 94 | + * @returns {Promise<void>} A promise that resolves when the addon request is successful. |
| 95 | + * @example |
| 96 | + * ``` |
| 97 | + * [] |
| 98 | + * |
| 99 | + * ``` |
| 100 | + */ |
| 101 | +export function addonRequestForApplication( |
| 102 | + addonId: number, |
| 103 | + serverId: number, |
| 104 | + appId: number, |
| 105 | + version: number |
| 106 | +): Promise<void> { |
| 107 | + const data = { |
| 108 | + addon_id: addonId, |
| 109 | + server_id: serverId, |
| 110 | + app_id: appId, |
| 111 | + version: version, |
| 112 | + }; |
| 113 | + return apiCall("/addon/request", HttpMethod.POST, data); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Deactivates an addon on a specified server. |
| 118 | + * @param {number} serverId - The numeric ID of the server where the addon will be deactivated. |
| 119 | + * @param {number} addonId - The numeric ID of the addon to be deactivated. |
| 120 | + * @returns {Promise<void>} A promise that resolves when the addon deactivation is successful. |
| 121 | + * @example |
| 122 | + * ``` |
| 123 | + * [] |
| 124 | + * |
| 125 | + * ``` |
| 126 | + */ |
| 127 | +export function deactivateAddOnYourServer( |
| 128 | + serverId: number, |
| 129 | + addonId: number |
| 130 | +): Promise<void> { |
| 131 | + const data = { |
| 132 | + server_id: serverId, |
| 133 | + addon_id: addonId, |
| 134 | + }; |
| 135 | + return apiCall("/addon/deactivateOnServer", HttpMethod.POST, data); |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Deactivates an addon on the specified server. |
| 140 | + * @param {number} serverId - The ID of the server where the addon will be deactivated. |
| 141 | + * @param {number} addonId - The ID of the addon to deactivate. |
| 142 | + * @returns {Promise<DeactivateAnAddonResponse>} A promise that resolves to the response containing information about the deactivated addon. |
| 143 | + * @example |
| 144 | + * ``` |
| 145 | + * { |
| 146 | + "message" : "Your Own SMTP has been successfully deactivated.", |
| 147 | + "sub" : { |
| 148 | + "id_user_addons" : "1234", |
| 149 | + "status" : 0 |
| 150 | + } |
| 151 | +} |
| 152 | + * ``` |
| 153 | + */ |
| 154 | +export function deactivateAnAddon( |
| 155 | + addonId: number |
| 156 | +): Promise<DeactivateAnAddonResponse> { |
| 157 | + const data = { |
| 158 | + addon_id: addonId, |
| 159 | + }; |
| 160 | + return apiCall("/addon/deactivate", HttpMethod.POST, data).then( |
| 161 | + (response) => { |
| 162 | + return { |
| 163 | + message: response.message, |
| 164 | + sub: { |
| 165 | + id_user_addons: response.sub.id_user_addons, |
| 166 | + status: response.sub.status, |
| 167 | + }, |
| 168 | + }; |
| 169 | + } |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * Retrieve the list of addons from the Cloudways API. |
| 175 | + * @returns {Promise<AddonsListResponse>} - Promise resolving to the list of addons. |
| 176 | + */ |
| 177 | +export function getAddonsList(): Promise<AddonsListResponse> { |
| 178 | + return apiCall("/addon", HttpMethod.GET).then( |
| 179 | + (response: any) => response as AddonsListResponse |
| 180 | + ); |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Upgrade an addon package. |
| 185 | + * @param addonId - The numeric id of the addon. |
| 186 | + * @param packageId - The ID of the package to subscribe to. |
| 187 | + * @returns {Promise<UpgradeAddonResponse>} - Promise resolving to the response of upgrading an addon package. |
| 188 | + * @example |
| 189 | + * { |
| 190 | + "message" : "DNS Made Easy request has been successfully received. Our 24/7 support will contact you in a while with further details.", |
| 191 | + "sub" : { |
| 192 | + "id_user_addons" : "1234", |
| 193 | + "status" : "2", |
| 194 | + "id_package" : "7" |
| 195 | + } |
| 196 | +} |
| 197 | + * |
| 198 | + */ |
| 199 | +export function upgradeAddonPackage( |
| 200 | + addonId: number, |
| 201 | + packageId: number |
| 202 | +): Promise<UpgradeAddonResponse> { |
| 203 | + const data = { |
| 204 | + addon_id: addonId, |
| 205 | + package_id: packageId, |
| 206 | + }; |
| 207 | + |
| 208 | + return apiCall("/addon/upgrade", HttpMethod.POST, data).then((response) => { |
| 209 | + return { |
| 210 | + message: response.message, |
| 211 | + sub: { |
| 212 | + id_user_addons: response.sub.id_user_addons, |
| 213 | + status: response.sub.status, |
| 214 | + id_package: response.sub.id_package, |
| 215 | + }, |
| 216 | + }; |
| 217 | + }); |
| 218 | +} |
| 219 | + |
| 220 | +/** |
| 221 | + * Get the list of Elastic Email domains. |
| 222 | + * @returns {Promise<ElasticEmailDomainsResponse>} - Promise resolving to the response containing domain details. |
| 223 | + * @example |
| 224 | + * { |
| 225 | + * "status": true, |
| 226 | + * "data": { |
| 227 | + * "domains": [ |
| 228 | + * { |
| 229 | + * "domain": "indoorplants.pk", |
| 230 | + * "spf": false, |
| 231 | + * "mx": true, |
| 232 | + * "dkim": false, |
| 233 | + * "dmarc": false, |
| 234 | + * "tracking": false |
| 235 | + * } |
| 236 | + * ] |
| 237 | + * } |
| 238 | + * } |
| 239 | + */ |
| 240 | +export function getElasticEmailDomains(): Promise<ElasticEmailDomainsResponse> { |
| 241 | + return apiCall("/addon/elastic/domains", HttpMethod.GET).then((response) => { |
| 242 | + return { |
| 243 | + status: response.status, |
| 244 | + data: { |
| 245 | + domains: [ |
| 246 | + { |
| 247 | + domain: response.data.domain, |
| 248 | + spf: response.data.spf, |
| 249 | + mx: response.data.mx, |
| 250 | + dkim: response.data.dkim, |
| 251 | + dmarc: response.data.dmarc, |
| 252 | + tracking: response.data.tracking, |
| 253 | + }, |
| 254 | + ], |
| 255 | + }, |
| 256 | + }; |
| 257 | + }); |
| 258 | +} |
| 259 | + |
| 260 | +/** |
| 261 | + * Verify an Elastic Email domain. |
| 262 | + * @param {string} domain - The domain to verify. |
| 263 | + * @returns {Promise<verifyEmailDomainResponse>} - Promise resolving to the response of verifying the domain. |
| 264 | + * @example |
| 265 | + * { |
| 266 | + "status": true, |
| 267 | + "data": { |
| 268 | + "domain": "indoorplants.pk", |
| 269 | + "spf": false, |
| 270 | + "mx": true, |
| 271 | + "dkim": false, |
| 272 | + "dmarc": false, |
| 273 | + "tracking": false |
| 274 | + } |
| 275 | +} |
| 276 | + */ |
| 277 | +export function verifyElasticEmailDomain( |
| 278 | + domain: string |
| 279 | +): Promise<verifyEmailDomainResponse> { |
| 280 | + const data = { |
| 281 | + domain: domain, |
| 282 | + }; |
| 283 | + return apiCall("/addon/elastic/verify_domain", HttpMethod.POST, data).then( |
| 284 | + (response) => { |
| 285 | + return { |
| 286 | + status: response.status, |
| 287 | + data: { |
| 288 | + domain: response.data.domain, |
| 289 | + spf: response.data.spf, |
| 290 | + mx: response.data.mx, |
| 291 | + dkim: response.data.dkim, |
| 292 | + dmarc: response.data.dmarc, |
| 293 | + tracking: response.data.tracking, |
| 294 | + }, |
| 295 | + }; |
| 296 | + } |
| 297 | + ); |
| 298 | +} |
| 299 | + |
| 300 | + |
| 301 | +/** |
| 302 | + * Deletes an Elastic Email domain. |
| 303 | + * @param {string} domain - The domain to delete. |
| 304 | + * @returns {Promise<{status: boolean, message: string}>} - A promise that resolves to an object containing the status and message of the deletion. |
| 305 | + * @example |
| 306 | + * { |
| 307 | + "status": true, |
| 308 | + "message": "Domain successfully deleted" |
| 309 | +} |
| 310 | + */ |
| 311 | +export function deleteElasticEmailDomain(domain: string): Promise<{status: boolean, message : string}> { |
| 312 | + const data = { |
| 313 | + domain: domain, |
| 314 | + }; |
| 315 | + return apiCall("/addon/elastic/domain", HttpMethod.DELETE, data).then( |
| 316 | + (response) => ({ |
| 317 | + status: response.status, |
| 318 | + message: response.message, |
| 319 | + }) |
| 320 | + ); |
| 321 | +} |
0 commit comments