|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import error from '../../core/contentstackError' |
| 3 | + |
| 4 | +export function Request (http, data, param) { |
| 5 | + http.defaults.versioningStrategy = undefined |
| 6 | + this.urlPath = '/manifests' |
| 7 | + this.params = param || {} |
| 8 | + |
| 9 | + if (data) { |
| 10 | + if (data.organization_uid) { |
| 11 | + this.params = { |
| 12 | + organization_uid: data.organization_uid |
| 13 | + } |
| 14 | + } |
| 15 | + /** |
| 16 | + * @description The Delete app request call is used to delete an app request of an app in target_uid. |
| 17 | + * @param {string} requestUID The ID of the request to be deleted |
| 18 | + * @returns Promise<Response> |
| 19 | + * @memberof Request |
| 20 | + * @func delete |
| 21 | + * |
| 22 | + * @example |
| 23 | + * import * as contentstack from '@contentstack/management' |
| 24 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 25 | + * |
| 26 | + * client.organization('organization_uid').app().request().delete('request_uid`) |
| 27 | + * .then((response) => console.log(response)) |
| 28 | + * |
| 29 | + */ |
| 30 | + this.delete = async (requestUid) => { |
| 31 | + try { |
| 32 | + const headers = { |
| 33 | + headers: { ...cloneDeep(this.params) } |
| 34 | + } |
| 35 | + |
| 36 | + const response = await http.delete(`/requests/${requestUid}`, headers) |
| 37 | + if (response.data) { |
| 38 | + return response.data |
| 39 | + } else { |
| 40 | + throw error(response) |
| 41 | + } |
| 42 | + } catch (err) { |
| 43 | + throw error(err) |
| 44 | + } |
| 45 | + } |
| 46 | + if (data.app_uid) { |
| 47 | + /** |
| 48 | + * @description The Create installation call is used to create a app request for an app. |
| 49 | + * @param {string} targetUid The uid of the target, on which the app will be installed |
| 50 | + * @returns Promise<Response> |
| 51 | + * @memberof Request |
| 52 | + * @func create |
| 53 | + * |
| 54 | + * @example |
| 55 | + * import * as contentstack from '@contentstack/management' |
| 56 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 57 | + * |
| 58 | + * client.organization('organization_uid').app('app_uid').request().create('target_uid') |
| 59 | + * .then((response) => console.log(response)) |
| 60 | + * |
| 61 | + */ |
| 62 | + this.create = async (targetUid) => { |
| 63 | + try { |
| 64 | + const headers = { |
| 65 | + headers: { ...cloneDeep(this.params) } |
| 66 | + } |
| 67 | + |
| 68 | + const response = await http.post(`/requests`, { app_uid: data.app_uid, target_uid: targetUid }, headers) |
| 69 | + if (response.data) { |
| 70 | + return response.data |
| 71 | + } else { |
| 72 | + throw error(response) |
| 73 | + } |
| 74 | + } catch (err) { |
| 75 | + throw error(err) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @description The GET app requests of an app call is used to retrieve all requests of an app. |
| 81 | + * @returns Promise<Response> |
| 82 | + * @memberof Request |
| 83 | + * @func fetch |
| 84 | + * |
| 85 | + * @example |
| 86 | + * import * as contentstack from '@contentstack/management' |
| 87 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 88 | + * |
| 89 | + * client.organization('organization_uid').app('app_uid').request().fetch() |
| 90 | + * .then((response) => console.log(response)) |
| 91 | + * |
| 92 | + */ |
| 93 | + this.fetch = async () => { |
| 94 | + try { |
| 95 | + const headers = { |
| 96 | + headers: { ...cloneDeep(this.params) } |
| 97 | + } |
| 98 | + |
| 99 | + const response = await http.get(`/manifests/${data.app_uid}/requests`, headers) |
| 100 | + if (response.data) { |
| 101 | + return response.data |
| 102 | + } else { |
| 103 | + throw error(response) |
| 104 | + } |
| 105 | + } catch (err) { |
| 106 | + throw error(err) |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + /** |
| 111 | + * @description The GET all app requests call is used to retrieve all requests of all apps in an organization. |
| 112 | + * @param {object} param object for query params |
| 113 | + * @returns Promise<Response> |
| 114 | + * @memberof Request |
| 115 | + * @func findAll |
| 116 | + * |
| 117 | + * @example |
| 118 | + * import * as contentstack from '@contentstack/management' |
| 119 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 120 | + * |
| 121 | + * client.organization('organization_uid').app('app_uid').request().findAll() |
| 122 | + * .then((response) => console.log(response)) |
| 123 | + * |
| 124 | + */ |
| 125 | + this.findAll = async (param = {}) => { |
| 126 | + try { |
| 127 | + const headers = { |
| 128 | + headers: { ...cloneDeep(this.params) }, |
| 129 | + params: { ...param } |
| 130 | + } |
| 131 | + |
| 132 | + const response = await http.get(`/requests`, headers) |
| 133 | + if (response.data) { |
| 134 | + return response.data |
| 135 | + } else { |
| 136 | + throw error(response) |
| 137 | + } |
| 138 | + } catch (err) { |
| 139 | + throw error(err) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments