@pierre/storage 1.0.3 → 1.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pierre/storage",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "description": "Pierre Git Storage SDK",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -24,6 +24,7 @@ import {
24
24
  listBranchesResponseSchema,
25
25
  listCommitsResponseSchema,
26
26
  listFilesResponseSchema,
27
+ listFilesWithMetadataResponseSchema,
27
28
  listReposResponseSchema,
28
29
  noteReadResponseSchema,
29
30
  noteWriteResponseSchema,
@@ -36,6 +37,7 @@ import type {
36
37
  BranchInfo,
37
38
  CommitBuilder,
38
39
  CommitInfo,
40
+ CommitMetadata,
39
41
  CommitResult,
40
42
  CreateBranchOptions,
41
43
  CreateBranchResponse,
@@ -48,6 +50,7 @@ import type {
48
50
  DeleteRepoOptions,
49
51
  DeleteRepoResult,
50
52
  DiffFileState,
53
+ FileWithMetadata,
51
54
  FileDiff,
52
55
  FilteredFile,
53
56
  FindOneOptions,
@@ -74,16 +77,22 @@ import type {
74
77
  ListCommitsResult,
75
78
  ListFilesOptions,
76
79
  ListFilesResult,
80
+ ListFilesWithMetadataOptions,
81
+ ListFilesWithMetadataResponse,
82
+ ListFilesWithMetadataResult,
77
83
  ListReposOptions,
78
84
  ListReposResponse,
79
85
  ListReposResult,
80
86
  NoteWriteResult,
81
87
  PullUpstreamOptions,
82
88
  RawBranchInfo,
89
+ RawCommitMetadata,
83
90
  RawCommitInfo,
91
+ RawFileWithMetadata,
84
92
  RawFileDiff,
85
93
  RawFilteredFile,
86
94
  RefUpdate,
95
+ RepoOptions,
87
96
  Repo,
88
97
  RestoreCommitOptions,
89
98
  RestoreCommitResult,
@@ -310,6 +319,39 @@ function transformListCommitsResult(
310
319
  };
311
320
  }
312
321
 
322
+ function transformFileWithMetadata(raw: RawFileWithMetadata): FileWithMetadata {
323
+ return {
324
+ path: raw.path,
325
+ mode: raw.mode,
326
+ size: raw.size,
327
+ lastCommitSha: raw.last_commit_sha,
328
+ };
329
+ }
330
+
331
+ function transformCommitMetadata(raw: RawCommitMetadata): CommitMetadata {
332
+ return {
333
+ author: raw.author,
334
+ date: new Date(raw.date),
335
+ rawDate: raw.date,
336
+ message: raw.message,
337
+ };
338
+ }
339
+
340
+ function transformListFilesWithMetadataResult(
341
+ raw: ListFilesWithMetadataResponse
342
+ ): ListFilesWithMetadataResult {
343
+ const commits: Record<string, CommitMetadata> = {};
344
+ for (const [sha, commit] of Object.entries(raw.commits)) {
345
+ commits[sha] = transformCommitMetadata(commit);
346
+ }
347
+
348
+ return {
349
+ files: raw.files.map(transformFileWithMetadata),
350
+ commits,
351
+ ref: raw.ref,
352
+ };
353
+ }
354
+
313
355
  function normalizeDiffState(rawState: string): DiffFileState {
314
356
  if (!rawState) {
315
357
  return 'unknown';
@@ -675,6 +717,34 @@ class RepoImpl implements Repo {
675
717
  return { paths: raw.paths, ref: raw.ref };
676
718
  }
677
719
 
720
+ async listFilesWithMetadata(
721
+ options?: ListFilesWithMetadataOptions
722
+ ): Promise<ListFilesWithMetadataResult> {
723
+ const ttl = resolveInvocationTtlSeconds(options, DEFAULT_TOKEN_TTL_SECONDS);
724
+ const jwt = await this.generateJWT(this.id, {
725
+ permissions: ['git:read'],
726
+ ttl,
727
+ });
728
+
729
+ const params: Record<string, string> = {};
730
+ if (options?.ref) {
731
+ params.ref = options.ref;
732
+ }
733
+ if (typeof options?.ephemeral === 'boolean') {
734
+ params.ephemeral = String(options.ephemeral);
735
+ }
736
+ const response = await this.api.get(
737
+ {
738
+ path: 'repos/files/metadata',
739
+ params: Object.keys(params).length ? params : undefined,
740
+ },
741
+ jwt
742
+ );
743
+
744
+ const raw = listFilesWithMetadataResponseSchema.parse(await response.json());
745
+ return transformListFilesWithMetadataResult(raw);
746
+ }
747
+
678
748
  async listBranches(
679
749
  options?: ListBranchesOptions
680
750
  ): Promise<ListBranchesResult> {
@@ -1393,13 +1463,11 @@ export class GitStorage {
1393
1463
  throw new Error('Repository already exists');
1394
1464
  }
1395
1465
 
1396
- return new RepoImpl(
1397
- repoId,
1398
- resolvedDefaultBranch ?? 'main',
1399
- new Date().toISOString(),
1400
- this.options,
1401
- this.generateJWT.bind(this)
1402
- );
1466
+ return this.repo({
1467
+ id: repoId,
1468
+ defaultBranch: resolvedDefaultBranch ?? 'main',
1469
+ createdAt: new Date().toISOString(),
1470
+ });
1403
1471
  }
1404
1472
 
1405
1473
  /**
@@ -1448,13 +1516,33 @@ export class GitStorage {
1448
1516
  if (resp.status === 404) {
1449
1517
  return null;
1450
1518
  }
1451
- const body = (await resp.json()) as { default_branch?: string; created_at?: string };
1519
+
1520
+ const body = (await resp.json()) as {
1521
+ default_branch?: string;
1522
+ created_at?: string;
1523
+ };
1452
1524
  const defaultBranch = body.default_branch ?? 'main';
1453
1525
  const createdAt = body.created_at ?? '';
1454
- return new RepoImpl(
1455
- options.id,
1526
+
1527
+ return this.repo({
1528
+ id: options.id,
1456
1529
  defaultBranch,
1457
1530
  createdAt,
1531
+ });
1532
+ }
1533
+
1534
+ /**
1535
+ * Create a Repo handle from known metadata without making an HTTP request.
1536
+ */
1537
+ repo(options: RepoOptions): Repo {
1538
+ if (!options || typeof options.id !== 'string' || options.id.trim() === '') {
1539
+ throw new Error('repo requires a non-empty repository id.');
1540
+ }
1541
+
1542
+ return new RepoImpl(
1543
+ options.id,
1544
+ options.defaultBranch ?? 'main',
1545
+ options.createdAt ?? '',
1458
1546
  this.options,
1459
1547
  this.generateJWT.bind(this)
1460
1548
  );
package/src/schemas.ts CHANGED
@@ -5,6 +5,25 @@ export const listFilesResponseSchema = z.object({
5
5
  ref: z.string(),
6
6
  });
7
7
 
8
+ export const fileWithMetadataRawSchema = z.object({
9
+ path: z.string(),
10
+ mode: z.string(),
11
+ size: z.number(),
12
+ last_commit_sha: z.string(),
13
+ });
14
+
15
+ export const commitMetadataRawSchema = z.object({
16
+ author: z.string(),
17
+ date: z.string(),
18
+ message: z.string(),
19
+ });
20
+
21
+ export const listFilesWithMetadataResponseSchema = z.object({
22
+ files: z.array(fileWithMetadataRawSchema),
23
+ commits: z.record(commitMetadataRawSchema),
24
+ ref: z.string(),
25
+ });
26
+
8
27
  export const branchInfoSchema = z.object({
9
28
  cursor: z.string(),
10
29
  name: z.string(),
@@ -202,6 +221,11 @@ export const errorEnvelopeSchema = z.object({
202
221
  });
203
222
 
204
223
  export type ListFilesResponseRaw = z.infer<typeof listFilesResponseSchema>;
224
+ export type RawFileWithMetadata = z.infer<typeof fileWithMetadataRawSchema>;
225
+ export type RawCommitMetadata = z.infer<typeof commitMetadataRawSchema>;
226
+ export type ListFilesWithMetadataResponseRaw = z.infer<
227
+ typeof listFilesWithMetadataResponseSchema
228
+ >;
205
229
  export type RawBranchInfo = z.infer<typeof branchInfoSchema>;
206
230
  export type ListBranchesResponseRaw = z.infer<
207
231
  typeof listBranchesResponseSchema
package/src/types.ts CHANGED
@@ -8,11 +8,14 @@ import type {
8
8
  ListBranchesResponseRaw,
9
9
  ListCommitsResponseRaw,
10
10
  ListFilesResponseRaw,
11
+ ListFilesWithMetadataResponseRaw,
11
12
  ListReposResponseRaw,
12
13
  NoteReadResponseRaw,
13
14
  NoteWriteResponseRaw,
14
15
  RawBranchInfo as SchemaRawBranchInfo,
16
+ RawCommitMetadata as SchemaRawCommitMetadata,
15
17
  RawCommitInfo as SchemaRawCommitInfo,
18
+ RawFileWithMetadata as SchemaRawFileWithMetadata,
16
19
  RawFileDiff as SchemaRawFileDiff,
17
20
  RawFilteredFile as SchemaRawFilteredFile,
18
21
  RawRepoBaseInfo as SchemaRawRepoBaseInfo,
@@ -49,6 +52,9 @@ export interface Repo {
49
52
  getFileStream(options: GetFileOptions): Promise<Response>;
50
53
  getArchiveStream(options?: ArchiveOptions): Promise<Response>;
51
54
  listFiles(options?: ListFilesOptions): Promise<ListFilesResult>;
55
+ listFilesWithMetadata(
56
+ options?: ListFilesWithMetadataOptions
57
+ ): Promise<ListFilesWithMetadataResult>;
52
58
  listBranches(options?: ListBranchesOptions): Promise<ListBranchesResult>;
53
59
  listCommits(options?: ListCommitsOptions): Promise<ListCommitsResult>;
54
60
  getNote(options: GetNoteOptions): Promise<GetNoteResult>;
@@ -84,6 +90,12 @@ export interface FindOneOptions {
84
90
  id: string;
85
91
  }
86
92
 
93
+ export interface RepoOptions {
94
+ id: string;
95
+ defaultBranch?: string;
96
+ createdAt?: string;
97
+ }
98
+
87
99
  export type SupportedRepoProvider = 'github';
88
100
 
89
101
  export interface PublicGitHubBaseRepoAuth {
@@ -190,6 +202,37 @@ export interface ListFilesResult {
190
202
  ref: string;
191
203
  }
192
204
 
205
+ export interface ListFilesWithMetadataOptions extends GitStorageInvocationOptions {
206
+ ref?: string;
207
+ ephemeral?: boolean;
208
+ }
209
+
210
+ export type RawFileWithMetadata = SchemaRawFileWithMetadata;
211
+
212
+ export interface FileWithMetadata {
213
+ path: string;
214
+ mode: string;
215
+ size: number;
216
+ lastCommitSha: string;
217
+ }
218
+
219
+ export type RawCommitMetadata = SchemaRawCommitMetadata;
220
+
221
+ export interface CommitMetadata {
222
+ author: string;
223
+ date: Date;
224
+ rawDate: string;
225
+ message: string;
226
+ }
227
+
228
+ export type ListFilesWithMetadataResponse = ListFilesWithMetadataResponseRaw;
229
+
230
+ export interface ListFilesWithMetadataResult {
231
+ files: FileWithMetadata[];
232
+ commits: Record<string, CommitMetadata>;
233
+ ref: string;
234
+ }
235
+
193
236
  // List Branches API types
194
237
  export interface ListBranchesOptions extends GitStorageInvocationOptions {
195
238
  cursor?: string;