@transloadit/convex 0.0.6 → 0.1.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/README.md +26 -17
- package/dist/client/index.d.ts +39 -92
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +34 -104
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/component.d.ts +18 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/apiUtils.d.ts +1 -32
- package/dist/component/apiUtils.d.ts.map +1 -1
- package/dist/component/apiUtils.js.map +1 -1
- package/dist/component/lib.d.ts +33 -90
- package/dist/component/lib.d.ts.map +1 -1
- package/dist/component/lib.js +48 -157
- package/dist/component/lib.js.map +1 -1
- package/dist/component/schema.d.ts +4 -4
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +3 -31
- package/dist/component/schema.js.map +1 -1
- package/dist/shared/schemas.d.ts +419 -0
- package/dist/shared/schemas.d.ts.map +1 -0
- package/dist/shared/schemas.js +194 -0
- package/dist/shared/schemas.js.map +1 -0
- package/dist/test/index.d.ts +4 -4
- package/package.json +4 -16
- package/src/client/index.ts +68 -123
- package/src/component/_generated/component.ts +17 -0
- package/src/component/apiUtils.ts +7 -38
- package/src/component/lib.test.ts +19 -0
- package/src/component/lib.ts +80 -180
- package/src/component/schema.ts +3 -31
- package/src/shared/schemas.ts +279 -0
- package/dist/react/index.d.ts +0 -281
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js +0 -784
- package/dist/react/index.js.map +0 -1
- package/dist/shared/tusUpload.d.ts +0 -13
- package/dist/shared/tusUpload.d.ts.map +0 -1
- package/dist/shared/tusUpload.js +0 -32
- package/dist/shared/tusUpload.js.map +0 -1
- package/src/react/index.test.tsx +0 -340
- package/src/react/index.tsx +0 -1245
- package/src/react/uploadWithTus.test.tsx +0 -192
- package/src/shared/tusUpload.ts +0 -59
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
/// <reference types="vite/client" />
|
|
2
|
-
// @vitest-environment jsdom
|
|
3
|
-
|
|
4
|
-
import { describe, expect, it, vi } from "vitest";
|
|
5
|
-
import type { UploadState } from "./index.tsx";
|
|
6
|
-
|
|
7
|
-
vi.mock("tus-js-client", () => {
|
|
8
|
-
type UploadOptions = {
|
|
9
|
-
onUploadUrlAvailable?: () => void;
|
|
10
|
-
onProgress?: (bytesUploaded: number, bytesTotal: number) => void;
|
|
11
|
-
onSuccess?: () => void;
|
|
12
|
-
onError?: (error: Error) => void;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
class Upload {
|
|
16
|
-
url: string;
|
|
17
|
-
options: UploadOptions;
|
|
18
|
-
file: File;
|
|
19
|
-
aborted = false;
|
|
20
|
-
|
|
21
|
-
constructor(file: File, options: UploadOptions) {
|
|
22
|
-
this.file = file;
|
|
23
|
-
this.options = options;
|
|
24
|
-
this.url = `https://upload.example.com/${encodeURIComponent(file.name)}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
start() {
|
|
28
|
-
const shouldFail = this.file.name.includes("fail");
|
|
29
|
-
const delay = this.file.name.includes("slow") ? 25 : 0;
|
|
30
|
-
this.options.onUploadUrlAvailable?.();
|
|
31
|
-
this.options.onProgress?.(10, 10);
|
|
32
|
-
setTimeout(() => {
|
|
33
|
-
if (this.aborted) return;
|
|
34
|
-
if (shouldFail) {
|
|
35
|
-
this.options.onError?.(new Error("Upload failed"));
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
this.options.onSuccess?.();
|
|
39
|
-
}, delay);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
abort() {
|
|
43
|
-
this.aborted = true;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return { Upload };
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
import {
|
|
51
|
-
uploadFilesWithTransloaditTus,
|
|
52
|
-
uploadWithTransloaditTus,
|
|
53
|
-
} from "./index.tsx";
|
|
54
|
-
|
|
55
|
-
describe("uploadWithTransloaditTus", () => {
|
|
56
|
-
it("uploads with tus and emits progress", async () => {
|
|
57
|
-
const createAssembly = vi.fn(async () => ({
|
|
58
|
-
assemblyId: "asm_123",
|
|
59
|
-
data: {
|
|
60
|
-
tus_url: "https://tus.transloadit.com",
|
|
61
|
-
assembly_ssl_url: "https://transloadit.com/assembly",
|
|
62
|
-
},
|
|
63
|
-
}));
|
|
64
|
-
const file = new File(["hello"], "hello.txt", { type: "text/plain" });
|
|
65
|
-
const states: UploadState[] = [];
|
|
66
|
-
const progress: number[] = [];
|
|
67
|
-
|
|
68
|
-
const result = await uploadWithTransloaditTus(
|
|
69
|
-
createAssembly,
|
|
70
|
-
file,
|
|
71
|
-
{
|
|
72
|
-
numExpectedUploadFiles: 1,
|
|
73
|
-
onProgress: (value) => progress.push(value),
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
onStateChange: (state) => states.push(state),
|
|
77
|
-
},
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
expect(createAssembly).toHaveBeenCalledWith(
|
|
81
|
-
expect.objectContaining({ numExpectedUploadFiles: 1 }),
|
|
82
|
-
);
|
|
83
|
-
expect(result.assemblyId).toBe("asm_123");
|
|
84
|
-
expect(progress).toContain(100);
|
|
85
|
-
expect(states[0]).toEqual({ isUploading: true, progress: 0, error: null });
|
|
86
|
-
expect(states[states.length - 1]).toEqual({
|
|
87
|
-
isUploading: false,
|
|
88
|
-
progress: 100,
|
|
89
|
-
error: null,
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it("fails when tus_url is missing", async () => {
|
|
94
|
-
const createAssembly = vi.fn(async () => ({
|
|
95
|
-
assemblyId: "asm_456",
|
|
96
|
-
data: {},
|
|
97
|
-
}));
|
|
98
|
-
const file = new File(["hello"], "hello.txt", { type: "text/plain" });
|
|
99
|
-
const states: UploadState[] = [];
|
|
100
|
-
|
|
101
|
-
await expect(
|
|
102
|
-
uploadWithTransloaditTus(
|
|
103
|
-
createAssembly,
|
|
104
|
-
file,
|
|
105
|
-
{ numExpectedUploadFiles: 1 },
|
|
106
|
-
{ onStateChange: (state) => states.push(state) },
|
|
107
|
-
),
|
|
108
|
-
).rejects.toThrow("tus_url");
|
|
109
|
-
|
|
110
|
-
const lastState = states[states.length - 1];
|
|
111
|
-
expect(lastState?.error).toBeInstanceOf(Error);
|
|
112
|
-
expect(lastState?.isUploading).toBe(false);
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
describe("uploadFilesWithTransloaditTus", () => {
|
|
117
|
-
const createAssembly = vi.fn(async () => ({
|
|
118
|
-
assemblyId: "asm_multi",
|
|
119
|
-
data: {
|
|
120
|
-
tus_url: "https://tus.transloadit.com",
|
|
121
|
-
assembly_ssl_url: "https://transloadit.com/assembly",
|
|
122
|
-
},
|
|
123
|
-
}));
|
|
124
|
-
|
|
125
|
-
it("uploads multiple files with overall progress", async () => {
|
|
126
|
-
const files = [
|
|
127
|
-
new File(["one"], "one.txt", { type: "text/plain" }),
|
|
128
|
-
new File(["two"], "two.txt", { type: "text/plain" }),
|
|
129
|
-
];
|
|
130
|
-
const overall: number[] = [];
|
|
131
|
-
const perFile: Array<{ name: string; progress: number }> = [];
|
|
132
|
-
|
|
133
|
-
const controller = uploadFilesWithTransloaditTus(createAssembly, files, {
|
|
134
|
-
numExpectedUploadFiles: files.length,
|
|
135
|
-
onOverallProgress: (progress) => overall.push(progress),
|
|
136
|
-
onFileProgress: (file, progress) =>
|
|
137
|
-
perFile.push({ name: file.name, progress }),
|
|
138
|
-
});
|
|
139
|
-
const result = await controller.promise;
|
|
140
|
-
|
|
141
|
-
expect(result.assemblyId).toBe("asm_multi");
|
|
142
|
-
expect(result.files.every((file) => file.status === "success")).toBe(true);
|
|
143
|
-
expect(overall[overall.length - 1]).toBe(100);
|
|
144
|
-
expect(perFile.map((entry) => entry.name)).toEqual(
|
|
145
|
-
expect.arrayContaining(["one.txt", "two.txt"]),
|
|
146
|
-
);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("does not reach 100% before all files start", async () => {
|
|
150
|
-
const files = [
|
|
151
|
-
new File(["slow"], "slow.txt", { type: "text/plain" }),
|
|
152
|
-
new File(["two"], "two.txt", { type: "text/plain" }),
|
|
153
|
-
];
|
|
154
|
-
const overall: number[] = [];
|
|
155
|
-
|
|
156
|
-
const controller = uploadFilesWithTransloaditTus(createAssembly, files, {
|
|
157
|
-
numExpectedUploadFiles: files.length,
|
|
158
|
-
concurrency: 1,
|
|
159
|
-
onOverallProgress: (progress) => overall.push(progress),
|
|
160
|
-
});
|
|
161
|
-
await controller.promise;
|
|
162
|
-
|
|
163
|
-
expect(overall[0]).toBeLessThan(100);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it("returns results on partial failure when failFast is false", async () => {
|
|
167
|
-
const files = [
|
|
168
|
-
new File(["ok"], "ok.txt", { type: "text/plain" }),
|
|
169
|
-
new File(["bad"], "fail.txt", { type: "text/plain" }),
|
|
170
|
-
];
|
|
171
|
-
|
|
172
|
-
await expect(
|
|
173
|
-
uploadFilesWithTransloaditTus(createAssembly, files, {
|
|
174
|
-
failFast: false,
|
|
175
|
-
}).promise,
|
|
176
|
-
).rejects.toThrow("Failed to upload");
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it("cancels uploads and surfaces results", async () => {
|
|
180
|
-
const files = [
|
|
181
|
-
new File(["slow"], "slow.txt", { type: "text/plain" }),
|
|
182
|
-
new File(["slow"], "slow-2.txt", { type: "text/plain" }),
|
|
183
|
-
];
|
|
184
|
-
const controller = uploadFilesWithTransloaditTus(createAssembly, files, {
|
|
185
|
-
failFast: true,
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
controller.cancel();
|
|
189
|
-
|
|
190
|
-
await expect(controller.promise).rejects.toThrow("Upload canceled");
|
|
191
|
-
});
|
|
192
|
-
});
|
package/src/shared/tusUpload.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { parseAssemblyUrls } from "./assemblyUrls.ts";
|
|
2
|
-
import { transloaditError } from "./errors.ts";
|
|
3
|
-
|
|
4
|
-
export type TusUploadConfig = {
|
|
5
|
-
endpoint: string;
|
|
6
|
-
metadata: Record<string, string>;
|
|
7
|
-
addRequestId: boolean;
|
|
8
|
-
tusUrl: string;
|
|
9
|
-
assemblyUrl: string;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type TusMetadataOptions = {
|
|
13
|
-
fieldName?: string;
|
|
14
|
-
metadata?: Record<string, string>;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const buildTusUploadConfig = (
|
|
18
|
-
assemblyData: unknown,
|
|
19
|
-
file: File,
|
|
20
|
-
options: TusMetadataOptions = {},
|
|
21
|
-
): TusUploadConfig => {
|
|
22
|
-
const { tusUrl, assemblyUrl } = parseAssemblyUrls(assemblyData);
|
|
23
|
-
|
|
24
|
-
if (!tusUrl) {
|
|
25
|
-
throw transloaditError(
|
|
26
|
-
"upload",
|
|
27
|
-
"Transloadit response missing tus_url for resumable upload",
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (!assemblyUrl) {
|
|
32
|
-
throw transloaditError(
|
|
33
|
-
"upload",
|
|
34
|
-
"Transloadit response missing assembly_url for resumable upload",
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const metadata: Record<string, string> = {
|
|
39
|
-
filename: file.name,
|
|
40
|
-
...options.metadata,
|
|
41
|
-
};
|
|
42
|
-
if (file.type) {
|
|
43
|
-
metadata.filetype = file.type;
|
|
44
|
-
}
|
|
45
|
-
if (!metadata.fieldname) {
|
|
46
|
-
metadata.fieldname = options.fieldName ?? "file";
|
|
47
|
-
}
|
|
48
|
-
if (!metadata.assembly_url) {
|
|
49
|
-
metadata.assembly_url = assemblyUrl;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
endpoint: tusUrl,
|
|
54
|
-
metadata,
|
|
55
|
-
addRequestId: true,
|
|
56
|
-
tusUrl,
|
|
57
|
-
assemblyUrl,
|
|
58
|
-
};
|
|
59
|
-
};
|