@@ -49,12 +49,16 @@ class Item extends Base {
4949 * @param {Response } error.response - error response
5050 * @return {Function } Function that handles response
5151 */
52- errorHandler = ( { response } : { response : Response } ) : void => {
52+ errorHandler = ( error : any ) : void => {
5353 if ( this . isDestroyed ( ) ) {
5454 return ;
5555 }
56+ const { response } = error ;
5657 if ( response ) {
5758 response . json ( ) . then ( this . errorCallback ) ;
59+ } else if ( error instanceof Error ) {
60+ this . errorCallback ( ) ;
61+ throw error ;
5862 }
5963 } ;
6064
@@ -193,7 +197,7 @@ class Item extends Base {
193197 this . errorCallback = errorCallback ;
194198
195199 const url = `${ this . getUrl ( id ) } ${ type === TYPE_FOLDER ? '?recursive=true' : '' } ` ;
196- return this . xhr . delete ( url ) . then ( this . deleteSuccessHandler ) . catch ( this . errorHandler ) ;
200+ return this . xhr . delete ( { url } ) . then ( this . deleteSuccessHandler ) . catch ( this . errorHandler ) ;
197201 }
198202
199203 /**
@@ -211,22 +215,37 @@ class Item extends Base {
211215 /**
212216 * API to rename an Item
213217 *
214- * @param {string } id - item id
218+ * @param {Object } item - item to rename
215219 * @param {string } name - item new name
216220 * @param {Function } successCallback - success callback
217221 * @param {Function } errorCallback - error callback
218222 * @return {void }
219223 */
220- rename ( id : string , name : string , successCallback : Function , errorCallback : Function = noop ) : Promise < void > {
224+ rename ( item : BoxItem , name : string , successCallback : Function , errorCallback : Function = noop ) : Promise < void > {
221225 if ( this . isDestroyed ( ) ) {
222226 return Promise . reject ( ) ;
223227 }
224228
229+ const { id , permissions } : BoxItem = item ;
230+ if ( ! id || ! permissions ) {
231+ errorCallback ( ) ;
232+ return Promise . reject ( ) ;
233+ }
234+
235+ const { can_rename } : BoxItemPermission = permissions ;
236+ if ( ! can_rename ) {
237+ errorCallback ( ) ;
238+ return Promise . reject ( ) ;
239+ }
240+
225241 this . id = id ;
226242 this . successCallback = successCallback ;
227243 this . errorCallback = errorCallback ;
228244
229- return this . xhr . put ( `${ this . getUrl ( id ) } ` , { name } ) . then ( this . renameSuccessHandler ) . catch ( this . errorHandler ) ;
245+ return this . xhr
246+ . put ( { url : `${ this . getUrl ( id ) } ` , data : { name } } )
247+ . then ( this . renameSuccessHandler )
248+ . catch ( this . errorHandler ) ;
230249 }
231250
232251 /**
@@ -242,24 +261,41 @@ class Item extends Base {
242261 /**
243262 * Api to create or remove a shared link
244263 *
245- * @param {string } id - item id
264+ * @param {Object } item - item to share
246265 * @param {string } access - shared access level
247266 * @param {Function } successCallback - success callback
248267 * @param {Function|void } errorCallback - error callback
249268 * @return {void }
250269 */
251- share ( id : string , access : string , successCallback : Function , errorCallback : Function = noop ) : Promise < void > {
270+ share ( item : BoxItem , access : string , successCallback : Function , errorCallback : Function = noop ) : Promise < void > {
252271 if ( this . isDestroyed ( ) ) {
253272 return Promise . reject ( ) ;
254273 }
255274
275+ const { id , permissions } : BoxItem = item ;
276+ if ( ! id || ! permissions ) {
277+ errorCallback ( ) ;
278+ return Promise . reject ( ) ;
279+ }
280+
281+ const { can_share, can_set_share_access } : BoxItemPermission = permissions ;
282+ if ( ! can_share || ! can_set_share_access ) {
283+ errorCallback ( ) ;
284+ return Promise . reject ( ) ;
285+ }
286+
256287 this . id = id ;
257288 this . successCallback = successCallback ;
258289 this . errorCallback = errorCallback ;
259290
291+ // We use the parent folder's auth token since use case involves
292+ // only content explorer or picker which works onf folder tokens
260293 return this . xhr
261- . put ( this . getUrl ( this . id ) , {
262- shared_link : access === ACCESS_NONE ? null : { access }
294+ . put ( {
295+ url : this . getUrl ( this . id ) ,
296+ data : {
297+ shared_link : access === ACCESS_NONE ? null : { access }
298+ }
263299 } )
264300 . then ( this . shareSuccessHandler )
265301 . catch ( this . errorHandler ) ;
0 commit comments