From 2d2beadbba6501150fa7e0d1455eababf9f7d3bc Mon Sep 17 00:00:00 2001 From: myl7 Date: Sat, 23 Jul 2022 13:41:19 +0000 Subject: [PATCH] add sort param for index api --- pages/api/index.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pages/api/index.ts b/pages/api/index.ts index 56fe295..5df1710 100644 --- a/pages/api/index.ts +++ b/pages/api/index.ts @@ -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 - 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: // 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)) + // Validate sort param + if (typeof sort !== 'string') { + res.status(400).json({ error: 'Sort query invalid.' }) + return + } + const accessToken = await getAccessToken() // 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) { const { data: folderData } = await axios.get(`${requestUrl}${isRoot ? '' : ':'}/children`, { headers: { Authorization: `Bearer ${accessToken}` }, - params: next - ? { - select: 'name,size,id,lastModifiedDateTime,folder,file,video,image', - top: siteConfig.maxItems, - $skipToken: next, - } - : { - select: 'name,size,id,lastModifiedDateTime,folder,file,video,image', - top: siteConfig.maxItems, - }, + params: { + ...{ + select: 'name,size,id,lastModifiedDateTime,folder,file,video,image', + top: siteConfig.maxItems, + }, + ...(next ? { $skipToken: next } : {}), + ...(sort ? { $orderby: sort } : {}), + }, }) // Extract next page token from full @odata.nextLink