@ledewire/browser 0.5.0 → 0.6.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.
- package/README.md +11 -6
- package/dist/index.d.ts +138 -49
- package/dist/index.js +137 -87
- package/dist/index.js.map +1 -1
- package/dist/ledewire.min.js +1 -1
- package/dist/ledewire.min.js.map +1 -1
- package/llms.txt +28 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var n = (s, e, t) =>
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var g = (s, e, t) => e in s ? d(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var n = (s, e, t) => g(s, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
function w(s) {
|
|
5
|
+
return atob(s);
|
|
6
|
+
}
|
|
7
|
+
function u(s) {
|
|
8
|
+
return {
|
|
9
|
+
...s,
|
|
10
|
+
...typeof s.content_body == "string" && {
|
|
11
|
+
content_body: w(s.content_body)
|
|
12
|
+
},
|
|
13
|
+
...typeof s.teaser == "string" && { teaser: w(s.teaser) }
|
|
14
|
+
};
|
|
15
|
+
}
|
|
4
16
|
class c extends Error {
|
|
5
17
|
constructor(t, r, o) {
|
|
6
18
|
super(t);
|
|
@@ -11,33 +23,33 @@ class c extends Error {
|
|
|
11
23
|
this.name = "LedewireError", this.statusCode = r, this.code = o, Object.setPrototypeOf(this, new.target.prototype);
|
|
12
24
|
}
|
|
13
25
|
}
|
|
14
|
-
class
|
|
26
|
+
class l extends c {
|
|
15
27
|
constructor(e, t) {
|
|
16
28
|
super(e, 401, t), this.name = "AuthError";
|
|
17
29
|
}
|
|
18
30
|
}
|
|
19
|
-
class
|
|
31
|
+
class k extends c {
|
|
20
32
|
constructor(e, t) {
|
|
21
33
|
super(e, 403, t), this.name = "ForbiddenError";
|
|
22
34
|
}
|
|
23
35
|
}
|
|
24
|
-
class
|
|
36
|
+
class f extends c {
|
|
25
37
|
constructor(e, t) {
|
|
26
38
|
super(e, 404, t), this.name = "NotFoundError";
|
|
27
39
|
}
|
|
28
40
|
}
|
|
29
|
-
class
|
|
41
|
+
class B extends c {
|
|
30
42
|
constructor(e, t, r) {
|
|
31
43
|
super(e, t, r), this.name = "PurchaseError";
|
|
32
44
|
}
|
|
33
45
|
}
|
|
34
|
-
const
|
|
35
|
-
class
|
|
46
|
+
const T = "https://api.ledewire.com";
|
|
47
|
+
class y {
|
|
36
48
|
constructor(e = {}) {
|
|
37
49
|
n(this, "baseUrl");
|
|
38
50
|
n(this, "getAccessToken");
|
|
39
51
|
n(this, "onUnauthorized");
|
|
40
|
-
this.baseUrl = (e.baseUrl ??
|
|
52
|
+
this.baseUrl = (e.baseUrl ?? T).replace(/\/$/, ""), this.getAccessToken = e.getAccessToken ?? (() => null), this.onUnauthorized = e.onUnauthorized ?? (() => null);
|
|
41
53
|
}
|
|
42
54
|
/**
|
|
43
55
|
* GET request with optional query parameters.
|
|
@@ -99,7 +111,7 @@ class f {
|
|
|
99
111
|
if (a.status === 401 && !o) {
|
|
100
112
|
if (await this.onUnauthorized())
|
|
101
113
|
return this.request(e, t, r, !0);
|
|
102
|
-
throw new
|
|
114
|
+
throw new l("Session expired. Please re-authenticate.");
|
|
103
115
|
}
|
|
104
116
|
if (a.ok || await this.throwApiError(a), a.status !== 204)
|
|
105
117
|
return a.json();
|
|
@@ -113,18 +125,18 @@ class f {
|
|
|
113
125
|
const r = t?.error.message ?? e.statusText, o = t?.error.code;
|
|
114
126
|
switch (e.status) {
|
|
115
127
|
case 401:
|
|
116
|
-
throw new
|
|
128
|
+
throw new l(r, o);
|
|
117
129
|
case 403:
|
|
118
|
-
throw new
|
|
130
|
+
throw new k(r, o);
|
|
119
131
|
case 404:
|
|
120
|
-
throw new
|
|
132
|
+
throw new f(r, o);
|
|
121
133
|
default:
|
|
122
134
|
throw new c(r, e.status, o);
|
|
123
135
|
}
|
|
124
136
|
}
|
|
125
137
|
}
|
|
126
|
-
const
|
|
127
|
-
class
|
|
138
|
+
const m = 6e4;
|
|
139
|
+
class E {
|
|
128
140
|
constructor(e) {
|
|
129
141
|
n(this, "storage");
|
|
130
142
|
n(this, "refreshFn");
|
|
@@ -139,7 +151,7 @@ class y {
|
|
|
139
151
|
*/
|
|
140
152
|
async getAccessToken() {
|
|
141
153
|
const e = await this.storage.getTokens();
|
|
142
|
-
return e ? e.expiresAt - Date.now() <
|
|
154
|
+
return e ? e.expiresAt - Date.now() < m ? (await this.performRefresh(e.refreshToken))?.accessToken ?? null : e.accessToken : null;
|
|
143
155
|
}
|
|
144
156
|
/**
|
|
145
157
|
* Called by `HttpClient` when a 401 is received.
|
|
@@ -165,13 +177,13 @@ class y {
|
|
|
165
177
|
// ---------------------------------------------------------------------------
|
|
166
178
|
performRefresh(e) {
|
|
167
179
|
return this.refreshPromise ? this.refreshPromise : (this.refreshPromise = this.refreshFn(e).then(async (t) => (await this.storage.setTokens(t), await this.onTokenRefreshed?.(t), t)).catch((t) => {
|
|
168
|
-
throw this.storage.clearTokens(), new
|
|
180
|
+
throw this.storage.clearTokens(), new l("Token refresh failed. Please re-authenticate.");
|
|
169
181
|
}).finally(() => {
|
|
170
182
|
this.refreshPromise = null;
|
|
171
183
|
}), this.refreshPromise);
|
|
172
184
|
}
|
|
173
185
|
}
|
|
174
|
-
class
|
|
186
|
+
class A {
|
|
175
187
|
constructor() {
|
|
176
188
|
n(this, "tokens", null);
|
|
177
189
|
}
|
|
@@ -185,10 +197,10 @@ class m {
|
|
|
185
197
|
this.tokens = null;
|
|
186
198
|
}
|
|
187
199
|
}
|
|
188
|
-
function
|
|
200
|
+
function p(s) {
|
|
189
201
|
return new Date(s).getTime();
|
|
190
202
|
}
|
|
191
|
-
class
|
|
203
|
+
class S {
|
|
192
204
|
/** @internal */
|
|
193
205
|
constructor(e, t) {
|
|
194
206
|
this.http = e, this.tokenManager = t;
|
|
@@ -226,30 +238,6 @@ class E {
|
|
|
226
238
|
const t = await this.http.post("/v1/auth/login/google", e);
|
|
227
239
|
return await this.storeTokens(t), t;
|
|
228
240
|
}
|
|
229
|
-
/**
|
|
230
|
-
* Log in using an API key to obtain a seller token.
|
|
231
|
-
* Provide only `key` for read-only (`view`) access.
|
|
232
|
-
* Provide both `key` and `secret` for read/write (`full`) access.
|
|
233
|
-
* Tokens are stored automatically after successful authentication.
|
|
234
|
-
*
|
|
235
|
-
* Use this before calling `lw.seller.content.*` methods.
|
|
236
|
-
*
|
|
237
|
-
* @param body - API key credentials.
|
|
238
|
-
* @returns The authentication token response.
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```ts
|
|
242
|
-
* // View access (read-only) — sufficient for seller.content.list/search/get
|
|
243
|
-
* await lw.auth.loginWithApiKey({ key: 'your_api_key' })
|
|
244
|
-
*
|
|
245
|
-
* // Full access (read/write)
|
|
246
|
-
* await lw.auth.loginWithApiKey({ key: 'your_api_key', secret: 'your_secret' })
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
async loginWithApiKey(e) {
|
|
250
|
-
const t = await this.http.post("/v1/auth/login/api-key", e);
|
|
251
|
-
return await this.storeTokens(t), t;
|
|
252
|
-
}
|
|
253
241
|
/**
|
|
254
242
|
* Request a password reset code to be sent to the buyer's email address.
|
|
255
243
|
*
|
|
@@ -292,11 +280,11 @@ class E {
|
|
|
292
280
|
await this.tokenManager.setTokens({
|
|
293
281
|
accessToken: e.access_token,
|
|
294
282
|
refreshToken: e.refresh_token,
|
|
295
|
-
expiresAt:
|
|
283
|
+
expiresAt: p(e.expires_at)
|
|
296
284
|
});
|
|
297
285
|
}
|
|
298
286
|
}
|
|
299
|
-
class
|
|
287
|
+
class v {
|
|
300
288
|
/** @internal */
|
|
301
289
|
constructor(e) {
|
|
302
290
|
this.http = e;
|
|
@@ -319,7 +307,7 @@ class A {
|
|
|
319
307
|
return this.http.get("/v1/config/public");
|
|
320
308
|
}
|
|
321
309
|
}
|
|
322
|
-
class
|
|
310
|
+
class x {
|
|
323
311
|
constructor(e) {
|
|
324
312
|
this.http = e;
|
|
325
313
|
}
|
|
@@ -328,18 +316,24 @@ class v {
|
|
|
328
316
|
* authenticated buyer (or a generic access response when unauthenticated).
|
|
329
317
|
*
|
|
330
318
|
* @param id - The content ID.
|
|
331
|
-
* @
|
|
332
|
-
*
|
|
319
|
+
* @returns The content item with access information. `content_body` and
|
|
320
|
+
* `teaser` are returned as plain UTF-8 text — the SDK decodes base64
|
|
321
|
+
* transparently so you can render them directly.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* const result = await lw.content.getWithAccess('article-123')
|
|
326
|
+
* if (result.access_info.next_required_action === 'view_content') {
|
|
327
|
+
* renderMarkdown(result.content_body ?? '')
|
|
328
|
+
* }
|
|
329
|
+
* ```
|
|
333
330
|
*/
|
|
334
|
-
async getWithAccess(e
|
|
335
|
-
const
|
|
336
|
-
return
|
|
337
|
-
`/v1/content/${e}/with-access`,
|
|
338
|
-
Object.keys(r).length > 0 ? r : void 0
|
|
339
|
-
);
|
|
331
|
+
async getWithAccess(e) {
|
|
332
|
+
const t = await this.http.get(`/v1/content/${e}/with-access`);
|
|
333
|
+
return u(t);
|
|
340
334
|
}
|
|
341
335
|
}
|
|
342
|
-
class
|
|
336
|
+
class b {
|
|
343
337
|
constructor(e) {
|
|
344
338
|
this.http = e;
|
|
345
339
|
}
|
|
@@ -379,7 +373,8 @@ class U {
|
|
|
379
373
|
* List all content for the authenticated seller's store.
|
|
380
374
|
* Requires a token with at least `view` permission.
|
|
381
375
|
*
|
|
382
|
-
* @returns Array of content items.
|
|
376
|
+
* @returns Array of content items. `teaser` is returned as plain text —
|
|
377
|
+
* the SDK decodes base64 automatically.
|
|
383
378
|
*
|
|
384
379
|
* @example
|
|
385
380
|
* ```ts
|
|
@@ -387,7 +382,7 @@ class U {
|
|
|
387
382
|
* ```
|
|
388
383
|
*/
|
|
389
384
|
async list() {
|
|
390
|
-
return this.http.get("/v1/seller/content");
|
|
385
|
+
return (await this.http.get("/v1/seller/content")).map(u);
|
|
391
386
|
}
|
|
392
387
|
/**
|
|
393
388
|
* Search content by title, URI, and/or metadata (AND logic).
|
|
@@ -395,43 +390,77 @@ class U {
|
|
|
395
390
|
* Requires a token with at least `view` permission.
|
|
396
391
|
*
|
|
397
392
|
* @param body - Search criteria (title, uri, and/or metadata).
|
|
398
|
-
* @returns Array of matching content items.
|
|
393
|
+
* @returns Array of matching content items. `teaser` is returned as plain
|
|
394
|
+
* text — the SDK decodes base64 automatically.
|
|
399
395
|
*
|
|
400
396
|
* @example
|
|
401
397
|
* ```ts
|
|
402
398
|
* const results = await lw.seller.content.search({ title: 'intro' })
|
|
403
399
|
* const byUri = await lw.seller.content.search({ uri: 'vimeo.com' })
|
|
400
|
+
* const byId = await lw.seller.content.search({ external_identifier: 'vimeo:123456789' })
|
|
404
401
|
* const byMeta = await lw.seller.content.search({ metadata: { author: 'Alice' } })
|
|
405
402
|
* ```
|
|
406
403
|
*/
|
|
407
404
|
async search(e) {
|
|
408
|
-
return this.http.post("/v1/seller/content/search", e);
|
|
405
|
+
return (await this.http.post("/v1/seller/content/search", e)).map(u);
|
|
409
406
|
}
|
|
410
407
|
/**
|
|
411
408
|
* Get a single content item by ID.
|
|
412
409
|
* Requires a token with at least `view` permission.
|
|
413
410
|
*
|
|
414
411
|
* @param id - The content ID.
|
|
415
|
-
* @returns The content item.
|
|
412
|
+
* @returns The content item. `content_body` and `teaser` are returned as
|
|
413
|
+
* plain UTF-8 text — the SDK decodes base64 automatically so you can render
|
|
414
|
+
* them directly without calling `atob()`.
|
|
416
415
|
*
|
|
417
416
|
* @example
|
|
418
417
|
* ```ts
|
|
419
418
|
* const item = await lw.seller.content.get('content-id')
|
|
419
|
+
* // item.content_body is plain markdown — no atob() needed
|
|
420
420
|
* ```
|
|
421
421
|
*/
|
|
422
422
|
async get(e) {
|
|
423
|
-
|
|
423
|
+
const t = await this.http.get(`/v1/seller/content/${e}`);
|
|
424
|
+
return u(t);
|
|
424
425
|
}
|
|
425
426
|
}
|
|
426
|
-
class
|
|
427
|
+
class P {
|
|
427
428
|
/** @internal */
|
|
428
|
-
constructor(e) {
|
|
429
|
+
constructor(e, t) {
|
|
429
430
|
/** Seller content: list, search, and get by API key. */
|
|
430
431
|
n(this, "content");
|
|
431
|
-
this.content = new U(e);
|
|
432
|
+
this.http = e, this.tokenManager = t, this.content = new U(e);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Log in using an API key to obtain a seller token.
|
|
436
|
+
* Provide only `key` for read-only (`view`) access.
|
|
437
|
+
* Provide both `key` and `secret` for read/write (`full`) access.
|
|
438
|
+
* Tokens are stored automatically after successful authentication.
|
|
439
|
+
*
|
|
440
|
+
* Use this before calling `lw.seller.content.*` methods.
|
|
441
|
+
*
|
|
442
|
+
* @param body - API key credentials.
|
|
443
|
+
* @returns The authentication token response.
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* ```ts
|
|
447
|
+
* // View access (read-only) — sufficient for seller.content.list/search/get
|
|
448
|
+
* await lw.seller.loginWithApiKey({ key: 'your_api_key' })
|
|
449
|
+
*
|
|
450
|
+
* // Full access (read/write)
|
|
451
|
+
* await lw.seller.loginWithApiKey({ key: 'your_api_key', secret: 'your_secret' })
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
454
|
+
async loginWithApiKey(e) {
|
|
455
|
+
const t = await this.http.post("/v1/auth/login/api-key", e);
|
|
456
|
+
return await this.tokenManager.setTokens({
|
|
457
|
+
accessToken: t.access_token,
|
|
458
|
+
refreshToken: t.refresh_token,
|
|
459
|
+
expiresAt: p(t.expires_at)
|
|
460
|
+
}), t;
|
|
432
461
|
}
|
|
433
462
|
}
|
|
434
|
-
class
|
|
463
|
+
class N {
|
|
435
464
|
constructor(e) {
|
|
436
465
|
this.http = e;
|
|
437
466
|
}
|
|
@@ -470,7 +499,7 @@ class b {
|
|
|
470
499
|
return this.http.get(`/v1/wallet/payment-status/${e}`);
|
|
471
500
|
}
|
|
472
501
|
}
|
|
473
|
-
class
|
|
502
|
+
class _ {
|
|
474
503
|
constructor(e) {
|
|
475
504
|
this.http = e;
|
|
476
505
|
}
|
|
@@ -489,8 +518,8 @@ class P {
|
|
|
489
518
|
return this.http.get(`/v1/checkout/state/${e}`);
|
|
490
519
|
}
|
|
491
520
|
}
|
|
492
|
-
function
|
|
493
|
-
const e = s.baseUrl ?? "https://api.ledewire.com", t = s.storage ?? new
|
|
521
|
+
function q(s) {
|
|
522
|
+
const e = s.baseUrl ?? "https://api.ledewire.com", t = s.storage ?? new A(), r = new E({
|
|
494
523
|
storage: t,
|
|
495
524
|
refreshFn: async (h) => {
|
|
496
525
|
const i = await fetch(`${e}/v1/auth/token/refresh`, {
|
|
@@ -503,23 +532,23 @@ function C(s) {
|
|
|
503
532
|
return {
|
|
504
533
|
accessToken: a.access_token,
|
|
505
534
|
refreshToken: a.refresh_token,
|
|
506
|
-
expiresAt:
|
|
535
|
+
expiresAt: p(a.expires_at)
|
|
507
536
|
};
|
|
508
537
|
},
|
|
509
538
|
...s.onAuthExpired !== void 0 && { onAuthExpired: s.onAuthExpired }
|
|
510
|
-
}), o = new
|
|
539
|
+
}), o = new y({
|
|
511
540
|
baseUrl: e,
|
|
512
541
|
getAccessToken: () => r.getAccessToken(),
|
|
513
542
|
onUnauthorized: () => r.handleUnauthorized()
|
|
514
543
|
});
|
|
515
|
-
return new
|
|
544
|
+
return new R(o, r, s);
|
|
516
545
|
}
|
|
517
|
-
class
|
|
546
|
+
class R {
|
|
518
547
|
/** @internal */
|
|
519
548
|
constructor(e, t, r) {
|
|
520
549
|
/** Platform-level public configuration (no auth required) */
|
|
521
550
|
n(this, "config");
|
|
522
|
-
/** Buyer authentication: email/password signup/login, Google, password reset */
|
|
551
|
+
/** Buyer authentication: email/password signup/login, Google OAuth, password reset */
|
|
523
552
|
n(this, "auth");
|
|
524
553
|
/** Checkout state machine: determines next action for a piece of content */
|
|
525
554
|
n(this, "checkout");
|
|
@@ -529,13 +558,13 @@ class N {
|
|
|
529
558
|
n(this, "purchases");
|
|
530
559
|
/** Public content with per-user access information */
|
|
531
560
|
n(this, "content");
|
|
532
|
-
/** Seller
|
|
561
|
+
/** Seller operations: API key login, content list/search/get */
|
|
533
562
|
n(this, "seller");
|
|
534
|
-
this._http = e, this._tokenManager = t, this._config = r, this.config = new
|
|
563
|
+
this._http = e, this._tokenManager = t, this._config = r, this.config = new v(e), this.auth = new S(e, t), this.checkout = new _(e), this.wallet = new N(e), this.purchases = new b(e), this.content = new x(e), this.seller = new P(e, t);
|
|
535
564
|
}
|
|
536
565
|
}
|
|
537
|
-
const
|
|
538
|
-
function
|
|
566
|
+
const F = "lw:tokens";
|
|
567
|
+
function L(s = F) {
|
|
539
568
|
return {
|
|
540
569
|
getTokens() {
|
|
541
570
|
const e = localStorage.getItem(s);
|
|
@@ -554,15 +583,36 @@ function B(s = R) {
|
|
|
554
583
|
}
|
|
555
584
|
};
|
|
556
585
|
}
|
|
586
|
+
const O = "lw:tokens";
|
|
587
|
+
function M(s = O) {
|
|
588
|
+
return {
|
|
589
|
+
getTokens() {
|
|
590
|
+
const e = sessionStorage.getItem(s);
|
|
591
|
+
if (!e) return null;
|
|
592
|
+
try {
|
|
593
|
+
return JSON.parse(e);
|
|
594
|
+
} catch {
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
},
|
|
598
|
+
setTokens(e) {
|
|
599
|
+
sessionStorage.setItem(s, JSON.stringify(e));
|
|
600
|
+
},
|
|
601
|
+
clearTokens() {
|
|
602
|
+
sessionStorage.removeItem(s);
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
}
|
|
557
606
|
export {
|
|
558
|
-
|
|
559
|
-
|
|
607
|
+
l as AuthError,
|
|
608
|
+
k as ForbiddenError,
|
|
560
609
|
c as LedewireError,
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
610
|
+
A as MemoryTokenStorage,
|
|
611
|
+
f as NotFoundError,
|
|
612
|
+
B as PurchaseError,
|
|
613
|
+
q as init,
|
|
614
|
+
L as localStorageAdapter,
|
|
615
|
+
p as parseExpiresAt,
|
|
616
|
+
M as sessionStorageAdapter
|
|
567
617
|
};
|
|
568
618
|
//# sourceMappingURL=index.js.map
|