@edgestore/react 0.1.5-alpha.7 → 0.1.5-alpha.9
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/contextProvider.d.ts +20 -0
- package/dist/contextProvider.d.ts.map +1 -1
- package/dist/createNextProxy.d.ts.map +1 -1
- package/dist/index.js +75 -24
- package/dist/index.mjs +75 -24
- package/dist/libs/errors/{EdgeStoreError.d.ts → EdgeStoreClientError.d.ts} +1 -1
- package/dist/libs/errors/EdgeStoreClientError.d.ts.map +1 -0
- package/dist/libs/errors/handleError.d.ts +2 -0
- package/dist/libs/errors/handleError.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/contextProvider.tsx +87 -12
- package/src/createNextProxy.ts +6 -14
- package/src/libs/errors/handleError.ts +13 -0
- package/dist/libs/errors/EdgeStoreError.d.ts.map +0 -1
- /package/src/libs/errors/{EdgeStoreError.ts → EdgeStoreClientError.ts} +0 -0
|
@@ -10,6 +10,13 @@ type EdgeStoreContextValue<TRouter extends AnyRouter> = {
|
|
|
10
10
|
* Can be used after a sign-in or sign-out, for example.
|
|
11
11
|
*/
|
|
12
12
|
reset: () => Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* The current state of the Edge Store provider.
|
|
15
|
+
*
|
|
16
|
+
* You can use this to wait for the provider to be initialized
|
|
17
|
+
* before trying to show private images on your app.
|
|
18
|
+
*/
|
|
19
|
+
state: ProviderState;
|
|
13
20
|
};
|
|
14
21
|
export declare function createEdgeStoreProvider<TRouter extends AnyRouter>(opts?: {
|
|
15
22
|
/**
|
|
@@ -34,5 +41,18 @@ export declare function createEdgeStoreProvider<TRouter extends AnyRouter>(opts?
|
|
|
34
41
|
}) => React.JSX.Element;
|
|
35
42
|
useEdgeStore: () => EdgeStoreContextValue<TRouter>;
|
|
36
43
|
};
|
|
44
|
+
type ProviderState = {
|
|
45
|
+
loading: true;
|
|
46
|
+
initialized: false;
|
|
47
|
+
error: false;
|
|
48
|
+
} | {
|
|
49
|
+
loading: false;
|
|
50
|
+
initialized: false;
|
|
51
|
+
error: true;
|
|
52
|
+
} | {
|
|
53
|
+
loading: false;
|
|
54
|
+
initialized: true;
|
|
55
|
+
error: false;
|
|
56
|
+
};
|
|
37
57
|
export {};
|
|
38
58
|
//# sourceMappingURL=contextProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contextProvider.d.ts","sourceRoot":"","sources":["../src/contextProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAmB,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"contextProvider.d.ts","sourceRoot":"","sources":["../src/contextProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAmB,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAO1E,KAAK,qBAAqB,CAAC,OAAO,SAAS,SAAS,IAAI;IACtD,SAAS,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC;IACpC;;;;;OAKG;IACH,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B;;;;;OAKG;IACH,KAAK,EAAE,aAAa,CAAC;CACtB,CAAC;AAEF,wBAAgB,uBAAuB,CAAC,OAAO,SAAS,SAAS,EAAE,IAAI,CAAC,EAAE;IACxE;;;;;;OAMG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;;kBASa,MAAM,SAAS;QACzB;;;;;;WAMG;;;;EAgCN;AAED,KAAK,aAAa,GACd;IACE,OAAO,EAAE,IAAI,CAAC;IACd,WAAW,EAAE,KAAK,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC;CACd,GACD;IACE,OAAO,EAAE,KAAK,CAAC;IACf,WAAW,EAAE,KAAK,CAAC;IACnB,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,OAAO,EAAE,KAAK,CAAC;IACf,WAAW,EAAE,IAAI,CAAC;IAClB,KAAK,EAAE,KAAK,CAAC;CACd,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createNextProxy.d.ts","sourceRoot":"","sources":["../src/createNextProxy.ts"],"names":[],"mappings":";AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"createNextProxy.d.ts","sourceRoot":"","sources":["../src/createNextProxy.ts"],"names":[],"mappings":"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"}
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,17 @@ class EdgeStoreClientError extends Error {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
async function handleError(res) {
|
|
36
|
+
try {
|
|
37
|
+
const json = await res.json();
|
|
38
|
+
throw new core.EdgeStoreApiClientError({
|
|
39
|
+
response: json
|
|
40
|
+
});
|
|
41
|
+
} catch (err) {
|
|
42
|
+
throw new EdgeStoreClientError(`Failed to parse response. Make sure the api is correctly configured at ${res.url}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
35
46
|
function createNextProxy({ apiPath, uploadingCountRef, maxConcurrentUploads = 5 }) {
|
|
36
47
|
return new Proxy({}, {
|
|
37
48
|
get (_, prop) {
|
|
@@ -97,9 +108,7 @@ async function uploadFile({ file, input, onProgressChange, options }, { apiPath,
|
|
|
97
108
|
}
|
|
98
109
|
});
|
|
99
110
|
if (!res.ok) {
|
|
100
|
-
|
|
101
|
-
response: await res.json()
|
|
102
|
-
});
|
|
111
|
+
await handleError(res);
|
|
103
112
|
}
|
|
104
113
|
const json = await res.json();
|
|
105
114
|
if ('multipart' in json) {
|
|
@@ -228,9 +237,7 @@ async function multipartUpload(params) {
|
|
|
228
237
|
}
|
|
229
238
|
});
|
|
230
239
|
if (!res.ok) {
|
|
231
|
-
|
|
232
|
-
response: await res.json()
|
|
233
|
-
});
|
|
240
|
+
await handleError(res);
|
|
234
241
|
}
|
|
235
242
|
}
|
|
236
243
|
async function confirmUpload({ url }, { apiPath, bucketName }) {
|
|
@@ -245,9 +252,7 @@ async function confirmUpload({ url }, { apiPath, bucketName }) {
|
|
|
245
252
|
}
|
|
246
253
|
});
|
|
247
254
|
if (!res.ok) {
|
|
248
|
-
|
|
249
|
-
response: await res.json()
|
|
250
|
-
});
|
|
255
|
+
await handleError(res);
|
|
251
256
|
}
|
|
252
257
|
return res.json();
|
|
253
258
|
}
|
|
@@ -263,9 +268,7 @@ async function deleteFile({ url }, { apiPath, bucketName }) {
|
|
|
263
268
|
}
|
|
264
269
|
});
|
|
265
270
|
if (!res.ok) {
|
|
266
|
-
|
|
267
|
-
response: await res.json()
|
|
268
|
-
});
|
|
271
|
+
await handleError(res);
|
|
269
272
|
}
|
|
270
273
|
return res.json();
|
|
271
274
|
}
|
|
@@ -336,24 +339,71 @@ function createEdgeStoreProvider(opts) {
|
|
|
336
339
|
}
|
|
337
340
|
function EdgeStoreProviderInner({ children, context, basePath, maxConcurrentUploads }) {
|
|
338
341
|
const apiPath = basePath ? `${basePath}` : '/api/edgestore';
|
|
342
|
+
const [state, setState] = React__namespace.useState({
|
|
343
|
+
loading: true,
|
|
344
|
+
initialized: false,
|
|
345
|
+
error: false
|
|
346
|
+
});
|
|
339
347
|
const uploadingCountRef = React__namespace.useRef(0);
|
|
348
|
+
const initExecuted = React__namespace.useRef(false); // to make sure we don't run init twice
|
|
340
349
|
React__namespace.useEffect(()=>{
|
|
341
|
-
|
|
350
|
+
if (!initExecuted.current) {
|
|
351
|
+
void init();
|
|
352
|
+
}
|
|
353
|
+
return ()=>{
|
|
354
|
+
initExecuted.current = true;
|
|
355
|
+
};
|
|
342
356
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
343
357
|
}, []);
|
|
344
358
|
async function init() {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
359
|
+
try {
|
|
360
|
+
setState({
|
|
361
|
+
loading: true,
|
|
362
|
+
initialized: false,
|
|
363
|
+
error: false
|
|
364
|
+
});
|
|
365
|
+
const res = await fetch(`${apiPath}/init`, {
|
|
366
|
+
method: 'POST'
|
|
367
|
+
});
|
|
368
|
+
if (res.ok) {
|
|
369
|
+
const json = await res.json();
|
|
370
|
+
const innerRes = await fetch(`${DEFAULT_BASE_URL}/_init`, {
|
|
371
|
+
method: 'GET',
|
|
372
|
+
credentials: 'include',
|
|
373
|
+
headers: {
|
|
374
|
+
'x-edgestore-token': json.token
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
if (innerRes.ok) {
|
|
378
|
+
// update state
|
|
379
|
+
setState({
|
|
380
|
+
loading: false,
|
|
381
|
+
initialized: true,
|
|
382
|
+
error: false
|
|
383
|
+
});
|
|
384
|
+
} else {
|
|
385
|
+
setState({
|
|
386
|
+
loading: false,
|
|
387
|
+
initialized: false,
|
|
388
|
+
error: true
|
|
389
|
+
});
|
|
390
|
+
throw new EdgeStoreClientError("Couldn't initialize Edge Store.");
|
|
355
391
|
}
|
|
392
|
+
} else {
|
|
393
|
+
setState({
|
|
394
|
+
loading: false,
|
|
395
|
+
initialized: false,
|
|
396
|
+
error: true
|
|
397
|
+
});
|
|
398
|
+
await handleError(res);
|
|
399
|
+
}
|
|
400
|
+
} catch (err) {
|
|
401
|
+
setState({
|
|
402
|
+
loading: false,
|
|
403
|
+
initialized: false,
|
|
404
|
+
error: true
|
|
356
405
|
});
|
|
406
|
+
throw err;
|
|
357
407
|
}
|
|
358
408
|
}
|
|
359
409
|
async function reset() {
|
|
@@ -366,7 +416,8 @@ function EdgeStoreProviderInner({ children, context, basePath, maxConcurrentUplo
|
|
|
366
416
|
uploadingCountRef,
|
|
367
417
|
maxConcurrentUploads
|
|
368
418
|
}),
|
|
369
|
-
reset
|
|
419
|
+
reset,
|
|
420
|
+
state
|
|
370
421
|
}
|
|
371
422
|
}, children));
|
|
372
423
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -8,6 +8,17 @@ class EdgeStoreClientError extends Error {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
async function handleError(res) {
|
|
12
|
+
try {
|
|
13
|
+
const json = await res.json();
|
|
14
|
+
throw new EdgeStoreApiClientError({
|
|
15
|
+
response: json
|
|
16
|
+
});
|
|
17
|
+
} catch (err) {
|
|
18
|
+
throw new EdgeStoreClientError(`Failed to parse response. Make sure the api is correctly configured at ${res.url}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
11
22
|
function createNextProxy({ apiPath, uploadingCountRef, maxConcurrentUploads = 5 }) {
|
|
12
23
|
return new Proxy({}, {
|
|
13
24
|
get (_, prop) {
|
|
@@ -73,9 +84,7 @@ async function uploadFile({ file, input, onProgressChange, options }, { apiPath,
|
|
|
73
84
|
}
|
|
74
85
|
});
|
|
75
86
|
if (!res.ok) {
|
|
76
|
-
|
|
77
|
-
response: await res.json()
|
|
78
|
-
});
|
|
87
|
+
await handleError(res);
|
|
79
88
|
}
|
|
80
89
|
const json = await res.json();
|
|
81
90
|
if ('multipart' in json) {
|
|
@@ -204,9 +213,7 @@ async function multipartUpload(params) {
|
|
|
204
213
|
}
|
|
205
214
|
});
|
|
206
215
|
if (!res.ok) {
|
|
207
|
-
|
|
208
|
-
response: await res.json()
|
|
209
|
-
});
|
|
216
|
+
await handleError(res);
|
|
210
217
|
}
|
|
211
218
|
}
|
|
212
219
|
async function confirmUpload({ url }, { apiPath, bucketName }) {
|
|
@@ -221,9 +228,7 @@ async function confirmUpload({ url }, { apiPath, bucketName }) {
|
|
|
221
228
|
}
|
|
222
229
|
});
|
|
223
230
|
if (!res.ok) {
|
|
224
|
-
|
|
225
|
-
response: await res.json()
|
|
226
|
-
});
|
|
231
|
+
await handleError(res);
|
|
227
232
|
}
|
|
228
233
|
return res.json();
|
|
229
234
|
}
|
|
@@ -239,9 +244,7 @@ async function deleteFile({ url }, { apiPath, bucketName }) {
|
|
|
239
244
|
}
|
|
240
245
|
});
|
|
241
246
|
if (!res.ok) {
|
|
242
|
-
|
|
243
|
-
response: await res.json()
|
|
244
|
-
});
|
|
247
|
+
await handleError(res);
|
|
245
248
|
}
|
|
246
249
|
return res.json();
|
|
247
250
|
}
|
|
@@ -312,24 +315,71 @@ function createEdgeStoreProvider(opts) {
|
|
|
312
315
|
}
|
|
313
316
|
function EdgeStoreProviderInner({ children, context, basePath, maxConcurrentUploads }) {
|
|
314
317
|
const apiPath = basePath ? `${basePath}` : '/api/edgestore';
|
|
318
|
+
const [state, setState] = React.useState({
|
|
319
|
+
loading: true,
|
|
320
|
+
initialized: false,
|
|
321
|
+
error: false
|
|
322
|
+
});
|
|
315
323
|
const uploadingCountRef = React.useRef(0);
|
|
324
|
+
const initExecuted = React.useRef(false); // to make sure we don't run init twice
|
|
316
325
|
React.useEffect(()=>{
|
|
317
|
-
|
|
326
|
+
if (!initExecuted.current) {
|
|
327
|
+
void init();
|
|
328
|
+
}
|
|
329
|
+
return ()=>{
|
|
330
|
+
initExecuted.current = true;
|
|
331
|
+
};
|
|
318
332
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
319
333
|
}, []);
|
|
320
334
|
async function init() {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
335
|
+
try {
|
|
336
|
+
setState({
|
|
337
|
+
loading: true,
|
|
338
|
+
initialized: false,
|
|
339
|
+
error: false
|
|
340
|
+
});
|
|
341
|
+
const res = await fetch(`${apiPath}/init`, {
|
|
342
|
+
method: 'POST'
|
|
343
|
+
});
|
|
344
|
+
if (res.ok) {
|
|
345
|
+
const json = await res.json();
|
|
346
|
+
const innerRes = await fetch(`${DEFAULT_BASE_URL}/_init`, {
|
|
347
|
+
method: 'GET',
|
|
348
|
+
credentials: 'include',
|
|
349
|
+
headers: {
|
|
350
|
+
'x-edgestore-token': json.token
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
if (innerRes.ok) {
|
|
354
|
+
// update state
|
|
355
|
+
setState({
|
|
356
|
+
loading: false,
|
|
357
|
+
initialized: true,
|
|
358
|
+
error: false
|
|
359
|
+
});
|
|
360
|
+
} else {
|
|
361
|
+
setState({
|
|
362
|
+
loading: false,
|
|
363
|
+
initialized: false,
|
|
364
|
+
error: true
|
|
365
|
+
});
|
|
366
|
+
throw new EdgeStoreClientError("Couldn't initialize Edge Store.");
|
|
331
367
|
}
|
|
368
|
+
} else {
|
|
369
|
+
setState({
|
|
370
|
+
loading: false,
|
|
371
|
+
initialized: false,
|
|
372
|
+
error: true
|
|
373
|
+
});
|
|
374
|
+
await handleError(res);
|
|
375
|
+
}
|
|
376
|
+
} catch (err) {
|
|
377
|
+
setState({
|
|
378
|
+
loading: false,
|
|
379
|
+
initialized: false,
|
|
380
|
+
error: true
|
|
332
381
|
});
|
|
382
|
+
throw err;
|
|
333
383
|
}
|
|
334
384
|
}
|
|
335
385
|
async function reset() {
|
|
@@ -342,7 +392,8 @@ function EdgeStoreProviderInner({ children, context, basePath, maxConcurrentUplo
|
|
|
342
392
|
uploadingCountRef,
|
|
343
393
|
maxConcurrentUploads
|
|
344
394
|
}),
|
|
345
|
-
reset
|
|
395
|
+
reset,
|
|
396
|
+
state
|
|
346
397
|
}
|
|
347
398
|
}, children));
|
|
348
399
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EdgeStoreClientError.d.ts","sourceRoot":"","sources":["../../../src/libs/errors/EdgeStoreClientError.ts"],"names":[],"mappings":"AAAA,cAAM,oBAAqB,SAAQ,KAAK;gBAC1B,OAAO,EAAE,MAAM;CAI5B;AAED,eAAe,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../../src/libs/errors/handleError.ts"],"names":[],"mappings":"AAGA,wBAAsB,WAAW,CAAC,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAS/D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edgestore/react",
|
|
3
|
-
"version": "0.1.5-alpha.
|
|
3
|
+
"version": "0.1.5-alpha.9",
|
|
4
4
|
"description": "Upload files with ease from React/Next.js",
|
|
5
5
|
"homepage": "https://edgestore.dev",
|
|
6
6
|
"repository": "https://github.com/edgestorejs/edgestore.git",
|
|
@@ -60,14 +60,14 @@
|
|
|
60
60
|
"uuid": "^9.0.0"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"@edgestore/server": "0.1.5-alpha.
|
|
63
|
+
"@edgestore/server": "0.1.5-alpha.9",
|
|
64
64
|
"next": "*",
|
|
65
65
|
"react": ">=16.8.0",
|
|
66
66
|
"react-dom": ">=16.8.0",
|
|
67
67
|
"zod": ">=3.0.0"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@edgestore/server": "0.1.5-alpha.
|
|
70
|
+
"@edgestore/server": "0.1.5-alpha.9",
|
|
71
71
|
"@types/cookie": "^0.5.1",
|
|
72
72
|
"@types/node": "^18.11.18",
|
|
73
73
|
"@types/uuid": "^9.0.1",
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"typescript": "^5.1.6",
|
|
78
78
|
"zod": "^3.21.4"
|
|
79
79
|
},
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "a2739cac672d5fb29feb5c1c867a96faf5115756"
|
|
81
81
|
}
|
package/src/contextProvider.tsx
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type AnyRouter } from '@edgestore/server/core';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import { createNextProxy, type BucketFunctions } from './createNextProxy';
|
|
4
|
+
import EdgeStoreClientError from './libs/errors/EdgeStoreClientError';
|
|
5
|
+
import { handleError } from './libs/errors/handleError';
|
|
4
6
|
|
|
5
7
|
const DEFAULT_BASE_URL =
|
|
6
8
|
process.env.NEXT_PUBLIC_EDGE_STORE_BASE_URL ?? 'https://files.edgestore.dev';
|
|
@@ -14,6 +16,13 @@ type EdgeStoreContextValue<TRouter extends AnyRouter> = {
|
|
|
14
16
|
* Can be used after a sign-in or sign-out, for example.
|
|
15
17
|
*/
|
|
16
18
|
reset: () => Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* The current state of the Edge Store provider.
|
|
21
|
+
*
|
|
22
|
+
* You can use this to wait for the provider to be initialized
|
|
23
|
+
* before trying to show private images on your app.
|
|
24
|
+
*/
|
|
25
|
+
state: ProviderState;
|
|
17
26
|
};
|
|
18
27
|
|
|
19
28
|
export function createEdgeStoreProvider<TRouter extends AnyRouter>(opts?: {
|
|
@@ -75,6 +84,23 @@ export function createEdgeStoreProvider<TRouter extends AnyRouter>(opts?: {
|
|
|
75
84
|
};
|
|
76
85
|
}
|
|
77
86
|
|
|
87
|
+
type ProviderState =
|
|
88
|
+
| {
|
|
89
|
+
loading: true;
|
|
90
|
+
initialized: false;
|
|
91
|
+
error: false;
|
|
92
|
+
}
|
|
93
|
+
| {
|
|
94
|
+
loading: false;
|
|
95
|
+
initialized: false;
|
|
96
|
+
error: true;
|
|
97
|
+
}
|
|
98
|
+
| {
|
|
99
|
+
loading: false;
|
|
100
|
+
initialized: true;
|
|
101
|
+
error: false;
|
|
102
|
+
};
|
|
103
|
+
|
|
78
104
|
function EdgeStoreProviderInner<TRouter extends AnyRouter>({
|
|
79
105
|
children,
|
|
80
106
|
context,
|
|
@@ -87,25 +113,73 @@ function EdgeStoreProviderInner<TRouter extends AnyRouter>({
|
|
|
87
113
|
maxConcurrentUploads?: number;
|
|
88
114
|
}) {
|
|
89
115
|
const apiPath = basePath ? `${basePath}` : '/api/edgestore';
|
|
116
|
+
const [state, setState] = React.useState<ProviderState>({
|
|
117
|
+
loading: true,
|
|
118
|
+
initialized: false,
|
|
119
|
+
error: false,
|
|
120
|
+
});
|
|
90
121
|
const uploadingCountRef = React.useRef(0);
|
|
122
|
+
const initExecuted = React.useRef(false); // to make sure we don't run init twice
|
|
91
123
|
React.useEffect(() => {
|
|
92
|
-
|
|
124
|
+
if (!initExecuted.current) {
|
|
125
|
+
void init();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return () => {
|
|
129
|
+
initExecuted.current = true;
|
|
130
|
+
};
|
|
93
131
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
94
132
|
}, []);
|
|
95
133
|
|
|
96
134
|
async function init() {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
135
|
+
try {
|
|
136
|
+
setState({
|
|
137
|
+
loading: true,
|
|
138
|
+
initialized: false,
|
|
139
|
+
error: false,
|
|
140
|
+
});
|
|
141
|
+
const res = await fetch(`${apiPath}/init`, {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
});
|
|
144
|
+
if (res.ok) {
|
|
145
|
+
const json = await res.json();
|
|
146
|
+
const innerRes = await fetch(`${DEFAULT_BASE_URL}/_init`, {
|
|
147
|
+
method: 'GET',
|
|
148
|
+
credentials: 'include',
|
|
149
|
+
headers: {
|
|
150
|
+
'x-edgestore-token': json.token,
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
if (innerRes.ok) {
|
|
154
|
+
// update state
|
|
155
|
+
setState({
|
|
156
|
+
loading: false,
|
|
157
|
+
initialized: true,
|
|
158
|
+
error: false,
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
setState({
|
|
162
|
+
loading: false,
|
|
163
|
+
initialized: false,
|
|
164
|
+
error: true,
|
|
165
|
+
});
|
|
166
|
+
throw new EdgeStoreClientError("Couldn't initialize Edge Store.");
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
setState({
|
|
170
|
+
loading: false,
|
|
171
|
+
initialized: false,
|
|
172
|
+
error: true,
|
|
173
|
+
});
|
|
174
|
+
await handleError(res);
|
|
175
|
+
}
|
|
176
|
+
} catch (err) {
|
|
177
|
+
setState({
|
|
178
|
+
loading: false,
|
|
179
|
+
initialized: false,
|
|
180
|
+
error: true,
|
|
108
181
|
});
|
|
182
|
+
throw err;
|
|
109
183
|
}
|
|
110
184
|
}
|
|
111
185
|
|
|
@@ -123,6 +197,7 @@ function EdgeStoreProviderInner<TRouter extends AnyRouter>({
|
|
|
123
197
|
maxConcurrentUploads,
|
|
124
198
|
}),
|
|
125
199
|
reset,
|
|
200
|
+
state,
|
|
126
201
|
}}
|
|
127
202
|
>
|
|
128
203
|
{children}
|
package/src/createNextProxy.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { type RequestUploadRes } from '@edgestore/server/adapters';
|
|
2
2
|
import {
|
|
3
|
-
EdgeStoreApiClientError,
|
|
4
3
|
type AnyRouter,
|
|
5
4
|
type InferBucketPathObject,
|
|
6
5
|
type InferMetadataObject,
|
|
7
6
|
type UploadOptions,
|
|
8
7
|
} from '@edgestore/server/core';
|
|
9
8
|
import { type z } from 'zod';
|
|
10
|
-
import EdgeStoreClientError from './libs/errors/
|
|
9
|
+
import EdgeStoreClientError from './libs/errors/EdgeStoreClientError';
|
|
10
|
+
import { handleError } from './libs/errors/handleError';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @internal
|
|
@@ -160,9 +160,7 @@ async function uploadFile(
|
|
|
160
160
|
},
|
|
161
161
|
});
|
|
162
162
|
if (!res.ok) {
|
|
163
|
-
|
|
164
|
-
response: await res.json(),
|
|
165
|
-
});
|
|
163
|
+
await handleError(res);
|
|
166
164
|
}
|
|
167
165
|
const json = (await res.json()) as RequestUploadRes;
|
|
168
166
|
if ('multipart' in json) {
|
|
@@ -327,9 +325,7 @@ async function multipartUpload(params: {
|
|
|
327
325
|
},
|
|
328
326
|
});
|
|
329
327
|
if (!res.ok) {
|
|
330
|
-
|
|
331
|
-
response: await res.json(),
|
|
332
|
-
});
|
|
328
|
+
await handleError(res);
|
|
333
329
|
}
|
|
334
330
|
}
|
|
335
331
|
|
|
@@ -358,9 +354,7 @@ async function confirmUpload(
|
|
|
358
354
|
},
|
|
359
355
|
});
|
|
360
356
|
if (!res.ok) {
|
|
361
|
-
|
|
362
|
-
response: await res.json(),
|
|
363
|
-
});
|
|
357
|
+
await handleError(res);
|
|
364
358
|
}
|
|
365
359
|
return res.json();
|
|
366
360
|
}
|
|
@@ -390,9 +384,7 @@ async function deleteFile(
|
|
|
390
384
|
},
|
|
391
385
|
});
|
|
392
386
|
if (!res.ok) {
|
|
393
|
-
|
|
394
|
-
response: await res.json(),
|
|
395
|
-
});
|
|
387
|
+
await handleError(res);
|
|
396
388
|
}
|
|
397
389
|
return res.json();
|
|
398
390
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EdgeStoreApiClientError } from '@edgestore/server/core';
|
|
2
|
+
import EdgeStoreClientError from './EdgeStoreClientError';
|
|
3
|
+
|
|
4
|
+
export async function handleError(res: Response): Promise<never> {
|
|
5
|
+
try {
|
|
6
|
+
const json = await res.json();
|
|
7
|
+
throw new EdgeStoreApiClientError({ response: json });
|
|
8
|
+
} catch (err) {
|
|
9
|
+
throw new EdgeStoreClientError(
|
|
10
|
+
`Failed to parse response. Make sure the api is correctly configured at ${res.url}`,
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"EdgeStoreError.d.ts","sourceRoot":"","sources":["../../../src/libs/errors/EdgeStoreError.ts"],"names":[],"mappings":"AAAA,cAAM,oBAAqB,SAAQ,KAAK;gBAC1B,OAAO,EAAE,MAAM;CAI5B;AAED,eAAe,oBAAoB,CAAC"}
|
|
File without changes
|