@rws-framework/client 2.24.0 → 2.26.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,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/client",
3
3
  "private": false,
4
- "version": "2.24.0",
4
+ "version": "2.26.0",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
7
7
  "docs": "typedoc --tsconfig ./tsconfig.json"
@@ -41,7 +41,6 @@
41
41
  "reflect-metadata": "^0.2.2",
42
42
  "resolve-url-loader": "^5.0.0",
43
43
  "socket.io-client": "^4.7.2",
44
- "upload": "^1.3.2",
45
44
  "url-router": "^13.0.0",
46
45
  "uuid": "^9.0.1",
47
46
  "v4": "^0.0.1",
package/src/index.ts CHANGED
@@ -25,7 +25,7 @@ import type IRWSUser from './types/IRWSUser';
25
25
  import type { IAssetShowOptions, IRWSViewComponent } from './components/_component';
26
26
  import type { RWSDecoratorOptions } from './components/_decorator';
27
27
  import type { DOMOutputType, TagsProcessorType } from './services/DOMService';
28
- import type { IBackendRoute, IHTTProute, IPrefixedHTTProutes } from './services/ApiService';
28
+ import type { IBackendRoute, IHTTProute, IPrefixedHTTProutes, UploadFunctionOptions } from './services/ApiService';
29
29
  import type IRWSConfig from './types/IRWSConfig';
30
30
  import type RWSNotify from './types/RWSNotify';
31
31
  import type { NotifyUiType, NotifyLogType } from './types/RWSNotify';
@@ -87,5 +87,6 @@ export type {
87
87
  IRWSConfig,
88
88
  IRWSUser,
89
89
  TagsProcessorType,
90
- IRWSViewComponent
90
+ IRWSViewComponent,
91
+ UploadFunctionOptions as IRWSUploadFunctionOptions
91
92
  }
@@ -1,11 +1,10 @@
1
1
  import { ITypesResponse } from '../../../components/src/types/IBackendCore';
2
2
  import TheService from './_service';
3
+ import axios from 'axios';
3
4
 
4
5
  //@4DI
5
6
  import ConfigService, { ConfigServiceInstance } from './ConfigService';
6
7
 
7
- import { upload, UploadResponse } from 'upload';
8
-
9
8
  import { backend } from './_api/backend';
10
9
  import { calls } from './_api/calls';
11
10
 
