add sort param for index api

This commit is contained in:
myl7
2022-07-23 13:41:19 +00:00
parent 9e887b2b38
commit 2d2beadbba
+15 -11
View File
@@ -169,7 +169,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
} }
// If method is GET, then the API is a normal request to the OneDrive API for files or folders // If method is GET, then the API is a normal request to the OneDrive API for files or folders
const { path = '/', raw = false, next = '' } = req.query const { path = '/', raw = false, next = '', sort = '' } = req.query
// Set edge function caching for faster load times, check docs: // Set edge function caching for faster load times, check docs:
// https://vercel.com/docs/concepts/functions/edge-caching // https://vercel.com/docs/concepts/functions/edge-caching
@@ -187,6 +187,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
} }
const cleanPath = pathPosix.resolve('/', pathPosix.normalize(path)) const cleanPath = pathPosix.resolve('/', pathPosix.normalize(path))
// Validate sort param
if (typeof sort !== 'string') {
res.status(400).json({ error: 'Sort query invalid.' })
return
}
const accessToken = await getAccessToken() const accessToken = await getAccessToken()
// Return error 403 if access_token is empty // Return error 403 if access_token is empty
@@ -248,16 +254,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if ('folder' in identityData) { if ('folder' in identityData) {
const { data: folderData } = await axios.get(`${requestUrl}${isRoot ? '' : ':'}/children`, { const { data: folderData } = await axios.get(`${requestUrl}${isRoot ? '' : ':'}/children`, {
headers: { Authorization: `Bearer ${accessToken}` }, headers: { Authorization: `Bearer ${accessToken}` },
params: next params: {
? { ...{
select: 'name,size,id,lastModifiedDateTime,folder,file,video,image', select: 'name,size,id,lastModifiedDateTime,folder,file,video,image',
top: siteConfig.maxItems, top: siteConfig.maxItems,
$skipToken: next, },
} ...(next ? { $skipToken: next } : {}),
: { ...(sort ? { $orderby: sort } : {}),
select: 'name,size,id,lastModifiedDateTime,folder,file,video,image', },
top: siteConfig.maxItems,
},
}) })
// Extract next page token from full @odata.nextLink // Extract next page token from full @odata.nextLink