Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@stackone/files-sdk

Package Overview
Dependencies
Maintainers
3
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stackone/files-sdk - npm Package Compare versions

Comparing version
0.31.0
to
0.31.1
+1
-1
dist/index.es.mjs

@@ -1,1 +0,1 @@

import{HttpClient as e}from"@stackone/transport";import{z as t}from"zod";import"@googleworkspace/drive-picker-element";import{PublicClientApplication as i}from"@azure/msal-browser";import{nanoid as s}from"nanoid";const n=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:r(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),r=(e,t)=>"true"===e?"folder":"true"===t||"false"===t&&"false"===e?"file":"folder",a="id,name,size,created_at,remote_drive_id",o=(e,t,i,s,n,r)=>{const o=r?.fields?.length?r.fields.join(","):r?.useMinimalFields?a:"id,name,has_children,has_content,size,created_at,remote_drive_id,url";let c=`${e}${e.includes("?")?"&":"?"}fields=${o}`;return s&&(c+=`&next=${s}`),n&&(c+="&proxy[d]=t",Object.entries(n).forEach(([e,t])=>{c+=`&filter[${e}]=${t}`})),i?`${c}&filter[folder_id]=${i}`:t&&!n?`${c}&filter[is_root]=true`:c};function c(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)}function l(e,t,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(e,i):n?n.value=i:t.set(e,i),i}"function"==typeof SuppressedError&&SuppressedError;const d=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:h(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),h=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",f=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),u=(e,t,i,s,n)=>{let r=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{r+=`&filter[${e}]=${t}`}),i?`${r}&filter[folder_id]=${i}`:t?n?`${r}&filter[drive_id]=${t}`:`${r}&filter[drive_id]=${t}&filter[is_root]=true`:e},p=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var m,w,v,k;const g=()=>new e;class y{constructor({buildHttpClient:e=g}={}){m.set(this,void 0),w.set(this,void 0),v.set(this,void 0),k.set(this,void 0),l(this,m,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=p.parse(e);l(this,v,t,"f"),l(this,k,i??"https://api.stackone.com","f"),l(this,w,c(this,m,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,v,"f"))throw new Error("Session token is required");return(await c(this,w,"f").get({url:`${c(this,k,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":c(this,v,"f")}})).data.data.map(f)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s,n){try{if(!c(this,v,"f"))throw new Error("Session token is required");const n=s&&Object.keys(s).length>0,r=await c(this,w,"f").get({url:u(n?`${c(this,k,"f")}/unified/documents/files?page_size=25`:`${c(this,k,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":c(this,v,"f")}}),a=Array.isArray(r.data.data)?r.data.data:[];return{files:d(a),next:r.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}m=new WeakMap,w=new WeakMap,v=new WeakMap,k=new WeakMap;const b=e=>{if(!e.id)return;const t={name:e.name??"",url:e.url??"",remote_drive_id:"",type:e.type??""};return{identifiers:[{key:"id",value:e.id}],metadata:t}},E=t.object({appId:t.string(),clientId:t.string(),apiKey:t.string(),origin:t.string(),accessToken:t.string(),folderSelectionEnabled:t.boolean().optional().default(!1)});var $,_,M,S,W,F,I,C;const A="message",P=["picked","cancel","loaded"],N=["picker:oauth:error"];class T{constructor(){$.set(this,void 0),_.set(this,void 0),M.set(this,void 0),S.set(this,void 0),W.set(this,void 0),F.set(this,void 0),I.set(this,void 0),C.set(this,void 0)}initialize(e,t){l(this,$,t,"f");const i=E.parse(e);return l(this,_,i.clientId,"f"),l(this,M,i.apiKey,"f"),l(this,S,i.appId,"f"),l(this,F,i.origin,"f"),l(this,I,i.accessToken,"f"),l(this,C,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(A,this.messageListener.bind(this)),l(this,$,void 0,"f"),l(this,_,void 0,"f"),l(this,M,void 0,"f"),l(this,S,void 0,"f"),l(this,I,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),c(this,W,"f")){const e=document.getElementById(c(this,W,"f"));l(this,W,null,"f"),e&&e.remove()}return Promise.resolve()}async openNativeFilePicker({win:e=window,rootElementId:t="site-picker-container"}){const i=`<drive-picker scope="https://www.googleapis.com/auth/drive.readonly" multiselect="true" client-id="${c(this,_,"f")}" app-id="${c(this,S,"f")}" developer-key="${c(this,M,"f")}" origin="${c(this,F,"f")}" oauth-token="${c(this,I,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${c(this,C,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(A,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const n=document.getElementsByTagName("drive-picker");if(n&&Array.from(n).forEach(e=>e.remove()),c(this,W,"f")){const e=document.getElementById(c(this,W,"f"));l(this,W,null,"f"),e&&e.remove()}c(this,$,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const r=e.document.getElementById(t);r&&(r.style.display="none",r?.parentElement?.parentElement&&(r.parentElement.parentElement.style.border="none"))}async messageListener(e){const t="string"==typeof e.data?JSON.parse(e.data):e.data,i=t?.a?.[0];if(P.includes(i?.action)||N.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":l(this,W,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(b).filter(Boolean);await(c(this,$,"f")?.onSelect?.(e)),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){c(this,$,"f")?.onCancel?.(),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}}$=new WeakMap,_=new WeakMap,M=new WeakMap,S=new WeakMap,W=new WeakMap,F=new WeakMap,I=new WeakMap,C=new WeakMap;const z=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var L,U,O,x;const j=()=>new e;class B{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),x.set(this,void 0),l(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=z.parse(e);l(this,O,t,"f"),l(this,x,i??"https://api.stackone.com","f"),l(this,U,c(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,O,"f"))throw new Error("Session token is required");const t=await c(this,U,"f").get({url:`${c(this,x,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":c(this,O,"f"),Prefer:"heartbeat"}}),i=Array.isArray(t.data.data)?t.data.data:[];return i.length>0?[(e=i[0],{id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""})]:[]}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}var e}isNative(){return!1}async listFiles(e,t,i,s,r){try{if(!c(this,O,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,l=Boolean(t);if(!a&&!l)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};let d;r?.fields?.length?d={fields:r.fields}:r?.useMinimalFields&&(d={useMinimalFields:!0});const h=await c(this,U,"f").get({url:o(`${c(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s,d),headers:{"x-stackone-api-session-token":c(this,O,"f"),Prefer:"heartbeat"}}),f=Array.isArray(h.data.data)?h.data.data:[];return{files:n(f),next:h.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}async getFolder(e,t){try{if(!c(this,O,"f"))throw new Error("Session token is required");const i=t?.fields?.length?`?fields=${t.fields.join(",")}`:"",s=`${c(this,x,"f")}/unified/documents/folders/${encodeURIComponent(e)}${i}`,r=await c(this,U,"f").get({url:s,headers:{"x-stackone-api-session-token":c(this,O,"f")}}),a=r.data?.data;return a&&a.id?n([a])[0]??null:null}catch(e){throw new Error(`Failed to get Notion folder: ${e.message}`)}}async getFile(e,t){try{return await this.getFolder(e,t)}catch(e){throw new Error(`Failed to get Notion file: ${e instanceof Error?e.message:e}`)}}getSitesNaming(){return"Workspace"}}L=new WeakMap,U=new WeakMap,O=new WeakMap,x=new WeakMap;const q=e=>{const t=document.createElement("iframe");return t.id="filePickerIFrame",t.name="File Picker",t.style.position="absolute",t.style.zIndex="9999",t.style.left="0",t.style.right="0",t.style.top="0",t.style.bottom="0",t.style.width="100%",t.style.height="100%",t.style.backgroundColor="rgb(0,0,0, 0.5)",t.style.border="none","static"===getComputedStyle(e).position&&(e.style.position="relative"),e.appendChild(t),t},R=e=>{const t=e.parentReference?.driveId;if(!e.id)return;const i={name:e.name??"",url:e.webUrl??"",remote_drive_id:t??""};return{identifiers:[{key:"id",value:e.id}],metadata:i}},D=t.object({tenantId:t.string(),clientId:t.string(),redirectUri:t.string()});var H,J,G,K,V,Q,X,Y,Z;const ee="message",te=e=>new i(e),ie=()=>new e;class se{constructor({buildClientApplication:e=te,buildHttpClient:t=ie,channelIdGenerator:i=s}={}){H.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,void 0),Q.set(this,null),X.set(this,void 0),Y.set(this,null),Z.set(this,void 0),l(this,H,e,"f"),l(this,J,t,"f"),l(this,V,i(),"f")}async initialize(e,t){try{const i=D.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,G,c(this,H,"f").call(this,s),"f"),await c(this,G,"f").initialize(),l(this,K,c(this,J,"f").call(this),"f"),l(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=c(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await c(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await c(this,G,"f").loginPopup({scopes:i});if(c(this,G,"f").setActiveAccount(e?.account),e?.accessToken)return e.accessToken;throw new Error("Failed to retrieve the token to access OneDrive file storage client")}}catch(e){throw new Error(`Failed to login to OneDrive file storage client: ${e.message}`)}}async getAccessToken(e=[]){if(!c(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await c(this,G,"f").acquireTokenSilent({scopes:e})).accessToken}catch(e){throw new Error(`Failed to acquire access token: ${e.message}`)}}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,Y,"f")&&(c(this,Y,"f").remove(),l(this,Y,null,"f")),c(this,Q,"f")&&(c(this,Q,"f").close(),l(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),l(this,X,void 0,"f"),l(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,Y,"f"))return;const n={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,Y,q(a),"f");const o=c(this,Y,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(c(this,Y,"f")?.contentWindow&&e.source===c(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,V,"f")&&(l(this,Q,e.ports[0],"f"),c(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),c(this,Q,"f").start(),c(this,Q,"f").postMessage({type:"activate"}))}}),l(this,X,await this.getAccessToken([`${e}/.default`]),"f");const d=new URL("_layouts/15/FilePicker.aspx",e);d.search=r.toString();const h=o.document.createElement("form");h.setAttribute("action",d.toString()),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,X,"f")),h.appendChild(f),o.document.body.appendChild(h),c(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,X,"f")}});break;case"close":c(this,Z,"f")?.onCancel?.(),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,Q,"f")&&c(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(R).filter(Boolean);c(this,Z,"f")?.onSelect?.(i),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker()}}}H=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap,Z=new WeakMap;const ne=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),re=e=>{const t=e.sharepointIds?.siteId,i=e.parentReference?.driveId;if(!t||!i||!e.id)return;const s={name:e.name??"",url:e.webUrl??"",remote_drive_id:i??""};return{identifiers:[{key:"fileId",value:e.id},{key:"siteId",value:t},{key:"driveId",value:i}],metadata:s}},ae=t.object({tenantId:t.string(),clientId:t.string(),redirectUri:t.string(),accessToken:t.string(),secondaryAccessToken:t.string(),folderSelectionEnabled:t.boolean().optional().default(!1)});var oe,ce,le,de,he,fe,ue,pe,me,we,ve;const ke="message",ge=e=>new i(e),ye=()=>new e;class be{constructor({buildClientApplication:e=ge,buildHttpClient:t=ye,channelIdGenerator:i=s}={}){oe.set(this,void 0),ce.set(this,void 0),le.set(this,void 0),de.set(this,void 0),he.set(this,void 0),fe.set(this,null),ue.set(this,void 0),pe.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),l(this,oe,e,"f"),l(this,ce,t,"f"),l(this,he,i(),"f")}async initialize(e,t){try{const i=ae.parse(e);l(this,ue,i.accessToken,"f"),l(this,pe,i.secondaryAccessToken,"f"),l(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,le,c(this,oe,"f").call(this,s),"f"),await c(this,le,"f").initialize(),l(this,de,c(this,ce,"f").call(this),"f"),l(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await c(this,de,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${c(this,ue,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(ne)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,me,"f")&&(c(this,me,"f").remove(),l(this,me,null,"f")),c(this,fe,"f")&&(c(this,fe,"f").close(),l(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),l(this,ue,void 0,"f"),l(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,me,"f"))return;const n={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:c(this,ve,"f")?"all":"files",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,me,q(a),"f");const o=c(this,me,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(c(this,me,"f")?.contentWindow&&e.source===c(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,he,"f")&&(l(this,fe,e.ports[0],"f"),c(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),c(this,fe,"f").start(),c(this,fe,"f").postMessage({type:"activate"}))}});const d=`${e}_layouts/15/FilePicker.aspx?${r.toString()}`,h=o.document.createElement("form");h.setAttribute("action",d),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,pe,"f")??""),h.appendChild(f),o.document.body.appendChild(h),c(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,pe,"f")}});break;case"close":c(this,we,"f")?.onCancel?.(),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,fe,"f")&&c(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(re).filter(Boolean);c(this,we,"f")?.onSelect?.(i),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}oe=new WeakMap,ce=new WeakMap,le=new WeakMap,de=new WeakMap,he=new WeakMap,fe=new WeakMap,ue=new WeakMap,pe=new WeakMap,me=new WeakMap,we=new WeakMap,ve=new WeakMap;const Ee={build(e){switch(e){case"sharepoint":return new be;case"confluence":return new y;case"googledrive":return new T;case"onedrive":return new se;case"notion_documents":return new B;default:throw new Error(`Unsupported file storage type: ${e}`)}}};export{a as FILES_LIST_FIELDS_MINIMAL,Ee as FileStorageClientFactory};
import{HttpClient as e}from"@stackone/transport";import{z as t}from"zod";import"@googleworkspace/drive-picker-element";import{PublicClientApplication as i}from"@azure/msal-browser";import{nanoid as s}from"nanoid";const n=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:r(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),r=(e,t)=>"true"===e?"folder":"true"===t||"false"===t&&"false"===e?"file":"folder",a="id,name,size,created_at,remote_drive_id",o=(e,t,i,s,n,r)=>{const o=r?.fields?.length?r.fields.join(","):r?.useMinimalFields?a:"id,name,has_children,has_content,size,created_at,remote_drive_id,url";let c=`${e}${e.includes("?")?"&":"?"}fields=${o}`;return s&&(c+=`&next=${s}`),n&&(c+="&proxy[d]=t",Object.entries(n).forEach(([e,t])=>{c+=`&filter[${e}]=${t}`})),i?`${c}&filter[folder_id]=${i}`:t&&!n?`${c}&filter[is_root]=true`:c};function c(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)}function l(e,t,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(e,i):n?n.value=i:t.set(e,i),i}"function"==typeof SuppressedError&&SuppressedError;const d=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:h(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),h=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",f=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),u=(e,t,i,s,n)=>{let r=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{r+=`&filter[${e}]=${t}`}),i?`${r}&filter[folder_id]=${i}`:t?n?`${r}&filter[drive_id]=${t}`:`${r}&filter[drive_id]=${t}&filter[is_root]=true`:e},p=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var m,w,v,k;const g=()=>new e;class y{constructor({buildHttpClient:e=g}={}){m.set(this,void 0),w.set(this,void 0),v.set(this,void 0),k.set(this,void 0),l(this,m,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=p.parse(e);l(this,v,t,"f"),l(this,k,i??"https://api.stackone.com","f"),l(this,w,c(this,m,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,v,"f"))throw new Error("Session token is required");return(await c(this,w,"f").get({url:`${c(this,k,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":c(this,v,"f")}})).data.data.map(f)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s,n){try{if(!c(this,v,"f"))throw new Error("Session token is required");const n=s&&Object.keys(s).length>0,r=await c(this,w,"f").get({url:u(n?`${c(this,k,"f")}/unified/documents/files?page_size=25`:`${c(this,k,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":c(this,v,"f")}}),a=Array.isArray(r.data.data)?r.data.data:[];return{files:d(a),next:r.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}m=new WeakMap,w=new WeakMap,v=new WeakMap,k=new WeakMap;const b=e=>{if(!e.id)return;const t={name:e.name??"",url:e.url??"",remote_drive_id:"",type:e.type??""};return{identifiers:[{key:"id",value:e.id}],metadata:t}},E=t.object({appId:t.string(),clientId:t.string(),apiKey:t.string(),origin:t.string(),accessToken:t.string(),folderSelectionEnabled:t.boolean().optional().default(!1)});var $,_,M,S,W,F,I,C;const A="message",P=["picked","cancel","loaded"],N=["picker:oauth:error"];class T{constructor(){$.set(this,void 0),_.set(this,void 0),M.set(this,void 0),S.set(this,void 0),W.set(this,void 0),F.set(this,void 0),I.set(this,void 0),C.set(this,void 0)}initialize(e,t){l(this,$,t,"f");const i=E.parse(e);return l(this,_,i.clientId,"f"),l(this,M,i.apiKey,"f"),l(this,S,i.appId,"f"),l(this,F,i.origin,"f"),l(this,I,i.accessToken,"f"),l(this,C,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(A,this.messageListener.bind(this)),l(this,$,void 0,"f"),l(this,_,void 0,"f"),l(this,M,void 0,"f"),l(this,S,void 0,"f"),l(this,I,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),c(this,W,"f")){const e=document.getElementById(c(this,W,"f"));l(this,W,null,"f"),e&&e.remove()}return Promise.resolve()}async openNativeFilePicker({win:e=window,rootElementId:t="site-picker-container"}){const i=`<drive-picker scope="https://www.googleapis.com/auth/drive.readonly" multiselect="true" client-id="${c(this,_,"f")}" app-id="${c(this,S,"f")}" developer-key="${c(this,M,"f")}" origin="${c(this,F,"f")}" oauth-token="${c(this,I,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${c(this,C,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(A,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const n=document.getElementsByTagName("drive-picker");if(n&&Array.from(n).forEach(e=>e.remove()),c(this,W,"f")){const e=document.getElementById(c(this,W,"f"));l(this,W,null,"f"),e&&e.remove()}c(this,$,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const r=e.document.getElementById(t);r&&(r.style.display="none",r?.parentElement?.parentElement&&(r.parentElement.parentElement.style.border="none"))}async messageListener(e){const t="string"==typeof e.data?JSON.parse(e.data):e.data,i=t?.a?.[0];if(P.includes(i?.action)||N.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":l(this,W,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(b).filter(Boolean);await(c(this,$,"f")?.onSelect?.(e)),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){c(this,$,"f")?.onCancel?.(),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}}$=new WeakMap,_=new WeakMap,M=new WeakMap,S=new WeakMap,W=new WeakMap,F=new WeakMap,I=new WeakMap,C=new WeakMap;const z=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var L,U,O,x;const j=()=>new e;class B{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),x.set(this,void 0),l(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=z.parse(e);l(this,O,t,"f"),l(this,x,i??"https://api.stackone.com","f"),l(this,U,c(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,O,"f"))throw new Error("Session token is required");const t=await c(this,U,"f").get({url:`${c(this,x,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":c(this,O,"f"),Prefer:"heartbeat"}}),i=Array.isArray(t.data.data)?t.data.data:[];return i.length>0?[(e=i[0],{id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""})]:[]}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}var e}isNative(){return!1}async listFiles(e,t,i,s,r){try{if(!c(this,O,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,l=Boolean(t);if(!a&&!l)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};let d;r?.fields?.length?d={fields:r.fields}:r?.useMinimalFields&&(d={useMinimalFields:!0});const h=await c(this,U,"f").get({url:o(`${c(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s,d),headers:{"x-stackone-api-session-token":c(this,O,"f"),...a?{}:{Prefer:"heartbeat"}}}),f=Array.isArray(h.data.data)?h.data.data:[];return{files:n(f),next:h.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}async getFolder(e,t){try{if(!c(this,O,"f"))throw new Error("Session token is required");const i=t?.fields?.length?`?fields=${t.fields.join(",")}`:"",s=`${c(this,x,"f")}/unified/documents/folders/${encodeURIComponent(e)}${i}`,r=await c(this,U,"f").get({url:s,headers:{"x-stackone-api-session-token":c(this,O,"f")}}),a=r.data?.data;return a&&a.id?n([a])[0]??null:null}catch(e){throw new Error(`Failed to get Notion folder: ${e.message}`)}}async getFile(e,t){try{return await this.getFolder(e,t)}catch(e){throw new Error(`Failed to get Notion file: ${e instanceof Error?e.message:e}`)}}getSitesNaming(){return"Workspace"}}L=new WeakMap,U=new WeakMap,O=new WeakMap,x=new WeakMap;const q=e=>{const t=document.createElement("iframe");return t.id="filePickerIFrame",t.name="File Picker",t.style.position="absolute",t.style.zIndex="9999",t.style.left="0",t.style.right="0",t.style.top="0",t.style.bottom="0",t.style.width="100%",t.style.height="100%",t.style.backgroundColor="rgb(0,0,0, 0.5)",t.style.border="none","static"===getComputedStyle(e).position&&(e.style.position="relative"),e.appendChild(t),t},R=e=>{const t=e.parentReference?.driveId;if(!e.id)return;const i={name:e.name??"",url:e.webUrl??"",remote_drive_id:t??""};return{identifiers:[{key:"id",value:e.id}],metadata:i}},D=t.object({tenantId:t.string(),clientId:t.string(),redirectUri:t.string()});var H,J,G,K,V,Q,X,Y,Z;const ee="message",te=e=>new i(e),ie=()=>new e;class se{constructor({buildClientApplication:e=te,buildHttpClient:t=ie,channelIdGenerator:i=s}={}){H.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,void 0),Q.set(this,null),X.set(this,void 0),Y.set(this,null),Z.set(this,void 0),l(this,H,e,"f"),l(this,J,t,"f"),l(this,V,i(),"f")}async initialize(e,t){try{const i=D.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,G,c(this,H,"f").call(this,s),"f"),await c(this,G,"f").initialize(),l(this,K,c(this,J,"f").call(this),"f"),l(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=c(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await c(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await c(this,G,"f").loginPopup({scopes:i});if(c(this,G,"f").setActiveAccount(e?.account),e?.accessToken)return e.accessToken;throw new Error("Failed to retrieve the token to access OneDrive file storage client")}}catch(e){throw new Error(`Failed to login to OneDrive file storage client: ${e.message}`)}}async getAccessToken(e=[]){if(!c(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await c(this,G,"f").acquireTokenSilent({scopes:e})).accessToken}catch(e){throw new Error(`Failed to acquire access token: ${e.message}`)}}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,Y,"f")&&(c(this,Y,"f").remove(),l(this,Y,null,"f")),c(this,Q,"f")&&(c(this,Q,"f").close(),l(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),l(this,X,void 0,"f"),l(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,Y,"f"))return;const n={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,Y,q(a),"f");const o=c(this,Y,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(c(this,Y,"f")?.contentWindow&&e.source===c(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,V,"f")&&(l(this,Q,e.ports[0],"f"),c(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),c(this,Q,"f").start(),c(this,Q,"f").postMessage({type:"activate"}))}}),l(this,X,await this.getAccessToken([`${e}/.default`]),"f");const d=new URL("_layouts/15/FilePicker.aspx",e);d.search=r.toString();const h=o.document.createElement("form");h.setAttribute("action",d.toString()),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,X,"f")),h.appendChild(f),o.document.body.appendChild(h),c(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,X,"f")}});break;case"close":c(this,Z,"f")?.onCancel?.(),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,Q,"f")&&c(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(R).filter(Boolean);c(this,Z,"f")?.onSelect?.(i),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker()}}}H=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap,Z=new WeakMap;const ne=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),re=e=>{const t=e.sharepointIds?.siteId,i=e.parentReference?.driveId;if(!t||!i||!e.id)return;const s={name:e.name??"",url:e.webUrl??"",remote_drive_id:i??""};return{identifiers:[{key:"fileId",value:e.id},{key:"siteId",value:t},{key:"driveId",value:i}],metadata:s}},ae=t.object({tenantId:t.string(),clientId:t.string(),redirectUri:t.string(),accessToken:t.string(),secondaryAccessToken:t.string(),folderSelectionEnabled:t.boolean().optional().default(!1)});var oe,ce,le,de,he,fe,ue,pe,me,we,ve;const ke="message",ge=e=>new i(e),ye=()=>new e;class be{constructor({buildClientApplication:e=ge,buildHttpClient:t=ye,channelIdGenerator:i=s}={}){oe.set(this,void 0),ce.set(this,void 0),le.set(this,void 0),de.set(this,void 0),he.set(this,void 0),fe.set(this,null),ue.set(this,void 0),pe.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),l(this,oe,e,"f"),l(this,ce,t,"f"),l(this,he,i(),"f")}async initialize(e,t){try{const i=ae.parse(e);l(this,ue,i.accessToken,"f"),l(this,pe,i.secondaryAccessToken,"f"),l(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,le,c(this,oe,"f").call(this,s),"f"),await c(this,le,"f").initialize(),l(this,de,c(this,ce,"f").call(this),"f"),l(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await c(this,de,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${c(this,ue,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(ne)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,me,"f")&&(c(this,me,"f").remove(),l(this,me,null,"f")),c(this,fe,"f")&&(c(this,fe,"f").close(),l(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),l(this,ue,void 0,"f"),l(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,me,"f"))return;const n={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:c(this,ve,"f")?"all":"files",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,me,q(a),"f");const o=c(this,me,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(c(this,me,"f")?.contentWindow&&e.source===c(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,he,"f")&&(l(this,fe,e.ports[0],"f"),c(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),c(this,fe,"f").start(),c(this,fe,"f").postMessage({type:"activate"}))}});const d=`${e}_layouts/15/FilePicker.aspx?${r.toString()}`,h=o.document.createElement("form");h.setAttribute("action",d),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,pe,"f")??""),h.appendChild(f),o.document.body.appendChild(h),c(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,pe,"f")}});break;case"close":c(this,we,"f")?.onCancel?.(),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,fe,"f")&&c(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(re).filter(Boolean);c(this,we,"f")?.onSelect?.(i),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}oe=new WeakMap,ce=new WeakMap,le=new WeakMap,de=new WeakMap,he=new WeakMap,fe=new WeakMap,ue=new WeakMap,pe=new WeakMap,me=new WeakMap,we=new WeakMap,ve=new WeakMap;const Ee={build(e){switch(e){case"sharepoint":return new be;case"confluence":return new y;case"googledrive":return new T;case"onedrive":return new se;case"notion_documents":return new B;default:throw new Error(`Unsupported file storage type: ${e}`)}}};export{a as FILES_LIST_FIELDS_MINIMAL,Ee as FileStorageClientFactory};

