@ledewire/browser 0.9.0 → 0.10.0

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.
@@ -1,2 +1,2 @@
1
- var Ledewire=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){return atob(e)}function n(e){return{...e,...typeof e.content_body==`string`&&{content_body:t(e.content_body)},...typeof e.teaser==`string`&&{teaser:t(e.teaser)}}}function r(e){"@babel/helpers - typeof";return r=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},r(e)}function i(e,t){if(r(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var i=n.call(e,t||`default`);if(r(i)!=`object`)return i;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function a(e){var t=i(e,`string`);return r(t)==`symbol`?t:t+``}function o(e,t,n){return(t=a(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s,c=Symbol.for(`@ledewire/error-brands`);s=Symbol.hasInstance;var l=class extends Error{constructor(e,t,n,r,i){super(e),o(this,`statusCode`,void 0),o(this,`code`,void 0),o(this,`type`,void 0),o(this,`details`,void 0),this.name=`LedewireError`,this.statusCode=t,this.code=n,this.type=r,this.details=i,Object.setPrototypeOf(this,new.target.prototype);let a=[],s=new.target;for(;typeof s==`function`;){let e=s.brand;typeof e==`string`&&Object.prototype.hasOwnProperty.call(s,`brand`)&&a.push(e),s=Object.getPrototypeOf(s)}Object.defineProperty(this,c,{value:a,enumerable:!1,configurable:!0})}static[s](e){if(Function.prototype[Symbol.hasInstance].call(this,e))return!0;if(!e||typeof e!=`object`&&typeof e!=`function`)return!1;let t=e[c];return Array.isArray(t)&&t.includes(this.brand)}};o(l,`brand`,`LedewireError`);var u=class extends l{constructor(e,t,n,r){super(e,401,t,n,r),this.name=`AuthError`}};o(u,`brand`,`AuthError`);var d=class extends l{constructor(e,t,n,r){super(e,403,t,n,r),this.name=`ForbiddenError`}};o(d,`brand`,`ForbiddenError`);var f=class extends l{constructor(e,t,n,r){super(e,404,t,n,r),this.name=`NotFoundError`}};o(f,`brand`,`NotFoundError`);var p=class extends l{constructor(e,t,n,r,i){super(e,t,n,r,i),this.name=`PurchaseError`}};o(p,`brand`,`PurchaseError`);var m=class extends l{constructor(e,t,n){super(e,402,n,`daily_spend_cap_reached`),o(this,`capCents`,void 0),o(this,`spentCents`,void 0),o(this,`remainingCents`,void 0),o(this,`resetsAt`,void 0),o(this,`bulkExempt`,void 0),this.name=`SpendCapReachedError`,this.capCents=t.capCents,this.spentCents=t.spentCents,this.remainingCents=t.remainingCents,this.resetsAt=t.resetsAt,this.bulkExempt=t.bulkExempt,Object.setPrototypeOf(this,new.target.prototype)}};o(m,`brand`,`SpendCapReachedError`);function h(e){if(!e||typeof e!=`object`)return null;let t=e,n=t.error;if(!n||typeof n!=`object`)return null;let r=n;if(r.type!==`daily_spend_cap_reached`)return null;let i=t.cap_cents,a=t.spent_cents,o=t.remaining_cents,s=t.resets_at,c=t.bulk_exempt;if(typeof i!=`number`||typeof a!=`number`||typeof o!=`number`||typeof s!=`string`||typeof c!=`boolean`)return null;let l=typeof r.message==`string`?r.message:typeof t.message==`string`?t.message:`Daily spend cap reached.`,u=typeof r.code==`number`?r.code:void 0;return new m(l,{capCents:i,spentCents:a,remainingCents:o,resetsAt:s,bulkExempt:c},u)}function g(e,t){if(!e||typeof e!=`object`||Array.isArray(e))return{message:t,code:void 0,type:void 0,details:void 0};let{error:n,...r}=e,i=Object.keys(r).length>0?r:void 0;if(typeof n==`string`)return{message:n,code:void 0,type:void 0,details:i};if(!n||typeof n!=`object`||Array.isArray(n))return{message:t,code:void 0,type:void 0,details:i};let a=n;return{message:typeof a.message==`string`?a.message:t,code:typeof a.code==`number`?a.code:void 0,type:typeof a.type==`string`?a.type:void 0,details:i}}var _=class{constructor(e={}){o(this,`baseUrl`,void 0),o(this,`getAccessToken`,void 0),o(this,`onUnauthorized`,void 0),this.baseUrl=(e.baseUrl??`https://api.ledewire.com`).replace(/\/$/,``),this.getAccessToken=e.getAccessToken??(()=>null),this.onUnauthorized=e.onUnauthorized??(()=>null)}async get(e,t,n){return this.request(`GET`,this.buildUrl(e,t),void 0,n)}async post(e,t,n){return this.request(`POST`,this.buildUrl(e,n),t)}async put(e,t){return this.request(`PUT`,this.buildUrl(e),t)}async patch(e,t){return this.request(`PATCH`,this.buildUrl(e),t)}async delete(e){return this.request(`DELETE`,this.buildUrl(e))}async getRaw(e){return this.performRequest(`GET`,this.buildUrl(e),{Accept:`application/gzip, application/json`})}buildUrl(e,t){let n=`${this.baseUrl}${e}`;if(!t)return n;let r=Object.entries(t).filter(e=>e[1]!==void 0);return r.length===0?n:`${n}?${new URLSearchParams(r.map(([e,t])=>[e,String(t)])).toString()}`}async request(e,t,n,r){let i=await this.performRequest(e,t,{"Content-Type":`application/json`,Accept:`application/json`},n,!1,r);if(i.status!==204)return i.json()}async performRequest(e,t,n,r,i=!1,a){let o=a?.auth!==!1,s=o?await this.getAccessToken():null,c={...n};s&&(c.Authorization=`Bearer ${s}`);let l=await fetch(t,{method:e,headers:c,...r!==void 0&&{body:JSON.stringify(r)}});if(l.status===401&&!i){if(o&&await this.onUnauthorized())return this.performRequest(e,t,n,r,!0,a);throw new u(`Session expired. Please re-authenticate.`)}return l.ok||await this.throwApiError(l),l}async throwApiError(e){let t;try{t=await e.json()}catch{}let n=h(t);if(n)throw n;let{message:r,code:i,type:a,details:o}=g(t,e.statusText);switch(e.status){case 401:throw new u(r,i,a,o);case 403:throw new d(r,i,a,o);case 404:throw new f(r,i,a,o);default:throw new l(r,e.status,i,a,o)}}},v=6e4,y=class{constructor(e){o(this,`storage`,void 0),o(this,`refreshFn`,void 0),o(this,`onTokenRefreshed`,void 0),o(this,`onAuthExpired`,void 0),o(this,`refreshPromise`,null),this.storage=e.storage,this.refreshFn=e.refreshFn,this.onTokenRefreshed=e.onTokenRefreshed,this.onAuthExpired=e.onAuthExpired}async getAccessToken(){let e=await this.storage.getTokens();return e?e.expiresAt-Date.now()<v?(await this.performRefresh(e.refreshToken))?.accessToken??null:e.accessToken:null}async handleUnauthorized(){let e=await this.storage.getTokens();if(!e)return await this.onAuthExpired?.(),null;let t=await this.performRefresh(e.refreshToken);return t||await this.onAuthExpired?.(),t?.accessToken??null}async setTokens(e){await this.storage.setTokens(e)}async clearTokens(){await this.storage.clearTokens()}performRefresh(e){return this.refreshPromise||(this.refreshPromise=this.refreshFn(e).then(async e=>(await this.storage.setTokens(e),await this.onTokenRefreshed?.(e),e)).catch(e=>{throw this.storage.clearTokens(),new u(`Token refresh failed. Please re-authenticate.`)}).finally(()=>{this.refreshPromise=null})),this.refreshPromise}},b=class{constructor(){o(this,`tokens`,null)}getTokens(){return this.tokens}setTokens(e){this.tokens=e}clearTokens(){this.tokens=null}};function x(e){return new Date(e).getTime()}function S(e){return async t=>{let n=await fetch(`${e}/v1/auth/token/refresh`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({refresh_token:t})});if(!n.ok)throw Error(`Token refresh failed`);let r=await n.json();return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresAt:x(r.expires_at)}}}var C=class{constructor(e,t){o(this,`http`,void 0),o(this,`tokenManager`,void 0),this.http=e,this.tokenManager=t}async signup(e){let t=await this.http.post(`/v1/auth/signup`,e);return await this.storeTokens(t),t}async loginWithEmail(e){let t=await this.http.post(`/v1/auth/login/email`,e);return await this.storeTokens(t),t}async loginWithGoogle(e){let t=await this.http.post(`/v1/auth/login/google`,e);return await this.storeTokens(t),t}async loginWithBuyerApiKey(e){let t=await this.http.post(`/v1/auth/login/buyer-api-key`,e);return await this.storeTokens(t),t}async requestPasswordReset(e){return this.http.post(`/v1/auth/password/reset-request`,e)}async resetPassword(e){return this.http.post(`/v1/auth/password/reset`,e)}async storeTokens(e){await this.tokenManager.setTokens({accessToken:e.access_token,refreshToken:e.refresh_token,expiresAt:x(e.expires_at)})}},w=class{constructor(e){o(this,`http`,void 0),this.http=e}async getPublic(){return this.http.get(`/v1/config/public`)}},T=class{constructor(e){o(this,`http`,void 0),this.http=e}async getWithAccess(e){return n(await this.http.get(`/v1/content/${encodeURIComponent(e)}/with-access`))}},E=class{constructor(e){o(this,`http`,void 0),this.http=e}async create(e){return this.http.post(`/v1/purchases`,e)}async list(){return this.http.get(`/v1/purchases`)}async get(e){return this.http.get(`/v1/purchases/${encodeURIComponent(e)}`)}},D=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return(await this.http.get(`/v1/seller/content`)).map(n)}async search(e){return(await this.http.post(`/v1/seller/content/search`,e)).map(n)}async get(e){return n(await this.http.get(`/v1/seller/content/${encodeURIComponent(e)}`))}},O=class{constructor(e,t){o(this,`http`,void 0),o(this,`tokenManager`,void 0),o(this,`content`,void 0),this.http=e,this.tokenManager=t,this.content=new D(e)}async loginWithApiKey(e){let t=await this.http.post(`/v1/auth/login/api-key`,e);return await this.tokenManager.setTokens({accessToken:t.access_token,refreshToken:t.refresh_token,expiresAt:x(t.expires_at)}),t}},k=class{constructor(e){o(this,`http`,void 0),this.http=e}async balance(){return this.http.get(`/v1/wallet/balance`)}async transactions(){return this.http.get(`/v1/wallet/transactions`)}async createPaymentSession(e){return this.http.post(`/v1/wallet/payment-session`,e)}async getPaymentStatus(e){return this.http.get(`/v1/wallet/payment-status/${encodeURIComponent(e)}`)}},A=class{constructor(e){o(this,`http`,void 0),this.http=e}async state(e){return this.http.get(`/v1/checkout/state/${encodeURIComponent(e)}`)}},j=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/company/invitations`)}async create(e){return this.http.post(`/v1/company/invitations`,e)}async accept(e){return this.http.post(`/v1/company/invitations/accept`,e)}};function M(e){return`/v1/company/machine-users/${encodeURIComponent(e)}`}var N=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`${M(e)}/buyer-keys`)}async create(e,t){return this.http.post(`${M(e)}/buyer-keys`,t)}async revoke(e,t){return this.http.delete(`${M(e)}/buyer-keys/${encodeURIComponent(t)}`)}},P=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`${M(e)}/mcp-keys`)}async create(e,t){return this.http.post(`${M(e)}/mcp-keys`,t)}async revoke(e,t){return this.http.delete(`${M(e)}/mcp-keys/${encodeURIComponent(t)}`)}},F=class{constructor(e){o(this,`http`,void 0),o(this,`buyerKeys`,void 0),o(this,`mcpKeys`,void 0),this.http=e,this.buyerKeys=new N(e),this.mcpKeys=new P(e)}async list(){return this.http.get(`/v1/company/machine-users`)}async create(e){return this.http.post(`/v1/company/machine-users`,e)}async deactivate(e){return this.http.delete(M(e))}},I=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/company/members`)}async update(e,t){return this.http.patch(`/v1/company/members/${encodeURIComponent(e)}`,t)}async remove(e){return this.http.delete(`/v1/company/members/${encodeURIComponent(e)}`)}},L=class{constructor(e){o(this,`http`,void 0),this.http=e}async get(){return this.http.get(`/v1/company/membership`)}async leave(){return this.http.delete(`/v1/company/membership`)}},R=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`/v1/company/purchases`,e)}},z=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`/v1/company/spend`,e)}},B=class{constructor(e){o(this,`http`,void 0),this.http=e}async createPaymentSession(e){return this.http.post(`/v1/company/wallet/payment-session`,e)}async listPendingTopUps(){return this.http.get(`/v1/company/wallet/pending-top-ups`)}},V=class{constructor(e){o(this,`membership`,void 0),o(this,`invitations`,void 0),o(this,`members`,void 0),o(this,`machineUsers`,void 0),o(this,`wallet`,void 0),o(this,`purchases`,void 0),o(this,`spend`,void 0),this.membership=new L(e),this.invitations=new j(e),this.members=new I(e),this.machineUsers=new F(e),this.wallet=new B(e),this.purchases=new R(e),this.spend=new z(e)}},H=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/user/api-keys`)}async create(e){return this.http.post(`/v1/user/api-keys`,e)}async revoke(e){return this.http.delete(`/v1/user/api-keys/${encodeURIComponent(e)}`)}},U=class{constructor(e){o(this,`http`,void 0),this.http=e}async get(){return this.http.get(`/v1/user/spend-cap`)}async update(e){return this.http.patch(`/v1/user/spend-cap`,e)}},W=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/mcp/keys`)}async create(e){return this.http.post(`/v1/mcp/keys`,e)}async revoke(e){return this.http.delete(`/v1/mcp/keys/${encodeURIComponent(e)}`)}},G=class{constructor(e){o(this,`apiKeys`,void 0),o(this,`spendCap`,void 0),o(this,`mcpKeys`,void 0),this.apiKeys=new H(e),this.spendCap=new U(e),this.mcpKeys=new W(e)}};function K(e){let t=e.baseUrl??`https://api.ledewire.com`,n=new y({storage:e.storage??new b,refreshFn:S(t),...e.onAuthExpired!==void 0&&{onAuthExpired:e.onAuthExpired}});return new q(new _({baseUrl:t,getAccessToken:()=>n.getAccessToken(),onUnauthorized:()=>n.handleUnauthorized()}),n,e)}var q=class{constructor(e,t,n){o(this,`_http`,void 0),o(this,`_tokenManager`,void 0),o(this,`_config`,void 0),o(this,`config`,void 0),o(this,`auth`,void 0),o(this,`checkout`,void 0),o(this,`wallet`,void 0),o(this,`purchases`,void 0),o(this,`content`,void 0),o(this,`seller`,void 0),o(this,`user`,void 0),o(this,`company`,void 0),this._http=e,this._tokenManager=t,this._config=n,this.config=new w(e),this.auth=new C(e,t),this.checkout=new A(e),this.wallet=new k(e),this.purchases=new E(e),this.content=new T(e),this.seller=new O(e,t),this.user=new G(e),this.company=new V(e)}},J=`lw:tokens`;function Y(e,t=J){return{getTokens(){let n=e.getItem(t);if(!n)return null;try{return JSON.parse(n)}catch{return null}},setTokens(n){e.setItem(t,JSON.stringify(n))},clearTokens(){e.removeItem(t)}}}function X(e){return Y(localStorage,e)}function Z(e){return Y(sessionStorage,e)}return e.AuthError=u,e.ForbiddenError=d,e.LedewireError=l,e.MemoryTokenStorage=b,e.NotFoundError=f,e.PurchaseError=p,e.SpendCapReachedError=m,e.init=K,e.localStorageAdapter=X,e.parseExpiresAt=x,e.sessionStorageAdapter=Z,e})({});
1
+ var Ledewire=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){return atob(e)}function n(e){return{...e,...typeof e.content_body==`string`&&{content_body:t(e.content_body)},...typeof e.teaser==`string`&&{teaser:t(e.teaser)}}}function r(e){"@babel/helpers - typeof";return r=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},r(e)}function i(e,t){if(r(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var i=n.call(e,t||`default`);if(r(i)!=`object`)return i;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function a(e){var t=i(e,`string`);return r(t)==`symbol`?t:t+``}function o(e,t,n){return(t=a(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s,c=Symbol.for(`@ledewire/error-brands`);s=Symbol.hasInstance;var l=class extends Error{constructor(e,t,n,r,i){super(e),o(this,`statusCode`,void 0),o(this,`code`,void 0),o(this,`type`,void 0),o(this,`details`,void 0),this.name=`LedewireError`,this.statusCode=t,this.code=n,this.type=r,this.details=i,Object.setPrototypeOf(this,new.target.prototype);let a=[],s=new.target;for(;typeof s==`function`;){let e=s.brand;typeof e==`string`&&Object.prototype.hasOwnProperty.call(s,`brand`)&&a.push(e),s=Object.getPrototypeOf(s)}Object.defineProperty(this,c,{value:a,enumerable:!1,configurable:!0})}static[s](e){if(Function.prototype[Symbol.hasInstance].call(this,e))return!0;if(!e||typeof e!=`object`&&typeof e!=`function`)return!1;let t=e[c];return Array.isArray(t)&&t.includes(this.brand)}};o(l,`brand`,`LedewireError`);var u=class extends l{constructor(e,t,n,r){super(e,401,t,n,r),this.name=`AuthError`}};o(u,`brand`,`AuthError`);var d=class extends l{constructor(e,t,n,r){super(e,403,t,n,r),this.name=`ForbiddenError`}};o(d,`brand`,`ForbiddenError`);var f=class extends l{constructor(e,t,n,r){super(e,404,t,n,r),this.name=`NotFoundError`}};o(f,`brand`,`NotFoundError`);var p=class extends l{constructor(e,t,n,r,i){super(e,t,n,r,i),this.name=`PurchaseError`}};o(p,`brand`,`PurchaseError`);var m=class extends l{constructor(e,t,n){super(e,402,n,`daily_spend_cap_reached`),o(this,`capCents`,void 0),o(this,`spentCents`,void 0),o(this,`remainingCents`,void 0),o(this,`resetsAt`,void 0),o(this,`bulkExempt`,void 0),this.name=`SpendCapReachedError`,this.capCents=t.capCents,this.spentCents=t.spentCents,this.remainingCents=t.remainingCents,this.resetsAt=t.resetsAt,this.bulkExempt=t.bulkExempt,Object.setPrototypeOf(this,new.target.prototype)}};o(m,`brand`,`SpendCapReachedError`);function h(e){if(!e||typeof e!=`object`)return null;let t=e,n=t.error;if(!n||typeof n!=`object`)return null;let r=n;if(r.type!==`daily_spend_cap_reached`)return null;let i=t.cap_cents,a=t.spent_cents,o=t.remaining_cents,s=t.resets_at,c=t.bulk_exempt;if(typeof i!=`number`||typeof a!=`number`||typeof o!=`number`||typeof s!=`string`||typeof c!=`boolean`)return null;let l=typeof r.message==`string`?r.message:typeof t.message==`string`?t.message:`Daily spend cap reached.`,u=typeof r.code==`number`?r.code:void 0;return new m(l,{capCents:i,spentCents:a,remainingCents:o,resetsAt:s,bulkExempt:c},u)}function g(e,t){if(!e||typeof e!=`object`||Array.isArray(e))return{message:t,code:void 0,type:void 0,details:void 0};let{error:n,...r}=e,i=Object.keys(r).length>0?r:void 0;if(typeof n==`string`)return{message:n,code:void 0,type:void 0,details:i};if(!n||typeof n!=`object`||Array.isArray(n))return{message:t,code:void 0,type:void 0,details:i};let a=n;return{message:typeof a.message==`string`?a.message:t,code:typeof a.code==`number`?a.code:void 0,type:typeof a.type==`string`?a.type:void 0,details:i}}var _=class{constructor(e={}){o(this,`baseUrl`,void 0),o(this,`getAccessToken`,void 0),o(this,`onUnauthorized`,void 0),this.baseUrl=(e.baseUrl??`https://api.ledewire.com`).replace(/\/$/,``),this.getAccessToken=e.getAccessToken??(()=>null),this.onUnauthorized=e.onUnauthorized??(()=>null)}async get(e,t,n){return this.request(`GET`,this.buildUrl(e,t),void 0,n)}async post(e,t,n){return this.request(`POST`,this.buildUrl(e,n),t)}async put(e,t){return this.request(`PUT`,this.buildUrl(e),t)}async patch(e,t){return this.request(`PATCH`,this.buildUrl(e),t)}async delete(e){return this.request(`DELETE`,this.buildUrl(e))}async getRaw(e){return this.performRequest(`GET`,this.buildUrl(e),{Accept:`application/gzip, application/json`})}buildUrl(e,t){let n=`${this.baseUrl}${e}`;if(!t)return n;let r=Object.entries(t).filter(e=>e[1]!==void 0);return r.length===0?n:`${n}?${new URLSearchParams(r.map(([e,t])=>[e,String(t)])).toString()}`}async request(e,t,n,r){let i=await this.performRequest(e,t,{"Content-Type":`application/json`,Accept:`application/json`},n,!1,r);if(i.status!==204)return i.json()}async performRequest(e,t,n,r,i=!1,a){let o=a?.auth!==!1,s=o?await this.getAccessToken():null,c={...n};s&&(c.Authorization=`Bearer ${s}`);let l=await fetch(t,{method:e,headers:c,...r!==void 0&&{body:JSON.stringify(r)}});if(l.status===401&&!i){if(o&&await this.onUnauthorized())return this.performRequest(e,t,n,r,!0,a);throw new u(`Session expired. Please re-authenticate.`)}return l.ok||await this.throwApiError(l),l}async throwApiError(e){let t;try{t=await e.json()}catch{}let n=h(t);if(n)throw n;let{message:r,code:i,type:a,details:o}=g(t,e.statusText);switch(e.status){case 401:throw new u(r,i,a,o);case 403:throw new d(r,i,a,o);case 404:throw new f(r,i,a,o);default:throw new l(r,e.status,i,a,o)}}},v=6e4,y=class{constructor(e){o(this,`storage`,void 0),o(this,`refreshFn`,void 0),o(this,`onTokenRefreshed`,void 0),o(this,`onAuthExpired`,void 0),o(this,`refreshPromise`,null),this.storage=e.storage,this.refreshFn=e.refreshFn,this.onTokenRefreshed=e.onTokenRefreshed,this.onAuthExpired=e.onAuthExpired}async getAccessToken(){let e=await this.storage.getTokens();return e?e.expiresAt-Date.now()<v?(await this.performRefresh(e.refreshToken))?.accessToken??null:e.accessToken:null}async handleUnauthorized(){let e=await this.storage.getTokens();if(!e)return await this.onAuthExpired?.(),null;let t=await this.performRefresh(e.refreshToken);return t||await this.onAuthExpired?.(),t?.accessToken??null}async setTokens(e){await this.storage.setTokens(e)}async clearTokens(){await this.storage.clearTokens()}performRefresh(e){return this.refreshPromise||(this.refreshPromise=this.refreshFn(e).then(async e=>(await this.storage.setTokens(e),await this.onTokenRefreshed?.(e),e)).catch(e=>{throw this.storage.clearTokens(),new u(`Token refresh failed. Please re-authenticate.`)}).finally(()=>{this.refreshPromise=null})),this.refreshPromise}},b=class{constructor(){o(this,`tokens`,null)}getTokens(){return this.tokens}setTokens(e){this.tokens=e}clearTokens(){this.tokens=null}};function x(e){return new Date(e).getTime()}function S(e){return async t=>{let n=await fetch(`${e}/v1/auth/token/refresh`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({refresh_token:t})});if(!n.ok)throw Error(`Token refresh failed`);let r=await n.json();return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresAt:x(r.expires_at)}}}var C=class{constructor(e,t){o(this,`http`,void 0),o(this,`tokenManager`,void 0),this.http=e,this.tokenManager=t}async signup(e){let t=await this.http.post(`/v1/auth/signup`,e);return await this.storeTokens(t),t}async loginWithEmail(e){let t=await this.http.post(`/v1/auth/login/email`,e);return await this.storeTokens(t),t}async loginWithGoogle(e){let t=await this.http.post(`/v1/auth/login/google`,e);return await this.storeTokens(t),t}async loginWithBuyerApiKey(e){let t=await this.http.post(`/v1/auth/login/buyer-api-key`,e);return await this.storeTokens(t),t}async requestPasswordReset(e){return this.http.post(`/v1/auth/password/reset-request`,e)}async resetPassword(e){return this.http.post(`/v1/auth/password/reset`,e)}async storeTokens(e){await this.tokenManager.setTokens({accessToken:e.access_token,refreshToken:e.refresh_token,expiresAt:x(e.expires_at)})}},w=class{constructor(e){o(this,`http`,void 0),this.http=e}async getPublic(){return this.http.get(`/v1/config/public`)}},T=class{constructor(e){o(this,`http`,void 0),this.http=e}async getWithAccess(e){return n(await this.http.get(`/v1/content/${encodeURIComponent(e)}/with-access`))}},E=class{constructor(e){o(this,`http`,void 0),this.http=e}async create(e){return this.http.post(`/v1/purchases`,e)}async list(){return this.http.get(`/v1/purchases`)}async get(e){return this.http.get(`/v1/purchases/${encodeURIComponent(e)}`)}},D=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return(await this.http.get(`/v1/seller/content`)).map(n)}async search(e){return(await this.http.post(`/v1/seller/content/search`,e)).map(n)}async get(e){return n(await this.http.get(`/v1/seller/content/${encodeURIComponent(e)}`))}},O=class{constructor(e,t){o(this,`http`,void 0),o(this,`tokenManager`,void 0),o(this,`content`,void 0),this.http=e,this.tokenManager=t,this.content=new D(e)}async loginWithApiKey(e){let t=await this.http.post(`/v1/auth/login/api-key`,e);return await this.tokenManager.setTokens({accessToken:t.access_token,refreshToken:t.refresh_token,expiresAt:x(t.expires_at)}),t}},k=class{constructor(e){o(this,`http`,void 0),this.http=e}async balance(){return this.http.get(`/v1/wallet/balance`)}async transactions(){return this.http.get(`/v1/wallet/transactions`)}async createPaymentSession(e){return this.http.post(`/v1/wallet/payment-session`,e)}async getPaymentStatus(e){return this.http.get(`/v1/wallet/payment-status/${encodeURIComponent(e)}`)}},A=class{constructor(e){o(this,`http`,void 0),this.http=e}async state(e){return this.http.get(`/v1/checkout/state/${encodeURIComponent(e)}`)}},j=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/company/invitations`)}async create(e){return this.http.post(`/v1/company/invitations`,e)}async revoke(e){return this.http.delete(`/v1/company/invitations/${encodeURIComponent(e)}`)}async accept(e){return this.http.post(`/v1/company/invitations/accept`,e)}};function M(e){return`/v1/company/machine-users/${encodeURIComponent(e)}`}var N=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`${M(e)}/buyer-keys`)}async create(e,t){return this.http.post(`${M(e)}/buyer-keys`,t)}async revoke(e,t){return this.http.delete(`${M(e)}/buyer-keys/${encodeURIComponent(t)}`)}},P=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`${M(e)}/mcp-keys`)}async create(e,t){return this.http.post(`${M(e)}/mcp-keys`,t)}async revoke(e,t){return this.http.delete(`${M(e)}/mcp-keys/${encodeURIComponent(t)}`)}},F=class{constructor(e){o(this,`http`,void 0),o(this,`buyerKeys`,void 0),o(this,`mcpKeys`,void 0),this.http=e,this.buyerKeys=new N(e),this.mcpKeys=new P(e)}async list(){return this.http.get(`/v1/company/machine-users`)}async create(e){return this.http.post(`/v1/company/machine-users`,e)}async update(e,t){return this.http.patch(M(e),t)}async deactivate(e){return this.http.delete(M(e))}},I=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/company/members`)}async update(e,t){return this.http.patch(`/v1/company/members/${encodeURIComponent(e)}`,t)}async remove(e){return this.http.delete(`/v1/company/members/${encodeURIComponent(e)}`)}},L=class{constructor(e){o(this,`http`,void 0),this.http=e}async get(){return this.http.get(`/v1/company/membership`)}async leave(){return this.http.delete(`/v1/company/membership`)}},R=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`/v1/company/purchases`,e)}},z=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(e){return this.http.get(`/v1/company/spend`,e)}},B=class{constructor(e){o(this,`http`,void 0),this.http=e}async get(){return this.http.get(`/v1/company/wallet`)}async createPaymentSession(e){return this.http.post(`/v1/company/wallet/payment-session`,e)}async listPendingTopUps(){return this.http.get(`/v1/company/wallet/pending-top-ups`)}},V=class{constructor(e){o(this,`membership`,void 0),o(this,`invitations`,void 0),o(this,`members`,void 0),o(this,`machineUsers`,void 0),o(this,`wallet`,void 0),o(this,`purchases`,void 0),o(this,`spend`,void 0),this.membership=new L(e),this.invitations=new j(e),this.members=new I(e),this.machineUsers=new F(e),this.wallet=new B(e),this.purchases=new R(e),this.spend=new z(e)}},H=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/user/api-keys`)}async create(e){return this.http.post(`/v1/user/api-keys`,e)}async revoke(e){return this.http.delete(`/v1/user/api-keys/${encodeURIComponent(e)}`)}},U=class{constructor(e){o(this,`http`,void 0),this.http=e}async get(){return this.http.get(`/v1/user/spend-cap`)}async update(e){return this.http.patch(`/v1/user/spend-cap`,e)}},W=class{constructor(e){o(this,`http`,void 0),this.http=e}async list(){return this.http.get(`/v1/mcp/keys`)}async create(e){return this.http.post(`/v1/mcp/keys`,e)}async revoke(e){return this.http.delete(`/v1/mcp/keys/${encodeURIComponent(e)}`)}},G=class{constructor(e){o(this,`apiKeys`,void 0),o(this,`spendCap`,void 0),o(this,`mcpKeys`,void 0),this.apiKeys=new H(e),this.spendCap=new U(e),this.mcpKeys=new W(e)}};function K(e){let t=e.baseUrl??`https://api.ledewire.com`,n=new y({storage:e.storage??new b,refreshFn:S(t),...e.onAuthExpired!==void 0&&{onAuthExpired:e.onAuthExpired}});return new q(new _({baseUrl:t,getAccessToken:()=>n.getAccessToken(),onUnauthorized:()=>n.handleUnauthorized()}),n,e)}var q=class{constructor(e,t,n){o(this,`_http`,void 0),o(this,`_tokenManager`,void 0),o(this,`_config`,void 0),o(this,`config`,void 0),o(this,`auth`,void 0),o(this,`checkout`,void 0),o(this,`wallet`,void 0),o(this,`purchases`,void 0),o(this,`content`,void 0),o(this,`seller`,void 0),o(this,`user`,void 0),o(this,`company`,void 0),this._http=e,this._tokenManager=t,this._config=n,this.config=new w(e),this.auth=new C(e,t),this.checkout=new A(e),this.wallet=new k(e),this.purchases=new E(e),this.content=new T(e),this.seller=new O(e,t),this.user=new G(e),this.company=new V(e)}},J=`lw:tokens`;function Y(e,t=J){return{getTokens(){let n=e.getItem(t);if(!n)return null;try{return JSON.parse(n)}catch{return null}},setTokens(n){e.setItem(t,JSON.stringify(n))},clearTokens(){e.removeItem(t)}}}function X(e){return Y(localStorage,e)}function Z(e){return Y(sessionStorage,e)}return e.AuthError=u,e.ForbiddenError=d,e.LedewireError=l,e.MemoryTokenStorage=b,e.NotFoundError=f,e.PurchaseError=p,e.SpendCapReachedError=m,e.init=K,e.localStorageAdapter=X,e.parseExpiresAt=x,e.sessionStorageAdapter=Z,e})({});
2
2
  //# sourceMappingURL=ledewire.min.js.map