@bitbybit-dev/create-app 1.0.0-rc.2 → 1.0.1
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 +41 -5
- package/dist/index.js +478 -32
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
- package/templates/cloud/backends/dotnet-rest/BitbybitClient.cs +371 -0
- package/templates/cloud/backends/dotnet-rest/Program.cs +147 -0
- package/templates/cloud/backends/dotnet-rest/Properties/launchSettings.json +14 -0
- package/templates/cloud/backends/dotnet-rest/appsettings.json +13 -0
- package/templates/cloud/backends/dotnet-rest/dotnet-rest.csproj +10 -0
- package/templates/cloud/backends/hono-rest/package.json +19 -0
- package/templates/cloud/backends/hono-rest/src/bitbybit-client.ts +387 -0
- package/templates/cloud/backends/hono-rest/src/index.ts +128 -0
- package/templates/cloud/backends/hono-rest/src/types.ts +4 -0
- package/templates/cloud/backends/hono-rest/tsconfig.json +16 -0
- package/templates/cloud/backends/hono-rest/wrangler.jsonc +11 -0
- package/templates/cloud/backends/hono-sdk/package.json +19 -0
- package/templates/cloud/backends/hono-sdk/src/bitbybit-client.ts +239 -0
- package/templates/cloud/backends/hono-sdk/src/index.ts +155 -0
- package/templates/cloud/backends/hono-sdk/src/types.ts +4 -0
- package/templates/cloud/backends/hono-sdk/tsconfig.json +16 -0
- package/templates/cloud/backends/hono-sdk/wrangler.jsonc +11 -0
- package/templates/cloud/backends/nodejs-rest/.env.example +3 -0
- package/templates/cloud/backends/nodejs-rest/package.json +22 -0
- package/templates/cloud/backends/nodejs-rest/src/bitbybit-client.ts +387 -0
- package/templates/cloud/backends/nodejs-rest/src/index.ts +143 -0
- package/templates/cloud/backends/nodejs-rest/src/types.ts +4 -0
- package/templates/cloud/backends/nodejs-rest/tsconfig.json +17 -0
- package/templates/cloud/backends/nodejs-sdk/.env.example +3 -0
- package/templates/cloud/backends/nodejs-sdk/package.json +23 -0
- package/templates/cloud/backends/nodejs-sdk/src/bitbybit-client.ts +235 -0
- package/templates/cloud/backends/nodejs-sdk/src/index.ts +161 -0
- package/templates/cloud/backends/nodejs-sdk/tsconfig.json +17 -0
- package/templates/cloud/frontend/index.html +13 -0
- package/templates/cloud/frontend/package.json +24 -0
- package/templates/cloud/frontend/src/App.tsx +42 -0
- package/templates/cloud/frontend/src/components/ApiKeyWarning.tsx +41 -0
- package/templates/cloud/frontend/src/components/Header.tsx +8 -0
- package/templates/cloud/frontend/src/components/Tabs.tsx +25 -0
- package/templates/cloud/frontend/src/components/Viewer.tsx +155 -0
- package/templates/cloud/frontend/src/main.tsx +10 -0
- package/templates/cloud/frontend/src/panels/ModelsPanel.tsx +134 -0
- package/templates/cloud/frontend/src/panels/PipelinesPanel.tsx +213 -0
- package/templates/cloud/frontend/src/styles.css +320 -0
- package/templates/cloud/frontend/tsconfig.json +17 -0
- package/templates/cloud/frontend/vite.config.ts +15 -0
- package/templates/vite/babylonjs/typescript/package.json +1 -1
- package/templates/vite/playcanvas/typescript/package.json +1 -1
- package/templates/vite/threejs/typescript/package.json +1 -1
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { BitbybitClient } from "@bitbybit-dev/cad-cloud-sdk";
|
|
2
|
+
import { step } from "@bitbybit-dev/cad-cloud-sdk/pipeline";
|
|
3
|
+
import type { Env } from "./types";
|
|
4
|
+
|
|
5
|
+
function getClient(env: Env): BitbybitClient {
|
|
6
|
+
return new BitbybitClient({
|
|
7
|
+
apiKey: env.BITBYBIT_API_KEY,
|
|
8
|
+
baseUrl: env.BITBYBIT_API_URL,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function createDragonCup(env: Env): Promise<{ taskId: string; downloads: { format: string; downloadUrl: string; filename: string }[] }> {
|
|
13
|
+
const client = getClient(env);
|
|
14
|
+
|
|
15
|
+
return client.models.run("dragon-cup", {
|
|
16
|
+
params: {
|
|
17
|
+
height: 8,
|
|
18
|
+
radiusBottom: 4,
|
|
19
|
+
radiusTopOffset: 2,
|
|
20
|
+
radiusMidOffset: 2,
|
|
21
|
+
rotationTopAngle: 20,
|
|
22
|
+
rotationMidAngle: 20,
|
|
23
|
+
nrSkinCellsVertical: 5,
|
|
24
|
+
nrSkinCellsHorizontal: 10,
|
|
25
|
+
thickness: 0.6,
|
|
26
|
+
bottomThickness: 1,
|
|
27
|
+
scale: 1,
|
|
28
|
+
origin: [0, 0, 0],
|
|
29
|
+
direction: [0, 1, 0],
|
|
30
|
+
},
|
|
31
|
+
outputs: {
|
|
32
|
+
formats: ["gltf"],
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function createDragonCupBatch(env: Env): Promise<{
|
|
38
|
+
taskId: string;
|
|
39
|
+
subTasks: { taskId: string; index: number; downloads: { format: string; downloadUrl: string; filename: string }[] }[];
|
|
40
|
+
}> {
|
|
41
|
+
const client = getClient(env);
|
|
42
|
+
|
|
43
|
+
const result = await client.models.batchRun("dragon-cup", {
|
|
44
|
+
items: [
|
|
45
|
+
{
|
|
46
|
+
params: {
|
|
47
|
+
height: 4, radiusBottom: 4, radiusTopOffset: 2, radiusMidOffset: 2,
|
|
48
|
+
rotationTopAngle: 20, rotationMidAngle: 20,
|
|
49
|
+
nrSkinCellsVertical: 2, nrSkinCellsHorizontal: 6,
|
|
50
|
+
nrSkinCellDivisionsTop: 2, nrSkinCellDivisionsBottom: 3,
|
|
51
|
+
skinCellOuterHeight: 1, skinCellInnerHeight: 0.3,
|
|
52
|
+
skinCellBottomHeight: 1, skinCellTopHeight: 1,
|
|
53
|
+
thickness: 2,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
params: {
|
|
58
|
+
height: 7, radiusBottom: 4, radiusTopOffset: 2, radiusMidOffset: 2,
|
|
59
|
+
rotationTopAngle: 20, rotationMidAngle: 20,
|
|
60
|
+
nrSkinCellsVertical: 1, nrSkinCellsHorizontal: 12,
|
|
61
|
+
nrSkinCellDivisionsTop: 1, nrSkinCellDivisionsBottom: 1,
|
|
62
|
+
skinCellOuterHeight: 2, skinCellInnerHeight: 1,
|
|
63
|
+
skinCellBottomHeight: 1, skinCellTopHeight: 1,
|
|
64
|
+
thickness: 0.5,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
params: {
|
|
69
|
+
height: 9, radiusBottom: 2, radiusTopOffset: 1, radiusMidOffset: 1,
|
|
70
|
+
rotationTopAngle: -30, rotationMidAngle: -30,
|
|
71
|
+
nrSkinCellsVertical: 1, nrSkinCellsHorizontal: 5,
|
|
72
|
+
nrSkinCellDivisionsTop: 1, nrSkinCellDivisionsBottom: 1,
|
|
73
|
+
skinCellOuterHeight: 4, skinCellInnerHeight: -0.3,
|
|
74
|
+
skinCellBottomHeight: 0.5, skinCellTopHeight: 0.3,
|
|
75
|
+
thickness: 0.5,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
outputs: {
|
|
80
|
+
formats: ["gltf"],
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return { taskId: result.taskId, subTasks: [...result.subTasks] };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function getTaskResult(env: Env, taskId: string): Promise<{ status: string; downloads?: { format: string; downloadUrl: string; filename: string }[] }> {
|
|
88
|
+
const client = getClient(env);
|
|
89
|
+
|
|
90
|
+
const task = await client.tasks.get(taskId);
|
|
91
|
+
|
|
92
|
+
if (task.status === "failed" || task.status === "cancelled" || task.status === "expired") {
|
|
93
|
+
throw new Error(`Task ${task.status}: ${task.error ?? "no details"}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (task.status !== "completed") {
|
|
97
|
+
return { status: task.status };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const downloads = await client.tasks.getResults(taskId);
|
|
101
|
+
return { status: "completed", downloads };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Intentionally invalid request — height exceeds the 50cm maximum.
|
|
106
|
+
* The SDK validates against JSON Schema before sending, so this throws
|
|
107
|
+
* a BitbybitValidationError without ever hitting the network.
|
|
108
|
+
*/
|
|
109
|
+
export async function createInvalidCup(env: Env): Promise<never> {
|
|
110
|
+
const client = getClient(env);
|
|
111
|
+
|
|
112
|
+
await client.models.run("dragon-cup", {
|
|
113
|
+
params: {
|
|
114
|
+
height: 999, // max is 50
|
|
115
|
+
radiusBottom: -5, // must be > 0
|
|
116
|
+
},
|
|
117
|
+
outputs: {
|
|
118
|
+
formats: ["gltf"],
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
throw new Error("unreachable");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
// Pipeline examples (using typed step() helper)
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Translate, Union + Fillet: createBox → translate → union → fillet
|
|
131
|
+
* Creates a box, translates a copy, unions both, fillets the result.
|
|
132
|
+
*/
|
|
133
|
+
export async function runTranslateUnionFilletPipeline(env: Env): Promise<{ taskId: string; downloads: { format: string; downloadUrl: string; filename: string }[] }> {
|
|
134
|
+
const client = getClient(env);
|
|
135
|
+
|
|
136
|
+
return client.cad.pipelineAndPoll({
|
|
137
|
+
steps: [
|
|
138
|
+
step("occt.shapes.solid.createBox", { width: 8, length: 6, height: 4, center: [0, 0, 0] }),
|
|
139
|
+
step("occt.transforms.translate", { shape: "$ref:0", translation: [4, 2, 3] }),
|
|
140
|
+
step("occt.booleans.union", { shapes: ["$ref:0", "$ref:1"] }),
|
|
141
|
+
step("occt.fillets.filletEdges", { shape: "$ref:2", radius: 0.5 }),
|
|
142
|
+
],
|
|
143
|
+
outputs: { formats: ["stpz", "gltf"] },
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Map: Cylinders at Positions
|
|
149
|
+
* Parses an array of center positions, maps a cylinder at each, unions the result.
|
|
150
|
+
*/
|
|
151
|
+
export async function runMapCylindersPipeline(env: Env): Promise<{ taskId: string; downloads: { format: string; downloadUrl: string; filename: string }[] }> {
|
|
152
|
+
const client = getClient(env);
|
|
153
|
+
|
|
154
|
+
return client.cad.pipelineAndPoll({
|
|
155
|
+
steps: [
|
|
156
|
+
step("json.parse", { text: "[[0,0,0],[1.5,0,0],[3,0,0],[4.5,0,0]]" }),
|
|
157
|
+
{
|
|
158
|
+
type: "map",
|
|
159
|
+
items: "$ref:0",
|
|
160
|
+
steps: [step("occt.shapes.solid.createCylinder", { radius: 1, height: 5, center: "$item" as never })],
|
|
161
|
+
},
|
|
162
|
+
step("occt.booleans.union", { shapes: "$ref:5" }),
|
|
163
|
+
],
|
|
164
|
+
outputs: { formats: ["stpz", "gltf"] },
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Map: Spheres at Different Radii
|
|
170
|
+
* Creates spheres of radii 1–5 at pre-computed positions, compounds the result.
|
|
171
|
+
*/
|
|
172
|
+
export async function runMapSpheresPipeline(env: Env): Promise<{ taskId: string; downloads: { format: string; downloadUrl: string; filename: string }[] }> {
|
|
173
|
+
const client = getClient(env);
|
|
174
|
+
|
|
175
|
+
return client.cad.pipelineAndPoll({
|
|
176
|
+
steps: [
|
|
177
|
+
step("json.parse", { text: "[[0,0,0],[4,0,0],[10,0,0],[18,0,0],[28,0,0]]" }),
|
|
178
|
+
{
|
|
179
|
+
type: "map",
|
|
180
|
+
items: "$ref:0",
|
|
181
|
+
steps: [
|
|
182
|
+
step("math.twoNrOperation", { first: "$index" as never, second: 1, operation: "add" }),
|
|
183
|
+
step("occt.shapes.solid.createSphere", { radius: "$prev" as never, center: "$item" as never }),
|
|
184
|
+
],
|
|
185
|
+
},
|
|
186
|
+
step("occt.shapes.compound.makeCompound", { shapes: "$ref:11" }),
|
|
187
|
+
],
|
|
188
|
+
outputs: { formats: ["stpz", "gltf"] },
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Choice: Conditional Shape Size
|
|
194
|
+
* Parses a number, increments it, then picks a large box (> 5) or small sphere.
|
|
195
|
+
*/
|
|
196
|
+
export async function runChoicePipeline(env: Env): Promise<{ taskId: string; downloads: { format: string; downloadUrl: string; filename: string }[] }> {
|
|
197
|
+
const client = getClient(env);
|
|
198
|
+
|
|
199
|
+
return client.cad.pipelineAndPoll({
|
|
200
|
+
steps: [
|
|
201
|
+
step("json.parse", { text: "10" }),
|
|
202
|
+
step("math.twoNrOperation", { first: "$ref:0" as never, second: 1, operation: "add" }),
|
|
203
|
+
{
|
|
204
|
+
type: "choice",
|
|
205
|
+
value: "$ref:1",
|
|
206
|
+
operator: "gt",
|
|
207
|
+
compareTo: 5,
|
|
208
|
+
then: [step("occt.shapes.solid.createBox", { width: 10, length: 10, height: 10, center: [0, 0, 0] })],
|
|
209
|
+
else: [step("occt.shapes.solid.createSphere", { radius: 2, center: [0, 0, 0] })],
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
outputs: { formats: ["stpz", "gltf"] },
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* File-input pipeline: import an uploaded STEP file, fillet all edges, export.
|
|
218
|
+
*/
|
|
219
|
+
export async function runFileInputPipeline(env: Env, fileId: string): Promise<{ taskId: string; downloads: { format: string; downloadUrl: string; filename: string }[] }> {
|
|
220
|
+
const client = getClient(env);
|
|
221
|
+
|
|
222
|
+
return client.cad.pipelineAndPoll({
|
|
223
|
+
steps: [
|
|
224
|
+
step("occt.io.loadSTEPorIGES", { filetext: "$file:0", fileName: "shape.step", adjustZtoY: true }),
|
|
225
|
+
step("occt.fillets.filletEdges", { shape: "$ref:0", radius: 0.1 }),
|
|
226
|
+
],
|
|
227
|
+
inputFiles: [{ fileId, role: "input" }],
|
|
228
|
+
outputs: { formats: ["stpz", "gltf"], meshPrecision: 0.001 },
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Upload a file via the SDK and get a fileId for use in pipelines.
|
|
234
|
+
*/
|
|
235
|
+
export async function uploadFile(env: Env, fileBuffer: ArrayBuffer, filename: string): Promise<string> {
|
|
236
|
+
const client = getClient(env);
|
|
237
|
+
const result = await client.files.uploadBytes(filename, fileBuffer);
|
|
238
|
+
return result.fileId;
|
|
239
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { BitbybitValidationError } from "@bitbybit-dev/cad-cloud-sdk";
|
|
3
|
+
import type { Env } from "./types";
|
|
4
|
+
import { createDragonCup, createDragonCupBatch, createInvalidCup, getTaskResult, runTranslateUnionFilletPipeline, runMapCylindersPipeline, runMapSpheresPipeline, runChoicePipeline, runFileInputPipeline, uploadFile } from "./bitbybit-client";
|
|
5
|
+
|
|
6
|
+
const app = new Hono<{ Bindings: Env }>();
|
|
7
|
+
|
|
8
|
+
// Check for missing API key and return a helpful error
|
|
9
|
+
app.use("/api/*", async (c, next) => {
|
|
10
|
+
if (!c.env.BITBYBIT_API_KEY) {
|
|
11
|
+
return c.json({
|
|
12
|
+
error: "BITBYBIT_API_KEY is not configured.",
|
|
13
|
+
help: "You need a Bitbybit API key to use this service. Create an account on https://bitbybit.dev and purchase an API key plan at https://bitbybit.dev/auth/pick-plan?api-keys=true to get access to managed CAD cloud servers.",
|
|
14
|
+
}, 503);
|
|
15
|
+
}
|
|
16
|
+
await next();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Backend endpoint — calls bitbybit API with server-side API key
|
|
20
|
+
app.post("/api/generate", async (c) => {
|
|
21
|
+
try {
|
|
22
|
+
const result = await createDragonCup(c.env);
|
|
23
|
+
return c.json(result);
|
|
24
|
+
} catch (e: unknown) {
|
|
25
|
+
if (e instanceof BitbybitValidationError) {
|
|
26
|
+
return c.json({ error: e.message, validationErrors: e.errors }, 400);
|
|
27
|
+
}
|
|
28
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
29
|
+
return c.json({ error: message }, 500);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Batch generation — creates 3 dragon cup variations in parallel
|
|
34
|
+
app.post("/api/generate-batch", async (c) => {
|
|
35
|
+
try {
|
|
36
|
+
const result = await createDragonCupBatch(c.env);
|
|
37
|
+
return c.json(result);
|
|
38
|
+
} catch (e: unknown) {
|
|
39
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
40
|
+
return c.json({ error: message }, 500);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Validation demo — intentionally sends invalid params to show client-side validation
|
|
45
|
+
app.post("/api/validate-demo", async (c) => {
|
|
46
|
+
try {
|
|
47
|
+
await createInvalidCup(c.env);
|
|
48
|
+
return c.json({ error: "Should not reach here" }, 500);
|
|
49
|
+
} catch (e: unknown) {
|
|
50
|
+
if (e instanceof BitbybitValidationError) {
|
|
51
|
+
return c.json({
|
|
52
|
+
caught: "BitbybitValidationError",
|
|
53
|
+
message: e.message,
|
|
54
|
+
errors: e.errors,
|
|
55
|
+
}, 400);
|
|
56
|
+
}
|
|
57
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
58
|
+
return c.json({ error: message }, 500);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Fetch result for an existing task
|
|
63
|
+
app.get("/api/task/:id", async (c) => {
|
|
64
|
+
try {
|
|
65
|
+
const taskId = c.req.param("id");
|
|
66
|
+
const result = await getTaskResult(c.env, taskId);
|
|
67
|
+
return c.json(result);
|
|
68
|
+
} catch (e: unknown) {
|
|
69
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
70
|
+
return c.json({ error: message }, 500);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Pipeline: translate → union → fillet
|
|
75
|
+
app.post("/api/pipeline/translate-union-fillet", async (c) => {
|
|
76
|
+
try {
|
|
77
|
+
const result = await runTranslateUnionFilletPipeline(c.env);
|
|
78
|
+
return c.json(result);
|
|
79
|
+
} catch (e: unknown) {
|
|
80
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
81
|
+
return c.json({ error: message }, 500);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Pipeline: map cylinders at positions
|
|
86
|
+
app.post("/api/pipeline/map-cylinders", async (c) => {
|
|
87
|
+
try {
|
|
88
|
+
const result = await runMapCylindersPipeline(c.env);
|
|
89
|
+
return c.json(result);
|
|
90
|
+
} catch (e: unknown) {
|
|
91
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
92
|
+
return c.json({ error: message }, 500);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Pipeline: map spheres at different radii
|
|
97
|
+
app.post("/api/pipeline/map-spheres", async (c) => {
|
|
98
|
+
try {
|
|
99
|
+
const result = await runMapSpheresPipeline(c.env);
|
|
100
|
+
return c.json(result);
|
|
101
|
+
} catch (e: unknown) {
|
|
102
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
103
|
+
return c.json({ error: message }, 500);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Pipeline: choice conditional
|
|
108
|
+
app.post("/api/pipeline/choice", async (c) => {
|
|
109
|
+
try {
|
|
110
|
+
const result = await runChoicePipeline(c.env);
|
|
111
|
+
return c.json(result);
|
|
112
|
+
} catch (e: unknown) {
|
|
113
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
114
|
+
return c.json({ error: message }, 500);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Pipeline: file input (upload STEP → fillet)
|
|
119
|
+
app.post("/api/pipeline/file-input", async (c) => {
|
|
120
|
+
try {
|
|
121
|
+
const formData = await c.req.formData();
|
|
122
|
+
const file = formData.get("file") as File | null;
|
|
123
|
+
if (!file) return c.json({ error: "No file uploaded" }, 400);
|
|
124
|
+
|
|
125
|
+
const buffer = await file.arrayBuffer();
|
|
126
|
+
const fileId = await uploadFile(c.env, buffer, file.name);
|
|
127
|
+
const result = await runFileInputPipeline(c.env, fileId);
|
|
128
|
+
return c.json(result);
|
|
129
|
+
} catch (e: unknown) {
|
|
130
|
+
console.error("file-input error:", e);
|
|
131
|
+
const message = e instanceof Error ? e.message : "Unknown error";
|
|
132
|
+
return c.json({ error: message }, 500);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Proxy download — fetches a remote file through the backend to avoid CORS issues with GLTFLoader
|
|
137
|
+
app.get("/api/proxy-download", async (c) => {
|
|
138
|
+
const url = c.req.query("url");
|
|
139
|
+
if (!url) return c.json({ error: "Missing url parameter" }, 400);
|
|
140
|
+
|
|
141
|
+
const response = await fetch(url);
|
|
142
|
+
if (!response.ok) return c.json({ error: `Upstream error: ${response.status}` }, 502);
|
|
143
|
+
|
|
144
|
+
const buffer = await response.arrayBuffer();
|
|
145
|
+
|
|
146
|
+
return new Response(buffer, {
|
|
147
|
+
status: 200,
|
|
148
|
+
headers: {
|
|
149
|
+
"Content-Type": "application/octet-stream",
|
|
150
|
+
"Access-Control-Allow-Origin": "*",
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
export default app;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"lib": ["ES2022"],
|
|
9
|
+
"types": ["@cloudflare/workers-types"],
|
|
10
|
+
"outDir": "dist",
|
|
11
|
+
"rootDir": "src",
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*.ts"]
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "node_modules/wrangler/config-schema.json",
|
|
3
|
+
"name": "bitbybit-api-sdk-threejs-example",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"compatibility_date": "2025-04-16",
|
|
6
|
+
"compatibility_flags": ["nodejs_compat"],
|
|
7
|
+
"dev": { "port": 3000 },
|
|
8
|
+
"vars": {
|
|
9
|
+
"BITBYBIT_API_URL": "https://api.bitbybit.dev"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bitbybit-api-nodejs-rest-threejs",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "tsx watch --env-file=.env src/index.ts",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"start": "node --env-file=.env dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"express": "^5.1.0",
|
|
13
|
+
"multer": "^1.4.5-lts.2"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/express": "^5.0.2",
|
|
17
|
+
"@types/multer": "^1.4.12",
|
|
18
|
+
"@types/node": "^22.15.0",
|
|
19
|
+
"tsx": "^4.19.0",
|
|
20
|
+
"typescript": "^5.8.0"
|
|
21
|
+
}
|
|
22
|
+
}
|