Kaynağa Gözat

@uppy/companion: fix onedrive pagination (#4686)

fix onedrive pagination
Mikael Finstad 1 yıl önce
ebeveyn
işleme
1870c6b765

+ 9 - 8
packages/@uppy/companion/src/server/provider/onedrive/adapter.js

@@ -1,5 +1,3 @@
-const querystring = require('node:querystring')
-
 const isFolder = (item) => {
   if (item.remoteItem) {
     return !!item.remoteItem.folder
@@ -51,16 +49,19 @@ const getItemModifiedDate = (item) => {
   return item.lastModifiedDateTime
 }
 
-const getNextPagePath = (data) => {
-  if (!data['@odata.nextLink']) {
+const getNextPagePath = ({ res, query: currentQuery, directory }) => {
+  const nextLink = res['@odata.nextLink']
+  if (!nextLink) {
     return null
   }
 
-  const query = { cursor: querystring.parse(data['@odata.nextLink']).$skiptoken }
-  return `?${querystring.stringify(query)}`
+  const skipToken = new URL(nextLink).searchParams.get('$skiptoken')
+
+  const query = { ...currentQuery, cursor: skipToken }
+  return `${directory}?${new URLSearchParams(query).toString()}`
 }
 
-module.exports = (res, username) => {
+module.exports = (res, username, query, directory) => {
   const data = { username, items: [] }
   const items = getItemSubList(res)
   items.forEach((item) => {
@@ -77,7 +78,7 @@ module.exports = (res, username) => {
     })
   })
 
-  data.nextPagePath = getNextPagePath(res)
+  data.nextPagePath = getNextPagePath({ res, query, directory })
 
   return data
 }

+ 4 - 2
packages/@uppy/companion/src/server/provider/onedrive/index.js

@@ -45,7 +45,9 @@ class OneDrive extends Provider {
     return this.#withErrorHandling('provider.onedrive.list.error', async () => {
       const path = directory ? `items/${directory}` : 'root'
       // https://learn.microsoft.com/en-us/graph/query-parameters?tabs=http#top-parameter
-      const qs = { $expand: 'thumbnails', $top: 999 }
+      const pageSize = 999
+      // const pageSize = 20 // to test pagination easily
+      const qs = { $expand: 'thumbnails', $top: pageSize }
       if (query.cursor) {
         qs.$skiptoken = query.cursor
       }
@@ -57,7 +59,7 @@ class OneDrive extends Provider {
         client.get(`${getRootPath(query)}/${path}/children`, { searchParams: qs, responseType: 'json' }).json(),
       ])
 
-      return adaptData(list, mail || userPrincipalName)
+      return adaptData(list, mail || userPrincipalName, query, directory)
     })
   }