@yeying-community/web3-bs 1.0.7 → 1.0.8

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.
@@ -1441,6 +1441,9 @@
1441
1441
  const suffix = path.startsWith('/') ? path : `/${path}`;
1442
1442
  return `${base}${suffix}`;
1443
1443
  }
1444
+ function isRecord(value) {
1445
+ return typeof value === 'object' && value !== null;
1446
+ }
1444
1447
  function resolveAuthHeader(auth, token) {
1445
1448
  if (auth?.type === 'bearer') {
1446
1449
  return `Bearer ${auth.token}`;
@@ -1629,6 +1632,72 @@
1629
1632
  }
1630
1633
  return await res.json();
1631
1634
  }
1635
+ async requestApiJson(method, apiPath, body, options) {
1636
+ const headers = this.buildHeaders({
1637
+ auth: options?.auth,
1638
+ token: options?.token,
1639
+ contentType: body === undefined ? undefined : 'application/json',
1640
+ });
1641
+ const response = await this.fetcher(joinUrl(this.baseUrl, apiPath), {
1642
+ method,
1643
+ headers,
1644
+ body: body === undefined ? undefined : JSON.stringify(body),
1645
+ credentials: this.credentials,
1646
+ signal: options?.signal,
1647
+ });
1648
+ if (!response.ok) {
1649
+ throw new Error(`WebDAV ${method} ${apiPath} failed: ${response.status} ${response.statusText}`);
1650
+ }
1651
+ return await response.json();
1652
+ }
1653
+ getShareAccessUrl(token, fileName) {
1654
+ const normalizedToken = encodeURIComponent(String(token || '').trim());
1655
+ if (!normalizedToken) {
1656
+ throw new Error('Share token is required');
1657
+ }
1658
+ const encodedFileName = String(fileName || '').trim()
1659
+ ? `/${encodeURIComponent(String(fileName || '').trim())}`
1660
+ : '';
1661
+ return joinUrl(this.baseUrl, `/api/v1/public/share/${normalizedToken}${encodedFileName}`);
1662
+ }
1663
+ async createShareLink(options) {
1664
+ const normalizedPath = String(options.path || '').trim();
1665
+ if (!normalizedPath) {
1666
+ throw new Error('Share path is required');
1667
+ }
1668
+ const payload = await this.requestApiJson('POST', '/api/v1/public/share/create', {
1669
+ path: normalizedPath,
1670
+ expiresIn: options.expiresIn,
1671
+ expiresValue: options.expiresValue,
1672
+ expiresUnit: options.expiresUnit,
1673
+ }, options);
1674
+ if (!isRecord(payload)) {
1675
+ throw new Error('WebDAV share create response is invalid');
1676
+ }
1677
+ return payload;
1678
+ }
1679
+ async listShareLinks(options = {}) {
1680
+ const payload = await this.requestApiJson('GET', '/api/v1/public/share/list', undefined, options);
1681
+ if (!isRecord(payload)) {
1682
+ return [];
1683
+ }
1684
+ const items = payload.items;
1685
+ if (!Array.isArray(items)) {
1686
+ return [];
1687
+ }
1688
+ return items.filter(isRecord);
1689
+ }
1690
+ async revokeShareLink(token, options = {}) {
1691
+ const normalizedToken = String(token || '').trim();
1692
+ if (!normalizedToken) {
1693
+ throw new Error('Share token is required');
1694
+ }
1695
+ const payload = await this.requestApiJson('POST', '/api/v1/public/share/revoke', { token: normalizedToken }, options);
1696
+ if (!isRecord(payload)) {
1697
+ return {};
1698
+ }
1699
+ return payload;
1700
+ }
1632
1701
  }
1633
1702
  function createWebDavClient(options) {
1634
1703
  return new WebDavClient(options);