|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import error from '../../core/contentstackError' |
| 3 | + |
| 4 | +export function Authorization (http, data, params) { |
| 5 | + http.defaults.versioningStrategy = undefined |
| 6 | + this.params = params || {} |
| 7 | + if (data) { |
| 8 | + if (data.organization_uid) { |
| 9 | + this.params = { |
| 10 | + organization_uid: data.organization_uid |
| 11 | + } |
| 12 | + } |
| 13 | + if (data.app_uid) { |
| 14 | + this.urlPath = `/manifests/${data.app_uid}/authorizations` |
| 15 | + /** |
| 16 | + * @description List all user authorizations made to an authorized app under a particular organization |
| 17 | + * @memberof Authorization |
| 18 | + * @func findAll |
| 19 | + * @returns {Promise<Response>} |
| 20 | + * |
| 21 | + * @example |
| 22 | + * import * as contentstack from '@contentstack/management' |
| 23 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 24 | + * |
| 25 | + * client.organization('organization_uid').app('manifest_uid').authorization().findAll() |
| 26 | + * .then((response) => console.log(response)) |
| 27 | + */ |
| 28 | + this.findAll = async (param = {}) => { |
| 29 | + try { |
| 30 | + const headers = { |
| 31 | + headers: { ...cloneDeep(this.params) }, |
| 32 | + params: { |
| 33 | + ...cloneDeep(param) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + const response = await http.get(this.urlPath, headers) |
| 38 | + if (response.data) { |
| 39 | + return response.data |
| 40 | + } else { |
| 41 | + throw error(response) |
| 42 | + } |
| 43 | + } catch (err) { |
| 44 | + throw error(err) |
| 45 | + } |
| 46 | + } |
| 47 | + /** |
| 48 | + * @description Revoke all users tokens issued to an authorized app for the particular organization |
| 49 | + * @memberof Authorization |
| 50 | + * @func revokeAll |
| 51 | + * @returns {Promise<Response>} |
| 52 | + * |
| 53 | + * @example |
| 54 | + * import * as contentstack from '@contentstack/management' |
| 55 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 56 | + * |
| 57 | + * client.organization('organization_uid').app('manifest_uid').authorization().revokeAll() |
| 58 | + * .then((response) => console.log(response)) |
| 59 | + */ |
| 60 | + this.revokeAll = async () => { |
| 61 | + try { |
| 62 | + const headers = { |
| 63 | + headers: { ...cloneDeep(this.params) } |
| 64 | + } |
| 65 | + |
| 66 | + const response = await http.delete(this.urlPath, headers) |
| 67 | + if (response.data) { |
| 68 | + return response.data |
| 69 | + } else { |
| 70 | + throw error(response) |
| 71 | + } |
| 72 | + } catch (err) { |
| 73 | + throw error(err) |
| 74 | + } |
| 75 | + } |
| 76 | + /** |
| 77 | + * @description Revoke user token issued to an authorized app for the particular organization |
| 78 | + * @memberof Authorization |
| 79 | + * @func revoke |
| 80 | + * @returns {Promise<Response>} |
| 81 | + * |
| 82 | + * @example |
| 83 | + * import * as contentstack from '@contentstack/management' |
| 84 | + * const client = contentstack.client({ authtoken: 'TOKEN'}) |
| 85 | + * |
| 86 | + * client.organization('organization_uid').app('manifest_uid').authorization().revoke('authorization_uid') |
| 87 | + * .then((response) => console.log(response)) |
| 88 | + */ |
| 89 | + this.revoke = async (authorizationUid) => { |
| 90 | + try { |
| 91 | + const headers = { |
| 92 | + headers: { ...cloneDeep(this.params) } |
| 93 | + } |
| 94 | + |
| 95 | + const response = await http.delete(`${this.urlPath}/${authorizationUid}`, headers) |
| 96 | + if (response.data) { |
| 97 | + return response.data |
| 98 | + } else { |
| 99 | + throw error(response) |
| 100 | + } |
| 101 | + } catch (err) { |
| 102 | + throw error(err) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments