teamx-projects 1.0.0 → 1.1.2
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.
- package/LICENSE +21 -0
- package/index.js +2 -196
- package/package.json +19 -7
- package/src/http.js +48 -0
- package/src/projectsClient.js +92 -0
- package/src/zip.js +40 -0
- package/README.md +0 -30
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 TeamX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
CHANGED
|
@@ -1,197 +1,3 @@
|
|
|
1
|
+
const { createProjectsClient } = require("./src/projectsClient");
|
|
1
2
|
|
|
2
|
-
|
|
3
|
-
* TeamX Projects
|
|
4
|
-
* Node 18+ (built-in fetch)
|
|
5
|
-
*
|
|
6
|
-
* Commands:
|
|
7
|
-
* list
|
|
8
|
-
* status <projectId>
|
|
9
|
-
* artifacts <projectId>
|
|
10
|
-
* deploy <projectId> --env production|staging|dev --by email
|
|
11
|
-
* create --name <projectName> --path <folderPath> --lang ts|js
|
|
12
|
-
*
|
|
13
|
-
* Optional env:
|
|
14
|
-
* BASE_URL=https://teamx-discord-services.fly.dev
|
|
15
|
-
* TEAMX_API_KEY=... (Bearer token, optional)
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
const fs = require("node:fs");
|
|
19
|
-
const path = require("node:path");
|
|
20
|
-
const yazl = require("yazl");
|
|
21
|
-
|
|
22
|
-
const BASEFIX = "https://teamx-discord-services.fly.dev"
|
|
23
|
-
const BASE_URL = BASEFIX.replace(/\/$/, "");
|
|
24
|
-
const API_KEY = process.env.TEAMX_API_KEY || "";
|
|
25
|
-
|
|
26
|
-
function usage(exitCode = 1) {
|
|
27
|
-
console.log(`
|
|
28
|
-
TeamX Projects API Client (CJS)
|
|
29
|
-
|
|
30
|
-
Commands:
|
|
31
|
-
list
|
|
32
|
-
status <projectId>
|
|
33
|
-
artifacts <projectId>
|
|
34
|
-
deploy <projectId> [--env production|staging|dev] [--by email]
|
|
35
|
-
create --name <projectName> --path <folderPath> [--lang ts|js]
|
|
36
|
-
|
|
37
|
-
`);
|
|
38
|
-
process.exit(exitCode);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function argValue(flag, def = "") {
|
|
42
|
-
const i = process.argv.indexOf(flag);
|
|
43
|
-
if (i === -1) return def;
|
|
44
|
-
return process.argv[i + 1] ?? def;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function fetchJson(p, opts = {}) {
|
|
48
|
-
const url = `${BASE_URL}${p}`;
|
|
49
|
-
|
|
50
|
-
const headers = { "content-type": "application/json", ...(opts.headers || {}) };
|
|
51
|
-
if (API_KEY) headers.Authorization = `Bearer ${API_KEY}`;
|
|
52
|
-
|
|
53
|
-
const res = await fetch(url, { ...opts, headers });
|
|
54
|
-
|
|
55
|
-
const text = await res.text();
|
|
56
|
-
let data;
|
|
57
|
-
try {
|
|
58
|
-
data = text ? JSON.parse(text) : null;
|
|
59
|
-
} catch {
|
|
60
|
-
data = { raw: text };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (!res.ok) {
|
|
64
|
-
const msg = (data && data.error) || res.statusText || "Request failed";
|
|
65
|
-
const err = new Error(`${res.status} ${msg}`);
|
|
66
|
-
err.status = res.status;
|
|
67
|
-
err.data = data;
|
|
68
|
-
throw err;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return data;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function print(obj) {
|
|
75
|
-
console.log(JSON.stringify(obj, null, 2));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Zip a directory into an in-memory Buffer (no temp files).
|
|
80
|
-
*/
|
|
81
|
-
function zipDirToBuffer(dir) {
|
|
82
|
-
return new Promise((resolve, reject) => {
|
|
83
|
-
const zipfile = new yazl.ZipFile();
|
|
84
|
-
const chunks = [];
|
|
85
|
-
const out = zipfile.outputStream;
|
|
86
|
-
|
|
87
|
-
out.on("data", (c) => chunks.push(c));
|
|
88
|
-
out.on("error", reject);
|
|
89
|
-
out.on("end", () => resolve(Buffer.concat(chunks)));
|
|
90
|
-
|
|
91
|
-
function walk(folder, prefix = "") {
|
|
92
|
-
const entries = fs.readdirSync(folder, { withFileTypes: true });
|
|
93
|
-
for (const e of entries) {
|
|
94
|
-
const full = path.join(folder, e.name);
|
|
95
|
-
const rel = path.join(prefix, e.name);
|
|
96
|
-
|
|
97
|
-
// Skip common junk
|
|
98
|
-
if (e.name === "node_modules" || e.name === ".git") continue;
|
|
99
|
-
|
|
100
|
-
if (e.isDirectory()) walk(full, rel);
|
|
101
|
-
else zipfile.addFile(full, rel);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
walk(dir);
|
|
106
|
-
zipfile.end();
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function main() {
|
|
111
|
-
const [, , cmd, ...rest] = process.argv;
|
|
112
|
-
if (!cmd) usage(1);
|
|
113
|
-
|
|
114
|
-
if (cmd === "list") {
|
|
115
|
-
const projects = await fetchJson("/api/projects");
|
|
116
|
-
print(projects);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (cmd === "status") {
|
|
121
|
-
const id = rest[0];
|
|
122
|
-
if (!id) usage(1);
|
|
123
|
-
const status = await fetchJson(`/api/projects/${encodeURIComponent(id)}/status`);
|
|
124
|
-
print(status);
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (cmd === "artifacts") {
|
|
129
|
-
const id = rest[0];
|
|
130
|
-
if (!id) usage(1);
|
|
131
|
-
const artifacts = await fetchJson(`/api/projects/${encodeURIComponent(id)}/artifacts`);
|
|
132
|
-
print(artifacts);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (cmd === "deploy") {
|
|
137
|
-
const id = rest[0];
|
|
138
|
-
if (!id) usage(1);
|
|
139
|
-
|
|
140
|
-
const environment = argValue("--env", "production");
|
|
141
|
-
const initiated_by = argValue("--by", "");
|
|
142
|
-
|
|
143
|
-
const payload = { environment, initiated_by };
|
|
144
|
-
const deploy = await fetchJson(`/api/projects/${encodeURIComponent(id)}/deploy`, {
|
|
145
|
-
method: "POST",
|
|
146
|
-
body: JSON.stringify(payload),
|
|
147
|
-
});
|
|
148
|
-
print(deploy);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (cmd === "create") {
|
|
153
|
-
const name = argValue("--name");
|
|
154
|
-
const folderPath = argValue("--path");
|
|
155
|
-
const language = argValue("--lang", "ts");
|
|
156
|
-
|
|
157
|
-
if (!name || !folderPath) usage(1);
|
|
158
|
-
|
|
159
|
-
const abs = path.resolve(folderPath);
|
|
160
|
-
if (!fs.existsSync(abs) || !fs.statSync(abs).isDirectory()) {
|
|
161
|
-
throw new Error(`Invalid --path folder: ${abs}`);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const zipBuf = await zipDirToBuffer(abs);
|
|
165
|
-
const source = zipBuf.toString("base64");
|
|
166
|
-
|
|
167
|
-
// If your backend expects different keys, change them here.
|
|
168
|
-
const payload = {
|
|
169
|
-
name,
|
|
170
|
-
template: "advanced-discord-bot",
|
|
171
|
-
language,
|
|
172
|
-
source, // base64 zip
|
|
173
|
-
meta: {
|
|
174
|
-
createdAt: new Date().toISOString(),
|
|
175
|
-
},
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
const created = await fetchJson("/api/projects", {
|
|
179
|
-
method: "POST",
|
|
180
|
-
body: JSON.stringify(payload),
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
print(created);
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
usage(1);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
main().catch((e) => {
|
|
191
|
-
console.error("Error:", e.message);
|
|
192
|
-
if (e.data) {
|
|
193
|
-
console.error("Details:");
|
|
194
|
-
console.error(JSON.stringify(e.data, null, 2));
|
|
195
|
-
}
|
|
196
|
-
process.exit(1);
|
|
197
|
-
});
|
|
3
|
+
module.exports = { createProjectsClient };
|
package/package.json
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "teamx-projects",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"type": "commonjs",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Programmatic (non-CLI) client for TeamX Discord Services Projects API (CommonJS, Node 18+).",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"teamx",
|
|
8
|
+
"projects",
|
|
9
|
+
"flyio",
|
|
10
|
+
"discord",
|
|
11
|
+
"api",
|
|
12
|
+
"client"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
6
15
|
"main": "index.js",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
16
|
+
"files": [
|
|
17
|
+
"index.js",
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
12
24
|
},
|
|
13
25
|
"dependencies": {
|
|
14
26
|
"yazl": "^2.5.1"
|
package/src/http.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
function normalizeBaseUrl(baseUrl) {
|
|
2
|
+
return String(baseUrl || "https://teamx-discord-services.fly.dev").replace(/\/$/, "");
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {object} opts
|
|
7
|
+
* @param {string} opts.baseUrl
|
|
8
|
+
* @param {string=} opts.apiKey
|
|
9
|
+
*/
|
|
10
|
+
function createHttpClient({ baseUrl, apiKey }) {
|
|
11
|
+
const BASE_URL = normalizeBaseUrl(baseUrl);
|
|
12
|
+
const API_KEY = apiKey || "";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} p
|
|
16
|
+
* @param {RequestInit=} opts
|
|
17
|
+
*/
|
|
18
|
+
async function fetchJson(p, opts = {}) {
|
|
19
|
+
const url = `${BASE_URL}${p}`;
|
|
20
|
+
|
|
21
|
+
const headers = { "content-type": "application/json", ...(opts.headers || {}) };
|
|
22
|
+
if (API_KEY) headers.Authorization = `Bearer ${API_KEY}`;
|
|
23
|
+
|
|
24
|
+
const res = await fetch(url, { ...opts, headers });
|
|
25
|
+
|
|
26
|
+
const text = await res.text();
|
|
27
|
+
let data;
|
|
28
|
+
try {
|
|
29
|
+
data = text ? JSON.parse(text) : null;
|
|
30
|
+
} catch {
|
|
31
|
+
data = { raw: text };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!res.ok) {
|
|
35
|
+
const msg = (data && data.error) || res.statusText || "Request failed";
|
|
36
|
+
const err = new Error(`${res.status} ${msg}`);
|
|
37
|
+
err.status = res.status;
|
|
38
|
+
err.data = data;
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return { fetchJson, BASE_URL };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = { createHttpClient };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const { createHttpClient } = require("./http");
|
|
4
|
+
const { zipDirToBuffer } = require("./zip");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} CreateProjectsClientOptions
|
|
8
|
+
* @property {string=} baseUrl
|
|
9
|
+
* @property {string=} apiKey
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create a programmatic client for TeamX Projects API.
|
|
14
|
+
* @param {CreateProjectsClientOptions=} opts
|
|
15
|
+
*/
|
|
16
|
+
function createProjectsClient(opts = {}) {
|
|
17
|
+
const http = createHttpClient({
|
|
18
|
+
baseUrl: opts.baseUrl || process.env.BASE_URL || "https://teamx-discord-services.fly.dev",
|
|
19
|
+
apiKey: opts.apiKey || process.env.TEAMX_API_KEY || "",
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
baseUrl: http.BASE_URL,
|
|
24
|
+
|
|
25
|
+
/** GET /api/projects */
|
|
26
|
+
async listProjects() {
|
|
27
|
+
return http.fetchJson("/api/projects");
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
/** GET /api/projects/:id/status */
|
|
31
|
+
async getProjectStatus(projectId) {
|
|
32
|
+
if (!projectId) throw new Error("projectId is required");
|
|
33
|
+
return http.fetchJson(`/api/projects/${encodeURIComponent(projectId)}/status`);
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
/** GET /api/projects/:id/artifacts */
|
|
37
|
+
async getProjectArtifacts(projectId) {
|
|
38
|
+
if (!projectId) throw new Error("projectId is required");
|
|
39
|
+
return http.fetchJson(`/api/projects/${encodeURIComponent(projectId)}/artifacts`);
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
/** POST /api/projects/:id/deploy */
|
|
43
|
+
async deployProject(projectId, { environment = "production", initiated_by = "" } = {}) {
|
|
44
|
+
if (!projectId) throw new Error("projectId is required");
|
|
45
|
+
const payload = { environment, initiated_by };
|
|
46
|
+
return http.fetchJson(`/api/projects/${encodeURIComponent(projectId)}/deploy`, {
|
|
47
|
+
method: "POST",
|
|
48
|
+
body: JSON.stringify(payload),
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* POST /api/projects
|
|
54
|
+
* Zips a folder, base64 encodes it, and uploads as JSON payload.
|
|
55
|
+
*
|
|
56
|
+
* @param {Object} params
|
|
57
|
+
* @param {string} params.name
|
|
58
|
+
* @param {string} params.folderPath
|
|
59
|
+
* @param {"ts"|"js"=} params.language
|
|
60
|
+
* @param {string=} params.template
|
|
61
|
+
* @param {Object=} params.meta
|
|
62
|
+
*/
|
|
63
|
+
async createProjectFromFolder({ name, folderPath, language = "ts", template = "advanced-discord-bot", meta = {} }) {
|
|
64
|
+
if (!name) throw new Error("name is required");
|
|
65
|
+
if (!folderPath) throw new Error("folderPath is required");
|
|
66
|
+
|
|
67
|
+
const abs = path.resolve(folderPath);
|
|
68
|
+
if (!fs.existsSync(abs) || !fs.statSync(abs).isDirectory()) {
|
|
69
|
+
throw new Error(`Invalid folderPath: ${abs}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const zipBuf = await zipDirToBuffer(abs);
|
|
73
|
+
const source = zipBuf.toString("base64");
|
|
74
|
+
|
|
75
|
+
// If your backend expects different keys, change them here.
|
|
76
|
+
const payload = {
|
|
77
|
+
name,
|
|
78
|
+
template,
|
|
79
|
+
language,
|
|
80
|
+
source, // base64 zip
|
|
81
|
+
meta: { createdAt: new Date().toISOString(), ...meta },
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return http.fetchJson("/api/projects", {
|
|
85
|
+
method: "POST",
|
|
86
|
+
body: JSON.stringify(payload),
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
module.exports = { createProjectsClient };
|
package/src/zip.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const yazl = require("yazl");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Zip a directory into an in-memory Buffer (no temp files).
|
|
7
|
+
* Skips node_modules and .git automatically.
|
|
8
|
+
* @param {string} dir
|
|
9
|
+
* @returns {Promise<Buffer>}
|
|
10
|
+
*/
|
|
11
|
+
function zipDirToBuffer(dir) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const zipfile = new yazl.ZipFile();
|
|
14
|
+
const chunks = [];
|
|
15
|
+
const out = zipfile.outputStream;
|
|
16
|
+
|
|
17
|
+
out.on("data", (c) => chunks.push(c));
|
|
18
|
+
out.on("error", reject);
|
|
19
|
+
out.on("end", () => resolve(Buffer.concat(chunks)));
|
|
20
|
+
|
|
21
|
+
function walk(folder, prefix = "") {
|
|
22
|
+
const entries = fs.readdirSync(folder, { withFileTypes: true });
|
|
23
|
+
for (const e of entries) {
|
|
24
|
+
// Skip common junk folders early
|
|
25
|
+
if (e.name === "node_modules" || e.name === ".git") continue;
|
|
26
|
+
|
|
27
|
+
const full = path.join(folder, e.name);
|
|
28
|
+
const rel = path.join(prefix, e.name);
|
|
29
|
+
|
|
30
|
+
if (e.isDirectory()) walk(full, rel);
|
|
31
|
+
else zipfile.addFile(full, rel);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
walk(dir);
|
|
36
|
+
zipfile.end();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = { zipDirToBuffer };
|
package/README.md
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# Projects API Client (CommonJS, .js)
|
|
2
|
-
|
|
3
|
-
Node 18+ (uses built-in `fetch`).
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm i
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Run
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npx teamx-projects list
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Commands
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
list
|
|
21
|
-
status <projectId>
|
|
22
|
-
artifacts <projectId>
|
|
23
|
-
deploy <projectId> --env production|staging|dev --by someone@teamx.com
|
|
24
|
-
create --name <projectName> --path <folderPath> --lang ts|js
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Env
|
|
28
|
-
|
|
29
|
-
- `BASE_URL` (optional) default: `https://teamx-discord-services.fly.dev`
|
|
30
|
-
- `TEAMX_API_KEY` (optional) Bearer token header
|