1 | import { extractObject, INFRASTRUCTURE_MODES, loadConfigurationFromModule } from '../libs/loader';
|
2 | import Types from '../types';
|
3 | import {getBasename} from '../libs/iso-libs';
|
4 | import ExecutionEnvironment from 'exenv';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export async function callService(
|
14 | id: string,
|
15 | args: any,
|
16 | onResult: (result: any) => void,
|
17 | onError: (error: any) => void,
|
18 | config: any,
|
19 | isOffline: Boolean = false
|
20 | ) {
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const path = require("path");
|
26 |
|
27 |
|
28 |
|
29 | const isoConfig = config == undefined ?
|
30 | loadConfigurationFromModule(require('__CONFIG_FILE_PATH__'), INFRASTRUCTURE_MODES.RUNTIME) : config;
|
31 |
|
32 |
|
33 | const serviceComponent = extractObject(
|
34 | isoConfig,
|
35 | Types.INFRASTRUCTURE_TYPE_COMPONENT,
|
36 | id
|
37 | );
|
38 |
|
39 | if (!serviceComponent) {
|
40 | console.error("could not find service with id: ", id);
|
41 | onError(`could not find service with id: ${id}`);
|
42 | return;
|
43 | }
|
44 |
|
45 | const basename = getBasename();
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | const urlPath = !ExecutionEnvironment.canUseDOM ? (
|
54 | isOffline ? "http://localhost:3000"+ serviceComponent.path : process.env.DOMAIN_URL.toString()+serviceComponent.path
|
55 | ) : (basename !== undefined ? (
|
56 | basename.startsWith("http") ?
|
57 | basename + serviceComponent.path :
|
58 | path.join(basename, serviceComponent.path)
|
59 | ) : serviceComponent.path);
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | const params = {
|
65 | method: serviceComponent.method,
|
66 |
|
67 | headers: {
|
68 | "Content-Type": "application/x-www-form-urlencoded",
|
69 | "Accept": "application/json",
|
70 | "Accept-Charset": "utf-8"
|
71 | }
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | await fetch(
|
78 | serviceComponent.method === "POST" ? urlPath : urlPath.concat(
|
79 | Object.keys(args).reduce((result, key, index) => result.concat(index > 0 ? "&" : "?", key, "=", args[key]), "")
|
80 | ),
|
81 | serviceComponent.method === "POST" ? Object.assign({
|
82 | body: JSON.stringify(args)
|
83 | }, params) : params).then(result => {
|
84 |
|
85 | onResult(result);
|
86 |
|
87 | }).catch(error => {
|
88 |
|
89 | onError(error);
|
90 | });
|
91 |
|
92 | }
|
93 |
|
94 | export function getServiceUrl(id: string, args: any) {
|
95 |
|
96 | const path = require("path");
|
97 |
|
98 |
|
99 |
|
100 | const isoConfig = loadConfigurationFromModule(require('__CONFIG_FILE_PATH__'), INFRASTRUCTURE_MODES.RUNTIME);
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const serviceComponent = extractObject(
|
106 | isoConfig,
|
107 | Types.INFRASTRUCTURE_TYPE_COMPONENT,
|
108 | id
|
109 | );
|
110 |
|
111 | if (!serviceComponent) {
|
112 | console.error("could not find service with id: ", id);
|
113 | return undefined;
|
114 | }
|
115 |
|
116 |
|
117 | const servicePath = Object.keys(args).reduce((res, key) => {
|
118 | return res.replace(new RegExp(`:${key}`), args[key])
|
119 | }, serviceComponent.path);
|
120 |
|
121 |
|
122 | return getBasename() !== undefined ? path.join(getBasename(), servicePath) : servicePath;
|
123 |
|
124 | }
|
125 |
|