@@ -1,1 +0,1 @@

"use strict";var e=require("@stackone/transport"),t=require("zod");require("@googleworkspace/drive-picker-element");var i=require("@azure/msal-browser"),s=require("nanoid");const n=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:r(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),r=(e,t)=>"true"===e?"folder":"true"===t||"false"===t&&"false"===e?"file":"folder",a="id,name,size,created_at,remote_drive_id",o=(e,t,i,s,n,r)=>{const o=r?.fields?.length?r.fields.join(","):r?.useMinimalFields?a:"id,name,has_children,has_content,size,created_at,remote_drive_id,url";let c=`${e}${e.includes("?")?"&":"?"}fields=${o}`;return s&&(c+=`&next=${s}`),n&&(c+="&proxy[d]=t",Object.entries(n).forEach(([e,t])=>{c+=`&filter[${e}]=${t}`})),i?`${c}&filter[folder_id]=${i}`:t&&!n?`${c}&filter[is_root]=true`:c};function c(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)}function l(e,t,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(e,i):n?n.value=i:t.set(e,i),i}"function"==typeof SuppressedError&&SuppressedError;const d=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:h(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),h=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",f=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),u=(e,t,i,s,n)=>{let r=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{r+=`&filter[${e}]=${t}`}),i?`${r}&filter[folder_id]=${i}`:t?n?`${r}&filter[drive_id]=${t}`:`${r}&filter[drive_id]=${t}&filter[is_root]=true`:e},p=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var m,w,v,k;const g=()=>new e.HttpClient;class y{constructor({buildHttpClient:e=g}={}){m.set(this,void 0),w.set(this,void 0),v.set(this,void 0),k.set(this,void 0),l(this,m,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=p.parse(e);l(this,v,t,"f"),l(this,k,i??"https://api.stackone.com","f"),l(this,w,c(this,m,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,v,"f"))throw new Error("Session token is required");return(await c(this,w,"f").get({url:`${c(this,k,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":c(this,v,"f")}})).data.data.map(f)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s,n){try{if(!c(this,v,"f"))throw new Error("Session token is required");const n=s&&Object.keys(s).length>0,r=await c(this,w,"f").get({url:u(n?`${c(this,k,"f")}/unified/documents/files?page_size=25`:`${c(this,k,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":c(this,v,"f")}}),a=Array.isArray(r.data.data)?r.data.data:[];return{files:d(a),next:r.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}m=new WeakMap,w=new WeakMap,v=new WeakMap,k=new WeakMap;const b=e=>{if(!e.id)return;const t={name:e.name??"",url:e.url??"",remote_drive_id:"",type:e.type??""};return{identifiers:[{key:"id",value:e.id}],metadata:t}},E=t.z.object({appId:t.z.string(),clientId:t.z.string(),apiKey:t.z.string(),origin:t.z.string(),accessToken:t.z.string(),folderSelectionEnabled:t.z.boolean().optional().default(!1)});var $,_,z,M,S,F,I,C;const W="message",A=["picked","cancel","loaded"],P=["picker:oauth:error"];class N{constructor(){$.set(this,void 0),_.set(this,void 0),z.set(this,void 0),M.set(this,void 0),S.set(this,void 0),F.set(this,void 0),I.set(this,void 0),C.set(this,void 0)}initialize(e,t){l(this,$,t,"f");const i=E.parse(e);return l(this,_,i.clientId,"f"),l(this,z,i.apiKey,"f"),l(this,M,i.appId,"f"),l(this,F,i.origin,"f"),l(this,I,i.accessToken,"f"),l(this,C,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(W,this.messageListener.bind(this)),l(this,$,void 0,"f"),l(this,_,void 0,"f"),l(this,z,void 0,"f"),l(this,M,void 0,"f"),l(this,I,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),c(this,S,"f")){const e=document.getElementById(c(this,S,"f"));l(this,S,null,"f"),e&&e.remove()}return Promise.resolve()}async openNativeFilePicker({win:e=window,rootElementId:t="site-picker-container"}){const i=`<drive-picker scope="https://www.googleapis.com/auth/drive.readonly" multiselect="true" client-id="${c(this,_,"f")}" app-id="${c(this,M,"f")}" developer-key="${c(this,z,"f")}" origin="${c(this,F,"f")}" oauth-token="${c(this,I,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${c(this,C,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(W,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const n=document.getElementsByTagName("drive-picker");if(n&&Array.from(n).forEach(e=>e.remove()),c(this,S,"f")){const e=document.getElementById(c(this,S,"f"));l(this,S,null,"f"),e&&e.remove()}c(this,$,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const r=e.document.getElementById(t);r&&(r.style.display="none",r?.parentElement?.parentElement&&(r.parentElement.parentElement.style.border="none"))}async messageListener(e){const t="string"==typeof e.data?JSON.parse(e.data):e.data,i=t?.a?.[0];if(A.includes(i?.action)||P.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":l(this,S,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(b).filter(Boolean);await(c(this,$,"f")?.onSelect?.(e)),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){c(this,$,"f")?.onCancel?.(),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}}$=new WeakMap,_=new WeakMap,z=new WeakMap,M=new WeakMap,S=new WeakMap,F=new WeakMap,I=new WeakMap,C=new WeakMap;const T=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var L,U,O,x;const j=()=>new e.HttpClient;class q{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),x.set(this,void 0),l(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=T.parse(e);l(this,O,t,"f"),l(this,x,i??"https://api.stackone.com","f"),l(this,U,c(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,O,"f"))throw new Error("Session token is required");const t=await c(this,U,"f").get({url:`${c(this,x,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":c(this,O,"f"),Prefer:"heartbeat"}}),i=Array.isArray(t.data.data)?t.data.data:[];return i.length>0?[(e=i[0],{id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""})]:[]}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}var e}isNative(){return!1}async listFiles(e,t,i,s,r){try{if(!c(this,O,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,l=Boolean(t);if(!a&&!l)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};let d;r?.fields?.length?d={fields:r.fields}:r?.useMinimalFields&&(d={useMinimalFields:!0});const h=await c(this,U,"f").get({url:o(`${c(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s,d),headers:{"x-stackone-api-session-token":c(this,O,"f"),Prefer:"heartbeat"}}),f=Array.isArray(h.data.data)?h.data.data:[];return{files:n(f),next:h.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}async getFolder(e,t){try{if(!c(this,O,"f"))throw new Error("Session token is required");const i=t?.fields?.length?`?fields=${t.fields.join(",")}`:"",s=`${c(this,x,"f")}/unified/documents/folders/${encodeURIComponent(e)}${i}`,r=await c(this,U,"f").get({url:s,headers:{"x-stackone-api-session-token":c(this,O,"f")}}),a=r.data?.data;return a&&a.id?n([a])[0]??null:null}catch(e){throw new Error(`Failed to get Notion folder: ${e.message}`)}}async getFile(e,t){try{return await this.getFolder(e,t)}catch(e){throw new Error(`Failed to get Notion file: ${e instanceof Error?e.message:e}`)}}getSitesNaming(){return"Workspace"}}L=new WeakMap,U=new WeakMap,O=new WeakMap,x=new WeakMap;const B=e=>{const t=document.createElement("iframe");return t.id="filePickerIFrame",t.name="File Picker",t.style.position="absolute",t.style.zIndex="9999",t.style.left="0",t.style.right="0",t.style.top="0",t.style.bottom="0",t.style.width="100%",t.style.height="100%",t.style.backgroundColor="rgb(0,0,0, 0.5)",t.style.border="none","static"===getComputedStyle(e).position&&(e.style.position="relative"),e.appendChild(t),t},H=e=>{const t=e.parentReference?.driveId;if(!e.id)return;const i={name:e.name??"",url:e.webUrl??"",remote_drive_id:t??""};return{identifiers:[{key:"id",value:e.id}],metadata:i}},D=t.z.object({tenantId:t.z.string(),clientId:t.z.string(),redirectUri:t.z.string()});var R,J,G,K,V,Q,X,Y,Z;const ee="message",te=e=>new i.PublicClientApplication(e),ie=()=>new e.HttpClient;class se{constructor({buildClientApplication:e=te,buildHttpClient:t=ie,channelIdGenerator:i=s.nanoid}={}){R.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,void 0),Q.set(this,null),X.set(this,void 0),Y.set(this,null),Z.set(this,void 0),l(this,R,e,"f"),l(this,J,t,"f"),l(this,V,i(),"f")}async initialize(e,t){try{const i=D.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,G,c(this,R,"f").call(this,s),"f"),await c(this,G,"f").initialize(),l(this,K,c(this,J,"f").call(this),"f"),l(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=c(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await c(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await c(this,G,"f").loginPopup({scopes:i});if(c(this,G,"f").setActiveAccount(e?.account),e?.accessToken)return e.accessToken;throw new Error("Failed to retrieve the token to access OneDrive file storage client")}}catch(e){throw new Error(`Failed to login to OneDrive file storage client: ${e.message}`)}}async getAccessToken(e=[]){if(!c(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await c(this,G,"f").acquireTokenSilent({scopes:e})).accessToken}catch(e){throw new Error(`Failed to acquire access token: ${e.message}`)}}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,Y,"f")&&(c(this,Y,"f").remove(),l(this,Y,null,"f")),c(this,Q,"f")&&(c(this,Q,"f").close(),l(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),l(this,X,void 0,"f"),l(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,Y,"f"))return;const n={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,Y,B(a),"f");const o=c(this,Y,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(c(this,Y,"f")?.contentWindow&&e.source===c(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,V,"f")&&(l(this,Q,e.ports[0],"f"),c(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),c(this,Q,"f").start(),c(this,Q,"f").postMessage({type:"activate"}))}}),l(this,X,await this.getAccessToken([`${e}/.default`]),"f");const d=new URL("_layouts/15/FilePicker.aspx",e);d.search=r.toString();const h=o.document.createElement("form");h.setAttribute("action",d.toString()),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,X,"f")),h.appendChild(f),o.document.body.appendChild(h),c(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,X,"f")}});break;case"close":c(this,Z,"f")?.onCancel?.(),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,Q,"f")&&c(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(H).filter(Boolean);c(this,Z,"f")?.onSelect?.(i),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker()}}}R=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap,Z=new WeakMap;const ne=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),re=e=>{const t=e.sharepointIds?.siteId,i=e.parentReference?.driveId;if(!t||!i||!e.id)return;const s={name:e.name??"",url:e.webUrl??"",remote_drive_id:i??""};return{identifiers:[{key:"fileId",value:e.id},{key:"siteId",value:t},{key:"driveId",value:i}],metadata:s}},ae=t.z.object({tenantId:t.z.string(),clientId:t.z.string(),redirectUri:t.z.string(),accessToken:t.z.string(),secondaryAccessToken:t.z.string(),folderSelectionEnabled:t.z.boolean().optional().default(!1)});var oe,ce,le,de,he,fe,ue,pe,me,we,ve;const ke="message",ge=e=>new i.PublicClientApplication(e),ye=()=>new e.HttpClient;class be{constructor({buildClientApplication:e=ge,buildHttpClient:t=ye,channelIdGenerator:i=s.nanoid}={}){oe.set(this,void 0),ce.set(this,void 0),le.set(this,void 0),de.set(this,void 0),he.set(this,void 0),fe.set(this,null),ue.set(this,void 0),pe.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),l(this,oe,e,"f"),l(this,ce,t,"f"),l(this,he,i(),"f")}async initialize(e,t){try{const i=ae.parse(e);l(this,ue,i.accessToken,"f"),l(this,pe,i.secondaryAccessToken,"f"),l(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,le,c(this,oe,"f").call(this,s),"f"),await c(this,le,"f").initialize(),l(this,de,c(this,ce,"f").call(this),"f"),l(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await c(this,de,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${c(this,ue,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(ne)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,me,"f")&&(c(this,me,"f").remove(),l(this,me,null,"f")),c(this,fe,"f")&&(c(this,fe,"f").close(),l(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),l(this,ue,void 0,"f"),l(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,me,"f"))return;const n={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:c(this,ve,"f")?"all":"files",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,me,B(a),"f");const o=c(this,me,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(c(this,me,"f")?.contentWindow&&e.source===c(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,he,"f")&&(l(this,fe,e.ports[0],"f"),c(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),c(this,fe,"f").start(),c(this,fe,"f").postMessage({type:"activate"}))}});const d=`${e}_layouts/15/FilePicker.aspx?${r.toString()}`,h=o.document.createElement("form");h.setAttribute("action",d),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,pe,"f")??""),h.appendChild(f),o.document.body.appendChild(h),c(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,pe,"f")}});break;case"close":c(this,we,"f")?.onCancel?.(),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,fe,"f")&&c(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(re).filter(Boolean);c(this,we,"f")?.onSelect?.(i),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}oe=new WeakMap,ce=new WeakMap,le=new WeakMap,de=new WeakMap,he=new WeakMap,fe=new WeakMap,ue=new WeakMap,pe=new WeakMap,me=new WeakMap,we=new WeakMap,ve=new WeakMap;const Ee={build(e){switch(e){case"sharepoint":return new be;case"confluence":return new y;case"googledrive":return new N;case"onedrive":return new se;case"notion_documents":return new q;default:throw new Error(`Unsupported file storage type: ${e}`)}}};exports.FILES_LIST_FIELDS_MINIMAL=a,exports.FileStorageClientFactory=Ee;
"use strict";var e=require("@stackone/transport"),t=require("zod");require("@googleworkspace/drive-picker-element");var i=require("@azure/msal-browser"),s=require("nanoid");const n=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:r(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),r=(e,t)=>"true"===e?"folder":"true"===t||"false"===t&&"false"===e?"file":"folder",a="id,name,size,created_at,remote_drive_id",o=(e,t,i,s,n,r)=>{const o=r?.fields?.length?r.fields.join(","):r?.useMinimalFields?a:"id,name,has_children,has_content,size,created_at,remote_drive_id,url";let c=`${e}${e.includes("?")?"&":"?"}fields=${o}`;return s&&(c+=`&next=${s}`),n&&(c+="&proxy[d]=t",Object.entries(n).forEach(([e,t])=>{c+=`&filter[${e}]=${t}`})),i?`${c}&filter[folder_id]=${i}`:t&&!n?`${c}&filter[is_root]=true`:c};function c(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)}function l(e,t,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(e,i):n?n.value=i:t.set(e,i),i}"function"==typeof SuppressedError&&SuppressedError;const d=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:h(e.has_children,e.has_content),size:e.size,createdAt:e.created_at??"",fileType:"html",remote_drive_id:e.remote_drive_id,hasContent:"true"===e.has_content,hasChildren:"true"===e.has_children,url:e.url})),h=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",f=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),u=(e,t,i,s,n)=>{let r=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{r+=`&filter[${e}]=${t}`}),i?`${r}&filter[folder_id]=${i}`:t?n?`${r}&filter[drive_id]=${t}`:`${r}&filter[drive_id]=${t}&filter[is_root]=true`:e},p=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var m,w,v,k;const g=()=>new e.HttpClient;class y{constructor({buildHttpClient:e=g}={}){m.set(this,void 0),w.set(this,void 0),v.set(this,void 0),k.set(this,void 0),l(this,m,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=p.parse(e);l(this,v,t,"f"),l(this,k,i??"https://api.stackone.com","f"),l(this,w,c(this,m,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,v,"f"))throw new Error("Session token is required");return(await c(this,w,"f").get({url:`${c(this,k,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":c(this,v,"f")}})).data.data.map(f)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s,n){try{if(!c(this,v,"f"))throw new Error("Session token is required");const n=s&&Object.keys(s).length>0,r=await c(this,w,"f").get({url:u(n?`${c(this,k,"f")}/unified/documents/files?page_size=25`:`${c(this,k,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":c(this,v,"f")}}),a=Array.isArray(r.data.data)?r.data.data:[];return{files:d(a),next:r.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}m=new WeakMap,w=new WeakMap,v=new WeakMap,k=new WeakMap;const b=e=>{if(!e.id)return;const t={name:e.name??"",url:e.url??"",remote_drive_id:"",type:e.type??""};return{identifiers:[{key:"id",value:e.id}],metadata:t}},E=t.z.object({appId:t.z.string(),clientId:t.z.string(),apiKey:t.z.string(),origin:t.z.string(),accessToken:t.z.string(),folderSelectionEnabled:t.z.boolean().optional().default(!1)});var $,_,z,M,S,F,I,C;const W="message",A=["picked","cancel","loaded"],P=["picker:oauth:error"];class N{constructor(){$.set(this,void 0),_.set(this,void 0),z.set(this,void 0),M.set(this,void 0),S.set(this,void 0),F.set(this,void 0),I.set(this,void 0),C.set(this,void 0)}initialize(e,t){l(this,$,t,"f");const i=E.parse(e);return l(this,_,i.clientId,"f"),l(this,z,i.apiKey,"f"),l(this,M,i.appId,"f"),l(this,F,i.origin,"f"),l(this,I,i.accessToken,"f"),l(this,C,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(W,this.messageListener.bind(this)),l(this,$,void 0,"f"),l(this,_,void 0,"f"),l(this,z,void 0,"f"),l(this,M,void 0,"f"),l(this,I,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),c(this,S,"f")){const e=document.getElementById(c(this,S,"f"));l(this,S,null,"f"),e&&e.remove()}return Promise.resolve()}async openNativeFilePicker({win:e=window,rootElementId:t="site-picker-container"}){const i=`<drive-picker scope="https://www.googleapis.com/auth/drive.readonly" multiselect="true" client-id="${c(this,_,"f")}" app-id="${c(this,M,"f")}" developer-key="${c(this,z,"f")}" origin="${c(this,F,"f")}" oauth-token="${c(this,I,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${c(this,C,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(W,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const n=document.getElementsByTagName("drive-picker");if(n&&Array.from(n).forEach(e=>e.remove()),c(this,S,"f")){const e=document.getElementById(c(this,S,"f"));l(this,S,null,"f"),e&&e.remove()}c(this,$,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const r=e.document.getElementById(t);r&&(r.style.display="none",r?.parentElement?.parentElement&&(r.parentElement.parentElement.style.border="none"))}async messageListener(e){const t="string"==typeof e.data?JSON.parse(e.data):e.data,i=t?.a?.[0];if(A.includes(i?.action)||P.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":l(this,S,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(b).filter(Boolean);await(c(this,$,"f")?.onSelect?.(e)),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){c(this,$,"f")?.onCancel?.(),c(this,$,"f")?.onClose?.(),this.closeNativeFilePicker()}}$=new WeakMap,_=new WeakMap,z=new WeakMap,M=new WeakMap,S=new WeakMap,F=new WeakMap,I=new WeakMap,C=new WeakMap;const T=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var L,U,O,x;const j=()=>new e.HttpClient;class q{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),x.set(this,void 0),l(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=T.parse(e);l(this,O,t,"f"),l(this,x,i??"https://api.stackone.com","f"),l(this,U,c(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(){try{if(!c(this,O,"f"))throw new Error("Session token is required");const t=await c(this,U,"f").get({url:`${c(this,x,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":c(this,O,"f"),Prefer:"heartbeat"}}),i=Array.isArray(t.data.data)?t.data.data:[];return i.length>0?[(e=i[0],{id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""})]:[]}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}var e}isNative(){return!1}async listFiles(e,t,i,s,r){try{if(!c(this,O,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,l=Boolean(t);if(!a&&!l)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};let d;r?.fields?.length?d={fields:r.fields}:r?.useMinimalFields&&(d={useMinimalFields:!0});const h=await c(this,U,"f").get({url:o(`${c(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s,d),headers:{"x-stackone-api-session-token":c(this,O,"f"),...a?{}:{Prefer:"heartbeat"}}}),f=Array.isArray(h.data.data)?h.data.data:[];return{files:n(f),next:h.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}async getFolder(e,t){try{if(!c(this,O,"f"))throw new Error("Session token is required");const i=t?.fields?.length?`?fields=${t.fields.join(",")}`:"",s=`${c(this,x,"f")}/unified/documents/folders/${encodeURIComponent(e)}${i}`,r=await c(this,U,"f").get({url:s,headers:{"x-stackone-api-session-token":c(this,O,"f")}}),a=r.data?.data;return a&&a.id?n([a])[0]??null:null}catch(e){throw new Error(`Failed to get Notion folder: ${e.message}`)}}async getFile(e,t){try{return await this.getFolder(e,t)}catch(e){throw new Error(`Failed to get Notion file: ${e instanceof Error?e.message:e}`)}}getSitesNaming(){return"Workspace"}}L=new WeakMap,U=new WeakMap,O=new WeakMap,x=new WeakMap;const B=e=>{const t=document.createElement("iframe");return t.id="filePickerIFrame",t.name="File Picker",t.style.position="absolute",t.style.zIndex="9999",t.style.left="0",t.style.right="0",t.style.top="0",t.style.bottom="0",t.style.width="100%",t.style.height="100%",t.style.backgroundColor="rgb(0,0,0, 0.5)",t.style.border="none","static"===getComputedStyle(e).position&&(e.style.position="relative"),e.appendChild(t),t},H=e=>{const t=e.parentReference?.driveId;if(!e.id)return;const i={name:e.name??"",url:e.webUrl??"",remote_drive_id:t??""};return{identifiers:[{key:"id",value:e.id}],metadata:i}},D=t.z.object({tenantId:t.z.string(),clientId:t.z.string(),redirectUri:t.z.string()});var R,J,G,K,V,Q,X,Y,Z;const ee="message",te=e=>new i.PublicClientApplication(e),ie=()=>new e.HttpClient;class se{constructor({buildClientApplication:e=te,buildHttpClient:t=ie,channelIdGenerator:i=s.nanoid}={}){R.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,void 0),Q.set(this,null),X.set(this,void 0),Y.set(this,null),Z.set(this,void 0),l(this,R,e,"f"),l(this,J,t,"f"),l(this,V,i(),"f")}async initialize(e,t){try{const i=D.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,G,c(this,R,"f").call(this,s),"f"),await c(this,G,"f").initialize(),l(this,K,c(this,J,"f").call(this),"f"),l(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=c(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await c(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await c(this,G,"f").loginPopup({scopes:i});if(c(this,G,"f").setActiveAccount(e?.account),e?.accessToken)return e.accessToken;throw new Error("Failed to retrieve the token to access OneDrive file storage client")}}catch(e){throw new Error(`Failed to login to OneDrive file storage client: ${e.message}`)}}async getAccessToken(e=[]){if(!c(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await c(this,G,"f").acquireTokenSilent({scopes:e})).accessToken}catch(e){throw new Error(`Failed to acquire access token: ${e.message}`)}}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,Y,"f")&&(c(this,Y,"f").remove(),l(this,Y,null,"f")),c(this,Q,"f")&&(c(this,Q,"f").close(),l(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),l(this,X,void 0,"f"),l(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,Y,"f"))return;const n={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,Y,B(a),"f");const o=c(this,Y,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(c(this,Y,"f")?.contentWindow&&e.source===c(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,V,"f")&&(l(this,Q,e.ports[0],"f"),c(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),c(this,Q,"f").start(),c(this,Q,"f").postMessage({type:"activate"}))}}),l(this,X,await this.getAccessToken([`${e}/.default`]),"f");const d=new URL("_layouts/15/FilePicker.aspx",e);d.search=r.toString();const h=o.document.createElement("form");h.setAttribute("action",d.toString()),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,X,"f")),h.appendChild(f),o.document.body.appendChild(h),c(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,X,"f")}});break;case"close":c(this,Z,"f")?.onCancel?.(),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,Q,"f")&&c(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(H).filter(Boolean);c(this,Z,"f")?.onSelect?.(i),c(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker()}}}R=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap,Z=new WeakMap;const ne=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),re=e=>{const t=e.sharepointIds?.siteId,i=e.parentReference?.driveId;if(!t||!i||!e.id)return;const s={name:e.name??"",url:e.webUrl??"",remote_drive_id:i??""};return{identifiers:[{key:"fileId",value:e.id},{key:"siteId",value:t},{key:"driveId",value:i}],metadata:s}},ae=t.z.object({tenantId:t.z.string(),clientId:t.z.string(),redirectUri:t.z.string(),accessToken:t.z.string(),secondaryAccessToken:t.z.string(),folderSelectionEnabled:t.z.boolean().optional().default(!1)});var oe,ce,le,de,he,fe,ue,pe,me,we,ve;const ke="message",ge=e=>new i.PublicClientApplication(e),ye=()=>new e.HttpClient;class be{constructor({buildClientApplication:e=ge,buildHttpClient:t=ye,channelIdGenerator:i=s.nanoid}={}){oe.set(this,void 0),ce.set(this,void 0),le.set(this,void 0),de.set(this,void 0),he.set(this,void 0),fe.set(this,null),ue.set(this,void 0),pe.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),l(this,oe,e,"f"),l(this,ce,t,"f"),l(this,he,i(),"f")}async initialize(e,t){try{const i=ae.parse(e);l(this,ue,i.accessToken,"f"),l(this,pe,i.secondaryAccessToken,"f"),l(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};l(this,le,c(this,oe,"f").call(this,s),"f"),await c(this,le,"f").initialize(),l(this,de,c(this,ce,"f").call(this),"f"),l(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await c(this,de,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${c(this,ue,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(ne)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){c(this,me,"f")&&(c(this,me,"f").remove(),l(this,me,null,"f")),c(this,fe,"f")&&(c(this,fe,"f").close(),l(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),l(this,ue,void 0,"f"),l(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(c(this,me,"f"))return;const n={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:c(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:c(this,ve,"f")?"all":"files",pivots:{}}},r=new URLSearchParams({filePicker:JSON.stringify(n)}),a=i.document.getElementById(s);if(!a)throw new Error("Failed to open native file picker: root element not found");l(this,me,B(a),"f");const o=c(this,me,"f").contentWindow;if(!o)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(c(this,me,"f")?.contentWindow&&e.source===c(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===c(this,he,"f")&&(l(this,fe,e.ports[0],"f"),c(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),c(this,fe,"f").start(),c(this,fe,"f").postMessage({type:"activate"}))}});const d=`${e}_layouts/15/FilePicker.aspx?${r.toString()}`,h=o.document.createElement("form");h.setAttribute("action",d),h.setAttribute("method","POST");const f=o.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",c(this,pe,"f")??""),h.appendChild(f),o.document.body.appendChild(h),c(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":c(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:c(this,pe,"f")}});break;case"close":c(this,we,"f")?.onCancel?.(),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":c(this,fe,"f")&&c(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(re).filter(Boolean);c(this,we,"f")?.onSelect?.(i),c(this,we,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}oe=new WeakMap,ce=new WeakMap,le=new WeakMap,de=new WeakMap,he=new WeakMap,fe=new WeakMap,ue=new WeakMap,pe=new WeakMap,me=new WeakMap,we=new WeakMap,ve=new WeakMap;const Ee={build(e){switch(e){case"sharepoint":return new be;case"confluence":return new y;case"googledrive":return new N;case"onedrive":return new se;case"notion_documents":return new q;default:throw new Error(`Unsupported file storage type: ${e}`)}}};exports.FILES_LIST_FIELDS_MINIMAL=a,exports.FileStorageClientFactory=Ee;
{
"name": "@stackone/files-sdk",
"version": "0.31.0",
"version": "0.31.1",
"description": "Files SDK to ease the integration with file storage providers",

@@ -5,0 +5,0 @@ "main": "dist/index.js",