@flashbacktech/flashbackclient 0.0.17 → 0.0.20
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/dist/api/client.d.ts +7 -0
- package/dist/api/client.js +27 -0
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.js +2 -0
- package/dist/consumer/file/delete.d.ts +3 -0
- package/dist/consumer/file/delete.js +17 -0
- package/dist/consumer/file/getUrl.d.ts +3 -0
- package/dist/consumer/file/getUrl.js +14 -0
- package/dist/consumer/file/index.d.ts +30 -0
- package/dist/consumer/file/index.js +45 -0
- package/dist/consumer/file/types.d.ts +25 -0
- package/dist/consumer/file/types.js +1 -0
- package/dist/consumer/file/upload.d.ts +3 -0
- package/dist/consumer/file/upload.js +69 -0
- package/dist/consumer/index.d.ts +1 -70
- package/dist/consumer/index.js +1 -143
- package/package.json +7 -10
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
export class ApiClient {
|
|
3
|
+
constructor(baseURL = 'https://api.flashback.tech') {
|
|
4
|
+
this.setAuthToken = (token) => {
|
|
5
|
+
if (token) {
|
|
6
|
+
this.apiClient.defaults.headers.common['Authorization'] = `Bearer ${token}`;
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
delete this.apiClient.defaults.headers.common['Authorization'];
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
this.authenticateGoogle = async (token) => {
|
|
13
|
+
const response = await this.apiClient.post('/auth/google', { token });
|
|
14
|
+
return response.data;
|
|
15
|
+
};
|
|
16
|
+
this.authenticateGithub = async (code) => {
|
|
17
|
+
const response = await this.apiClient.post('/auth/github', { code });
|
|
18
|
+
return response.data;
|
|
19
|
+
};
|
|
20
|
+
this.apiClient = axios.create({
|
|
21
|
+
baseURL: baseURL,
|
|
22
|
+
headers: {
|
|
23
|
+
'Content-Type': 'application/json',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
export async function deleteFile(client, params) {
|
|
3
|
+
try {
|
|
4
|
+
const { data } = await client.delete('/file/', {
|
|
5
|
+
params,
|
|
6
|
+
data: params,
|
|
7
|
+
});
|
|
8
|
+
return data;
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
if (axios.isAxiosError(error) && error.response?.data) {
|
|
12
|
+
const errorData = error.response.data;
|
|
13
|
+
throw new Error(`Delete failed: ${errorData.message}`);
|
|
14
|
+
}
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
export async function getUrl(client, params) {
|
|
3
|
+
try {
|
|
4
|
+
const { data } = await client.get('/file/', { params });
|
|
5
|
+
return data;
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
if (axios.isAxiosError(error) && error.response?.data) {
|
|
9
|
+
const errorData = error.response.data;
|
|
10
|
+
throw new Error(`Get URL failed: ${errorData.message}`);
|
|
11
|
+
}
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DeleteParams, DeleteResponse, GetUrlParams, UploadRequest, UploadResponse } from './types';
|
|
2
|
+
export type { DeleteParams, DeleteResponse, GetUrlParams, UploadRequest, UploadResponse };
|
|
3
|
+
export interface StorageClientConfig {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
timeout?: number;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
export declare class StorageClient {
|
|
9
|
+
private client;
|
|
10
|
+
private readonly config;
|
|
11
|
+
constructor(config: StorageClientConfig);
|
|
12
|
+
/**
|
|
13
|
+
* Upload a file to storage
|
|
14
|
+
* @param params Upload parameters including file data and type
|
|
15
|
+
* @returns Promise with the file URL
|
|
16
|
+
*/
|
|
17
|
+
upload(params: UploadRequest): Promise<UploadResponse>;
|
|
18
|
+
/**
|
|
19
|
+
* Get the URL for a file
|
|
20
|
+
* @param params File identifier parameters
|
|
21
|
+
* @returns Promise with the file URL
|
|
22
|
+
*/
|
|
23
|
+
getUrl(params: GetUrlParams): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Delete a file from storage
|
|
26
|
+
* @param params File identifier parameters
|
|
27
|
+
* @returns Promise indicating success
|
|
28
|
+
*/
|
|
29
|
+
delete(params: DeleteParams): Promise<DeleteResponse>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { upload } from './upload';
|
|
3
|
+
import { getUrl } from './getUrl';
|
|
4
|
+
import { deleteFile } from './delete';
|
|
5
|
+
export class StorageClient {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.config = {
|
|
8
|
+
baseUrl: config.baseUrl.replace(/\/$/, ''),
|
|
9
|
+
timeout: config.timeout || 30000,
|
|
10
|
+
headers: {
|
|
11
|
+
'Content-Type': 'application/json',
|
|
12
|
+
...config.headers,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
this.client = axios.create({
|
|
16
|
+
baseURL: this.config.baseUrl,
|
|
17
|
+
timeout: this.config.timeout,
|
|
18
|
+
headers: this.config.headers,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Upload a file to storage
|
|
23
|
+
* @param params Upload parameters including file data and type
|
|
24
|
+
* @returns Promise with the file URL
|
|
25
|
+
*/
|
|
26
|
+
async upload(params) {
|
|
27
|
+
return upload(this.client, params);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get the URL for a file
|
|
31
|
+
* @param params File identifier parameters
|
|
32
|
+
* @returns Promise with the file URL
|
|
33
|
+
*/
|
|
34
|
+
async getUrl(params) {
|
|
35
|
+
return getUrl(this.client, params);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Delete a file from storage
|
|
39
|
+
* @param params File identifier parameters
|
|
40
|
+
* @returns Promise indicating success
|
|
41
|
+
*/
|
|
42
|
+
async delete(params) {
|
|
43
|
+
return deleteFile(this.client, params);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Buffer, Blob, File } from 'buffer';
|
|
2
|
+
export interface UploadRequest {
|
|
3
|
+
folderId: string;
|
|
4
|
+
fileId: string;
|
|
5
|
+
data: string | Buffer | ArrayBuffer | Blob | File | globalThis.File;
|
|
6
|
+
type: 'base64' | 'binary' | 'multipart';
|
|
7
|
+
}
|
|
8
|
+
export interface UploadResponse {
|
|
9
|
+
url: string;
|
|
10
|
+
}
|
|
11
|
+
export interface GetUrlParams {
|
|
12
|
+
folderId: string;
|
|
13
|
+
fileId: string;
|
|
14
|
+
}
|
|
15
|
+
export interface DeleteParams {
|
|
16
|
+
folderId: string;
|
|
17
|
+
fileId: string;
|
|
18
|
+
}
|
|
19
|
+
export interface DeleteResponse {
|
|
20
|
+
success: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface ErrorResponse {
|
|
23
|
+
error: string;
|
|
24
|
+
message: string;
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
//import type { Blob } from 'buffer';
|
|
3
|
+
import { FormData } from 'formdata-node';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
export async function upload(client, params) {
|
|
6
|
+
try {
|
|
7
|
+
let url = '';
|
|
8
|
+
switch (params.type) {
|
|
9
|
+
case 'multipart': {
|
|
10
|
+
const formData = new FormData();
|
|
11
|
+
formData.append('type', 'multipart');
|
|
12
|
+
formData.append('folderId', params.folderId);
|
|
13
|
+
formData.append('fileId', params.fileId);
|
|
14
|
+
// If params.data is already a Blob/File, use it directly
|
|
15
|
+
// Otherwise, create a new Blob with the correct type
|
|
16
|
+
const fileBlob = params.data;
|
|
17
|
+
formData.append('file', fileBlob);
|
|
18
|
+
const response = await client.post('/file/', formData, {
|
|
19
|
+
headers: {
|
|
20
|
+
'Content-Type': 'multipart/form-data',
|
|
21
|
+
},
|
|
22
|
+
// Prevent any data transformation
|
|
23
|
+
transformRequest: [(data) => data],
|
|
24
|
+
// Ensure binary data handling
|
|
25
|
+
responseType: 'json',
|
|
26
|
+
maxBodyLength: Infinity,
|
|
27
|
+
maxContentLength: Infinity,
|
|
28
|
+
});
|
|
29
|
+
url = response.data.url;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 'binary': {
|
|
33
|
+
// Binary upload
|
|
34
|
+
const response = await client.post('/file/', params.data, {
|
|
35
|
+
headers: {
|
|
36
|
+
'Content-Type': 'application/octet-stream',
|
|
37
|
+
'x-upload-type': 'binary',
|
|
38
|
+
'x-folder-id': params.folderId,
|
|
39
|
+
'x-file-id': params.fileId,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
url = response.data.url;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case 'base64': {
|
|
46
|
+
const response = await client.post('/file/', {
|
|
47
|
+
type: 'base64',
|
|
48
|
+
folderId: params.folderId,
|
|
49
|
+
fileId: params.fileId,
|
|
50
|
+
data: params.data,
|
|
51
|
+
}, {
|
|
52
|
+
headers: { 'Content-Type': 'application/json' },
|
|
53
|
+
});
|
|
54
|
+
url = response.data.url;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
default:
|
|
58
|
+
throw new Error('Invalid file type');
|
|
59
|
+
}
|
|
60
|
+
return { url };
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (axios.isAxiosError(error) && error.response?.data) {
|
|
64
|
+
const errorData = error.response.data;
|
|
65
|
+
throw new Error(`Upload failed: ${errorData.message}`);
|
|
66
|
+
}
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
package/dist/consumer/index.d.ts
CHANGED
|
@@ -1,70 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export interface UploadRequest {
|
|
3
|
-
folderId: string;
|
|
4
|
-
fileId: string;
|
|
5
|
-
data: string | Blob | Buffer | File | ArrayBuffer;
|
|
6
|
-
type: 'base64' | 'binary' | 'multipart';
|
|
7
|
-
}
|
|
8
|
-
export interface UploadResponse {
|
|
9
|
-
url: string;
|
|
10
|
-
}
|
|
11
|
-
export interface GetUrlParams {
|
|
12
|
-
folder_id: string;
|
|
13
|
-
file_id: string;
|
|
14
|
-
}
|
|
15
|
-
export interface DeleteParams {
|
|
16
|
-
folder_id: string;
|
|
17
|
-
file_id: string;
|
|
18
|
-
}
|
|
19
|
-
export interface DeleteResponse {
|
|
20
|
-
success: boolean;
|
|
21
|
-
}
|
|
22
|
-
export interface ErrorResponse {
|
|
23
|
-
error: string;
|
|
24
|
-
message: string;
|
|
25
|
-
}
|
|
26
|
-
interface StorageClientConfig {
|
|
27
|
-
baseUrl: string;
|
|
28
|
-
timeout?: number;
|
|
29
|
-
headers?: Record<string, string>;
|
|
30
|
-
}
|
|
31
|
-
export declare class StorageClient {
|
|
32
|
-
private static instance;
|
|
33
|
-
private client;
|
|
34
|
-
private readonly config;
|
|
35
|
-
private constructor();
|
|
36
|
-
/**
|
|
37
|
-
* Initialize and get the StorageClient instance
|
|
38
|
-
* @param config - Configuration for the storage service
|
|
39
|
-
* @returns StorageClient instance
|
|
40
|
-
*/
|
|
41
|
-
static initialize(config: StorageClientConfig): StorageClient;
|
|
42
|
-
/**
|
|
43
|
-
* Get the current instance of StorageClient
|
|
44
|
-
* @throws Error if client hasn't been initialized
|
|
45
|
-
*/
|
|
46
|
-
static getInstance(): StorageClient;
|
|
47
|
-
/**
|
|
48
|
-
* Reset the client instance (useful for testing or reconfiguration)
|
|
49
|
-
*/
|
|
50
|
-
static reset(): void;
|
|
51
|
-
/**
|
|
52
|
-
* Upload a file to storage
|
|
53
|
-
* @param params Upload parameters including file data and type
|
|
54
|
-
* @returns Promise with the file URL
|
|
55
|
-
*/
|
|
56
|
-
upload(params: UploadRequest): Promise<UploadResponse>;
|
|
57
|
-
/**
|
|
58
|
-
* Get the URL for a file
|
|
59
|
-
* @param params File identifier parameters
|
|
60
|
-
* @returns Promise with the file URL
|
|
61
|
-
*/
|
|
62
|
-
getUrl(params: GetUrlParams): Promise<UploadResponse>;
|
|
63
|
-
/**
|
|
64
|
-
* Delete a file from storage
|
|
65
|
-
* @param params File identifier parameters
|
|
66
|
-
* @returns Promise indicating success
|
|
67
|
-
*/
|
|
68
|
-
delete(params: DeleteParams): Promise<DeleteResponse>;
|
|
69
|
-
}
|
|
70
|
-
export {};
|
|
1
|
+
export * from './file';
|
package/dist/consumer/index.js
CHANGED
|
@@ -1,143 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { FormData } from 'formdata-node';
|
|
3
|
-
export class StorageClient {
|
|
4
|
-
constructor(config) {
|
|
5
|
-
this.config = {
|
|
6
|
-
baseUrl: config.baseUrl.replace(/\/$/, ''),
|
|
7
|
-
timeout: config.timeout || 30000,
|
|
8
|
-
headers: {
|
|
9
|
-
'Content-Type': 'application/json',
|
|
10
|
-
...config.headers,
|
|
11
|
-
},
|
|
12
|
-
};
|
|
13
|
-
this.client = axios.create({
|
|
14
|
-
baseURL: this.config.baseUrl,
|
|
15
|
-
timeout: this.config.timeout,
|
|
16
|
-
headers: this.config.headers,
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Initialize and get the StorageClient instance
|
|
21
|
-
* @param config - Configuration for the storage service
|
|
22
|
-
* @returns StorageClient instance
|
|
23
|
-
*/
|
|
24
|
-
static initialize(config) {
|
|
25
|
-
if (!StorageClient.instance) {
|
|
26
|
-
StorageClient.instance = new StorageClient(config);
|
|
27
|
-
}
|
|
28
|
-
return StorageClient.instance;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Get the current instance of StorageClient
|
|
32
|
-
* @throws Error if client hasn't been initialized
|
|
33
|
-
*/
|
|
34
|
-
static getInstance() {
|
|
35
|
-
if (!StorageClient.instance) {
|
|
36
|
-
throw new Error('StorageClient has not been initialized. Call initialize() first.');
|
|
37
|
-
}
|
|
38
|
-
return StorageClient.instance;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Reset the client instance (useful for testing or reconfiguration)
|
|
42
|
-
*/
|
|
43
|
-
static reset() {
|
|
44
|
-
StorageClient.instance = null;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Upload a file to storage
|
|
48
|
-
* @param params Upload parameters including file data and type
|
|
49
|
-
* @returns Promise with the file URL
|
|
50
|
-
*/
|
|
51
|
-
async upload(params) {
|
|
52
|
-
try {
|
|
53
|
-
let url = '';
|
|
54
|
-
switch (params.type) {
|
|
55
|
-
case 'multipart': {
|
|
56
|
-
const formData = new FormData();
|
|
57
|
-
formData.append('type', 'multipart');
|
|
58
|
-
formData.append('folderId', params.folderId);
|
|
59
|
-
formData.append('fileId', params.fileId);
|
|
60
|
-
formData.append('file', params.data);
|
|
61
|
-
const response = await this.client.post('/', formData, {
|
|
62
|
-
headers: { 'Content-Type': 'multipart/form-data' },
|
|
63
|
-
// Note: axios will automatically set the correct Content-Type boundary
|
|
64
|
-
});
|
|
65
|
-
url = response.data.url;
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
case 'binary': {
|
|
69
|
-
// Binary upload
|
|
70
|
-
const response = await this.client.post('/', params.data, {
|
|
71
|
-
headers: {
|
|
72
|
-
'Content-Type': 'application/octet-stream',
|
|
73
|
-
'x-upload-type': 'binary',
|
|
74
|
-
'x-folder-id': params.folderId,
|
|
75
|
-
'x-file-id': params.fileId,
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
url = response.data.url;
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
case 'base64': {
|
|
82
|
-
const response = await this.client.post('/', {
|
|
83
|
-
type: 'base64',
|
|
84
|
-
folderId: params.folderId,
|
|
85
|
-
fileId: params.fileId,
|
|
86
|
-
data: params.data,
|
|
87
|
-
}, {
|
|
88
|
-
headers: { 'Content-Type': 'application/json' },
|
|
89
|
-
});
|
|
90
|
-
url = response.data.url;
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
default:
|
|
94
|
-
throw new Error('Invalid file type');
|
|
95
|
-
}
|
|
96
|
-
return { url };
|
|
97
|
-
}
|
|
98
|
-
catch (error) {
|
|
99
|
-
if (axios.isAxiosError(error) && error.response?.data) {
|
|
100
|
-
const errorData = error.response.data;
|
|
101
|
-
throw new Error(`Upload failed: ${errorData.message}`);
|
|
102
|
-
}
|
|
103
|
-
throw error;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Get the URL for a file
|
|
108
|
-
* @param params File identifier parameters
|
|
109
|
-
* @returns Promise with the file URL
|
|
110
|
-
*/
|
|
111
|
-
async getUrl(params) {
|
|
112
|
-
try {
|
|
113
|
-
const { data } = await this.client.get('/', { params });
|
|
114
|
-
return data;
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
if (axios.isAxiosError(error) && error.response?.data) {
|
|
118
|
-
const errorData = error.response.data;
|
|
119
|
-
throw new Error(`Get URL failed: ${errorData.message}`);
|
|
120
|
-
}
|
|
121
|
-
throw error;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Delete a file from storage
|
|
126
|
-
* @param params File identifier parameters
|
|
127
|
-
* @returns Promise indicating success
|
|
128
|
-
*/
|
|
129
|
-
async delete(params) {
|
|
130
|
-
try {
|
|
131
|
-
const { data } = await this.client.delete('/', { params });
|
|
132
|
-
return data;
|
|
133
|
-
}
|
|
134
|
-
catch (error) {
|
|
135
|
-
if (axios.isAxiosError(error) && error.response?.data) {
|
|
136
|
-
const errorData = error.response.data;
|
|
137
|
-
throw new Error(`Delete failed: ${errorData.message}`);
|
|
138
|
-
}
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
StorageClient.instance = null;
|
|
1
|
+
export * from './file';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flashbacktech/flashbackclient",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -36,22 +36,19 @@
|
|
|
36
36
|
"import": "./dist/stellar/index.js",
|
|
37
37
|
"default": "./dist/stellar/index.js"
|
|
38
38
|
},
|
|
39
|
-
"./
|
|
40
|
-
"types": "./dist/
|
|
41
|
-
"import": "./dist/
|
|
42
|
-
"default": "./dist/
|
|
43
|
-
},
|
|
44
|
-
"./path3": {
|
|
45
|
-
"types": "./dist/path3/index.d.ts",
|
|
46
|
-
"import": "./dist/path3/index.js",
|
|
47
|
-
"default": "./dist/path3/index.js"
|
|
39
|
+
"./api": {
|
|
40
|
+
"types": "./dist/api/index.d.ts",
|
|
41
|
+
"import": "./dist/api/index.js",
|
|
42
|
+
"default": "./dist/api/index.js"
|
|
48
43
|
}
|
|
49
44
|
},
|
|
50
45
|
"files": [
|
|
51
46
|
"dist"
|
|
52
47
|
],
|
|
53
48
|
"devDependencies": {
|
|
49
|
+
"@aws-sdk/client-s3": "^3.726.1",
|
|
54
50
|
"@eslint/js": "^8.56.0",
|
|
51
|
+
"@google-cloud/storage": "^7.15.0",
|
|
55
52
|
"@types/jest": "^29.5.14",
|
|
56
53
|
"@types/node": "^22.10.1",
|
|
57
54
|
"@typescript-eslint/eslint-plugin": "^8.17.0",
|