|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import ContentstackCollection from '../../contentstackCollection' |
| 3 | +import error from '../../core/contentstackError' |
| 4 | + |
| 5 | +export function Deployment (http, data, params) { |
| 6 | + http.defaults.versioningStrategy = undefined |
| 7 | + |
| 8 | + if (data && data.app_uid) { |
| 9 | + this.params = params || {} |
| 10 | + this.urlPath = `/manifests/${data.app_uid}/hosting/deployments` |
| 11 | + if (data.organization_uid) { |
| 12 | + this.params = { |
| 13 | + organization_uid: data.organization_uid |
| 14 | + } |
| 15 | + } |
| 16 | + if (data.data) { |
| 17 | + Object.assign(this, cloneDeep(data.data)) |
| 18 | + if (this.organization_uid) { |
| 19 | + this.params = { |
| 20 | + organization_uid: this.organization_uid |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + if (this.uid) { |
| 25 | + this.urlPath = `/manifests/${data.app_uid}/hosting/deployments/${this.uid}` |
| 26 | + /** |
| 27 | + * @descriptionThe The GET deployment call is used to get all the details of an deployment of an app |
| 28 | + * @memberof Deployment |
| 29 | + * @func fetch |
| 30 | + * @returns {Promise<Deployment>} |
| 31 | + * |
| 32 | + * @example |
| 33 | + * import * as contentstack from '@contentstack/management' |
| 34 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 35 | + * client.organization('organization_uid').app('manifest_uid').hosting().deployment('deployment_uid').fetch() |
| 36 | + * .then((data) => {}) |
| 37 | + */ |
| 38 | + this.fetch = async () => { |
| 39 | + try { |
| 40 | + const headers = { |
| 41 | + headers: { ...cloneDeep(this.params) } |
| 42 | + } |
| 43 | + |
| 44 | + const response = await http.get(`${this.urlPath}`, headers) |
| 45 | + if (response.data) { |
| 46 | + const content = response.data.data |
| 47 | + return new Deployment(http, { data: content, app_uid: data.app_uid }, this.params) |
| 48 | + } else { |
| 49 | + throw error(response) |
| 50 | + } |
| 51 | + } catch (err) { |
| 52 | + throw error(err) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @descriptionThe The list deployment logs call is used to list logs of a deployment. |
| 58 | + * @memberof Deployment |
| 59 | + * @func logs |
| 60 | + * @returns {Promise<Response>} |
| 61 | + * |
| 62 | + * @example |
| 63 | + * import * as contentstack from '@contentstack/management' |
| 64 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 65 | + * client.organization('organization_uid').app('manifest_uid').hosting().deployment('deployment_uid').logs() |
| 66 | + * .then((data) => {}) |
| 67 | + */ |
| 68 | + this.logs = async () => { |
| 69 | + try { |
| 70 | + const headers = { |
| 71 | + headers: { ...cloneDeep(this.params) } |
| 72 | + } |
| 73 | + |
| 74 | + const response = await http.get(`${this.urlPath}/logs`, headers) |
| 75 | + if (response.data) { |
| 76 | + return response.data.data |
| 77 | + } else { |
| 78 | + throw error(response) |
| 79 | + } |
| 80 | + } catch (err) { |
| 81 | + throw error(err) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @descriptionThe The create signed download url call is used to get the download url of the deployment source code. |
| 87 | + * @memberof signedDownloadUrl |
| 88 | + * @func logs |
| 89 | + * @returns {Promise<Response>} |
| 90 | + * |
| 91 | + * @example |
| 92 | + * import * as contentstack from '@contentstack/management' |
| 93 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 94 | + * client.organization('organization_uid').app('manifest_uid').hosting().deployment('deployment_uid').signedDownloadUrl() |
| 95 | + * .then((data) => {}) |
| 96 | + */ |
| 97 | + this.signedDownloadUrl = async () => { |
| 98 | + try { |
| 99 | + const headers = { |
| 100 | + headers: { ...cloneDeep(this.params) } |
| 101 | + } |
| 102 | + |
| 103 | + const response = await http.post(`${this.urlPath}/signedDownloadUrl`, {}, headers) |
| 104 | + if (response.data) { |
| 105 | + return response.data.data |
| 106 | + } else { |
| 107 | + throw error(response) |
| 108 | + } |
| 109 | + } catch (err) { |
| 110 | + throw error(err) |
| 111 | + } |
| 112 | + } |
| 113 | + } else { |
| 114 | + /** |
| 115 | + * @descriptionThe The create hosting deployments call is used to deploy the uploaded file in hosting |
| 116 | + * @memberof Deployment |
| 117 | + * @func create |
| 118 | + * @returns {Promise<Deployment>} |
| 119 | + * |
| 120 | + * @example |
| 121 | + * import * as contentstack from '@contentstack/management' |
| 122 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 123 | + * client.organization('organization_uid').app('manifest_uid').hosting().deployment().create() |
| 124 | + * .then((data) => {}) |
| 125 | + */ |
| 126 | + this.create = async ({ uploadUid, fileType, withAdvancedOptions }) => { |
| 127 | + try { |
| 128 | + const headers = { |
| 129 | + headers: { ...cloneDeep(this.params) } |
| 130 | + } |
| 131 | + if (withAdvancedOptions) { |
| 132 | + headers.params = { |
| 133 | + with_advanced_options: withAdvancedOptions |
| 134 | + } |
| 135 | + } |
| 136 | + const response = await http.post(`${this.urlPath}`, { upload_uid: uploadUid, file_type: fileType }, headers) |
| 137 | + if (response.data) { |
| 138 | + const content = response.data.data |
| 139 | + return new Deployment(http, { data: content, app_uid: data.app_uid }, this.params) |
| 140 | + } else { |
| 141 | + throw error(response) |
| 142 | + } |
| 143 | + } catch (err) { |
| 144 | + throw error(err) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @descriptionThe The list deployments call is used to get all the available deployments made for an app. |
| 150 | + * @memberof Deployment |
| 151 | + * @func findAll |
| 152 | + * @returns {Promise<ContentstackCollection>} |
| 153 | + * |
| 154 | + * @example |
| 155 | + * import * as contentstack from '@contentstack/management' |
| 156 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 157 | + * client.organization('organization_uid').app('manifest_uid').hosting().deployment().create() |
| 158 | + * .then((data) => {}) |
| 159 | + */ |
| 160 | + this.findAll = async (param = {}) => { |
| 161 | + try { |
| 162 | + const headers = { |
| 163 | + headers: { ...cloneDeep(this.params) }, |
| 164 | + params: { ...cloneDeep(param) } |
| 165 | + } |
| 166 | + |
| 167 | + const response = await http.get(`${this.urlPath}`, headers) |
| 168 | + if (response.data) { |
| 169 | + const content = response.data |
| 170 | + const collection = new ContentstackCollection(response, http) |
| 171 | + collection.items = DeploymentCollection(http, content, data.app_uid, this.params) |
| 172 | + return collection |
| 173 | + } else { |
| 174 | + throw error(response) |
| 175 | + } |
| 176 | + } catch (err) { |
| 177 | + throw error(err) |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +export function DeploymentCollection (http, data, appUid, param) { |
| 185 | + const obj = cloneDeep(data.data) || [] |
| 186 | + return obj.map((content) => { |
| 187 | + return new Deployment(http, { data: content, app_uid: appUid }, param) |
| 188 | + }) |
| 189 | +} |
0 commit comments