@morda-dev/create-sdk 1.1.0 → 1.1.3
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/README.md +123 -51
- package/bin/create-sdk.js +261 -261
- package/package.json +28 -3
- package/template/.github/workflows/ci.yml +30 -30
- package/template/LICENSE +21 -21
- package/template/README.md +49 -49
- package/template/etc/project-template.api.md +32 -32
- package/template/src/index.ts +25 -25
- package/template/src/modules/user.ts +31 -31
- package/template/src/types/user.ts +17 -17
package/README.md
CHANGED
|
@@ -1,51 +1,123 @@
|
|
|
1
|
-
# create-sdk
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
## ✨
|
|
15
|
-
|
|
16
|
-
⚡
|
|
17
|
-
|
|
18
|
-
🔒 Strict public API
|
|
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
|
-
|
|
1
|
+
# create-sdk
|
|
2
|
+

|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
> Scaffold a **production-ready TypeScript SDK** with a **strict public API**.
|
|
7
|
+
|
|
8
|
+
`create-sdk` is a CLI tool that bootstraps a modern TypeScript SDK with
|
|
9
|
+
**ESM-first setup**, **API Extractor**, and **CI-ready structure** — so you
|
|
10
|
+
can focus on features, not boilerplate.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## ✨ Features
|
|
15
|
+
|
|
16
|
+
- ⚡ **Interactive CLI** (or fully automated with flags)
|
|
17
|
+
- 📦 **ESM-first TypeScript setup**
|
|
18
|
+
- 🔒 **Strict public API** via API Extractor
|
|
19
|
+
- 🧪 **CI-verified & smoke-tested**
|
|
20
|
+
- 🧱 Clean, scalable project structure
|
|
21
|
+
- 🚫 No hidden magic, no vendor lock-in
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## 🚀 Quick start
|
|
26
|
+
|
|
27
|
+
This creates a clean SDK with a strict public API enforced by API Extractor.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx @morda-dev/create-sdk my-sdk
|
|
31
|
+
cd my-sdk
|
|
32
|
+
npm install
|
|
33
|
+
npm run build
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
That’s it. You now have a production-ready SDK.
|
|
37
|
+
|
|
38
|
+
## 📘 Example SDK
|
|
39
|
+
|
|
40
|
+
See a minimal, production-style SDK generated with **create-sdk**:
|
|
41
|
+
|
|
42
|
+
👉 https://github.com/mordaHQ/example-sdk
|
|
43
|
+
|
|
44
|
+
This example demonstrates:
|
|
45
|
+
- a single, explicit public API entrypoint (`src/index.ts`)
|
|
46
|
+
- API Extractor–controlled surface
|
|
47
|
+
- ESM-first configuration
|
|
48
|
+
- CI-ready setup
|
|
49
|
+
- safe evolution without accidental breaking changes
|
|
50
|
+
|
|
51
|
+
Use it as a reference for structuring real-world SDKs.
|
|
52
|
+
|
|
53
|
+
## ❓ Why create-sdk?
|
|
54
|
+
|
|
55
|
+
Most SDK starters fall into one of two traps:
|
|
56
|
+
|
|
57
|
+
- ❌ too minimal — no real production setup
|
|
58
|
+
- ❌ too complex — opinionated, hard to extend
|
|
59
|
+
|
|
60
|
+
**create-sdk** sits in the middle:
|
|
61
|
+
|
|
62
|
+
> Strict where it matters. Flexible where it should be.
|
|
63
|
+
|
|
64
|
+
You get:
|
|
65
|
+
|
|
66
|
+
- a clearly defined public API
|
|
67
|
+
- predictable build & release flow
|
|
68
|
+
- freedom to grow the SDK your way
|
|
69
|
+
|
|
70
|
+
## 🧩 What’s included
|
|
71
|
+
|
|
72
|
+
- TypeScript project with strict config
|
|
73
|
+
- API Extractor setup
|
|
74
|
+
- Ready-to-use CI workflow
|
|
75
|
+
- Clean module-based structure
|
|
76
|
+
- Example public API and types
|
|
77
|
+
|
|
78
|
+
## 🛠 CLI options
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npx @morda-dev/create-sdk <name> [options]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Options
|
|
85
|
+
|
|
86
|
+
- `--yes` — skip all prompts
|
|
87
|
+
- `--no-install` — skip dependency installation
|
|
88
|
+
- `--no-git` — skip git initialization
|
|
89
|
+
|
|
90
|
+
### Example
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
npx @morda-dev/create-sdk my-sdk --yes --no-install
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## 📦 Requirements
|
|
97
|
+
|
|
98
|
+
- Node.js >= 18
|
|
99
|
+
- npm / pnpm / yarn
|
|
100
|
+
|
|
101
|
+
## ✅ Used in production
|
|
102
|
+
|
|
103
|
+
This template is used to scaffold and maintain real-world TypeScript SDKs.
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
## 🧭 Roadmap
|
|
107
|
+
|
|
108
|
+
- Custom templates
|
|
109
|
+
- Optional monorepo mode
|
|
110
|
+
- Plugin system
|
|
111
|
+
- SDK release automation helpers
|
|
112
|
+
|
|
113
|
+
## 🤝 Contributing
|
|
114
|
+
|
|
115
|
+
Issues and PRs are welcome.
|
|
116
|
+
This project is intentionally kept small, clean, and well-documented.
|
|
117
|
+
|
|
118
|
+
## 📄 License
|
|
119
|
+
|
|
120
|
+
ISC
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
---
|
package/bin/create-sdk.js
CHANGED
|
@@ -1,261 +1,261 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import fs from "fs";
|
|
4
|
-
import path from "path";
|
|
5
|
-
import process from "process";
|
|
6
|
-
import { fileURLToPath } from "url";
|
|
7
|
-
import { execSync } from "child_process";
|
|
8
|
-
import prompts from "prompts";
|
|
9
|
-
|
|
10
|
-
/* ============================================================================
|
|
11
|
-
* Environment checks
|
|
12
|
-
* ============================================================================
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const REQUIRED_NODE_MAJOR = 18;
|
|
16
|
-
const CURRENT_NODE_MAJOR = Number(process.versions.node.split(".")[0]);
|
|
17
|
-
|
|
18
|
-
if (CURRENT_NODE_MAJOR < REQUIRED_NODE_MAJOR) {
|
|
19
|
-
console.error(
|
|
20
|
-
`❌ Node.js ${REQUIRED_NODE_MAJOR}+ is required.\n` +
|
|
21
|
-
` You are using Node.js ${process.versions.node}`
|
|
22
|
-
);
|
|
23
|
-
process.exit(1);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/* ============================================================================
|
|
27
|
-
* CLI args & flags
|
|
28
|
-
* ============================================================================
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
const rawArgs = process.argv.slice(2);
|
|
32
|
-
|
|
33
|
-
const flags = {
|
|
34
|
-
yes: rawArgs.includes("--yes"),
|
|
35
|
-
noGit: rawArgs.includes("--no-git"),
|
|
36
|
-
noInstall: rawArgs.includes("--no-install"),
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
if (rawArgs.includes("--version") || rawArgs.includes("-v")) {
|
|
40
|
-
console.log("create-sdk v1.1.0");
|
|
41
|
-
process.exit(0);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
|
|
45
|
-
console.log(`
|
|
46
|
-
create-sdk — scaffold a production-ready TypeScript SDK
|
|
47
|
-
|
|
48
|
-
Usage:
|
|
49
|
-
npx @morda-dev/create-sdk <project-name>
|
|
50
|
-
|
|
51
|
-
Options:
|
|
52
|
-
--yes Skip prompts and use defaults
|
|
53
|
-
--no-git Do not initialize git repository
|
|
54
|
-
--no-install Do not install dependencies
|
|
55
|
-
--help, -h Show this help
|
|
56
|
-
--version, -v Show version
|
|
57
|
-
|
|
58
|
-
Example:
|
|
59
|
-
npx @morda-dev/create-sdk my-sdk
|
|
60
|
-
npx @morda-dev/create-sdk my-sdk --yes --no-install
|
|
61
|
-
`);
|
|
62
|
-
process.exit(0);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/* ============================================================================
|
|
66
|
-
* Paths
|
|
67
|
-
* ============================================================================
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
71
|
-
const __dirname = path.dirname(__filename);
|
|
72
|
-
const templateDir = path.resolve(__dirname, "../template");
|
|
73
|
-
|
|
74
|
-
/* ============================================================================
|
|
75
|
-
* Project name & interactive flow
|
|
76
|
-
* ============================================================================
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
const argProjectName = rawArgs.find((a) => !a.startsWith("-"));
|
|
80
|
-
|
|
81
|
-
let projectName;
|
|
82
|
-
let initGit = !flags.noGit;
|
|
83
|
-
let installDeps = !flags.noInstall;
|
|
84
|
-
|
|
85
|
-
if (flags.yes && argProjectName) {
|
|
86
|
-
projectName = argProjectName;
|
|
87
|
-
} else if (!argProjectName) {
|
|
88
|
-
const answers = await prompts(
|
|
89
|
-
[
|
|
90
|
-
{
|
|
91
|
-
type: "text",
|
|
92
|
-
name: "name",
|
|
93
|
-
message: "Project name:",
|
|
94
|
-
validate: (v) => (v ? true : "Project name is required"),
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
type: "confirm",
|
|
98
|
-
name: "initGit",
|
|
99
|
-
message: "Initialize git repository?",
|
|
100
|
-
initial: true,
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
type: "confirm",
|
|
104
|
-
name: "installDeps",
|
|
105
|
-
message: "Install dependencies now?",
|
|
106
|
-
initial: false,
|
|
107
|
-
},
|
|
108
|
-
],
|
|
109
|
-
{
|
|
110
|
-
onCancel: () => {
|
|
111
|
-
console.log("\n❌ Cancelled");
|
|
112
|
-
process.exit(1);
|
|
113
|
-
},
|
|
114
|
-
}
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
projectName = answers.name;
|
|
118
|
-
initGit = answers.initGit;
|
|
119
|
-
installDeps = answers.installDeps;
|
|
120
|
-
} else {
|
|
121
|
-
projectName = argProjectName;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (!projectName) {
|
|
125
|
-
console.error("❌ Project name is required");
|
|
126
|
-
process.exit(1);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/* ============================================================================
|
|
130
|
-
* Target directory
|
|
131
|
-
* ============================================================================
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
const dirName = projectName.startsWith("@")
|
|
135
|
-
? projectName.split("/")[1]
|
|
136
|
-
: projectName;
|
|
137
|
-
|
|
138
|
-
const targetDir = path.resolve(process.cwd(), dirName);
|
|
139
|
-
|
|
140
|
-
/* ============================================================================
|
|
141
|
-
* Safety checks
|
|
142
|
-
* ============================================================================
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
if (!fs.existsSync(templateDir)) {
|
|
146
|
-
console.error("❌ Template directory not found:", templateDir);
|
|
147
|
-
process.exit(1);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (fs.existsSync(targetDir)) {
|
|
151
|
-
console.error(`❌ Directory "${dirName}" already exists`);
|
|
152
|
-
process.exit(1);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/* ============================================================================
|
|
156
|
-
* Scaffold project
|
|
157
|
-
* ============================================================================
|
|
158
|
-
*/
|
|
159
|
-
|
|
160
|
-
console.log("🚀 Creating SDK:", projectName);
|
|
161
|
-
|
|
162
|
-
copyDir(templateDir, targetDir);
|
|
163
|
-
|
|
164
|
-
/* ============================================================================
|
|
165
|
-
* Update package.json
|
|
166
|
-
* ============================================================================
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
const pkgPath = path.join(targetDir, "package.json");
|
|
170
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
171
|
-
|
|
172
|
-
pkg.name = projectName;
|
|
173
|
-
pkg.version = "0.1.0";
|
|
174
|
-
delete pkg.private;
|
|
175
|
-
|
|
176
|
-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
|
|
177
|
-
|
|
178
|
-
/* ============================================================================
|
|
179
|
-
* Update README title
|
|
180
|
-
* ============================================================================
|
|
181
|
-
*/
|
|
182
|
-
|
|
183
|
-
const readmePath = path.join(targetDir, "README.md");
|
|
184
|
-
|
|
185
|
-
if (fs.existsSync(readmePath)) {
|
|
186
|
-
const updated = fs
|
|
187
|
-
.readFileSync(readmePath, "utf8")
|
|
188
|
-
.replace(/^# .*/m, `# ${projectName}`);
|
|
189
|
-
|
|
190
|
-
fs.writeFileSync(readmePath, updated);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/* ============================================================================
|
|
194
|
-
* Git init
|
|
195
|
-
* ============================================================================
|
|
196
|
-
*/
|
|
197
|
-
|
|
198
|
-
if (initGit) {
|
|
199
|
-
try {
|
|
200
|
-
execSync("git init", { cwd: targetDir, stdio: "ignore" });
|
|
201
|
-
} catch {}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/* ============================================================================
|
|
205
|
-
* Install dependencies
|
|
206
|
-
* ============================================================================
|
|
207
|
-
*/
|
|
208
|
-
|
|
209
|
-
if (installDeps) {
|
|
210
|
-
try {
|
|
211
|
-
execSync("npm install", { cwd: targetDir, stdio: "inherit" });
|
|
212
|
-
} catch {
|
|
213
|
-
console.warn("⚠️ Failed to install dependencies");
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/* ============================================================================
|
|
218
|
-
* Done
|
|
219
|
-
* ============================================================================
|
|
220
|
-
*/
|
|
221
|
-
|
|
222
|
-
console.log("");
|
|
223
|
-
console.log("🎉 SDK scaffolded successfully!");
|
|
224
|
-
console.log("");
|
|
225
|
-
console.log("Next steps:");
|
|
226
|
-
console.log(` cd ${dirName}`);
|
|
227
|
-
if (!installDeps) console.log(" npm install");
|
|
228
|
-
console.log(" npm run build");
|
|
229
|
-
console.log("");
|
|
230
|
-
console.log("Happy hacking 🚀");
|
|
231
|
-
|
|
232
|
-
/* ============================================================================
|
|
233
|
-
* Helpers
|
|
234
|
-
* ============================================================================
|
|
235
|
-
*/
|
|
236
|
-
|
|
237
|
-
function copyDir(src, dest) {
|
|
238
|
-
fs.mkdirSync(dest, { recursive: true });
|
|
239
|
-
|
|
240
|
-
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
241
|
-
if (shouldIgnore(entry.name)) continue;
|
|
242
|
-
|
|
243
|
-
const srcPath = path.join(src, entry.name);
|
|
244
|
-
const destPath = path.join(dest, entry.name);
|
|
245
|
-
|
|
246
|
-
if (entry.isDirectory()) {
|
|
247
|
-
copyDir(srcPath, destPath);
|
|
248
|
-
} else {
|
|
249
|
-
fs.copyFileSync(srcPath, destPath);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function shouldIgnore(name) {
|
|
255
|
-
return (
|
|
256
|
-
name === "node_modules" ||
|
|
257
|
-
name === ".git" ||
|
|
258
|
-
name === "dist" ||
|
|
259
|
-
name === ".DS_Store"
|
|
260
|
-
);
|
|
261
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import process from "process";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import { execSync } from "child_process";
|
|
8
|
+
import prompts from "prompts";
|
|
9
|
+
|
|
10
|
+
/* ============================================================================
|
|
11
|
+
* Environment checks
|
|
12
|
+
* ============================================================================
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const REQUIRED_NODE_MAJOR = 18;
|
|
16
|
+
const CURRENT_NODE_MAJOR = Number(process.versions.node.split(".")[0]);
|
|
17
|
+
|
|
18
|
+
if (CURRENT_NODE_MAJOR < REQUIRED_NODE_MAJOR) {
|
|
19
|
+
console.error(
|
|
20
|
+
`❌ Node.js ${REQUIRED_NODE_MAJOR}+ is required.\n` +
|
|
21
|
+
` You are using Node.js ${process.versions.node}`
|
|
22
|
+
);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* ============================================================================
|
|
27
|
+
* CLI args & flags
|
|
28
|
+
* ============================================================================
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const rawArgs = process.argv.slice(2);
|
|
32
|
+
|
|
33
|
+
const flags = {
|
|
34
|
+
yes: rawArgs.includes("--yes"),
|
|
35
|
+
noGit: rawArgs.includes("--no-git"),
|
|
36
|
+
noInstall: rawArgs.includes("--no-install"),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (rawArgs.includes("--version") || rawArgs.includes("-v")) {
|
|
40
|
+
console.log("create-sdk v1.1.0");
|
|
41
|
+
process.exit(0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
|
|
45
|
+
console.log(`
|
|
46
|
+
create-sdk — scaffold a production-ready TypeScript SDK
|
|
47
|
+
|
|
48
|
+
Usage:
|
|
49
|
+
npx @morda-dev/create-sdk <project-name>
|
|
50
|
+
|
|
51
|
+
Options:
|
|
52
|
+
--yes Skip prompts and use defaults
|
|
53
|
+
--no-git Do not initialize git repository
|
|
54
|
+
--no-install Do not install dependencies
|
|
55
|
+
--help, -h Show this help
|
|
56
|
+
--version, -v Show version
|
|
57
|
+
|
|
58
|
+
Example:
|
|
59
|
+
npx @morda-dev/create-sdk my-sdk
|
|
60
|
+
npx @morda-dev/create-sdk my-sdk --yes --no-install
|
|
61
|
+
`);
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* ============================================================================
|
|
66
|
+
* Paths
|
|
67
|
+
* ============================================================================
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
71
|
+
const __dirname = path.dirname(__filename);
|
|
72
|
+
const templateDir = path.resolve(__dirname, "../template");
|
|
73
|
+
|
|
74
|
+
/* ============================================================================
|
|
75
|
+
* Project name & interactive flow
|
|
76
|
+
* ============================================================================
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
const argProjectName = rawArgs.find((a) => !a.startsWith("-"));
|
|
80
|
+
|
|
81
|
+
let projectName;
|
|
82
|
+
let initGit = !flags.noGit;
|
|
83
|
+
let installDeps = !flags.noInstall;
|
|
84
|
+
|
|
85
|
+
if (flags.yes && argProjectName) {
|
|
86
|
+
projectName = argProjectName;
|
|
87
|
+
} else if (!argProjectName) {
|
|
88
|
+
const answers = await prompts(
|
|
89
|
+
[
|
|
90
|
+
{
|
|
91
|
+
type: "text",
|
|
92
|
+
name: "name",
|
|
93
|
+
message: "Project name:",
|
|
94
|
+
validate: (v) => (v ? true : "Project name is required"),
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
type: "confirm",
|
|
98
|
+
name: "initGit",
|
|
99
|
+
message: "Initialize git repository?",
|
|
100
|
+
initial: true,
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
type: "confirm",
|
|
104
|
+
name: "installDeps",
|
|
105
|
+
message: "Install dependencies now?",
|
|
106
|
+
initial: false,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
{
|
|
110
|
+
onCancel: () => {
|
|
111
|
+
console.log("\n❌ Cancelled");
|
|
112
|
+
process.exit(1);
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
projectName = answers.name;
|
|
118
|
+
initGit = answers.initGit;
|
|
119
|
+
installDeps = answers.installDeps;
|
|
120
|
+
} else {
|
|
121
|
+
projectName = argProjectName;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!projectName) {
|
|
125
|
+
console.error("❌ Project name is required");
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* ============================================================================
|
|
130
|
+
* Target directory
|
|
131
|
+
* ============================================================================
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
const dirName = projectName.startsWith("@")
|
|
135
|
+
? projectName.split("/")[1]
|
|
136
|
+
: projectName;
|
|
137
|
+
|
|
138
|
+
const targetDir = path.resolve(process.cwd(), dirName);
|
|
139
|
+
|
|
140
|
+
/* ============================================================================
|
|
141
|
+
* Safety checks
|
|
142
|
+
* ============================================================================
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
if (!fs.existsSync(templateDir)) {
|
|
146
|
+
console.error("❌ Template directory not found:", templateDir);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (fs.existsSync(targetDir)) {
|
|
151
|
+
console.error(`❌ Directory "${dirName}" already exists`);
|
|
152
|
+
process.exit(1);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/* ============================================================================
|
|
156
|
+
* Scaffold project
|
|
157
|
+
* ============================================================================
|
|
158
|
+
*/
|
|
159
|
+
|
|
160
|
+
console.log("🚀 Creating SDK:", projectName);
|
|
161
|
+
|
|
162
|
+
copyDir(templateDir, targetDir);
|
|
163
|
+
|
|
164
|
+
/* ============================================================================
|
|
165
|
+
* Update package.json
|
|
166
|
+
* ============================================================================
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
const pkgPath = path.join(targetDir, "package.json");
|
|
170
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
171
|
+
|
|
172
|
+
pkg.name = projectName;
|
|
173
|
+
pkg.version = "0.1.0";
|
|
174
|
+
delete pkg.private;
|
|
175
|
+
|
|
176
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
|
|
177
|
+
|
|
178
|
+
/* ============================================================================
|
|
179
|
+
* Update README title
|
|
180
|
+
* ============================================================================
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
const readmePath = path.join(targetDir, "README.md");
|
|
184
|
+
|
|
185
|
+
if (fs.existsSync(readmePath)) {
|
|
186
|
+
const updated = fs
|
|
187
|
+
.readFileSync(readmePath, "utf8")
|
|
188
|
+
.replace(/^# .*/m, `# ${projectName}`);
|
|
189
|
+
|
|
190
|
+
fs.writeFileSync(readmePath, updated);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/* ============================================================================
|
|
194
|
+
* Git init
|
|
195
|
+
* ============================================================================
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
if (initGit) {
|
|
199
|
+
try {
|
|
200
|
+
execSync("git init", { cwd: targetDir, stdio: "ignore" });
|
|
201
|
+
} catch {}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/* ============================================================================
|
|
205
|
+
* Install dependencies
|
|
206
|
+
* ============================================================================
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
if (installDeps) {
|
|
210
|
+
try {
|
|
211
|
+
execSync("npm install", { cwd: targetDir, stdio: "inherit" });
|
|
212
|
+
} catch {
|
|
213
|
+
console.warn("⚠️ Failed to install dependencies");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* ============================================================================
|
|
218
|
+
* Done
|
|
219
|
+
* ============================================================================
|
|
220
|
+
*/
|
|
221
|
+
|
|
222
|
+
console.log("");
|
|
223
|
+
console.log("🎉 SDK scaffolded successfully!");
|
|
224
|
+
console.log("");
|
|
225
|
+
console.log("Next steps:");
|
|
226
|
+
console.log(` cd ${dirName}`);
|
|
227
|
+
if (!installDeps) console.log(" npm install");
|
|
228
|
+
console.log(" npm run build");
|
|
229
|
+
console.log("");
|
|
230
|
+
console.log("Happy hacking 🚀");
|
|
231
|
+
|
|
232
|
+
/* ============================================================================
|
|
233
|
+
* Helpers
|
|
234
|
+
* ============================================================================
|
|
235
|
+
*/
|
|
236
|
+
|
|
237
|
+
function copyDir(src, dest) {
|
|
238
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
239
|
+
|
|
240
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
241
|
+
if (shouldIgnore(entry.name)) continue;
|
|
242
|
+
|
|
243
|
+
const srcPath = path.join(src, entry.name);
|
|
244
|
+
const destPath = path.join(dest, entry.name);
|
|
245
|
+
|
|
246
|
+
if (entry.isDirectory()) {
|
|
247
|
+
copyDir(srcPath, destPath);
|
|
248
|
+
} else {
|
|
249
|
+
fs.copyFileSync(srcPath, destPath);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function shouldIgnore(name) {
|
|
255
|
+
return (
|
|
256
|
+
name === "node_modules" ||
|
|
257
|
+
name === ".git" ||
|
|
258
|
+
name === "dist" ||
|
|
259
|
+
name === ".DS_Store"
|
|
260
|
+
);
|
|
261
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morda-dev/create-sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "CLI to scaffold a production-ready TypeScript SDK with a strict public API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
|
-
"create-sdk": "bin/create-sdk.js"
|
|
7
|
+
"create-sdk": "./bin/create-sdk.js"
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
10
|
"bin",
|
|
@@ -14,11 +14,36 @@
|
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=18"
|
|
16
16
|
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/mordaHQ/create-sdk.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/mordaHQ/create-sdk",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/mordaHQ/create-sdk/issues"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"create-sdk",
|
|
27
|
+
"typescript",
|
|
28
|
+
"sdk",
|
|
29
|
+
"cli",
|
|
30
|
+
"scaffold",
|
|
31
|
+
"api-extractor",
|
|
32
|
+
"library"
|
|
33
|
+
],
|
|
17
34
|
"scripts": {
|
|
18
|
-
"test:smoke": "node ./bin/create-sdk.js __tmp-smoke-sdk --yes --no-install --no-git && node -e \"import('fs').then(fs => fs.rmSync('__tmp-smoke-sdk', { recursive: true, force: true }))\""
|
|
35
|
+
"test:smoke": "node ./bin/create-sdk.js __tmp-smoke-sdk --yes --no-install --no-git && node -e \"import('fs').then(fs => fs.rmSync('__tmp-smoke-sdk', { recursive: true, force: true }))\"",
|
|
36
|
+
"changeset": "changeset",
|
|
37
|
+
"version": "changeset version",
|
|
38
|
+
"release": "changeset publish"
|
|
19
39
|
},
|
|
20
40
|
"dependencies": {
|
|
21
41
|
"prompts": "^2.4.2"
|
|
22
42
|
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@changesets/cli": "^2.29.8",
|
|
45
|
+
"@types/node": "^18.19.0",
|
|
46
|
+
"undici-types": "^7.16.0"
|
|
47
|
+
},
|
|
23
48
|
"license": "ISC"
|
|
24
49
|
}
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
name: CI
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
pull_request:
|
|
5
|
-
branches: [ main ]
|
|
6
|
-
push:
|
|
7
|
-
branches: [ main ]
|
|
8
|
-
|
|
9
|
-
jobs:
|
|
10
|
-
build-and-api-check:
|
|
11
|
-
runs-on: ubuntu-latest
|
|
12
|
-
|
|
13
|
-
steps:
|
|
14
|
-
- name: Checkout repo
|
|
15
|
-
uses: actions/checkout@v4
|
|
16
|
-
|
|
17
|
-
- name: Setup Node
|
|
18
|
-
uses: actions/setup-node@v4
|
|
19
|
-
with:
|
|
20
|
-
node-version: 20
|
|
21
|
-
cache: npm
|
|
22
|
-
|
|
23
|
-
- name: Install dependencies
|
|
24
|
-
run: npm ci
|
|
25
|
-
|
|
26
|
-
- name: Build
|
|
27
|
-
run: npm run build
|
|
28
|
-
|
|
29
|
-
- name: API Extractor check
|
|
30
|
-
run: npm run api:check
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
branches: [ main ]
|
|
6
|
+
push:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
build-and-api-check:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
|
|
13
|
+
steps:
|
|
14
|
+
- name: Checkout repo
|
|
15
|
+
uses: actions/checkout@v4
|
|
16
|
+
|
|
17
|
+
- name: Setup Node
|
|
18
|
+
uses: actions/setup-node@v4
|
|
19
|
+
with:
|
|
20
|
+
node-version: 20
|
|
21
|
+
cache: npm
|
|
22
|
+
|
|
23
|
+
- name: Install dependencies
|
|
24
|
+
run: npm ci
|
|
25
|
+
|
|
26
|
+
- name: Build
|
|
27
|
+
run: npm run build
|
|
28
|
+
|
|
29
|
+
- name: API Extractor check
|
|
30
|
+
run: npm run api:check
|
package/template/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Morda Dev
|
|
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
|
|
13
|
-
all 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
|
|
20
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
-
IN THE SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Morda Dev
|
|
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
|
|
13
|
+
all 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
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
IN THE SOFTWARE.
|
package/template/README.md
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
# @morda-dev/project-template
|
|
2
|
-
|
|
3
|
-
Production-ready TypeScript SDK template with a strict public API, ESM-first setup,
|
|
4
|
-
and best practices baked in.
|
|
5
|
-
|
|
6
|
-
This package can be used as:
|
|
7
|
-
- a base for building your own SDK
|
|
8
|
-
- a reference for ESM + TypeScript library architecture
|
|
9
|
-
- a starting point for production-ready npm packages
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
## Installation
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install @morda-dev/project-template
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Usage
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import { getUser, sumAges } from '@morda-dev/project-template';
|
|
23
|
-
|
|
24
|
-
const user = getUser(1);
|
|
25
|
-
console.log(user);
|
|
26
|
-
|
|
27
|
-
const totalAge = sumAges([
|
|
28
|
-
{ id: 1, name: 'Alice', age: 20 },
|
|
29
|
-
{ id: 2, name: 'Bob', age: 30 },
|
|
30
|
-
]);
|
|
31
|
-
|
|
32
|
-
console.log(totalAge);
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## API
|
|
36
|
-
### getUser(id: number)
|
|
37
|
-
|
|
38
|
-
Returns a user wrapped in `ApiResult`.
|
|
39
|
-
|
|
40
|
-
### sumAges(users: User[])
|
|
41
|
-
|
|
42
|
-
Returns the sum of all user ages.
|
|
43
|
-
|
|
44
|
-
## License
|
|
45
|
-
|
|
46
|
-
ISC
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
---
|
|
1
|
+
# @morda-dev/project-template
|
|
2
|
+
|
|
3
|
+
Production-ready TypeScript SDK template with a strict public API, ESM-first setup,
|
|
4
|
+
and best practices baked in.
|
|
5
|
+
|
|
6
|
+
This package can be used as:
|
|
7
|
+
- a base for building your own SDK
|
|
8
|
+
- a reference for ESM + TypeScript library architecture
|
|
9
|
+
- a starting point for production-ready npm packages
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @morda-dev/project-template
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { getUser, sumAges } from '@morda-dev/project-template';
|
|
23
|
+
|
|
24
|
+
const user = getUser(1);
|
|
25
|
+
console.log(user);
|
|
26
|
+
|
|
27
|
+
const totalAge = sumAges([
|
|
28
|
+
{ id: 1, name: 'Alice', age: 20 },
|
|
29
|
+
{ id: 2, name: 'Bob', age: 30 },
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
console.log(totalAge);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
### getUser(id: number)
|
|
37
|
+
|
|
38
|
+
Returns a user wrapped in `ApiResult`.
|
|
39
|
+
|
|
40
|
+
### sumAges(users: User[])
|
|
41
|
+
|
|
42
|
+
Returns the sum of all user ages.
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
ISC
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
---
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
## API Report File for "@morda-dev/project-template"
|
|
2
|
-
|
|
3
|
-
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
|
|
7
|
-
// @public
|
|
8
|
-
export type ApiResult<T> = {
|
|
9
|
-
success: true;
|
|
10
|
-
data: T;
|
|
11
|
-
} | {
|
|
12
|
-
success: false;
|
|
13
|
-
error: string;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// @public
|
|
17
|
-
export function getUser(id: number): ApiResult<User>;
|
|
18
|
-
|
|
19
|
-
// @public
|
|
20
|
-
export function sumAges(users: User[]): number;
|
|
21
|
-
|
|
22
|
-
// @public
|
|
23
|
-
export interface User {
|
|
24
|
-
// (undocumented)
|
|
25
|
-
age: number;
|
|
26
|
-
// (undocumented)
|
|
27
|
-
id: number;
|
|
28
|
-
// (undocumented)
|
|
29
|
-
name: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
```
|
|
1
|
+
## API Report File for "@morda-dev/project-template"
|
|
2
|
+
|
|
3
|
+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
|
|
7
|
+
// @public
|
|
8
|
+
export type ApiResult<T> = {
|
|
9
|
+
success: true;
|
|
10
|
+
data: T;
|
|
11
|
+
} | {
|
|
12
|
+
success: false;
|
|
13
|
+
error: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// @public
|
|
17
|
+
export function getUser(id: number): ApiResult<User>;
|
|
18
|
+
|
|
19
|
+
// @public
|
|
20
|
+
export function sumAges(users: User[]): number;
|
|
21
|
+
|
|
22
|
+
// @public
|
|
23
|
+
export interface User {
|
|
24
|
+
// (undocumented)
|
|
25
|
+
age: number;
|
|
26
|
+
// (undocumented)
|
|
27
|
+
id: number;
|
|
28
|
+
// (undocumented)
|
|
29
|
+
name: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
```
|
package/template/src/index.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @packageDocumentation
|
|
3
|
-
*
|
|
4
|
-
* Production-ready TypeScript SDK template.
|
|
5
|
-
*
|
|
6
|
-
* This file defines the public API surface of the package.
|
|
7
|
-
* Only symbols exported from this file are considered stable
|
|
8
|
-
* and part of the public contract.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/* ============================================================================
|
|
12
|
-
* Types (Public)
|
|
13
|
-
* ============================================================================
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/** @public */
|
|
17
|
-
export type { User, ApiResult } from "./types/user.js";
|
|
18
|
-
|
|
19
|
-
/* ============================================================================
|
|
20
|
-
* Functions (Public)
|
|
21
|
-
* ============================================================================
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/** @public */
|
|
25
|
-
export { getUser, sumAges } from "./modules/user.js";
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Production-ready TypeScript SDK template.
|
|
5
|
+
*
|
|
6
|
+
* This file defines the public API surface of the package.
|
|
7
|
+
* Only symbols exported from this file are considered stable
|
|
8
|
+
* and part of the public contract.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* ============================================================================
|
|
12
|
+
* Types (Public)
|
|
13
|
+
* ============================================================================
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** @public */
|
|
17
|
+
export type { User, ApiResult } from "./types/user.js";
|
|
18
|
+
|
|
19
|
+
/* ============================================================================
|
|
20
|
+
* Functions (Public)
|
|
21
|
+
* ============================================================================
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** @public */
|
|
25
|
+
export { getUser, sumAges } from "./modules/user.js";
|
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { ApiResult, User } from "../types/user.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @public
|
|
5
|
-
* Get a user by id
|
|
6
|
-
*/
|
|
7
|
-
export function getUser(id: number): ApiResult<User> {
|
|
8
|
-
if (id <= 0) {
|
|
9
|
-
return {
|
|
10
|
-
success: false,
|
|
11
|
-
error: "Invalid user id",
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return {
|
|
16
|
-
success: true,
|
|
17
|
-
data: {
|
|
18
|
-
id,
|
|
19
|
-
name: "John Doe",
|
|
20
|
-
age: 30,
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @public
|
|
27
|
-
* Sum ages of users
|
|
28
|
-
*/
|
|
29
|
-
export function sumAges(users: User[]): number {
|
|
30
|
-
return users.reduce((sum, user) => sum + user.age, 0);
|
|
31
|
-
}
|
|
1
|
+
import { ApiResult, User } from "../types/user.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
* Get a user by id
|
|
6
|
+
*/
|
|
7
|
+
export function getUser(id: number): ApiResult<User> {
|
|
8
|
+
if (id <= 0) {
|
|
9
|
+
return {
|
|
10
|
+
success: false,
|
|
11
|
+
error: "Invalid user id",
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
success: true,
|
|
17
|
+
data: {
|
|
18
|
+
id,
|
|
19
|
+
name: "John Doe",
|
|
20
|
+
age: 30,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
* Sum ages of users
|
|
28
|
+
*/
|
|
29
|
+
export function sumAges(users: User[]): number {
|
|
30
|
+
return users.reduce((sum, user) => sum + user.age, 0);
|
|
31
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @public
|
|
3
|
-
* User entity
|
|
4
|
-
*/
|
|
5
|
-
export interface User {
|
|
6
|
-
id: number;
|
|
7
|
-
name: string;
|
|
8
|
-
age: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @public
|
|
13
|
-
* Generic API result
|
|
14
|
-
*/
|
|
15
|
-
export type ApiResult<T> =
|
|
16
|
-
| { success: true; data: T }
|
|
17
|
-
| { success: false; error: string };
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
* User entity
|
|
4
|
+
*/
|
|
5
|
+
export interface User {
|
|
6
|
+
id: number;
|
|
7
|
+
name: string;
|
|
8
|
+
age: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @public
|
|
13
|
+
* Generic API result
|
|
14
|
+
*/
|
|
15
|
+
export type ApiResult<T> =
|
|
16
|
+
| { success: true; data: T }
|
|
17
|
+
| { success: false; error: string };
|