@pierre/storage 0.2.2 → 0.3.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 CHANGED
@@ -76,6 +76,26 @@ const readOnlyUrl = await repo.getRemoteURL({
76
76
  // - 'repo:write' - Create a repository
77
77
  ```
78
78
 
79
+ #### Ephemeral Branches
80
+
81
+ For working with ephemeral branches (temporary branches isolated from the main repository), use
82
+ `getEphemeralRemote()`:
83
+
84
+ ```typescript
85
+ // Get ephemeral namespace remote URL
86
+ const ephemeralUrl = await repo.getEphemeralRemoteURL();
87
+ // Returns: https://t:JWT@your-name.code.storage/repo-id+ephemeral.git
88
+
89
+ // Configure separate remotes for default and ephemeral branches
90
+ console.log(`Run: git remote add origin ${await repo.getRemoteURL()}`);
91
+ console.log(`Run: git remote add ephemeral ${await repo.getEphemeralRemoteURL()}`);
92
+
93
+ // Push ephemeral branch
94
+ // git push ephemeral feature-branch
95
+
96
+ // The ephemeral remote supports all the same options and permission as regular remotes
97
+ ```
98
+
79
99
  ### Working with Repository Content
80
100
 
81
101
  Once you have a repository instance, you can perform various Git operations:
@@ -290,6 +310,7 @@ interface FindOneOptions {
290
310
  interface Repo {
291
311
  id: string;
292
312
  getRemoteURL(options?: GetRemoteURLOptions): Promise<string>;
313
+ getEphemeralRemoteURL(options?: GetRemoteURLOptions): Promise<string>;
293
314
 
294
315
  getFileStream(options: GetFileOptions): Promise<Response>;
295
316
  listFiles(options?: ListFilesOptions): Promise<ListFilesResult>;
@@ -387,6 +408,8 @@ interface GetBranchDiffOptions {
387
408
  branch: string;
388
409
  base?: string; // Defaults to 'main'
389
410
  ttl?: number;
411
+ ephemeral?: boolean;
412
+ ephemeralBase?: boolean;
390
413
  }
391
414
 
392
415
  interface GetCommitDiffOptions {
package/dist/index.cjs CHANGED
@@ -448,6 +448,17 @@ function concatChunks(a, b) {
448
448
  return merged;
449
449
  }
450
450
 
451
+ // package.json
452
+ var package_default = {
453
+ version: "0.3.0"};
454
+
455
+ // src/version.ts
456
+ var PACKAGE_NAME = "code-storage-sdk";
457
+ var PACKAGE_VERSION = package_default.version;
458
+ function getUserAgent() {
459
+ return `${PACKAGE_NAME}/${PACKAGE_VERSION}`;
460
+ }
461
+
451
462
  // src/commit.ts
452
463
  var DEFAULT_TTL_SECONDS = 60 * 60;
453
464
  var HEADS_REF_PREFIX = "refs/heads/";
@@ -620,7 +631,8 @@ var FetchCommitTransport = class {
620
631
  headers: {
621
632
  Authorization: `Bearer ${request.authorization}`,
622
633
  "Content-Type": "application/x-ndjson",
623
- Accept: "application/json"
634
+ Accept: "application/json",
635
+ "Code-Storage-Agent": getUserAgent()
624
636
  },
625
637
  body,
626
638
  signal: request.signal
@@ -850,7 +862,8 @@ var FetchDiffCommitTransport = class {
850
862
  headers: {
851
863
  Authorization: `Bearer ${request.authorization}`,
852
864
  "Content-Type": "application/x-ndjson",
853
- Accept: "application/json"
865
+ Accept: "application/json",
866
+ "Code-Storage-Agent": getUserAgent()
854
867
  },
855
868
  body,
856
869
  signal: request.signal
@@ -987,7 +1000,8 @@ var ApiFetcher = class {
987
1000
  method,
988
1001
  headers: {
989
1002
  Authorization: `Bearer ${jwt}`,
990
- "Content-Type": "application/json"
1003
+ "Content-Type": "application/json",
1004
+ "Code-Storage-Agent": getUserAgent()
991
1005
  }
992
1006
  };
993
1007
  if (method !== "GET" && typeof path !== "string" && path.body) {
@@ -1283,8 +1297,8 @@ function isRecord(value) {
1283
1297
  }
1284
1298
 
1285
1299
  // src/index.ts
1286
- var API_BASE_URL = "https://api.code.storage";
1287
- var STORAGE_BASE_URL = "code.storage";
1300
+ var API_BASE_URL = "https://api.{{org}}.code.storage";
1301
+ var STORAGE_BASE_URL = "{{org}}.code.storage";
1288
1302
  var API_VERSION = 1;
1289
1303
  var apiInstanceMap = /* @__PURE__ */ new Map();
1290
1304
  var DEFAULT_TOKEN_TTL_SECONDS = 60 * 60;
@@ -1511,14 +1525,19 @@ var RepoImpl = class {
1511
1525
  this.options = options;
1512
1526
  this.generateJWT = generateJWT;
1513
1527
  this.api = getApiInstance(
1514
- this.options.apiBaseUrl ?? API_BASE_URL,
1528
+ this.options.apiBaseUrl ?? GitStorage.getDefaultAPIBaseUrl(options.name),
1515
1529
  this.options.apiVersion ?? API_VERSION
1516
1530
  );
1517
1531
  }
1518
1532
  api;
1519
1533
  async getRemoteURL(urlOptions) {
1520
- const storageBaseUrl = this.options.storageBaseUrl ?? STORAGE_BASE_URL;
1521
- const url = new URL(`https://${this.options.name}.${storageBaseUrl}/${this.id}.git`);
1534
+ const url = new URL(`https://${this.options.storageBaseUrl}/${this.id}.git`);
1535
+ url.username = `t`;
1536
+ url.password = await this.generateJWT(this.id, urlOptions);
1537
+ return url.toString();
1538
+ }
1539
+ async getEphemeralRemoteURL(urlOptions) {
1540
+ const url = new URL(`https://${this.options.storageBaseUrl}/${this.id}+ephemeral.git`);
1522
1541
  url.username = `t`;
1523
1542
  url.password = await this.generateJWT(this.id, urlOptions);
1524
1543
  return url.toString();
@@ -1538,6 +1557,9 @@ var RepoImpl = class {
1538
1557
  if (typeof options.ephemeral === "boolean") {
1539
1558
  params.ephemeral = String(options.ephemeral);
1540
1559
  }
1560
+ if (typeof options.ephemeralBase === "boolean") {
1561
+ params.ephemeral_base = String(options.ephemeralBase);
1562
+ }
1541
1563
  return this.api.get({ path: "repos/file", params }, jwt);
1542
1564
  }
1543
1565
  async listFiles(options) {
@@ -1623,6 +1645,12 @@ var RepoImpl = class {
1623
1645
  if (options.base) {
1624
1646
  params.base = options.base;
1625
1647
  }
1648
+ if (typeof options.ephemeral === "boolean") {
1649
+ params.ephemeral = String(options.ephemeral);
1650
+ }
1651
+ if (typeof options.ephemeralBase === "boolean") {
1652
+ params.ephemeral_base = String(options.ephemeralBase);
1653
+ }
1626
1654
  const response = await this.api.get({ path: "repos/branches/diff", params }, jwt);
1627
1655
  const raw = branchDiffResponseSchema.parse(await response.json());
1628
1656
  return transformBranchDiffResult(raw);
@@ -1636,6 +1664,9 @@ var RepoImpl = class {
1636
1664
  const params = {
1637
1665
  sha: options.sha
1638
1666
  };
1667
+ if (options.baseSha) {
1668
+ params.baseSha = options.baseSha;
1669
+ }
1639
1670
  const response = await this.api.get({ path: "repos/diff", params }, jwt);
1640
1671
  const raw = commitDiffResponseSchema.parse(await response.json());
1641
1672
  return transformCommitDiffResult(raw);
@@ -1755,7 +1786,7 @@ var RepoImpl = class {
1755
1786
  }
1756
1787
  createCommit(options) {
1757
1788
  const version = this.options.apiVersion ?? API_VERSION;
1758
- const baseUrl = this.options.apiBaseUrl ?? API_BASE_URL;
1789
+ const baseUrl = this.options.apiBaseUrl ?? GitStorage.getDefaultAPIBaseUrl(this.options.name);
1759
1790
  const transport = new FetchCommitTransport({ baseUrl, version });
1760
1791
  const ttl = resolveCommitTtlSeconds(options);
1761
1792
  const builderOptions = {
@@ -1774,7 +1805,7 @@ var RepoImpl = class {
1774
1805
  }
1775
1806
  async createCommitFromDiff(options) {
1776
1807
  const version = this.options.apiVersion ?? API_VERSION;
1777
- const baseUrl = this.options.apiBaseUrl ?? API_BASE_URL;
1808
+ const baseUrl = this.options.apiBaseUrl ?? GitStorage.getDefaultAPIBaseUrl(this.options.name);
1778
1809
  const transport = new FetchDiffCommitTransport({ baseUrl, version });
1779
1810
  const ttl = resolveCommitTtlSeconds(options);
1780
1811
  const requestOptions = {
@@ -1793,7 +1824,6 @@ var RepoImpl = class {
1793
1824
  }
1794
1825
  };
1795
1826
  var GitStorage = class _GitStorage {
1796
- static overrides = {};
1797
1827
  options;
1798
1828
  api;
1799
1829
  constructor(options) {
@@ -1808,10 +1838,10 @@ var GitStorage = class _GitStorage {
1808
1838
  if (typeof options.key !== "string" || options.key.trim() === "") {
1809
1839
  throw new Error("GitStorage key must be a non-empty string.");
1810
1840
  }
1811
- const resolvedApiBaseUrl = options.apiBaseUrl ?? _GitStorage.overrides.apiBaseUrl ?? API_BASE_URL;
1812
- const resolvedApiVersion = options.apiVersion ?? _GitStorage.overrides.apiVersion ?? API_VERSION;
1813
- const resolvedStorageBaseUrl = options.storageBaseUrl ?? _GitStorage.overrides.storageBaseUrl ?? STORAGE_BASE_URL;
1814
- const resolvedDefaultTtl = options.defaultTTL ?? _GitStorage.overrides.defaultTTL;
1841
+ const resolvedApiBaseUrl = options.apiBaseUrl ?? _GitStorage.getDefaultAPIBaseUrl(options.name);
1842
+ const resolvedApiVersion = options.apiVersion ?? API_VERSION;
1843
+ const resolvedStorageBaseUrl = options.storageBaseUrl ?? _GitStorage.getDefaultStorageBaseUrl(options.name);
1844
+ const resolvedDefaultTtl = options.defaultTTL;
1815
1845
  this.api = getApiInstance(resolvedApiBaseUrl, resolvedApiVersion);
1816
1846
  this.options = {
1817
1847
  key: options.key,
@@ -1822,8 +1852,11 @@ var GitStorage = class _GitStorage {
1822
1852
  defaultTTL: resolvedDefaultTtl
1823
1853
  };
1824
1854
  }
1825
- static override(options) {
1826
- this.overrides = Object.assign({}, this.overrides, options);
1855
+ static getDefaultAPIBaseUrl(name) {
1856
+ return API_BASE_URL.replace("{{org}}", name);
1857
+ }
1858
+ static getDefaultStorageBaseUrl(name) {
1859
+ return STORAGE_BASE_URL.replace("{{org}}", name);
1827
1860
  }
1828
1861
  /**
1829
1862
  * Create a new repository
@@ -1903,6 +1936,7 @@ function createClient(options) {
1903
1936
  }
1904
1937
 
1905
1938
  exports.ApiError = ApiError;
1939
+ exports.CodeStorage = GitStorage;
1906
1940
  exports.GitStorage = GitStorage;
1907
1941
  exports.RefUpdateError = RefUpdateError;
1908
1942
  exports.createClient = createClient;