@@ -44,12 +43,29 @@ interface IPrefixedHTTProutes<P = {[key: string]: any}> {
44
43
 
45
44
  type IBackendRoute = IHTTProute | IPrefixedHTTProutes;
46
45
 
46
+ interface UploadFunctionOptions {
47
+ headers?: Record<string, string>;
48
+ method?: 'POST' | 'PUT' | 'PATCH';
49
+ onProgress?: (progress: number) => void;
50
+ }
51
+
52
+ interface UploadResponse {
53
+ success: boolean;
54
+ data?: any;
55
+ error?: string;
56
+ }
47
57
 
48
58
 
49
59
  class ApiService extends TheService {
50
60
  static _DEFAULT: boolean = true;
51
61
  public token?: string;
52
62
 
63
+ private defaultUploadOptions: () => UploadFunctionOptions = () => ({
64
+ headers: this.token ? { Authorization: `Bearer ${this.token}` } : {},
65
+ method: 'POST' as const,
66
+ onProgress: (progress: number) => null,
67
+ });
68
+
53
69
  constructor(@ConfigService public config: ConfigServiceInstance) {
54
70
  super();
55
71
  }
@@ -69,20 +85,58 @@ class ApiService extends TheService {
69
85
  }
70
86
  }
71
87
 
72
- async uploadFile(url: string, file: File, onProgress: (progress: number) => void, payload: any = {}): Promise<UploadResponse>
73
- {
74
- return upload(
75
-
76
- url,
77
- {
78
- file,
79
- ...payload
80
- },
81
- {
82
- onProgress,
83
- headers: this.token ? { Authorization: `Bearer ${this.token}` } : null,
88
+ async uploadFile(url: string, files: Record<string, File>, payload: any = {}, uploadOptions: UploadFunctionOptions = this.defaultUploadOptions()): Promise<UploadResponse>
89
+ {
90
+ const formData = new FormData();
91
+
92
+ // Add files to FormData
93
+ Object.entries(files).forEach(([key, file]) => {
94
+ formData.append(key, file);
95
+ });
96
+
97
+ // Add payload data to FormData
98
+ Object.entries(payload).forEach(([key, value]) => {
99
+ if (value !== undefined && value !== null) {
100
+ formData.append(key, typeof value === 'object' ? JSON.stringify(value) : String(value));
84
101
  }
85
- );
102
+ });
103
+
104
+ const options = {
105
+ ...this.defaultUploadOptions(),
106
+ ...uploadOptions
107
+ };
108
+
109
+ try {
110
+ const method = options.method || 'POST';
111
+
112
+ const axiosConfig = {
113
+ method: method.toLowerCase() as any,
114
+ url,
115
+ data: formData,
116
+ headers: {
117
+ 'Content-Type': 'multipart/form-data',
118
+ ...options.headers
119
+ },
120
+ onUploadProgress: (progressEvent: any) => {
121
+ if (options.onProgress && progressEvent.total) {
122
+ const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total);
123
+ options.onProgress(progress);
124
+ }
125
+ }
126
+ };
127
+
128
+ const result = await axios(axiosConfig);
129
+
130
+ return {
131
+ success: true,
132
+ data: result.data
133
+ };
134
+ } catch (error: any) {
135
+ return {
136
+ success: false,
137
+ error: error.response?.data?.message || error.message || 'Upload failed'
138
+ };
139
+ }
86
140
  }
87
141
 
88
142
  public pureGet = calls.pureGet;
@@ -96,7 +150,7 @@ class ApiService extends TheService {
96
150
  post: async <T, P extends object = object>(routeName: string, payload?: P, options?: IAPIOptions): Promise<T> => calls.post.bind(this)(backend.getBackendUrl.bind(this)(routeName, options?.routeParams, options?.queryParams), payload, options) as Promise<T>,
97
151
  put: async <T, P extends object = object>(routeName: string, payload: P, options?: IAPIOptions): Promise<T> => calls.put.bind(this)(backend.getBackendUrl.bind(this)(routeName, options?.routeParams, options?.queryParams), payload, options) as Promise<T>,
98
152
  delete: async <T>(routeName: string, options?: IAPIOptions): Promise<T> => calls.delete.bind(this)(backend.getBackendUrl.bind(this)(routeName, options?.routeParams, options?.queryParams), options) as Promise<T>,
99
- uploadFile: async (routeName: string, file: File, onProgress: (progress: number) => void, options: IAPIOptions = {}, payload: any = {}): Promise<UploadResponse> => this.uploadFile(backend.getBackendUrl.bind(this)(routeName, options?.routeParams), file, onProgress, payload),
153
+ uploadFile: async (routeName: string, files: Record<string, File>, payload: any = {}, uploadOptions: UploadFunctionOptions = this.defaultUploadOptions(), options: IAPIOptions = {}): Promise<UploadResponse> => this.uploadFile(backend.getBackendUrl.bind(this)(routeName, options?.routeParams), files, payload, uploadOptions),
100
154
  };
101
155
 
102
156
  async getResource(resourceName: string): Promise<ITypesResponse>
@@ -108,4 +162,4 @@ class ApiService extends TheService {
108
162
  }
109
163
 
110
164
  export default ApiService.getSingleton();
111
- export { IBackendRoute, RequestOptions, ApiService as ApiServiceInstance, IHTTProute, IPrefixedHTTProutes, IAPIOptions };
165
+ export { IBackendRoute, RequestOptions, ApiService as ApiServiceInstance, IHTTProute, IPrefixedHTTProutes, IAPIOptions, UploadFunctionOptions };
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "compilerOptions": {
2
+ "compilerOptions": {
3
3
  "experimentalDecorators": true,
4
4
  "emitDecoratorMetadata": true,
5
5
  "target": "ES2018",
@@ -7,7 +7,7 @@
7
7
  "moduleResolution": "node",
8
8
  "strict": true,
9
9
  "esModuleInterop": true,
10
- "sourceMap": true,
10
+ "sourceMap": true,
11
11
  "strictNullChecks": false,
12
12
  "allowSyntheticDefaultImports": true,
13
13
  "lib": [