|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import error from '../core/contentstackError' |
| 3 | +import { create, deleteEntity, fetch, fetchAll, update } from '../entity' |
| 4 | +export function App (http, data) { |
| 5 | + http.defaults.versioningStrategy = undefined |
| 6 | + this.urlPath = '/apps' |
| 7 | + this.params = {} |
| 8 | + if (data) { |
| 9 | + if (data.organization_uid) { |
| 10 | + this.params = { |
| 11 | + organization_uid: data.organization_uid |
| 12 | + } |
| 13 | + } |
| 14 | + if (data.data) { |
| 15 | + Object.assign(this, cloneDeep(data.data)) |
| 16 | + if (this.organization_uid) { |
| 17 | + this.params = { |
| 18 | + organization_uid: this.organization_uid |
| 19 | + } |
| 20 | + } |
| 21 | + this.urlPath = `/apps/${this.uid}` |
| 22 | + |
| 23 | + /** |
| 24 | + * @description The update an app call is used to update the app details such as name, description, icon, and so on. |
| 25 | + * @memberof App |
| 26 | + * @func update |
| 27 | + * @returns {Promise<App>} |
| 28 | + * |
| 29 | + * @example |
| 30 | + * import * as contentstack from '@contentstack/management' |
| 31 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 32 | + * const app = { |
| 33 | + * name: 'APP_NAME', |
| 34 | + * description: 'APP_DESCRIPTION', |
| 35 | + * target_type: 'stack'/'organization', |
| 36 | + * webhook: // optional |
| 37 | + * { |
| 38 | + * target_url: 'TARGET_URL', |
| 39 | + * channel: 'CHANNEL' |
| 40 | + * }, |
| 41 | + * oauth: // optional |
| 42 | + * { |
| 43 | + * redirect_uri: 'REDIRECT_URI', |
| 44 | + * enabled: true, |
| 45 | + * } |
| 46 | + * } |
| 47 | + * |
| 48 | + * client.organization('organization_uid').app('app_uid').update({app}) |
| 49 | + * .then((app) => console.log(app)) |
| 50 | + * |
| 51 | + */ |
| 52 | + this.update = update(http, undefined, this.params) |
| 53 | + |
| 54 | + /** |
| 55 | + * @description The get app details call is used to fetch details of a particular app with its ID. |
| 56 | + * @memberof App |
| 57 | + * @func fetch |
| 58 | + * @returns {Promise<App>} |
| 59 | + * |
| 60 | + * @example |
| 61 | + * import * as contentstack from '@contentstack/management' |
| 62 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 63 | + * |
| 64 | + * client.organization('organization_uid').app('app_uid').fetch() |
| 65 | + * .then((app) => console.log(app)) |
| 66 | + * |
| 67 | + */ |
| 68 | + this.fetch = fetch(http, 'data', this.params) |
| 69 | + |
| 70 | + /** |
| 71 | + * @description The delete an app call is used to delete the app. |
| 72 | + * @memberof App |
| 73 | + * @func delete |
| 74 | + * @returns {Promise<App>} |
| 75 | + * |
| 76 | + * @example |
| 77 | + * import * as contentstack from '@contentstack/management' |
| 78 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 79 | + * |
| 80 | + * client.organization('organization_uid').app('app_uid').delete() |
| 81 | + * .then((app) => console.log(app)) |
| 82 | + */ |
| 83 | + this.delete = deleteEntity(http, false, this.params) |
| 84 | + |
| 85 | + /** |
| 86 | + * @description The get oauth call is used to fetch the OAuth details of the app. |
| 87 | + * @memberof App |
| 88 | + * @func fetchOAuth |
| 89 | + * @returns {Promise<AppOAuth>} |
| 90 | + * |
| 91 | + * @example |
| 92 | + * import * as contentstack from '@contentstack/management' |
| 93 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 94 | + * |
| 95 | + * client.organization('organization_uid').app('app_uid').fetchOAuth() |
| 96 | + * .then((oAuthConfig) => console.log(oAuthConfig)) |
| 97 | + */ |
| 98 | + this.fetchOAuth = async (param = {}) => { |
| 99 | + try { |
| 100 | + const headers = { |
| 101 | + headers: { ...cloneDeep(this.params) }, |
| 102 | + params: { |
| 103 | + ...cloneDeep(param) |
| 104 | + } |
| 105 | + } || {} |
| 106 | + |
| 107 | + const response = await http.get(`${this.urlPath}/oauth`, headers) |
| 108 | + if (response.data) { |
| 109 | + return response.data.data || {} |
| 110 | + } else { |
| 111 | + throw error(response) |
| 112 | + } |
| 113 | + } catch (err) { |
| 114 | + throw error(err) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @description The change oauth details call is used to update the OAuth details, (redirect url and permission scope) of an app. |
| 120 | + * @memberof App |
| 121 | + * @func updateOAuth |
| 122 | + * @returns {Promise<AppOAuth>} |
| 123 | + * |
| 124 | + * @example |
| 125 | + * import * as contentstack from '@contentstack/management' |
| 126 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 127 | + * const config = { |
| 128 | + * redirect_uri: 'REDIRECT_URI', |
| 129 | + * app_token_config: { |
| 130 | + * enabled: true, |
| 131 | + * scopes: ['scope1', 'scope2'] |
| 132 | + * }, |
| 133 | + * user_token_config: { |
| 134 | + * enabled: true, |
| 135 | + * scopes: ['scope1', 'scope2'] |
| 136 | + * } |
| 137 | + * } |
| 138 | + * client.organization('organization_uid').app('app_uid').updateOAuth({ config }) |
| 139 | + * .then((oAuthConfig) => console.log(oAuthConfig)) |
| 140 | + */ |
| 141 | + this.updateOAuth = async ({ config, param = {} }) => { |
| 142 | + try { |
| 143 | + const headers = { |
| 144 | + headers: { ...cloneDeep(this.params) }, |
| 145 | + params: { |
| 146 | + ...cloneDeep(param) |
| 147 | + } |
| 148 | + } || {} |
| 149 | + |
| 150 | + const response = await http.put(`${this.urlPath}/oauth`, config, headers) |
| 151 | + if (response.data) { |
| 152 | + return response.data.data || {} |
| 153 | + } else { |
| 154 | + throw error(response) |
| 155 | + } |
| 156 | + } catch (err) { |
| 157 | + throw error(err) |
| 158 | + } |
| 159 | + } |
| 160 | + } else { |
| 161 | + /** |
| 162 | + * @description The create an app call is used for creating a new app in your Contentstack organization. |
| 163 | + * @memberof App |
| 164 | + * @func create |
| 165 | + * @returns {Promise<App>} |
| 166 | + * |
| 167 | + * @example |
| 168 | + * import * as contentstack from '@contentstack/management' |
| 169 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 170 | + * const app = { |
| 171 | + * name: 'APP_NAME', |
| 172 | + * description: 'APP_DESCRIPTION', |
| 173 | + * target_type: 'stack'/'organization', |
| 174 | + * webhook: // optional |
| 175 | + * { |
| 176 | + * target_url: 'TARGET_URL', |
| 177 | + * channel: 'CHANNEL' |
| 178 | + * }, |
| 179 | + * oauth: // optional |
| 180 | + * { |
| 181 | + * redirect_uri: 'REDIRECT_URI', |
| 182 | + * enabled: true, |
| 183 | + * } |
| 184 | + * } |
| 185 | + * |
| 186 | + * client.organization('organization_uid').app().create(app) |
| 187 | + * .then((app) => console.log(app)) |
| 188 | + * |
| 189 | + */ |
| 190 | + this.create = create({ http, params: this.params }) |
| 191 | + |
| 192 | + /** |
| 193 | + * @description The create an app call is used for creating a new app in your Contentstack organization. |
| 194 | + * @memberof App |
| 195 | + * @func create |
| 196 | + * @returns {Promise<App>} |
| 197 | + * |
| 198 | + * @example |
| 199 | + * import * as contentstack from '@contentstack/management' |
| 200 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 201 | + * |
| 202 | + * client.organization('organization_uid').app().fetchAll() |
| 203 | + * .then((app) => console.log(app)) |
| 204 | + * |
| 205 | + */ |
| 206 | + this.findAll = fetchAll(http, AppCollection, this.params) |
| 207 | + } |
| 208 | + } |
| 209 | + return this |
| 210 | +} |
| 211 | + |
| 212 | +export function AppCollection (http, data) { |
| 213 | + const obj = cloneDeep(data.data) || [] |
| 214 | + return obj.map((appData) => { |
| 215 | + return new App(http, { data: appData, stackHeaders: data.stackHeaders }) |
| 216 | + }) |
| 217 | +} |
0 commit comments