-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
69 lines (56 loc) · 1.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { getBuiltMesh, getMeshSDK, Sdk } from './.mesh';
import https from 'https';
type Awaited<T> = T extends PromiseLike<infer U> ? U : T;
const init = async (
hashavshevetPersonalToken: string,
hashavshevetCompanyKey: string,
hashavshevetUrl: string,
): Promise<{ sdk: Sdk; execute: Awaited<ReturnType<typeof getBuiltMesh>>['execute'] }> => {
console.log('initiating sdk...');
process.env.HASHAVSHEVET_MESH_URL = hashavshevetUrl;
process.env.HASHAVSHEVET_MESH_AUTH_TOKEN = (await login(
hashavshevetPersonalToken,
hashavshevetCompanyKey,
hashavshevetUrl,
)) as string;
console.log('logged in to Hashavshevet');
const { execute } = await getBuiltMesh();
const sdk = await getMeshSDK();
return { sdk, execute };
};
const login = (hashavshevetKey: string, company: string, hashavshevetUrl: string) => {
const p = new Promise((resolve, reject) => {
if (!hashavshevetKey || !company) {
reject(new Error('Missing Hashavshevet user key or company ID'));
return;
}
const path = `/createSession/${hashavshevetKey}/${company}`;
const req = https.request(
{
host: hashavshevetUrl,
path,
method: 'GET',
},
(res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (data === 'iligal token') {
reject(new Error('Illegal token'));
}
resolve(data);
});
},
);
req.on('error', (error) => {
console.error(error);
reject(error);
});
req.end();
});
return p;
};
export * from './.mesh';
export { init };