@oasis-path/gamma-sdk 1.0.8 → 1.0.9

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 ADDED
File without changes
package/dist/client.d.ts CHANGED
@@ -1,14 +1,25 @@
1
- import { GammaFilesClientConfig, UploadFileOptions, UploadFileResponse, ListFilesResponse, FileMetadataResponse, DeleteFileResponse, PresignedUrlOptions, PresignedUrlResponse, ViewFileByTokenResult } from './types';
1
+ import { GammaFilesClientConfig, UploadFileOptions, UploadFileResponse, UploadWithPresignedTokenOptions, DownloadFileWithPresignedTokenOptions, ListFilesResponse, FileMetadataResponse, DeleteFileResponse, PresignedUrlOptions, PresignedUrlResponse, PresignedUploadUrlResponse, PresignedFolderDownloadUrlOptions, PresignedFolderDownloadUrlResponse, PrepareFolderDownloadOptions, PrepareFolderDownloadResponse, CheckPreparedDownloadResponse, DownloadFolderOptions, ViewFileByTokenResult } from './types';
2
+ import { GeneratePresignedUploadUrlOptions } from './methods/generatePresignedUploadUrl';
2
3
  export declare class GammaFilesClient {
3
4
  private baseUrl;
4
5
  private apiKey;
6
+ private jwtToken?;
5
7
  constructor(config: GammaFilesClientConfig);
8
+ setJwtToken(token: string): void;
6
9
  uploadFile(options: UploadFileOptions): Promise<UploadFileResponse>;
10
+ uploadFileWithPresignedToken(options: UploadWithPresignedTokenOptions): Promise<UploadFileResponse>;
11
+ uploadFileWithJWT(jwtToken: string, options: UploadFileOptions): Promise<UploadFileResponse>;
12
+ generatePresignedUploadUrl(options: GeneratePresignedUploadUrlOptions): Promise<PresignedUploadUrlResponse>;
7
13
  downloadFile(fileId: string): Promise<ArrayBuffer>;
14
+ downloadFileWithPresignedToken(options: DownloadFileWithPresignedTokenOptions): Promise<ArrayBuffer>;
8
15
  listFiles(folderId: string): Promise<ListFilesResponse>;
9
16
  deleteFile(fileId: string): Promise<DeleteFileResponse>;
10
17
  getFileMetadata(fileId: string): Promise<FileMetadataResponse>;
11
18
  generatePresignedUrl(options: PresignedUrlOptions): Promise<PresignedUrlResponse>;
12
19
  viewFileByToken(fileId: string, token: string, compressionValue?: number): Promise<ViewFileByTokenResult>;
20
+ generatePresignedFolderDownloadUrl(options: PresignedFolderDownloadUrlOptions): Promise<PresignedFolderDownloadUrlResponse>;
21
+ prepareFolderDownload(options: PrepareFolderDownloadOptions): Promise<PrepareFolderDownloadResponse>;
22
+ checkPreparedDownload(folderId: string): Promise<CheckPreparedDownloadResponse>;
23
+ downloadFolder(options: DownloadFolderOptions): Promise<ArrayBuffer>;
13
24
  private request;
14
25
  }
package/dist/client.js CHANGED
@@ -2,24 +2,48 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GammaFilesClient = void 0;
4
4
  const uploadFile_1 = require("./methods/uploadFile");
5
+ const uploadFileWithPresignedToken_1 = require("./methods/uploadFileWithPresignedToken");
6
+ const uploadFileWithJWT_1 = require("./methods/uploadFileWithJWT");
5
7
  const downloadFile_1 = require("./methods/downloadFile");
8
+ const downloadFileWithPresignedToken_1 = require("./methods/downloadFileWithPresignedToken");
6
9
  const listFiles_1 = require("./methods/listFiles");
7
10
  const deleteFile_1 = require("./methods/deleteFile");
8
11
  const getFileMetadata_1 = require("./methods/getFileMetadata");
9
12
  const generatePresignedUrl_1 = require("./methods/generatePresignedUrl");
13
+ const generatePresignedUploadUrl_1 = require("./methods/generatePresignedUploadUrl");
10
14
  const viewFileByToken_1 = require("./methods/viewFileByToken");
15
+ const folderOperations_1 = require("./methods/folderOperations");
11
16
  const request_1 = require("./methods/request");
