@rws-framework/client 2.15.6 → 2.16.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/builder/webpack/rws.webpack.config.js +1 -3
- package/cfg/build_steps/webpack/_aliases.js +5 -2
- package/cfg/build_steps/webpack/_loaders.js +8 -1
- package/cfg/build_steps/webpack/_rws_externals.js +5 -5
- package/package.json +3 -2
- package/src/client.ts +1 -9
- package/src/index.ts +21 -17
- package/src/services/ApiService.ts +32 -169
- package/src/services/_api/backend.ts +88 -0
- package/src/services/_api/calls.ts +95 -0
- package/src/types/IBackendCore.ts +12 -0
- package/src/types/IRWSResource.ts +5 -0
|
@@ -63,11 +63,9 @@ const RWSWebpackWrapper = async (appRoot, rwsFrontendConfig, _packageDir) => {
|
|
|
63
63
|
const WEBPACK_AFTER_ACTIONS = rwsFrontendConfig.actions || [];
|
|
64
64
|
const WEBPACK_AFTER_ERROR_ACTIONS = rwsFrontendConfig.error_actions || [];
|
|
65
65
|
|
|
66
|
-
const modules_setup = [path.
|
|
67
|
-
|
|
66
|
+
const modules_setup = [path.join(_packageDir, 'node_modules'), path.join(executionDir, 'node_modules'), path.join(tools.findRootWorkspacePath(appRoot), 'node_modules')];
|
|
68
67
|
let optimConfig = null;
|
|
69
68
|
let aliases = rwsFrontendConfig.aliases || {};
|
|
70
|
-
|
|
71
69
|
aliases = { ...aliases, ...loadAliases(_packageDir, tsConfig,path.resolve(_MAIN_PACKAGE, 'node_modules'), executionDir) }
|
|
72
70
|
|
|
73
71
|
// #SECTION PLUGIN STARTING HOOKS
|
|
@@ -12,8 +12,11 @@ function loadAliases(packageDir, tsConfig, nodeModulesPath, executionDir){
|
|
|
12
12
|
return {
|
|
13
13
|
...tsPaths,
|
|
14
14
|
'@rws-framework/foundation': path.resolve(packageDir, 'foundation', 'rws-foundation.js'),
|
|
15
|
-
'@rws-framework/foundation/*': path.resolve(packageDir, 'foundation', '*')
|
|
15
|
+
'@rws-framework/foundation/*': path.resolve(packageDir, 'foundation', '*'),
|
|
16
|
+
// 'entities/lib/maps/entities.json': path.resolve(nodeModulesPath, 'entities/lib/maps/entities.json'),
|
|
17
|
+
// 'entities/lib/maps/legacy.json': path.resolve(nodeModulesPath, 'entities/lib/maps/legacy.json'),
|
|
18
|
+
// 'entities/lib/maps/xml.json': path.resolve(nodeModulesPath, 'entities/lib/maps/xml.json')
|
|
16
19
|
}
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
module.exports = { loadAliases }
|
|
22
|
+
module.exports = { loadAliases }
|
|
@@ -18,6 +18,13 @@ function getRWSLoaders(packageDir, executionDir, tsConfigData, appRootDir, entry
|
|
|
18
18
|
const tsConfigPath = tsConfigData.path;
|
|
19
19
|
|
|
20
20
|
const loaders = [
|
|
21
|
+
{
|
|
22
|
+
test: /\.json$/,
|
|
23
|
+
type: 'javascript/auto',
|
|
24
|
+
include: [
|
|
25
|
+
path.resolve(appRootDir, 'node_modules/entities/lib/maps')
|
|
26
|
+
],
|
|
27
|
+
},
|
|
21
28
|
{
|
|
22
29
|
test: /\.html$/,
|
|
23
30
|
use: [
|
|
@@ -256,4 +263,4 @@ let rwsTemplate: any = T.html\`${templateContent}\`;
|
|
|
256
263
|
return [template, htmlFastImports, templateExists];
|
|
257
264
|
}
|
|
258
265
|
|
|
259
|
-
module.exports = { getRWSLoaders, extractRWSViewArgs, getTemplate, getStyles }
|
|
266
|
+
module.exports = { getRWSLoaders, extractRWSViewArgs, getTemplate, getStyles }
|
|
@@ -21,15 +21,13 @@ const externals = (declaredCodeBase, nodeModules, automatedChunks, externalOptio
|
|
|
21
21
|
|
|
22
22
|
const codeBase = path.resolve(declaredCodeBase);
|
|
23
23
|
|
|
24
|
-
const ignored = [
|
|
25
|
-
// /css-loader/,
|
|
24
|
+
const ignored = [
|
|
26
25
|
/tslib/,
|
|
27
26
|
/reflect-metadata/,
|
|
28
|
-
/\@microsoft\/fast-foundation
|
|
27
|
+
/\@microsoft\/fast-foundation\/.*/,
|
|
29
28
|
]
|
|
30
29
|
|
|
31
|
-
const enforced = [
|
|
32
|
-
/entities/,
|
|
30
|
+
const enforced = [
|
|
33
31
|
/\@microsoft\/fast-foundation\/.*\/di/,
|
|
34
32
|
/\@microsoft\/fast-foundation\/.*\/foundation-element/,
|
|
35
33
|
]
|
|
@@ -39,6 +37,8 @@ const externals = (declaredCodeBase, nodeModules, automatedChunks, externalOptio
|
|
|
39
37
|
path.resolve(__dirname,'..','..','..')
|
|
40
38
|
];
|
|
41
39
|
|
|
40
|
+
console.log({frontendDirs})
|
|
41
|
+
|
|
42
42
|
const inFrontendContext = frontendDirs.some(dir => context.startsWith(dir)) ||
|
|
43
43
|
externalOptions._vars.frontendRequestContextCache.some(package => context.indexOf(package.request) > -1)
|
|
44
44
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rws-framework/client",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.16.0",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"docs": "typedoc --tsconfig ./tsconfig.json"
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"@types/moment": "^2.13.0",
|
|
33
33
|
"deepmerge": "^4.3.1",
|
|
34
34
|
"dragula": "^3.7.3",
|
|
35
|
+
"entities": "6.0.0",
|
|
35
36
|
"he": "^1.2.0",
|
|
36
37
|
"json5": "^2.2.3",
|
|
37
38
|
"lodash": "^4.17.21",
|
|
@@ -39,7 +40,7 @@
|
|
|
39
40
|
"partial-json-parser": "^1.0.0",
|
|
40
41
|
"reflect-metadata": "^0.2.2",
|
|
41
42
|
"resolve-url-loader": "^5.0.0",
|
|
42
|
-
"sanitize-html": "^2.
|
|
43
|
+
"sanitize-html": "^2.14.0",
|
|
43
44
|
"socket.io-client": "^4.7.2",
|
|
44
45
|
"upload": "^1.3.2",
|
|
45
46
|
"url-router": "^13.0.0",
|
package/src/client.ts
CHANGED
|
@@ -25,14 +25,6 @@ import ConfigHelper from './client/config';
|
|
|
25
25
|
import { DefaultRWSPluginOptionsType, RWSPlugin } from './plugins/_plugin';
|
|
26
26
|
import { IStaticRWSPlugin } from './types/IRWSPlugin'
|
|
27
27
|
|
|
28
|
-
interface IHotModule extends NodeModule {
|
|
29
|
-
hot?: {
|
|
30
|
-
accept(dependencies: string[], callback?: (updatedDependencies: string[]) => void): void;
|
|
31
|
-
accept(dependency: string, callback?: () => void): void;
|
|
32
|
-
accept(errHandler?: (err: Error) => void): void;
|
|
33
|
-
dispose(callback: (data: any) => void): void;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
28
|
|
|
37
29
|
type RWSEventListener = (event: CustomEvent) => void;
|
|
38
30
|
|
|
@@ -210,4 +202,4 @@ class RWSClient {
|
|
|
210
202
|
}
|
|
211
203
|
|
|
212
204
|
export default DI.createInterface<RWSClient>(x => x.singleton(RWSClient));
|
|
213
|
-
export {
|
|
205
|
+
export { RWSClient as RWSClientInstance };
|
package/src/index.ts
CHANGED
|
@@ -30,12 +30,31 @@ import type { IAssetShowOptions } from './components/_component';
|
|
|
30
30
|
import RWSContainer from './components/_container';
|
|
31
31
|
|
|
32
32
|
import type { RWSDecoratorOptions } from './components/_decorator';
|
|
33
|
+
import type { IKDBTypeInfo, IKDBTypesResponse } from './types/IBackendCore';
|
|
33
34
|
|
|
34
35
|
import { RWSIgnore, RWSInject, RWSView } from './components/_decorator';
|
|
35
36
|
|
|
36
37
|
import { declareRWSComponents } from './components';
|
|
37
38
|
|
|
38
39
|
export default RWSClient;
|
|
40
|
+
|
|
41
|
+
export type {
|
|
42
|
+
IKDBTypeInfo, IKDBTypesResponse,
|
|
43
|
+
NotifyUiType,
|
|
44
|
+
NotifyLogType,
|
|
45
|
+
IBackendRoute as IRWSBackendRoute,
|
|
46
|
+
RWSDecoratorOptions as IRWSDecoratorOptions,
|
|
47
|
+
IHTTProute as IRWSHttpRoute,
|
|
48
|
+
IPrefixedHTTProutes as IRWSPrefixedHTTProutes,
|
|
49
|
+
IAssetShowOptions as IRWSAssetShowOptions,
|
|
50
|
+
IRWSConfig,
|
|
51
|
+
IRWSUser,
|
|
52
|
+
TagsProcessorType,
|
|
53
|
+
HTMLTagTransformerType,
|
|
54
|
+
HTMLTag,
|
|
55
|
+
HTMLAttributes
|
|
56
|
+
}
|
|
57
|
+
|
|
39
58
|
export {
|
|
40
59
|
RWSClient,
|
|
41
60
|
RWSClientInstance,
|
|
@@ -43,11 +62,7 @@ export {
|
|
|
43
62
|
RWSPlugin,
|
|
44
63
|
IPluginSpawnOption,
|
|
45
64
|
IRWSPlugin, IStaticRWSPlugin,
|
|
46
|
-
DefaultRWSPluginOptionsType,
|
|
47
|
-
|
|
48
|
-
NotifyUiType,
|
|
49
|
-
NotifyLogType,
|
|
50
|
-
|
|
65
|
+
DefaultRWSPluginOptionsType,
|
|
51
66
|
ApiServiceInstance,
|
|
52
67
|
ApiService,
|
|
53
68
|
UtilsServiceInstance,
|
|
@@ -63,18 +78,7 @@ export {
|
|
|
63
78
|
ServiceWorkerService,
|
|
64
79
|
|
|
65
80
|
RWSNotify,
|
|
66
|
-
|
|
67
|
-
RWSDecoratorOptions as IRWSDecoratorOptions,
|
|
68
|
-
IHTTProute as IRWSHttpRoute,
|
|
69
|
-
IPrefixedHTTProutes as IRWSPrefixedHTTProutes,
|
|
70
|
-
IAssetShowOptions as IRWSAssetShowOptions,
|
|
71
|
-
IRWSConfig,
|
|
72
|
-
IRWSUser,
|
|
73
|
-
TagsProcessorType,
|
|
74
|
-
HTMLTagTransformerType,
|
|
75
|
-
HTMLTag,
|
|
76
|
-
HTMLAttributes,
|
|
77
|
-
|
|
81
|
+
|
|
78
82
|
RWSView,
|
|
79
83
|
sanitizedAttr,
|
|
80
84
|
RWSIgnore,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IKDBTypesResponse } from '../types/IBackendCore';
|
|
1
2
|
import TheService from './_service';
|
|
2
3
|
|
|
3
4
|
//@4DI
|
|
@@ -5,6 +6,8 @@ import ConfigService, { ConfigServiceInstance } from './ConfigService';
|
|
|
5
6
|
|
|
6
7
|
import { upload, UploadResponse } from 'upload';
|
|
7
8
|
|
|
9
|
+
import { backend } from './_api/backend';
|
|
10
|
+
import { calls } from './_api/calls';
|
|
8
11
|
|
|
9
12
|
interface RequestOptions {
|
|
10
13
|
method?: string;
|
|
@@ -19,182 +22,49 @@ interface IAPIOptions {
|
|
|
19
22
|
},
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
interface IHTTProute {
|
|
25
|
+
interface IHTTProute<P = {[key: string]: any}> {
|
|
23
26
|
name: string;
|
|
24
|
-
path: string;
|
|
27
|
+
path: string;
|
|
28
|
+
method: string;
|
|
29
|
+
noParams?: boolean;
|
|
30
|
+
options?: any;
|
|
31
|
+
plugins?: P
|
|
25
32
|
}
|
|
26
33
|
|
|
27
|
-
|
|
28
|
-
interface IPrefixedHTTProutes {
|
|
34
|
+
interface IPrefixedHTTProutes<P = {[key: string]: any}> {
|
|
29
35
|
prefix: string;
|
|
30
|
-
|
|
36
|
+
controllerName: string;
|
|
37
|
+
exportAutoRoutes?: boolean,
|
|
38
|
+
routes: IHTTProute<P>[];
|
|
31
39
|
}
|
|
32
40
|
|
|
41
|
+
|
|
33
42
|
type IBackendRoute = IHTTProute | IPrefixedHTTProutes;
|
|
34
43
|
|
|
35
44
|
|
|
36
|
-
const _DEFAULT_CONTENT_TYPE = 'application/json';
|
|
37
45
|
|
|
38
46
|
class ApiService extends TheService {
|
|
39
47
|
static _DEFAULT: boolean = true;
|
|
40
48
|
private token?: string;
|
|
41
49
|
|
|
42
|
-
constructor(@ConfigService
|
|
50
|
+
constructor(@ConfigService public config: ConfigServiceInstance) {
|
|
43
51
|
super();
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
private addHeader(headers: Headers | [string, string][] | {[key: string]: string}, key: string, val: string)
|
|
47
|
-
{
|
|
48
|
-
if (headers instanceof Headers) {
|
|
49
|
-
headers.append(key, val);
|
|
50
|
-
} else if (Array.isArray(headers)) {
|
|
51
|
-
headers.push([key, val]);
|
|
52
|
-
} else {
|
|
53
|
-
headers[key] = val;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Function to get headers
|
|
58
|
-
private getHeaders(optHeaders: HeadersInit = {}): HeadersInit {
|
|
59
|
-
const headers: HeadersInit = { ...optHeaders };
|
|
60
|
-
|
|
61
|
-
if (!('Content-Type' in headers)) {
|
|
62
|
-
this.addHeader(headers, 'Content-Type', _DEFAULT_CONTENT_TYPE);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (this.token) {
|
|
66
|
-
this.addHeader(headers, 'Authorization', `Bearer ${this.token}`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if((headers as any)['Content-Type']){
|
|
70
|
-
this.addHeader(headers, 'Accept', '*/*');
|
|
71
|
-
}else{
|
|
72
|
-
this.addHeader(headers, 'Accept', (headers as any)['Content-Type']);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return headers;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
54
|
public setToken(token: string)
|
|
79
55
|
{
|
|
80
56
|
this.token = token;
|
|
81
57
|
}
|
|
82
58
|
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
const response = await fetch(url, {
|
|
86
|
-
headers: this.getHeaders(options.headers),
|
|
87
|
-
});
|
|
88
|
-
return await response.text();
|
|
89
|
-
} catch (error) {
|
|
90
|
-
console.error('GET request failed:', error);
|
|
91
|
-
throw error;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
59
|
+
|
|
94
60
|
|
|
95
61
|
public async isGetTargetReachable(url: string, options: IAPIOptions = {}): Promise<boolean> {
|
|
96
62
|
try {
|
|
97
|
-
return !!(await
|
|
63
|
+
return !!(await calls.pureGet(url, options, this.token));
|
|
98
64
|
} catch (error) {
|
|
99
65
|
return false;
|
|
100
66
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
public async get<T>(url: string, options: IAPIOptions = {}): Promise<T> {
|
|
104
|
-
try {
|
|
105
|
-
const response = await fetch(url, {
|
|
106
|
-
headers: this.getHeaders(options.headers),
|
|
107
|
-
});
|
|
108
|
-
return await response.json();
|
|
109
|
-
} catch (error) {
|
|
110
|
-
console.error('GET request failed:', error);
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public async post<T, P extends object = object>(url: string, payload?: P, options: IAPIOptions = {}): Promise<T> {
|
|
116
|
-
try {
|
|
117
|
-
const response = await fetch(url, {
|
|
118
|
-
method: 'POST',
|
|
119
|
-
headers: this.getHeaders(options.headers),
|
|
120
|
-
body: payload ? JSON.stringify(payload) : null,
|
|
121
|
-
});
|
|
122
|
-
return await response.json();
|
|
123
|
-
} catch (error) {
|
|
124
|
-
console.error('POST request failed:', error);
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
public async put<T, P extends object = object>(url: string, payload?: P, options: IAPIOptions = {}): Promise<T> {
|
|
130
|
-
try {
|
|
131
|
-
const response = await fetch(url, {
|
|
132
|
-
method: 'PUT',
|
|
133
|
-
headers: this.getHeaders(options.headers),
|
|
134
|
-
body: payload ? JSON.stringify(payload) : null,
|
|
135
|
-
});
|
|
136
|
-
return await response.json();
|
|
137
|
-
} catch (error) {
|
|
138
|
-
console.error('PUT request failed:', error);
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public async delete<T>(url: string, options: IAPIOptions = {}): Promise<T> {
|
|
144
|
-
try {
|
|
145
|
-
const response = await fetch(url, {
|
|
146
|
-
method: 'DELETE',
|
|
147
|
-
headers: this.getHeaders(options.headers),
|
|
148
|
-
});
|
|
149
|
-
return await response.json();
|
|
150
|
-
} catch (error) {
|
|
151
|
-
console.error('DELETE request failed:', error);
|
|
152
|
-
throw error;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private getBackendUrl(routeName: string, params: {[key: string]: string} = {})
|
|
157
|
-
{
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const routesPackage = this.config.get('backendRoutes');
|
|
161
|
-
|
|
162
|
-
let routes: IHTTProute[] = [];
|
|
163
|
-
|
|
164
|
-
routesPackage.forEach((item: IBackendRoute) => {
|
|
165
|
-
// Check if item is an instance of IPrefixedHTTProutes
|
|
166
|
-
if ('prefix' in item && 'routes' in item && Array.isArray(item.routes)) {
|
|
167
|
-
// Handle the case where item is of type IPrefixedHTTProutes
|
|
168
|
-
routes = [...routes, ...item.routes.map((subRouteItem: IHTTProute): IHTTProute => {
|
|
169
|
-
const subRoute: IHTTProute = {
|
|
170
|
-
path: item.prefix + subRouteItem.path,
|
|
171
|
-
name: subRouteItem.name
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
return subRoute;
|
|
175
|
-
})];
|
|
176
|
-
} else {
|
|
177
|
-
// Handle the case where item is of type IHTTProute
|
|
178
|
-
routes.push(item as IHTTProute);
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
const route = routes.find((item: IHTTProute) => item.name === routeName);
|
|
183
|
-
|
|
184
|
-
if(!route){
|
|
185
|
-
throw new Error(`Backend route '${routeName}' does not exist.`);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
let apiPath = route.path;
|
|
189
|
-
|
|
190
|
-
Object.keys(params).forEach((paramKey: string) => {
|
|
191
|
-
const paramValue = params[paramKey];
|
|
192
|
-
|
|
193
|
-
apiPath = apiPath.replace(`:${paramKey}`, paramValue);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
return `${this.config.get('backendUrl')}${this.config.get('apiPrefix') || ''}${apiPath}`;
|
|
197
|
-
}
|
|
67
|
+
}
|
|
198
68
|
|
|
199
69
|
async uploadFile(url: string, file: File, onProgress: (progress: number) => void, payload: any = {}): Promise<UploadResponse>
|
|
200
70
|
{
|
|
@@ -212,32 +82,25 @@ class ApiService extends TheService {
|
|
|
212
82
|
);
|
|
213
83
|
}
|
|
214
84
|
|
|
85
|
+
public pureGet = calls.pureGet;
|
|
86
|
+
public get = calls.get;
|
|
87
|
+
public post = calls.post;
|
|
88
|
+
public put = calls.put;
|
|
89
|
+
public delete = calls.delete;
|
|
90
|
+
|
|
215
91
|
public back = {
|
|
216
|
-
get: <T>(routeName: string, options?: IAPIOptions): Promise<T> =>
|
|
217
|
-
post: <T, P extends object = object>(routeName: string, payload?: P, options?: IAPIOptions): Promise<T> =>
|
|
218
|
-
put: <T, P extends object = object>(routeName: string, payload: P, options?: IAPIOptions): Promise<T> =>
|
|
219
|
-
delete: <T>(routeName: string, options?: IAPIOptions): Promise<T> =>
|
|
220
|
-
uploadFile: (routeName: string, file: File, onProgress: (progress: number) => void, options: IAPIOptions = {}, payload: any = {}): Promise<UploadResponse> => this.uploadFile(
|
|
92
|
+
get: <T>(routeName: string, options?: IAPIOptions): Promise<T> => calls.get(backend.getBackendUrl.bind(this)(routeName, options?.routeParams), options, this.token),
|
|
93
|
+
post: <T, P extends object = object>(routeName: string, payload?: P, options?: IAPIOptions): Promise<T> => calls.post(backend.getBackendUrl.bind(this)(routeName, options?.routeParams), payload, options, this.token),
|
|
94
|
+
put: <T, P extends object = object>(routeName: string, payload: P, options?: IAPIOptions): Promise<T> => calls.put(backend.getBackendUrl.bind(this)(routeName, options?.routeParams), payload, options, this.token),
|
|
95
|
+
delete: <T>(routeName: string, options?: IAPIOptions): Promise<T> => calls.delete(backend.getBackendUrl.bind(this)(routeName, options?.routeParams), options, this.token),
|
|
96
|
+
uploadFile: (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),
|
|
221
97
|
};
|
|
222
98
|
|
|
223
|
-
|
|
99
|
+
async getResource(resourceName: string): Promise<IKDBTypesResponse>
|
|
224
100
|
{
|
|
225
|
-
|
|
226
|
-
// const client = generateClient<Schema>() // use this Data client for CRUDL requests
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
// /*== STEP 3 ===============================================================
|
|
230
|
-
// Fetch records from the database and use them in your frontend component.
|
|
231
|
-
// (THIS SNIPPET WILL ONLY WORK IN THE FRONTEND CODE FILE.)
|
|
232
|
-
// =========================================================================*/
|
|
233
|
-
|
|
234
|
-
// /* For example, in a React component, you can use this snippet in your
|
|
235
|
-
// function's RETURN statement */
|
|
236
|
-
// // const { data: todos } = client.models.Todo.list()
|
|
237
|
-
|
|
238
|
-
// // return <ul>{todos.map(todo => <li key={todo.id}>{todo.content}</li>)}</ul>
|
|
101
|
+
return calls.get(`${this.config.get('backendUrl')}${this.config.get('apiPrefix') || ''}/api/rws/resource/${resourceName}`)
|
|
239
102
|
}
|
|
240
103
|
}
|
|
241
104
|
|
|
242
105
|
export default ApiService.getSingleton();
|
|
243
|
-
export { IBackendRoute, RequestOptions, ApiService as ApiServiceInstance, IHTTProute, IPrefixedHTTProutes };
|
|
106
|
+
export { IBackendRoute, RequestOptions, ApiService as ApiServiceInstance, IHTTProute, IPrefixedHTTProutes, IAPIOptions };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ApiServiceInstance, IBackendRoute, IHTTProute } from "../ApiService";
|
|
2
|
+
import { ConfigServiceInstance } from "../ConfigService";
|
|
3
|
+
|
|
4
|
+
export const backend = {
|
|
5
|
+
getBackendUrl(this: ApiServiceInstance, routeName: string, params: {[key: string]: string} = {})
|
|
6
|
+
{
|
|
7
|
+
const config = this.config;
|
|
8
|
+
const routesPackage = config.get('backendRoutes');
|
|
9
|
+
|
|
10
|
+
let routes: IHTTProute[] = [];
|
|
11
|
+
|
|
12
|
+
routesPackage.forEach((item: IBackendRoute) => {
|
|
13
|
+
// Check if item is an instance of IPrefixedHTTProutes
|
|
14
|
+
if ('prefix' in item && 'routes' in item && Array.isArray(item.routes)) {
|
|
15
|
+
// Handle the case where item is of type IPrefixedHTTProutes
|
|
16
|
+
if(item.exportAutoRoutes){
|
|
17
|
+
item.routes = [...item.routes,
|
|
18
|
+
{
|
|
19
|
+
name: `list`,
|
|
20
|
+
path: '/',
|
|
21
|
+
method: 'GET'
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: `create`,
|
|
25
|
+
path: '/',
|
|
26
|
+
method: 'POST'
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: `show`,
|
|
30
|
+
path: '/:id',
|
|
31
|
+
method: 'GET'
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: `update`,
|
|
35
|
+
path: '/:id',
|
|
36
|
+
method: 'PUT'
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: `delete`,
|
|
40
|
+
path: '/:id',
|
|
41
|
+
method: 'DELETE'
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
routes = [...routes, ...item.routes.map((subRouteItem: IHTTProute): IHTTProute => {
|
|
47
|
+
const subRoute: IHTTProute = {
|
|
48
|
+
path: item.prefix + subRouteItem.path,
|
|
49
|
+
name: backend.checkPrefixedRouteName(subRouteItem.name, item.controllerName),
|
|
50
|
+
method: subRouteItem.method || 'GET'
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return subRoute;
|
|
54
|
+
})];
|
|
55
|
+
|
|
56
|
+
console.log({routes});
|
|
57
|
+
} else {
|
|
58
|
+
// Handle the case where item is of type IHTTProute
|
|
59
|
+
routes.push(item as IHTTProute);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const route = routes.find((item: IHTTProute) => item.name === routeName);
|
|
64
|
+
|
|
65
|
+
if(!route){
|
|
66
|
+
throw new Error(`Backend route '${routeName}' does not exist.`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let apiPath = route.path;
|
|
70
|
+
|
|
71
|
+
Object.keys(params).forEach((paramKey: string) => {
|
|
72
|
+
const paramValue = params[paramKey];
|
|
73
|
+
|
|
74
|
+
apiPath = apiPath.replace(`:${paramKey}`, paramValue);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return `${config.get('backendUrl')}${config.get('apiPrefix') || ''}${apiPath}`;
|
|
78
|
+
},
|
|
79
|
+
checkPrefixedRouteName(routeName: string, prefixName: string){
|
|
80
|
+
let finalRoute = routeName;
|
|
81
|
+
|
|
82
|
+
if(routeName.indexOf(prefixName) === -1){
|
|
83
|
+
finalRoute = `${prefixName}:${routeName}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return finalRoute;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { IAPIOptions } from "../ApiService";
|
|
2
|
+
|
|
3
|
+
const _DEFAULT_CONTENT_TYPE = 'application/json';
|
|
4
|
+
|
|
5
|
+
export const calls = {
|
|
6
|
+
addHeader(headers: Headers | [string, string][] | {[key: string]: string}, key: string, val: string)
|
|
7
|
+
{
|
|
8
|
+
if (headers instanceof Headers) {
|
|
9
|
+
headers.append(key, val);
|
|
10
|
+
} else if (Array.isArray(headers)) {
|
|
11
|
+
headers.push([key, val]);
|
|
12
|
+
} else {
|
|
13
|
+
headers[key] = val;
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
getHeaders(token: string = null, optHeaders: HeadersInit = {}): HeadersInit {
|
|
17
|
+
const headers: HeadersInit = { ...optHeaders };
|
|
18
|
+
|
|
19
|
+
if (!('Content-Type' in headers)) {
|
|
20
|
+
this.addHeader(headers, 'Content-Type', _DEFAULT_CONTENT_TYPE);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (token) {
|
|
24
|
+
this.addHeader(headers, 'Authorization', `Bearer ${token}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if((headers as any)['Content-Type']){
|
|
28
|
+
this.addHeader(headers, 'Accept', '*/*');
|
|
29
|
+
}else{
|
|
30
|
+
this.addHeader(headers, 'Accept', (headers as any)['Content-Type']);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return headers;
|
|
34
|
+
},
|
|
35
|
+
async pureGet(url: string, options: IAPIOptions = {}, token: string = null): Promise<string> {
|
|
36
|
+
try {
|
|
37
|
+
const response = await fetch(url, {
|
|
38
|
+
headers: this.getHeaders(token, options.headers),
|
|
39
|
+
});
|
|
40
|
+
return await response.text();
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error('GET request failed:', error);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
async get<T>(url: string, options: IAPIOptions = {}, token: string = null): Promise<T> {
|
|
47
|
+
try {
|
|
48
|
+
const response = await fetch(url, {
|
|
49
|
+
headers: this.getHeaders(token, options.headers),
|
|
50
|
+
});
|
|
51
|
+
return await response.json();
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('GET request failed:', error);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
async post<T, P extends object = object>(url: string, payload?: P, options: IAPIOptions = {}, token: string = null): Promise<T> {
|
|
58
|
+
try {
|
|
59
|
+
const response = await fetch(url, {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers: this.getHeaders(token, options.headers),
|
|
62
|
+
body: payload ? JSON.stringify(payload) : null,
|
|
63
|
+
});
|
|
64
|
+
return await response.json();
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error('POST request failed:', error);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
async put<T, P extends object = object>(url: string, payload?: P, options: IAPIOptions = {}, token: string = null): Promise<T> {
|
|
71
|
+
try {
|
|
72
|
+
const response = await fetch(url, {
|
|
73
|
+
method: 'PUT',
|
|
74
|
+
headers: this.getHeaders(token, options.headers),
|
|
75
|
+
body: payload ? JSON.stringify(payload) : null,
|
|
76
|
+
});
|
|
77
|
+
return await response.json();
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error('PUT request failed:', error);
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
async delete<T>(url: string, options: IAPIOptions = {}, token: string = null): Promise<T> {
|
|
84
|
+
try {
|
|
85
|
+
const response = await fetch(url, {
|
|
86
|
+
method: 'DELETE',
|
|
87
|
+
headers: this.getHeaders(token, options.headers),
|
|
88
|
+
});
|
|
89
|
+
return await response.json();
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.error('DELETE request failed:', error);
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|