codenotch-react 1.0.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.
Files changed (3) hide show
  1. package/index.d.ts +33 -0
  2. package/index.js +106 -0
  3. package/package.json +8 -0
package/index.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ export interface ICodenotch {
2
+ ENV: ICodenotchAppProps,
3
+
4
+ requestSioql: (sioql: string) => Promise<any>;
5
+
6
+ startProcess: (processName: string) => Promise<IProcessResult>;
7
+
8
+ onSignal: (signalId: string, callback: (data: any) => void) => IDisposable;
9
+
10
+ }
11
+
12
+ interface IDisposable {
13
+ dispose(): void;
14
+ }
15
+
16
+ interface IProcessResult {
17
+ success: boolean;
18
+ }
19
+
20
+ export function useCodenotch(): ICodenotch;
21
+
22
+ export function useCodenotchApp(props: ICodenotchAppProps): ICodenotch;
23
+
24
+ export interface ICodenotchAppProps {
25
+ clusterUrl?: string;
26
+ serviceName?: string;
27
+ tenantName?: string;
28
+ userId?: string;
29
+ accessToken?: string;
30
+ baseUrl?: string;
31
+ }
32
+
33
+ export type CodenotchApp<T> = React.FC<(ICodenotchAppProps & T)>;
package/index.js ADDED
@@ -0,0 +1,106 @@
1
+ const CODENOTCH_ENV = {
2
+
3
+ };
4
+
5
+ function useCodenotch() {
6
+ console.log("useCodenotch has been called");
7
+ return {
8
+ ENV: CODENOTCH_ENV,
9
+ requestSioql: async (sioql, verbose) => {
10
+ if (CODENOTCH_ENV.clusterUrl === undefined) {
11
+ throw new Error("Codenotch cluster URL is not defined. Please set it in the Codenotch configuration.");
12
+ }
13
+ if (CODENOTCH_ENV.serviceName === undefined) {
14
+ throw new Error("Codenotch service name is not defined. Please set it in the Codenotch configuration.");
15
+ }
16
+
17
+ let url = `${CODENOTCH_ENV.clusterUrl}/${CODENOTCH_ENV.serviceName}/sioql`;
18
+
19
+ if (verbose) {
20
+ url += "?v=true";
21
+ }
22
+
23
+ let request = {
24
+ method: 'POST',
25
+ mode: 'cors',
26
+ credentials: 'same-origin'
27
+ };
28
+
29
+ request.headers = {};
30
+
31
+ if (CODENOTCH_ENV.accessToken && CODENOTCH_ENV.accessToken !== "") {
32
+ if (CODENOTCH_ENV.tenantName === undefined) {
33
+ throw new Error("Codenotch tenant name is not defined. Please set it in the Codenotch configuration.");
34
+ }
35
+
36
+ request.headers[`${CODENOTCH_ENV.tenantName}AccessToken`] = CODENOTCH_ENV.accessToken;
37
+ }
38
+
39
+ request.headers['Content-Type'] = 'application/json';
40
+
41
+ request.body = `"${sioql.replace(/\"/g, '\\\"')}"`;
42
+
43
+ let response = await fetch(url, request);
44
+
45
+ if (!response.ok) {
46
+ let error = await response.text();
47
+ throw new Error(error);
48
+ }
49
+
50
+ return await response.json();
51
+ },
52
+
53
+ startProcess: async (processName) => {
54
+
55
+ return { success: true };
56
+ },
57
+
58
+ onSignal: (signalId, callback) => {
59
+ console.log(`Listening to signal ${signalId}`);
60
+ return {
61
+ dispose: () => {
62
+ console.log(`Stopped listening to signal ${signalId}`);
63
+ }
64
+ }
65
+ }
66
+ };
67
+ }
68
+
69
+
70
+ function useCodenotchApp(props) {
71
+ if (props) {
72
+ if (props.clusterUrl) {
73
+ CODENOTCH_ENV.clusterUrl = props.clusterUrl;
74
+ }
75
+
76
+ if (props.serviceName) {
77
+ CODENOTCH_ENV.serviceName = props.serviceName;
78
+ }
79
+
80
+ if (props.tenantName) {
81
+ CODENOTCH_ENV.tenantName = props.tenantName;
82
+ }
83
+
84
+ if (props.userId) {
85
+ CODENOTCH_ENV.userId = props.userId;
86
+ }
87
+
88
+ if (props.accessToken) {
89
+ CODENOTCH_ENV.accessToken = props.accessToken;
90
+ }
91
+ }
92
+ else {
93
+ console.warn("useCodenotchApp: No Codenotch configuration provided.");
94
+ }
95
+
96
+ const params = new URL(window.location.href).searchParams;
97
+ params.forEach((value, key) => {
98
+ if(props[key] === undefined){
99
+ props[key] = value;
100
+ }
101
+ });
102
+
103
+ return useCodenotch();
104
+ }
105
+
106
+ module.exports = { useCodenotch, useCodenotchApp };
package/package.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "codenotch-react",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "types": "index.d.ts",
6
+ "type": "module",
7
+ "author": "Codenotch SA"
8
+ }