@edgestore/react 0.5.7 → 0.6.0-canary.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/dist/errors/index.cjs +11 -0
- package/dist/errors/index.d.cts +9 -0
- package/dist/errors/index.d.cts.map +1 -0
- package/dist/errors/index.d.mts +9 -0
- package/dist/errors/index.d.mts.map +1 -0
- package/dist/errors/index.mjs +4 -2
- package/dist/index.cjs +425 -0
- package/dist/index.d.cts +131 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +131 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +369 -436
- package/dist/index.mjs.map +1 -0
- package/dist/shared/index.cjs +11 -0
- package/dist/shared/index.d.cts +11 -0
- package/dist/shared/index.d.cts.map +1 -0
- package/dist/shared/index.d.mts +11 -0
- package/dist/shared/index.d.mts.map +1 -0
- package/dist/shared/index.mjs +7 -5
- package/dist/shared/index.mjs.map +1 -0
- package/dist/uploadAbortedError-1Hka0_24.mjs +11 -0
- package/dist/uploadAbortedError-1Hka0_24.mjs.map +1 -0
- package/dist/uploadAbortedError-CyhLukhg.cjs +16 -0
- package/dist/utils/index.cjs +40 -0
- package/dist/utils/{index.d.ts → index.d.cts} +6 -3
- package/dist/utils/index.d.cts.map +1 -0
- package/dist/utils/index.d.mts +17 -0
- package/dist/utils/index.d.mts.map +1 -0
- package/dist/utils/index.mjs +33 -28
- package/dist/utils/index.mjs.map +1 -0
- package/errors/package.json +1 -0
- package/package.json +48 -29
- package/shared/package.json +1 -0
- package/utils/package.json +1 -0
- package/dist/contextProvider.d.ts +0 -65
- package/dist/contextProvider.d.ts.map +0 -1
- package/dist/createNextProxy.d.ts +0 -71
- package/dist/createNextProxy.d.ts.map +0 -1
- package/dist/errors/index.d.ts +0 -3
- package/dist/errors/index.d.ts.map +0 -1
- package/dist/errors/index.js +0 -14
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -488
- package/dist/libs/errors/EdgeStoreClientError.d.ts +0 -5
- package/dist/libs/errors/EdgeStoreClientError.d.ts.map +0 -1
- package/dist/libs/errors/handleError.d.ts +0 -2
- package/dist/libs/errors/handleError.d.ts.map +0 -1
- package/dist/libs/errors/uploadAbortedError.d.ts +0 -4
- package/dist/libs/errors/uploadAbortedError.d.ts.map +0 -1
- package/dist/shared/index.d.ts +0 -7
- package/dist/shared/index.d.ts.map +0 -1
- package/dist/shared/index.js +0 -13
- package/dist/uploadAbortedError-a628b025.js +0 -8
- package/dist/uploadAbortedError-e1379bb0.mjs +0 -8
- package/dist/uploadAbortedError-fbfcc57b.js +0 -10
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -39
- package/errors/index.d.ts +0 -1
- package/errors/index.js +0 -1
- package/shared/index.d.ts +0 -1
- package/shared/index.js +0 -1
- package/utils/index.d.ts +0 -1
- package/utils/index.js +0 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const require_uploadAbortedError = require('../uploadAbortedError-CyhLukhg.cjs');
|
|
2
|
+
const require_index = require('../index.cjs');
|
|
3
|
+
let _edgestore_shared = require("@edgestore/shared");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, 'EdgeStoreApiClientError', {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () {
|
|
8
|
+
return _edgestore_shared.EdgeStoreApiClientError;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
exports.UploadAbortedError = require_uploadAbortedError.UploadAbortedError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EdgeStoreApiClientError } from "@edgestore/shared";
|
|
2
|
+
|
|
3
|
+
//#region src/libs/errors/uploadAbortedError.d.ts
|
|
4
|
+
declare class UploadAbortedError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { EdgeStoreApiClientError, UploadAbortedError };
|
|
9
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/libs/errors/uploadAbortedError.ts"],"sourcesContent":[],"mappings":";;;cAAa,kBAAA,SAA2B,KAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EdgeStoreApiClientError } from "@edgestore/shared";
|
|
2
|
+
|
|
3
|
+
//#region src/libs/errors/uploadAbortedError.d.ts
|
|
4
|
+
declare class UploadAbortedError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { EdgeStoreApiClientError, UploadAbortedError };
|
|
9
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/libs/errors/uploadAbortedError.ts"],"sourcesContent":[],"mappings":";;;cAAa,kBAAA,SAA2B,KAAA"}
|
package/dist/errors/index.mjs
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { t as UploadAbortedError } from "../uploadAbortedError-1Hka0_24.mjs";
|
|
2
|
+
import { EdgeStoreApiClientError } from "@edgestore/shared";
|
|
3
|
+
|
|
4
|
+
export { EdgeStoreApiClientError, UploadAbortedError };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
const require_uploadAbortedError = require('./uploadAbortedError-CyhLukhg.cjs');
|
|
29
|
+
let react = require("react");
|
|
30
|
+
react = __toESM(react);
|
|
31
|
+
let _edgestore_shared = require("@edgestore/shared");
|
|
32
|
+
|
|
33
|
+
//#region src/libs/errors/EdgeStoreClientError.ts
|
|
34
|
+
var EdgeStoreClientError = class extends Error {
|
|
35
|
+
constructor(message) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.name = "EdgeStoreError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var EdgeStoreClientError_default = EdgeStoreClientError;
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/libs/errors/handleError.ts
|
|
44
|
+
async function handleError(res) {
|
|
45
|
+
let json = {};
|
|
46
|
+
try {
|
|
47
|
+
json = await res.json();
|
|
48
|
+
} catch (err) {
|
|
49
|
+
throw new EdgeStoreClientError_default(`Failed to parse response. Make sure the api is correctly configured at ${res.url}`);
|
|
50
|
+
}
|
|
51
|
+
throw new _edgestore_shared.EdgeStoreApiClientError({ response: json });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/createNextProxy.ts
|
|
56
|
+
function createNextProxy({ apiPath, uploadingCountRef, maxConcurrentUploads = 5, disableDevProxy }) {
|
|
57
|
+
return new Proxy({}, { get(_, prop) {
|
|
58
|
+
const bucketName = prop;
|
|
59
|
+
return {
|
|
60
|
+
upload: async (params) => {
|
|
61
|
+
try {
|
|
62
|
+
var _params$onProgressCha;
|
|
63
|
+
(_params$onProgressCha = params.onProgressChange) === null || _params$onProgressCha === void 0 || _params$onProgressCha.call(params, 0);
|
|
64
|
+
const abortPromise = new Promise((resolve) => {
|
|
65
|
+
var _params$signal;
|
|
66
|
+
(_params$signal = params.signal) === null || _params$signal === void 0 || _params$signal.addEventListener("abort", () => {
|
|
67
|
+
resolve();
|
|
68
|
+
}, { once: true });
|
|
69
|
+
});
|
|
70
|
+
while (uploadingCountRef.current >= maxConcurrentUploads && uploadingCountRef.current > 0) {
|
|
71
|
+
var _params$signal2;
|
|
72
|
+
await Promise.race([new Promise((resolve) => setTimeout(resolve, 300)), abortPromise]);
|
|
73
|
+
if ((_params$signal2 = params.signal) === null || _params$signal2 === void 0 ? void 0 : _params$signal2.aborted) throw new require_uploadAbortedError.UploadAbortedError("File upload aborted");
|
|
74
|
+
}
|
|
75
|
+
uploadingCountRef.current++;
|
|
76
|
+
return await uploadFile(params, {
|
|
77
|
+
bucketName,
|
|
78
|
+
apiPath
|
|
79
|
+
}, disableDevProxy);
|
|
80
|
+
} finally {
|
|
81
|
+
uploadingCountRef.current--;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
confirmUpload: async (params) => {
|
|
85
|
+
const { success } = await confirmUpload(params, {
|
|
86
|
+
bucketName,
|
|
87
|
+
apiPath
|
|
88
|
+
});
|
|
89
|
+
if (!success) throw new EdgeStoreClientError_default("Failed to confirm upload");
|
|
90
|
+
},
|
|
91
|
+
delete: async (params) => {
|
|
92
|
+
const { success } = await deleteFile(params, {
|
|
93
|
+
bucketName,
|
|
94
|
+
apiPath
|
|
95
|
+
});
|
|
96
|
+
if (!success) throw new EdgeStoreClientError_default("Failed to delete file");
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
} });
|
|
100
|
+
}
|
|
101
|
+
async function uploadFile({ file, signal, input, onProgressChange, options }, { apiPath, bucketName }, disableDevProxy) {
|
|
102
|
+
try {
|
|
103
|
+
onProgressChange === null || onProgressChange === void 0 || onProgressChange(0);
|
|
104
|
+
const res = await fetch(`${apiPath}/request-upload`, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
credentials: "include",
|
|
107
|
+
signal,
|
|
108
|
+
body: JSON.stringify({
|
|
109
|
+
bucketName,
|
|
110
|
+
input,
|
|
111
|
+
fileInfo: {
|
|
112
|
+
extension: file.name.split(".").pop(),
|
|
113
|
+
type: file.type,
|
|
114
|
+
size: file.size,
|
|
115
|
+
fileName: options === null || options === void 0 ? void 0 : options.manualFileName,
|
|
116
|
+
replaceTargetUrl: options === null || options === void 0 ? void 0 : options.replaceTargetUrl,
|
|
117
|
+
temporary: options === null || options === void 0 ? void 0 : options.temporary
|
|
118
|
+
}
|
|
119
|
+
}),
|
|
120
|
+
headers: { "Content-Type": "application/json" }
|
|
121
|
+
});
|
|
122
|
+
if (!res.ok) await handleError(res);
|
|
123
|
+
const json = await res.json();
|
|
124
|
+
if ("multipart" in json) await multipartUpload({
|
|
125
|
+
bucketName,
|
|
126
|
+
multipartInfo: json.multipart,
|
|
127
|
+
onProgressChange,
|
|
128
|
+
signal,
|
|
129
|
+
file,
|
|
130
|
+
apiPath
|
|
131
|
+
});
|
|
132
|
+
else if ("uploadUrl" in json) await uploadFileInner({
|
|
133
|
+
file,
|
|
134
|
+
uploadUrl: json.uploadUrl,
|
|
135
|
+
onProgressChange,
|
|
136
|
+
signal
|
|
137
|
+
});
|
|
138
|
+
else throw new EdgeStoreClientError_default("An error occurred");
|
|
139
|
+
return {
|
|
140
|
+
url: getUrl(json.accessUrl, apiPath, disableDevProxy),
|
|
141
|
+
thumbnailUrl: json.thumbnailUrl ? getUrl(json.thumbnailUrl, apiPath, disableDevProxy) : null,
|
|
142
|
+
size: json.size,
|
|
143
|
+
uploadedAt: new Date(json.uploadedAt),
|
|
144
|
+
path: json.path,
|
|
145
|
+
pathOrder: json.pathOrder,
|
|
146
|
+
metadata: json.metadata
|
|
147
|
+
};
|
|
148
|
+
} catch (e) {
|
|
149
|
+
if (e instanceof Error && e.name === "AbortError") throw new require_uploadAbortedError.UploadAbortedError("File upload aborted");
|
|
150
|
+
onProgressChange === null || onProgressChange === void 0 || onProgressChange(0);
|
|
151
|
+
throw e;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Protected files need third-party cookies to work.
|
|
156
|
+
* Since third party cookies don't work on localhost,
|
|
157
|
+
* we need to proxy the file through the server.
|
|
158
|
+
*/
|
|
159
|
+
function getUrl(url, apiPath, disableDevProxy) {
|
|
160
|
+
var _import$meta$env$1;
|
|
161
|
+
if ((typeof process !== "undefined" ? process.env.NODE_ENV : ((_import$meta$env$1 = {}.env) === null || _import$meta$env$1 === void 0 ? void 0 : _import$meta$env$1.DEV) ? "development" : "production") === "development" && !url.includes("/_public/") && !disableDevProxy) {
|
|
162
|
+
const proxyUrl = new URL(window.location.origin);
|
|
163
|
+
proxyUrl.pathname = `${apiPath}/proxy-file`;
|
|
164
|
+
proxyUrl.search = new URLSearchParams({ url }).toString();
|
|
165
|
+
return proxyUrl.toString();
|
|
166
|
+
}
|
|
167
|
+
return url;
|
|
168
|
+
}
|
|
169
|
+
async function uploadFileInner(props) {
|
|
170
|
+
const { file, uploadUrl, onProgressChange, signal } = props;
|
|
171
|
+
return new Promise((resolve, reject) => {
|
|
172
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
173
|
+
reject(new require_uploadAbortedError.UploadAbortedError("File upload aborted"));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const request = new XMLHttpRequest();
|
|
177
|
+
request.open("PUT", uploadUrl);
|
|
178
|
+
request.setRequestHeader("x-ms-blob-type", "BlockBlob");
|
|
179
|
+
request.addEventListener("loadstart", () => {
|
|
180
|
+
onProgressChange === null || onProgressChange === void 0 || onProgressChange(0);
|
|
181
|
+
});
|
|
182
|
+
request.upload.addEventListener("progress", (e) => {
|
|
183
|
+
if (e.lengthComputable) {
|
|
184
|
+
const progress = Math.round(e.loaded / e.total * 1e4) / 100;
|
|
185
|
+
onProgressChange === null || onProgressChange === void 0 || onProgressChange(progress);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
request.addEventListener("error", () => {
|
|
189
|
+
reject(/* @__PURE__ */ new Error("Error uploading file"));
|
|
190
|
+
});
|
|
191
|
+
request.addEventListener("abort", () => {
|
|
192
|
+
reject(new require_uploadAbortedError.UploadAbortedError("File upload aborted"));
|
|
193
|
+
});
|
|
194
|
+
request.addEventListener("loadend", () => {
|
|
195
|
+
resolve(request.getResponseHeader("ETag"));
|
|
196
|
+
});
|
|
197
|
+
if (signal) signal.addEventListener("abort", () => {
|
|
198
|
+
request.abort();
|
|
199
|
+
});
|
|
200
|
+
request.send(file);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
async function multipartUpload(params) {
|
|
204
|
+
const { bucketName, multipartInfo, onProgressChange, file, signal, apiPath } = params;
|
|
205
|
+
const { partSize, parts, totalParts, uploadId, key } = multipartInfo;
|
|
206
|
+
const uploadingParts = [];
|
|
207
|
+
const uploadPart = async (params$1) => {
|
|
208
|
+
const { part, chunk } = params$1;
|
|
209
|
+
const { uploadUrl } = part;
|
|
210
|
+
const eTag = await uploadFileInner({
|
|
211
|
+
file: chunk,
|
|
212
|
+
uploadUrl,
|
|
213
|
+
signal,
|
|
214
|
+
onProgressChange: (progress) => {
|
|
215
|
+
const uploadingPart = uploadingParts.find((p) => p.partNumber === part.partNumber);
|
|
216
|
+
if (uploadingPart) uploadingPart.progress = progress;
|
|
217
|
+
else uploadingParts.push({
|
|
218
|
+
partNumber: part.partNumber,
|
|
219
|
+
progress
|
|
220
|
+
});
|
|
221
|
+
const totalProgress = Math.round(uploadingParts.reduce((acc, p) => acc + p.progress * 100, 0) / totalParts) / 100;
|
|
222
|
+
onProgressChange === null || onProgressChange === void 0 || onProgressChange(totalProgress);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
if (!eTag) throw new EdgeStoreClientError_default("Could not get ETag from multipart response");
|
|
226
|
+
return {
|
|
227
|
+
partNumber: part.partNumber,
|
|
228
|
+
eTag
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
const completedParts = await queuedPromises({
|
|
232
|
+
items: parts.map((part) => ({
|
|
233
|
+
part,
|
|
234
|
+
chunk: file.slice((part.partNumber - 1) * partSize, part.partNumber * partSize)
|
|
235
|
+
})),
|
|
236
|
+
fn: uploadPart,
|
|
237
|
+
maxParallel: 5,
|
|
238
|
+
maxRetries: 10
|
|
239
|
+
});
|
|
240
|
+
const res = await fetch(`${apiPath}/complete-multipart-upload`, {
|
|
241
|
+
method: "POST",
|
|
242
|
+
credentials: "include",
|
|
243
|
+
body: JSON.stringify({
|
|
244
|
+
bucketName,
|
|
245
|
+
uploadId,
|
|
246
|
+
key,
|
|
247
|
+
parts: completedParts
|
|
248
|
+
}),
|
|
249
|
+
headers: { "Content-Type": "application/json" }
|
|
250
|
+
});
|
|
251
|
+
if (!res.ok) await handleError(res);
|
|
252
|
+
}
|
|
253
|
+
async function confirmUpload({ url }, { apiPath, bucketName }) {
|
|
254
|
+
const res = await fetch(`${apiPath}/confirm-upload`, {
|
|
255
|
+
method: "POST",
|
|
256
|
+
credentials: "include",
|
|
257
|
+
body: JSON.stringify({
|
|
258
|
+
url,
|
|
259
|
+
bucketName
|
|
260
|
+
}),
|
|
261
|
+
headers: { "Content-Type": "application/json" }
|
|
262
|
+
});
|
|
263
|
+
if (!res.ok) await handleError(res);
|
|
264
|
+
return res.json();
|
|
265
|
+
}
|
|
266
|
+
async function deleteFile({ url }, { apiPath, bucketName }) {
|
|
267
|
+
const res = await fetch(`${apiPath}/delete-file`, {
|
|
268
|
+
method: "POST",
|
|
269
|
+
credentials: "include",
|
|
270
|
+
body: JSON.stringify({
|
|
271
|
+
url,
|
|
272
|
+
bucketName
|
|
273
|
+
}),
|
|
274
|
+
headers: { "Content-Type": "application/json" }
|
|
275
|
+
});
|
|
276
|
+
if (!res.ok) await handleError(res);
|
|
277
|
+
return res.json();
|
|
278
|
+
}
|
|
279
|
+
async function queuedPromises({ items, fn, maxParallel, maxRetries = 0 }) {
|
|
280
|
+
const results = new Array(items.length);
|
|
281
|
+
const executeWithRetry = async (func, retries) => {
|
|
282
|
+
try {
|
|
283
|
+
return await func();
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (error instanceof require_uploadAbortedError.UploadAbortedError) throw error;
|
|
286
|
+
if (retries > 0) {
|
|
287
|
+
await new Promise((resolve) => setTimeout(resolve, 5e3));
|
|
288
|
+
return executeWithRetry(func, retries - 1);
|
|
289
|
+
} else throw error;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
const semaphore = {
|
|
293
|
+
count: maxParallel,
|
|
294
|
+
async wait() {
|
|
295
|
+
var _this = this;
|
|
296
|
+
while (_this.count <= 0) await new Promise((resolve) => setTimeout(resolve, 500));
|
|
297
|
+
_this.count--;
|
|
298
|
+
},
|
|
299
|
+
signal() {
|
|
300
|
+
this.count++;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
const tasks = items.map((item, i) => (async () => {
|
|
304
|
+
await semaphore.wait();
|
|
305
|
+
try {
|
|
306
|
+
results[i] = await executeWithRetry(() => fn(item), maxRetries);
|
|
307
|
+
} finally {
|
|
308
|
+
semaphore.signal();
|
|
309
|
+
}
|
|
310
|
+
})());
|
|
311
|
+
await Promise.all(tasks);
|
|
312
|
+
return results;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/contextProvider.tsx
|
|
317
|
+
var _ref, _import$meta$env;
|
|
318
|
+
const DEFAULT_BASE_URL = (_ref = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_EDGE_STORE_BASE_URL : (_import$meta$env = {}.env) === null || _import$meta$env === void 0 ? void 0 : _import$meta$env.EDGE_STORE_BASE_URL) !== null && _ref !== void 0 ? _ref : "https://files.edgestore.dev";
|
|
319
|
+
function createEdgeStoreProvider(opts) {
|
|
320
|
+
const EdgeStoreContext = react.createContext(void 0);
|
|
321
|
+
const EdgeStoreProvider = ({ children, basePath }) => {
|
|
322
|
+
return EdgeStoreProviderInner({
|
|
323
|
+
children,
|
|
324
|
+
context: EdgeStoreContext,
|
|
325
|
+
basePath,
|
|
326
|
+
maxConcurrentUploads: opts === null || opts === void 0 ? void 0 : opts.maxConcurrentUploads,
|
|
327
|
+
disableDevProxy: opts === null || opts === void 0 ? void 0 : opts.disableDevProxy
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
function useEdgeStore() {
|
|
331
|
+
if (!EdgeStoreContext) throw new Error("React Context is unavailable in Server Components");
|
|
332
|
+
const value = react.useContext(EdgeStoreContext);
|
|
333
|
+
if (!value && process.env.NODE_ENV !== "production") throw new Error("[edgestore]: `useEdgeStore` must be wrapped in a <EdgeStoreProvider />");
|
|
334
|
+
return value;
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
EdgeStoreProvider,
|
|
338
|
+
useEdgeStore
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
function EdgeStoreProviderInner({ children, context, basePath, maxConcurrentUploads, disableDevProxy }) {
|
|
342
|
+
const apiPath = basePath ? `${basePath}` : "/api/edgestore";
|
|
343
|
+
const [state, setState] = react.useState({
|
|
344
|
+
loading: true,
|
|
345
|
+
initialized: false,
|
|
346
|
+
error: false
|
|
347
|
+
});
|
|
348
|
+
const uploadingCountRef = react.useRef(0);
|
|
349
|
+
const initExecuted = react.useRef(false);
|
|
350
|
+
react.useEffect(() => {
|
|
351
|
+
if (!initExecuted.current) init();
|
|
352
|
+
return () => {
|
|
353
|
+
initExecuted.current = true;
|
|
354
|
+
};
|
|
355
|
+
}, []);
|
|
356
|
+
async function init() {
|
|
357
|
+
try {
|
|
358
|
+
setState({
|
|
359
|
+
loading: true,
|
|
360
|
+
initialized: false,
|
|
361
|
+
error: false
|
|
362
|
+
});
|
|
363
|
+
const res = await fetch(`${apiPath}/init`, {
|
|
364
|
+
method: "POST",
|
|
365
|
+
credentials: "include"
|
|
366
|
+
});
|
|
367
|
+
if (res.ok) {
|
|
368
|
+
const json = await res.json();
|
|
369
|
+
if (json.providerName === "edgestore") if ((await fetch(`${DEFAULT_BASE_URL}/_init`, {
|
|
370
|
+
method: "GET",
|
|
371
|
+
credentials: "include",
|
|
372
|
+
headers: { "x-edgestore-token": json.token }
|
|
373
|
+
})).ok) setState({
|
|
374
|
+
loading: false,
|
|
375
|
+
initialized: true,
|
|
376
|
+
error: false
|
|
377
|
+
});
|
|
378
|
+
else {
|
|
379
|
+
setState({
|
|
380
|
+
loading: false,
|
|
381
|
+
initialized: false,
|
|
382
|
+
error: true
|
|
383
|
+
});
|
|
384
|
+
throw new EdgeStoreClientError_default("Couldn't initialize EdgeStore.");
|
|
385
|
+
}
|
|
386
|
+
else setState({
|
|
387
|
+
loading: false,
|
|
388
|
+
initialized: true,
|
|
389
|
+
error: false
|
|
390
|
+
});
|
|
391
|
+
} else {
|
|
392
|
+
setState({
|
|
393
|
+
loading: false,
|
|
394
|
+
initialized: false,
|
|
395
|
+
error: true
|
|
396
|
+
});
|
|
397
|
+
await handleError(res);
|
|
398
|
+
}
|
|
399
|
+
} catch (err) {
|
|
400
|
+
setState({
|
|
401
|
+
loading: false,
|
|
402
|
+
initialized: false,
|
|
403
|
+
error: true
|
|
404
|
+
});
|
|
405
|
+
throw err;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
async function reset() {
|
|
409
|
+
await init();
|
|
410
|
+
}
|
|
411
|
+
return /* @__PURE__ */ react.createElement(react.Fragment, null, /* @__PURE__ */ react.createElement(context.Provider, { value: {
|
|
412
|
+
edgestore: createNextProxy({
|
|
413
|
+
apiPath,
|
|
414
|
+
uploadingCountRef,
|
|
415
|
+
maxConcurrentUploads,
|
|
416
|
+
disableDevProxy
|
|
417
|
+
}),
|
|
418
|
+
reset,
|
|
419
|
+
state
|
|
420
|
+
} }, children));
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
//#endregion
|
|
424
|
+
exports.__toESM = __toESM;
|
|
425
|
+
exports.createEdgeStoreProvider = createEdgeStoreProvider;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { AnyRouter, InferBucketPathObject, InferMetadataObject, UploadOptions } from "@edgestore/shared";
|
|
2
|
+
import * as React$1 from "react";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/createNextProxy.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
* @see https://www.totaltypescript.com/concepts/the-prettify-helper
|
|
10
|
+
*/
|
|
11
|
+
type Prettify<TType> = { [K in keyof TType]: TType[K] } & {};
|
|
12
|
+
type BucketFunctions<TRouter extends AnyRouter> = { [K in keyof TRouter['buckets']]: {
|
|
13
|
+
/**
|
|
14
|
+
* Upload a file to the bucket
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* await edgestore.myBucket.upload({
|
|
18
|
+
* file: file,
|
|
19
|
+
* signal: abortController.signal, // if you want to be able to cancel the ongoing upload
|
|
20
|
+
* onProgressChange: (progress) => { console.log(progress) }, // if you want to show the progress of the upload
|
|
21
|
+
* input: {...} // if the bucket has an input schema
|
|
22
|
+
* options: {
|
|
23
|
+
* manualFileName: file.name, // if you want to use a custom file name
|
|
24
|
+
* replaceTargetUrl: url, // if you want to replace an existing file
|
|
25
|
+
* temporary: true, // if you want to delete the file after 24 hours
|
|
26
|
+
* }
|
|
27
|
+
* })
|
|
28
|
+
*/
|
|
29
|
+
upload: (params: z.infer<TRouter['buckets'][K]['_def']['input']> extends never ? {
|
|
30
|
+
file: File;
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
onProgressChange?: OnProgressChangeHandler;
|
|
33
|
+
options?: UploadOptions;
|
|
34
|
+
} : {
|
|
35
|
+
file: File;
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
input: z.infer<TRouter['buckets'][K]['_def']['input']>;
|
|
38
|
+
onProgressChange?: OnProgressChangeHandler;
|
|
39
|
+
options?: UploadOptions;
|
|
40
|
+
}) => Promise<TRouter['buckets'][K]['_def']['type'] extends 'IMAGE' ? {
|
|
41
|
+
url: string;
|
|
42
|
+
thumbnailUrl: string | null;
|
|
43
|
+
size: number;
|
|
44
|
+
uploadedAt: Date;
|
|
45
|
+
metadata: InferMetadataObject<TRouter['buckets'][K]>;
|
|
46
|
+
path: InferBucketPathObject<TRouter['buckets'][K]>;
|
|
47
|
+
pathOrder: Prettify<keyof InferBucketPathObject<TRouter['buckets'][K]>>[];
|
|
48
|
+
} : {
|
|
49
|
+
url: string;
|
|
50
|
+
size: number;
|
|
51
|
+
uploadedAt: Date;
|
|
52
|
+
metadata: InferMetadataObject<TRouter['buckets'][K]>;
|
|
53
|
+
path: InferBucketPathObject<TRouter['buckets'][K]>;
|
|
54
|
+
pathOrder: Prettify<keyof InferBucketPathObject<TRouter['buckets'][K]>>[];
|
|
55
|
+
}>;
|
|
56
|
+
confirmUpload: (params: {
|
|
57
|
+
url: string;
|
|
58
|
+
}) => Promise<void>;
|
|
59
|
+
delete: (params: {
|
|
60
|
+
url: string;
|
|
61
|
+
}) => Promise<void>;
|
|
62
|
+
} };
|
|
63
|
+
type OnProgressChangeHandler = (progress: number) => void;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/contextProvider.d.ts
|
|
66
|
+
type EdgeStoreContextValue<TRouter extends AnyRouter> = {
|
|
67
|
+
edgestore: BucketFunctions<TRouter>;
|
|
68
|
+
/**
|
|
69
|
+
* This will re-run the EdgeStore initialization process,
|
|
70
|
+
* which will run the `createContext` function again.
|
|
71
|
+
*
|
|
72
|
+
* Can be used after a sign-in or sign-out, for example.
|
|
73
|
+
*/
|
|
74
|
+
reset: () => Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* The current state of the EdgeStore provider.
|
|
77
|
+
*
|
|
78
|
+
* You can use this to wait for the provider to be initialized
|
|
79
|
+
* before trying to show private images on your app.
|
|
80
|
+
*/
|
|
81
|
+
state: ProviderState;
|
|
82
|
+
};
|
|
83
|
+
declare function createEdgeStoreProvider<TRouter extends AnyRouter>(opts?: {
|
|
84
|
+
/**
|
|
85
|
+
* The maximum number of concurrent uploads.
|
|
86
|
+
*
|
|
87
|
+
* Uploads will automatically be queued if this limit is reached.
|
|
88
|
+
*
|
|
89
|
+
* @default 5
|
|
90
|
+
*/
|
|
91
|
+
maxConcurrentUploads?: number;
|
|
92
|
+
/**
|
|
93
|
+
* Accessing EdgeStore protected files in development mode requires a proxy.
|
|
94
|
+
* You might want to disable this for other providers if you are overwriting the path.
|
|
95
|
+
*
|
|
96
|
+
* @default false
|
|
97
|
+
*/
|
|
98
|
+
disableDevProxy?: boolean;
|
|
99
|
+
}): {
|
|
100
|
+
EdgeStoreProvider: ({
|
|
101
|
+
children,
|
|
102
|
+
basePath
|
|
103
|
+
}: {
|
|
104
|
+
children: React$1.ReactNode;
|
|
105
|
+
/**
|
|
106
|
+
* In case your app is not hosted at the root of your domain, you can specify the base path here.
|
|
107
|
+
* If you set this, make sure to set the full path to the EdgeStore API.
|
|
108
|
+
* e.g. `/my-app/api/edgestore` or `https://example.com/my-app/api/edgestore`
|
|
109
|
+
*
|
|
110
|
+
* @example - If your app is hosted at `https://example.com/my-app`, you can set the `basePath` to `/my-app/api/edgestore`.
|
|
111
|
+
*/
|
|
112
|
+
basePath?: string;
|
|
113
|
+
}) => React$1.JSX.Element;
|
|
114
|
+
useEdgeStore: () => EdgeStoreContextValue<TRouter>;
|
|
115
|
+
};
|
|
116
|
+
type ProviderState = {
|
|
117
|
+
loading: true;
|
|
118
|
+
initialized: false;
|
|
119
|
+
error: false;
|
|
120
|
+
} | {
|
|
121
|
+
loading: false;
|
|
122
|
+
initialized: false;
|
|
123
|
+
error: true;
|
|
124
|
+
} | {
|
|
125
|
+
loading: false;
|
|
126
|
+
initialized: true;
|
|
127
|
+
error: false;
|
|
128
|
+
};
|
|
129
|
+
//#endregion
|
|
130
|
+
export { createEdgeStoreProvider };
|
|
131
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/createNextProxy.ts","../src/contextProvider.tsx"],"sourcesContent":[],"mappings":"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"}
|