@stackone/files-sdk
Advanced tools
@@ -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 r(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 a=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 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},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),r(this,h,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=l.parse(e);r(this,p,t,"f"),r(this,u,i??"https://api.stackone.com","f"),r(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 r=s&&Object.keys(s).length>0,o=await n(this,f,"f").get({url:d(r?`${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:a(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){r(this,g,t,"f");const i=k.parse(e);return r(this,y,i.clientId,"f"),r(this,b,i.apiKey,"f"),r(this,E,i.appId,"f"),r(this,_,i.origin,"f"),r(this,M,i.accessToken,"f"),r(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)),r(this,g,void 0,"f"),r(this,y,void 0,"f"),r(this,b,void 0,"f"),r(this,E,void 0,"f"),r(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"));r(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 a=document.getElementsByTagName("drive-picker");if(a&&Array.from(a).forEach(e=>e.remove()),n(this,$,"f")){const e=document.getElementById(n(this,$,"f"));r(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":r(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,t,i,s,n)=>{let r=s?`${e}&next=${s}`:e;return n&&(r+="&proxy[d]=t",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},T=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var z,L,U,O;const x=()=>new e;class j{constructor({buildHttpClient:e=x}={}){z.set(this,void 0),L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),r(this,z,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=T.parse(e);r(this,U,t,"f"),r(this,O,i??"https://api.stackone.com","f"),r(this,L,n(this,z,"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");const t=await n(this,L,"f").get({url:`${n(this,O,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":n(this,U,"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){try{if(!n(this,U,"f"))throw new Error("Session token is required");const r=s&&Object.keys(s).length>0,a=Boolean(t);if(!r&&!a)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};const o=await n(this,L,"f").get({url:N(r?`${n(this,O,"f")}/unified/documents/files?page_size=25`:`${n(this,O,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...r?{}:{Prefer:"heartbeat"}}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:P(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace"}}z=new WeakMap,L=new WeakMap,U=new WeakMap,O=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},q=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 R,H,J,G,K,V,Q,X,Y;const Z="message",ee=e=>new i(e),te=()=>new e;class ie{constructor({buildClientApplication:e=ee,buildHttpClient:t=te,channelIdGenerator:i=s}={}){R.set(this,void 0),H.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,null),Q.set(this,void 0),X.set(this,null),Y.set(this,void 0),r(this,R,e,"f"),r(this,H,t,"f"),r(this,K,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}`}};r(this,J,n(this,R,"f").call(this,s),"f"),await n(this,J,"f").initialize(),r(this,G,n(this,H,"f").call(this),"f"),r(this,Y,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,J,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,J,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,J,"f").loginPopup({scopes:i});if(n(this,J,"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,J,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(this,J,"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,X,"f")&&(n(this,X,"f").remove(),r(this,X,null,"f")),n(this,V,"f")&&(n(this,V,"f").close(),r(this,V,null,"f")),e.removeEventListener(Z,this.messageListener.bind(this)),r(this,Q,void 0,"f"),r(this,Y,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,X,"f"))return;const a={sdk:"8.0",entry:{oneDrive:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,K,"f")},selection:{mode:"multiple",enablePersistence:!0},typesAndSources:{mode:"all",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(a)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");r(this,X,B(c),"f");const d=n(this,X,"f").contentWindow;if(!d)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(Z,e=>{if(n(this,X,"f")?.contentWindow&&e.source===n(this,X,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,K,"f")&&(r(this,V,e.ports[0],"f"),n(this,V,"f").addEventListener(Z,this.messageListener.bind(this)),n(this,V,"f").start(),n(this,V,"f").postMessage({type:"activate"}))}}),r(this,Q,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,Q,"f")),h.appendChild(f),d.document.body.appendChild(h),n(this,Y,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,V,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,Q,"f")}});break;case"close":n(this,Y,"f")?.onCancel?.(),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,V,"f")&&n(this,V,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(q).filter(Boolean);n(this,Y,"f")?.onSelect?.(i),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker()}}}R=new WeakMap,H=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap;const se=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),ne=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 ae,oe,ce,de,le,he,fe,pe,ue,me,we;const ve="message",ke=e=>new i(e),ge=()=>new e;class ye{constructor({buildClientApplication:e=ke,buildHttpClient:t=ge,channelIdGenerator:i=s}={}){ae.set(this,void 0),oe.set(this,void 0),ce.set(this,void 0),de.set(this,void 0),le.set(this,void 0),he.set(this,null),fe.set(this,void 0),pe.set(this,void 0),ue.set(this,null),me.set(this,void 0),we.set(this,void 0),r(this,ae,e,"f"),r(this,oe,t,"f"),r(this,le,i(),"f")}async initialize(e,t){try{const i=re.parse(e);r(this,fe,i.accessToken,"f"),r(this,pe,i.secondaryAccessToken,"f"),r(this,we,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};r(this,ce,n(this,ae,"f").call(this,s),"f"),await n(this,ce,"f").initialize(),r(this,de,n(this,oe,"f").call(this),"f"),r(this,me,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,fe,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(se)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){n(this,ue,"f")&&(n(this,ue,"f").remove(),r(this,ue,null,"f")),n(this,he,"f")&&(n(this,he,"f").close(),r(this,he,null,"f")),e.removeEventListener(ve,this.messageListener.bind(this)),r(this,fe,void 0,"f"),r(this,me,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,ue,"f"))return;const a={sdk:"8.0",entry:{sharePoint:{byPath:{web:t}},site:{}},search:{enabled:!0},authentication:{},messaging:{origin:window.location.origin,channelId:n(this,le,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,we,"f")?"all":"files",pivots:{}}},o=new URLSearchParams({filePicker:JSON.stringify(a)}),c=i.document.getElementById(s);if(!c)throw new Error("Failed to open native file picker: root element not found");r(this,ue,B(c),"f");const d=n(this,ue,"f").contentWindow;if(!d)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ve,e=>{if(n(this,ue,"f")?.contentWindow&&e.source===n(this,ue,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,le,"f")&&(r(this,he,e.ports[0],"f"),n(this,he,"f").addEventListener(ve,this.messageListener.bind(this)),n(this,he,"f").start(),n(this,he,"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,pe,"f")??""),h.appendChild(f),d.document.body.appendChild(h),n(this,me,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,he,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,pe,"f")}});break;case"close":n(this,me,"f")?.onCancel?.(),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,he,"f")&&n(this,he,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ne).filter(Boolean);n(this,me,"f")?.onSelect?.(i),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}ae=new WeakMap,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;const be={build(e){switch(e){case"sharepoint":return new ye;case"confluence":return new w;case"googledrive":return new C;case"onedrive":return new ie;case"notion_documents":return new j;default:throw new Error(`Unsupported file storage type: ${e}`)}}};export{be 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||""}),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.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}=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.object({appId:t.string(),clientId:t.string(),apiKey:t.string(),origin:t.string(),accessToken:t.string(),folderSelectionEnabled:t.boolean().optional().default(!1)});var y,g,b,E,$,_,M,W;const S="message",I=["picked","cancel","loaded"],A=["picker:oauth:error"];class C{constructor(){y.set(this,void 0),g.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,y,t,"f");const i=k.parse(e);return a(this,g,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,y,void 0,"f"),a(this,g,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,g,"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,y,"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,y,"f")?.onSelect?.(e)),n(this,y,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){n(this,y,"f")?.onCancel?.(),n(this,y,"f")?.onClose?.(),this.closeNativeFilePicker()}}y=new WeakMap,g=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||"false"===t&&"false"===e?"file":"folder",N=(e,t,i,s,n)=>{let a=`${e}${e.includes("?")?"&":"?"}fields=id,name,has_children,has_content,size,created_at,remote_drive_id,url`;return s&&(a+=`&next=${s}`),n&&(a+="&proxy[d]=t",Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`})),i?`${a}&filter[folder_id]=${i}`:t&&!n?`${a}&filter[is_root]=true`:a},T=t.object({sessionToken:t.string(),apiUrl:t.string().optional()});var z,L,U,O;const x=()=>new e;class j{constructor({buildHttpClient:e=x}={}){z.set(this,void 0),L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),a(this,z,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=T.parse(e);a(this,U,t,"f"),a(this,O,i??"https://api.stackone.com","f"),a(this,L,n(this,z,"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");const t=await n(this,L,"f").get({url:`${n(this,O,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":n(this,U,"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){try{if(!n(this,U,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,r=Boolean(t);if(!a&&!r)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};const o=await n(this,L,"f").get({url:N(`${n(this,O,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...a?{}:{Prefer:"heartbeat"}}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:P(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace"}}z=new WeakMap,L=new WeakMap,U=new WeakMap,O=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},q=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 R,H,J,G,K,V,Q,X,Y;const Z="message",ee=e=>new i(e),te=()=>new e;class ie{constructor({buildClientApplication:e=ee,buildHttpClient:t=te,channelIdGenerator:i=s}={}){R.set(this,void 0),H.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,null),Q.set(this,void 0),X.set(this,null),Y.set(this,void 0),a(this,R,e,"f"),a(this,H,t,"f"),a(this,K,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,J,n(this,R,"f").call(this,s),"f"),await n(this,J,"f").initialize(),a(this,G,n(this,H,"f").call(this),"f"),a(this,Y,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,J,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,J,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,J,"f").loginPopup({scopes:i});if(n(this,J,"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,J,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(this,J,"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,X,"f")&&(n(this,X,"f").remove(),a(this,X,null,"f")),n(this,V,"f")&&(n(this,V,"f").close(),a(this,V,null,"f")),e.removeEventListener(Z,this.messageListener.bind(this)),a(this,Q,void 0,"f"),a(this,Y,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,X,"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,K,"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,X,B(c),"f");const l=n(this,X,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(Z,e=>{if(n(this,X,"f")?.contentWindow&&e.source===n(this,X,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,K,"f")&&(a(this,V,e.ports[0],"f"),n(this,V,"f").addEventListener(Z,this.messageListener.bind(this)),n(this,V,"f").start(),n(this,V,"f").postMessage({type:"activate"}))}}),a(this,Q,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,Q,"f")),h.appendChild(f),l.document.body.appendChild(h),n(this,Y,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,V,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,Q,"f")}});break;case"close":n(this,Y,"f")?.onCancel?.(),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,V,"f")&&n(this,V,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(q).filter(Boolean);n(this,Y,"f")?.onSelect?.(i),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker()}}}R=new WeakMap,H=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap;const se=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),ne=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 re,oe,ce,le,de,he,fe,pe,ue,me,we;const ve="message",ke=e=>new i(e),ye=()=>new e;class ge{constructor({buildClientApplication:e=ke,buildHttpClient:t=ye,channelIdGenerator:i=s}={}){re.set(this,void 0),oe.set(this,void 0),ce.set(this,void 0),le.set(this,void 0),de.set(this,void 0),he.set(this,null),fe.set(this,void 0),pe.set(this,void 0),ue.set(this,null),me.set(this,void 0),we.set(this,void 0),a(this,re,e,"f"),a(this,oe,t,"f"),a(this,de,i(),"f")}async initialize(e,t){try{const i=ae.parse(e);a(this,fe,i.accessToken,"f"),a(this,pe,i.secondaryAccessToken,"f"),a(this,we,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,ce,n(this,re,"f").call(this,s),"f"),await n(this,ce,"f").initialize(),a(this,le,n(this,oe,"f").call(this),"f"),a(this,me,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,fe,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(se)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){n(this,ue,"f")&&(n(this,ue,"f").remove(),a(this,ue,null,"f")),n(this,he,"f")&&(n(this,he,"f").close(),a(this,he,null,"f")),e.removeEventListener(ve,this.messageListener.bind(this)),a(this,fe,void 0,"f"),a(this,me,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,ue,"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,de,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,we,"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,ue,B(c),"f");const l=n(this,ue,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ve,e=>{if(n(this,ue,"f")?.contentWindow&&e.source===n(this,ue,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,de,"f")&&(a(this,he,e.ports[0],"f"),n(this,he,"f").addEventListener(ve,this.messageListener.bind(this)),n(this,he,"f").start(),n(this,he,"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,pe,"f")??""),h.appendChild(f),l.document.body.appendChild(h),n(this,me,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,he,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,pe,"f")}});break;case"close":n(this,me,"f")?.onCancel?.(),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,he,"f")&&n(this,he,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ne).filter(Boolean);n(this,me,"f")?.onSelect?.(i),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}re=new WeakMap,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;const be={build(e){switch(e){case"sharepoint":return new ge;case"confluence":return new w;case"googledrive":return new C;case"onedrive":return new ie;case"notion_documents":return new j;default:throw new Error(`Unsupported file storage type: ${e}`)}}};export{be as FileStorageClientFactory}; |
+1
-1
@@ -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",S=["picked","cancel","loaded"],W=["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(S.includes(i?.action)||W.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 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,t,i,s,n)=>{let a=s?`${e}&next=${s}`:e;return n&&(a+="&proxy[d]=t",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},N=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var T,L,U,O;const x=()=>new e.HttpClient;class j{constructor({buildHttpClient:e=x}={}){T.set(this,void 0),L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),a(this,T,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=N.parse(e);a(this,U,t,"f"),a(this,O,i??"https://api.stackone.com","f"),a(this,L,n(this,T,"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");const t=await n(this,L,"f").get({url:`${n(this,O,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":n(this,U,"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){try{if(!n(this,U,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,r=Boolean(t);if(!a&&!r)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};const o=await n(this,L,"f").get({url:F(a?`${n(this,O,"f")}/unified/documents/files?page_size=25`:`${n(this,O,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...a?{}:{Prefer:"heartbeat"}}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:A(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace"}}T=new WeakMap,L=new WeakMap,U=new WeakMap,O=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},B=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}},H=t.z.object({tenantId:t.z.string(),clientId:t.z.string(),redirectUri:t.z.string()});var D,R,J,G,K,V,Q,X,Y;const Z="message",ee=e=>new i.PublicClientApplication(e),te=()=>new e.HttpClient;class ie{constructor({buildClientApplication:e=ee,buildHttpClient:t=te,channelIdGenerator:i=s.nanoid}={}){D.set(this,void 0),R.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,null),Q.set(this,void 0),X.set(this,null),Y.set(this,void 0),a(this,D,e,"f"),a(this,R,t,"f"),a(this,K,i(),"f")}async initialize(e,t){try{const i=H.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,J,n(this,D,"f").call(this,s),"f"),await n(this,J,"f").initialize(),a(this,G,n(this,R,"f").call(this),"f"),a(this,Y,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,J,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,J,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,J,"f").loginPopup({scopes:i});if(n(this,J,"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,J,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(this,J,"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,X,"f")&&(n(this,X,"f").remove(),a(this,X,null,"f")),n(this,V,"f")&&(n(this,V,"f").close(),a(this,V,null,"f")),e.removeEventListener(Z,this.messageListener.bind(this)),a(this,Q,void 0,"f"),a(this,Y,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,X,"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,K,"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,X,q(c),"f");const l=n(this,X,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(Z,e=>{if(n(this,X,"f")?.contentWindow&&e.source===n(this,X,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,K,"f")&&(a(this,V,e.ports[0],"f"),n(this,V,"f").addEventListener(Z,this.messageListener.bind(this)),n(this,V,"f").start(),n(this,V,"f").postMessage({type:"activate"}))}}),a(this,Q,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,Q,"f")),h.appendChild(f),l.document.body.appendChild(h),n(this,Y,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,V,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,Q,"f")}});break;case"close":n(this,Y,"f")?.onCancel?.(),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,V,"f")&&n(this,V,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(B).filter(Boolean);n(this,Y,"f")?.onSelect?.(i),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker()}}}D=new WeakMap,R=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap;const se=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),ne=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 re,oe,ce,le,de,he,fe,pe,ue,me,we;const ve="message",ke=e=>new i.PublicClientApplication(e),ge=()=>new e.HttpClient;class ye{constructor({buildClientApplication:e=ke,buildHttpClient:t=ge,channelIdGenerator:i=s.nanoid}={}){re.set(this,void 0),oe.set(this,void 0),ce.set(this,void 0),le.set(this,void 0),de.set(this,void 0),he.set(this,null),fe.set(this,void 0),pe.set(this,void 0),ue.set(this,null),me.set(this,void 0),we.set(this,void 0),a(this,re,e,"f"),a(this,oe,t,"f"),a(this,de,i(),"f")}async initialize(e,t){try{const i=ae.parse(e);a(this,fe,i.accessToken,"f"),a(this,pe,i.secondaryAccessToken,"f"),a(this,we,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,ce,n(this,re,"f").call(this,s),"f"),await n(this,ce,"f").initialize(),a(this,le,n(this,oe,"f").call(this),"f"),a(this,me,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,fe,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(se)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){n(this,ue,"f")&&(n(this,ue,"f").remove(),a(this,ue,null,"f")),n(this,he,"f")&&(n(this,he,"f").close(),a(this,he,null,"f")),e.removeEventListener(ve,this.messageListener.bind(this)),a(this,fe,void 0,"f"),a(this,me,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,ue,"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,de,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,we,"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,ue,q(c),"f");const l=n(this,ue,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ve,e=>{if(n(this,ue,"f")?.contentWindow&&e.source===n(this,ue,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,de,"f")&&(a(this,he,e.ports[0],"f"),n(this,he,"f").addEventListener(ve,this.messageListener.bind(this)),n(this,he,"f").start(),n(this,he,"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,pe,"f")??""),h.appendChild(f),l.document.body.appendChild(h),n(this,me,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,he,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,pe,"f")}});break;case"close":n(this,me,"f")?.onCancel?.(),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,he,"f")&&n(this,he,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ne).filter(Boolean);n(this,me,"f")?.onSelect?.(i),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}re=new WeakMap,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;const be={build(e){switch(e){case"sharepoint":return new ye;case"confluence":return new w;case"googledrive":return new I;case"onedrive":return new ie;case"notion_documents":return new j;default:throw new Error(`Unsupported file storage type: ${e}`)}}};exports.FileStorageClientFactory=be; | ||
| "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 y,g,b,E,z,$,_,C;const M="message",S=["picked","cancel","loaded"],W=["picker:oauth:error"];class I{constructor(){y.set(this,void 0),g.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,y,t,"f");const i=k.parse(e);return a(this,g,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,y,void 0,"f"),a(this,g,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,g,"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,y,"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(S.includes(i?.action)||W.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,y,"f")?.onSelect?.(e)),n(this,y,"f")?.onClose?.(),this.closeNativeFilePicker()}else this.handleOnCancel()}handleOnCancel(){n(this,y,"f")?.onCancel?.(),n(this,y,"f")?.onClose?.(),this.closeNativeFilePicker()}}y=new WeakMap,g=new WeakMap,b=new WeakMap,E=new WeakMap,z=new WeakMap,$=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||"false"===t&&"false"===e?"file":"folder",F=(e,t,i,s,n)=>{let a=`${e}${e.includes("?")?"&":"?"}fields=id,name,has_children,has_content,size,created_at,remote_drive_id,url`;return s&&(a+=`&next=${s}`),n&&(a+="&proxy[d]=t",Object.entries(n).forEach(([e,t])=>{a+=`&filter[${e}]=${t}`})),i?`${a}&filter[folder_id]=${i}`:t&&!n?`${a}&filter[is_root]=true`:a},N=t.z.object({sessionToken:t.z.string(),apiUrl:t.z.string().optional()});var T,L,U,O;const x=()=>new e.HttpClient;class j{constructor({buildHttpClient:e=x}={}){T.set(this,void 0),L.set(this,void 0),U.set(this,void 0),O.set(this,void 0),a(this,T,e,"f")}async initialize(e){try{const{sessionToken:t,apiUrl:i}=N.parse(e);a(this,U,t,"f"),a(this,O,i??"https://api.stackone.com","f"),a(this,L,n(this,T,"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");const t=await n(this,L,"f").get({url:`${n(this,O,"f")}/unified/documents/drives?page_size=1`,headers:{"x-stackone-api-session-token":n(this,U,"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){try{if(!n(this,U,"f"))throw new Error("Session token is required");const a=s&&Object.keys(s).length>0,r=Boolean(t);if(!a&&!r)return{files:[],next:null,emptyStateMessage:"Use search to find folders and files"};const o=await n(this,L,"f").get({url:F(`${n(this,O,"f")}/unified/documents/folders?page_size=25`,e,t,i,s),headers:{"x-stackone-api-session-token":n(this,U,"f"),...a?{}:{Prefer:"heartbeat"}}}),c=Array.isArray(o.data.data)?o.data.data:[];return{files:A(c),next:o.data.next}}catch(e){throw new Error(`Failed to list Notion files: ${e.message}`)}}getSitesNaming(){return"Workspace"}}T=new WeakMap,L=new WeakMap,U=new WeakMap,O=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},B=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}},H=t.z.object({tenantId:t.z.string(),clientId:t.z.string(),redirectUri:t.z.string()});var D,R,J,G,K,V,Q,X,Y;const Z="message",ee=e=>new i.PublicClientApplication(e),te=()=>new e.HttpClient;class ie{constructor({buildClientApplication:e=ee,buildHttpClient:t=te,channelIdGenerator:i=s.nanoid}={}){D.set(this,void 0),R.set(this,void 0),J.set(this,void 0),G.set(this,void 0),K.set(this,void 0),V.set(this,null),Q.set(this,void 0),X.set(this,null),Y.set(this,void 0),a(this,D,e,"f"),a(this,R,t,"f"),a(this,K,i(),"f")}async initialize(e,t){try{const i=H.parse(e),s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,J,n(this,D,"f").call(this,s),"f"),await n(this,J,"f").initialize(),a(this,G,n(this,R,"f").call(this),"f"),a(this,Y,t,"f")}catch(e){throw new Error(`Failed to initialize ONEDRIVE file storage client: ${e.message}`)}}async login(e=[]){try{const t=n(this,J,"f").getActiveAccount(),i=[...e,"https://graph.microsoft.com/.default"];if(t){return(await n(this,J,"f").acquireTokenSilent({scopes:i})).accessToken}{const e=await n(this,J,"f").loginPopup({scopes:i});if(n(this,J,"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,J,"f").getActiveAccount())throw new Error("No active account found. Please call login() first.");try{return(await n(this,J,"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,X,"f")&&(n(this,X,"f").remove(),a(this,X,null,"f")),n(this,V,"f")&&(n(this,V,"f").close(),a(this,V,null,"f")),e.removeEventListener(Z,this.messageListener.bind(this)),a(this,Q,void 0,"f"),a(this,Y,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,X,"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,K,"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,X,q(c),"f");const l=n(this,X,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(Z,e=>{if(n(this,X,"f")?.contentWindow&&e.source===n(this,X,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,K,"f")&&(a(this,V,e.ports[0],"f"),n(this,V,"f").addEventListener(Z,this.messageListener.bind(this)),n(this,V,"f").start(),n(this,V,"f").postMessage({type:"activate"}))}}),a(this,Q,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,Q,"f")),h.appendChild(f),l.document.body.appendChild(h),n(this,Y,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,V,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,Q,"f")}});break;case"close":n(this,Y,"f")?.onCancel?.(),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,V,"f")&&n(this,V,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(B).filter(Boolean);n(this,Y,"f")?.onSelect?.(i),n(this,Y,"f")?.onClose?.(),this.closeNativeFilePicker()}}}D=new WeakMap,R=new WeakMap,J=new WeakMap,G=new WeakMap,K=new WeakMap,V=new WeakMap,Q=new WeakMap,X=new WeakMap,Y=new WeakMap;const se=e=>({id:e.id,displayName:e.displayName,url:e.webUrl,createdAt:e.createdDateTime}),ne=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 re,oe,ce,le,de,he,fe,pe,ue,me,we;const ve="message",ke=e=>new i.PublicClientApplication(e),ye=()=>new e.HttpClient;class ge{constructor({buildClientApplication:e=ke,buildHttpClient:t=ye,channelIdGenerator:i=s.nanoid}={}){re.set(this,void 0),oe.set(this,void 0),ce.set(this,void 0),le.set(this,void 0),de.set(this,void 0),he.set(this,null),fe.set(this,void 0),pe.set(this,void 0),ue.set(this,null),me.set(this,void 0),we.set(this,void 0),a(this,re,e,"f"),a(this,oe,t,"f"),a(this,de,i(),"f")}async initialize(e,t){try{const i=ae.parse(e);a(this,fe,i.accessToken,"f"),a(this,pe,i.secondaryAccessToken,"f"),a(this,we,i.folderSelectionEnabled,"f");const s={auth:{clientId:i.clientId,redirectUri:i.redirectUri,authority:`https://login.microsoftonline.com/${i.tenantId}`}};a(this,ce,n(this,re,"f").call(this,s),"f"),await n(this,ce,"f").initialize(),a(this,le,n(this,oe,"f").call(this),"f"),a(this,me,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,fe,"f")}`,"content-type":"application/json"}});return e?.data?.value?.map(se)||[]}catch(e){throw new Error(`Failed to list SharePoint sites: ${e.message}`)}}isNative(){return!0}async closeNativeFilePicker(e=window){n(this,ue,"f")&&(n(this,ue,"f").remove(),a(this,ue,null,"f")),n(this,he,"f")&&(n(this,he,"f").close(),a(this,he,null,"f")),e.removeEventListener(ve,this.messageListener.bind(this)),a(this,fe,void 0,"f"),a(this,me,void 0,"f")}async openNativeFilePicker({baseUrl:e,siteIdentifier:t="",win:i=window,rootElementId:s="site-picker-container"}){if(n(this,ue,"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,de,"f")},selection:{mode:"multiple"},typesAndSources:{mode:n(this,we,"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,ue,q(c),"f");const l=n(this,ue,"f").contentWindow;if(!l)throw new Error("Failed to open native file picker: iframe content window not found");i.addEventListener(ve,e=>{if(n(this,ue,"f")?.contentWindow&&e.source===n(this,ue,"f").contentWindow){const t=e.data;"initialize"===t.type&&t.channelId===n(this,de,"f")&&(a(this,he,e.ports[0],"f"),n(this,he,"f").addEventListener(ve,this.messageListener.bind(this)),n(this,he,"f").start(),n(this,he,"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,pe,"f")??""),h.appendChild(f),l.document.body.appendChild(h),n(this,me,"f")?.onOpen?.(),h.submit()}messageListener(e){if("command"!==e.data?.type)return;const t=e.data.data;switch(t.command){case"authenticate":n(this,he,"f")?.postMessage({type:"result",id:e.data.id,data:{result:"token",token:n(this,pe,"f")}});break;case"close":n(this,me,"f")?.onCancel?.(),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker();break;case"pick":n(this,he,"f")&&n(this,he,"f").postMessage({type:"result",id:e.data.id,data:{result:"success"}});const i=t.items.map(ne).filter(Boolean);n(this,me,"f")?.onSelect?.(i),n(this,me,"f")?.onClose?.(),this.closeNativeFilePicker()}}getSitesNaming(){return"Sites"}}re=new WeakMap,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;const be={build(e){switch(e){case"sharepoint":return new ge;case"confluence":return new w;case"googledrive":return new I;case"onedrive":return new ie;case"notion_documents":return new j;default:throw new Error(`Unsupported file storage type: ${e}`)}}};exports.FileStorageClientFactory=be; |
+1
-1
| { | ||
| "name": "@stackone/files-sdk", | ||
| "version": "0.29.0", | ||
| "version": "0.30.0", | ||
| "description": "Files SDK to ease the integration with file storage providers", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
55433
0.1%507
0.2%