@bitbybit-dev/cad-cloud-sdk 1.1.1 → 1.2.0-rc.0
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/dist/client.d.ts.map +1 -1
- package/dist/client.js +5 -15
- package/dist/client.js.map +1 -1
- package/dist/endpoints/cad.d.ts.map +1 -1
- package/dist/endpoints/cad.js +0 -3
- package/dist/endpoints/cad.js.map +1 -1
- package/dist/endpoints/convert.d.ts.map +1 -1
- package/dist/endpoints/convert.js +0 -3
- package/dist/endpoints/convert.js.map +1 -1
- package/dist/endpoints/files.d.ts.map +1 -1
- package/dist/endpoints/files.js +0 -3
- package/dist/endpoints/files.js.map +1 -1
- package/dist/endpoints/models.d.ts.map +1 -1
- package/dist/endpoints/models.js +0 -3
- package/dist/endpoints/models.js.map +1 -1
- package/dist/endpoints/tasks.d.ts.map +1 -1
- package/dist/endpoints/tasks.js +0 -3
- package/dist/endpoints/tasks.js.map +1 -1
- package/dist/errors.d.ts +4 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +0 -3
- package/dist/errors.js.map +1 -1
- package/dist/fetch-downloads.d.ts.map +1 -1
- package/dist/fetch-downloads.js +0 -3
- package/dist/fetch-downloads.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +1 -1
- package/dist/polling.d.ts.map +1 -1
- package/dist/polling.js +0 -3
- package/dist/polling.js.map +1 -1
- package/dist/types/custom.d.ts.map +1 -1
- package/dist/types/custom.js +0 -5
- package/dist/types/custom.js.map +1 -1
- package/dist/types/generated.d.ts +19 -6
- package/dist/types/generated.d.ts.map +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +0 -7
- package/dist/types/index.js.map +1 -1
- package/dist/types/pipeline-operations.d.ts +54 -46
- package/dist/types/pipeline-operations.d.ts.map +1 -1
- package/dist/types/pipeline-operations.js +1 -1
- package/dist/types/pipeline-operations.js.map +1 -1
- package/dist/unwrap.d.ts.map +1 -1
- package/dist/unwrap.js +0 -3
- package/dist/unwrap.js.map +1 -1
- package/dist/validation/validator.d.ts.map +1 -1
- package/dist/validation/validator.js +0 -8
- package/dist/validation/validator.js.map +1 -1
- package/package.json +6 -3
- package/src/__test__/helpers.ts +71 -0
- package/src/__test__/index.ts +1 -0
- package/src/client.test.ts +206 -0
- package/src/client.ts +153 -0
- package/src/endpoints/cad.test.ts +101 -0
- package/src/endpoints/cad.ts +53 -0
- package/src/endpoints/convert.test.ts +154 -0
- package/src/endpoints/convert.ts +83 -0
- package/src/endpoints/files.test.ts +170 -0
- package/src/endpoints/files.ts +89 -0
- package/src/endpoints/index.ts +5 -0
- package/src/endpoints/models.test.ts +228 -0
- package/src/endpoints/models.ts +131 -0
- package/src/endpoints/tasks.test.ts +193 -0
- package/src/endpoints/tasks.ts +93 -0
- package/src/errors.test.ts +57 -0
- package/src/errors.ts +21 -0
- package/src/fetch-downloads.test.ts +67 -0
- package/src/fetch-downloads.ts +18 -0
- package/src/index.ts +19 -0
- package/src/polling.test.ts +194 -0
- package/src/polling.ts +81 -0
- package/src/types/custom.ts +45 -0
- package/src/types/generated.ts +3136 -0
- package/src/types/index.ts +3 -0
- package/src/types/pipeline-operations.ts +9008 -0
- package/src/types/schema-exports.ts +228 -0
- package/src/unwrap.test.ts +75 -0
- package/src/unwrap.ts +20 -0
- package/src/validation/index.ts +1 -0
- package/src/validation/request-schemas.ts +1001 -0
- package/src/validation/validator.test.ts +218 -0
- package/src/validation/validator.ts +60 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { validateRequestBody, getEndpointSchema, BitbybitValidationError } from "./validator.js";
|
|
3
|
+
|
|
4
|
+
describe("validateRequestBody", () => {
|
|
5
|
+
it("passes for a valid ExecuteBody", () => {
|
|
6
|
+
// Arrange
|
|
7
|
+
const body = { operation: "occt.shapes.solid.createBox", params: { width: 10 } };
|
|
8
|
+
|
|
9
|
+
// Act & Assert
|
|
10
|
+
expect(() => validateRequestBody("ExecuteBody", body)).not.toThrow();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("throws BitbybitValidationError when required field is missing", () => {
|
|
14
|
+
// Arrange
|
|
15
|
+
const body = { params: { width: 10 } };
|
|
16
|
+
|
|
17
|
+
// Act
|
|
18
|
+
const act = () => validateRequestBody("ExecuteBody", body);
|
|
19
|
+
|
|
20
|
+
// Assert
|
|
21
|
+
expect(act).toThrow(BitbybitValidationError);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("includes schema name in error message", () => {
|
|
25
|
+
// Arrange
|
|
26
|
+
const body = { params: { width: 10 } };
|
|
27
|
+
|
|
28
|
+
// Act
|
|
29
|
+
let error: BitbybitValidationError | undefined;
|
|
30
|
+
try {
|
|
31
|
+
validateRequestBody("ExecuteBody", body);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
error = e as BitbybitValidationError;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Assert
|
|
37
|
+
expect(error).toBeInstanceOf(BitbybitValidationError);
|
|
38
|
+
expect(error!.message).toContain("ExecuteBody");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("populates errors array with OutputUnit objects", () => {
|
|
42
|
+
// Arrange
|
|
43
|
+
const body = { params: { width: 10 } };
|
|
44
|
+
|
|
45
|
+
// Act
|
|
46
|
+
let error: BitbybitValidationError | undefined;
|
|
47
|
+
try {
|
|
48
|
+
validateRequestBody("ExecuteBody", body);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
error = e as BitbybitValidationError;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Assert
|
|
54
|
+
expect(error!.errors.length).toBe(2);
|
|
55
|
+
expect(error!.errors[0]).toMatchObject({
|
|
56
|
+
keyword: expect.any(String),
|
|
57
|
+
instanceLocation: expect.any(String),
|
|
58
|
+
error: expect.any(String),
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("rejects extra properties when additionalProperties is false", () => {
|
|
63
|
+
// Arrange
|
|
64
|
+
const body = { operation: "test", bogus: 123 };
|
|
65
|
+
|
|
66
|
+
// Act
|
|
67
|
+
const act = () => validateRequestBody("ExecuteBody", body);
|
|
68
|
+
|
|
69
|
+
// Assert
|
|
70
|
+
expect(act).toThrow(BitbybitValidationError);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("rejects DragonCupBody when height exceeds maximum of 50", () => {
|
|
74
|
+
// Arrange
|
|
75
|
+
const body = { params: { height: 999 }, outputs: { formats: ["gltf"] } };
|
|
76
|
+
|
|
77
|
+
// Act
|
|
78
|
+
const act = () => validateRequestBody("DragonCupBody", body);
|
|
79
|
+
|
|
80
|
+
// Assert
|
|
81
|
+
expect(act).toThrow(BitbybitValidationError);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("passes for a valid DragonCupBody", () => {
|
|
85
|
+
// Arrange
|
|
86
|
+
const body = { params: { height: 8 }, outputs: { formats: ["gltf"] } };
|
|
87
|
+
|
|
88
|
+
// Act & Assert
|
|
89
|
+
expect(() => validateRequestBody("DragonCupBody", body)).not.toThrow();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("rejects FileUploadBody missing required contentType and bytes", () => {
|
|
93
|
+
// Arrange
|
|
94
|
+
const body = { filename: "test.step" };
|
|
95
|
+
|
|
96
|
+
// Act
|
|
97
|
+
let error: BitbybitValidationError | undefined;
|
|
98
|
+
try {
|
|
99
|
+
validateRequestBody("FileUploadBody", body);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
error = e as BitbybitValidationError;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Assert
|
|
105
|
+
expect(error).toBeInstanceOf(BitbybitValidationError);
|
|
106
|
+
expect(error!.message).toContain("contentType");
|
|
107
|
+
expect(error!.message).toContain("bytes");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("skips validation silently for unknown schema names", () => {
|
|
111
|
+
// Arrange
|
|
112
|
+
const body = { anything: true };
|
|
113
|
+
|
|
114
|
+
// Act & Assert
|
|
115
|
+
expect(() => validateRequestBody("NonExistentSchema", body)).not.toThrow();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("passes for a valid PipelineBody", () => {
|
|
119
|
+
// Arrange
|
|
120
|
+
const body = {
|
|
121
|
+
steps: [{ operation: "occt.shapes.solid.createBox", params: { width: 10 } }],
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// Act & Assert
|
|
125
|
+
expect(() => validateRequestBody("PipelineBody", body)).not.toThrow();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("rejects PipelineBody with empty steps array", () => {
|
|
129
|
+
// Arrange
|
|
130
|
+
const body = { steps: [] };
|
|
131
|
+
|
|
132
|
+
// Act
|
|
133
|
+
const act = () => validateRequestBody("PipelineBody", body);
|
|
134
|
+
|
|
135
|
+
// Assert
|
|
136
|
+
expect(act).toThrow(BitbybitValidationError);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("getEndpointSchema", () => {
|
|
141
|
+
it("returns schema name for known static endpoint", () => {
|
|
142
|
+
// Act
|
|
143
|
+
const result = getEndpointSchema("cad.execute");
|
|
144
|
+
|
|
145
|
+
// Assert
|
|
146
|
+
expect(result).toBe("ExecuteBody");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("returns schema name for cad.pipeline", () => {
|
|
150
|
+
// Act
|
|
151
|
+
const result = getEndpointSchema("cad.pipeline");
|
|
152
|
+
|
|
153
|
+
// Assert
|
|
154
|
+
expect(result).toBe("PipelineBody");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("returns schema name for files.upload", () => {
|
|
158
|
+
// Act
|
|
159
|
+
const result = getEndpointSchema("files.upload");
|
|
160
|
+
|
|
161
|
+
// Assert
|
|
162
|
+
expect(result).toBe("FileUploadBody");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("returns schema name for per-model endpoint", () => {
|
|
166
|
+
// Act
|
|
167
|
+
const result = getEndpointSchema("models.submit.dragon-cup");
|
|
168
|
+
|
|
169
|
+
// Assert
|
|
170
|
+
expect(result).toBe("DragonCupBody");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("returns undefined for unknown endpoint key", () => {
|
|
174
|
+
// Act
|
|
175
|
+
const result = getEndpointSchema("unknown.endpoint");
|
|
176
|
+
|
|
177
|
+
// Assert
|
|
178
|
+
expect(result).toBe(undefined);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("BitbybitValidationError", () => {
|
|
183
|
+
it("has name set to BitbybitValidationError", () => {
|
|
184
|
+
// Arrange
|
|
185
|
+
const errors = [{ keyword: "required", keywordLocation: "", instanceLocation: "#", error: "missing field" }];
|
|
186
|
+
|
|
187
|
+
// Act
|
|
188
|
+
const error = new BitbybitValidationError("TestSchema", errors);
|
|
189
|
+
|
|
190
|
+
// Assert
|
|
191
|
+
expect(error.name).toBe("BitbybitValidationError");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("extends Error", () => {
|
|
195
|
+
// Arrange
|
|
196
|
+
const errors = [{ keyword: "required", keywordLocation: "", instanceLocation: "#", error: "missing field" }];
|
|
197
|
+
|
|
198
|
+
// Act
|
|
199
|
+
const error = new BitbybitValidationError("TestSchema", errors);
|
|
200
|
+
|
|
201
|
+
// Assert
|
|
202
|
+
expect(error).toBeInstanceOf(Error);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("stores errors array", () => {
|
|
206
|
+
// Arrange
|
|
207
|
+
const errors = [
|
|
208
|
+
{ keyword: "required", keywordLocation: "", instanceLocation: "#", error: "missing a" },
|
|
209
|
+
{ keyword: "type", keywordLocation: "", instanceLocation: "#/b", error: "wrong type" },
|
|
210
|
+
];
|
|
211
|
+
|
|
212
|
+
// Act
|
|
213
|
+
const error = new BitbybitValidationError("TestSchema", errors);
|
|
214
|
+
|
|
215
|
+
// Assert
|
|
216
|
+
expect(error.errors).toStrictEqual(errors);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Validator } from "@cfworker/json-schema";
|
|
2
|
+
import type { OutputUnit, Schema } from "@cfworker/json-schema";
|
|
3
|
+
import { schemaBundle } from "./request-schemas.js";
|
|
4
|
+
|
|
5
|
+
/** Structured validation error thrown before a request is sent. */
|
|
6
|
+
export class BitbybitValidationError extends Error {
|
|
7
|
+
readonly errors: OutputUnit[];
|
|
8
|
+
|
|
9
|
+
constructor(schemaName: string, errors: OutputUnit[]) {
|
|
10
|
+
const details = errors
|
|
11
|
+
.map((e) => `${e.instanceLocation} ${e.error}`)
|
|
12
|
+
.join("; ");
|
|
13
|
+
super(`Validation failed for ${schemaName}: ${details}`);
|
|
14
|
+
this.name = "BitbybitValidationError";
|
|
15
|
+
this.errors = errors;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const validators = new Map<string, Validator>();
|
|
20
|
+
|
|
21
|
+
function getValidator(schemaName: string): Validator | undefined {
|
|
22
|
+
const cached = validators.get(schemaName);
|
|
23
|
+
if (cached) return cached;
|
|
24
|
+
|
|
25
|
+
const def = (schemaBundle.$defs as unknown as Record<string, Schema>)[schemaName];
|
|
26
|
+
if (!def) return undefined;
|
|
27
|
+
|
|
28
|
+
const rootSchema: Schema = {
|
|
29
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
30
|
+
$ref: `#/$defs/${schemaName}`,
|
|
31
|
+
$defs: schemaBundle.$defs,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const validator = new Validator(rootSchema, "2020-12", false);
|
|
35
|
+
validators.set(schemaName, validator);
|
|
36
|
+
return validator;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Validate `data` against the JSON Schema for `schemaName`.
|
|
41
|
+
* Throws `BitbybitValidationError` if invalid.
|
|
42
|
+
*/
|
|
43
|
+
export function validateRequestBody(schemaName: string, data: unknown): void {
|
|
44
|
+
const validator = getValidator(schemaName);
|
|
45
|
+
if (!validator) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const result = validator.validate(data);
|
|
49
|
+
if (!result.valid) {
|
|
50
|
+
throw new BitbybitValidationError(schemaName, result.errors);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Look up the schema name for a given endpoint key.
|
|
56
|
+
* Returns undefined if not mapped.
|
|
57
|
+
*/
|
|
58
|
+
export function getEndpointSchema(endpointKey: string): string | undefined {
|
|
59
|
+
return (schemaBundle.endpoints as Record<string, string>)[endpointKey];
|
|
60
|
+
}
|