@theholocron/github-client 1.58.0-alpha.58 → 1.58.0-alpha.60

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
@@ -37,6 +37,11 @@ const blob = await client.git.createBlob("owner/name", "file contents");
37
37
  // (one request each) and returns a flat list of full paths.
38
38
  const entries = await client.git.listDirectory("owner/name", ".holocron", { recursive: true });
39
39
  // [{ name, path, type: "file" | "dir" | "symlink" | "submodule", size, sha }]
40
+
41
+ // The same list in ONE request, through the git trees API. It answers `undefined`, and the caller lists the
42
+ // directory the other way, when it cannot answer for certain: a `ref` that is not the default branch, a full
43
+ // commit SHA or a branch name with no slash, a tree GitHub cut short, a 404 or a 422.
44
+ const tree = await client.git.listTree("owner/name", ".holocron", { ref: commitSha });
40
45
  ```
41
46
 
42
47
  ## Namespaces
@@ -55,7 +60,7 @@ const entries = await client.git.listDirectory("owner/name", ".holocron", { recu
55
60
  | `labels` | `listLabels`, `createLabel`, `updateLabel`, `deleteLabel` |
56
61
  | `topics` | `setTopics` |
57
62
  | `properties` | `setProperties` |
58
- | `git` | `getRef`, `getCommit`, `getTree`, `getContents`, `listDirectory`, `putContents`, `createBlob`, `createTree`, `createCommit`, `createRef`, `updateRef`, `createPull` |
63
+ | `git` | `getRef`, `getCommit`, `getTree`, `getContents`, `listDirectory`, `listTree`, `putContents`, `createBlob`, `createTree`, `createCommit`, `createRef`, `updateRef`, `createPull` |
59
64
  | `checks` | `createCheckRun` |
60
65
 
61
66
  GitHub accepts at most 50 annotations per check-run request. Slice with the
package/dist/index.d.mts CHANGED
@@ -1232,6 +1232,7 @@ export declare function createGitHubClient(opts: GitHubClientOptions): {
1232
1232
  getTree: (repo: string, sha: string, recursive?: boolean) => Promise<GitTree>;
1233
1233
  getContents: (repo: string, path: string, ref?: string) => Promise<GitContents>;
1234
1234
  listDirectory: (repo: string, path: string, options?: ListDirectoryOptions) => Promise<GitDirectoryEntry[]>;
1235
+ listTree: (repo: string, path: string, options?: Pick<ListDirectoryOptions, "ref">) => Promise<GitDirectoryEntry[] | undefined>;
1235
1236
  putContents: (repo: string, path: string, input: PutContentsInput) => Promise<PutContentsResult>;
1236
1237
  createBlob: (repo: string, content: string, encoding?: string) => Promise<GitBlob>;
1237
1238
  createTree: (repo: string, tree: Array<{
package/dist/index.mjs CHANGED
@@ -175,6 +175,12 @@ function environments(rest) {
175
175
  };
176
176
  }
177
177
  // src/git/git.ts
178
+ /** What a git tree entry is, in the words of the contents API: a blob is a file, unless its mode says symlink. */
179
+ function treeEntryType(item) {
180
+ if (item.type === "tree") return "dir";
181
+ if (item.type === "commit") return "submodule";
182
+ return item.mode === "120000" ? "symlink" : "file";
183
+ }
178
184
  function git(rest) {
179
185
  return {
180
186
  getRef: (repo, branch) => rest.request(`${repoBase(repo)}/git/ref/heads/${branch}`),
@@ -220,6 +226,36 @@ function git(rest) {
220
226
  return list(path.replace(/^\/+|\/+$/g, ""));
221
227
  },
222
228
  /**
229
+ * Lists a directory and everything under it in ONE request, through the git trees API
230
+ * (`GET /repos/:repo/git/trees/<ref>:<path>?recursive=1`), as the same flat list of full paths
231
+ * `listDirectory(..., { recursive: true })` returns, which makes one request per directory. It answers
232
+ * `undefined`, so the caller can list the directory the other way, when it cannot answer for certain:
233
+ * a `ref` that is not the default branch, a full commit SHA or a branch name with no slash (a name with a
234
+ * slash is ambiguous in this form and GitHub answers 404), a tree GitHub cut short (`truncated`), a directory GitHub says it does not have (404) or a
235
+ * request it cannot read (422). Any other failure throws. Checked against the live API with `HEAD`, a
236
+ * commit SHA and a plain branch name, and a missing directory.
237
+ */
238
+ listTree: async (repo, path, options = {}) => {
239
+ const dir = path.replace(/^\/+|\/+$/g, "");
240
+ const ref = options.ref ?? "HEAD";
241
+ if (!/^(HEAD|[0-9a-f]{40}|[\w.-]+)$/i.test(ref)) return void 0;
242
+ let tree;
243
+ try {
244
+ tree = await rest.request(`${repoBase(repo)}/git/trees/${ref}:${filePath(dir)}`, { query: { recursive: "1" } });
245
+ } catch (err) {
246
+ if (err instanceof ProviderApiError && (err.status === 404 || err.status === 422)) return void 0;
247
+ throw err;
248
+ }
249
+ if (tree.truncated) return void 0;
250
+ return tree.tree.map((item) => ({
251
+ name: item.path.slice(item.path.lastIndexOf("/") + 1),
252
+ path: `${dir}/${item.path}`,
253
+ type: treeEntryType(item),
254
+ size: item.size ?? 0,
255
+ sha: item.sha
256
+ }));
257
+ },
258
+ /**
223
259
  * `PUT /repos/:repo/contents/:path` — creates or updates one file in a single commit. Pass `sha` when
224
260
  * updating so a concurrent change is refused (409) and not silently overwritten.
225
261
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/github-client",
3
- "version": "1.58.0-alpha.58",
3
+ "version": "1.58.0-alpha.60",
4
4
  "description": "A TypeScript client for the GitHub REST API",
5
5
  "keywords": [
6
6
  "github",
@@ -32,23 +32,23 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@theholocron/http-client": "^1.58.0-alpha.58",
36
- "@theholocron/observability": "^0.7.0-alpha.7"
35
+ "@theholocron/http-client": "^1.58.0-alpha.60",
36
+ "@theholocron/observability": "^0.7.0-alpha.8"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/node": "^26",
40
- "@vitest/coverage-v8": "^5.1.0",
40
+ "@vitest/coverage-v8": "^5.1.1",
41
41
  "@vitest/eslint-plugin": "^1.6.28",
42
42
  "eslint": "^10.15.0",
43
43
  "eslint-plugin-n": "^18.5.0",
44
44
  "globals": "^17.14.0",
45
45
  "tsdown": "^0.23.7",
46
46
  "typescript": "^6.0.3",
47
- "vitest": "^5.1.0",
48
- "@theholocron/eslint-config": "^8.9.0-alpha.22",
49
- "@theholocron/tsconfig": "^8.9.0-alpha.22",
50
- "@theholocron/tsdown-config": "^8.9.0-alpha.22",
51
- "@theholocron/vitest-config": "^8.9.0-alpha.22"
47
+ "vitest": "^5.1.1",
48
+ "@theholocron/eslint-config": "^8.9.0-alpha.24",
49
+ "@theholocron/tsconfig": "^8.9.0-alpha.24",
50
+ "@theholocron/tsdown-config": "^8.9.0-alpha.24",
51
+ "@theholocron/vitest-config": "^8.9.0-alpha.24"
52
52
  },
53
53
  "engines": {
54
54
  "node": ">=22.0.0"