@edgestore/server 0.7.0 → 1.0.0-next.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/adapters/astro/index.d.ts +1 -1
- package/adapters/astro/index.js +1 -1
- package/adapters/express/index.d.ts +1 -1
- package/adapters/express/index.js +1 -1
- package/adapters/fastify/index.d.ts +1 -1
- package/adapters/fastify/index.js +1 -1
- package/adapters/hono/index.d.ts +1 -1
- package/adapters/hono/index.js +1 -1
- package/adapters/next/app/index.d.ts +1 -1
- package/adapters/next/app/index.js +1 -1
- package/adapters/next/pages/index.d.ts +1 -1
- package/adapters/next/pages/index.js +1 -1
- package/adapters/remix/index.d.ts +1 -1
- package/adapters/remix/index.js +1 -1
- package/adapters/start/index.d.ts +1 -1
- package/adapters/start/index.js +1 -1
- package/core/index.d.ts +1 -1
- package/core/index.js +1 -1
- package/dist/adapters/astro/index.d.ts +2 -2
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +35 -43
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +34 -41
- package/dist/adapters/fastify/index.d.ts +3 -3
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +33 -42
- package/dist/adapters/hono/index.d.ts +6 -72
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +38 -41
- package/dist/adapters/imageTypes.js +1 -3
- package/dist/adapters/next/app/index.d.ts +2 -2
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +34 -42
- package/dist/adapters/next/pages/index.d.ts +2 -2
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +35 -42
- package/dist/adapters/remix/index.d.ts +2 -2
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +35 -42
- package/dist/adapters/shared.d.ts +8 -0
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +79 -56
- package/dist/adapters/start/index.d.ts +2 -2
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +35 -43
- package/dist/core/client/index.d.ts +112 -69
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +59 -13
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -10
- package/dist/core/sdk/index.d.ts +44 -2
- package/dist/core/sdk/index.d.ts.map +1 -1
- package/dist/core/sdk/index.js +43 -16
- package/dist/index.js +1 -10
- package/dist/libs/errors/EdgeStoreCredentialsError.js +1 -3
- package/dist/libs/logger.js +5 -9
- package/dist/libs/utils.js +1 -3
- package/dist/providers/aws/index.d.ts +1 -1
- package/dist/providers/aws/index.d.ts.map +1 -1
- package/dist/providers/aws/index.js +14 -17
- package/dist/providers/azure/index.d.ts.map +1 -1
- package/dist/providers/azure/index.js +38 -15
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +36 -18
- package/dist/test-utils/edgestoreSmoke.d.ts +32 -0
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +4 -0
- package/package.json +59 -49
- package/providers/aws/index.d.ts +1 -1
- package/providers/aws/index.js +1 -1
- package/providers/azure/index.d.ts +1 -1
- package/providers/azure/index.js +1 -1
- package/providers/edgestore/index.d.ts +1 -1
- package/providers/edgestore/index.js +1 -1
- package/src/adapters/astro/index.ts +11 -21
- package/src/adapters/express/index.ts +10 -16
- package/src/adapters/fastify/index.ts +13 -19
- package/src/adapters/hono/index.ts +19 -23
- package/src/adapters/next/app/index.ts +12 -22
- package/src/adapters/next/pages/index.ts +11 -16
- package/src/adapters/remix/index.ts +11 -12
- package/src/adapters/shared.ts +61 -17
- package/src/adapters/start/index.ts +11 -12
- package/src/core/client/index.ts +199 -75
- package/src/core/sdk/index.ts +76 -8
- package/src/providers/aws/index.ts +4 -5
- package/src/providers/azure/index.ts +40 -11
- package/src/providers/edgestore/index.ts +29 -0
- package/src/test-utils/edgestoreSmoke.ts +141 -0
- package/src/version.ts +2 -0
- package/dist/adapters/astro/index.mjs +0 -178
- package/dist/adapters/express/index.mjs +0 -113
- package/dist/adapters/fastify/index.mjs +0 -142
- package/dist/adapters/hono/index.mjs +0 -132
- package/dist/adapters/imageTypes.mjs +0 -12
- package/dist/adapters/next/app/index.mjs +0 -169
- package/dist/adapters/next/pages/index.mjs +0 -117
- package/dist/adapters/remix/index.mjs +0 -153
- package/dist/adapters/shared.mjs +0 -443
- package/dist/adapters/start/index.mjs +0 -187
- package/dist/core/client/index.mjs +0 -173
- package/dist/core/index.mjs +0 -2
- package/dist/core/sdk/index.mjs +0 -223
- package/dist/index.mjs +0 -1
- package/dist/libs/errors/EdgeStoreCredentialsError.mjs +0 -10
- package/dist/libs/logger.mjs +0 -53
- package/dist/libs/utils.mjs +0 -10
- package/dist/providers/aws/index.mjs +0 -103
- package/dist/providers/azure/index.mjs +0 -59
- package/dist/providers/edgestore/index.mjs +0 -144
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
completeMultipartUpload,
|
|
14
14
|
confirmUpload,
|
|
15
15
|
deleteFile,
|
|
16
|
+
fetchProxyFile,
|
|
16
17
|
getCookieConfig,
|
|
17
18
|
init,
|
|
18
19
|
requestUpload,
|
|
@@ -92,7 +93,7 @@ export function createEdgeStoreRemixHandler<TCtx>(config: Config<TCtx>) {
|
|
|
92
93
|
cause: err instanceof Error ? err : undefined,
|
|
93
94
|
});
|
|
94
95
|
}
|
|
95
|
-
const { newCookies,
|
|
96
|
+
const { newCookies, ...body } = await init({
|
|
96
97
|
ctx,
|
|
97
98
|
provider,
|
|
98
99
|
router: config.router,
|
|
@@ -110,7 +111,7 @@ export function createEdgeStoreRemixHandler<TCtx>(config: Config<TCtx>) {
|
|
|
110
111
|
responseHeaders.append('Set-Cookie', newCookies);
|
|
111
112
|
}
|
|
112
113
|
|
|
113
|
-
return new Response(JSON.stringify(
|
|
114
|
+
return new Response(JSON.stringify(body), {
|
|
114
115
|
headers: responseHeaders,
|
|
115
116
|
status: 200,
|
|
116
117
|
});
|
|
@@ -166,20 +167,18 @@ export function createEdgeStoreRemixHandler<TCtx>(config: Config<TCtx>) {
|
|
|
166
167
|
} else if (matchPath(pathname, '/proxy-file')) {
|
|
167
168
|
const url = new URL(req.url).searchParams.get('url');
|
|
168
169
|
if (typeof url === 'string') {
|
|
169
|
-
const proxyRes = await
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
},
|
|
170
|
+
const proxyRes = await fetchProxyFile({
|
|
171
|
+
cookieHeader: req.headers.get('cookie') ?? undefined,
|
|
172
|
+
url,
|
|
173
173
|
});
|
|
174
174
|
|
|
175
|
-
const data = await proxyRes.arrayBuffer();
|
|
176
175
|
const headers = new Headers();
|
|
177
|
-
headers.set(
|
|
178
|
-
'Content-Type',
|
|
179
|
-
proxyRes.headers.get('Content-Type') ?? 'application/octet-stream',
|
|
180
|
-
);
|
|
176
|
+
headers.set('Content-Type', proxyRes.contentType);
|
|
181
177
|
|
|
182
|
-
return new Response(
|
|
178
|
+
return new Response(proxyRes.body, {
|
|
179
|
+
headers,
|
|
180
|
+
status: proxyRes.status,
|
|
181
|
+
});
|
|
183
182
|
} else {
|
|
184
183
|
return new Response(null, { status: 400 });
|
|
185
184
|
}
|
package/src/adapters/shared.ts
CHANGED
|
@@ -9,9 +9,8 @@ import {
|
|
|
9
9
|
type SharedRequestUploadRes,
|
|
10
10
|
} from '@edgestore/shared';
|
|
11
11
|
import { hkdf } from '@panva/hkdf';
|
|
12
|
-
import {
|
|
12
|
+
import { stringifySetCookie } from 'cookie';
|
|
13
13
|
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
14
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
15
14
|
import type Logger from '../libs/logger';
|
|
16
15
|
import { IMAGE_MIME_TYPES } from './imageTypes';
|
|
17
16
|
|
|
@@ -22,6 +21,33 @@ declare const globalThis: {
|
|
|
22
21
|
_EDGE_STORE_LOGGER: Logger;
|
|
23
22
|
};
|
|
24
23
|
|
|
24
|
+
const NO_BODY_STATUSES = new Set([204, 205, 304]);
|
|
25
|
+
|
|
26
|
+
export async function fetchProxyFile({
|
|
27
|
+
cookieHeader,
|
|
28
|
+
url,
|
|
29
|
+
}: {
|
|
30
|
+
cookieHeader?: string;
|
|
31
|
+
url: string;
|
|
32
|
+
}) {
|
|
33
|
+
const proxyRes = await fetch(url, {
|
|
34
|
+
headers: {
|
|
35
|
+
cookie: cookieHeader ?? '',
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const body = NO_BODY_STATUSES.has(proxyRes.status)
|
|
40
|
+
? null
|
|
41
|
+
: await proxyRes.arrayBuffer();
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
body,
|
|
45
|
+
contentType:
|
|
46
|
+
proxyRes.headers.get('Content-Type') ?? 'application/octet-stream',
|
|
47
|
+
status: proxyRes.status,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
25
51
|
export type CookieOptions = {
|
|
26
52
|
/**
|
|
27
53
|
* Cookie path
|
|
@@ -145,24 +171,38 @@ export async function init<TCtx>(params: {
|
|
|
145
171
|
const resolvedCookieConfig = getCookieConfig(cookieConfig);
|
|
146
172
|
|
|
147
173
|
const ctxToken = await encryptJWT(ctx);
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
router
|
|
151
|
-
|
|
174
|
+
const requiresFileAccessCookie =
|
|
175
|
+
provider.name === 'edgestore' &&
|
|
176
|
+
Object.values(router.buckets).some(
|
|
177
|
+
(bucket) =>
|
|
178
|
+
bucket._def.accessControl !== undefined &&
|
|
179
|
+
bucket._def.accessControl !== 'private',
|
|
180
|
+
);
|
|
181
|
+
const shouldRunProviderInit =
|
|
182
|
+
provider.name !== 'edgestore' || requiresFileAccessCookie;
|
|
183
|
+
|
|
184
|
+
let token: string | undefined;
|
|
185
|
+
if (shouldRunProviderInit) {
|
|
186
|
+
const initRes = await provider.init({
|
|
187
|
+
ctx,
|
|
188
|
+
router: router,
|
|
189
|
+
});
|
|
190
|
+
token = initRes.token;
|
|
191
|
+
}
|
|
152
192
|
const newCookies = [
|
|
153
|
-
|
|
154
|
-
resolvedCookieConfig.ctx.name,
|
|
155
|
-
ctxToken,
|
|
156
|
-
resolvedCookieConfig.ctx.options,
|
|
157
|
-
),
|
|
193
|
+
stringifySetCookie({
|
|
194
|
+
name: resolvedCookieConfig.ctx.name,
|
|
195
|
+
value: ctxToken,
|
|
196
|
+
...resolvedCookieConfig.ctx.options,
|
|
197
|
+
}),
|
|
158
198
|
];
|
|
159
199
|
if (token) {
|
|
160
200
|
newCookies.push(
|
|
161
|
-
|
|
162
|
-
resolvedCookieConfig.token.name,
|
|
163
|
-
token,
|
|
164
|
-
resolvedCookieConfig.token.options,
|
|
165
|
-
),
|
|
201
|
+
stringifySetCookie({
|
|
202
|
+
name: resolvedCookieConfig.token.name,
|
|
203
|
+
value: token,
|
|
204
|
+
...resolvedCookieConfig.token.options,
|
|
205
|
+
}),
|
|
166
206
|
);
|
|
167
207
|
}
|
|
168
208
|
const baseUrl = await provider.getBaseUrl();
|
|
@@ -173,6 +213,7 @@ export async function init<TCtx>(params: {
|
|
|
173
213
|
token,
|
|
174
214
|
baseUrl,
|
|
175
215
|
providerName: provider.name,
|
|
216
|
+
requiresFileAccessCookie,
|
|
176
217
|
});
|
|
177
218
|
|
|
178
219
|
return {
|
|
@@ -180,6 +221,7 @@ export async function init<TCtx>(params: {
|
|
|
180
221
|
token,
|
|
181
222
|
baseUrl,
|
|
182
223
|
providerName: provider.name,
|
|
224
|
+
requiresFileAccessCookie,
|
|
183
225
|
};
|
|
184
226
|
}
|
|
185
227
|
|
|
@@ -313,6 +355,7 @@ export async function requestUpload<TCtx>(params: {
|
|
|
313
355
|
});
|
|
314
356
|
const metadata = await bucket._def.metadata?.({ ctx, input });
|
|
315
357
|
const isPublic = bucket._def.accessControl === undefined;
|
|
358
|
+
const autoSignedUrls = bucket._def.autoSignedUrls;
|
|
316
359
|
|
|
317
360
|
log.debug('upload info', {
|
|
318
361
|
path,
|
|
@@ -330,6 +373,7 @@ export async function requestUpload<TCtx>(params: {
|
|
|
330
373
|
isPublic,
|
|
331
374
|
metadata,
|
|
332
375
|
},
|
|
376
|
+
autoSignedUrls,
|
|
333
377
|
});
|
|
334
378
|
const { parsedPath, pathOrder } = parsePath(path);
|
|
335
379
|
|
|
@@ -569,7 +613,7 @@ async function encryptJWT(ctx: any) {
|
|
|
569
613
|
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
|
|
570
614
|
.setIssuedAt()
|
|
571
615
|
.setExpirationTime(Date.now() / 1000 + DEFAULT_MAX_AGE)
|
|
572
|
-
.setJti(
|
|
616
|
+
.setJti(crypto.randomUUID())
|
|
573
617
|
.encrypt(encryptionSecret);
|
|
574
618
|
}
|
|
575
619
|
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
completeMultipartUpload,
|
|
14
14
|
confirmUpload,
|
|
15
15
|
deleteFile,
|
|
16
|
+
fetchProxyFile,
|
|
16
17
|
getCookieConfig,
|
|
17
18
|
init,
|
|
18
19
|
requestUpload,
|
|
@@ -90,7 +91,7 @@ export function createEdgeStoreStartHandler<TCtx>(config: Config<TCtx>) {
|
|
|
90
91
|
cause: err instanceof Error ? err : undefined,
|
|
91
92
|
});
|
|
92
93
|
}
|
|
93
|
-
const { newCookies,
|
|
94
|
+
const { newCookies, ...body } = await init({
|
|
94
95
|
ctx,
|
|
95
96
|
provider,
|
|
96
97
|
router: config.router,
|
|
@@ -101,7 +102,7 @@ export function createEdgeStoreStartHandler<TCtx>(config: Config<TCtx>) {
|
|
|
101
102
|
headers.append('Set-Cookie', cookie);
|
|
102
103
|
});
|
|
103
104
|
headers.set('Content-Type', 'application/json');
|
|
104
|
-
return new Response(JSON.stringify(
|
|
105
|
+
return new Response(JSON.stringify(body), {
|
|
105
106
|
status: 200,
|
|
106
107
|
headers,
|
|
107
108
|
});
|
|
@@ -170,18 +171,16 @@ export function createEdgeStoreStartHandler<TCtx>(config: Config<TCtx>) {
|
|
|
170
171
|
} else if (matchPath(pathname, '/proxy-file')) {
|
|
171
172
|
const urlParam = new URL(request.url).searchParams.get('url');
|
|
172
173
|
if (typeof urlParam === 'string') {
|
|
173
|
-
const proxyRes = await
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
},
|
|
174
|
+
const proxyRes = await fetchProxyFile({
|
|
175
|
+
cookieHeader: request.headers.get('cookie') ?? undefined,
|
|
176
|
+
url: urlParam,
|
|
177
177
|
});
|
|
178
|
-
const data = await proxyRes.arrayBuffer();
|
|
179
178
|
const headers = new Headers();
|
|
180
|
-
headers.set(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
headers.set('Content-Type', proxyRes.contentType);
|
|
180
|
+
return new Response(proxyRes.body, {
|
|
181
|
+
headers,
|
|
182
|
+
status: proxyRes.status,
|
|
183
|
+
});
|
|
185
184
|
} else {
|
|
186
185
|
return new Response(null, { status: 400 });
|
|
187
186
|
}
|
package/src/core/client/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
1
|
import {
|
|
3
2
|
type AnyBuilder,
|
|
4
3
|
type AnyRouter,
|
|
5
4
|
type InferBucketPathKeys,
|
|
6
5
|
type InferBucketPathObject,
|
|
6
|
+
type InferBucketPathOrder,
|
|
7
7
|
type InferMetadataObject,
|
|
8
|
+
type MaybePromise,
|
|
8
9
|
type Prettify,
|
|
9
10
|
type Simplify,
|
|
10
11
|
} from '@edgestore/shared';
|
|
@@ -49,6 +50,14 @@ export type UploadOptions = {
|
|
|
49
50
|
* This avoids unnecessary zombie files in the bucket.
|
|
50
51
|
*/
|
|
51
52
|
temporary?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Transform the file before it is validated and uploaded.
|
|
55
|
+
*
|
|
56
|
+
* This can be used to compress images, convert formats, encrypt files, etc.
|
|
57
|
+
* The transformed blob's size, MIME type, and extension will be used for the
|
|
58
|
+
* upload request.
|
|
59
|
+
*/
|
|
60
|
+
transform?: ServerUploadTransform;
|
|
52
61
|
};
|
|
53
62
|
|
|
54
63
|
type TextContent = string;
|
|
@@ -61,6 +70,17 @@ type UrlContent = {
|
|
|
61
70
|
extension: string;
|
|
62
71
|
};
|
|
63
72
|
|
|
73
|
+
export type UploadContent = TextContent | BlobContent | UrlContent;
|
|
74
|
+
|
|
75
|
+
export type ServerUploadTransform = (params: {
|
|
76
|
+
blob: Blob;
|
|
77
|
+
extension: string;
|
|
78
|
+
type: string;
|
|
79
|
+
}) => MaybePromise<{
|
|
80
|
+
blob: Blob;
|
|
81
|
+
extension: string;
|
|
82
|
+
}>;
|
|
83
|
+
|
|
64
84
|
// type guard for `content`
|
|
65
85
|
function isTextContent(
|
|
66
86
|
content: TextContent | BlobContent | UrlContent,
|
|
@@ -98,7 +118,7 @@ export type UploadFileRequest<TBucket extends AnyBuilder> = {
|
|
|
98
118
|
* extension: "csv",
|
|
99
119
|
* }
|
|
100
120
|
*/
|
|
101
|
-
content:
|
|
121
|
+
content: UploadContent;
|
|
102
122
|
options?: UploadOptions;
|
|
103
123
|
} & (TBucket['$config']['ctx'] extends Record<string, never>
|
|
104
124
|
? {}
|
|
@@ -111,23 +131,37 @@ export type UploadFileRequest<TBucket extends AnyBuilder> = {
|
|
|
111
131
|
input: z.infer<TBucket['_def']['input']>;
|
|
112
132
|
});
|
|
113
133
|
|
|
134
|
+
type UploadImplementationParams = {
|
|
135
|
+
content: UploadContent;
|
|
136
|
+
ctx?: Record<string, unknown>;
|
|
137
|
+
input?: Record<string, unknown>;
|
|
138
|
+
};
|
|
139
|
+
|
|
114
140
|
export type UploadFileRes<TBucket extends AnyBuilder> =
|
|
115
|
-
TBucket['_def']['type'] extends 'IMAGE'
|
|
141
|
+
(TBucket['_def']['type'] extends 'IMAGE'
|
|
116
142
|
? {
|
|
117
143
|
url: string;
|
|
118
144
|
thumbnailUrl: string | null;
|
|
119
145
|
size: number;
|
|
120
146
|
metadata: InferMetadataObject<TBucket>;
|
|
121
147
|
path: InferBucketPathObject<TBucket>;
|
|
122
|
-
pathOrder:
|
|
148
|
+
pathOrder: InferBucketPathOrder<TBucket>;
|
|
123
149
|
}
|
|
124
150
|
: {
|
|
125
151
|
url: string;
|
|
126
152
|
size: number;
|
|
127
153
|
metadata: InferMetadataObject<TBucket>;
|
|
128
154
|
path: InferBucketPathObject<TBucket>;
|
|
129
|
-
pathOrder:
|
|
130
|
-
}
|
|
155
|
+
pathOrder: InferBucketPathOrder<TBucket>;
|
|
156
|
+
}) &
|
|
157
|
+
(undefined extends TBucket['_def']['autoSignedUrls']
|
|
158
|
+
? unknown
|
|
159
|
+
: {
|
|
160
|
+
signedUrl: string;
|
|
161
|
+
expiresAt: Date;
|
|
162
|
+
expiresIn: number;
|
|
163
|
+
signedThumbnailUrl?: string | null;
|
|
164
|
+
});
|
|
131
165
|
|
|
132
166
|
type Filter<TBucket extends AnyBuilder> = {
|
|
133
167
|
AND?: Filter<TBucket>[];
|
|
@@ -173,69 +207,96 @@ export type ListFilesResponse<TBucket extends AnyBuilder> = {
|
|
|
173
207
|
};
|
|
174
208
|
};
|
|
175
209
|
|
|
210
|
+
type GetSignedUrlRes = {
|
|
211
|
+
url: string;
|
|
212
|
+
signedUrl: string;
|
|
213
|
+
expiresAt: Date;
|
|
214
|
+
expiresIn: number;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
type GetSignedUrlsRes<TBucket extends AnyBuilder> =
|
|
218
|
+
TBucket['_def']['type'] extends 'IMAGE'
|
|
219
|
+
? (GetSignedUrlRes & {
|
|
220
|
+
thumbnailUrl?: string | null;
|
|
221
|
+
signedThumbnailUrl?: string | null;
|
|
222
|
+
})[]
|
|
223
|
+
: GetSignedUrlRes[];
|
|
224
|
+
|
|
225
|
+
type BucketClient<TBucket extends AnyBuilder> = {
|
|
226
|
+
getFile: (params: { url: string }) => Promise<Prettify<GetFileRes<TBucket>>>;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Use this function to upload a file to the bucket directly from your backend.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* // simple example
|
|
234
|
+
* await backendClient.myBucket.upload({
|
|
235
|
+
* content: "some text",
|
|
236
|
+
* });
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* // complete example
|
|
242
|
+
* await backendClient.myBucket.upload({
|
|
243
|
+
* content: {
|
|
244
|
+
* blob: new Blob([text], { type: "text/csv" }),
|
|
245
|
+
* extension: "csv",
|
|
246
|
+
* },
|
|
247
|
+
* options: {
|
|
248
|
+
* temporary: true,
|
|
249
|
+
* replaceTargetUrl: replaceUrl,
|
|
250
|
+
* manualFileName: "test.csv",
|
|
251
|
+
* },
|
|
252
|
+
* ctx: {
|
|
253
|
+
* userId: "123",
|
|
254
|
+
* userRole: "admin",
|
|
255
|
+
* },
|
|
256
|
+
* input: {
|
|
257
|
+
* type: "post",
|
|
258
|
+
* },
|
|
259
|
+
* });
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
upload: (
|
|
263
|
+
params: Prettify<UploadFileRequest<TBucket>>,
|
|
264
|
+
) => Promise<Prettify<UploadFileRes<TBucket>>>;
|
|
265
|
+
/**
|
|
266
|
+
* Confirm a temporary file upload directly from your backend.
|
|
267
|
+
*/
|
|
268
|
+
confirmUpload: (params: { url: string }) => Promise<{ success: boolean }>;
|
|
269
|
+
/**
|
|
270
|
+
* Programmatically delete a file directly from your backend.
|
|
271
|
+
*/
|
|
272
|
+
deleteFile: (params: { url: string }) => Promise<{
|
|
273
|
+
success: boolean;
|
|
274
|
+
}>;
|
|
275
|
+
/**
|
|
276
|
+
* List files in a bucket.
|
|
277
|
+
*
|
|
278
|
+
* You can also filter the results by passing a filter object.
|
|
279
|
+
* The results are paginated.
|
|
280
|
+
*/
|
|
281
|
+
listFiles: (
|
|
282
|
+
params?: ListFilesRequest<TBucket>,
|
|
283
|
+
) => Promise<Prettify<ListFilesResponse<TBucket>>>;
|
|
284
|
+
} & (undefined extends TBucket['_def']['accessControl']
|
|
285
|
+
? unknown
|
|
286
|
+
: {
|
|
287
|
+
getSignedUrl: (params: {
|
|
288
|
+
url: string;
|
|
289
|
+
expiresIn?: number;
|
|
290
|
+
}) => Promise<Prettify<GetSignedUrlRes>>;
|
|
291
|
+
getSignedUrls: (params: {
|
|
292
|
+
urls: string[];
|
|
293
|
+
expiresIn?: number;
|
|
294
|
+
includeThumbnails?: boolean;
|
|
295
|
+
}) => Promise<Prettify<GetSignedUrlsRes<TBucket>[number]>[]>;
|
|
296
|
+
});
|
|
297
|
+
|
|
176
298
|
type EdgeStoreClient<TRouter extends AnyRouter> = {
|
|
177
|
-
[K in keyof TRouter['buckets']]:
|
|
178
|
-
getFile: (params: {
|
|
179
|
-
url: string;
|
|
180
|
-
}) => Promise<GetFileRes<TRouter['buckets'][K]>>;
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Use this function to upload a file to the bucket directly from your backend.
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```ts
|
|
187
|
-
* // simple example
|
|
188
|
-
* await backendClient.myBucket.upload({
|
|
189
|
-
* content: "some text",
|
|
190
|
-
* });
|
|
191
|
-
* ```
|
|
192
|
-
*
|
|
193
|
-
* @example
|
|
194
|
-
* ```ts
|
|
195
|
-
* // complete example
|
|
196
|
-
* await backendClient.myBucket.upload({
|
|
197
|
-
* content: {
|
|
198
|
-
* blob: new Blob([text], { type: "text/csv" }),
|
|
199
|
-
* extension: "csv",
|
|
200
|
-
* },
|
|
201
|
-
* options: {
|
|
202
|
-
* temporary: true,
|
|
203
|
-
* replaceTargetUrl: replaceUrl,
|
|
204
|
-
* manualFileName: "test.csv",
|
|
205
|
-
* },
|
|
206
|
-
* ctx: {
|
|
207
|
-
* userId: "123",
|
|
208
|
-
* userRole: "admin",
|
|
209
|
-
* },
|
|
210
|
-
* input: {
|
|
211
|
-
* type: "post",
|
|
212
|
-
* },
|
|
213
|
-
* });
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
upload: (
|
|
217
|
-
params: UploadFileRequest<TRouter['buckets'][K]>,
|
|
218
|
-
) => Promise<Prettify<UploadFileRes<TRouter['buckets'][K]>>>;
|
|
219
|
-
/**
|
|
220
|
-
* Confirm a temporary file upload directly from your backend.
|
|
221
|
-
*/
|
|
222
|
-
confirmUpload: (params: { url: string }) => Promise<{ success: boolean }>;
|
|
223
|
-
/**
|
|
224
|
-
* Programmatically delete a file directly from your backend.
|
|
225
|
-
*/
|
|
226
|
-
deleteFile: (params: { url: string }) => Promise<{
|
|
227
|
-
success: boolean;
|
|
228
|
-
}>;
|
|
229
|
-
/**
|
|
230
|
-
* List files in a bucket.
|
|
231
|
-
*
|
|
232
|
-
* You can also filter the results by passing a filter object.
|
|
233
|
-
* The results are paginated.
|
|
234
|
-
*/
|
|
235
|
-
listFiles: (
|
|
236
|
-
params?: ListFilesRequest<TRouter['buckets'][K]>,
|
|
237
|
-
) => Promise<Prettify<ListFilesResponse<TRouter['buckets'][K]>>>;
|
|
238
|
-
};
|
|
299
|
+
[K in keyof TRouter['buckets']]: BucketClient<TRouter['buckets'][K]>;
|
|
239
300
|
};
|
|
240
301
|
|
|
241
302
|
export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
@@ -264,11 +325,13 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
264
325
|
}
|
|
265
326
|
const client: EdgeStoreClient<TRouter>[string] = {
|
|
266
327
|
async upload(params) {
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
328
|
+
const {
|
|
329
|
+
content,
|
|
330
|
+
ctx = {},
|
|
331
|
+
input = {},
|
|
332
|
+
}: UploadImplementationParams = params;
|
|
270
333
|
|
|
271
|
-
|
|
334
|
+
let { blob, extension } = await (async () => {
|
|
272
335
|
if (isTextContent(content)) {
|
|
273
336
|
return {
|
|
274
337
|
blob: new Blob([content], { type: 'text/plain' }),
|
|
@@ -287,6 +350,16 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
287
350
|
}
|
|
288
351
|
})();
|
|
289
352
|
|
|
353
|
+
if (params.options?.transform) {
|
|
354
|
+
const transformed = await params.options.transform({
|
|
355
|
+
blob,
|
|
356
|
+
extension,
|
|
357
|
+
type: blob.type,
|
|
358
|
+
});
|
|
359
|
+
blob = transformed.blob;
|
|
360
|
+
extension = transformed.extension;
|
|
361
|
+
}
|
|
362
|
+
|
|
290
363
|
const path = buildPath({
|
|
291
364
|
bucket,
|
|
292
365
|
pathAttrs: {
|
|
@@ -310,6 +383,7 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
310
383
|
const requestUploadRes = await sdk.requestUpload({
|
|
311
384
|
bucketName,
|
|
312
385
|
bucketType: bucket._def.type,
|
|
386
|
+
autoSignedUrls: bucket._def.autoSignedUrls,
|
|
313
387
|
fileInfo: {
|
|
314
388
|
fileName: params.options?.manualFileName,
|
|
315
389
|
replaceTargetUrl: params.options?.replaceTargetUrl,
|
|
@@ -348,13 +422,12 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
348
422
|
...{
|
|
349
423
|
thumbnailUrl: requestUploadRes.thumbnailUrl,
|
|
350
424
|
},
|
|
425
|
+
...mapSignedUploadAccess(requestUploadRes),
|
|
351
426
|
size: blob.size,
|
|
352
427
|
metadata,
|
|
353
428
|
path: parsedPath,
|
|
354
429
|
pathOrder,
|
|
355
|
-
}
|
|
356
|
-
TRouter['buckets'][string]
|
|
357
|
-
>;
|
|
430
|
+
} as unknown as UploadFileRes<TRouter['buckets'][string]>;
|
|
358
431
|
},
|
|
359
432
|
|
|
360
433
|
async getFile(params) {
|
|
@@ -402,6 +475,38 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
402
475
|
pagination: res.pagination,
|
|
403
476
|
};
|
|
404
477
|
},
|
|
478
|
+
|
|
479
|
+
async getSignedUrl(params: { url: string; expiresIn?: number }) {
|
|
480
|
+
const [signedUrl] = await sdk.getSignedUrls({
|
|
481
|
+
bucketName,
|
|
482
|
+
urls: [params.url],
|
|
483
|
+
expiresIn: params.expiresIn,
|
|
484
|
+
});
|
|
485
|
+
if (!signedUrl) {
|
|
486
|
+
throw new Error('Missing signed URL response');
|
|
487
|
+
}
|
|
488
|
+
return {
|
|
489
|
+
...signedUrl,
|
|
490
|
+
expiresAt: new Date(signedUrl.expiresAt),
|
|
491
|
+
};
|
|
492
|
+
},
|
|
493
|
+
|
|
494
|
+
async getSignedUrls(params: {
|
|
495
|
+
urls: string[];
|
|
496
|
+
expiresIn?: number;
|
|
497
|
+
includeThumbnails?: boolean;
|
|
498
|
+
}) {
|
|
499
|
+
const signedUrls = await sdk.getSignedUrls({
|
|
500
|
+
bucketName,
|
|
501
|
+
urls: params.urls,
|
|
502
|
+
expiresIn: params.expiresIn,
|
|
503
|
+
includeThumbnails: params.includeThumbnails,
|
|
504
|
+
});
|
|
505
|
+
return signedUrls.map((signedUrl) => ({
|
|
506
|
+
...signedUrl,
|
|
507
|
+
expiresAt: new Date(signedUrl.expiresAt),
|
|
508
|
+
})) as any;
|
|
509
|
+
},
|
|
405
510
|
};
|
|
406
511
|
return client;
|
|
407
512
|
},
|
|
@@ -435,6 +540,25 @@ async function getBlobFromUrl(url: string) {
|
|
|
435
540
|
return await res.blob();
|
|
436
541
|
}
|
|
437
542
|
|
|
543
|
+
function mapSignedUploadAccess(res: {
|
|
544
|
+
accessSignedUrl?: string;
|
|
545
|
+
accessSignedThumbnailUrl?: string | null;
|
|
546
|
+
accessSignedUrlExpiresAt?: Date | string;
|
|
547
|
+
accessSignedUrlExpiresIn?: number;
|
|
548
|
+
}) {
|
|
549
|
+
if (!res.accessSignedUrl) {
|
|
550
|
+
return {};
|
|
551
|
+
}
|
|
552
|
+
return {
|
|
553
|
+
signedUrl: res.accessSignedUrl,
|
|
554
|
+
expiresAt: res.accessSignedUrlExpiresAt
|
|
555
|
+
? new Date(res.accessSignedUrlExpiresAt)
|
|
556
|
+
: new Date(),
|
|
557
|
+
expiresIn: res.accessSignedUrlExpiresIn ?? 0,
|
|
558
|
+
signedThumbnailUrl: res.accessSignedThumbnailUrl ?? null,
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
438
562
|
export type InferClientResponse<TRouter extends AnyRouter> = {
|
|
439
563
|
[TBucketName in keyof TRouter['buckets']]: {
|
|
440
564
|
[TClienFn in keyof EdgeStoreClient<TRouter>[TBucketName]]: Simplify<
|