//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}:`
}
/**
* Create Folder
* Create a folder in your OneDrive, or in another drive when `drive_id` is set. `parent` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026`), the root when empty. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.
*/
export async function main(
auth: RT.MsOnedrive,
name: string,
parent: string | undefined,
conflict_behavior: "rename" | "replace" | "fail" = "rename",
drive_id: string | undefined
) {
const drive = driveUrl(drive_id)
const response = await fetch(`${drive}/${ref(parent)}/children`, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
name,
folder: {},
"@microsoft.graph.conflictBehavior": conflict_behavior,
}),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 hours ago