@kevisual/cnb 0.0.78 → 0.0.79
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 +1 -1
- package/src/git/index.ts +28 -5
- package/src/repo/download.ts +26 -0
package/package.json
CHANGED
package/src/git/index.ts
CHANGED
|
@@ -69,11 +69,12 @@ export type Content = {
|
|
|
69
69
|
html_url: string;
|
|
70
70
|
git_url: string;
|
|
71
71
|
download_url: string;
|
|
72
|
-
type:
|
|
72
|
+
type: 'lfs' | 'blob';
|
|
73
73
|
content?: string;
|
|
74
|
-
encoding?:
|
|
74
|
+
encoding?: 'base64' | 'utf-8';
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
+
|
|
77
78
|
/**
|
|
78
79
|
* Git Blob 对象
|
|
79
80
|
*/
|
|
@@ -307,7 +308,7 @@ export class Git extends CNBCore {
|
|
|
307
308
|
* @param params 查询参数
|
|
308
309
|
* @returns 文件或目录内容
|
|
309
310
|
*/
|
|
310
|
-
async getContent(repo: string, params?: GetContentParams): Promise<Result<FileListContent>> {
|
|
311
|
+
async getContent(repo: string, params?: GetContentParams): Promise<Result<FileListContent | FileContent>> {
|
|
311
312
|
const url = `/${repo}/-/git/contents`;
|
|
312
313
|
return this.get({ url, params });
|
|
313
314
|
}
|
|
@@ -319,7 +320,7 @@ export class Git extends CNBCore {
|
|
|
319
320
|
* @param params 查询参数
|
|
320
321
|
* @returns 文件内容
|
|
321
322
|
*/
|
|
322
|
-
async getContentWithPath(repo: string, filePath: string, params?: GetContentWithPathParams): Promise<Result<FileListContent>> {
|
|
323
|
+
async getContentWithPath(repo: string, filePath: string, params?: GetContentWithPathParams): Promise<Result<FileListContent | FileContent>> {
|
|
323
324
|
const url = `/${repo}/-/git/contents/${filePath}`;
|
|
324
325
|
return this.get({ url, params });
|
|
325
326
|
}
|
|
@@ -711,7 +712,7 @@ type PutTagAnnotationsData = {
|
|
|
711
712
|
};
|
|
712
713
|
|
|
713
714
|
|
|
714
|
-
type FileListContent = {
|
|
715
|
+
export type FileListContent = {
|
|
715
716
|
type: 'blob' | 'tree';
|
|
716
717
|
size: number;
|
|
717
718
|
path: string;
|
|
@@ -725,4 +726,26 @@ type FIleEntry = {
|
|
|
725
726
|
sha: string;
|
|
726
727
|
path: string;
|
|
727
728
|
name: string;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
type FileContent = FileContentBase64 | FileContentLFS;
|
|
732
|
+
type FileContentBase64 = {
|
|
733
|
+
type: 'blob';
|
|
734
|
+
size: number;
|
|
735
|
+
path: string;
|
|
736
|
+
name: string;
|
|
737
|
+
sha: string;
|
|
738
|
+
encoding: 'base64';
|
|
739
|
+
content: string;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
type FileContentLFS = {
|
|
743
|
+
type: 'lfs';
|
|
744
|
+
size: number;
|
|
745
|
+
path: string;
|
|
746
|
+
name: string;
|
|
747
|
+
sha: string;
|
|
748
|
+
lfs_oid: string;
|
|
749
|
+
lfs_size: number;
|
|
750
|
+
lfs_download_url: string;
|
|
728
751
|
}
|
package/src/repo/download.ts
CHANGED
|
@@ -94,6 +94,32 @@ export const downloadByCNBApi: DownloadFn = async (url, opts) => {
|
|
|
94
94
|
return { code: res.status, buffer: null };
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
*
|
|
99
|
+
* @param url
|
|
100
|
+
* @param opts
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
export const downloadByLFSUrl: DownloadFn = async (url, opts) => {
|
|
104
|
+
const headers = {
|
|
105
|
+
Accept: "application/vnd.cnb.api+json",
|
|
106
|
+
// Authorization: `${CNB_API_KEY}`
|
|
107
|
+
...(opts?.token ? { Authorization: `${opts.token}` } : {}),
|
|
108
|
+
...opts?.headers
|
|
109
|
+
}
|
|
110
|
+
const res = await fetch(url, { headers });
|
|
111
|
+
const stream = opts?.stream || false;
|
|
112
|
+
if (res.ok) {
|
|
113
|
+
if (stream) {
|
|
114
|
+
const readableStream = res.body;
|
|
115
|
+
return { code: 200, buffer: null, stream: readableStream };
|
|
116
|
+
}
|
|
117
|
+
const arrayBuffer = await res.arrayBuffer();
|
|
118
|
+
return { code: 200, buffer: Buffer.from(arrayBuffer) };
|
|
119
|
+
}
|
|
120
|
+
console.error(`下载失败 ${url}: ${res.statusText}`);
|
|
121
|
+
return { code: res.status, buffer: null };
|
|
122
|
+
}
|
|
97
123
|
|
|
98
124
|
|
|
99
125
|
export const downloadResource: DownloadFn = async (url, opts) => {
|