@edgestore/server 1.0.0-next.3 → 1.0.0-next.4
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/agent-docs/README.md +20 -0
- package/agent-docs/astro.md +153 -0
- package/agent-docs/backend-client.md +294 -0
- package/agent-docs/configuration.md +269 -0
- package/agent-docs/express.md +130 -0
- package/agent-docs/fastify.md +129 -0
- package/agent-docs/hono.md +137 -0
- package/agent-docs/next.md +94 -0
- package/agent-docs/provider-azure-blob.md +87 -0
- package/agent-docs/provider-custom.md +266 -0
- package/agent-docs/provider-edgestore.md +53 -0
- package/agent-docs/provider-s3.md +166 -0
- package/agent-docs/remix.md +123 -0
- package/agent-docs/tanstack-start.md +105 -0
- package/agent-docs/troubleshooting.md +83 -0
- package/dist/adapters/shared.js +1 -1
- package/dist/providers/edgestore/index.d.ts +4 -1
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +11 -2
- package/dist/version.d.ts +1 -1
- package/package.json +6 -4
- package/src/adapters/shared.ts +1 -1
- package/src/providers/edgestore/index.ts +9 -1
- package/src/version.ts +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @edgestore/server 1.0.0-next.4: agent references
|
|
2
|
+
|
|
3
|
+
These references ship with this exact package version. Prefer them over newer online examples.
|
|
4
|
+
Generated from the authored documentation; do not edit these files directly.
|
|
5
|
+
For other EdgeStore packages, read their own agent-docs directory and installed types.
|
|
6
|
+
|
|
7
|
+
- [next.md](next.md)
|
|
8
|
+
- [configuration.md](configuration.md)
|
|
9
|
+
- [backend-client.md](backend-client.md)
|
|
10
|
+
- [hono.md](hono.md)
|
|
11
|
+
- [tanstack-start.md](tanstack-start.md)
|
|
12
|
+
- [remix.md](remix.md)
|
|
13
|
+
- [astro.md](astro.md)
|
|
14
|
+
- [express.md](express.md)
|
|
15
|
+
- [fastify.md](fastify.md)
|
|
16
|
+
- [troubleshooting.md](troubleshooting.md)
|
|
17
|
+
- [provider-edgestore.md](provider-edgestore.md)
|
|
18
|
+
- [provider-s3.md](provider-s3.md)
|
|
19
|
+
- [provider-azure-blob.md](provider-azure-blob.md)
|
|
20
|
+
- [provider-custom.md](provider-custom.md)
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Astro
|
|
2
|
+
|
|
3
|
+
Source: https://next.edgestore.dev/docs/adapters/astro
|
|
4
|
+
|
|
5
|
+
The Astro adapter handles server requests. The upload UI below uses a hydrated
|
|
6
|
+
React component.
|
|
7
|
+
|
|
8
|
+
## Setup
|
|
9
|
+
|
|
10
|
+
### Install
|
|
11
|
+
|
|
12
|
+
Packages to install (use the application’s package manager):
|
|
13
|
+
|
|
14
|
+
```text
|
|
15
|
+
@edgestore/server @edgestore/react
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Environment variables
|
|
19
|
+
|
|
20
|
+
Copy your project's keys from the [dashboard](https://dashboard.edgestore.dev/) to the backend env file:
|
|
21
|
+
|
|
22
|
+
Example: title=".env"
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
EDGE_STORE_ACCESS_KEY=your-access-key
|
|
26
|
+
EDGE_STORE_SECRET_KEY=your-secret-key
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
> WARNING
|
|
30
|
+
>
|
|
31
|
+
> Add `.env` to `.gitignore`.
|
|
32
|
+
|
|
33
|
+
### Backend
|
|
34
|
+
|
|
35
|
+
Configure an [Astro server adapter](https://docs.astro.build/en/guides/on-demand-rendering/)
|
|
36
|
+
for your deployment and enable the React integration for the UI below. The
|
|
37
|
+
EdgeStore endpoint must run on demand; a static-only deployment cannot serve it.
|
|
38
|
+
|
|
39
|
+
Create `src/pages/api/edgestore/[...edgestore].ts`:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createEdgeStore, initEdgeStore } from '@edgestore/server';
|
|
43
|
+
import { createEdgeStoreAstroHandler } from '@edgestore/server/adapters/astro';
|
|
44
|
+
import { edgestore } from '@edgestore/server/providers/edgestore';
|
|
45
|
+
|
|
46
|
+
export const prerender = false;
|
|
47
|
+
|
|
48
|
+
const es = initEdgeStore.create();
|
|
49
|
+
|
|
50
|
+
const router = es.router({
|
|
51
|
+
publicFiles: es.fileBucket(),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const configuredEdgeStore = createEdgeStore({
|
|
55
|
+
router,
|
|
56
|
+
provider: edgestore(),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const handler = createEdgeStoreAstroHandler({
|
|
60
|
+
edgestore: configuredEdgeStore,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export { handler as GET, handler as POST };
|
|
64
|
+
|
|
65
|
+
export type EdgeStoreRouter = typeof router;
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
This bucket's files are accessible to anyone with the link.
|
|
69
|
+
|
|
70
|
+
### Frontend
|
|
71
|
+
|
|
72
|
+
Create the React provider:
|
|
73
|
+
|
|
74
|
+
Example: title="src/lib/edgestore.ts"
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import { createEdgeStoreProvider } from '@edgestore/react';
|
|
78
|
+
import type { EdgeStoreRouter } from '../pages/api/edgestore/[...edgestore].ts';
|
|
79
|
+
|
|
80
|
+
const { EdgeStoreProvider, useEdgeStore } =
|
|
81
|
+
createEdgeStoreProvider<EdgeStoreRouter>();
|
|
82
|
+
|
|
83
|
+
export { EdgeStoreProvider, useEdgeStore };
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Create the upload component:
|
|
87
|
+
|
|
88
|
+
Example: title="src/components/FileUploader.tsx"
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
import { useState } from 'react';
|
|
92
|
+
import { EdgeStoreProvider, useEdgeStore } from '../lib/edgestore';
|
|
93
|
+
|
|
94
|
+
export function FileUploader() {
|
|
95
|
+
return (
|
|
96
|
+
<EdgeStoreProvider>
|
|
97
|
+
<FileUploaderInner />
|
|
98
|
+
</EdgeStoreProvider>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function FileUploaderInner() {
|
|
103
|
+
const [file, setFile] = useState<File | null>(null);
|
|
104
|
+
const { edgestore } = useEdgeStore();
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<div>
|
|
108
|
+
<input
|
|
109
|
+
type="file"
|
|
110
|
+
onChange={(e) => {
|
|
111
|
+
setFile(e.target.files?.[0] ?? null);
|
|
112
|
+
}}
|
|
113
|
+
/>
|
|
114
|
+
<button
|
|
115
|
+
onClick={async () => {
|
|
116
|
+
if (file) {
|
|
117
|
+
const res = await edgestore.publicFiles.upload({
|
|
118
|
+
file,
|
|
119
|
+
onProgressChange: (progress) => {
|
|
120
|
+
// you can use this to show a progress bar
|
|
121
|
+
console.log(progress);
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
// you can run some server action or api here
|
|
125
|
+
// to add the necessary data to your database
|
|
126
|
+
console.log(res);
|
|
127
|
+
}
|
|
128
|
+
}}
|
|
129
|
+
>
|
|
130
|
+
Upload
|
|
131
|
+
</button>
|
|
132
|
+
</div>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Hydrate the component with `client:load`:
|
|
138
|
+
|
|
139
|
+
Example: title="src/pages/index.astro"
|
|
140
|
+
|
|
141
|
+
```astro
|
|
142
|
+
---
|
|
143
|
+
import { FileUploader } from '../components/FileUploader';
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
<html lang="en">
|
|
147
|
+
{/* ... */}
|
|
148
|
+
<body>
|
|
149
|
+
<h1>EdgeStore with Astro</h1>
|
|
150
|
+
<FileUploader client:load />
|
|
151
|
+
</body>
|
|
152
|
+
</html>
|
|
153
|
+
```
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# Backend Client
|
|
2
|
+
|
|
3
|
+
Source: https://next.edgestore.dev/docs/backend-client
|
|
4
|
+
|
|
5
|
+
Sometimes you might want to use the EdgeStore functionality directly from your backend. Things like deleting, uploading or even listing files can be done with the use of the backend client.
|
|
6
|
+
|
|
7
|
+
## Setup
|
|
8
|
+
|
|
9
|
+
Configure the router and hosted provider once. The resulting `configuredEdgeStore`
|
|
10
|
+
instance is used by the HTTP handler and exposes the type-safe backend client.
|
|
11
|
+
|
|
12
|
+
Since Next.js doesn't allow exports in the api route, you will need to move your router to an external file.
|
|
13
|
+
|
|
14
|
+
Example: title="src/lib/edgestore-server.ts"
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { createEdgeStore, initEdgeStore } from '@edgestore/server';
|
|
18
|
+
import { createEdgeStoreNextHandler } from '@edgestore/server/adapters/next/app';
|
|
19
|
+
import { edgestore } from '@edgestore/server/providers/edgestore';
|
|
20
|
+
|
|
21
|
+
const es = initEdgeStore.create();
|
|
22
|
+
|
|
23
|
+
export const router = es.router({
|
|
24
|
+
publicFiles: es.fileBucket(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const configuredEdgeStore = createEdgeStore({
|
|
28
|
+
router,
|
|
29
|
+
provider: edgestore(),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const handler = createEdgeStoreNextHandler({
|
|
33
|
+
edgestore: configuredEdgeStore,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export const backendClient = configuredEdgeStore.client;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Then you will need to update your api route to use the exported handler.
|
|
40
|
+
|
|
41
|
+
Example: title="src/app/api/edgestore/\[...edgestore]/route.ts"
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { handler } from '@/lib/edgestore-server';
|
|
45
|
+
|
|
46
|
+
export { handler as GET, handler as POST };
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
> NOTE
|
|
50
|
+
>
|
|
51
|
+
> You can find an example of the backend client usage in the
|
|
52
|
+
> [next-advanced](https://github.com/edgestorejs/edgestore/tree/main/examples/next-advanced)
|
|
53
|
+
> example.
|
|
54
|
+
|
|
55
|
+
> WARN
|
|
56
|
+
>
|
|
57
|
+
> The backend client is privileged. It validates router input and applies file
|
|
58
|
+
> type, size, transform, path, and metadata rules, but it does not run
|
|
59
|
+
> `accessControl`, `beforeUpload`, or `beforeDelete`. Perform authorization in
|
|
60
|
+
> the server code that calls it.
|
|
61
|
+
|
|
62
|
+
The hosted `edgestore()` provider supports the complete backend client. S3
|
|
63
|
+
currently exposes `get`, `delete`, and `deleteMany`. Azure Blob Storage also
|
|
64
|
+
exposes `createSignedUrl` and `createSignedUrls` for private reads. Methods such
|
|
65
|
+
as backend upload and listing remain absent from those clients until the
|
|
66
|
+
provider implements them.
|
|
67
|
+
|
|
68
|
+
## Backend Upload
|
|
69
|
+
|
|
70
|
+
You can use the `upload` function to upload files from your backend.
|
|
71
|
+
|
|
72
|
+
### Upload a text file
|
|
73
|
+
|
|
74
|
+
The simplest use case would be to just upload a `txt` file:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const res = await backendClient.publicFiles.upload({
|
|
78
|
+
content: 'some text content',
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Upload a blob
|
|
83
|
+
|
|
84
|
+
You can also upload a more complex file using the Blob object. And there are also all the other options available in the normal upload.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const res = await backendClient.publicFiles.upload({
|
|
88
|
+
content: {
|
|
89
|
+
blob: new Blob(['col1,col2,col2'], { type: 'text/csv' }),
|
|
90
|
+
extension: 'csv',
|
|
91
|
+
},
|
|
92
|
+
options: {
|
|
93
|
+
temporary: true,
|
|
94
|
+
},
|
|
95
|
+
ctx: {
|
|
96
|
+
userId: '123',
|
|
97
|
+
userRole: 'admin',
|
|
98
|
+
},
|
|
99
|
+
input: {
|
|
100
|
+
type: 'post',
|
|
101
|
+
},
|
|
102
|
+
signal,
|
|
103
|
+
onProgress: ({ percentage, phase }) => {
|
|
104
|
+
console.log(phase, `${percentage}%`);
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
console.log(res.id, res.key, res.sizeBytes);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Copy an existing file
|
|
112
|
+
|
|
113
|
+
You can use an existing file's URL to copy it into the EdgeStore bucket. This can be an external file (from outside of EdgeStore) or an existing EdgeStore file.
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const res = await backendClient.publicFiles.upload({
|
|
117
|
+
content: {
|
|
118
|
+
url: 'https://some-url.com/file.txt',
|
|
119
|
+
extension: 'txt',
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Transform a file before upload
|
|
125
|
+
|
|
126
|
+
You can transform backend uploads before EdgeStore validates and uploads them.
|
|
127
|
+
The transform receives the resolved `Blob` and extension, and returns the new
|
|
128
|
+
`Blob` and extension.
|
|
129
|
+
|
|
130
|
+
For example, you can use `sharp` to convert an image to WebP before upload:
|
|
131
|
+
|
|
132
|
+
Packages to install (use the application’s package manager):
|
|
133
|
+
|
|
134
|
+
```text
|
|
135
|
+
npm install sharp
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import sharp from 'sharp';
|
|
140
|
+
|
|
141
|
+
const res = await backendClient.publicImages.upload({
|
|
142
|
+
content: {
|
|
143
|
+
url: 'https://some-url.com/image.jpg',
|
|
144
|
+
extension: 'jpg',
|
|
145
|
+
},
|
|
146
|
+
options: {
|
|
147
|
+
transform: async ({ blob }) => {
|
|
148
|
+
const input = Buffer.from(await blob.arrayBuffer());
|
|
149
|
+
const output = await sharp(input).webp({ quality: 80 }).toBuffer();
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
blob: new Blob([output], { type: 'image/webp' }),
|
|
153
|
+
extension: 'webp',
|
|
154
|
+
};
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Confirm a temporary file upload
|
|
161
|
+
|
|
162
|
+
If you upload a temporary file, you can confirm it by using the `confirm` function.
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
const res = await backendClient.publicFiles.confirm({
|
|
166
|
+
id: file.id,
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
File operations accept a stable file ID, storage key, or URL. Singular
|
|
171
|
+
operations throw `EdgeStoreFileMutationError` when that file fails. Use the
|
|
172
|
+
plural form when partial success should be preserved:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
const result = await backendClient.publicFiles.confirmMany({
|
|
176
|
+
refs: [{ id: first.id }, { key: second.key }],
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
for (const failure of result.failed) {
|
|
180
|
+
console.error(failure.ref, failure.error.code);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Backend Delete
|
|
185
|
+
|
|
186
|
+
You can use the `delete` function to delete files from your backend.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
const res = await backendClient.publicFiles.delete({
|
|
190
|
+
id: file.id,
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
`deleteMany`, `restore`, and `restoreMany` use the same singular and
|
|
195
|
+
partial-batch semantics.
|
|
196
|
+
|
|
197
|
+
## Backend List Files (search)
|
|
198
|
+
|
|
199
|
+
You can use the `list` function to list files from your backend. It's also possible to filter the results by path, metadata or upload timing.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
// simple usage
|
|
203
|
+
// get the first 20 files in the bucket
|
|
204
|
+
const res = await backendClient.publicFiles.list();
|
|
205
|
+
|
|
206
|
+
// with filter and pagination
|
|
207
|
+
const res = await backendClient.publicFiles.list({
|
|
208
|
+
filter: {
|
|
209
|
+
metadata: {
|
|
210
|
+
role: 'admin',
|
|
211
|
+
},
|
|
212
|
+
path: {
|
|
213
|
+
type: 'post',
|
|
214
|
+
},
|
|
215
|
+
uploadedAt: {
|
|
216
|
+
gt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 7), // past 7 days
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
cursor: 'cursor-from-previous-response',
|
|
220
|
+
limit: 50, // default: 20 (max: 100)
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
for (const file of res.items) {
|
|
224
|
+
console.log(file.id, file.url);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (res.hasMore) {
|
|
228
|
+
console.log('Next cursor:', res.nextCursor);
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Private read URLs
|
|
233
|
+
|
|
234
|
+
Providers with signed-read support expose `createSignedUrl` on protected
|
|
235
|
+
buckets:
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
const access = await backendClient.privateFiles.createSignedUrl({
|
|
239
|
+
url: { id: file.id },
|
|
240
|
+
expiresIn: 15 * 60,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
console.log(access.signedUrl, access.expiresAt);
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Use `createSignedUrls` to sign several references in one provider call.
|
|
247
|
+
|
|
248
|
+
## Reusing backend client types
|
|
249
|
+
|
|
250
|
+
Derive a single method's exact input or output directly from the configured
|
|
251
|
+
client:
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
type UploadInput = Parameters<
|
|
255
|
+
typeof configuredEdgeStore.client.publicFiles.upload
|
|
256
|
+
>[0];
|
|
257
|
+
|
|
258
|
+
type UploadOutput = Awaited<
|
|
259
|
+
ReturnType<typeof configuredEdgeStore.client.publicFiles.upload>
|
|
260
|
+
>;
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
This includes the router's context, input, path, and metadata configuration and
|
|
264
|
+
the provider's file and reference types.
|
|
265
|
+
|
|
266
|
+
Use `InferClientInputs` and `InferClientOutputs` when you need a reusable map of
|
|
267
|
+
every bucket and method:
|
|
268
|
+
|
|
269
|
+
Example: title="src/lib/edgestore-server.ts"
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
import type {
|
|
273
|
+
InferClientInputs,
|
|
274
|
+
InferClientOutputs,
|
|
275
|
+
} from '@edgestore/server';
|
|
276
|
+
|
|
277
|
+
export type BackendInputs = InferClientInputs<typeof router>;
|
|
278
|
+
export type BackendOutputs = InferClientOutputs<typeof router>;
|
|
279
|
+
|
|
280
|
+
type UploadInput = BackendInputs['publicFiles']['upload'];
|
|
281
|
+
type UploadOutput = BackendOutputs['publicFiles']['upload'];
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
The default represents the hosted provider. Pass a custom provider as the
|
|
285
|
+
second generic when its capabilities or associated types differ:
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
type BackendOutputs = InferClientOutputs<
|
|
289
|
+
typeof router,
|
|
290
|
+
typeof customProvider
|
|
291
|
+
>;
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
`InferClientResponse` remains as a deprecated alias of `InferClientOutputs`.
|