create-cellsweb-rpc-api 1.0.2 → 1.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-cellsweb-rpc-api",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -138,10 +138,10 @@ export const outputSchema = Type.Object({
138
138
  function tplRun(ns: string, fn: string, isMultipart: boolean, mws: string[]): string {
139
139
  const nsPath = ns.replace(/\./g, "/");
140
140
  let contextTypeStr = "MiddlewareContext";
141
- let importContextStr = 'import type { MiddlewareContext } from "@/context";';
141
+ let importContextStr = 'import type { MiddlewareContext } from "@cellsweb/rpc-core";';
142
142
 
143
143
  if (mws.length > 0) {
144
- importContextStr = 'import type { InferContext } from "@/registry";\nimport { middlewares } from "./middlewares";';
144
+ importContextStr = 'import type { InferContext } from "@cellsweb/rpc-core";\nimport { middlewares } from "./middlewares";';
145
145
  contextTypeStr = "InferContext<typeof middlewares>";
146
146
  }
147
147
 
@@ -4,7 +4,6 @@
4
4
  * CellsWeb RPC API Server Entrypoint powered by @cellsweb/rpc-core
5
5
  */
6
6
 
7
- import "./env";
8
7
  import { CoreRPC } from "@cellsweb/rpc-core";
9
8
  import { env } from "./env";
10
9
 
@@ -16,13 +15,4 @@ export const app = new CoreRPC({
16
15
  docs: true,
17
16
  });
18
17
 
19
- export async function start(): Promise<void> {
20
- await app.start();
21
- }
22
-
23
- if (typeof require !== "undefined" && require.main === module) {
24
- start().catch((err) => {
25
- console.error("Startup error:", err);
26
- process.exit(1);
27
- });
28
- }
18
+ await app.start();
package/template/test.ts CHANGED
@@ -3,10 +3,9 @@ import http from "node:http";
3
3
  import fs from "node:fs";
4
4
  import path from "node:path";
5
5
  import os from "node:os";
6
- import { buildRegistry } from "./src/registry";
7
- import { createRequestHandler } from "./src/server";
6
+ import { CoreRPC } from "@cellsweb/rpc-core";
8
7
 
9
- describe("CellsWeb RPC API Suite", () => {
8
+ describe("CellsWeb RPC API Boilerplate Suite", () => {
10
9
  let server: http.Server;
11
10
  let baseUrl: string;
12
11
  let tempDir: string;
@@ -38,25 +37,6 @@ describe("CellsWeb RPC API Suite", () => {
38
37
  };`,
39
38
  );
40
39
 
41
- const rateLimitDir = path.join(middlewaresDir, "rateLimit");
42
- fs.mkdirSync(rateLimitDir, { recursive: true });
43
- fs.writeFileSync(
44
- path.join(rateLimitDir, "constants.ts"),
45
- `import { Type } from "@sinclair/typebox";
46
- export const optionsSchema = Type.Object({ windowSeconds: Type.Number(), maxRequests: Type.Number() });`,
47
- );
48
- fs.writeFileSync(
49
- path.join(rateLimitDir, "run.ts"),
50
- `import { AppError, ErrorCode } from "@/errors";
51
- let callCount = 0;
52
- export const run = async ({ options }) => {
53
- callCount++;
54
- if (callCount > options.maxRequests) {
55
- throw new AppError(ErrorCode.RATE_LIMITED, "Rate limited in test");
56
- }
57
- };`,
58
- );
59
-
60
40
  // ── Create Functions ──
61
41
  // 1. public.echoJson
62
42
  const echoDir = path.join(functionsDir, "public", "echoJson");
@@ -75,71 +55,19 @@ describe("CellsWeb RPC API Suite", () => {
75
55
  );
76
56
  fs.writeFileSync(
77
57
  path.join(echoDir, "middlewares.ts"),
78
- `import { defineMiddlewares } from "@/registry";
58
+ `import { defineMiddlewares } from "@cellsweb/rpc-core";
79
59
  export const middlewares = defineMiddlewares([
80
60
  { name: "requireHeader", options: { tag: "echo-tag" } }
81
61
  ]);`,
82
62
  );
83
63
 
84
- // 2. public.upload
85
- const uploadDir = path.join(functionsDir, "public", "upload");
86
- fs.mkdirSync(uploadDir, { recursive: true });
87
- fs.writeFileSync(
88
- path.join(uploadDir, "constants.ts"),
89
- `import { Type } from "@sinclair/typebox";
90
- import { File } from "@/utils/typebox";
91
- export const metadata = { description: "upload", isPublic: true };
92
- export const contentType = "multipart/form-data";
93
- export const inputSchema = Type.Object({ title: Type.String(), file: File() });
94
- export const outputSchema = Type.Object({ title: Type.String(), filename: Type.String() });`,
95
- );
96
- fs.writeFileSync(
97
- path.join(uploadDir, "run.ts"),
98
- `export const run = async (input) => ({ title: input.title, filename: input.file.filename });`,
99
- );
100
-
101
- // 3. public.badOutput
102
- const badOutDir = path.join(functionsDir, "public", "badOutput");
103
- fs.mkdirSync(badOutDir, { recursive: true });
104
- fs.writeFileSync(
105
- path.join(badOutDir, "constants.ts"),
106
- `import { Type } from "@sinclair/typebox";
107
- export const metadata = { description: "bad output", isPublic: true };
108
- export const contentType = "application/json";
109
- export const inputSchema = Type.Object({});
110
- export const outputSchema = Type.Object({ num: Type.Number() });`,
111
- );
112
- fs.writeFileSync(
113
- path.join(badOutDir, "run.ts"),
114
- `export const run = async () => ({ num: "not-a-number" });`,
115
- );
116
-
117
- // 4. public.rateLimitedFn
118
- const rlFnDir = path.join(functionsDir, "public", "rateLimitedFn");
119
- fs.mkdirSync(rlFnDir, { recursive: true });
120
- fs.writeFileSync(
121
- path.join(rlFnDir, "constants.ts"),
122
- `import { Type } from "@sinclair/typebox";
123
- export const metadata = { description: "rl", isPublic: true };
124
- export const contentType = "application/json";
125
- export const inputSchema = Type.Object({});
126
- export const outputSchema = Type.Object({ ok: Type.Boolean() });`,
127
- );
128
- fs.writeFileSync(
129
- path.join(rlFnDir, "run.ts"),
130
- `export const run = async () => ({ ok: true });`,
131
- );
132
- fs.writeFileSync(
133
- path.join(rlFnDir, "middlewares.ts"),
134
- `import { defineMiddlewares } from "@/registry";
135
- export const middlewares = defineMiddlewares([
136
- { name: "rateLimit", options: { windowSeconds: 60, maxRequests: 1 } }
137
- ]);`,
138
- );
139
-
140
- await buildRegistry(functionsDir);
64
+ const app = new CoreRPC({
65
+ functionsDir,
66
+ middlewaresDir,
67
+ docs: true,
68
+ });
141
69
 
142
- const handler = createRequestHandler();
70
+ const handler = await app.createHandler();
143
71
  server = http.createServer(handler);
144
72
  await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
145
73
  const addr = server.address() as any;
@@ -151,8 +79,15 @@ describe("CellsWeb RPC API Suite", () => {
151
79
  fs.rmSync(tempDir, { recursive: true, force: true });
152
80
  });
153
81
 
154
- it("1. JSON function succeeds with correct headers and schema", async () => {
155
- const res = await fetch(`${baseUrl}/api`, {
82
+ it("1. Responds to health check at /health", async () => {
83
+ const res = await fetch(`${baseUrl}/health`);
84
+ const json = await res.json();
85
+ expect(res.status).toBe(200);
86
+ expect(json.status).toBe("ok");
87
+ });
88
+
89
+ it("2. JSON RPC function succeeds with headers & schema", async () => {
90
+ const res = await fetch(`${baseUrl}/rpc`, {
156
91
  method: "POST",
157
92
  headers: {
158
93
  "content-type": "application/json",
@@ -169,8 +104,8 @@ describe("CellsWeb RPC API Suite", () => {
169
104
  expect(json.data.reply).toBe("hello_echo-tag");
170
105
  });
171
106
 
172
- it("2. JSON function rejects invalid input schema", async () => {
173
- const res = await fetch(`${baseUrl}/api`, {
107
+ it("3. Rejects invalid input schema", async () => {
108
+ const res = await fetch(`${baseUrl}/rpc`, {
174
109
  method: "POST",
175
110
  headers: {
176
111
  "content-type": "application/json",
@@ -186,80 +121,4 @@ describe("CellsWeb RPC API Suite", () => {
186
121
  expect(json.status).toBe("error");
187
122
  expect(json.code).toBe("validation_error");
188
123
  });
189
-
190
- it("3. Multipart function succeeds and receives files under data", async () => {
191
- const form = new FormData();
192
- form.append("namespace", "public");
193
- form.append("functionName", "upload");
194
- form.append("title", "AvatarImage");
195
- form.append("file", new Blob(["file-content"], { type: "text/plain" }), "avatar.txt");
196
-
197
- const res = await fetch(`${baseUrl}/api`, {
198
- method: "POST",
199
- body: form,
200
- });
201
- const json = await res.json();
202
- expect(json.status).toBe("ok");
203
- expect(json.data.title).toBe("AvatarImage");
204
- expect(json.data.filename).toBe("avatar.txt");
205
- });
206
-
207
- it("4. Multipart request to JSON function returns invalid_content_type", async () => {
208
- const form = new FormData();
209
- form.append("namespace", "public");
210
- form.append("functionName", "echoJson");
211
-
212
- const res = await fetch(`${baseUrl}/api`, {
213
- method: "POST",
214
- body: form,
215
- });
216
- const json = await res.json();
217
- expect(json.status).toBe("error");
218
- expect(json.code).toBe("invalid_content_type");
219
- });
220
-
221
- it("5. JSON request to multipart function returns invalid_content_type", async () => {
222
- const res = await fetch(`${baseUrl}/api`, {
223
- method: "POST",
224
- headers: { "content-type": "application/json" },
225
- body: JSON.stringify({
226
- namespace: "public",
227
- functionName: "upload",
228
- data: {},
229
- }),
230
- });
231
- const json = await res.json();
232
- expect(json.status).toBe("error");
233
- expect(json.code).toBe("invalid_content_type");
234
- });
235
-
236
- it("6. Rate limit options in middlewares.ts are enforced", async () => {
237
- const res1 = await fetch(`${baseUrl}/api`, {
238
- method: "POST",
239
- headers: { "content-type": "application/json" },
240
- body: JSON.stringify({ namespace: "public", functionName: "rateLimitedFn" }),
241
- });
242
- const json1 = await res1.json();
243
- expect(json1.status).toBe("ok");
244
-
245
- const res2 = await fetch(`${baseUrl}/api`, {
246
- method: "POST",
247
- headers: { "content-type": "application/json" },
248
- body: JSON.stringify({ namespace: "public", functionName: "rateLimitedFn" }),
249
- });
250
- const json2 = await res2.json();
251
- expect(json2.status).toBe("error");
252
- expect(json2.code).toBe("rate_limited");
253
- });
254
-
255
- it("7. Output schema mismatch returns schema_mismatch", async () => {
256
- const res = await fetch(`${baseUrl}/api`, {
257
- method: "POST",
258
- headers: { "content-type": "application/json" },
259
- body: JSON.stringify({ namespace: "public", functionName: "badOutput" }),
260
- });
261
- const json = await res.json();
262
- expect(json.status).toBe("error");
263
- expect(json.code).toBe("schema_mismatch");
264
- });
265
124
  });
@@ -1,7 +0,0 @@
1
- {
2
- "watch": ["src"],
3
- "ext": "ts,json",
4
- "ignore": ["node_modules", "dist", "src/**/*.spec.ts"],
5
- "delay": 1500,
6
- "exec": "tsc && tsc-alias && exec node dist/server.js"
7
- }