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.27.0
to
0.28.0
+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";function n(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 a(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 r=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:o(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})),o=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",c=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),d=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},l=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var h,f,p,u;const m=()=>new e;class w{constructor({buildHttpClient:e=m}={}){h.set(this,void 0),f.set(this,void 0),p.set(this,void 0),u.set(this,void 0),a(this,h,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=l.parse(e);a(this,p,t,"f"),a(this,u,i??"https://api.stackone.com","f"),a(this,f,n(this,h,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!n(this,p,"f"))throw new Error("Session token is required");return(await n(this,f,"f").get({url:`${n(this,u,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":n(this,p,"f")}})).data.data.map(c)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,p,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,o=await n(this,f,"f").get({url:d(a?`${n(this,u,"f")}/unified/documents/files?page_size=25`:`${n(this,u,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,p,"f")}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:r(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}h=new WeakMap,f=new WeakMap,p=new WeakMap,u=new WeakMap;const v=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}},k=t.object({appId:t.string(),clientId:t.string(),apiKey:t.string(),origin:t.string(),accessToken:t.string(),folderSelectionEnabled:t.boolean().optional().default(!1)});var g,y,b,E,$,_,M,W;const S="message",I=["picked","cancel","loaded"],C=["picker:oauth:error"];class P{constructor(){g.set(this,void 0),y.set(this,void 0),b.set(this,void 0),E.set(this,void 0),$.set(this,void 0),_.set(this,void 0),M.set(this,void 0),W.set(this,void 0)}initialize(e,t){a(this,g,t,"f");const i=k.parse(e);return a(this,y,i.clientId,"f"),a(this,b,i.apiKey,"f"),a(this,E,i.appId,"f"),a(this,_,i.origin,"f"),a(this,M,i.accessToken,"f"),a(this,W,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(S,this.messageListener.bind(this)),a(this,g,void 0,"f"),a(this,y,void 0,"f"),a(this,b,void 0,"f"),a(this,E,void 0,"f"),a(this,M,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),n(this,$,"f")){const e=document.getElementById(n(this,$,"f"));a(this,$,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="${n(this,y,"f")}" app-id="${n(this,E,"f")}" developer-key="${n(this,b,"f")}" origin="${n(this,_,"f")}" oauth-token="${n(this,M,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${n(this,W,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(S,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const r=document.getElementsByTagName("drive-picker");if(r&&Array.from(r).forEach(e=>e.remove()),n(this,$,"f")){const e=document.getElementById(n(this,$,"f"));a(this,$,null,"f"),e&&e.remove()}n(this,g,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const o=e.document.getElementById(t);o&&(o.style.display="none",o?.parentElement?.parentElement&&(o.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(I.includes(i?.action)||C.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":a(this,$,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(v).filter(Boolean);await(n(this,g,"f")?.onSelect?.(e)),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){n(this,g,"f")?.onCancel?.(),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}}g=new WeakMap,y=new WeakMap,b=new WeakMap,E=new WeakMap,$=new WeakMap,_=new WeakMap,M=new WeakMap,W=new WeakMap;const A=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:F(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})),F=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",N=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),T=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},z=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var L,O,U,x;const j=()=>new e;class B{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),O.set(this,void 0),U.set(this,void 0),x.set(this,void 0),a(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=z.parse(e);a(this,U,t,"f"),a(this,x,i??"https://api.stackone.com","f"),a(this,O,n(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(){try{if(!n(this,U,"f"))throw new Error("Session token is required");return(await n(this,O,"f").get({url:`${n(this,x,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":n(this,U,"f")}})).data.data.map(N)}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,U,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,r=await n(this,O,"f").get({url:T(a?`${n(this,x,"f")}/unified/documents/files?page_size=25`:`${n(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...a?{}:{Prefer:"heartbeat"}}}),o=Array.isArray(r.data.data)?r.data.data:[];return{files:A(o),next:r.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace & Databases"}}L=new WeakMap,O=new WeakMap,U=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},D=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}},R=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),a(this,H,e,"f"),a(this,J,t,"f"),a(this,V,i(),"f")}async initialize(e,t){try{const i=R.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,G,n(this,H,"f").call(this,s),"f"),await n(this,G,"f").initialize(),a(this,K,n(this,J,"f").call(this),"f"),a(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,G,"f").loginPopup({scopes:i});if(n(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(!n(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(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){n(this,Y,"f")&&(n(this,Y,"f").remove(),a(this,Y,null,"f")),n(this,Q,"f")&&(n(this,Q,"f").close(),a(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),a(this,X,void 0,"f"),a(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,Y,"f"))return;const r={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,Y,q(c),"f");const d=n(this,Y,"f").contentWindow;if(!d)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(n(this,Y,"f")?.contentWindow&&e.source===n(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,V,"f")&&(a(this,Q,e.ports[0],"f"),n(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),n(this,Q,"f").start(),n(this,Q,"f").postMessage({type:"activate"}))}}),a(this,X,await this.getAccessToken([`${e}/.default`]),"f");const l=new URL("_layouts/15/FilePicker.aspx",e);l.search=o.toString();const h=d.document.createElement("form");h.setAttribute("action",l.toString()),h.setAttribute("method","POST");const f=d.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,X,"f")),h.appendChild(f),d.document.body.appendChild(h),n(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,X,"f")}});break;case"close":n(this,Z,"f")?.onCancel?.(),n(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,Q,"f")&&n(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(D).filter(Boolean);n(this,Z,"f")?.onSelect?.(i),n(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}),ae=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}},re=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,de,le,he,fe,pe,ue,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),de.set(this,void 0),le.set(this,void 0),he.set(this,void 0),fe.set(this,null),pe.set(this,void 0),ue.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),a(this,oe,e,"f"),a(this,ce,t,"f"),a(this,he,i(),"f")}async initialize(e,t){try{const i=re.parse(e);a(this,pe,i.accessToken,"f"),a(this,ue,i.secondaryAccessToken,"f"),a(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,de,n(this,oe,"f").call(this,s),"f"),await n(this,de,"f").initialize(),a(this,le,n(this,ce,"f").call(this),"f"),a(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await n(this,le,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${n(this,pe,"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){n(this,me,"f")&&(n(this,me,"f").remove(),a(this,me,null,"f")),n(this,fe,"f")&&(n(this,fe,"f").close(),a(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),a(this,pe,void 0,"f"),a(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,me,"f"))return;const r={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,ve,"f")?"all":"files",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,me,q(c),"f");const d=n(this,me,"f").contentWindow;if(!d)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(n(this,me,"f")?.contentWindow&&e.source===n(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,he,"f")&&(a(this,fe,e.ports[0],"f"),n(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),n(this,fe,"f").start(),n(this,fe,"f").postMessage({type:"activate"}))}});const l=`${e}_layouts/15/FilePicker.aspx?${o.toString()}`,h=d.document.createElement("form");h.setAttribute("action",l),h.setAttribute("method","POST");const f=d.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,ue,"f")??""),h.appendChild(f),d.document.body.appendChild(h),n(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,ue,"f")}});break;case"close":n(this,we,"f")?.onCancel?.(),n(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,fe,"f")&&n(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ae).filter(Boolean);n(this,we,"f")?.onSelect?.(i),n(this,we,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}oe=new WeakMap,ce=new WeakMap,de=new WeakMap,le=new WeakMap,he=new WeakMap,fe=new WeakMap,pe=new WeakMap,ue=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 w;case"googledrive":return new P;case"onedrive":return new se;case"notion_documents":return new B;default:throw new Error(`Unsupported file storage type: ${e}`)}}};export{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";function n(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 a(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 r=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:o(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})),o=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",c=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),d=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},l=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var h,f,p,u;const m=()=>new e;class w{constructor({buildHttpClient:e=m}={}){h.set(this,void 0),f.set(this,void 0),p.set(this,void 0),u.set(this,void 0),a(this,h,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=l.parse(e);a(this,p,t,"f"),a(this,u,i??"https://api.stackone.com","f"),a(this,f,n(this,h,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!n(this,p,"f"))throw new Error("Session token is required");return(await n(this,f,"f").get({url:`${n(this,u,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":n(this,p,"f")}})).data.data.map(c)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,p,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,o=await n(this,f,"f").get({url:d(a?`${n(this,u,"f")}/unified/documents/files?page_size=25`:`${n(this,u,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,p,"f")}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:r(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}h=new WeakMap,f=new WeakMap,p=new WeakMap,u=new WeakMap;const v=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}},k=t.object({appId:t.string(),clientId:t.string(),apiKey:t.string(),origin:t.string(),accessToken:t.string(),folderSelectionEnabled:t.boolean().optional().default(!1)});var g,y,b,E,$,_,M,W;const S="message",I=["picked","cancel","loaded"],A=["picker:oauth:error"];class C{constructor(){g.set(this,void 0),y.set(this,void 0),b.set(this,void 0),E.set(this,void 0),$.set(this,void 0),_.set(this,void 0),M.set(this,void 0),W.set(this,void 0)}initialize(e,t){a(this,g,t,"f");const i=k.parse(e);return a(this,y,i.clientId,"f"),a(this,b,i.apiKey,"f"),a(this,E,i.appId,"f"),a(this,_,i.origin,"f"),a(this,M,i.accessToken,"f"),a(this,W,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(S,this.messageListener.bind(this)),a(this,g,void 0,"f"),a(this,y,void 0,"f"),a(this,b,void 0,"f"),a(this,E,void 0,"f"),a(this,M,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),n(this,$,"f")){const e=document.getElementById(n(this,$,"f"));a(this,$,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="${n(this,y,"f")}" app-id="${n(this,E,"f")}" developer-key="${n(this,b,"f")}" origin="${n(this,_,"f")}" oauth-token="${n(this,M,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${n(this,W,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(S,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const r=document.getElementsByTagName("drive-picker");if(r&&Array.from(r).forEach(e=>e.remove()),n(this,$,"f")){const e=document.getElementById(n(this,$,"f"));a(this,$,null,"f"),e&&e.remove()}n(this,g,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const o=e.document.getElementById(t);o&&(o.style.display="none",o?.parentElement?.parentElement&&(o.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(I.includes(i?.action)||A.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":a(this,$,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(v).filter(Boolean);await(n(this,g,"f")?.onSelect?.(e)),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){n(this,g,"f")?.onCancel?.(),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}}g=new WeakMap,y=new WeakMap,b=new WeakMap,E=new WeakMap,$=new WeakMap,_=new WeakMap,M=new WeakMap,W=new WeakMap;const P=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:F(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})),F=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",N=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),T=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},z=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var L,O,U,x;const j=()=>new e;class B{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),O.set(this,void 0),U.set(this,void 0),x.set(this,void 0),a(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=z.parse(e);a(this,U,t,"f"),a(this,x,i??"https://api.stackone.com","f"),a(this,O,n(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(e){try{if(!n(this,U,"f"))throw new Error("Session token is required");const t=`${n(this,x,"f")}/unified/documents/drives?page_size=25`,i=e?`${t}&next=${e}`:t,s=await n(this,O,"f").get({url:i,headers:{"x-stackone-api-session-token":n(this,U,"f"),Prefer:"heartbeat"}});return{sites:(Array.isArray(s.data.data)?s.data.data:[]).map(N),next:s.data.next}}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,U,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,r=await n(this,O,"f").get({url:T(a?`${n(this,x,"f")}/unified/documents/files?page_size=25`:`${n(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...a?{}:{Prefer:"heartbeat"}}}),o=Array.isArray(r.data.data)?r.data.data:[];return{files:P(o),next:r.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace & Databases"}}L=new WeakMap,O=new WeakMap,U=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},D=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}},R=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),a(this,H,e,"f"),a(this,J,t,"f"),a(this,V,i(),"f")}async initialize(e,t){try{const i=R.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,G,n(this,H,"f").call(this,s),"f"),await n(this,G,"f").initialize(),a(this,K,n(this,J,"f").call(this),"f"),a(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,G,"f").loginPopup({scopes:i});if(n(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(!n(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(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){n(this,Y,"f")&&(n(this,Y,"f").remove(),a(this,Y,null,"f")),n(this,Q,"f")&&(n(this,Q,"f").close(),a(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),a(this,X,void 0,"f"),a(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,Y,"f"))return;const r={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,Y,q(c),"f");const d=n(this,Y,"f").contentWindow;if(!d)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(n(this,Y,"f")?.contentWindow&&e.source===n(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,V,"f")&&(a(this,Q,e.ports[0],"f"),n(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),n(this,Q,"f").start(),n(this,Q,"f").postMessage({type:"activate"}))}}),a(this,X,await this.getAccessToken([`${e}/.default`]),"f");const l=new URL("_layouts/15/FilePicker.aspx",e);l.search=o.toString();const h=d.document.createElement("form");h.setAttribute("action",l.toString()),h.setAttribute("method","POST");const f=d.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,X,"f")),h.appendChild(f),d.document.body.appendChild(h),n(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,X,"f")}});break;case"close":n(this,Z,"f")?.onCancel?.(),n(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,Q,"f")&&n(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(D).filter(Boolean);n(this,Z,"f")?.onSelect?.(i),n(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}),ae=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}},re=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,de,le,he,fe,pe,ue,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),de.set(this,void 0),le.set(this,void 0),he.set(this,void 0),fe.set(this,null),pe.set(this,void 0),ue.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),a(this,oe,e,"f"),a(this,ce,t,"f"),a(this,he,i(),"f")}async initialize(e,t){try{const i=re.parse(e);a(this,pe,i.accessToken,"f"),a(this,ue,i.secondaryAccessToken,"f"),a(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,de,n(this,oe,"f").call(this,s),"f"),await n(this,de,"f").initialize(),a(this,le,n(this,ce,"f").call(this),"f"),a(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await n(this,le,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${n(this,pe,"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){n(this,me,"f")&&(n(this,me,"f").remove(),a(this,me,null,"f")),n(this,fe,"f")&&(n(this,fe,"f").close(),a(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),a(this,pe,void 0,"f"),a(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,me,"f"))return;const r={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,ve,"f")?"all":"files",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,me,q(c),"f");const d=n(this,me,"f").contentWindow;if(!d)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(n(this,me,"f")?.contentWindow&&e.source===n(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,he,"f")&&(a(this,fe,e.ports[0],"f"),n(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),n(this,fe,"f").start(),n(this,fe,"f").postMessage({type:"activate"}))}});const l=`${e}_layouts/15/FilePicker.aspx?${o.toString()}`,h=d.document.createElement("form");h.setAttribute("action",l),h.setAttribute("method","POST");const f=d.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,ue,"f")??""),h.appendChild(f),d.document.body.appendChild(h),n(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,ue,"f")}});break;case"close":n(this,we,"f")?.onCancel?.(),n(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,fe,"f")&&n(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ae).filter(Boolean);n(this,we,"f")?.onSelect?.(i),n(this,we,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}oe=new WeakMap,ce=new WeakMap,de=new WeakMap,le=new WeakMap,he=new WeakMap,fe=new WeakMap,pe=new WeakMap,ue=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 w;case"googledrive":return new C;case"onedrive":return new se;case"notion_documents":return new B;default:throw new Error(`Unsupported file storage type: ${e}`)}}};export{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");function n(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 a(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 r=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:o(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})),o=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",c=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),l=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},d=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var h,f,p,u;const m=()=>new e.HttpClient;class w{constructor({buildHttpClient:e=m}={}){h.set(this,void 0),f.set(this,void 0),p.set(this,void 0),u.set(this,void 0),a(this,h,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=d.parse(e);a(this,p,t,"f"),a(this,u,i??"https://api.stackone.com","f"),a(this,f,n(this,h,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!n(this,p,"f"))throw new Error("Session token is required");return(await n(this,f,"f").get({url:`${n(this,u,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":n(this,p,"f")}})).data.data.map(c)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,p,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,o=await n(this,f,"f").get({url:l(a?`${n(this,u,"f")}/unified/documents/files?page_size=25`:`${n(this,u,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,p,"f")}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:r(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}h=new WeakMap,f=new WeakMap,p=new WeakMap,u=new WeakMap;const v=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}},k=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 g,y,b,E,z,$,_,C;const M="message",W=["picked","cancel","loaded"],S=["picker:oauth:error"];class I{constructor(){g.set(this,void 0),y.set(this,void 0),b.set(this,void 0),E.set(this,void 0),z.set(this,void 0),$.set(this,void 0),_.set(this,void 0),C.set(this,void 0)}initialize(e,t){a(this,g,t,"f");const i=k.parse(e);return a(this,y,i.clientId,"f"),a(this,b,i.apiKey,"f"),a(this,E,i.appId,"f"),a(this,$,i.origin,"f"),a(this,_,i.accessToken,"f"),a(this,C,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(M,this.messageListener.bind(this)),a(this,g,void 0,"f"),a(this,y,void 0,"f"),a(this,b,void 0,"f"),a(this,E,void 0,"f"),a(this,_,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),n(this,z,"f")){const e=document.getElementById(n(this,z,"f"));a(this,z,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="${n(this,y,"f")}" app-id="${n(this,E,"f")}" developer-key="${n(this,b,"f")}" origin="${n(this,$,"f")}" oauth-token="${n(this,_,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${n(this,C,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(M,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const r=document.getElementsByTagName("drive-picker");if(r&&Array.from(r).forEach(e=>e.remove()),n(this,z,"f")){const e=document.getElementById(n(this,z,"f"));a(this,z,null,"f"),e&&e.remove()}n(this,g,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const o=e.document.getElementById(t);o&&(o.style.display="none",o?.parentElement?.parentElement&&(o.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(W.includes(i?.action)||S.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":a(this,z,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(v).filter(Boolean);await(n(this,g,"f")?.onSelect?.(e)),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){n(this,g,"f")?.onCancel?.(),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}}g=new WeakMap,y=new WeakMap,b=new WeakMap,E=new WeakMap,z=new WeakMap,$=new WeakMap,_=new WeakMap,C=new WeakMap;const P=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:A(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})),A=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",F=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),N=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},T=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var L,O,U,x;const j=()=>new e.HttpClient;class q{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),O.set(this,void 0),U.set(this,void 0),x.set(this,void 0),a(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=T.parse(e);a(this,U,t,"f"),a(this,x,i??"https://api.stackone.com","f"),a(this,O,n(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(){try{if(!n(this,U,"f"))throw new Error("Session token is required");return(await n(this,O,"f").get({url:`${n(this,x,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":n(this,U,"f")}})).data.data.map(F)}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,U,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,r=await n(this,O,"f").get({url:N(a?`${n(this,x,"f")}/unified/documents/files?page_size=25`:`${n(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...a?{}:{Prefer:"heartbeat"}}}),o=Array.isArray(r.data.data)?r.data.data:[];return{files:P(o),next:r.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace & Databases"}}L=new WeakMap,O=new WeakMap,U=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),a(this,R,e,"f"),a(this,J,t,"f"),a(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}`}};a(this,G,n(this,R,"f").call(this,s),"f"),await n(this,G,"f").initialize(),a(this,K,n(this,J,"f").call(this),"f"),a(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,G,"f").loginPopup({scopes:i});if(n(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(!n(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(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){n(this,Y,"f")&&(n(this,Y,"f").remove(),a(this,Y,null,"f")),n(this,Q,"f")&&(n(this,Q,"f").close(),a(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),a(this,X,void 0,"f"),a(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,Y,"f"))return;const r={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,Y,B(c),"f");const l=n(this,Y,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(n(this,Y,"f")?.contentWindow&&e.source===n(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,V,"f")&&(a(this,Q,e.ports[0],"f"),n(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),n(this,Q,"f").start(),n(this,Q,"f").postMessage({type:"activate"}))}}),a(this,X,await this.getAccessToken([`${e}/.default`]),"f");const d=new URL("_layouts/15/FilePicker.aspx",e);d.search=o.toString();const h=l.document.createElement("form");h.setAttribute("action",d.toString()),h.setAttribute("method","POST");const f=l.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,X,"f")),h.appendChild(f),l.document.body.appendChild(h),n(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,X,"f")}});break;case"close":n(this,Z,"f")?.onCancel?.(),n(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,Q,"f")&&n(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(H).filter(Boolean);n(this,Z,"f")?.onSelect?.(i),n(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}),ae=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}},re=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,pe,ue,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),pe.set(this,void 0),ue.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),a(this,oe,e,"f"),a(this,ce,t,"f"),a(this,he,i(),"f")}async initialize(e,t){try{const i=re.parse(e);a(this,pe,i.accessToken,"f"),a(this,ue,i.secondaryAccessToken,"f"),a(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,le,n(this,oe,"f").call(this,s),"f"),await n(this,le,"f").initialize(),a(this,de,n(this,ce,"f").call(this),"f"),a(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await n(this,de,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${n(this,pe,"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){n(this,me,"f")&&(n(this,me,"f").remove(),a(this,me,null,"f")),n(this,fe,"f")&&(n(this,fe,"f").close(),a(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),a(this,pe,void 0,"f"),a(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,me,"f"))return;const r={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,ve,"f")?"all":"files",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,me,B(c),"f");const l=n(this,me,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(n(this,me,"f")?.contentWindow&&e.source===n(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,he,"f")&&(a(this,fe,e.ports[0],"f"),n(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),n(this,fe,"f").start(),n(this,fe,"f").postMessage({type:"activate"}))}});const d=`${e}_layouts/15/FilePicker.aspx?${o.toString()}`,h=l.document.createElement("form");h.setAttribute("action",d),h.setAttribute("method","POST");const f=l.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,ue,"f")??""),h.appendChild(f),l.document.body.appendChild(h),n(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,ue,"f")}});break;case"close":n(this,we,"f")?.onCancel?.(),n(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,fe,"f")&&n(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ae).filter(Boolean);n(this,we,"f")?.onSelect?.(i),n(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,pe=new WeakMap,ue=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 w;case"googledrive":return new I;case"onedrive":return new se;case"notion_documents":return new q;default:throw new Error(`Unsupported file storage type: ${e}`)}}};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");function n(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 a(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 r=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:o(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})),o=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",c=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),l=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},d=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var h,f,p,u;const m=()=>new e.HttpClient;class w{constructor({buildHttpClient:e=m}={}){h.set(this,void 0),f.set(this,void 0),p.set(this,void 0),u.set(this,void 0),a(this,h,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=d.parse(e);a(this,p,t,"f"),a(this,u,i??"https://api.stackone.com","f"),a(this,f,n(this,h,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Confluence file storage client: ${e.message}`)}}async listSites(){try{if(!n(this,p,"f"))throw new Error("Session token is required");return(await n(this,f,"f").get({url:`${n(this,u,"f")}/unified/documents/drives?page_size=99999`,headers:{"x-stackone-api-session-token":n(this,p,"f")}})).data.data.map(c)}catch(e){throw new Error(`Failed to list Confluence sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,p,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,o=await n(this,f,"f").get({url:l(a?`${n(this,u,"f")}/unified/documents/files?page_size=25`:`${n(this,u,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,p,"f")}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:r(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Confluence files: ${e.message}`)}}getSitesNaming(){return"Spaces"}}h=new WeakMap,f=new WeakMap,p=new WeakMap,u=new WeakMap;const v=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}},k=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 g,y,b,E,$,z,_,C;const M="message",W=["picked","cancel","loaded"],S=["picker:oauth:error"];class I{constructor(){g.set(this,void 0),y.set(this,void 0),b.set(this,void 0),E.set(this,void 0),$.set(this,void 0),z.set(this,void 0),_.set(this,void 0),C.set(this,void 0)}initialize(e,t){a(this,g,t,"f");const i=k.parse(e);return a(this,y,i.clientId,"f"),a(this,b,i.apiKey,"f"),a(this,E,i.appId,"f"),a(this,z,i.origin,"f"),a(this,_,i.accessToken,"f"),a(this,C,i.folderSelectionEnabled??!1,"f"),Promise.resolve()}async listSites(){return null}isNative(){return!0}async closeNativeFilePicker(e=window){e.removeEventListener(M,this.messageListener.bind(this)),a(this,g,void 0,"f"),a(this,y,void 0,"f"),a(this,b,void 0,"f"),a(this,E,void 0,"f"),a(this,_,void 0,"f");const t=document.getElementsByTagName("drive-picker");if(t&&Array.from(t).forEach(e=>e.remove()),n(this,$,"f")){const e=document.getElementById(n(this,$,"f"));a(this,$,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="${n(this,y,"f")}" app-id="${n(this,E,"f")}" developer-key="${n(this,b,"f")}" origin="${n(this,z,"f")}" oauth-token="${n(this,_,"f")}">\n <drive-picker-docs-view view-id="DOCS" include-folders="true" select-folder-enabled="${n(this,C,"f")}"></drive-picker-docs-view>\n </drive-picker>`;e.addEventListener(M,this.messageListener.bind(this));const s=document.createElement("div");s.innerHTML=i;const r=document.getElementsByTagName("drive-picker");if(r&&Array.from(r).forEach(e=>e.remove()),n(this,$,"f")){const e=document.getElementById(n(this,$,"f"));a(this,$,null,"f"),e&&e.remove()}n(this,g,"f")?.onOpen?.(),document.body.appendChild(s),s.firstElementChild?.addEventListener("picker:oauth:error",this.messageListener.bind(this));const o=e.document.getElementById(t);o&&(o.style.display="none",o?.parentElement?.parentElement&&(o.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(W.includes(i?.action)||S.includes(e.type))if("picker:oauth:error"!==e.type)switch(i?.action){case"loaded":a(this,$,t?.f,"f");break;case"cancel":this.handleOnCancel();break;case"picked":const e=i.docs.map(v).filter(Boolean);await(n(this,g,"f")?.onSelect?.(e)),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){n(this,g,"f")?.onCancel?.(),n(this,g,"f")?.onClose?.(),this.closeNativeFilePicker()}}g=new WeakMap,y=new WeakMap,b=new WeakMap,E=new WeakMap,$=new WeakMap,z=new WeakMap,_=new WeakMap,C=new WeakMap;const A=e=>e.map(e=>({id:e.id??"",name:e.name??"",type:P(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})),P=(e,t)=>"true"===e?"folder":"true"===t?"file":"folder",F=e=>({id:e.id||"",displayName:e.name||"",url:e.url||"",createdAt:e.created_at||""}),N=(e,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`}),i?`${a}&filter[folder_id]=${i}`:t?n?`${a}&filter[drive_id]=${t}`:`${a}&filter[drive_id]=${t}&filter[is_root]=true`:e},T=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var L,O,U,x;const j=()=>new e.HttpClient;class q{constructor({buildHttpClient:e=j}={}){L.set(this,void 0),O.set(this,void 0),U.set(this,void 0),x.set(this,void 0),a(this,L,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=T.parse(e);a(this,U,t,"f"),a(this,x,i??"https://api.stackone.com","f"),a(this,O,n(this,L,"f").call(this),"f")}catch(e){throw new Error(`Failed to initialize Notion file storage client: ${e.message}`)}}async listSites(e){try{if(!n(this,U,"f"))throw new Error("Session token is required");const t=`${n(this,x,"f")}/unified/documents/drives?page_size=25`,i=e?`${t}&next=${e}`:t,s=await n(this,O,"f").get({url:i,headers:{"x-stackone-api-session-token":n(this,U,"f"),Prefer:"heartbeat"}});return{sites:(Array.isArray(s.data.data)?s.data.data:[]).map(F),next:s.data.next}}catch(e){throw new Error(`Failed to list Notion sites: ${e.message}`)}}isNative(){return!1}async listFiles(e,t,i,s){try{if(!n(this,U,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,r=await n(this,O,"f").get({url:N(a?`${n(this,x,"f")}/unified/documents/files?page_size=25`:`${n(this,x,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...a?{}:{Prefer:"heartbeat"}}}),o=Array.isArray(r.data.data)?r.data.data:[];return{files:A(o),next:r.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace & Databases"}}L=new WeakMap,O=new WeakMap,U=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),a(this,R,e,"f"),a(this,J,t,"f"),a(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}`}};a(this,G,n(this,R,"f").call(this,s),"f"),await n(this,G,"f").initialize(),a(this,K,n(this,J,"f").call(this),"f"),a(this,Z,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,G,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,G,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,G,"f").loginPopup({scopes:i});if(n(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(!n(this,G,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(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){n(this,Y,"f")&&(n(this,Y,"f").remove(),a(this,Y,null,"f")),n(this,Q,"f")&&(n(this,Q,"f").close(),a(this,Q,null,"f")),e.removeEventListener(ee,this.messageListener.bind(this)),a(this,X,void 0,"f"),a(this,Z,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,Y,"f"))return;const r={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,V,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,Y,B(c),"f");const l=n(this,Y,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ee,e=>{if(n(this,Y,"f")?.contentWindow&&e.source===n(this,Y,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,V,"f")&&(a(this,Q,e.ports[0],"f"),n(this,Q,"f").addEventListener(ee,this.messageListener.bind(this)),n(this,Q,"f").start(),n(this,Q,"f").postMessage({type:"activate"}))}}),a(this,X,await this.getAccessToken([`${e}/.default`]),"f");const d=new URL("_layouts/15/FilePicker.aspx",e);d.search=o.toString();const h=l.document.createElement("form");h.setAttribute("action",d.toString()),h.setAttribute("method","POST");const f=l.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,X,"f")),h.appendChild(f),l.document.body.appendChild(h),n(this,Z,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,Q,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,X,"f")}});break;case"close":n(this,Z,"f")?.onCancel?.(),n(this,Z,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,Q,"f")&&n(this,Q,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(H).filter(Boolean);n(this,Z,"f")?.onSelect?.(i),n(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}),ae=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}},re=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,pe,ue,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),pe.set(this,void 0),ue.set(this,void 0),me.set(this,null),we.set(this,void 0),ve.set(this,void 0),a(this,oe,e,"f"),a(this,ce,t,"f"),a(this,he,i(),"f")}async initialize(e,t){try{const i=re.parse(e);a(this,pe,i.accessToken,"f"),a(this,ue,i.secondaryAccessToken,"f"),a(this,ve,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,le,n(this,oe,"f").call(this,s),"f"),await n(this,le,"f").initialize(),a(this,de,n(this,ce,"f").call(this),"f"),a(this,we,t,"f")}catch(e){throw new Error(`Failed to initialize SharePoint file storage client: ${e.message}`)}}async listSites(){try{const e=await n(this,de,"f").get({url:"https://graph.microsoft.com/v1.0/sites?search=*",headers:{authorization:`Bearer ${n(this,pe,"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){n(this,me,"f")&&(n(this,me,"f").remove(),a(this,me,null,"f")),n(this,fe,"f")&&(n(this,fe,"f").close(),a(this,fe,null,"f")),e.removeEventListener(ke,this.messageListener.bind(this)),a(this,pe,void 0,"f"),a(this,we,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,me,"f"))return;const r={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,he,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,ve,"f")?"all":"files",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(r)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");a(this,me,B(c),"f");const l=n(this,me,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ke,e=>{if(n(this,me,"f")?.contentWindow&&e.source===n(this,me,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,he,"f")&&(a(this,fe,e.ports[0],"f"),n(this,fe,"f").addEventListener(ke,this.messageListener.bind(this)),n(this,fe,"f").start(),n(this,fe,"f").postMessage({type:"activate"}))}});const d=`${e}_layouts/15/FilePicker.aspx?${o.toString()}`,h=l.document.createElement("form");h.setAttribute("action",d),h.setAttribute("method","POST");const f=l.document.createElement("input");f.setAttribute("type","hidden"),f.setAttribute("name","access_token"),f.setAttribute("value",n(this,ue,"f")??""),h.appendChild(f),l.document.body.appendChild(h),n(this,we,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,fe,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,ue,"f")}});break;case"close":n(this,we,"f")?.onCancel?.(),n(this,we,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,fe,"f")&&n(this,fe,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ae).filter(Boolean);n(this,we,"f")?.onSelect?.(i),n(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,pe=new WeakMap,ue=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 w;case"googledrive":return new I;case"onedrive":return new se;case"notion_documents":return new q;default:throw new Error(`Unsupported file storage type: ${e}`)}}};exports.FileStorageClientFactory=Ee;

@@ -10,3 +10,6 @@ import { IHttpClient } from '@stackone/transport';

initialize(config: unknown): Promise<void>;
listSites(): Promise<StorageSite[]>;
listSites(next?: string | null): Promise<{
sites: StorageSite[];
next: string | null;
}>;
isNative(): boolean;

@@ -13,0 +16,0 @@ listFiles(siteId?: string, folderId?: string, next?: string | null, search?: Record<string, string>): Promise<{

@@ -5,3 +5,6 @@ export interface IFileStorageClient {

getAccessToken?(scopes?: string[]): Promise<string>;
listSites?(): Promise<StorageSite[] | null>;
listSites?(next?: string | null): Promise<StorageSite[] | {
sites: StorageSite[];
next: string | null;
} | null>;
listFiles?(siteId?: string, folderId?: string, next?: string | null, search?: Record<string, string>): Promise<{

@@ -8,0 +11,0 @@ files: StorageFile[];

{
"name": "@stackone/files-sdk",
"version": "0.27.0",
"version": "0.28.0",
"description": "Files SDK to ease the integration with file storage providers",

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