This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
71 lines (65 loc) · 1.68 KB
/
api.js
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
70
71
import os from "node:os";
import path from "node:path";
import fs from "node:fs";
const INNGEST_CONFIG_PATH = path.join(os.homedir(), ".config/inngest/state");
export const getCredentials = () => {
const file = fs.readFileSync(INNGEST_CONFIG_PATH, "utf8");
const config = JSON.parse(file);
const buff = Buffer.from(config.credentials, "base64");
return buff.toString("ascii");
};
const request = async (query = "", variables = {}) => {
const credentials = getCredentials();
const res = await fetch("https://api.inngest.com/gql", {
method: "POST",
headers: {
Authorization: `Bearer ${credentials}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
variables,
}),
}).then((res) => res.json());
if (res.errors?.length && res.errors[0].message.match(/unauthorized/)) {
console.log(res);
throw new Error("Please login with `inngest login` to re-authorize");
}
return res.data;
};
export const getProductionWorkspaceID = async () => {
const res = await request(
`query GetWorkspaces() {
workspaces {
id
name
test
}
}
`
);
return res.workspaces.find((w) => w.name === "default" && w.test === false)
.id;
};
export const getEventCollections = async (workspaceID) => {
const res = await request(
`query GetEventCollections($workspaceID: ID!) {
events(query: {
workspaceID: $workspaceID
schemaSource: "custom"
}) {
data {
name
versions {
name
version
jsonSchema
}
}
}
}
`,
{ workspaceID }
);
return res.events.data.map((e) => e.versions[0]);
};