12
17
  class GammaFilesClient {
13
18
  constructor(config) {
14
19
  this.baseUrl = config.baseUrl.replace(/\/$/, '');
15
20
  this.apiKey = config.apiKey;
21
+ this.jwtToken = config.jwtToken;
22
+ }
23
+ setJwtToken(token) {
24
+ this.jwtToken = token;
16
25
  }
17
26
  async uploadFile(options) {
18
27
  return (0, uploadFile_1.uploadFile)(this.baseUrl, this.apiKey, options, this.request.bind(this));
19
28
  }
29
+ async uploadFileWithPresignedToken(options) {
30
+ return (0, uploadFileWithPresignedToken_1.uploadFileWithPresignedToken)(this.baseUrl, options, this.request.bind(this));
31
+ }
32
+ async uploadFileWithJWT(jwtToken, options) {
33
+ return (0, uploadFileWithJWT_1.uploadFileWithJWT)(this.baseUrl, jwtToken, options, this.request.bind(this));
34
+ }
35
+ async generatePresignedUploadUrl(options) {
36
+ if (!this.jwtToken) {
37
+ throw new Error('JWT token required. Call setJwtToken() first or use the jwtToken in constructor.');
38
+ }
39
+ return (0, generatePresignedUploadUrl_1.generatePresignedUploadUrl)(options, this.request.bind(this));
40
+ }
20
41
  async downloadFile(fileId) {
21
42
  return (0, downloadFile_1.downloadFile)(fileId, this.request.bind(this));
22
43
  }
44
+ async downloadFileWithPresignedToken(options) {
45
+ return (0, downloadFileWithPresignedToken_1.downloadFileWithPresignedToken)(this.baseUrl, options, this.request.bind(this));
46
+ }
23
47
  async listFiles(folderId) {
24
48
  return (0, listFiles_1.listFiles)(folderId, this.request.bind(this));
25
49
  }
@@ -35,6 +59,18 @@ class GammaFilesClient {
35
59
  async viewFileByToken(fileId, token, compressionValue) {
36
60
  return (0, viewFileByToken_1.viewFileByToken)(fileId, token, this.request.bind(this), compressionValue);
37
61
  }
62
+ async generatePresignedFolderDownloadUrl(options) {
63
+ return (0, folderOperations_1.generatePresignedFolderDownloadUrl)(options, this.request.bind(this));
64
+ }
65
+ async prepareFolderDownload(options) {
66
+ return (0, folderOperations_1.prepareFolderDownload)(options, this.request.bind(this));
67
+ }
68
+ async checkPreparedDownload(folderId) {
69
+ return (0, folderOperations_1.checkPreparedDownload)(folderId, this.request.bind(this));
70
+ }
71
+ async downloadFolder(options) {
72
+ return (0, folderOperations_1.downloadFolder)(this.baseUrl, options, this.request.bind(this));
73
+ }
38
74
  async request(method, path, options) {
39
75
  return (0, request_1.makeRequest)(method, path, this.baseUrl, this.apiKey, options);
40
76
  }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,61 @@
1
- export { GammaFilesClient } from './client';
2
- export * from './types';
1
+ export interface GammaFilesClientConfig {
2
+ baseUrl: string;
3
+ apiKey?: string;
4
+ }
5
+ export interface FileMetadata {
6
+ id: string;
7
+ name: string;
8
+ originalName: string;
9
+ mimeType: string;
10
+ size: number;
11
+ folderId: string;
12
+ uploadedBy: string;
13
+ createdAt: string;
14
+ updatedAt: string | null;
15
+ }
16
+ export interface ListFolderFilesResponse {
17
+ success: true;
18
+ files: FileMetadata[];
19
+ }
20
+ export interface UploadPresignedOptions {
21
+ file: Buffer | Blob | File | NodeJS.ReadableStream;
22
+ filename: string;
23
+ mimeType?: string;
24
+ folderId?: string;
25
+ token: string;
26
+ }
27
+ export interface UploadPresignedResponse {
28
+ success: true;
29
+ file: {
30
+ id: string;
31
+ name: string;
32
+ originalName: string;
33
+ mimeType: string;
34
+ size: number;
35
+ folderId: string;
36
+ uploadedBy: string;
37
+ createdAt: string;
38
+ };
39
+ }
40
+ export interface ViewFileResult {
41
+ data: ArrayBuffer;
42
+ headers: {
43
+ contentType: string;
44
+ contentLength: number;
45
+ contentDisposition: string;
46
+ cacheControl?: string;
47
+ cdnCacheControl?: string;
48
+ contentEncoding?: string;
49
+ vary?: string;
50
+ };
51
+ }
52
+ export declare class GammaFilesClient {
53
+ private baseUrl;
54
+ private apiKey?;
55
+ constructor(config: GammaFilesClientConfig);
56
+ uploadPresigned(options: UploadPresignedOptions): Promise<UploadPresignedResponse>;
57
+ downloadPresigned(fileId: string, token: string): Promise<ArrayBuffer>;
58
+ downloadFolder(folderId: string, token: string): Promise<ArrayBuffer>;
59
+ viewFile(fileId: string, token: string, compressionValue?: number): Promise<ViewFileResult>;
60
+ listFolderFiles(folderId: string): Promise<ListFolderFilesResponse>;
61
+ }
package/dist/index.js CHANGED
@@ -1,20 +1,34 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
3
  exports.GammaFilesClient = void 0;
18
- var client_1 = require("./client");
19
- Object.defineProperty(exports, "GammaFilesClient", { enumerable: true, get: function () { return client_1.GammaFilesClient; } });
20
- __exportStar(require("./types"), exports);
4
+ const uploadPresigned_1 = require("./methods/uploadPresigned");
5
+ const downloadPresigned_1 = require("./methods/downloadPresigned");
6
+ const downloadFolder_1 = require("./methods/downloadFolder");
7
+ const viewFile_1 = require("./methods/viewFile");
8
+ const listFolderFiles_1 = require("./methods/listFolderFiles");
9
+ // Client
10
+ class GammaFilesClient {
11
+ constructor(config) {
12
+ this.baseUrl = config.baseUrl.replace(/\/$/, '');
13
+ this.apiKey = config.apiKey;
14
+ }
15
+ async uploadPresigned(options) {
16
+ return (0, uploadPresigned_1.uploadPresigned)(options, this.baseUrl);
17
+ }
18
+ async downloadPresigned(fileId, token) {
19
+ return (0, downloadPresigned_1.downloadPresigned)(fileId, token, this.baseUrl);
20
+ }
21
+ async downloadFolder(folderId, token) {
22
+ return (0, downloadFolder_1.downloadFolder)(folderId, token, this.baseUrl);
23
+ }
24
+ async viewFile(fileId, token, compressionValue) {
25
+ return (0, viewFile_1.viewFile)(fileId, token, this.baseUrl, compressionValue);
26
+ }
27
+ async listFolderFiles(folderId) {
28
+ if (!this.apiKey) {
29
+ throw new Error('API key is required for listFolderFiles method. Please provide apiKey in the constructor config.');
30
+ }
31
+ return (0, listFolderFiles_1.listFolderFiles)(folderId, this.apiKey, this.baseUrl);
32
+ }
33
+ }
34
+ exports.GammaFilesClient = GammaFilesClient;
@@ -0,0 +1,2 @@
1
+ import { DownloadFileWithPresignedTokenOptions } from '../types';
2
+ export declare function downloadFileWithPresignedToken(baseUrl: string, options: DownloadFileWithPresignedTokenOptions, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<ArrayBuffer>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.downloadFileWithPresignedToken = downloadFileWithPresignedToken;
4
+ async function downloadFileWithPresignedToken(baseUrl, options, request) {
5
+ const { fileId, token } = options;
6
+ const url = `/api/files/download-presigned/${fileId}?token=${encodeURIComponent(token)}`;
7
+ return request('GET', url, {
8
+ binary: true,
9
+ skipAuth: true, // Presigned token is in URL
10
+ });
11
+ }
@@ -0,0 +1 @@
1
+ export declare function downloadFolder(folderId: string, token: string, baseUrl: string): Promise<ArrayBuffer>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.downloadFolder = downloadFolder;
4
+ async function downloadFolder(folderId, token, baseUrl) {
5
+ const url = new URL(`/api/files/download-folder/${folderId}`, baseUrl);
6
+ url.searchParams.append('token', token);
7
+ const response = await fetch(url.toString());
8
+ if (!response.ok) {
9
+ const errorText = await response.text();
10
+ let errorData;
11
+ try {
12
+ errorData = JSON.parse(errorText);
13
+ }
14
+ catch {
15
+ errorData = { error: errorText };
16
+ }
17
+ throw new Error(errorData.error || errorData.message || `Download folder failed with status ${response.status}`);
18
+ }
19
+ return await response.arrayBuffer();
20
+ }
@@ -0,0 +1 @@
1
+ export declare function downloadPresigned(fileId: string, token: string, baseUrl: string): Promise<ArrayBuffer>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.downloadPresigned = downloadPresigned;
4
+ async function downloadPresigned(fileId, token, baseUrl) {
5
+ const url = new URL(`/api/files/download-presigned/${fileId}`, baseUrl);
6
+ url.searchParams.append('token', token);
7
+ const response = await fetch(url.toString());
8
+ if (!response.ok) {
9
+ const errorText = await response.text();
10
+ let errorData;
11
+ try {
12
+ errorData = JSON.parse(errorText);
13
+ }
14
+ catch {
15
+ errorData = { error: errorText };
16
+ }
17
+ throw new Error(errorData.error || errorData.message || `Download presigned failed with status ${response.status}`);
18
+ }
19
+ return await response.arrayBuffer();
20
+ }
@@ -0,0 +1,5 @@
1
+ import { PresignedFolderDownloadUrlOptions, PresignedFolderDownloadUrlResponse, PrepareFolderDownloadOptions, PrepareFolderDownloadResponse, CheckPreparedDownloadResponse, DownloadFolderOptions } from '../types';
2
+ export declare function generatePresignedFolderDownloadUrl(options: PresignedFolderDownloadUrlOptions, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<PresignedFolderDownloadUrlResponse>;
3
+ export declare function prepareFolderDownload(options: PrepareFolderDownloadOptions, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<PrepareFolderDownloadResponse>;
4
+ export declare function checkPreparedDownload(folderId: string, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<CheckPreparedDownloadResponse>;
5
+ export declare function downloadFolder(baseUrl: string, options: DownloadFolderOptions, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<ArrayBuffer>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generatePresignedFolderDownloadUrl = generatePresignedFolderDownloadUrl;
4
+ exports.prepareFolderDownload = prepareFolderDownload;
5
+ exports.checkPreparedDownload = checkPreparedDownload;
6
+ exports.downloadFolder = downloadFolder;
7
+ async function generatePresignedFolderDownloadUrl(options, request) {
8
+ const url = '/api/files/presigned-download-folder-url';
9
+ const payload = {
10
+ folderId: options.folderId,
11
+ expiresIn: options.expiresIn || 3600
12
+ };
13
+ return request('POST', url, {
14
+ body: JSON.stringify(payload),
15
+ headers: {
16
+ 'Content-Type': 'application/json',
17
+ },
18
+ });
19
+ }
20
+ async function prepareFolderDownload(options, request) {
21
+ const url = '/api/files/prepare-folder-download';
22
+ const payload = {
23
+ folderId: options.folderId
24
+ };
25
+ return request('POST', url, {
26
+ body: JSON.stringify(payload),
27
+ headers: {
28
+ 'Content-Type': 'application/json',
29
+ },
30
+ });
31
+ }
32
+ async function checkPreparedDownload(folderId, request) {
33
+ const url = `/api/files/check-prepared-download/${folderId}`;
34
+ return request('GET', url);
35
+ }
36
+ async function downloadFolder(baseUrl, options, request) {
37
+ const { folderId, token } = options;
38
+ const url = `/api/files/download-folder/${folderId}?token=${encodeURIComponent(token)}`;
39
+ return request('GET', url, {
40
+ binary: true,
41
+ skipAuth: true, // Presigned token is in URL
42
+ });
43
+ }
@@ -0,0 +1,6 @@
1
+ import { PresignedUploadUrlResponse } from '../types';
2
+ export interface GeneratePresignedUploadUrlOptions {
3
+ folderId?: string;
4
+ expiresIn?: number;
5
+ }
6
+ export declare function generatePresignedUploadUrl(options: GeneratePresignedUploadUrlOptions, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<PresignedUploadUrlResponse>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generatePresignedUploadUrl = generatePresignedUploadUrl;
4
+ async function generatePresignedUploadUrl(options, request) {
5
+ const url = '/api/files/presigned-upload-url';
6
+ const payload = {
7
+ folderId: options.folderId,
8
+ expiresIn: options.expiresIn || 3600
9
+ };
10
+ return request('POST', url, {
11
+ body: JSON.stringify(payload),
12
+ headers: {
13
+ 'Content-Type': 'application/json',
14
+ },
15
+ });
16
+ }
@@ -0,0 +1,16 @@
1
+ export interface FileMetadata {
2
+ id: string;
3
+ name: string;
4
+ originalName: string;
5
+ mimeType: string;
6
+ size: number;
7
+ folderId: string;
8
+ uploadedBy: string;
9
+ createdAt: string;
10
+ updatedAt: string | null;
11
+ }
12
+ export interface ListFolderFilesResponse {
13
+ success: true;
14
+ files: FileMetadata[];
15
+ }
16
+ export declare function listFolderFiles(folderId: string, apiKey: string, baseUrl: string): Promise<ListFolderFilesResponse>;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listFolderFiles = listFolderFiles;
4
+ const request_1 = require("./request");
5
+ async function listFolderFiles(folderId, apiKey, baseUrl) {
6
+ return (0, request_1.makeRequest)('GET', `/trpc/listFolderFiles?input=${encodeURIComponent(JSON.stringify({ folderId }))}`, baseUrl, apiKey);
7
+ }
@@ -1,7 +1,5 @@
1
1
  export declare function makeRequest<T>(method: string, path: string, baseUrl: string, apiKey: string, options?: {
2
2
  body?: any;
3
3
  headers?: Record<string, string>;
4
- binary?: boolean;
5
- skipAuth?: boolean;
6
- includeHeaders?: boolean;
4
+ query?: Record<string, string>;
7
5
  }): Promise<T>;
@@ -1,122 +1,46 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
3
  exports.makeRequest = makeRequest;
40
- const https = __importStar(require("https"));
41
- const http = __importStar(require("http"));
42
- const url_1 = require("url");
43
- const form_data_1 = __importDefault(require("form-data"));
44
- async function makeRequest(method, path, baseUrl, apiKey, options = {}) {
45
- const url = new url_1.URL(path, baseUrl);
46
- const isHttps = url.protocol === 'https:';
47
- const lib = isHttps ? https : http;
48
- return new Promise((resolve, reject) => {
49
- const headers = {
50
- ...options.headers,
51
- };
52
- if (!options.skipAuth) {
53
- headers['Authorization'] = `Bearer ${apiKey}`;
54
- }
55
- if (options.body && !(options.body instanceof form_data_1.default)) {
56
- if (typeof options.body === 'string') {
57
- headers['Content-Length'] = Buffer.byteLength(options.body).toString();
4
+ async function makeRequest(method, path, baseUrl, apiKey, options) {
5
+ const url = new URL(path, baseUrl);
6
+ if (options?.query) {
7
+ Object.entries(options.query).forEach(([key, value]) => {
8
+ if (value !== undefined && value !== null) {
9
+ url.searchParams.append(key, value);
58
10
  }
59
- }
60
- const reqOptions = {
61
- method,
62
- headers,
63
- };
64
- const req = lib.request(url, reqOptions, (res) => {
65
- const chunks = [];
66
- res.on('data', (chunk) => {
67
- chunks.push(chunk);
68
- });
69
- res.on('end', () => {
70
- const buffer = Buffer.concat(chunks);
71
- const responseText = buffer.toString();
72
- if (res.statusCode && res.statusCode >= 400) {
73
- const errorBody = JSON.parse(responseText);
74
- const fullError = new Error(errorBody.error || errorBody.message || `HTTP ${res.statusCode}`);
75
- fullError.statusCode = res.statusCode;
76
- fullError.responseBody = errorBody;
77
- reject(fullError);
78
- return;
79
- }
80
- if (options.binary) {
81
- const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
82
- if (options.includeHeaders) {
83
- const result = {
84
- data: arrayBuffer,
85
- headers: {
86
- contentType: res.headers['content-type'] || '',
87
- contentLength: parseInt(res.headers['content-length'] || '0', 10),
88
- contentDisposition: res.headers['content-disposition'] || '',
89
- etag: res.headers['etag'],
90
- cacheControl: res.headers['cache-control'],
91
- cfCacheTag: res.headers['cf-cache-tag'],
92
- }
93
- };
94
- resolve(result);
95
- }
96
- else {
97
- resolve(arrayBuffer);
98
- }
99
- }
100
- else {
101
- const data = JSON.parse(responseText);
102
- resolve(data);
103
- }
104
- });
105
- });
106
- req.on('error', (err) => {
107
- reject(err);
108
11
  });
109
- if (options.body) {
110
- if (options.body instanceof form_data_1.default) {
111
- options.body.pipe(req);
112
- }
113
- else {
114
- req.write(options.body);
115
- req.end();
116
- }
12
+ }
13
+ const headers = {
14
+ 'X-API-Key': apiKey,
15
+ ...options?.headers,
16
+ };
17
+ if (options?.body && typeof options.body === 'object' && !(options.body instanceof FormData)) {
18
+ headers['Content-Type'] = 'application/json';
19
+ }
20
+ const fetchOptions = {
21
+ method,
22
+ headers,
23
+ };
24
+ if (options?.body) {
25
+ fetchOptions.body = options.body instanceof FormData || typeof options.body === 'string'
26
+ ? options.body
27
+ : JSON.stringify(options.body);
28
+ }
29
+ const response = await fetch(url.toString(), fetchOptions);
30
+ if (!response.ok) {
31
+ const errorText = await response.text();
32
+ let errorData;
33
+ try {
34
+ errorData = JSON.parse(errorText);
117
35
  }
118
- else {
119
- req.end();
36
+ catch {
37
+ errorData = { error: errorText };
120
38
  }
121
- });
39
+ throw new Error(errorData.error || errorData.message || `Request failed with status ${response.status}`);
40
+ }
41
+ const contentType = response.headers.get('content-type');
42
+ if (contentType?.includes('application/json')) {
43
+ return await response.json();
44
+ }
45
+ return response;
122
46
  }
@@ -0,0 +1,2 @@
1
+ import { UploadFileOptions, UploadFileResponse } from '../types';
2
+ export declare function uploadFileWithJWT(baseUrl: string, jwtToken: string, options: UploadFileOptions, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<UploadFileResponse>;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.uploadFileWithJWT = uploadFileWithJWT;
7
+ const form_data_1 = __importDefault(require("form-data"));
8
+ const stream_1 = require("stream");
9
+ async function uploadFileWithJWT(baseUrl, jwtToken, options, request) {
10
+ const { file, filename, mimeType, folderId } = options;
11
+ const form = new form_data_1.default();
12
+ let contentType = mimeType || 'application/octet-stream';
13
+ // Handle different file input types
14
+ if (file instanceof stream_1.Readable || file.pipe) {
15
+ form.append('file', file, {
16
+ filename,
17
+ contentType,
18
+ });
19
+ }
20
+ else if (Buffer.isBuffer(file)) {
21
+ form.append('file', file, {
22
+ filename,
23
+ contentType,
24
+ });
25
+ }
26
+ else if (typeof file === 'string') {
27
+ const fileBuffer = Buffer.from(file, 'base64');
28
+ form.append('file', fileBuffer, {
29
+ filename,
30
+ contentType,
31
+ });
32
+ }
33
+ else if (typeof Blob !== 'undefined' && file instanceof Blob) {
34
+ const arrayBuffer = await file.arrayBuffer();
35
+ const fileBuffer = Buffer.from(arrayBuffer);
36
+ contentType = mimeType || file.type || 'application/octet-stream';
37
+ form.append('file', fileBuffer, {
38
+ filename,
39
+ contentType,
40
+ });
41
+ }
42
+ else if (typeof File !== 'undefined' && file instanceof File) {
43
+ const arrayBuffer = await file.arrayBuffer();
44
+ const fileBuffer = Buffer.from(arrayBuffer);
45
+ contentType = mimeType || file.type || 'application/octet-stream';
46
+ form.append('file', fileBuffer, {
47
+ filename,
48
+ contentType,
49
+ });
50
+ }
51
+ else {
52
+ throw new Error('Unsupported file type. Supported types: Buffer, Stream, Blob, File, or base64 string');
53
+ }
54
+ const url = folderId
55
+ ? `/api/files/upload-auth/${folderId}`
56
+ : '/api/files/upload-auth';
57
+ const headers = {
58
+ ...form.getHeaders(),
59
+ 'Authorization': `Bearer ${jwtToken}`
60
+ };
61
+ return request('POST', url, {
62
+ body: form,
63
+ headers: headers,
64
+ skipAuth: true, // Using JWT instead of API key
65
+ });
66
+ }
@@ -0,0 +1,2 @@
1
+ import { UploadWithPresignedTokenOptions, UploadFileResponse } from '../types';
2
+ export declare function uploadFileWithPresignedToken(baseUrl: string, options: UploadWithPresignedTokenOptions, request: <T>(method: string, path: string, options?: any) => Promise<T>): Promise<UploadFileResponse>;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.uploadFileWithPresignedToken = uploadFileWithPresignedToken;
7
+ const form_data_1 = __importDefault(require("form-data"));
8
+ const stream_1 = require("stream");
9
+ async function uploadFileWithPresignedToken(baseUrl, options, request) {
10
+ const { file, filename, mimeType, token, folderId } = options;
11
+ const form = new form_data_1.default();
12
+ let contentType = mimeType || 'application/octet-stream';
13
+ // Handle different file input types
14
+ if (file instanceof stream_1.Readable || file.pipe) {
15
+ // Stream support for Node.js - efficient for large files
16
+ form.append('file', file, {
17
+ filename,
18
+ contentType,
19
+ });
20
+ }
21
+ else if (Buffer.isBuffer(file)) {
22
+ // Direct buffer support
23
+ form.append('file', file, {
24
+ filename,
25
+ contentType,
26
+ });
27
+ }
28
+ else if (typeof file === 'string') {
29
+ // Handle base64 string - convert to buffer
30
+ const fileBuffer = Buffer.from(file, 'base64');
31
+ form.append('file', fileBuffer, {
32
+ filename,
33
+ contentType,
34
+ });
35
+ }
36
+ else if (typeof Blob !== 'undefined' && file instanceof Blob) {
37
+ // Browser Blob support - must convert to buffer
38
+ const arrayBuffer = await file.arrayBuffer();
39
+ const fileBuffer = Buffer.from(arrayBuffer);
40
+ contentType = mimeType || file.type || 'application/octet-stream';
41
+ form.append('file', fileBuffer, {
42
+ filename,
43
+ contentType,
44
+ });
45
+ }
46
+ else if (typeof File !== 'undefined' && file instanceof File) {
47
+ // Browser File support - must convert to buffer
48
+ const arrayBuffer = await file.arrayBuffer();
49
+ const fileBuffer = Buffer.from(arrayBuffer);
50
+ contentType = mimeType || file.type || 'application/octet-stream';
51
+ form.append('file', fileBuffer, {
52
+ filename,
53
+ contentType,
54
+ });
55
+ }
56
+ else {
57
+ throw new Error('Unsupported file type. Supported types: Buffer, Stream, Blob, File, or base64 string');
58
+ }
59
+ const url = folderId
60
+ ? `/api/files/upload-presigned/${folderId}?token=${encodeURIComponent(token)}`
61
+ : `/api/files/upload-presigned?token=${encodeURIComponent(token)}`;
62
+ return request('POST', url, {
63
+ body: form,
64
+ headers: form.getHeaders(),
65
+ skipAuth: true, // Presigned token is in URL
66
+ });
67
+ }
@@ -0,0 +1,21 @@
1
+ export interface UploadPresignedOptions {
2
+ file: Buffer | Blob | File | NodeJS.ReadableStream;
3
+ filename: string;
4
+ mimeType?: string;
5
+ folderId?: string;
6
+ token: string;
7
+ }
8
+ export interface UploadPresignedResponse {
9
+ success: true;
10
+ file: {
11
+ id: string;
12
+ name: string;
13
+ originalName: string;
14
+ mimeType: string;
15
+ size: number;
16
+ folderId: string;
17
+ uploadedBy: string;
18
+ createdAt: string;
19
+ };
20
+ }
21
+ export declare function uploadPresigned(options: UploadPresignedOptions, baseUrl: string): Promise<UploadPresignedResponse>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uploadPresigned = uploadPresigned;
4
+ async function uploadPresigned(options, baseUrl) {
5
+ const { file, filename, mimeType, folderId, token } = options;
6
+ const formData = new FormData();
7
+ // Handle different file input types
8
+ if (file instanceof Buffer) {
9
+ const blob = new Blob([file], { type: mimeType || 'application/octet-stream' });
10
+ formData.append('file', blob, filename);
11
+ }
12
+ else if (typeof file === 'object' && 'pipe' in file) {
13
+ // NodeJS.ReadableStream - not directly supported in FormData in browser
14
+ throw new Error('ReadableStream not supported in browser environment. Use Buffer or Blob.');
15
+ }
16
+ else {
17
+ // Blob or File
18
+ formData.append('file', file, filename);
19
+ }
20
+ const url = new URL(`/api/files/upload-presigned/${folderId || ''}`, baseUrl);
21
+ url.searchParams.append('token', token);
22
+ const response = await fetch(url.toString(), {
23
+ method: 'POST',
24
+ body: formData,
25
+ });
26
+ if (!response.ok) {
27
+ const errorText = await response.text();
28
+ let errorData;
29
+ try {
30
+ errorData = JSON.parse(errorText);
31
+ }
32
+ catch {
33
+ errorData = { error: errorText };
34
+ }
35
+ throw new Error(errorData.error || errorData.message || `Upload presigned failed with status ${response.status}`);
36
+ }
37
+ return await response.json();
38
+ }
@@ -0,0 +1,13 @@
1
+ export interface ViewFileResult {
2
+ data: ArrayBuffer;
3
+ headers: {
4
+ contentType: string;
5
+ contentLength: number;
6
+ contentDisposition: string;
7
+ cacheControl?: string;
8
+ cdnCacheControl?: string;
9
+ contentEncoding?: string;
10
+ vary?: string;
11
+ };
12
+ }
13
+ export declare function viewFile(fileId: string, token: string, baseUrl: string, compressionValue?: number): Promise<ViewFileResult>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.viewFile = viewFile;
4
+ async function viewFile(fileId, token, baseUrl, compressionValue) {
5
+ const url = new URL(`/api/files/view/${fileId}`, baseUrl);
6
+ url.searchParams.append('token', token);
7
+ if (compressionValue !== undefined) {
8
+ url.searchParams.append('compressionValue', compressionValue.toString());
9
+ }
10
+ const response = await fetch(url.toString());
11
+ if (!response.ok) {
12
+ const errorText = await response.text();
13
+ let errorData;
14
+ try {
15
+ errorData = JSON.parse(errorText);
16
+ }
17
+ catch {
18
+ errorData = { error: errorText };
19
+ }
20
+ throw new Error(errorData.error || errorData.message || `View file failed with status ${response.status}`);
21
+ }
22
+ const data = await response.arrayBuffer();
23
+ return {
24
+ data,
25
+ headers: {
26
+ contentType: response.headers.get('content-type') || 'application/octet-stream',
27
+ contentLength: parseInt(response.headers.get('content-length') || '0', 10),
28
+ contentDisposition: response.headers.get('content-disposition') || '',
29
+ cacheControl: response.headers.get('cache-control') || undefined,
30
+ cdnCacheControl: response.headers.get('cdn-cache-control') || undefined,
31
+ contentEncoding: response.headers.get('content-encoding') || undefined,
32
+ vary: response.headers.get('vary') || undefined,
33
+ },
34
+ };
35
+ }
package/dist/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export interface GammaFilesClientConfig {
2
2
  baseUrl: string;
3
3
  apiKey: string;
4
+ jwtToken?: string;
4
5
  }
5
6
  export interface FileMetadata {
6
7
  id: string;
@@ -60,6 +61,52 @@ export interface ViewFileByTokenResult {
60
61
  cfCacheTag?: string;
61
62
  };
62
63
  }
64
+ export interface UploadWithPresignedTokenOptions {
65
+ file: Buffer | Blob | File | string | NodeJS.ReadableStream;
66
+ filename: string;
67
+ mimeType?: string;
68
+ token: string;
69
+ folderId?: string;
70
+ }
71
+ export interface DownloadFileWithPresignedTokenOptions {
72
+ fileId: string;
73
+ token: string;
74
+ }
75
+ export interface PresignedUploadUrlResponse {
76
+ success: true;
77
+ token: string;
78
+ url: string;
79
+ expiresAt: string;
80
+ }
81
+ export interface PresignedFolderDownloadUrlOptions {
82
+ folderId: string;
83
+ expiresIn?: number;
84
+ }
85
+ export interface PresignedFolderDownloadUrlResponse {
86
+ success: true;
87
+ token: string;
88
+ url: string;
89
+ expiresAt: string;
90
+ }
91
+ export interface PrepareFolderDownloadOptions {
92
+ folderId: string;
93
+ }
94
+ export interface PrepareFolderDownloadResponse {
95
+ success: true;
96
+ message: string;
97
+ zipPath: string;
98
+ size: number;
99
+ }
100
+ export interface CheckPreparedDownloadResponse {
101
+ success: true;
102
+ exists: boolean;
103
+ zipPath?: string;
104
+ size?: number;
105
+ }
106
+ export interface DownloadFolderOptions {
107
+ folderId: string;
108
+ token: string;
109
+ }
63
110
  export interface ApiErrorResponse {
64
111
  success: false;
65
112
  error: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oasis-path/gamma-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "TypeScript SDK for Gamma Files API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,8 @@
8
8
  "scripts": {
9
9
  "build": "tsc",
10
10
  "watch": "tsc --watch",
11
- "prepublishOnly": "pnpm run build"
11
+ "prepublishOnly": "pnpm run build",
12
+ "publish": "pnpm run build && git add . && git commit -m 'chore: publish new version' && git push && pnpm publish --no-git-checks"
12
13
  },
13
14
  "keywords": [
14
15
  "gamma",