//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 Sharing Link
* Create a sharing link for a file or folder, or return the existing one of the same type and scope. `item` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). `scope` is who the link works for: `anonymous` (anyone, when the organization allows it), `organization` (signed-in members; work or school accounts only) or `users` (only the people the item is shared with); empty uses the organization's default. `embed` links exist only on personal OneDrive accounts. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.
*/
export async function main(
auth: RT.MsOnedrive,
item: string,
type: "view" | "edit" | "embed" = "view",
scope: "anonymous" | "organization" | "users" | undefined,
expiration_date_time: string | undefined,
drive_id: string | undefined
) {
const drive = driveUrl(drive_id)
const body: { [key: string]: any } = { type }
if (scope != null && (scope as string) !== "") body.scope = scope
if (expiration_date_time != null && expiration_date_time !== "") {
body.expirationDateTime = expiration_date_time
}
const response = await fetch(`${drive}/${ref(item)}/createLink`, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 hours ago