@pierre/storage 0.2.3 → 0.4.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 +2 -0
- package/dist/index.cjs +70 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -3
- package/dist/index.d.ts +21 -3
- package/dist/index.js +70 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/commit.ts +2 -0
- package/src/diff-commit.ts +2 -0
- package/src/fetch.ts +2 -0
- package/src/index.ts +61 -16
- package/src/types.ts +13 -0
- package/src/version.ts +8 -0
package/package.json
CHANGED
package/src/commit.ts
CHANGED
|
@@ -20,6 +20,7 @@ import type {
|
|
|
20
20
|
CreateCommitOptions,
|
|
21
21
|
LegacyCreateCommitOptions,
|
|
22
22
|
} from './types';
|
|
23
|
+
import { getUserAgent } from './version';
|
|
23
24
|
|
|
24
25
|
const DEFAULT_TTL_SECONDS = 60 * 60;
|
|
25
26
|
const HEADS_REF_PREFIX = 'refs/heads/';
|
|
@@ -294,6 +295,7 @@ export class FetchCommitTransport implements CommitTransport {
|
|
|
294
295
|
Authorization: `Bearer ${request.authorization}`,
|
|
295
296
|
'Content-Type': 'application/x-ndjson',
|
|
296
297
|
Accept: 'application/json',
|
|
298
|
+
'Code-Storage-Agent': getUserAgent(),
|
|
297
299
|
},
|
|
298
300
|
body: body as any,
|
|
299
301
|
signal: request.signal,
|
package/src/diff-commit.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
CreateCommitFromDiffOptions,
|
|
17
17
|
DiffSource,
|
|
18
18
|
} from './types';
|
|
19
|
+
import { getUserAgent } from './version';
|
|
19
20
|
|
|
20
21
|
interface DiffCommitMetadataPayload {
|
|
21
22
|
target_branch: string;
|
|
@@ -190,6 +191,7 @@ export class FetchDiffCommitTransport implements DiffCommitTransport {
|
|
|
190
191
|
Authorization: `Bearer ${request.authorization}`,
|
|
191
192
|
'Content-Type': 'application/x-ndjson',
|
|
192
193
|
Accept: 'application/json',
|
|
194
|
+
'Code-Storage-Agent': getUserAgent(),
|
|
193
195
|
},
|
|
194
196
|
body: body as any,
|
|
195
197
|
signal: request.signal,
|
package/src/fetch.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { errorEnvelopeSchema } from './schemas';
|
|
2
2
|
import type { ValidAPIVersion, ValidMethod, ValidPath } from './types';
|
|
3
|
+
import { getUserAgent } from './version';
|
|
3
4
|
|
|
4
5
|
interface RequestOptions {
|
|
5
6
|
allowedStatus?: number[];
|
|
@@ -59,6 +60,7 @@ export class ApiFetcher {
|
|
|
59
60
|
headers: {
|
|
60
61
|
Authorization: `Bearer ${jwt}`,
|
|
61
62
|
'Content-Type': 'application/json',
|
|
63
|
+
'Code-Storage-Agent': getUserAgent(),
|
|
62
64
|
},
|
|
63
65
|
};
|
|
64
66
|
|
package/src/index.ts
CHANGED
|
@@ -32,6 +32,8 @@ import type {
|
|
|
32
32
|
CreateCommitFromDiffOptions,
|
|
33
33
|
CreateCommitOptions,
|
|
34
34
|
CreateRepoOptions,
|
|
35
|
+
DeleteRepoOptions,
|
|
36
|
+
DeleteRepoResult,
|
|
35
37
|
DiffFileState,
|
|
36
38
|
FileDiff,
|
|
37
39
|
FilteredFile,
|
|
@@ -53,7 +55,6 @@ import type {
|
|
|
53
55
|
ListCommitsResult,
|
|
54
56
|
ListFilesOptions,
|
|
55
57
|
ListFilesResult,
|
|
56
|
-
OverrideableGitStorageOptions,
|
|
57
58
|
PullUpstreamOptions,
|
|
58
59
|
RawBranchInfo,
|
|
59
60
|
RawCommitInfo,
|
|
@@ -346,22 +347,20 @@ class RepoImpl implements Repo {
|
|
|
346
347
|
) => Promise<string>,
|
|
347
348
|
) {
|
|
348
349
|
this.api = getApiInstance(
|
|
349
|
-
this.options.apiBaseUrl ??
|
|
350
|
+
this.options.apiBaseUrl ?? GitStorage.getDefaultAPIBaseUrl(options.name),
|
|
350
351
|
this.options.apiVersion ?? API_VERSION,
|
|
351
352
|
);
|
|
352
353
|
}
|
|
353
354
|
|
|
354
355
|
async getRemoteURL(urlOptions?: GetRemoteURLOptions): Promise<string> {
|
|
355
|
-
const
|
|
356
|
-
const url = new URL(`https://${this.options.name}.${storageBaseUrl}/${this.id}.git`);
|
|
356
|
+
const url = new URL(`https://${this.options.storageBaseUrl}/${this.id}.git`);
|
|
357
357
|
url.username = `t`;
|
|
358
358
|
url.password = await this.generateJWT(this.id, urlOptions);
|
|
359
359
|
return url.toString();
|
|
360
360
|
}
|
|
361
361
|
|
|
362
362
|
async getEphemeralRemoteURL(urlOptions?: GetRemoteURLOptions): Promise<string> {
|
|
363
|
-
const
|
|
364
|
-
const url = new URL(`https://${this.options.name}.${storageBaseUrl}/${this.id}+ephemeral.git`);
|
|
363
|
+
const url = new URL(`https://${this.options.storageBaseUrl}/${this.id}+ephemeral.git`);
|
|
365
364
|
url.username = `t`;
|
|
366
365
|
url.password = await this.generateJWT(this.id, urlOptions);
|
|
367
366
|
return url.toString();
|
|
@@ -384,6 +383,9 @@ class RepoImpl implements Repo {
|
|
|
384
383
|
if (typeof options.ephemeral === 'boolean') {
|
|
385
384
|
params.ephemeral = String(options.ephemeral);
|
|
386
385
|
}
|
|
386
|
+
if (typeof options.ephemeralBase === 'boolean') {
|
|
387
|
+
params.ephemeral_base = String(options.ephemeralBase);
|
|
388
|
+
}
|
|
387
389
|
|
|
388
390
|
// Return the raw fetch Response for streaming
|
|
389
391
|
return this.api.get({ path: 'repos/file', params }, jwt);
|
|
@@ -488,6 +490,12 @@ class RepoImpl implements Repo {
|
|
|
488
490
|
if (options.base) {
|
|
489
491
|
params.base = options.base;
|
|
490
492
|
}
|
|
493
|
+
if (typeof options.ephemeral === 'boolean') {
|
|
494
|
+
params.ephemeral = String(options.ephemeral);
|
|
495
|
+
}
|
|
496
|
+
if (typeof options.ephemeralBase === 'boolean') {
|
|
497
|
+
params.ephemeral_base = String(options.ephemeralBase);
|
|
498
|
+
}
|
|
491
499
|
|
|
492
500
|
const response = await this.api.get({ path: 'repos/branches/diff', params }, jwt);
|
|
493
501
|
|
|
@@ -506,6 +514,10 @@ class RepoImpl implements Repo {
|
|
|
506
514
|
sha: options.sha,
|
|
507
515
|
};
|
|
508
516
|
|
|
517
|
+
if (options.baseSha) {
|
|
518
|
+
params.baseSha = options.baseSha;
|
|
519
|
+
}
|
|
520
|
+
|
|
509
521
|
const response = await this.api.get({ path: 'repos/diff', params }, jwt);
|
|
510
522
|
|
|
511
523
|
const raw = commitDiffResponseSchema.parse(await response.json());
|
|
@@ -653,7 +665,7 @@ class RepoImpl implements Repo {
|
|
|
653
665
|
|
|
654
666
|
createCommit(options: CreateCommitOptions): CommitBuilder {
|
|
655
667
|
const version = this.options.apiVersion ?? API_VERSION;
|
|
656
|
-
const baseUrl = this.options.apiBaseUrl ??
|
|
668
|
+
const baseUrl = this.options.apiBaseUrl ?? GitStorage.getDefaultAPIBaseUrl(this.options.name);
|
|
657
669
|
const transport = new FetchCommitTransport({ baseUrl, version });
|
|
658
670
|
const ttl = resolveCommitTtlSeconds(options);
|
|
659
671
|
const builderOptions: CreateCommitOptions = {
|
|
@@ -675,7 +687,7 @@ class RepoImpl implements Repo {
|
|
|
675
687
|
|
|
676
688
|
async createCommitFromDiff(options: CreateCommitFromDiffOptions): Promise<CommitResult> {
|
|
677
689
|
const version = this.options.apiVersion ?? API_VERSION;
|
|
678
|
-
const baseUrl = this.options.apiBaseUrl ??
|
|
690
|
+
const baseUrl = this.options.apiBaseUrl ?? GitStorage.getDefaultAPIBaseUrl(this.options.name);
|
|
679
691
|
const transport = new FetchDiffCommitTransport({ baseUrl, version });
|
|
680
692
|
const ttl = resolveCommitTtlSeconds(options);
|
|
681
693
|
const requestOptions: CreateCommitFromDiffOptions = {
|
|
@@ -697,7 +709,6 @@ class RepoImpl implements Repo {
|
|
|
697
709
|
}
|
|
698
710
|
|
|
699
711
|
export class GitStorage {
|
|
700
|
-
private static overrides: OverrideableGitStorageOptions = {};
|
|
701
712
|
private options: GitStorageOptions;
|
|
702
713
|
private api: ApiFetcher;
|
|
703
714
|
|
|
@@ -722,12 +733,11 @@ export class GitStorage {
|
|
|
722
733
|
throw new Error('GitStorage key must be a non-empty string.');
|
|
723
734
|
}
|
|
724
735
|
|
|
725
|
-
const resolvedApiBaseUrl =
|
|
726
|
-
|
|
727
|
-
const resolvedApiVersion = options.apiVersion ?? GitStorage.overrides.apiVersion ?? API_VERSION;
|
|
736
|
+
const resolvedApiBaseUrl = options.apiBaseUrl ?? GitStorage.getDefaultAPIBaseUrl(options.name);
|
|
737
|
+
const resolvedApiVersion = options.apiVersion ?? API_VERSION;
|
|
728
738
|
const resolvedStorageBaseUrl =
|
|
729
|
-
options.storageBaseUrl ?? GitStorage.
|
|
730
|
-
const resolvedDefaultTtl = options.defaultTTL
|
|
739
|
+
options.storageBaseUrl ?? GitStorage.getDefaultStorageBaseUrl(options.name);
|
|
740
|
+
const resolvedDefaultTtl = options.defaultTTL;
|
|
731
741
|
|
|
732
742
|
this.api = getApiInstance(resolvedApiBaseUrl, resolvedApiVersion);
|
|
733
743
|
|
|
@@ -741,8 +751,12 @@ export class GitStorage {
|
|
|
741
751
|
};
|
|
742
752
|
}
|
|
743
753
|
|
|
744
|
-
static
|
|
745
|
-
|
|
754
|
+
static getDefaultAPIBaseUrl(name: string): string {
|
|
755
|
+
return API_BASE_URL.replace('{{org}}', name);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
static getDefaultStorageBaseUrl(name: string): string {
|
|
759
|
+
return STORAGE_BASE_URL.replace('{{org}}', name);
|
|
746
760
|
}
|
|
747
761
|
|
|
748
762
|
/**
|
|
@@ -808,6 +822,34 @@ export class GitStorage {
|
|
|
808
822
|
return new RepoImpl(options.id, this.options, this.generateJWT.bind(this));
|
|
809
823
|
}
|
|
810
824
|
|
|
825
|
+
/**
|
|
826
|
+
* Delete a repository by ID
|
|
827
|
+
* @param options The delete options containing the repo ID
|
|
828
|
+
* @returns The deletion result
|
|
829
|
+
*/
|
|
830
|
+
async deleteRepo(options: DeleteRepoOptions): Promise<DeleteRepoResult> {
|
|
831
|
+
const ttl = resolveInvocationTtlSeconds(options, DEFAULT_TOKEN_TTL_SECONDS);
|
|
832
|
+
const jwt = await this.generateJWT(options.id, {
|
|
833
|
+
permissions: ['repo:write'],
|
|
834
|
+
ttl,
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
// Allow 404 and 409 for clearer error handling
|
|
838
|
+
const resp = await this.api.delete('repos/delete', jwt, { allowedStatus: [404, 409] });
|
|
839
|
+
if (resp.status === 404) {
|
|
840
|
+
throw new Error('Repository not found');
|
|
841
|
+
}
|
|
842
|
+
if (resp.status === 409) {
|
|
843
|
+
throw new Error('Repository already deleted');
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
const body = (await resp.json()) as { repo_id: string; message: string };
|
|
847
|
+
return {
|
|
848
|
+
repoId: body.repo_id,
|
|
849
|
+
message: body.message,
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
|
|
811
853
|
/**
|
|
812
854
|
* Get the current configuration
|
|
813
855
|
* @returns The client configuration
|
|
@@ -853,5 +895,8 @@ export function createClient(options: GitStorageOptions): GitStorage {
|
|
|
853
895
|
return new GitStorage(options);
|
|
854
896
|
}
|
|
855
897
|
|
|
898
|
+
// Export CodeStorage as an alias for GitStorage
|
|
899
|
+
export { GitStorage as CodeStorage };
|
|
900
|
+
|
|
856
901
|
// Type alias for backward compatibility
|
|
857
902
|
export type StorageOptions = GitStorageOptions;
|
package/src/types.ts
CHANGED
|
@@ -88,11 +88,21 @@ export interface CreateRepoOptions extends GitStorageInvocationOptions {
|
|
|
88
88
|
defaultBranch?: string;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
export interface DeleteRepoOptions extends GitStorageInvocationOptions {
|
|
92
|
+
id: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface DeleteRepoResult {
|
|
96
|
+
repoId: string;
|
|
97
|
+
message: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
91
100
|
// Get File API types
|
|
92
101
|
export interface GetFileOptions extends GitStorageInvocationOptions {
|
|
93
102
|
path: string;
|
|
94
103
|
ref?: string;
|
|
95
104
|
ephemeral?: boolean;
|
|
105
|
+
ephemeralBase?: boolean;
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
export interface PullUpstreamOptions extends GitStorageInvocationOptions {
|
|
@@ -184,6 +194,8 @@ export interface ListCommitsResult {
|
|
|
184
194
|
export interface GetBranchDiffOptions extends GitStorageInvocationOptions {
|
|
185
195
|
branch: string;
|
|
186
196
|
base?: string;
|
|
197
|
+
ephemeral?: boolean;
|
|
198
|
+
ephemeralBase?: boolean;
|
|
187
199
|
}
|
|
188
200
|
|
|
189
201
|
export type GetBranchDiffResponse = GetBranchDiffResponseRaw;
|
|
@@ -199,6 +211,7 @@ export interface GetBranchDiffResult {
|
|
|
199
211
|
// Commit Diff API types
|
|
200
212
|
export interface GetCommitDiffOptions extends GitStorageInvocationOptions {
|
|
201
213
|
sha: string;
|
|
214
|
+
baseSha?: string;
|
|
202
215
|
}
|
|
203
216
|
|
|
204
217
|
export type GetCommitDiffResponse = GetCommitDiffResponseRaw;
|
package/src/version.ts
ADDED