UNPKG

3.78 kBPlain TextView Raw
1import { extractObject, INFRASTRUCTURE_MODES, loadConfigurationFromModule } from '../libs/loader';
2import Types from '../types';
3import {getBasename} from '../libs/iso-libs';
4import ExecutionEnvironment from 'exenv';
5
6/**
7 * Convenience function that
8 * @param id
9 * @param args
10 * @param onResult
11 * @param onError
12 */
13export 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 //console.log("callService: ", id, args);
23
24
25 const path = require("path");
26
27 // load the IsomorphicComponent
28 // we must load it directly from the module here, to enable the aliad of the config_file_path
29 const isoConfig = config == undefined ?
30 loadConfigurationFromModule(require('__CONFIG_FILE_PATH__'), INFRASTRUCTURE_MODES.RUNTIME) : config;
31
32 // let's extract it from the root configuration
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 /*console.log("basename: ", basename);
48
49 console.log("isOffline: ", isOffline);
50 console.log("Domain Url: ", process.env.DOMAIN_URL);*/
51
52 // only if were at the server, (ISO-only!) we need to add the server name
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 //console.log("urlPath: ", urlPath);
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 // apparently, the fetch does not require the hostname...why?
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 //console.log("post result: ", result);
85 onResult(result);
86
87 }).catch(error => {
88 //console.error("post-error: ", error);
89 onError(error);
90 });
91 //console.log("callService done")
92}
93
94export function getServiceUrl(id: string, args: any) {
95
96 const path = require("path");
97
98 // load the IsomorphicComponent
99 // we must load it directly from the module here, to enable the aliad of the config_file_path
100 const isoConfig = loadConfigurationFromModule(require('__CONFIG_FILE_PATH__'), INFRASTRUCTURE_MODES.RUNTIME);
101
102 //console.log("isoConfig: ", isoConfig)
103
104 // let's extract it from the root configuration
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