@bitbybit-dev/cad-cloud-sdk 1.1.0 → 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 +253 -47
- 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,53 @@
|
|
|
1
|
+
import type { ExecuteBody, PipelineBody, CompoundExecuteBody, TaskCreatedResult, CompoundTaskCreatedResult, TaskDownload } from "../types/index.js";
|
|
2
|
+
import type { TypedPipelineBody } from "../types/pipeline-operations.js";
|
|
3
|
+
import type { PollOptions } from "../polling.js";
|
|
4
|
+
import { pollTask } from "../polling.js";
|
|
5
|
+
import { unwrap } from "../unwrap.js";
|
|
6
|
+
import { fetchDownloads } from "../fetch-downloads.js";
|
|
7
|
+
|
|
8
|
+
type Fetcher = (method: string, path: string, body?: unknown) => Promise<Response>;
|
|
9
|
+
|
|
10
|
+
export class CadEndpoint {
|
|
11
|
+
constructor(private readonly fetch: Fetcher) {}
|
|
12
|
+
|
|
13
|
+
/** Submit a single CAD operation. Returns immediately with task info. */
|
|
14
|
+
async execute(body: ExecuteBody): Promise<TaskCreatedResult> {
|
|
15
|
+
return unwrap(await this.fetch("POST", "/api/v1/cad/execute", body));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Submit a CAD pipeline (sequential steps with $ref references). */
|
|
19
|
+
async pipeline(body: PipelineBody | TypedPipelineBody): Promise<TaskCreatedResult> {
|
|
20
|
+
return unwrap(await this.fetch("POST", "/api/v1/cad/pipeline", body));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Submit a compound (parallel) CAD execution. */
|
|
24
|
+
async compound(body: CompoundExecuteBody): Promise<CompoundTaskCreatedResult> {
|
|
25
|
+
return unwrap(await this.fetch("POST", "/api/v1/cad/compound", body));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Execute a single CAD operation, poll until complete, and return all downloads.
|
|
30
|
+
*/
|
|
31
|
+
async executeAndPoll(
|
|
32
|
+
body: ExecuteBody,
|
|
33
|
+
opts?: PollOptions,
|
|
34
|
+
): Promise<{ taskId: string; downloads: TaskDownload[] }> {
|
|
35
|
+
const created = await this.execute(body);
|
|
36
|
+
const task = await pollTask(this.fetch, created.taskId, opts);
|
|
37
|
+
const downloads = await fetchDownloads(this.fetch, task.taskId);
|
|
38
|
+
return { taskId: task.taskId, downloads };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Submit a pipeline, poll until complete, and return all downloads.
|
|
43
|
+
*/
|
|
44
|
+
async pipelineAndPoll(
|
|
45
|
+
body: PipelineBody | TypedPipelineBody,
|
|
46
|
+
opts?: PollOptions,
|
|
47
|
+
): Promise<{ taskId: string; downloads: TaskDownload[] }> {
|
|
48
|
+
const created = await this.pipeline(body);
|
|
49
|
+
const task = await pollTask(this.fetch, created.taskId, opts);
|
|
50
|
+
const downloads = await fetchDownloads(this.fetch, task.taskId);
|
|
51
|
+
return { taskId: task.taskId, downloads };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { ConvertEndpoint } from "./convert.js";
|
|
3
|
+
import { NO_WAIT, downloadsResponse, okResponse, spyFetcher, taskResponse } from "../__test__/helpers.js";
|
|
4
|
+
|
|
5
|
+
describe("ConvertEndpoint", () => {
|
|
6
|
+
describe("stepToGltf", () => {
|
|
7
|
+
it("sends POST to /api/v1/convert/step-to-gltf and returns task result", async () => {
|
|
8
|
+
// Arrange
|
|
9
|
+
const taskResult = { taskId: "t-1", status: "queued" };
|
|
10
|
+
const { fn, calls } = spyFetcher(okResponse(taskResult));
|
|
11
|
+
const convert = new ConvertEndpoint(fn);
|
|
12
|
+
const body = { fileId: "f-1" };
|
|
13
|
+
|
|
14
|
+
// Act
|
|
15
|
+
const result = await convert.stepToGltf(body as never);
|
|
16
|
+
|
|
17
|
+
// Assert
|
|
18
|
+
expect(calls[0]!.method).toBe("POST");
|
|
19
|
+
expect(calls[0]!.path).toBe("/api/v1/convert/step-to-gltf");
|
|
20
|
+
expect(calls[0]!.body).toStrictEqual(body);
|
|
21
|
+
expect(result).toStrictEqual(taskResult);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("stepToGltfAdvanced", () => {
|
|
26
|
+
it("sends POST to /api/v1/convert/step-to-gltf-advanced and returns task result", async () => {
|
|
27
|
+
// Arrange
|
|
28
|
+
const taskResult = { taskId: "t-2", status: "queued" };
|
|
29
|
+
const { fn, calls } = spyFetcher(okResponse(taskResult));
|
|
30
|
+
const convert = new ConvertEndpoint(fn);
|
|
31
|
+
const body = { fileId: "f-2", options: { quality: "high" } };
|
|
32
|
+
|
|
33
|
+
// Act
|
|
34
|
+
const result = await convert.stepToGltfAdvanced(body as never);
|
|
35
|
+
|
|
36
|
+
// Assert
|
|
37
|
+
expect(calls[0]!.method).toBe("POST");
|
|
38
|
+
expect(calls[0]!.path).toBe("/api/v1/convert/step-to-gltf-advanced");
|
|
39
|
+
expect(result).toStrictEqual(taskResult);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("stepToGltfWithDraco", () => {
|
|
44
|
+
it("sends POST to /api/v1/convert/step-to-gltf-with-draco and returns task result", async () => {
|
|
45
|
+
// Arrange
|
|
46
|
+
const taskResult = { taskId: "t-3", status: "queued" };
|
|
47
|
+
const { fn, calls } = spyFetcher(okResponse(taskResult));
|
|
48
|
+
const convert = new ConvertEndpoint(fn);
|
|
49
|
+
|
|
50
|
+
// Act
|
|
51
|
+
const result = await convert.stepToGltfWithDraco({ fileId: "f-3" } as never);
|
|
52
|
+
|
|
53
|
+
// Assert
|
|
54
|
+
expect(calls[0]!.path).toBe("/api/v1/convert/step-to-gltf-with-draco");
|
|
55
|
+
expect(result).toStrictEqual(taskResult);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("stepToGltfAdvancedWithDraco", () => {
|
|
60
|
+
it("sends POST to /api/v1/convert/step-to-gltf-advanced-with-draco and returns task result", async () => {
|
|
61
|
+
// Arrange
|
|
62
|
+
const taskResult = { taskId: "t-4", status: "queued" };
|
|
63
|
+
const { fn, calls } = spyFetcher(okResponse(taskResult));
|
|
64
|
+
const convert = new ConvertEndpoint(fn);
|
|
65
|
+
|
|
66
|
+
// Act
|
|
67
|
+
const result = await convert.stepToGltfAdvancedWithDraco({ fileId: "f-4" } as never);
|
|
68
|
+
|
|
69
|
+
// Assert
|
|
70
|
+
expect(calls[0]!.path).toBe("/api/v1/convert/step-to-gltf-advanced-with-draco");
|
|
71
|
+
expect(result).toStrictEqual(taskResult);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("stepToGltfAndPoll", () => {
|
|
76
|
+
it("submits, polls until the task completes, and returns its downloads", async () => {
|
|
77
|
+
// Arrange
|
|
78
|
+
const { fn, calls } = spyFetcher(
|
|
79
|
+
okResponse({ taskId: "t-1", status: "queued" }),
|
|
80
|
+
taskResponse("t-1", "completed"),
|
|
81
|
+
downloadsResponse({ format: "glb", url: "https://example.test/model.glb" }),
|
|
82
|
+
);
|
|
83
|
+
const convert = new ConvertEndpoint(fn);
|
|
84
|
+
|
|
85
|
+
// Act
|
|
86
|
+
const result = await convert.stepToGltfAndPoll({ fileId: "f-1" } as never, NO_WAIT);
|
|
87
|
+
|
|
88
|
+
// Assert
|
|
89
|
+
expect(calls.map((call) => call.path)).toStrictEqual([
|
|
90
|
+
"/api/v1/convert/step-to-gltf",
|
|
91
|
+
"/api/v1/tasks/t-1",
|
|
92
|
+
"/api/v1/tasks/t-1/results",
|
|
93
|
+
]);
|
|
94
|
+
expect(result).toStrictEqual({ taskId: "t-1", downloads: [{ format: "glb", url: "https://example.test/model.glb" }] });
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("stepToGltfAdvancedAndPoll", () => {
|
|
99
|
+
it("submits, polls until the task completes, and returns its downloads", async () => {
|
|
100
|
+
// Arrange
|
|
101
|
+
const { fn, calls } = spyFetcher(
|
|
102
|
+
okResponse({ taskId: "t-2", status: "queued" }),
|
|
103
|
+
taskResponse("t-2", "completed"),
|
|
104
|
+
downloadsResponse({ format: "glb", url: "https://example.test/model.glb" }),
|
|
105
|
+
);
|
|
106
|
+
const convert = new ConvertEndpoint(fn);
|
|
107
|
+
|
|
108
|
+
// Act
|
|
109
|
+
const result = await convert.stepToGltfAdvancedAndPoll({ fileId: "f-2" } as never, NO_WAIT);
|
|
110
|
+
|
|
111
|
+
// Assert
|
|
112
|
+
expect(calls[0]!.path).toBe("/api/v1/convert/step-to-gltf-advanced");
|
|
113
|
+
expect(result.taskId).toBe("t-2");
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("stepToGltfWithDracoAndPoll", () => {
|
|
118
|
+
it("submits, polls until the task completes, and returns its downloads", async () => {
|
|
119
|
+
// Arrange
|
|
120
|
+
const { fn, calls } = spyFetcher(
|
|
121
|
+
okResponse({ taskId: "t-3", status: "queued" }),
|
|
122
|
+
taskResponse("t-3", "completed"),
|
|
123
|
+
downloadsResponse({ format: "glb", url: "https://example.test/model.glb" }),
|
|
124
|
+
);
|
|
125
|
+
const convert = new ConvertEndpoint(fn);
|
|
126
|
+
|
|
127
|
+
// Act
|
|
128
|
+
const result = await convert.stepToGltfWithDracoAndPoll({ fileId: "f-3" } as never, NO_WAIT);
|
|
129
|
+
|
|
130
|
+
// Assert
|
|
131
|
+
expect(calls[0]!.path).toBe("/api/v1/convert/step-to-gltf-with-draco");
|
|
132
|
+
expect(result.downloads).toHaveLength(1);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("stepToGltfAdvancedWithDracoAndPoll", () => {
|
|
137
|
+
it("submits, polls until the task completes, and returns its downloads", async () => {
|
|
138
|
+
// Arrange
|
|
139
|
+
const { fn, calls } = spyFetcher(
|
|
140
|
+
okResponse({ taskId: "t-4", status: "queued" }),
|
|
141
|
+
taskResponse("t-4", "completed"),
|
|
142
|
+
downloadsResponse({ format: "glb", url: "https://example.test/model.glb" }),
|
|
143
|
+
);
|
|
144
|
+
const convert = new ConvertEndpoint(fn);
|
|
145
|
+
|
|
146
|
+
// Act
|
|
147
|
+
const result = await convert.stepToGltfAdvancedWithDracoAndPoll({ fileId: "f-4" } as never, NO_WAIT);
|
|
148
|
+
|
|
149
|
+
// Assert
|
|
150
|
+
expect(calls[0]!.path).toBe("/api/v1/convert/step-to-gltf-advanced-with-draco");
|
|
151
|
+
expect(result.taskId).toBe("t-4");
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { StepToGltfBody, StepToGltfAdvancedBody, StepToGltfWithDracoBody, StepToGltfAdvancedWithDracoBody, TaskCreatedResult, TaskDownload } from "../types/index.js";
|
|
2
|
+
import type { PollOptions } from "../polling.js";
|
|
3
|
+
import { pollTask } from "../polling.js";
|
|
4
|
+
import { unwrap } from "../unwrap.js";
|
|
5
|
+
import { fetchDownloads } from "../fetch-downloads.js";
|
|
6
|
+
|
|
7
|
+
type Fetcher = (method: string, path: string, body?: unknown) => Promise<Response>;
|
|
8
|
+
|
|
9
|
+
export class ConvertEndpoint {
|
|
10
|
+
constructor(private readonly fetch: Fetcher) {}
|
|
11
|
+
|
|
12
|
+
/** Convert STEP to glTF (simple mode). Returns task info. */
|
|
13
|
+
async stepToGltf(body: StepToGltfBody): Promise<TaskCreatedResult> {
|
|
14
|
+
return unwrap(await this.fetch("POST", "/api/v1/convert/step-to-gltf", body));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Convert STEP to glTF (advanced mode with full options). Returns task info. */
|
|
18
|
+
async stepToGltfAdvanced(body: StepToGltfAdvancedBody): Promise<TaskCreatedResult> {
|
|
19
|
+
return unwrap(await this.fetch("POST", "/api/v1/convert/step-to-gltf-advanced", body));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Convert STEP to glTF (simple), poll until complete, return all downloads.
|
|
24
|
+
*/
|
|
25
|
+
async stepToGltfAndPoll(
|
|
26
|
+
body: StepToGltfBody,
|
|
27
|
+
opts?: PollOptions,
|
|
28
|
+
): Promise<{ taskId: string; downloads: TaskDownload[] }> {
|
|
29
|
+
const created = await this.stepToGltf(body);
|
|
30
|
+
const task = await pollTask(this.fetch, created.taskId, opts);
|
|
31
|
+
const downloads = await fetchDownloads(this.fetch, task.taskId);
|
|
32
|
+
return { taskId: task.taskId, downloads };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Convert STEP to glTF (advanced), poll until complete, return all downloads.
|
|
37
|
+
*/
|
|
38
|
+
async stepToGltfAdvancedAndPoll(
|
|
39
|
+
body: StepToGltfAdvancedBody,
|
|
40
|
+
opts?: PollOptions,
|
|
41
|
+
): Promise<{ taskId: string; downloads: TaskDownload[] }> {
|
|
42
|
+
const created = await this.stepToGltfAdvanced(body);
|
|
43
|
+
const task = await pollTask(this.fetch, created.taskId, opts);
|
|
44
|
+
const downloads = await fetchDownloads(this.fetch, task.taskId);
|
|
45
|
+
return { taskId: task.taskId, downloads };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Convert STEP to Draco-compressed glTF (simple mode). Returns task info. */
|
|
49
|
+
async stepToGltfWithDraco(body: StepToGltfWithDracoBody): Promise<TaskCreatedResult> {
|
|
50
|
+
return unwrap(await this.fetch("POST", "/api/v1/convert/step-to-gltf-with-draco", body));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Convert STEP to Draco-compressed glTF (advanced mode with full options). Returns task info. */
|
|
54
|
+
async stepToGltfAdvancedWithDraco(body: StepToGltfAdvancedWithDracoBody): Promise<TaskCreatedResult> {
|
|
55
|
+
return unwrap(await this.fetch("POST", "/api/v1/convert/step-to-gltf-advanced-with-draco", body));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Convert STEP to Draco-compressed glTF (simple), poll until complete, return all downloads.
|
|
60
|
+
*/
|
|
61
|
+
async stepToGltfWithDracoAndPoll(
|
|
62
|
+
body: StepToGltfWithDracoBody,
|
|
63
|
+
opts?: PollOptions,
|
|
64
|
+
): Promise<{ taskId: string; downloads: TaskDownload[] }> {
|
|
65
|
+
const created = await this.stepToGltfWithDraco(body);
|
|
66
|
+
const task = await pollTask(this.fetch, created.taskId, opts);
|
|
67
|
+
const downloads = await fetchDownloads(this.fetch, task.taskId);
|
|
68
|
+
return { taskId: task.taskId, downloads };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Convert STEP to Draco-compressed glTF (advanced), poll until complete, return all downloads.
|
|
73
|
+
*/
|
|
74
|
+
async stepToGltfAdvancedWithDracoAndPoll(
|
|
75
|
+
body: StepToGltfAdvancedWithDracoBody,
|
|
76
|
+
opts?: PollOptions,
|
|
77
|
+
): Promise<{ taskId: string; downloads: TaskDownload[] }> {
|
|
78
|
+
const created = await this.stepToGltfAdvancedWithDraco(body);
|
|
79
|
+
const task = await pollTask(this.fetch, created.taskId, opts);
|
|
80
|
+
const downloads = await fetchDownloads(this.fetch, task.taskId);
|
|
81
|
+
return { taskId: task.taskId, downloads };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from "vitest";
|
|
2
|
+
import { FilesEndpoint } from "./files.js";
|
|
3
|
+
import { okResponse, spyFetcher } from "../__test__/helpers.js";
|
|
4
|
+
import { BitbybitApiError } from "../errors.js";
|
|
5
|
+
|
|
6
|
+
describe("FilesEndpoint", () => {
|
|
7
|
+
describe("upload", () => {
|
|
8
|
+
it("sends POST to /api/v1/files/upload with body", async () => {
|
|
9
|
+
// Arrange
|
|
10
|
+
const uploadResult = { fileId: "f-1", uploadUrl: "https://r2.example.com/upload" };
|
|
11
|
+
const { fn, calls } = spyFetcher(okResponse(uploadResult));
|
|
12
|
+
const files = new FilesEndpoint(fn);
|
|
13
|
+
const body = { filename: "test.step", contentType: "application/step", bytes: 1024 };
|
|
14
|
+
|
|
15
|
+
// Act
|
|
16
|
+
const result = await files.upload(body);
|
|
17
|
+
|
|
18
|
+
// Assert
|
|
19
|
+
expect(calls[0]!.method).toBe("POST");
|
|
20
|
+
expect(calls[0]!.path).toBe("/api/v1/files/upload");
|
|
21
|
+
expect(calls[0]!.body).toStrictEqual(body);
|
|
22
|
+
expect(result).toStrictEqual(uploadResult);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("confirm", () => {
|
|
27
|
+
it("sends POST to /api/v1/files/{id}/confirm", async () => {
|
|
28
|
+
// Arrange
|
|
29
|
+
const confirmResult = { fileId: "f-1", status: "confirmed" };
|
|
30
|
+
const { fn, calls } = spyFetcher(okResponse(confirmResult));
|
|
31
|
+
const files = new FilesEndpoint(fn);
|
|
32
|
+
|
|
33
|
+
// Act
|
|
34
|
+
const result = await files.confirm("f-1");
|
|
35
|
+
|
|
36
|
+
// Assert
|
|
37
|
+
expect(calls[0]!.method).toBe("POST");
|
|
38
|
+
expect(calls[0]!.path).toBe("/api/v1/files/f-1/confirm");
|
|
39
|
+
expect(result).toStrictEqual(confirmResult);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("get", () => {
|
|
44
|
+
it("sends GET to /api/v1/files/{id}", async () => {
|
|
45
|
+
// Arrange
|
|
46
|
+
const fileDetail = { fileId: "f-1", filename: "test.step", status: "confirmed" };
|
|
47
|
+
const { fn, calls } = spyFetcher(okResponse(fileDetail));
|
|
48
|
+
const files = new FilesEndpoint(fn);
|
|
49
|
+
|
|
50
|
+
// Act
|
|
51
|
+
const result = await files.get("f-1");
|
|
52
|
+
|
|
53
|
+
// Assert
|
|
54
|
+
expect(calls[0]!.method).toBe("GET");
|
|
55
|
+
expect(calls[0]!.path).toBe("/api/v1/files/f-1");
|
|
56
|
+
expect(result).toStrictEqual(fileDetail);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("list", () => {
|
|
61
|
+
it("sends GET to /api/v1/files without query when none provided", async () => {
|
|
62
|
+
// Arrange
|
|
63
|
+
const fileList = { files: [], total: 0 };
|
|
64
|
+
const { fn, calls } = spyFetcher(okResponse(fileList));
|
|
65
|
+
const files = new FilesEndpoint(fn);
|
|
66
|
+
|
|
67
|
+
// Act
|
|
68
|
+
const result = await files.list();
|
|
69
|
+
|
|
70
|
+
// Assert
|
|
71
|
+
expect(calls[0]!.method).toBe("GET");
|
|
72
|
+
expect(calls[0]!.path).toBe("/api/v1/files");
|
|
73
|
+
expect(result).toStrictEqual(fileList);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("appends query parameters when provided", async () => {
|
|
77
|
+
// Arrange
|
|
78
|
+
const { fn, calls } = spyFetcher(okResponse({ files: [], total: 0 }));
|
|
79
|
+
const files = new FilesEndpoint(fn);
|
|
80
|
+
|
|
81
|
+
// Act
|
|
82
|
+
await files.list({ page: 1, limit: 5, status: "confirmed" });
|
|
83
|
+
|
|
84
|
+
// Assert
|
|
85
|
+
expect(calls[0]!.path).toContain("page=1");
|
|
86
|
+
expect(calls[0]!.path).toContain("limit=5");
|
|
87
|
+
expect(calls[0]!.path).toContain("status=confirmed");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("delete", () => {
|
|
92
|
+
it("sends DELETE to /api/v1/files/{id}", async () => {
|
|
93
|
+
// Arrange
|
|
94
|
+
const { fn, calls } = spyFetcher(okResponse({ deleted: true }));
|
|
95
|
+
const files = new FilesEndpoint(fn);
|
|
96
|
+
|
|
97
|
+
// Act
|
|
98
|
+
const result = await files.delete("f-1");
|
|
99
|
+
|
|
100
|
+
// Assert
|
|
101
|
+
expect(calls[0]!.method).toBe("DELETE");
|
|
102
|
+
expect(calls[0]!.path).toBe("/api/v1/files/f-1");
|
|
103
|
+
expect(result).toStrictEqual({ deleted: true });
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("uploadBytes", () => {
|
|
108
|
+
afterEach(() => {
|
|
109
|
+
vi.unstubAllGlobals();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("asks for an upload url, puts the bytes there, and confirms the file", async () => {
|
|
113
|
+
// Arrange
|
|
114
|
+
const puts: { url: string; init: RequestInit }[] = [];
|
|
115
|
+
vi.stubGlobal("fetch", (url: string, init: RequestInit) => {
|
|
116
|
+
puts.push({ url, init });
|
|
117
|
+
return Promise.resolve(new Response(null, { status: 200 }));
|
|
118
|
+
});
|
|
119
|
+
const { fn, calls } = spyFetcher(
|
|
120
|
+
okResponse({ fileId: "f-1", uploadUrl: "https://store.test/put" }),
|
|
121
|
+
okResponse({ fileId: "f-1", status: "ready" }),
|
|
122
|
+
);
|
|
123
|
+
const files = new FilesEndpoint(fn);
|
|
124
|
+
const bytes = new Uint8Array([1, 2, 3]);
|
|
125
|
+
|
|
126
|
+
// Act
|
|
127
|
+
const result = await files.uploadBytes("part.step", bytes, "application/step");
|
|
128
|
+
|
|
129
|
+
// Assert
|
|
130
|
+
expect(calls[0]!.body).toStrictEqual({ filename: "part.step", contentType: "application/step", bytes: 3 });
|
|
131
|
+
expect(puts[0]!.url).toBe("https://store.test/put");
|
|
132
|
+
expect(result).toStrictEqual({ fileId: "f-1", status: "ready" });
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("puts the bytes as the content type it was given", async () => {
|
|
136
|
+
// Arrange
|
|
137
|
+
const puts: { init: RequestInit }[] = [];
|
|
138
|
+
vi.stubGlobal("fetch", (_url: string, init: RequestInit) => {
|
|
139
|
+
puts.push({ init });
|
|
140
|
+
return Promise.resolve(new Response(null, { status: 200 }));
|
|
141
|
+
});
|
|
142
|
+
const { fn } = spyFetcher(
|
|
143
|
+
okResponse({ fileId: "f-1", uploadUrl: "https://store.test/put" }),
|
|
144
|
+
okResponse({ fileId: "f-1", status: "ready" }),
|
|
145
|
+
);
|
|
146
|
+
const files = new FilesEndpoint(fn);
|
|
147
|
+
|
|
148
|
+
// Act
|
|
149
|
+
await files.uploadBytes("part.bin", new Uint8Array([1]));
|
|
150
|
+
|
|
151
|
+
// Assert
|
|
152
|
+
expect(puts[0]!.init.method).toBe("PUT");
|
|
153
|
+
expect(puts[0]!.init.headers).toStrictEqual({ "Content-Type": "application/octet-stream" });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("fails with what the store said when the put is refused", async () => {
|
|
157
|
+
// Arrange
|
|
158
|
+
vi.stubGlobal("fetch", () => Promise.resolve(new Response(null, { status: 403, statusText: "Forbidden" })));
|
|
159
|
+
const { fn } = spyFetcher(
|
|
160
|
+
okResponse({ fileId: "f-1", uploadUrl: "https://store.test/put" }),
|
|
161
|
+
okResponse({ fileId: "f-1", uploadUrl: "https://store.test/put" }),
|
|
162
|
+
);
|
|
163
|
+
const files = new FilesEndpoint(fn);
|
|
164
|
+
|
|
165
|
+
// Act & Assert
|
|
166
|
+
await expect(files.uploadBytes("part.step", new Uint8Array([1]))).rejects.toThrow(BitbybitApiError);
|
|
167
|
+
await expect(files.uploadBytes("part.step", new Uint8Array([1]))).rejects.toThrow("PUT to upload URL failed: 403 Forbidden");
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FileUploadBody,
|
|
3
|
+
UploadResult,
|
|
4
|
+
ConfirmResult,
|
|
5
|
+
FileDetail,
|
|
6
|
+
FileList,
|
|
7
|
+
FileListQuery,
|
|
8
|
+
} from "../types/index.js";
|
|
9
|
+
import { BitbybitApiError } from "../errors.js";
|
|
10
|
+
import { unwrap } from "../unwrap.js";
|
|
11
|
+
|
|
12
|
+
type Fetcher = (method: string, path: string, body?: unknown) => Promise<Response>;
|
|
13
|
+
|
|
14
|
+
export class FilesEndpoint {
|
|
15
|
+
constructor(private readonly fetch: Fetcher) {}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Request a pre-signed upload URL.
|
|
19
|
+
* After receiving the result, PUT the file bytes to `uploadUrl`, then call `confirm()`.
|
|
20
|
+
*/
|
|
21
|
+
async upload(body: FileUploadBody): Promise<UploadResult> {
|
|
22
|
+
return unwrap(await this.fetch("POST", "/api/v1/files/upload", body));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Confirm that bytes have been uploaded to the pre-signed URL. */
|
|
26
|
+
async confirm(fileId: string): Promise<ConfirmResult> {
|
|
27
|
+
return unwrap(
|
|
28
|
+
await this.fetch("POST", `/api/v1/files/${encodeURIComponent(fileId)}/confirm`),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Get details for a single file. */
|
|
33
|
+
async get(fileId: string): Promise<FileDetail> {
|
|
34
|
+
return unwrap(
|
|
35
|
+
await this.fetch("GET", `/api/v1/files/${encodeURIComponent(fileId)}`),
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** List uploaded files with optional filters. */
|
|
40
|
+
async list(query?: FileListQuery): Promise<FileList> {
|
|
41
|
+
const params = new URLSearchParams();
|
|
42
|
+
if (query?.page != null) params.set("page", String(query.page));
|
|
43
|
+
if (query?.limit != null) params.set("limit", String(query.limit));
|
|
44
|
+
if (query?.status) params.set("status", query.status);
|
|
45
|
+
const qs = params.toString();
|
|
46
|
+
return unwrap(await this.fetch("GET", `/api/v1/files${qs ? `?${qs}` : ""}`));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Delete a file. */
|
|
50
|
+
async delete(fileId: string): Promise<{ deleted: boolean }> {
|
|
51
|
+
return unwrap(
|
|
52
|
+
await this.fetch("DELETE", `/api/v1/files/${encodeURIComponent(fileId)}`),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Convenience: upload file bytes end-to-end.
|
|
58
|
+
* 1. Requests a pre-signed URL
|
|
59
|
+
* 2. PUTs the bytes
|
|
60
|
+
* 3. Confirms the upload
|
|
61
|
+
*/
|
|
62
|
+
async uploadBytes(
|
|
63
|
+
filename: string,
|
|
64
|
+
data: Uint8Array | ArrayBuffer,
|
|
65
|
+
contentType = "application/octet-stream",
|
|
66
|
+
): Promise<ConfirmResult> {
|
|
67
|
+
const uploadResult = await this.upload({
|
|
68
|
+
filename,
|
|
69
|
+
contentType,
|
|
70
|
+
bytes: data.byteLength,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const putRes = await fetch(uploadResult.uploadUrl, {
|
|
74
|
+
method: "PUT",
|
|
75
|
+
headers: { "Content-Type": contentType },
|
|
76
|
+
body: data as BodyInit,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (!putRes.ok) {
|
|
80
|
+
throw new BitbybitApiError({
|
|
81
|
+
code: "UPLOAD_FAILED",
|
|
82
|
+
message: `PUT to upload URL failed: ${putRes.status} ${putRes.statusText}`,
|
|
83
|
+
statusCode: putRes.status,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return this.confirm(uploadResult.fileId);
|
|
88
|
+
}
|
|
89
|
+
}
|