//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}:`
}
/**
* Get File or Folder
* Get the metadata of a file or folder in your OneDrive, or in another drive when `drive_id` is set: name, size, web URL, parent, timestamps and a short-lived `@microsoft.graph.downloadUrl`. `item` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). `expand` takes relationships such as `listItem`, `permissions` or `thumbnails`. 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,
item: string,
select: string | undefined,
expand: string | undefined,
drive_id: string | undefined
) {
const drive = driveUrl(drive_id)
const url = new URL(`${drive}/${ref(item)}`)
if (select != null && select !== "") {
url.searchParams.append("$select", select)
}
if (expand != null && expand !== "") {
url.searchParams.append("$expand", expand)
}
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