extract cors middleware outside function scope
This commit is contained in:
parent
39022be478
commit
46844c30ed
File diff suppressed because it is too large
Load Diff
|
|
@ -13,6 +13,20 @@ import { getOdAuthTokens, storeOdAuthTokens } from '../../utils/odAuthTokenStore
|
|||
const basePath = pathPosix.resolve('/', siteConfig.baseDirectory)
|
||||
const clientSecret = revealObfuscatedToken(apiConfig.obfuscatedClientSecret)
|
||||
|
||||
// CORS middleware for raw links: https://nextjs.org/docs/api-routes/api-middlewares
|
||||
function runCorsMiddleware(req: NextApiRequest, res: NextApiResponse) {
|
||||
const cors = Cors({ methods: ['GET', 'HEAD'] })
|
||||
return new Promise((resolve, reject) => {
|
||||
cors(req, res, result => {
|
||||
if (result instanceof Error) {
|
||||
return reject(result)
|
||||
}
|
||||
|
||||
return resolve(result)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the path of the file relative to the base directory
|
||||
*
|
||||
|
|
@ -169,17 +183,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
// Whether path is root, which requires some special treatment
|
||||
const isRoot = requestPath === ''
|
||||
|
||||
// Go for file raw download link and query with only temporary link parameter
|
||||
// Go for file raw download link, add CORS headers, and redirect to @microsoft.graph.downloadUrl
|
||||
if (raw) {
|
||||
const cors = Cors({ methods: ['GET', 'HEAD'] })
|
||||
await new Promise((resolve, reject) => {
|
||||
cors(req, res, result => {
|
||||
if (result instanceof Error) {
|
||||
return reject(result)
|
||||
}
|
||||
return resolve(result)
|
||||
})
|
||||
})
|
||||
await runCorsMiddleware(req, res)
|
||||
|
||||
const { data } = await axios.get(requestUrl, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
|
|
@ -199,8 +205,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
}
|
||||
|
||||
// Querying current path identity (file or folder) and follow up query childrens in folder
|
||||
// console.log(accessToken)
|
||||
|
||||
try {
|
||||
const { data: identityData } = await axios.get(requestUrl, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
|
|
|
|||
Loading…
Reference in New Issue