//native
const graph = "https://graph.microsoft.com/v1.0"
function driveUrl(drive_id: string | undefined) {
return drive_id != null && drive_id !== ""
? `${graph}/drives/${encodeURIComponent(drive_id)}`
: `${graph}/me/drive`
}
// An item is addressed by its id, or by a path from the drive root starting with "/"
function ref(item: string | undefined) {
if (item == null || item === "" || item === "/") return "root"
if (!item.startsWith("/")) return `items/${encodeURIComponent(item)}`
const path = item
.replace(/\/+$/, "")
.split("/")
.map(encodeURIComponent)
.join("/")
return `root:${path}:`
}
/**
* List Files in Folder
* List the files and folders directly inside a folder (the root when `folder` is empty) in your OneDrive, or in another drive when `drive_id` is set. `folder` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). `filter` and `order_by` take OData (e.g. `name desc`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Files.Read (covered by the default Files.ReadWrite); a drive that is not your own needs Files.Read.All.
*/
export async function main(
auth: RT.MsOnedrive,
folder: string | undefined,
order_by: string | undefined,
filter: string | undefined,
select: string | undefined,
top: number = 100,
next_link: string | undefined,
drive_id: string | undefined
) {
const drive = driveUrl(drive_id)
if (
next_link != null &&
next_link !== "" &&
!next_link.startsWith("https://graph.microsoft.com/")
) {
throw new Error(
"next_link must be an @odata.nextLink returned by Microsoft Graph"
)
}
let url: URL
if (next_link != null && next_link !== "") {
url = new URL(next_link)
} else {
url = new URL(`${drive}/${ref(folder)}/children`)
url.searchParams.append("$top", String(top))
if (order_by != null && order_by !== "") {
url.searchParams.append("$orderby", order_by)
}
if (filter != null && filter !== "") {
url.searchParams.append("$filter", filter)
}
if (select != null && select !== "") {
url.searchParams.append("$select", select)
}
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 hours ago