@@ -42,6 +42,24 @@ class Canvas {
4242 return this ;
4343 }
4444
45+ /**
46+ * Pass complete axios response that is in text type
47+ * @param response : complete axios response that is in text type
48+ * @param keys : all the keys that need to be converted to string
49+ * @returns : complete axios response
50+ */
51+ stringifyValues ( response , keys ) {
52+ const keySet = new Set ( keys ) ;
53+ const data = response . data ;
54+ response . data = JSON . parse ( data , ( key , value ) => {
55+ if ( keySet . has ( key ) ) {
56+ return value . toString ( ) ;
57+ }
58+ return value ;
59+ } ) ;
60+ return response ;
61+ }
62+
4563 /**
4664 * Returns a URL used to initiate the authorization process with Canvas and fetch
4765 * the authorization code
@@ -78,8 +96,8 @@ class Canvas {
7896 } ,
7997 } ) ;
8098
81- this . accessToken = resp . data . access_token ;
82- this . refreshToken = resp . data . refresh_token ;
99+ this . accessToken = resp . data . access_token || this . accessToken ;
100+ this . refreshToken = resp . data . refresh_token || this . refreshToken ;
83101 return { accessToken : this . accessToken , refreshToken : this . refreshToken } ;
84102 } catch ( err ) {
85103 this . handleError ( err , code ) ;
@@ -91,12 +109,19 @@ class Canvas {
91109 const resp = await this . makeRequest ( {
92110 url : '/api/v1/users/self/profile' ,
93111 method : 'GET' ,
112+ responseType : 'text' ,
113+ transformResponse : [ function ( data ) {
114+ // Do not parse the data
115+ return data ;
116+ } ] ,
94117 headers : {
95118 'Content-Type' : 'application/json' ,
96119 } ,
97120 } ) ;
98- this . canvasUserId = resp . data . id ;
99- return resp . data ;
121+ const response = this . stringifyValues ( resp , [ 'id' ] ) ;
122+
123+ this . canvasUserId = response . data . id ;
124+ return response . data ;
100125 } catch ( err ) {
101126 throw new LMSError ( 'Unable to fetch user profile' , 'canvas.USER_PROFILE_ERROR' , {
102127 userId : this . userId ,
@@ -107,9 +132,9 @@ class Canvas {
107132
108133 async getTokensFromUser ( ) {
109134 try {
110- const { accessToken , refreshToken , info } = await this . getUserToken ( this . userId ) ;
111- this . accessToken = accessToken ;
112- this . refreshToken = refreshToken ;
135+ const { access_token , refresh_token , info } = await this . getUserToken ( this . userId ) ;
136+ this . accessToken = access_token ;
137+ this . refreshToken = refresh_token ;
113138 this . canvasUserId = info . id ;
114139 } catch ( err ) {
115140 throw new LMSError ( 'Unable to fetch tokens from user' , 'canvas.TOKEN_FETCH_ERROR' , {
@@ -244,6 +269,18 @@ class Canvas {
244269 const courses = await paginatedCollect ( this , {
245270 url : '/api/v1/courses' ,
246271 method : 'GET' ,
272+ responseType : 'text' ,
273+ transformResponse : [
274+ function ( data ) {
275+ // Do not parse the data
276+ return data ;
277+ } ,
278+ ] ,
279+ } , ( resp ) => {
280+ return this . stringifyValues ( resp , [
281+ 'account_id' ,
282+ 'root_account_id' ,
283+ ] ) ;
247284 } ) ;
248285 return courses ;
249286 }
@@ -266,7 +303,16 @@ class Canvas {
266303 const students = await paginatedCollect ( this , {
267304 url : `/api/v1/courses/${ courseId } /users` ,
268305 method : 'GET' ,
306+ responseType : 'text' ,
307+ transformResponse : [
308+ function ( data ) {
309+ // Do not parse the data
310+ return data ;
311+ } ,
312+ ] ,
269313 data : { 'enrollment_type' : [ 'student' ] } ,
314+ } , ( resp ) => {
315+ return this . stringifyValues ( resp , [ 'id' ] ) ;
270316 } ) ;
271317 return students ;
272318 }
@@ -349,16 +395,34 @@ class Canvas {
349395 }
350396
351397 async getSubmission ( { courseId, assignmentId, studentCanvasId } ) {
352- const { data : submission } = await this . makeRequest ( {
398+ const res = await this . makeRequest ( {
353399 url : `/api/v1/courses/${ courseId } /assignments/${ assignmentId } /submissions/${ studentCanvasId } ` ,
354400 method : 'GET' ,
401+ responseType : 'text' ,
402+ transformResponse : [
403+ function ( data ) {
404+ // Do not parse the data
405+ return data ;
406+ } ,
407+ ] ,
355408 } ) ;
356- return submission ;
409+
410+ const response = this . stringifyValues ( res , [ 'user_id' ] ) ;
411+ return response . data ;
357412 }
358413
359414 async listSubmissions ( { courseId, assignmentId } ) {
360415 const submissions = await paginatedCollect ( this , {
361416 url : `api/v1/courses/${ courseId } /assignments/${ assignmentId } /submissions` ,
417+ responseType : 'text' ,
418+ transformResponse : [
419+ function ( data ) {
420+ // Do not parse the data
421+ return data ;
422+ } ,
423+ ] ,
424+ } , ( resp ) => {
425+ return this . stringifyValues ( resp , [ 'user_id' ] ) ;
362426 } ) ;
363427 return submissions ;
364428 }
@@ -381,6 +445,15 @@ class Canvas {
381445 const accounts = await paginatedCollect ( this , {
382446 url : '/api/v1/manageable_accounts' ,
383447 method : 'GET' ,
448+ responseType : 'text' ,
449+ transformResponse : [
450+ function ( data ) {
451+ // Do not parse the data
452+ return data ;
453+ } ,
454+ ] ,
455+ } , ( resp ) => {
456+ return this . stringifyValues ( resp , [ 'id' ] ) ;
384457 } ) ;
385458 return accounts ;
386459 }
@@ -392,7 +465,16 @@ class Canvas {
392465 const users = await paginatedCollect ( this , {
393466 url : `/api/v1/accounts/${ id } /users` ,
394467 method : 'GET' ,
468+ responseType : 'text' ,
469+ transformResponse : [
470+ function ( d ) {
471+ // Do not parse the data
472+ return d ;
473+ } ,
474+ ] ,
395475 data,
476+ } , ( resp ) => {
477+ return this . stringifyValues ( resp , [ 'id' ] ) ;
396478 } ) ;
397479 return users ;
398480 }
@@ -402,12 +484,20 @@ class Canvas {
402484 const resp = await this . makeRequest ( {
403485 url : `/api/v1/users/${ id } /profile` ,
404486 method : 'GET' ,
487+ responseType : 'text' ,
488+ transformResponse : [
489+ function ( data ) {
490+ // Do not parse the data
491+ return data ;
492+ } ,
493+ ] ,
405494 headers : {
406495 'Content-Type' : 'application/json' ,
407496 } ,
408497 } ) ;
409- this . canvasUserId = resp . data . id ;
410- return resp . data ;
498+ const response = this . stringifyValues ( resp , [ 'id' ] ) ;
499+ this . canvasUserId = response . data . id ;
500+ return response . data ;
411501 } catch ( err ) {
412502 throw new LMSError ( 'Unable to fetch user profile' , 'canvas.USER_PROFILE_ERROR' , {
413503 userId : this . userId ,
0 commit comments