teamx-projects 1.1.1 → 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/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const { createProjectsClient } = require("./src/projectsClient");
2
+
3
+ module.exports = { createProjectsClient };
package/package.json CHANGED
@@ -1,40 +1,27 @@
1
1
  {
2
2
  "name": "teamx-projects",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "type": "commonjs",
5
- "description": "CLI client for TeamX Discord Services Projects API (CommonJS, Node 18+).",
5
+ "description": "Programmatic (non-CLI) client for TeamX Discord Services Projects API (CommonJS, Node 18+).",
6
6
  "keywords": [
7
7
  "teamx",
8
8
  "projects",
9
9
  "flyio",
10
10
  "discord",
11
- "cli",
12
- "api"
11
+ "api",
12
+ "client"
13
13
  ],
14
14
  "license": "MIT",
15
- "homepage": "https://teamx-discord-services.fly.dev/",
16
- "repository": {
17
- "type": "git",
18
- "url": ""
19
- },
20
- "bugs": {
21
- "url": "https://teamx-discord-services.fly.dev/"
22
- },
23
- "main": "projects-api-client.js",
24
- "bin": {
25
- "teamx-projects": "projects-api-client.js"
26
- },
15
+ "main": "index.js",
27
16
  "files": [
28
- "projects-api-client.js",
17
+ "index.js",
18
+ "src",
29
19
  "README.md",
30
20
  "LICENSE"
31
21
  ],
32
22
  "engines": {
33
23
  "node": ">=18"
34
24
  },
35
- "scripts": {
36
- "start": "node projects-api-client.js"
37
- },
38
25
  "dependencies": {
39
26
  "yazl": "^2.5.1"
40
27
  }
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,71 +0,0 @@
1
- # teamx-projects-api-client
2
-
3
- A small CLI client for the **TeamX Discord Services Projects API**.
4
-
5
- - Node **18+** (uses built-in `fetch`)
6
- - CommonJS (`.js` + `"type": "commonjs"`)
7
- - Can **create** projects by zipping a local folder and POSTing to `/api/projects`
8
- - Can **deploy**, check **status**, and fetch **artifacts**
9
-
10
- ## Install
11
-
12
- ```bash
13
- npm i -g teamx-projects-api-client
14
- ```
15
-
16
- Or use without installing:
17
-
18
- ```bash
19
- npx teamx-projects-api-client list
20
- ```
21
-
22
- ## Usage
23
-
24
- The CLI command is:
25
-
26
- ```bash
27
- teamx-projects <command> [...args]
28
- ```
29
-
30
- Examples:
31
-
32
- ```bash
33
- # list projects
34
- BASE_URL=https://teamx-discord-services.fly.dev teamx-projects list
35
-
36
- # create project from a local folder (zips -> base64 -> POST /api/projects)
37
- teamx-projects create --name my-bot --path ./my-bot --lang ts
38
-
39
- # status
40
- teamx-projects status <projectId>
41
-
42
- # artifacts
43
- teamx-projects artifacts <projectId>
44
-
45
- # deploy
46
- teamx-projects deploy <projectId> --env production --by someone@teamx.com
47
- ```
48
-
49
- ## Commands
50
-
51
- ```bash
52
- list
53
- status <projectId>
54
- artifacts <projectId>
55
- deploy <projectId> --env production|staging|dev --by someone@teamx.com
56
- create --name <projectName> --path <folderPath> --lang ts|js
57
- ```
58
-
59
- ## Environment variables
60
-
61
- - `BASE_URL` (optional) default: `https://teamx-discord-services.fly.dev`
62
- - `TEAMX_API_KEY` (optional) sends `Authorization: Bearer <token>`
63
-
64
- ## Publishing (public)
65
-
66
- ```bash
67
- npm login
68
- npm publish --access public
69
- ```
70
-
71
- If you use a scoped package (like `@teamx/teamx-projects-api-client`), you still need `--access public` the first time.
@@ -1,200 +0,0 @@
1
- \
2
- #!/usr/bin/env node
3
- /**
4
- * TeamX Projects API Client (CommonJS, .js)
5
- * Node 18+ (built-in fetch)
6
- *
7
- * Commands:
8
- * list
9
- * status <projectId>
10
- * artifacts <projectId>
11
- * deploy <projectId> --env production|staging|dev --by email
12
- * create --name <projectName> --path <folderPath> --lang ts|js
13
- *
14
- * Optional env:
15
- * BASE_URL=https://teamx-discord-services.fly.dev
16
- * TEAMX_API_KEY=... (Bearer token, optional)
17
- */
18
-
19
- const fs = require("node:fs");
20
- const path = require("node:path");
21
- const yazl = require("yazl");
22
-
23
- const BASE_URL = (process.env.BASE_URL || "https://teamx-discord-services.fly.dev").replace(/\/$/, "");
24
- const API_KEY = process.env.TEAMX_API_KEY || "";
25
-
26
- function usage(exitCode = 1) {
27
- console.log(`
28
- TeamX Projects API Client
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
- Env:
38
- BASE_URL=${BASE_URL}
39
- TEAMX_API_KEY=${API_KEY ? "(set)" : "(not set)"}
40
- `);
41
- process.exit(exitCode);
42
- }
43
-
44
- function argValue(flag, def = "") {
45
- const i = process.argv.indexOf(flag);
46
- if (i === -1) return def;
47
- return process.argv[i + 1] ?? def;
48
- }
49
-
50
- async function fetchJson(p, opts = {}) {
51
- const url = `${BASE_URL}${p}`;
52
-
53
- const headers = { "content-type": "application/json", ...(opts.headers || {}) };
54
- if (API_KEY) headers.Authorization = `Bearer ${API_KEY}`;
55
-
56
- const res = await fetch(url, { ...opts, headers });
57
-
58
- const text = await res.text();
59
- let data;
60
- try {
61
- data = text ? JSON.parse(text) : null;
62
- } catch {
63
- data = { raw: text };
64
- }
65
-
66
- if (!res.ok) {
67
- const msg = (data && data.error) || res.statusText || "Request failed";
68
- const err = new Error(`${res.status} ${msg}`);
69
- err.status = res.status;
70
- err.data = data;
71
- throw err;
72
- }
73
-
74
- return data;
75
- }
76
-
77
- function print(obj) {
78
- console.log(JSON.stringify(obj, null, 2));
79
- }
80
-
81
- /**
82
- * Zip a directory into an in-memory Buffer (no temp files).
83
- */
84
- function zipDirToBuffer(dir) {
85
- return new Promise((resolve, reject) => {
86
- const zipfile = new yazl.ZipFile();
87
- const chunks = [];
88
- const out = zipfile.outputStream;
89
-
90
- out.on("data", (c) => chunks.push(c));
91
- out.on("error", reject);
92
- out.on("end", () => resolve(Buffer.concat(chunks)));
93
-
94
- function walk(folder, prefix = "") {
95
- const entries = fs.readdirSync(folder, { withFileTypes: true });
96
- for (const e of entries) {
97
- const full = path.join(folder, e.name);
98
- const rel = path.join(prefix, e.name);
99
-
100
- // Skip common junk
101
- if (e.name === "node_modules" || e.name === ".git") continue;
102
-
103
- if (e.isDirectory()) walk(full, rel);
104
- else zipfile.addFile(full, rel);
105
- }
106
- }
107
-
108
- walk(dir);
109
- zipfile.end();
110
- });
111
- }
112
-
113
- async function main() {
114
- const [, , cmd, ...rest] = process.argv;
115
- if (!cmd) usage(1);
116
-
117
- if (cmd === "list") {
118
- const projects = await fetchJson("/api/projects");
119
- print(projects);
120
- return;
121
- }
122
-
123
- if (cmd === "status") {
124
- const id = rest[0];
125
- if (!id) usage(1);
126
- const status = await fetchJson(`/api/projects/${encodeURIComponent(id)}/status`);
127
- print(status);
128
- return;
129
- }
130
-
131
- if (cmd === "artifacts") {
132
- const id = rest[0];
133
- if (!id) usage(1);
134
- const artifacts = await fetchJson(`/api/projects/${encodeURIComponent(id)}/artifacts`);
135
- print(artifacts);
136
- return;
137
- }
138
-
139
- if (cmd === "deploy") {
140
- const id = rest[0];
141
- if (!id) usage(1);
142
-
143
- const environment = argValue("--env", "production");
144
- const initiated_by = argValue("--by", "");
145
-
146
- const payload = { environment, initiated_by };
147
- const deploy = await fetchJson(`/api/projects/${encodeURIComponent(id)}/deploy`, {
148
- method: "POST",
149
- body: JSON.stringify(payload),
150
- });
151
- print(deploy);
152
- return;
153
- }
154
-
155
- if (cmd === "create") {
156
- const name = argValue("--name");
157
- const folderPath = argValue("--path");
158
- const language = argValue("--lang", "ts");
159
-
160
- if (!name || !folderPath) usage(1);
161
-
162
- const abs = path.resolve(folderPath);
163
- if (!fs.existsSync(abs) || !fs.statSync(abs).isDirectory()) {
164
- throw new Error(`Invalid --path folder: ${abs}`);
165
- }
166
-
167
- const zipBuf = await zipDirToBuffer(abs);
168
- const source = zipBuf.toString("base64");
169
-
170
- // If your backend expects different keys, change them here.
171
- const payload = {
172
- name,
173
- template: "advanced-discord-bot",
174
- language,
175
- source, // base64 zip
176
- meta: {
177
- createdAt: new Date().toISOString(),
178
- },
179
- };
180
-
181
- const created = await fetchJson("/api/projects", {
182
- method: "POST",
183
- body: JSON.stringify(payload),
184
- });
185
-
186
- print(created);
187
- return;
188
- }
189
-
190
- usage(1);
191
- }
192
-
193
- main().catch((e) => {
194
- console.error("Error:", e.message);
195
- if (e.data) {
196
- console.error("Details:");
197
- console.error(JSON.stringify(e.data, null, 2));
198
- }
199
- process.exit(1);
200
- });