@dypai-ai/client-sdk 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @dypai-ai/client-sdk might be problematic. Click here for more details.

package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- class t extends Error{constructor(t,e=500,s,n){super(t),this.status=e,this.code=s,this.details=n,this.name="DypaiError"}}const e={getItem:t=>"undefined"==typeof window?null:window.localStorage.getItem(t),setItem:(t,e)=>{if("undefined"!=typeof window)try{window.localStorage.setItem(t,e)}catch(t){console.error("[DYPAI SDK] ❌ Error crítico guardando en localStorage (¿Quota/Permisos?):",t)}},removeItem:t=>{if("undefined"!=typeof window)try{window.localStorage.removeItem(t)}catch(t){}}};async function s(e){try{return{data:await e,error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error de autenticación",e.status||400)}}}class n{constructor(t,s=null){this.config=t,this.t=null,this.i=null,this.o=null,this.h=null,this.l=[],this.u=null,this.p=null,this.storage=t.storage||e;const n=t.storageKey||this.deriveStorageKey(t.apiKey);console.log(`[DYPAI SDK] 🛠️ Inicializando AuthModule (apiKey: ${t.apiKey?.substring(0,8)}..., storageKey: ${n})`),this.STORAGE_KEY=`dypai-${n}-auth-session`,this.S=this.m().then(()=>{this.i&&this.getUser().catch(()=>{})}),"undefined"!=typeof window&&(window.addEventListener("visibilitychange",this.D.bind(this)),window.addEventListener("focus",this.D.bind(this)),window.addEventListener("storage",t=>{t.key===this.STORAGE_KEY&&(console.log("[DYPAI SDK] 🔄 Sesión actualizada en otra pestaña. Sincronizando..."),this.m(!1))}))}async D(){"undefined"!=typeof document&&"visible"===document.visibilityState&&(console.log("[DYPAI SDK] 👁️ Ventana visible. Sincronizando estado desde storage..."),await this.m(!0))}deriveStorageKey(t){return t?t.substring(0,8):"default"}get user(){return this.t}get token(){return this.i}onAuthStateChange(t){return console.log("[DYPAI SDK] 👂 Nuevo suscriptor añadido a onAuthStateChange"),this.l.push(t),this.S.then(()=>{const e=this._(),s=e?"SIGNED_IN":"INITIAL_SESSION";console.log(`[DYPAI SDK] 📣 Notificando estado inicial a nuevo suscriptor: ${s} (Sesión activa: ${!!e})`),t(s,e)}).catch(e=>{console.error("[DYPAI SDK] ❌ Error esperando recuperación de sesión para suscriptor:",e),t("INITIAL_SESSION",null)}),{data:{subscription:{unsubscribe:()=>{this.l=this.l.filter(e=>e!==t)}}}}}async signInWithPassword(e){return s((async()=>{const s=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/token?grant_type=password`,n={email:e.email||e.identifier||"",password:e.password},i=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(n)});if(!i.ok){const e=await i.json();throw new t(e.msg||e.error_description||"Login failed",i.status)}const a=await i.json(),o={token:a.access_token,refreshToken:a.refresh_token,expiresIn:a.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(a.expires_in||3600),user:this.v(a)};return this.I(o),o})())}async login(t){return this.signInWithPassword(t)}async signUp(e){return s((async()=>{const s=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/signup`,n={email:e.email,password:e.password,data:e.user_data||{}},i=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(n)});if(!i.ok){const e=await i.json();throw new t(e.msg||e.error_description||"Registration failed",i.status)}const a=await i.json(),o={token:a.access_token,refreshToken:a.refresh_token,expiresIn:a.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(a.expires_in||3600),user:this.v(a)};return o.token&&this.I(o),o})())}async register(t){return this.signUp(t)}async getSession(){try{if(!this.i||!this.t)return{data:null,error:null};const t=Math.floor(Date.now()/1e3);return(this.h||0)-t<30&&this.o&&(console.log("[DYPAI SDK] ⏳ getSession: Token próximo a expirar. Refrescando..."),await this.refreshSession().catch(t=>console.warn("Error refreshing session in getSession:",t))),{data:{access_token:this.i,refresh_token:this.o||void 0,token_type:"bearer",user:this.t},error:null}}catch(e){return{data:null,error:new t(e.message,500)}}}async getUser(){return s((async()=>{if(!this.i)throw new t("No hay sesión activa",401);const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,s=await fetch(e,{headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}});if(!s.ok)throw new t("Session invalid",s.status);const n=await s.json(),i=this.v(n);return this.P(i),i})())}async me(){return this.getUser()}async signInWithOAuth(t,e={}){return s((async()=>{const{redirectTo:s=window.location.href}=e,n=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/authorize?provider=${t}&redirect_to=${encodeURIComponent(s)}`;window.location.href=n})())}async signOut(){return s((async()=>{try{if(this.i){const t=this.config.baseUrl||"http://localhost:8000";await fetch(`${t}/auth/v1/logout`,{method:"POST",headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}})}}finally{this.$()}})())}async logout(){return this.signOut()}async resetPasswordForEmail(e){return s((async()=>{const s=this.config.baseUrl||"http://localhost:8000",n=await fetch(`${s}/auth/v1/recover`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e})});if(!n.ok)throw new t("Recovery failed",n.status);return await n.json()})())}async recoverPassword(t){return this.resetPasswordForEmail(t.email)}async refreshSession(){return this.p?(console.log("[DYPAI SDK] 🔄 Refresco de sesión ya en curso. Esperando resolución..."),this.p):(console.log("[DYPAI SDK] 🔄 Iniciando refresco de sesión con Refresh Token..."),this.p=(async()=>{try{if(!this.o)throw new t("No hay refresh token disponible",401);const e=`${this.config.baseUrl||"http://localhost:8000"}/api/v0/auth/token?grant_type=refresh_token`,s=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify({refresh_token:this.o})});if(!s.ok){const e=await s.json().catch(()=>({})),n=s.status;throw 400!==n&&401!==n&&403!==n||(console.error("[DYPAI SDK] ❌ El Refresh Token es inválido. Cerrando sesión automáticamente."),this.$()),new t(e.msg||e.error_description||"Refresh session failed",n)}const n=await s.json(),i={token:n.access_token,refreshToken:n.refresh_token,expiresIn:n.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(n.expires_in||3600),user:this.v(n)};return this.I(i),{data:i,error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error refrescando sesión",401)}}finally{this.p=null}})(),this.p)}async signInWithOtp(e){return s((async()=>{const s=this.config.baseUrl||"http://localhost:8000",n=await fetch(`${s}/auth/v1/otp`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(e)});if(!n.ok){const e=await n.json();throw new t(e.detail||"OTP request failed",n.status)}return await n.json()})())}async verifyOtp(e){return s((async()=>{const s=this.config.baseUrl||"http://localhost:8000",n=await fetch(`${s}/auth/v1/verify`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(e)});if(!n.ok){const e=await n.json();throw new t(e.detail||"OTP verification failed",n.status)}const i=await n.json(),a={token:i.access_token,refreshToken:i.refresh_token,expiresIn:i.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(i.expires_in||3600),user:this.v(i)};return a.token&&this.I(a),a})())}async updateUser(e){return s((async()=>{if(!this.i)throw new t("No hay sesión activa",401);const s=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,n=await fetch(s,{method:"PUT",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(e)});if(!n.ok){const e=await n.json();throw new t(e.detail||"Update user failed",n.status)}const i=await n.json(),a=this.v(i);return this.P(a),a})())}T(t){this.u&&(clearTimeout(this.u),this.u=null);const e=t-Math.floor(Date.now()/1e3)-60;e>0?(console.log(`[DYPAI SDK] ⏱️ Refresco de sesión programado en ${e} segundos.`),this.u=setTimeout(()=>{this.refreshSession().catch(t=>{console.error("Error in auto-refresh:",t)})},1e3*e)):this.o&&this.refreshSession().catch(()=>{})}v(t){if(!t)return console.warn("[DYPAI SDK] ⚠️ Intentando normalizar un usuario inexistente (data es null/undefined)"),{};const e=t.user||t,s=e.app_metadata||{},n=e.user_metadata||{};return{id:e.id,email:e.email,phone:e.phone,role:s.role||e.role,created_at:e.created_at,updated_at:e.updated_at,confirmed_at:e.confirmed_at,last_sign_in_at:e.last_sign_in_at,app_metadata:s,user_metadata:n,k:{name:s.role||"authenticated",weight:0},A:{app_id:"default"}}}I(t){t.token&&(this.i=t.token,this.o=t.refreshToken||null,this.h=t.expiresAt||null,this.P(t.user,t.token,t.refreshToken,t.expiresAt))}async P(t,e,s,n){this.t=t,e&&(this.i=e),void 0!==s&&(this.o=s||null),void 0!==n&&(this.h=n||null);try{if(this.i&&this.t){const t={access_token:this.i,refresh_token:this.o,expires_at:this.h,user:this.t};await this.storage.setItem(this.STORAGE_KEY,JSON.stringify(t))}else console.warn("[DYPAI SDK] ⚠️ _updateUser: No se guardó sesión porque falta token o user.",{hasToken:!!this.i,hasUser:!!this.t})}catch(t){console.error("[DYPAI SDK] ❌ Error guardando sesión en storage:",t)}this.h&&this.T(this.h);const i=e?"SIGNED_IN":"USER_UPDATED";this.K(i)}async $(){console.log("[DYPAI SDK] 🧹 Limpiando sesión del estado y storage."),this.i=null,this.o=null,this.t=null,this.h=null,this.u&&(clearTimeout(this.u),this.u=null);try{await this.storage.removeItem(this.STORAGE_KEY)}catch(t){console.error("[DYPAI SDK] ❌ Error eliminando sesión de storage:",t)}this.K()}async m(t=!0){console.log("[DYPAI SDK] 🔍 Iniciando recuperación de sesión...");try{const e=await this.storage.getItem(this.STORAGE_KEY);if(e){console.log("[DYPAI SDK] ✅ Sesión consolidada encontrada. Restaurando datos...");const t=JSON.parse(e);this.i=t.access_token,this.o=t.refresh_token,this.h=t.expires_at,this.t=t.user}else{console.log("[DYPAI SDK] ℹ️ No hay sesión consolidada. Buscando llaves antiguas para migración...");const t=this.STORAGE_KEY.replace("dypai-","").replace("-auth-session",""),e=await this.storage.getItem(`dypai-auth-token-${t}`),s=await this.storage.getItem(`dypai-auth-user-${t}`);if(!e||!s)return void console.log("[DYPAI SDK] ℹ️ No se encontró ninguna sesión (storage vacío).");{console.log("[DYPAI SDK] 🚚 Migración: Datos antiguos detectados. Consolidando...");const n=JSON.parse(s);this.i=e,this.t=n;const i=await this.storage.getItem(`dypai-auth-refresh-token-${t}`),a=await this.storage.getItem(`dypai-auth-expires-at-${t}`);this.o=i,this.h=a?parseInt(a,10):null,await this.P(n,this.i||void 0,this.o||void 0,this.h||void 0);const o=[`dypai-auth-token-${t}`,`dypai-auth-refresh-token-${t}`,`dypai-auth-user-${t}`,`dypai-auth-expires-at-${t}`];for(const t of o)try{await this.storage.removeItem(t)}catch(t){}console.log("[DYPAI SDK] ✨ Migración completada con éxito.")}}const s=Math.floor(Date.now()/1e3),n=this.h&&this.h<=s;console.log(`[DYPAI SDK] 🕒 Token expira en: ${this.h}. Ahora es: ${s}. Diferencia: ${(this.h||0)-s}s`),n&&this.o&&t?(console.log("[DYPAI SDK] ⚠️ El Access Token ha caducado. Intentando refrescar sesión inmediatamente..."),await this.refreshSession().catch(t=>{console.error("[DYPAI SDK] ❌ El refresco inicial falló:",t)})):this.h&&(console.log("[DYPAI SDK] ✨ Sesión válida. Programando refresco futuro."),this.T(this.h)),this.K("SIGNED_IN")}catch(t){console.error("[DYPAI SDK] ❌ Error crítico durante la recuperación de sesión:",t)}}_(){return this.i&&this.t?{access_token:this.i,refresh_token:this.o||void 0,expires_at:this.h||void 0,token_type:"bearer",user:this.t}:null}K(t="USER_UPDATED"){const e=this._();console.log(`[DYPAI SDK] 📢 Notificando a ${this.l.length} suscriptores: Evento=${t} (Sesión activa: ${!!e})`),this.l.forEach(s=>s(t,e))}handleSessionExpired(){this.$()}}class i{constructor(t){this.api=t}from(t){return new a(t,this.api)}}class a{constructor(t,e){this.table=t,this.api=e}async select(t={}){return this.api.get(this.table,{params:t})}async insert(t){return this.api.post(this.table,t)}async update(t,e){return this.api.patch(`${this.table}/${t}`,e)}async delete(t){return this.api.delete(`${this.table}/${t}`)}}async function o(e){try{return{data:await e,error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error desconocido",e.status||500)}}}class r{constructor(t){this.api=t}async list(t={}){return o(this.api.get("admin/users",{params:t}))}async create(t){return o(this.api.post("admin/users",{body:t}))}async update(t,e){return o(this.api.put(`admin/users/${t}`,{body:e}))}async delete(t){return o(this.api.delete(`admin/users/${t}`))}}let c=null;function h(t){c=t}const l=t=>{const{title:e,description:s,variant:n="default"}=t,i=`${"error"===n?"❌":"success"===n?"✅":"warning"===n?"⚠️":"info"===n?"ℹ️":"📢"} ${e}${s?`: ${s}`:""}`;"error"===n?console.error(i):"warning"===n?console.warn(i):console.log(i)};function u(){const t=c||l;return{toast:t,toastSuccess:(e,s)=>t({title:e,description:s,variant:"success"}),toastError:(e,s)=>t({title:e,description:s,variant:"error"}),toastWarning:(e,s)=>t({title:e,description:s,variant:"warning"}),toastInfo:(e,s)=>t({title:e,description:s,variant:"info"})}}const d=t=>(c||l)(t),p=(t,e)=>d({title:t,description:e,variant:"success"}),f=(t,e)=>d({title:t,description:e,variant:"error"}),y=(t,e)=>d({title:t,description:e,variant:"warning"}),w=(t,e)=>d({title:t,description:e,variant:"info"});let S={},m=null;function g(t){m=t}function D(t){S={...S,...t}}const _=new Map;function v(){let t=S;try{const{getGlobalConfig:e}=require("../config/global-config");t={...e(),...S}}catch(t){}return t}async function I(e,s,n,i,a,o,r,c){const h=v(),l=c||null;if(!l&&!n.startsWith("http"))throw new Error("Base URL no definida. Usa createClient(url, key).");if("string"!=typeof n||!n.trim())throw new Error("Endpoint debe ser un string válido");let u;if(n.startsWith("http"))u=n;else{const t=l.replace(/\/+$/,"");u=n.startsWith("/")?t+n:t+"/api/v0/"+n}if(o&&Object.keys(o).length>0){const t=new URLSearchParams,e=(s,n)=>{null!=n&&(Array.isArray(n)?n.forEach((t,n)=>e(`${s}[${n}]`,t)):"object"==typeof n?Object.entries(n).forEach(([t,n])=>e(`${s}[${t}]`,n)):t.append(s,String(n)))};Object.entries(o).forEach(([t,s])=>e(t,s));const s=t.toString();s&&(u+=`?${s}`)}const p="GET"===s?`${s}:${u}:${JSON.stringify(i)}`:null;if(p&&_.has(p))return _.get(p);const f=i instanceof FormData,y={...h.headers||{},...f?{}:{"Content-Type":"application/json"},...e&&{Authorization:`Bearer ${e}`},...r&&{"x-api-key":r}},w={method:s,headers:y,credentials:"include"};i&&"GET"!==s&&"DELETE"!==s&&(w.body=f?i:JSON.stringify(i));const S=h.fetch||("undefined"!=typeof window?window.fetch.bind(window):fetch);if(!S)throw new Error("Fetch no disponible.");const m=(async()=>{const e=v().toast||d,l=(void 0!==a?a:!1!==h.showToasts)&&e;try{const d=await S(u,w);if(!d.ok){let u,p="Error en la petición";try{const t=await d.text();try{const e=JSON.parse(t);u=e,p=e.message||e.msg||e.error_description||e.error||p}catch{t.length<200&&(p=t)}}catch{}if(401===d.status){if(h.onTokenExpired){console.log("[DYPAI SDK] 🔄 Token caducado detectado. Intentando refresco atómico...");const t=await h.onTokenExpired();if(t)return console.log("[DYPAI SDK] ✅ Token refrescado con éxito. Reintentando petición..."),I(t,s,n,i,a,o,r,c)}h.onUnauthorized&&(console.warn("[DYPAI SDK] ❌ No se pudo refrescar la sesión (401 definitivo). Limpiando..."),h.onUnauthorized())}throw l&&e({title:"Error",description:p,variant:"error"}),new t(p,d.status,void 0,u)}!l||"POST"!==s&&"PUT"!==s&&"PATCH"!==s&&"DELETE"!==s||e({title:"Éxito",description:"Operación completada",variant:"success"});const p=d.headers.get("content-type")||"";return p.includes("application/pdf")||p.includes("image/")||p.includes("audio/")||p.includes("video/")||p.includes("application/octet-stream")||p.includes("application/zip")||p.includes("application/vnd.openxmlformats-officedocument")?await d.blob():p.includes("application/json")?await d.json():await d.text()}finally{p&&_.delete(p)}})();return p&&_.set(p,m),m}function P(t,e){return async(s,n,i)=>{const a=t();let o,r={};n&&"object"==typeof n&&("token"in n||"params"in n||"apiKey"in n)?(r=n,o=r.body):(o=n,r=i||{});const c=r.token||a.token||(m?m():"")||"",h=r.apiKey||a.apiKey;return I(c,e,s,o,r.showToasts,r.params,h,a.baseUrl)}}function $(t){const e=()=>"function"==typeof t?t():t;return{get:T(e,"GET"),post:T(e,"POST"),put:T(e,"PUT"),patch:T(e,"PATCH"),delete:T(e,"DELETE")}}function T(t,e){return async(s,n,i)=>{const a=t(),o=await async function(t,e){let s,n={};t&&"object"==typeof t&&("token"in t||"params"in t||"apiKey"in t)?(n=t,s=n.body):(s=t,n=e||{});let i=n.token;return!i&&m&&(i=m()||""),i=i||"",{token:i,apiKey:n.apiKey,body:s,params:n.params,showToasts:n.showToasts}}(n,i),r=o.token||a.token||"",c=o.apiKey||a.apiKey;return I(r,e,s,o.body,o.showToasts,o.params,c,a.baseUrl)}}class k{constructor(t){this.api=t}from(t){return new A(t,this.api)}}class A{constructor(t,e){this.bucketName=t,this.api=e}async upload(e,s,n={}){return E(async()=>{const i=n.uploadEndpoint||`storage_upload_${this.bucketName}`,a={file_path:e,...n.params},o=await async function(e,s,n,i){const a=await e.post(s,{file_path:n.name,content_type:n.type||"application/octet-stream",size_bytes:n.size,confirm:!1,...i?.params||{}});if(!a||!a.upload_url)throw new t("El workflow no devolvió una URL de subida válida (¿falta get_upload_url?)",400);const{upload_url:o,method:r="PUT",headers:c={},file_path:h}=a;i?.onProgress&&i.onProgress(10);const l=await fetch(o,{method:r,headers:{"Content-Type":n.type||"application/octet-stream",...c},body:n});if(!l.ok)throw new t("Error en la subida directa a la nube",l.status);i?.onProgress&&i.onProgress(90);const u=i?.confirmEndpoint||s,d=await e.post(u,{...i?.params,bucket:a.bucket||i?.params?.bucket,file_path:h,filename:n.name,content_type:n.type||"application/octet-stream",size_bytes:n.size,confirm:!0});return i?.onProgress&&i.onProgress(100),d}(this.api,i,s,{confirmEndpoint:n.confirmEndpoint,params:a,onProgress:n.onProgress});return o})}async download(t,e={}){return E(async()=>{const s=e.downloadEndpoint||`storage_download_${this.bucketName}`,n={bucket:this.bucketName,file_path:t,...e.params};await async function(t,e,s,n){const i=n?.method,a=await("GET"===i?t.get(e,{params:n?.params}):t.post(e,s,{params:n?.params}));if(a instanceof Blob){const t=window.URL.createObjectURL(a),e=document.createElement("a");e.href=t,e.download=n?.fileName||"archivo-descargado",document.body.appendChild(e),e.click(),window.URL.revokeObjectURL(t),document.body.removeChild(e)}else if(a&&"object"==typeof a&&("url"in a||"signed_url"in a||"signedUrl"in a)){const t=a.url||a.signed_url||a.signedUrl;"string"==typeof t&&window.open(t,"_blank")}}(this.api,s,n,{fileName:e.fileName,method:e.method||"POST"})})}async createSignedUrl(t,e=15,s={}){return E(async()=>{const n=s.endpoint||`storage_signed_url_${this.bucketName}`,i={bucket:this.bucketName,file_path:t,expires_minutes:e,...s.params},a=await this.api.post(n,i);return{signedUrl:a.signed_url||a.signedUrl||a.url,expiresIn:a.expires_in||a.expiresIn||e,path:t}})}getPublicUrl(t){const e=t.replace(/^\/+/,"");return{data:{publicUrl:`${this.api.baseUrl||""}/storage/v1/render/public/${this.bucketName}/${e}`}}}async list(t="",e={}){return E(async()=>{const s=e.endpoint||`storage_list_${this.bucketName}`,n={bucket:this.bucketName,prefix:t||e.prefix||"",limit:e.limit,offset:e.offset,sort_by:e.sortBy,order:e.order,search:e.search,...e.params},i=await this.api.post(s,n);return{data:i.data||i.files||[],total:i.total,hasMore:i.has_more||i.hasMore||!1}})}async remove(t,e={}){return E(async()=>{const s=e.endpoint||`storage_delete_${this.bucketName}`,n=Array.isArray(t)?t:[t],i={bucket:this.bucketName,paths:n,...e.params};return await this.api.post(s,i),{success:!0}})}async move(t,e,s={}){return E(async()=>{const n=s.endpoint||`storage_move_${this.bucketName}`,i={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(n,i),{success:!0}})}async copy(t,e,s={}){return E(async()=>{const n=s.endpoint||`storage_copy_${this.bucketName}`,i={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(n,i),{success:!0}})}}async function E(e){try{return{data:await e(),error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error de storage",e.status||500)}}}class b{constructor(t){const{baseUrl:e,apiKey:s}=t,a=t.auth?.storageKey||t.storageKey;t.global&&function(t){S={...S,...t}}(t.global),this.auth=new n({baseUrl:e,apiKey:s,storageKey:a,storage:t.auth?.storage,autoRefreshToken:t.auth?.autoRefreshToken,persistSession:t.auth?.persistSession},null);const o={get:P(c=()=>({token:this.auth.token,apiKey:s,baseUrl:e}),"GET"),post:P(c,"POST"),put:P(c,"PUT"),patch:P(c,"PATCH"),delete:P(c,"DELETE")};var c;this.auth.api=o,this.db=new i(o),this.users=new r(o),this.storage=new k(o),this.api=$(()=>({token:this.auth.token||"",apiKey:s,baseUrl:e})),g(()=>this.auth.token),D({onTokenExpired:async()=>{const t=await this.auth.refreshSession();return t.data?.token||null},onUnauthorized:()=>this.auth.handleSessionExpired()})}from(t){return this.db.from(t)}async me(){return this.auth.getUser()}}function K(t,e,s){if(!t||!e)throw new Error("createClient() requiere URL y API Key");return new b({baseUrl:t,apiKey:e,...s})}let O={};function U(t){O={...O,toast:d,...t},Y()}function N(){return{...O}}function Y(){try{const{configureApiService:t}=require("../services/ApiService");t(O)}catch(t){}}function x(){O={},Y()}async function C(){Y()}const R={name:"@dypai-ai/client-sdk",version:"0.0.1",description:"Cliente JavaScript para Dypai Engine",features:["API REST autenticada","Smart Storage (Upload/Download delegado)","Gestión de usuarios (Admin)"]};export{b as DypaiClient,R as PACKAGE_INFO,I as callApi,D as configureApiService,U as configureDypaiServices,$ as createApiClient,K as createClient,N as getGlobalConfig,C as reloadDypaiConfig,x as resetGlobalConfig,h as setToastFunction,g as setTokenProvider,d as toast,f as toastError,w as toastInfo,p as toastSuccess,y as toastWarning,u as useToast};
1
+ class t extends Error{constructor(t,e=500,s,n){super(t),this.status=e,this.code=s,this.details=n,this.name="DypaiError"}}const e={getItem:t=>"undefined"==typeof window?null:window.localStorage.getItem(t),setItem:(t,e)=>{if("undefined"!=typeof window)try{window.localStorage.setItem(t,e)}catch(t){console.error("[DYPAI SDK] ❌ Error crítico guardando en localStorage (¿Quota/Permisos?):",t)}},removeItem:t=>{if("undefined"!=typeof window)try{window.localStorage.removeItem(t)}catch(t){}}};async function s(e){try{return{data:await e,error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error de autenticación",e.status||400)}}}class n{constructor(t,s=null){this.config=t,this.t=null,this.i=null,this.o=null,this.h=null,this.l=[],this.u=null,this.p=null,this.storage=t.storage||e;const n=t.storageKey||this.deriveStorageKey(t.apiKey);console.log(`[DYPAI SDK] 🛠️ Inicializando AuthModule (apiKey: ${t.apiKey?.substring(0,8)}..., storageKey: ${n})`),this.STORAGE_KEY=`dypai-${n}-auth-session`,this.S=this.m().then(()=>{this.i&&this.getUser().catch(()=>{})}),"undefined"!=typeof window&&(window.addEventListener("visibilitychange",this.D.bind(this)),window.addEventListener("focus",this.D.bind(this)),window.addEventListener("storage",t=>{t.key===this.STORAGE_KEY&&(console.log("[DYPAI SDK] 🔄 Sesión actualizada en otra pestaña. Sincronizando..."),this.m(!1))}))}async D(){"undefined"!=typeof document&&"visible"===document.visibilityState&&(console.log("[DYPAI SDK] 👁️ Ventana visible. Sincronizando estado desde storage..."),await this.m(!0))}deriveStorageKey(t){return t?t.substring(0,8):"default"}get user(){return this.t}get token(){return this.i}onAuthStateChange(t){return console.log("[DYPAI SDK] 👂 Nuevo suscriptor añadido a onAuthStateChange"),this.l.push(t),this.S.then(()=>{const e=this._(),s=e?"SIGNED_IN":"INITIAL_SESSION";console.log(`[DYPAI SDK] 📣 Notificando estado inicial a nuevo suscriptor: ${s} (Sesión activa: ${!!e})`),t(s,e)}).catch(e=>{console.error("[DYPAI SDK] ❌ Error esperando recuperación de sesión para suscriptor:",e),t("INITIAL_SESSION",null)}),{data:{subscription:{unsubscribe:()=>{this.l=this.l.filter(e=>e!==t)}}}}}async signInWithPassword(e){return s((async()=>{const s=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/token?grant_type=password`,n={email:e.email||e.identifier||"",password:e.password},i=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(n)});if(!i.ok){const e=await i.json();throw new t(e.msg||e.error_description||"Login failed",i.status)}const a=await i.json(),o={token:a.access_token,refreshToken:a.refresh_token,expiresIn:a.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(a.expires_in||3600),user:this.v(a)};return this.I(o),o})())}async login(t){return this.signInWithPassword(t)}async signUp(e){return s((async()=>{const s=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/signup`,n={email:e.email,password:e.password,data:e.user_data||{}},i=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(n)});if(!i.ok){const e=await i.json();throw new t(e.msg||e.error_description||"Registration failed",i.status)}const a=await i.json(),o={token:a.access_token,refreshToken:a.refresh_token,expiresIn:a.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(a.expires_in||3600),user:this.v(a)};return o.token&&this.I(o),o})())}async register(t){return this.signUp(t)}async getSession(){try{if(!this.i||!this.t)return{data:null,error:null};const t=Math.floor(Date.now()/1e3);return(this.h||0)-t<30&&this.o&&(console.log("[DYPAI SDK] ⏳ getSession: Token próximo a expirar. Refrescando..."),await this.refreshSession().catch(t=>console.warn("Error refreshing session in getSession:",t))),{data:{access_token:this.i,refresh_token:this.o||void 0,token_type:"bearer",user:this.t},error:null}}catch(e){return{data:null,error:new t(e.message,500)}}}async getUser(){return s((async()=>{if(!this.i)throw new t("No hay sesión activa",401);const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,s=await fetch(e,{headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}});if(!s.ok)throw new t("Session invalid",s.status);const n=await s.json(),i=this.v(n);return this.$(i),i})())}async me(){return this.getUser()}async signInWithOAuth(t,e={}){return s((async()=>{const{redirectTo:s=window.location.href}=e,n=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/authorize?provider=${t}&redirect_to=${encodeURIComponent(s)}`;window.location.href=n})())}async signOut(){return s((async()=>{try{if(this.i){const t=this.config.baseUrl||"http://localhost:8000";await fetch(`${t}/auth/v1/logout`,{method:"POST",headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}})}}finally{this.P("signOut called")}})())}async logout(){return this.signOut()}async resetPasswordForEmail(e){return s((async()=>{const s=this.config.baseUrl||"http://localhost:8000",n=await fetch(`${s}/auth/v1/recover`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e})});if(!n.ok)throw new t("Recovery failed",n.status);return await n.json()})())}async recoverPassword(t){return this.resetPasswordForEmail(t.email)}async refreshSession(){return this.p?(console.log("[DYPAI SDK] 🔄 Refresco de sesión ya en curso. Esperando resolución..."),this.p):(console.log("[DYPAI SDK] 🔄 Iniciando refresco de sesión con Refresh Token..."),this.p=(async()=>{try{if(!this.o)throw new t("No hay refresh token disponible",401);const e=`${this.config.baseUrl||"http://localhost:8000"}/api/v0/auth/token?grant_type=refresh_token`,s=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify({refresh_token:this.o})});if(!s.ok){const e=await s.json().catch(()=>({})),n=s.status,i=e.error||e.code||"unknown";throw 400===n||401===n||403===n?(console.error(`[DYPAI SDK] ❌ Error terminal en refresco (${n}: ${i}). Limpiando sesión.`),this.P(`refreshSession failed (${n}: ${i})`)):console.warn(`[DYPAI SDK] ⚠️ Error no-terminal en refresco (${n}). Manteniendo sesión.`),new t(e.msg||e.error_description||"Refresh session failed",n)}const n=await s.json(),i={token:n.access_token,refreshToken:n.refresh_token,expiresIn:n.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(n.expires_in||3600),user:this.v(n)};return this.I(i),{data:i,error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error refrescando sesión",401)}}finally{this.p=null}})(),this.p)}async signInWithOtp(e){return s((async()=>{const s=this.config.baseUrl||"http://localhost:8000",n=await fetch(`${s}/auth/v1/otp`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(e)});if(!n.ok){const e=await n.json();throw new t(e.detail||"OTP request failed",n.status)}return await n.json()})())}async verifyOtp(e){return s((async()=>{const s=this.config.baseUrl||"http://localhost:8000",n=await fetch(`${s}/auth/v1/verify`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(e)});if(!n.ok){const e=await n.json();throw new t(e.detail||"OTP verification failed",n.status)}const i=await n.json(),a={token:i.access_token,refreshToken:i.refresh_token,expiresIn:i.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(i.expires_in||3600),user:this.v(i)};return a.token&&this.I(a),a})())}async updateUser(e){return s((async()=>{if(!this.i)throw new t("No hay sesión activa",401);const s=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,n=await fetch(s,{method:"PUT",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(e)});if(!n.ok){const e=await n.json();throw new t(e.detail||"Update user failed",n.status)}const i=await n.json(),a=this.v(i);return this.$(a),a})())}k(t){this.u&&(clearTimeout(this.u),this.u=null);const e=t-Math.floor(Date.now()/1e3)-60;e>0?(console.log(`[DYPAI SDK] ⏱️ Refresco de sesión programado en ${e} segundos.`),this.u=setTimeout(()=>{this.refreshSession().catch(t=>{console.error("Error in auto-refresh:",t)})},1e3*e)):this.o&&this.refreshSession().catch(()=>{})}v(t){if(!t)return console.warn("[DYPAI SDK] ⚠️ Intentando normalizar un usuario inexistente (data es null/undefined)"),{};const e=t.user||t,s=e.app_metadata||{},n=e.user_metadata||{};return{id:e.id,email:e.email,phone:e.phone,role:s.role||e.role,created_at:e.created_at,updated_at:e.updated_at,confirmed_at:e.confirmed_at,last_sign_in_at:e.last_sign_in_at,app_metadata:s,user_metadata:n,A:{name:s.role||"authenticated",weight:0},T:{app_id:"default"}}}I(t){t.token&&(this.i=t.token,this.o=t.refreshToken||null,this.h=t.expiresAt||null,this.$(t.user,t.token,t.refreshToken,t.expiresAt))}async $(t,e,s,n){this.t=t,e&&(this.i=e),void 0!==s&&(this.o=s||null),void 0!==n&&(this.h=n||null);try{if(this.i&&this.t){const t={access_token:this.i,refresh_token:this.o,expires_at:this.h,user:this.t};await this.storage.setItem(this.STORAGE_KEY,JSON.stringify(t))}else console.warn("[DYPAI SDK] ⚠️ _updateUser: No se guardó sesión porque falta token o user.",{hasToken:!!this.i,hasUser:!!this.t})}catch(t){console.error("[DYPAI SDK] ❌ Error guardando sesión en storage:",t)}this.h&&this.k(this.h);const i=e?"SIGNED_IN":"USER_UPDATED";this.K(i)}async P(t="unknown"){console.log(`[DYPAI SDK] 🧹 Limpiando sesión del estado y storage. Motivo: ${t}`),this.i=null,this.o=null,this.t=null,this.h=null,this.u&&(clearTimeout(this.u),this.u=null);try{await this.storage.removeItem(this.STORAGE_KEY),console.log("[DYPAI SDK] ✅ Storage limpiado con éxito.")}catch(t){console.error("[DYPAI SDK] ❌ Error eliminando sesión de storage:",t)}this.K()}async m(t=!0){console.log("[DYPAI SDK] 🔍 Iniciando recuperación de sesión...");try{const e=await this.storage.getItem(this.STORAGE_KEY);if(e){console.log("[DYPAI SDK] ✅ Sesión consolidada encontrada. Restaurando datos...");const t=JSON.parse(e);this.i=t.access_token,this.o=t.refresh_token,this.h=t.expires_at,this.t=t.user}else{console.log("[DYPAI SDK] ℹ️ No hay sesión consolidada. Buscando llaves antiguas para migración...");const t=this.STORAGE_KEY.replace("dypai-","").replace("-auth-session",""),e=await this.storage.getItem(`dypai-auth-token-${t}`),s=await this.storage.getItem(`dypai-auth-user-${t}`);if(!e||!s)return void console.log("[DYPAI SDK] ℹ️ No se encontró ninguna sesión (storage vacío).");{console.log("[DYPAI SDK] 🚚 Migración: Datos antiguos detectados. Consolidando...");const n=JSON.parse(s);this.i=e,this.t=n;const i=await this.storage.getItem(`dypai-auth-refresh-token-${t}`),a=await this.storage.getItem(`dypai-auth-expires-at-${t}`);this.o=i,this.h=a?parseInt(a,10):null,await this.$(n,this.i||void 0,this.o||void 0,this.h||void 0);const o=[`dypai-auth-token-${t}`,`dypai-auth-refresh-token-${t}`,`dypai-auth-user-${t}`,`dypai-auth-expires-at-${t}`];for(const t of o)try{await this.storage.removeItem(t)}catch(t){}console.log("[DYPAI SDK] ✨ Migración completada con éxito.")}}const s=Math.floor(Date.now()/1e3),n=this.h&&this.h<=s;console.log(`[DYPAI SDK] 🕒 Token expira en: ${this.h}. Ahora es: ${s}. Diferencia: ${(this.h||0)-s}s`),n&&this.o&&t?(console.log("[DYPAI SDK] ⚠️ El Access Token ha caducado. Intentando refrescar sesión inmediatamente..."),await this.refreshSession().catch(t=>{console.error("[DYPAI SDK] ❌ El refresco inicial falló:",t)})):this.h&&(console.log("[DYPAI SDK] ✨ Sesión válida. Programando refresco futuro."),this.k(this.h)),this.K("SIGNED_IN")}catch(t){console.error("[DYPAI SDK] ❌ Error crítico durante la recuperación de sesión:",t)}}_(){return this.i&&this.t?{access_token:this.i,refresh_token:this.o||void 0,expires_at:this.h||void 0,token_type:"bearer",user:this.t}:null}K(t="USER_UPDATED"){const e=this._();console.log(`[DYPAI SDK] 📢 Notificando a ${this.l.length} suscriptores: Evento=${t} (Sesión activa: ${!!e})`),this.l.forEach(s=>s(t,e))}handleSessionExpired(){this.P("handleSessionExpired called (likely 401 from API)")}}class i{constructor(t){this.api=t}from(t){return new a(t,this.api)}}class a{constructor(t,e){this.table=t,this.api=e}async select(t={}){return this.api.get(this.table,{params:t})}async insert(t){return this.api.post(this.table,t)}async update(t,e){return this.api.patch(`${this.table}/${t}`,e)}async delete(t){return this.api.delete(`${this.table}/${t}`)}}async function o(e){try{return{data:await e,error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error desconocido",e.status||500)}}}class r{constructor(t){this.api=t}async list(t={}){return o(this.api.get("admin/users",{params:t}))}async create(t){return o(this.api.post("admin/users",{body:t}))}async update(t,e){return o(this.api.put(`admin/users/${t}`,{body:e}))}async delete(t){return o(this.api.delete(`admin/users/${t}`))}}let c=null;function h(t){c=t}const l=t=>{const{title:e,description:s,variant:n="default"}=t,i=`${"error"===n?"❌":"success"===n?"✅":"warning"===n?"⚠️":"info"===n?"ℹ️":"📢"} ${e}${s?`: ${s}`:""}`;"error"===n?console.error(i):"warning"===n?console.warn(i):console.log(i)};function u(){const t=c||l;return{toast:t,toastSuccess:(e,s)=>t({title:e,description:s,variant:"success"}),toastError:(e,s)=>t({title:e,description:s,variant:"error"}),toastWarning:(e,s)=>t({title:e,description:s,variant:"warning"}),toastInfo:(e,s)=>t({title:e,description:s,variant:"info"})}}const d=t=>(c||l)(t),p=(t,e)=>d({title:t,description:e,variant:"success"}),f=(t,e)=>d({title:t,description:e,variant:"error"}),y=(t,e)=>d({title:t,description:e,variant:"warning"}),w=(t,e)=>d({title:t,description:e,variant:"info"});let S={},m=null;function g(t){m=t}function D(t){S={...S,...t}}const _=new Map;function v(){let t=S;try{const{getGlobalConfig:e}=require("../config/global-config");t={...e(),...S}}catch(t){}return t}async function I(e,s,n,i,a,o,r,c){const h=v(),l=c||null;if(!l&&!n.startsWith("http"))throw new Error("Base URL no definida. Usa createClient(url, key).");if("string"!=typeof n||!n.trim())throw new Error("Endpoint debe ser un string válido");let u;if(n.startsWith("http"))u=n;else{const t=l.replace(/\/+$/,"");u=n.startsWith("/")?t+n:t+"/api/v0/"+n}if(o&&Object.keys(o).length>0){const t=new URLSearchParams,e=(s,n)=>{null!=n&&(Array.isArray(n)?n.forEach((t,n)=>e(`${s}[${n}]`,t)):"object"==typeof n?Object.entries(n).forEach(([t,n])=>e(`${s}[${t}]`,n)):t.append(s,String(n)))};Object.entries(o).forEach(([t,s])=>e(t,s));const s=t.toString();s&&(u+=`?${s}`)}const p="GET"===s?`${s}:${u}:${JSON.stringify(i)}`:null;if(p&&_.has(p))return _.get(p);const f=i instanceof FormData,y={...h.headers||{},...f?{}:{"Content-Type":"application/json"},...e&&{Authorization:`Bearer ${e}`},...r&&{"x-api-key":r}},w={method:s,headers:y,credentials:"include"};i&&"GET"!==s&&"DELETE"!==s&&(w.body=f?i:JSON.stringify(i));const S=h.fetch||("undefined"!=typeof window?window.fetch.bind(window):fetch);if(!S)throw new Error("Fetch no disponible.");const m=(async()=>{const e=v().toast||d,l=(void 0!==a?a:!1!==h.showToasts)&&e;try{const d=await S(u,w);if(!d.ok){let u,p="Error en la petición";try{const t=await d.text();try{const e=JSON.parse(t);u=e,p=e.message||e.msg||e.error_description||e.error||p}catch{t.length<200&&(p=t)}}catch{}if(401===d.status&&h.onTokenExpired){console.log("[DYPAI SDK] 🔄 Token caducado detectado. Intentando refresco atómico...");try{const t=await h.onTokenExpired();if(t)return console.log("[DYPAI SDK] ✅ Token refrescado con éxito. Reintentando petición..."),I(t,s,n,i,a,o,r,c)}catch(t){console.error("[DYPAI SDK] ❌ Error durante el intento de refresco:",t)}}throw l&&e({title:"Error",description:p,variant:"error"}),new t(p,d.status,void 0,u)}!l||"POST"!==s&&"PUT"!==s&&"PATCH"!==s&&"DELETE"!==s||e({title:"Éxito",description:"Operación completada",variant:"success"});const p=d.headers.get("content-type")||"";return p.includes("application/pdf")||p.includes("image/")||p.includes("audio/")||p.includes("video/")||p.includes("application/octet-stream")||p.includes("application/zip")||p.includes("application/vnd.openxmlformats-officedocument")?await d.blob():p.includes("application/json")?await d.json():await d.text()}finally{p&&_.delete(p)}})();return p&&_.set(p,m),m}function $(t,e){return async(s,n,i)=>{const a=t();let o,r={};n&&"object"==typeof n&&("token"in n||"params"in n||"apiKey"in n)?(r=n,o=r.body):(o=n,r=i||{});const c=r.token||a.token||(m?m():"")||"",h=r.apiKey||a.apiKey;return I(c,e,s,o,r.showToasts,r.params,h,a.baseUrl)}}function P(t){const e=()=>"function"==typeof t?t():t;return{get:k(e,"GET"),post:k(e,"POST"),put:k(e,"PUT"),patch:k(e,"PATCH"),delete:k(e,"DELETE")}}function k(t,e){return async(s,n,i)=>{const a=t(),o=await async function(t,e){let s,n={};t&&"object"==typeof t&&("token"in t||"params"in t||"apiKey"in t)?(n=t,s=n.body):(s=t,n=e||{});let i=n.token;return!i&&m&&(i=m()||""),i=i||"",{token:i,apiKey:n.apiKey,body:s,params:n.params,showToasts:n.showToasts}}(n,i),r=o.token||a.token||"",c=o.apiKey||a.apiKey;return I(r,e,s,o.body,o.showToasts,o.params,c,a.baseUrl)}}class A{constructor(t){this.api=t}from(t){return new E(t,this.api)}}class E{constructor(t,e){this.bucketName=t,this.api=e}async upload(e,s,n={}){return T(async()=>{const i=n.uploadEndpoint||`storage_upload_${this.bucketName}`,a={file_path:e,...n.params},o=await async function(e,s,n,i){const a=await e.post(s,{file_path:n.name,content_type:n.type||"application/octet-stream",size_bytes:n.size,confirm:!1,...i?.params||{}});if(!a||!a.upload_url)throw new t("El workflow no devolvió una URL de subida válida (¿falta get_upload_url?)",400);const{upload_url:o,method:r="PUT",headers:c={},file_path:h}=a;i?.onProgress&&i.onProgress(10);const l=await fetch(o,{method:r,headers:{"Content-Type":n.type||"application/octet-stream",...c},body:n});if(!l.ok)throw new t("Error en la subida directa a la nube",l.status);i?.onProgress&&i.onProgress(90);const u=i?.confirmEndpoint||s,d=await e.post(u,{...i?.params,bucket:a.bucket||i?.params?.bucket,file_path:h,filename:n.name,content_type:n.type||"application/octet-stream",size_bytes:n.size,confirm:!0});return i?.onProgress&&i.onProgress(100),d}(this.api,i,s,{confirmEndpoint:n.confirmEndpoint,params:a,onProgress:n.onProgress});return o})}async download(t,e={}){return T(async()=>{const s=e.downloadEndpoint||`storage_download_${this.bucketName}`,n={bucket:this.bucketName,file_path:t,...e.params};await async function(t,e,s,n){const i=n?.method,a=await("GET"===i?t.get(e,{params:n?.params}):t.post(e,s,{params:n?.params}));if(a instanceof Blob){const t=window.URL.createObjectURL(a),e=document.createElement("a");e.href=t,e.download=n?.fileName||"archivo-descargado",document.body.appendChild(e),e.click(),window.URL.revokeObjectURL(t),document.body.removeChild(e)}else if(a&&"object"==typeof a&&("url"in a||"signed_url"in a||"signedUrl"in a)){const t=a.url||a.signed_url||a.signedUrl;"string"==typeof t&&window.open(t,"_blank")}}(this.api,s,n,{fileName:e.fileName,method:e.method||"POST"})})}async createSignedUrl(t,e=15,s={}){return T(async()=>{const n=s.endpoint||`storage_signed_url_${this.bucketName}`,i={bucket:this.bucketName,file_path:t,expires_minutes:e,...s.params},a=await this.api.post(n,i);return{signedUrl:a.signed_url||a.signedUrl||a.url,expiresIn:a.expires_in||a.expiresIn||e,path:t}})}getPublicUrl(t){const e=t.replace(/^\/+/,"");return{data:{publicUrl:`${this.api.baseUrl||""}/storage/v1/render/public/${this.bucketName}/${e}`}}}async list(t="",e={}){return T(async()=>{const s=e.endpoint||`storage_list_${this.bucketName}`,n={bucket:this.bucketName,prefix:t||e.prefix||"",limit:e.limit,offset:e.offset,sort_by:e.sortBy,order:e.order,search:e.search,...e.params},i=await this.api.post(s,n);return{data:i.data||i.files||[],total:i.total,hasMore:i.has_more||i.hasMore||!1}})}async remove(t,e={}){return T(async()=>{const s=e.endpoint||`storage_delete_${this.bucketName}`,n=Array.isArray(t)?t:[t],i={bucket:this.bucketName,paths:n,...e.params};return await this.api.post(s,i),{success:!0}})}async move(t,e,s={}){return T(async()=>{const n=s.endpoint||`storage_move_${this.bucketName}`,i={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(n,i),{success:!0}})}async copy(t,e,s={}){return T(async()=>{const n=s.endpoint||`storage_copy_${this.bucketName}`,i={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(n,i),{success:!0}})}}async function T(e){try{return{data:await e(),error:null}}catch(e){return{data:null,error:e instanceof t?e:new t(e.message||"Error de storage",e.status||500)}}}class b{constructor(t){const{baseUrl:e,apiKey:s}=t,a=t.auth?.storageKey||t.storageKey;t.global&&function(t){S={...S,...t}}(t.global),this.auth=new n({baseUrl:e,apiKey:s,storageKey:a,storage:t.auth?.storage,autoRefreshToken:t.auth?.autoRefreshToken,persistSession:t.auth?.persistSession},null);const o={get:$(c=()=>({token:this.auth.token,apiKey:s,baseUrl:e}),"GET"),post:$(c,"POST"),put:$(c,"PUT"),patch:$(c,"PATCH"),delete:$(c,"DELETE")};var c;this.auth.api=o,this.db=new i(o),this.users=new r(o),this.storage=new A(o),this.api=P(()=>({token:this.auth.token||"",apiKey:s,baseUrl:e})),g(()=>this.auth.token),D({onTokenExpired:async()=>{const t=await this.auth.refreshSession();if(t.error)throw t.error;return t.data?.token||null},onUnauthorized:()=>this.auth.handleSessionExpired()})}from(t){return this.db.from(t)}async me(){return this.auth.getUser()}}function K(t,e,s){if(!t||!e)throw new Error("createClient() requiere URL y API Key");return new b({baseUrl:t,apiKey:e,...s})}let O={};function U(t){O={...O,toast:d,...t},N()}function Y(){return{...O}}function N(){try{const{configureApiService:t}=require("../services/ApiService");t(O)}catch(t){}}function x(){O={},N()}async function C(){N()}const R={name:"@dypai-ai/client-sdk",version:"0.0.1",description:"Cliente JavaScript para Dypai Engine",features:["API REST autenticada","Smart Storage (Upload/Download delegado)","Gestión de usuarios (Admin)"]};export{b as DypaiClient,R as PACKAGE_INFO,I as callApi,D as configureApiService,U as configureDypaiServices,P as createApiClient,K as createClient,Y as getGlobalConfig,C as reloadDypaiConfig,x as resetGlobalConfig,h as setToastFunction,g as setTokenProvider,d as toast,f as toastError,w as toastInfo,p as toastSuccess,y as toastWarning,u as useToast};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";class DypaiError extends Error{constructor(t,e=500,s,i){super(t),this.status=e,this.code=s,this.details=i,this.name="DypaiError"}}const localStorageAdapter={getItem:t=>"undefined"==typeof window?null:window.localStorage.getItem(t),setItem:(t,e)=>{if("undefined"!=typeof window)try{window.localStorage.setItem(t,e)}catch(t){console.error("[DYPAI SDK] ❌ Error crítico guardando en localStorage (¿Quota/Permisos?):",t)}},removeItem:t=>{if("undefined"!=typeof window)try{window.localStorage.removeItem(t)}catch(t){}}};async function wrapAuthResponse(t){try{return{data:await t,error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error de autenticación",t.status||400)}}}class AuthModule{constructor(t,e=null){this.config=t,this.t=null,this.i=null,this.o=null,this.h=null,this.l=[],this.u=null,this.p=null,this.storage=t.storage||localStorageAdapter;const s=t.storageKey||this.deriveStorageKey(t.apiKey);console.log(`[DYPAI SDK] 🛠️ Inicializando AuthModule (apiKey: ${t.apiKey?.substring(0,8)}..., storageKey: ${s})`),this.STORAGE_KEY=`dypai-${s}-auth-session`,this.S=this.D().then(()=>{this.i&&this.getUser().catch(()=>{})}),"undefined"!=typeof window&&(window.addEventListener("visibilitychange",this.m.bind(this)),window.addEventListener("focus",this.m.bind(this)),window.addEventListener("storage",t=>{t.key===this.STORAGE_KEY&&(console.log("[DYPAI SDK] 🔄 Sesión actualizada en otra pestaña. Sincronizando..."),this.D(!1))}))}async m(){"undefined"!=typeof document&&"visible"===document.visibilityState&&(console.log("[DYPAI SDK] 👁️ Ventana visible. Sincronizando estado desde storage..."),await this.D(!0))}deriveStorageKey(t){return t?t.substring(0,8):"default"}get user(){return this.t}get token(){return this.i}onAuthStateChange(t){return console.log("[DYPAI SDK] 👂 Nuevo suscriptor añadido a onAuthStateChange"),this.l.push(t),this.S.then(()=>{const e=this.v(),s=e?"SIGNED_IN":"INITIAL_SESSION";console.log(`[DYPAI SDK] 📣 Notificando estado inicial a nuevo suscriptor: ${s} (Sesión activa: ${!!e})`),t(s,e)}).catch(e=>{console.error("[DYPAI SDK] ❌ Error esperando recuperación de sesión para suscriptor:",e),t("INITIAL_SESSION",null)}),{data:{subscription:{unsubscribe:()=>{this.l=this.l.filter(e=>e!==t)}}}}}async signInWithPassword(t){return wrapAuthResponse((async()=>{const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/token?grant_type=password`,s={email:t.email||t.identifier||"",password:t.password},i=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(s)});if(!i.ok){const t=await i.json();throw new DypaiError(t.msg||t.error_description||"Login failed",i.status)}const n=await i.json(),o={token:n.access_token,refreshToken:n.refresh_token,expiresIn:n.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(n.expires_in||3600),user:this.A(n)};return this.T(o),o})())}async login(t){return this.signInWithPassword(t)}async signUp(t){return wrapAuthResponse((async()=>{const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/signup`,s={email:t.email,password:t.password,data:t.user_data||{}},i=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(s)});if(!i.ok){const t=await i.json();throw new DypaiError(t.msg||t.error_description||"Registration failed",i.status)}const n=await i.json(),o={token:n.access_token,refreshToken:n.refresh_token,expiresIn:n.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(n.expires_in||3600),user:this.A(n)};return o.token&&this.T(o),o})())}async register(t){return this.signUp(t)}async getSession(){try{if(!this.i||!this.t)return{data:null,error:null};const t=Math.floor(Date.now()/1e3);return(this.h||0)-t<30&&this.o&&(console.log("[DYPAI SDK] ⏳ getSession: Token próximo a expirar. Refrescando..."),await this.refreshSession().catch(t=>console.warn("Error refreshing session in getSession:",t))),{data:{access_token:this.i,refresh_token:this.o||void 0,token_type:"bearer",user:this.t},error:null}}catch(t){return{data:null,error:new DypaiError(t.message,500)}}}async getUser(){return wrapAuthResponse((async()=>{if(!this.i)throw new DypaiError("No hay sesión activa",401);const t=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,e=await fetch(t,{headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}});if(!e.ok)throw new DypaiError("Session invalid",e.status);const s=await e.json(),i=this.A(s);return this._(i),i})())}async me(){return this.getUser()}async signInWithOAuth(t,e={}){return wrapAuthResponse((async()=>{const{redirectTo:s=window.location.href}=e,i=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/authorize?provider=${t}&redirect_to=${encodeURIComponent(s)}`;window.location.href=i})())}async signOut(){return wrapAuthResponse((async()=>{try{if(this.i){const t=this.config.baseUrl||"http://localhost:8000";await fetch(`${t}/auth/v1/logout`,{method:"POST",headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}})}}finally{this.k()}})())}async logout(){return this.signOut()}async resetPasswordForEmail(t){return wrapAuthResponse((async()=>{const e=this.config.baseUrl||"http://localhost:8000",s=await fetch(`${e}/auth/v1/recover`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t})});if(!s.ok)throw new DypaiError("Recovery failed",s.status);return await s.json()})())}async recoverPassword(t){return this.resetPasswordForEmail(t.email)}async refreshSession(){return this.p?(console.log("[DYPAI SDK] 🔄 Refresco de sesión ya en curso. Esperando resolución..."),this.p):(console.log("[DYPAI SDK] 🔄 Iniciando refresco de sesión con Refresh Token..."),this.p=(async()=>{try{if(!this.o)throw new DypaiError("No hay refresh token disponible",401);const t=`${this.config.baseUrl||"http://localhost:8000"}/api/v0/auth/token?grant_type=refresh_token`,e=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify({refresh_token:this.o})});if(!e.ok){const t=await e.json().catch(()=>({})),s=e.status;throw 400!==s&&401!==s&&403!==s||(console.error("[DYPAI SDK] ❌ El Refresh Token es inválido. Cerrando sesión automáticamente."),this.k()),new DypaiError(t.msg||t.error_description||"Refresh session failed",s)}const s=await e.json(),i={token:s.access_token,refreshToken:s.refresh_token,expiresIn:s.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(s.expires_in||3600),user:this.A(s)};return this.T(i),{data:i,error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error refrescando sesión",401)}}finally{this.p=null}})(),this.p)}async signInWithOtp(t){return wrapAuthResponse((async()=>{const e=this.config.baseUrl||"http://localhost:8000",s=await fetch(`${e}/auth/v1/otp`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(t)});if(!s.ok){const t=await s.json();throw new DypaiError(t.detail||"OTP request failed",s.status)}return await s.json()})())}async verifyOtp(t){return wrapAuthResponse((async()=>{const e=this.config.baseUrl||"http://localhost:8000",s=await fetch(`${e}/auth/v1/verify`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(t)});if(!s.ok){const t=await s.json();throw new DypaiError(t.detail||"OTP verification failed",s.status)}const i=await s.json(),n={token:i.access_token,refreshToken:i.refresh_token,expiresIn:i.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(i.expires_in||3600),user:this.A(i)};return n.token&&this.T(n),n})())}async updateUser(t){return wrapAuthResponse((async()=>{if(!this.i)throw new DypaiError("No hay sesión activa",401);const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,s=await fetch(e,{method:"PUT",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(t)});if(!s.ok){const t=await s.json();throw new DypaiError(t.detail||"Update user failed",s.status)}const i=await s.json(),n=this.A(i);return this._(n),n})())}P(t){this.u&&(clearTimeout(this.u),this.u=null);const e=t-Math.floor(Date.now()/1e3)-60;e>0?(console.log(`[DYPAI SDK] ⏱️ Refresco de sesión programado en ${e} segundos.`),this.u=setTimeout(()=>{this.refreshSession().catch(t=>{console.error("Error in auto-refresh:",t)})},1e3*e)):this.o&&this.refreshSession().catch(()=>{})}A(t){if(!t)return console.warn("[DYPAI SDK] ⚠️ Intentando normalizar un usuario inexistente (data es null/undefined)"),{};const e=t.user||t,s=e.app_metadata||{},i=e.user_metadata||{};return{id:e.id,email:e.email,phone:e.phone,role:s.role||e.role,created_at:e.created_at,updated_at:e.updated_at,confirmed_at:e.confirmed_at,last_sign_in_at:e.last_sign_in_at,app_metadata:s,user_metadata:i,I:{name:s.role||"authenticated",weight:0},$:{app_id:"default"}}}T(t){t.token&&(this.i=t.token,this.o=t.refreshToken||null,this.h=t.expiresAt||null,this._(t.user,t.token,t.refreshToken,t.expiresAt))}async _(t,e,s,i){this.t=t,e&&(this.i=e),void 0!==s&&(this.o=s||null),void 0!==i&&(this.h=i||null);try{if(this.i&&this.t){const t={access_token:this.i,refresh_token:this.o,expires_at:this.h,user:this.t};await this.storage.setItem(this.STORAGE_KEY,JSON.stringify(t))}else console.warn("[DYPAI SDK] ⚠️ _updateUser: No se guardó sesión porque falta token o user.",{hasToken:!!this.i,hasUser:!!this.t})}catch(t){console.error("[DYPAI SDK] ❌ Error guardando sesión en storage:",t)}this.h&&this.P(this.h);const n=e?"SIGNED_IN":"USER_UPDATED";this.C(n)}async k(){console.log("[DYPAI SDK] 🧹 Limpiando sesión del estado y storage."),this.i=null,this.o=null,this.t=null,this.h=null,this.u&&(clearTimeout(this.u),this.u=null);try{await this.storage.removeItem(this.STORAGE_KEY)}catch(t){console.error("[DYPAI SDK] ❌ Error eliminando sesión de storage:",t)}this.C()}async D(t=!0){console.log("[DYPAI SDK] 🔍 Iniciando recuperación de sesión...");try{const e=await this.storage.getItem(this.STORAGE_KEY);if(e){console.log("[DYPAI SDK] ✅ Sesión consolidada encontrada. Restaurando datos...");const t=JSON.parse(e);this.i=t.access_token,this.o=t.refresh_token,this.h=t.expires_at,this.t=t.user}else{console.log("[DYPAI SDK] ℹ️ No hay sesión consolidada. Buscando llaves antiguas para migración...");const t=this.STORAGE_KEY.replace("dypai-","").replace("-auth-session",""),e=await this.storage.getItem(`dypai-auth-token-${t}`),s=await this.storage.getItem(`dypai-auth-user-${t}`);if(!e||!s)return void console.log("[DYPAI SDK] ℹ️ No se encontró ninguna sesión (storage vacío).");{console.log("[DYPAI SDK] 🚚 Migración: Datos antiguos detectados. Consolidando...");const i=JSON.parse(s);this.i=e,this.t=i;const n=await this.storage.getItem(`dypai-auth-refresh-token-${t}`),o=await this.storage.getItem(`dypai-auth-expires-at-${t}`);this.o=n,this.h=o?parseInt(o,10):null,await this._(i,this.i||void 0,this.o||void 0,this.h||void 0);const r=[`dypai-auth-token-${t}`,`dypai-auth-refresh-token-${t}`,`dypai-auth-user-${t}`,`dypai-auth-expires-at-${t}`];for(const t of r)try{await this.storage.removeItem(t)}catch(t){}console.log("[DYPAI SDK] ✨ Migración completada con éxito.")}}const s=Math.floor(Date.now()/1e3),i=this.h&&this.h<=s;console.log(`[DYPAI SDK] 🕒 Token expira en: ${this.h}. Ahora es: ${s}. Diferencia: ${(this.h||0)-s}s`),i&&this.o&&t?(console.log("[DYPAI SDK] ⚠️ El Access Token ha caducado. Intentando refrescar sesión inmediatamente..."),await this.refreshSession().catch(t=>{console.error("[DYPAI SDK] ❌ El refresco inicial falló:",t)})):this.h&&(console.log("[DYPAI SDK] ✨ Sesión válida. Programando refresco futuro."),this.P(this.h)),this.C("SIGNED_IN")}catch(t){console.error("[DYPAI SDK] ❌ Error crítico durante la recuperación de sesión:",t)}}v(){return this.i&&this.t?{access_token:this.i,refresh_token:this.o||void 0,expires_at:this.h||void 0,token_type:"bearer",user:this.t}:null}C(t="USER_UPDATED"){const e=this.v();console.log(`[DYPAI SDK] 📢 Notificando a ${this.l.length} suscriptores: Evento=${t} (Sesión activa: ${!!e})`),this.l.forEach(s=>s(t,e))}handleSessionExpired(){this.k()}}class DataModule{constructor(t){this.api=t}from(t){return new QueryBuilder(t,this.api)}}class QueryBuilder{constructor(t,e){this.table=t,this.api=e}async select(t={}){return this.api.get(this.table,{params:t})}async insert(t){return this.api.post(this.table,t)}async update(t,e){return this.api.patch(`${this.table}/${t}`,e)}async delete(t){return this.api.delete(`${this.table}/${t}`)}}async function wrapResponse(t){try{return{data:await t,error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error desconocido",t.status||500)}}}class UsersModule{constructor(t){this.api=t}async list(t={}){return wrapResponse(this.api.get("admin/users",{params:t}))}async create(t){return wrapResponse(this.api.post("admin/users",{body:t}))}async update(t,e){return wrapResponse(this.api.put(`admin/users/${t}`,{body:e}))}async delete(t){return wrapResponse(this.api.delete(`admin/users/${t}`))}}let customToastFunction=null;const fallbackToast=t=>{const{title:e,description:s,variant:i="default"}=t,n=`${"error"===i?"❌":"success"===i?"✅":"warning"===i?"⚠️":"info"===i?"ℹ️":"📢"} ${e}${s?`: ${s}`:""}`;"error"===i?console.error(n):"warning"===i?console.warn(n):console.log(n)},toast=t=>(customToastFunction||fallbackToast)(t);let serviceConfig={},globalTokenProvider=null;function setTokenProvider(t){globalTokenProvider=t}function configureApiService(t){serviceConfig={...serviceConfig,...t}}const pendingRequests=new Map;function getCompleteConfig(){let t=serviceConfig;try{const{getGlobalConfig:e}=require("../config/global-config");t={...e(),...serviceConfig}}catch(t){}return t}async function callApi(t,e,s,i,n,o,r,a){const c=getCompleteConfig(),h=a||null;if(!h&&!s.startsWith("http"))throw new Error("Base URL no definida. Usa createClient(url, key).");if("string"!=typeof s||!s.trim())throw new Error("Endpoint debe ser un string válido");let l;if(s.startsWith("http"))l=s;else{const t=h.replace(/\/+$/,"");l=s.startsWith("/")?t+s:t+"/api/v0/"+s}if(o&&Object.keys(o).length>0){const t=new URLSearchParams,e=(s,i)=>{null!=i&&(Array.isArray(i)?i.forEach((t,i)=>e(`${s}[${i}]`,t)):"object"==typeof i?Object.entries(i).forEach(([t,i])=>e(`${s}[${t}]`,i)):t.append(s,String(i)))};Object.entries(o).forEach(([t,s])=>e(t,s));const s=t.toString();s&&(l+=`?${s}`)}const u="GET"===e?`${e}:${l}:${JSON.stringify(i)}`:null;if(u&&pendingRequests.has(u))return pendingRequests.get(u);const p=i instanceof FormData,d={...c.headers||{},...p?{}:{"Content-Type":"application/json"},...t&&{Authorization:`Bearer ${t}`},...r&&{"x-api-key":r}},f={method:e,headers:d,credentials:"include"};i&&"GET"!==e&&"DELETE"!==e&&(f.body=p?i:JSON.stringify(i));const y=c.fetch||("undefined"!=typeof window?window.fetch.bind(window):fetch);if(!y)throw new Error("Fetch no disponible.");const w=(async()=>{const t=getCompleteConfig().toast||toast,h=(void 0!==n?n:!1!==c.showToasts)&&t;try{const u=await y(l,f);if(!u.ok){let l,p="Error en la petición";try{const t=await u.text();try{const e=JSON.parse(t);l=e,p=e.message||e.msg||e.error_description||e.error||p}catch{t.length<200&&(p=t)}}catch{}if(401===u.status){if(c.onTokenExpired){console.log("[DYPAI SDK] 🔄 Token caducado detectado. Intentando refresco atómico...");const t=await c.onTokenExpired();if(t)return console.log("[DYPAI SDK] ✅ Token refrescado con éxito. Reintentando petición..."),callApi(t,e,s,i,n,o,r,a)}c.onUnauthorized&&(console.warn("[DYPAI SDK] ❌ No se pudo refrescar la sesión (401 definitivo). Limpiando..."),c.onUnauthorized())}throw h&&t({title:"Error",description:p,variant:"error"}),new DypaiError(p,u.status,void 0,l)}!h||"POST"!==e&&"PUT"!==e&&"PATCH"!==e&&"DELETE"!==e||t({title:"Éxito",description:"Operación completada",variant:"success"});const p=u.headers.get("content-type")||"";return p.includes("application/pdf")||p.includes("image/")||p.includes("audio/")||p.includes("video/")||p.includes("application/octet-stream")||p.includes("application/zip")||p.includes("application/vnd.openxmlformats-officedocument")?await u.blob():p.includes("application/json")?await u.json():await u.text()}finally{u&&pendingRequests.delete(u)}})();return u&&pendingRequests.set(u,w),w}function createMethod(t,e){return async(s,i,n)=>{const o=t();let r,a={};i&&"object"==typeof i&&("token"in i||"params"in i||"apiKey"in i)?(a=i,r=a.body):(r=i,a=n||{});const c=a.token||o.token||(globalTokenProvider?globalTokenProvider():"")||"",h=a.apiKey||o.apiKey;return callApi(c,e,s,r,a.showToasts,a.params,h,o.baseUrl)}}function createApiClient(t){const e=()=>"function"==typeof t?t():t;return{get:createMethodFromCtx(e,"GET"),post:createMethodFromCtx(e,"POST"),put:createMethodFromCtx(e,"PUT"),patch:createMethodFromCtx(e,"PATCH"),delete:createMethodFromCtx(e,"DELETE")}}function createMethodFromCtx(t,e){return async(s,i,n)=>{const o=t(),r=await async function(t,e){let s,i={};t&&"object"==typeof t&&("token"in t||"params"in t||"apiKey"in t)?(i=t,s=i.body):(s=t,i=e||{});let n=i.token;return!n&&globalTokenProvider&&(n=globalTokenProvider()||""),n=n||"",{token:n,apiKey:i.apiKey,body:s,params:i.params,showToasts:i.showToasts}}(i,n),a=r.token||o.token||"",c=r.apiKey||o.apiKey;return callApi(a,e,s,r.body,r.showToasts,r.params,c,o.baseUrl)}}class StorageModule{constructor(t){this.api=t}from(t){return new StorageBucketBuilder(t,this.api)}}class StorageBucketBuilder{constructor(t,e){this.bucketName=t,this.api=e}async upload(t,e,s={}){return wrapStorageResponse(async()=>{const i=s.uploadEndpoint||`storage_upload_${this.bucketName}`,n={file_path:t,...s.params},o=await async function(t,e,s,i){const n=await t.post(e,{file_path:s.name,content_type:s.type||"application/octet-stream",size_bytes:s.size,confirm:!1,...i?.params||{}});if(!n||!n.upload_url)throw new DypaiError("El workflow no devolvió una URL de subida válida (¿falta get_upload_url?)",400);const{upload_url:o,method:r="PUT",headers:a={},file_path:c}=n;i?.onProgress&&i.onProgress(10);const h=await fetch(o,{method:r,headers:{"Content-Type":s.type||"application/octet-stream",...a},body:s});if(!h.ok)throw new DypaiError("Error en la subida directa a la nube",h.status);i?.onProgress&&i.onProgress(90);const l=i?.confirmEndpoint||e,u=await t.post(l,{...i?.params,bucket:n.bucket||i?.params?.bucket,file_path:c,filename:s.name,content_type:s.type||"application/octet-stream",size_bytes:s.size,confirm:!0});return i?.onProgress&&i.onProgress(100),u}(this.api,i,e,{confirmEndpoint:s.confirmEndpoint,params:n,onProgress:s.onProgress});return o})}async download(t,e={}){return wrapStorageResponse(async()=>{const s=e.downloadEndpoint||`storage_download_${this.bucketName}`,i={bucket:this.bucketName,file_path:t,...e.params};await async function(t,e,s,i){const n=i?.method,o=await("GET"===n?t.get(e,{params:i?.params}):t.post(e,s,{params:i?.params}));if(o instanceof Blob){const t=window.URL.createObjectURL(o),e=document.createElement("a");e.href=t,e.download=i?.fileName||"archivo-descargado",document.body.appendChild(e),e.click(),window.URL.revokeObjectURL(t),document.body.removeChild(e)}else if(o&&"object"==typeof o&&("url"in o||"signed_url"in o||"signedUrl"in o)){const t=o.url||o.signed_url||o.signedUrl;"string"==typeof t&&window.open(t,"_blank")}}(this.api,s,i,{fileName:e.fileName,method:e.method||"POST"})})}async createSignedUrl(t,e=15,s={}){return wrapStorageResponse(async()=>{const i=s.endpoint||`storage_signed_url_${this.bucketName}`,n={bucket:this.bucketName,file_path:t,expires_minutes:e,...s.params},o=await this.api.post(i,n);return{signedUrl:o.signed_url||o.signedUrl||o.url,expiresIn:o.expires_in||o.expiresIn||e,path:t}})}getPublicUrl(t){const e=t.replace(/^\/+/,"");return{data:{publicUrl:`${this.api.baseUrl||""}/storage/v1/render/public/${this.bucketName}/${e}`}}}async list(t="",e={}){return wrapStorageResponse(async()=>{const s=e.endpoint||`storage_list_${this.bucketName}`,i={bucket:this.bucketName,prefix:t||e.prefix||"",limit:e.limit,offset:e.offset,sort_by:e.sortBy,order:e.order,search:e.search,...e.params},n=await this.api.post(s,i);return{data:n.data||n.files||[],total:n.total,hasMore:n.has_more||n.hasMore||!1}})}async remove(t,e={}){return wrapStorageResponse(async()=>{const s=e.endpoint||`storage_delete_${this.bucketName}`,i=Array.isArray(t)?t:[t],n={bucket:this.bucketName,paths:i,...e.params};return await this.api.post(s,n),{success:!0}})}async move(t,e,s={}){return wrapStorageResponse(async()=>{const i=s.endpoint||`storage_move_${this.bucketName}`,n={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(i,n),{success:!0}})}async copy(t,e,s={}){return wrapStorageResponse(async()=>{const i=s.endpoint||`storage_copy_${this.bucketName}`,n={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(i,n),{success:!0}})}}async function wrapStorageResponse(t){try{return{data:await t(),error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error de storage",t.status||500)}}}class DypaiClient{constructor(t){const{baseUrl:e,apiKey:s}=t,i=t.auth?.storageKey||t.storageKey;t.global&&function(t){serviceConfig={...serviceConfig,...t}}(t.global),this.auth=new AuthModule({baseUrl:e,apiKey:s,storageKey:i,storage:t.auth?.storage,autoRefreshToken:t.auth?.autoRefreshToken,persistSession:t.auth?.persistSession},null);const n={get:createMethod(o=()=>({token:this.auth.token,apiKey:s,baseUrl:e}),"GET"),post:createMethod(o,"POST"),put:createMethod(o,"PUT"),patch:createMethod(o,"PATCH"),delete:createMethod(o,"DELETE")};var o;this.auth.api=n,this.db=new DataModule(n),this.users=new UsersModule(n),this.storage=new StorageModule(n),this.api=createApiClient(()=>({token:this.auth.token||"",apiKey:s,baseUrl:e})),setTokenProvider(()=>this.auth.token),configureApiService({onTokenExpired:async()=>{const t=await this.auth.refreshSession();return t.data?.token||null},onUnauthorized:()=>this.auth.handleSessionExpired()})}from(t){return this.db.from(t)}async me(){return this.auth.getUser()}}let globalConfig={};function applyConfigToAllServices(){try{const{configureApiService:t}=require("../services/ApiService");t(globalConfig)}catch(t){}}exports.DypaiClient=DypaiClient,exports.PACKAGE_INFO={name:"@dypai-ai/client-sdk",version:"0.0.1",description:"Cliente JavaScript para Dypai Engine",features:["API REST autenticada","Smart Storage (Upload/Download delegado)","Gestión de usuarios (Admin)"]},exports.callApi=callApi,exports.configureApiService=configureApiService,exports.configureDypaiServices=function(t){globalConfig={...globalConfig,toast:toast,...t},applyConfigToAllServices()},exports.createApiClient=createApiClient,exports.createClient=function(t,e,s){if(!t||!e)throw new Error("createClient() requiere URL y API Key");return new DypaiClient({baseUrl:t,apiKey:e,...s})},exports.getGlobalConfig=function(){return{...globalConfig}},exports.reloadDypaiConfig=async function(){applyConfigToAllServices()},exports.resetGlobalConfig=function(){globalConfig={},applyConfigToAllServices()},exports.setToastFunction=function(t){customToastFunction=t},exports.setTokenProvider=setTokenProvider,exports.toast=toast,exports.toastError=(t,e)=>toast({title:t,description:e,variant:"error"}),exports.toastInfo=(t,e)=>toast({title:t,description:e,variant:"info"}),exports.toastSuccess=(t,e)=>toast({title:t,description:e,variant:"success"}),exports.toastWarning=(t,e)=>toast({title:t,description:e,variant:"warning"}),exports.useToast=function(){const t=customToastFunction||fallbackToast;return{toast:t,toastSuccess:(e,s)=>t({title:e,description:s,variant:"success"}),toastError:(e,s)=>t({title:e,description:s,variant:"error"}),toastWarning:(e,s)=>t({title:e,description:s,variant:"warning"}),toastInfo:(e,s)=>t({title:e,description:s,variant:"info"})}};
1
+ "use strict";class DypaiError extends Error{constructor(t,e=500,s,i){super(t),this.status=e,this.code=s,this.details=i,this.name="DypaiError"}}const localStorageAdapter={getItem:t=>"undefined"==typeof window?null:window.localStorage.getItem(t),setItem:(t,e)=>{if("undefined"!=typeof window)try{window.localStorage.setItem(t,e)}catch(t){console.error("[DYPAI SDK] ❌ Error crítico guardando en localStorage (¿Quota/Permisos?):",t)}},removeItem:t=>{if("undefined"!=typeof window)try{window.localStorage.removeItem(t)}catch(t){}}};async function wrapAuthResponse(t){try{return{data:await t,error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error de autenticación",t.status||400)}}}class AuthModule{constructor(t,e=null){this.config=t,this.t=null,this.i=null,this.o=null,this.h=null,this.l=[],this.u=null,this.p=null,this.storage=t.storage||localStorageAdapter;const s=t.storageKey||this.deriveStorageKey(t.apiKey);console.log(`[DYPAI SDK] 🛠️ Inicializando AuthModule (apiKey: ${t.apiKey?.substring(0,8)}..., storageKey: ${s})`),this.STORAGE_KEY=`dypai-${s}-auth-session`,this.S=this.D().then(()=>{this.i&&this.getUser().catch(()=>{})}),"undefined"!=typeof window&&(window.addEventListener("visibilitychange",this.m.bind(this)),window.addEventListener("focus",this.m.bind(this)),window.addEventListener("storage",t=>{t.key===this.STORAGE_KEY&&(console.log("[DYPAI SDK] 🔄 Sesión actualizada en otra pestaña. Sincronizando..."),this.D(!1))}))}async m(){"undefined"!=typeof document&&"visible"===document.visibilityState&&(console.log("[DYPAI SDK] 👁️ Ventana visible. Sincronizando estado desde storage..."),await this.D(!0))}deriveStorageKey(t){return t?t.substring(0,8):"default"}get user(){return this.t}get token(){return this.i}onAuthStateChange(t){return console.log("[DYPAI SDK] 👂 Nuevo suscriptor añadido a onAuthStateChange"),this.l.push(t),this.S.then(()=>{const e=this.v(),s=e?"SIGNED_IN":"INITIAL_SESSION";console.log(`[DYPAI SDK] 📣 Notificando estado inicial a nuevo suscriptor: ${s} (Sesión activa: ${!!e})`),t(s,e)}).catch(e=>{console.error("[DYPAI SDK] ❌ Error esperando recuperación de sesión para suscriptor:",e),t("INITIAL_SESSION",null)}),{data:{subscription:{unsubscribe:()=>{this.l=this.l.filter(e=>e!==t)}}}}}async signInWithPassword(t){return wrapAuthResponse((async()=>{const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/token?grant_type=password`,s={email:t.email||t.identifier||"",password:t.password},i=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(s)});if(!i.ok){const t=await i.json();throw new DypaiError(t.msg||t.error_description||"Login failed",i.status)}const n=await i.json(),o={token:n.access_token,refreshToken:n.refresh_token,expiresIn:n.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(n.expires_in||3600),user:this.A(n)};return this.T(o),o})())}async login(t){return this.signInWithPassword(t)}async signUp(t){return wrapAuthResponse((async()=>{const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/signup`,s={email:t.email,password:t.password,data:t.user_data||{}},i=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(s)});if(!i.ok){const t=await i.json();throw new DypaiError(t.msg||t.error_description||"Registration failed",i.status)}const n=await i.json(),o={token:n.access_token,refreshToken:n.refresh_token,expiresIn:n.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(n.expires_in||3600),user:this.A(n)};return o.token&&this.T(o),o})())}async register(t){return this.signUp(t)}async getSession(){try{if(!this.i||!this.t)return{data:null,error:null};const t=Math.floor(Date.now()/1e3);return(this.h||0)-t<30&&this.o&&(console.log("[DYPAI SDK] ⏳ getSession: Token próximo a expirar. Refrescando..."),await this.refreshSession().catch(t=>console.warn("Error refreshing session in getSession:",t))),{data:{access_token:this.i,refresh_token:this.o||void 0,token_type:"bearer",user:this.t},error:null}}catch(t){return{data:null,error:new DypaiError(t.message,500)}}}async getUser(){return wrapAuthResponse((async()=>{if(!this.i)throw new DypaiError("No hay sesión activa",401);const t=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,e=await fetch(t,{headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}});if(!e.ok)throw new DypaiError("Session invalid",e.status);const s=await e.json(),i=this.A(s);return this._(i),i})())}async me(){return this.getUser()}async signInWithOAuth(t,e={}){return wrapAuthResponse((async()=>{const{redirectTo:s=window.location.href}=e,i=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/authorize?provider=${t}&redirect_to=${encodeURIComponent(s)}`;window.location.href=i})())}async signOut(){return wrapAuthResponse((async()=>{try{if(this.i){const t=this.config.baseUrl||"http://localhost:8000";await fetch(`${t}/auth/v1/logout`,{method:"POST",headers:{Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}}})}}finally{this.k("signOut called")}})())}async logout(){return this.signOut()}async resetPasswordForEmail(t){return wrapAuthResponse((async()=>{const e=this.config.baseUrl||"http://localhost:8000",s=await fetch(`${e}/auth/v1/recover`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t})});if(!s.ok)throw new DypaiError("Recovery failed",s.status);return await s.json()})())}async recoverPassword(t){return this.resetPasswordForEmail(t.email)}async refreshSession(){return this.p?(console.log("[DYPAI SDK] 🔄 Refresco de sesión ya en curso. Esperando resolución..."),this.p):(console.log("[DYPAI SDK] 🔄 Iniciando refresco de sesión con Refresh Token..."),this.p=(async()=>{try{if(!this.o)throw new DypaiError("No hay refresh token disponible",401);const t=`${this.config.baseUrl||"http://localhost:8000"}/api/v0/auth/token?grant_type=refresh_token`,e=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify({refresh_token:this.o})});if(!e.ok){const t=await e.json().catch(()=>({})),s=e.status,i=t.error||t.code||"unknown";throw 400===s||401===s||403===s?(console.error(`[DYPAI SDK] ❌ Error terminal en refresco (${s}: ${i}). Limpiando sesión.`),this.k(`refreshSession failed (${s}: ${i})`)):console.warn(`[DYPAI SDK] ⚠️ Error no-terminal en refresco (${s}). Manteniendo sesión.`),new DypaiError(t.msg||t.error_description||"Refresh session failed",s)}const s=await e.json(),i={token:s.access_token,refreshToken:s.refresh_token,expiresIn:s.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(s.expires_in||3600),user:this.A(s)};return this.T(i),{data:i,error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error refrescando sesión",401)}}finally{this.p=null}})(),this.p)}async signInWithOtp(t){return wrapAuthResponse((async()=>{const e=this.config.baseUrl||"http://localhost:8000",s=await fetch(`${e}/auth/v1/otp`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(t)});if(!s.ok){const t=await s.json();throw new DypaiError(t.detail||"OTP request failed",s.status)}return await s.json()})())}async verifyOtp(t){return wrapAuthResponse((async()=>{const e=this.config.baseUrl||"http://localhost:8000",s=await fetch(`${e}/auth/v1/verify`,{method:"POST",headers:{"Content-Type":"application/json",...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(t)});if(!s.ok){const t=await s.json();throw new DypaiError(t.detail||"OTP verification failed",s.status)}const i=await s.json(),n={token:i.access_token,refreshToken:i.refresh_token,expiresIn:i.expires_in,expiresAt:Math.floor(Date.now()/1e3)+(i.expires_in||3600),user:this.A(i)};return n.token&&this.T(n),n})())}async updateUser(t){return wrapAuthResponse((async()=>{if(!this.i)throw new DypaiError("No hay sesión activa",401);const e=`${this.config.baseUrl||"http://localhost:8000"}/auth/v1/user`,s=await fetch(e,{method:"PUT",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.i}`,...this.config.apiKey&&{apikey:this.config.apiKey}},body:JSON.stringify(t)});if(!s.ok){const t=await s.json();throw new DypaiError(t.detail||"Update user failed",s.status)}const i=await s.json(),n=this.A(i);return this._(n),n})())}P(t){this.u&&(clearTimeout(this.u),this.u=null);const e=t-Math.floor(Date.now()/1e3)-60;e>0?(console.log(`[DYPAI SDK] ⏱️ Refresco de sesión programado en ${e} segundos.`),this.u=setTimeout(()=>{this.refreshSession().catch(t=>{console.error("Error in auto-refresh:",t)})},1e3*e)):this.o&&this.refreshSession().catch(()=>{})}A(t){if(!t)return console.warn("[DYPAI SDK] ⚠️ Intentando normalizar un usuario inexistente (data es null/undefined)"),{};const e=t.user||t,s=e.app_metadata||{},i=e.user_metadata||{};return{id:e.id,email:e.email,phone:e.phone,role:s.role||e.role,created_at:e.created_at,updated_at:e.updated_at,confirmed_at:e.confirmed_at,last_sign_in_at:e.last_sign_in_at,app_metadata:s,user_metadata:i,I:{name:s.role||"authenticated",weight:0},$:{app_id:"default"}}}T(t){t.token&&(this.i=t.token,this.o=t.refreshToken||null,this.h=t.expiresAt||null,this._(t.user,t.token,t.refreshToken,t.expiresAt))}async _(t,e,s,i){this.t=t,e&&(this.i=e),void 0!==s&&(this.o=s||null),void 0!==i&&(this.h=i||null);try{if(this.i&&this.t){const t={access_token:this.i,refresh_token:this.o,expires_at:this.h,user:this.t};await this.storage.setItem(this.STORAGE_KEY,JSON.stringify(t))}else console.warn("[DYPAI SDK] ⚠️ _updateUser: No se guardó sesión porque falta token o user.",{hasToken:!!this.i,hasUser:!!this.t})}catch(t){console.error("[DYPAI SDK] ❌ Error guardando sesión en storage:",t)}this.h&&this.P(this.h);const n=e?"SIGNED_IN":"USER_UPDATED";this.C(n)}async k(t="unknown"){console.log(`[DYPAI SDK] 🧹 Limpiando sesión del estado y storage. Motivo: ${t}`),this.i=null,this.o=null,this.t=null,this.h=null,this.u&&(clearTimeout(this.u),this.u=null);try{await this.storage.removeItem(this.STORAGE_KEY),console.log("[DYPAI SDK] ✅ Storage limpiado con éxito.")}catch(t){console.error("[DYPAI SDK] ❌ Error eliminando sesión de storage:",t)}this.C()}async D(t=!0){console.log("[DYPAI SDK] 🔍 Iniciando recuperación de sesión...");try{const e=await this.storage.getItem(this.STORAGE_KEY);if(e){console.log("[DYPAI SDK] ✅ Sesión consolidada encontrada. Restaurando datos...");const t=JSON.parse(e);this.i=t.access_token,this.o=t.refresh_token,this.h=t.expires_at,this.t=t.user}else{console.log("[DYPAI SDK] ℹ️ No hay sesión consolidada. Buscando llaves antiguas para migración...");const t=this.STORAGE_KEY.replace("dypai-","").replace("-auth-session",""),e=await this.storage.getItem(`dypai-auth-token-${t}`),s=await this.storage.getItem(`dypai-auth-user-${t}`);if(!e||!s)return void console.log("[DYPAI SDK] ℹ️ No se encontró ninguna sesión (storage vacío).");{console.log("[DYPAI SDK] 🚚 Migración: Datos antiguos detectados. Consolidando...");const i=JSON.parse(s);this.i=e,this.t=i;const n=await this.storage.getItem(`dypai-auth-refresh-token-${t}`),o=await this.storage.getItem(`dypai-auth-expires-at-${t}`);this.o=n,this.h=o?parseInt(o,10):null,await this._(i,this.i||void 0,this.o||void 0,this.h||void 0);const r=[`dypai-auth-token-${t}`,`dypai-auth-refresh-token-${t}`,`dypai-auth-user-${t}`,`dypai-auth-expires-at-${t}`];for(const t of r)try{await this.storage.removeItem(t)}catch(t){}console.log("[DYPAI SDK] ✨ Migración completada con éxito.")}}const s=Math.floor(Date.now()/1e3),i=this.h&&this.h<=s;console.log(`[DYPAI SDK] 🕒 Token expira en: ${this.h}. Ahora es: ${s}. Diferencia: ${(this.h||0)-s}s`),i&&this.o&&t?(console.log("[DYPAI SDK] ⚠️ El Access Token ha caducado. Intentando refrescar sesión inmediatamente..."),await this.refreshSession().catch(t=>{console.error("[DYPAI SDK] ❌ El refresco inicial falló:",t)})):this.h&&(console.log("[DYPAI SDK] ✨ Sesión válida. Programando refresco futuro."),this.P(this.h)),this.C("SIGNED_IN")}catch(t){console.error("[DYPAI SDK] ❌ Error crítico durante la recuperación de sesión:",t)}}v(){return this.i&&this.t?{access_token:this.i,refresh_token:this.o||void 0,expires_at:this.h||void 0,token_type:"bearer",user:this.t}:null}C(t="USER_UPDATED"){const e=this.v();console.log(`[DYPAI SDK] 📢 Notificando a ${this.l.length} suscriptores: Evento=${t} (Sesión activa: ${!!e})`),this.l.forEach(s=>s(t,e))}handleSessionExpired(){this.k("handleSessionExpired called (likely 401 from API)")}}class DataModule{constructor(t){this.api=t}from(t){return new QueryBuilder(t,this.api)}}class QueryBuilder{constructor(t,e){this.table=t,this.api=e}async select(t={}){return this.api.get(this.table,{params:t})}async insert(t){return this.api.post(this.table,t)}async update(t,e){return this.api.patch(`${this.table}/${t}`,e)}async delete(t){return this.api.delete(`${this.table}/${t}`)}}async function wrapResponse(t){try{return{data:await t,error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error desconocido",t.status||500)}}}class UsersModule{constructor(t){this.api=t}async list(t={}){return wrapResponse(this.api.get("admin/users",{params:t}))}async create(t){return wrapResponse(this.api.post("admin/users",{body:t}))}async update(t,e){return wrapResponse(this.api.put(`admin/users/${t}`,{body:e}))}async delete(t){return wrapResponse(this.api.delete(`admin/users/${t}`))}}let customToastFunction=null;const fallbackToast=t=>{const{title:e,description:s,variant:i="default"}=t,n=`${"error"===i?"❌":"success"===i?"✅":"warning"===i?"⚠️":"info"===i?"ℹ️":"📢"} ${e}${s?`: ${s}`:""}`;"error"===i?console.error(n):"warning"===i?console.warn(n):console.log(n)},toast=t=>(customToastFunction||fallbackToast)(t);let serviceConfig={},globalTokenProvider=null;function setTokenProvider(t){globalTokenProvider=t}function configureApiService(t){serviceConfig={...serviceConfig,...t}}const pendingRequests=new Map;function getCompleteConfig(){let t=serviceConfig;try{const{getGlobalConfig:e}=require("../config/global-config");t={...e(),...serviceConfig}}catch(t){}return t}async function callApi(t,e,s,i,n,o,r,a){const c=getCompleteConfig(),h=a||null;if(!h&&!s.startsWith("http"))throw new Error("Base URL no definida. Usa createClient(url, key).");if("string"!=typeof s||!s.trim())throw new Error("Endpoint debe ser un string válido");let l;if(s.startsWith("http"))l=s;else{const t=h.replace(/\/+$/,"");l=s.startsWith("/")?t+s:t+"/api/v0/"+s}if(o&&Object.keys(o).length>0){const t=new URLSearchParams,e=(s,i)=>{null!=i&&(Array.isArray(i)?i.forEach((t,i)=>e(`${s}[${i}]`,t)):"object"==typeof i?Object.entries(i).forEach(([t,i])=>e(`${s}[${t}]`,i)):t.append(s,String(i)))};Object.entries(o).forEach(([t,s])=>e(t,s));const s=t.toString();s&&(l+=`?${s}`)}const u="GET"===e?`${e}:${l}:${JSON.stringify(i)}`:null;if(u&&pendingRequests.has(u))return pendingRequests.get(u);const p=i instanceof FormData,d={...c.headers||{},...p?{}:{"Content-Type":"application/json"},...t&&{Authorization:`Bearer ${t}`},...r&&{"x-api-key":r}},f={method:e,headers:d,credentials:"include"};i&&"GET"!==e&&"DELETE"!==e&&(f.body=p?i:JSON.stringify(i));const y=c.fetch||("undefined"!=typeof window?window.fetch.bind(window):fetch);if(!y)throw new Error("Fetch no disponible.");const w=(async()=>{const t=getCompleteConfig().toast||toast,h=(void 0!==n?n:!1!==c.showToasts)&&t;try{const u=await y(l,f);if(!u.ok){let l,p="Error en la petición";try{const t=await u.text();try{const e=JSON.parse(t);l=e,p=e.message||e.msg||e.error_description||e.error||p}catch{t.length<200&&(p=t)}}catch{}if(401===u.status&&c.onTokenExpired){console.log("[DYPAI SDK] 🔄 Token caducado detectado. Intentando refresco atómico...");try{const t=await c.onTokenExpired();if(t)return console.log("[DYPAI SDK] ✅ Token refrescado con éxito. Reintentando petición..."),callApi(t,e,s,i,n,o,r,a)}catch(t){console.error("[DYPAI SDK] ❌ Error durante el intento de refresco:",t)}}throw h&&t({title:"Error",description:p,variant:"error"}),new DypaiError(p,u.status,void 0,l)}!h||"POST"!==e&&"PUT"!==e&&"PATCH"!==e&&"DELETE"!==e||t({title:"Éxito",description:"Operación completada",variant:"success"});const p=u.headers.get("content-type")||"";return p.includes("application/pdf")||p.includes("image/")||p.includes("audio/")||p.includes("video/")||p.includes("application/octet-stream")||p.includes("application/zip")||p.includes("application/vnd.openxmlformats-officedocument")?await u.blob():p.includes("application/json")?await u.json():await u.text()}finally{u&&pendingRequests.delete(u)}})();return u&&pendingRequests.set(u,w),w}function createMethod(t,e){return async(s,i,n)=>{const o=t();let r,a={};i&&"object"==typeof i&&("token"in i||"params"in i||"apiKey"in i)?(a=i,r=a.body):(r=i,a=n||{});const c=a.token||o.token||(globalTokenProvider?globalTokenProvider():"")||"",h=a.apiKey||o.apiKey;return callApi(c,e,s,r,a.showToasts,a.params,h,o.baseUrl)}}function createApiClient(t){const e=()=>"function"==typeof t?t():t;return{get:createMethodFromCtx(e,"GET"),post:createMethodFromCtx(e,"POST"),put:createMethodFromCtx(e,"PUT"),patch:createMethodFromCtx(e,"PATCH"),delete:createMethodFromCtx(e,"DELETE")}}function createMethodFromCtx(t,e){return async(s,i,n)=>{const o=t(),r=await async function(t,e){let s,i={};t&&"object"==typeof t&&("token"in t||"params"in t||"apiKey"in t)?(i=t,s=i.body):(s=t,i=e||{});let n=i.token;return!n&&globalTokenProvider&&(n=globalTokenProvider()||""),n=n||"",{token:n,apiKey:i.apiKey,body:s,params:i.params,showToasts:i.showToasts}}(i,n),a=r.token||o.token||"",c=r.apiKey||o.apiKey;return callApi(a,e,s,r.body,r.showToasts,r.params,c,o.baseUrl)}}class StorageModule{constructor(t){this.api=t}from(t){return new StorageBucketBuilder(t,this.api)}}class StorageBucketBuilder{constructor(t,e){this.bucketName=t,this.api=e}async upload(t,e,s={}){return wrapStorageResponse(async()=>{const i=s.uploadEndpoint||`storage_upload_${this.bucketName}`,n={file_path:t,...s.params},o=await async function(t,e,s,i){const n=await t.post(e,{file_path:s.name,content_type:s.type||"application/octet-stream",size_bytes:s.size,confirm:!1,...i?.params||{}});if(!n||!n.upload_url)throw new DypaiError("El workflow no devolvió una URL de subida válida (¿falta get_upload_url?)",400);const{upload_url:o,method:r="PUT",headers:a={},file_path:c}=n;i?.onProgress&&i.onProgress(10);const h=await fetch(o,{method:r,headers:{"Content-Type":s.type||"application/octet-stream",...a},body:s});if(!h.ok)throw new DypaiError("Error en la subida directa a la nube",h.status);i?.onProgress&&i.onProgress(90);const l=i?.confirmEndpoint||e,u=await t.post(l,{...i?.params,bucket:n.bucket||i?.params?.bucket,file_path:c,filename:s.name,content_type:s.type||"application/octet-stream",size_bytes:s.size,confirm:!0});return i?.onProgress&&i.onProgress(100),u}(this.api,i,e,{confirmEndpoint:s.confirmEndpoint,params:n,onProgress:s.onProgress});return o})}async download(t,e={}){return wrapStorageResponse(async()=>{const s=e.downloadEndpoint||`storage_download_${this.bucketName}`,i={bucket:this.bucketName,file_path:t,...e.params};await async function(t,e,s,i){const n=i?.method,o=await("GET"===n?t.get(e,{params:i?.params}):t.post(e,s,{params:i?.params}));if(o instanceof Blob){const t=window.URL.createObjectURL(o),e=document.createElement("a");e.href=t,e.download=i?.fileName||"archivo-descargado",document.body.appendChild(e),e.click(),window.URL.revokeObjectURL(t),document.body.removeChild(e)}else if(o&&"object"==typeof o&&("url"in o||"signed_url"in o||"signedUrl"in o)){const t=o.url||o.signed_url||o.signedUrl;"string"==typeof t&&window.open(t,"_blank")}}(this.api,s,i,{fileName:e.fileName,method:e.method||"POST"})})}async createSignedUrl(t,e=15,s={}){return wrapStorageResponse(async()=>{const i=s.endpoint||`storage_signed_url_${this.bucketName}`,n={bucket:this.bucketName,file_path:t,expires_minutes:e,...s.params},o=await this.api.post(i,n);return{signedUrl:o.signed_url||o.signedUrl||o.url,expiresIn:o.expires_in||o.expiresIn||e,path:t}})}getPublicUrl(t){const e=t.replace(/^\/+/,"");return{data:{publicUrl:`${this.api.baseUrl||""}/storage/v1/render/public/${this.bucketName}/${e}`}}}async list(t="",e={}){return wrapStorageResponse(async()=>{const s=e.endpoint||`storage_list_${this.bucketName}`,i={bucket:this.bucketName,prefix:t||e.prefix||"",limit:e.limit,offset:e.offset,sort_by:e.sortBy,order:e.order,search:e.search,...e.params},n=await this.api.post(s,i);return{data:n.data||n.files||[],total:n.total,hasMore:n.has_more||n.hasMore||!1}})}async remove(t,e={}){return wrapStorageResponse(async()=>{const s=e.endpoint||`storage_delete_${this.bucketName}`,i=Array.isArray(t)?t:[t],n={bucket:this.bucketName,paths:i,...e.params};return await this.api.post(s,n),{success:!0}})}async move(t,e,s={}){return wrapStorageResponse(async()=>{const i=s.endpoint||`storage_move_${this.bucketName}`,n={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(i,n),{success:!0}})}async copy(t,e,s={}){return wrapStorageResponse(async()=>{const i=s.endpoint||`storage_copy_${this.bucketName}`,n={bucket:this.bucketName,from_path:t,to_path:e,...s.params};return await this.api.post(i,n),{success:!0}})}}async function wrapStorageResponse(t){try{return{data:await t(),error:null}}catch(t){return{data:null,error:t instanceof DypaiError?t:new DypaiError(t.message||"Error de storage",t.status||500)}}}class DypaiClient{constructor(t){const{baseUrl:e,apiKey:s}=t,i=t.auth?.storageKey||t.storageKey;t.global&&function(t){serviceConfig={...serviceConfig,...t}}(t.global),this.auth=new AuthModule({baseUrl:e,apiKey:s,storageKey:i,storage:t.auth?.storage,autoRefreshToken:t.auth?.autoRefreshToken,persistSession:t.auth?.persistSession},null);const n={get:createMethod(o=()=>({token:this.auth.token,apiKey:s,baseUrl:e}),"GET"),post:createMethod(o,"POST"),put:createMethod(o,"PUT"),patch:createMethod(o,"PATCH"),delete:createMethod(o,"DELETE")};var o;this.auth.api=n,this.db=new DataModule(n),this.users=new UsersModule(n),this.storage=new StorageModule(n),this.api=createApiClient(()=>({token:this.auth.token||"",apiKey:s,baseUrl:e})),setTokenProvider(()=>this.auth.token),configureApiService({onTokenExpired:async()=>{const t=await this.auth.refreshSession();if(t.error)throw t.error;return t.data?.token||null},onUnauthorized:()=>this.auth.handleSessionExpired()})}from(t){return this.db.from(t)}async me(){return this.auth.getUser()}}let globalConfig={};function applyConfigToAllServices(){try{const{configureApiService:t}=require("../services/ApiService");t(globalConfig)}catch(t){}}exports.DypaiClient=DypaiClient,exports.PACKAGE_INFO={name:"@dypai-ai/client-sdk",version:"0.0.1",description:"Cliente JavaScript para Dypai Engine",features:["API REST autenticada","Smart Storage (Upload/Download delegado)","Gestión de usuarios (Admin)"]},exports.callApi=callApi,exports.configureApiService=configureApiService,exports.configureDypaiServices=function(t){globalConfig={...globalConfig,toast:toast,...t},applyConfigToAllServices()},exports.createApiClient=createApiClient,exports.createClient=function(t,e,s){if(!t||!e)throw new Error("createClient() requiere URL y API Key");return new DypaiClient({baseUrl:t,apiKey:e,...s})},exports.getGlobalConfig=function(){return{...globalConfig}},exports.reloadDypaiConfig=async function(){applyConfigToAllServices()},exports.resetGlobalConfig=function(){globalConfig={},applyConfigToAllServices()},exports.setToastFunction=function(t){customToastFunction=t},exports.setTokenProvider=setTokenProvider,exports.toast=toast,exports.toastError=(t,e)=>toast({title:t,description:e,variant:"error"}),exports.toastInfo=(t,e)=>toast({title:t,description:e,variant:"info"}),exports.toastSuccess=(t,e)=>toast({title:t,description:e,variant:"success"}),exports.toastWarning=(t,e)=>toast({title:t,description:e,variant:"warning"}),exports.useToast=function(){const t=customToastFunction||fallbackToast;return{toast:t,toastSuccess:(e,s)=>t({title:e,description:s,variant:"success"}),toastError:(e,s)=>t({title:e,description:s,variant:"error"}),toastWarning:(e,s)=>t({title:e,description:s,variant:"warning"}),toastInfo:(e,s)=>t({title:e,description:s,variant:"info"})}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dypai-ai/client-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Cliente JavaScript para Dypai Engine",
5
5
  "type": "module",
6
6
  "private": false,