Skip to content

Commit

Permalink
@uppy/companion: fix onedrive pagination (#4686)
Browse files Browse the repository at this point in the history
fix onedrive pagination
  • Loading branch information
mifi authored Sep 19, 2023
1 parent 6479159 commit 1870c6b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
17 changes: 9 additions & 8 deletions packages/@uppy/companion/src/server/provider/onedrive/adapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const querystring = require('node:querystring')

const isFolder = (item) => {
if (item.remoteItem) {
return !!item.remoteItem.folder
Expand Down Expand Up @@ -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) => {
Expand All @@ -77,7 +78,7 @@ module.exports = (res, username) => {
})
})

data.nextPagePath = getNextPagePath(res)
data.nextPagePath = getNextPagePath({ res, query, directory })

return data
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
})
}

Expand Down

0 comments on commit 1870c6b

Please sign in to comment.