@edgestore/server 1.0.0-next.2 → 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,269 @@
|
|
|
1
|
+
# Configuration
|
|
2
|
+
|
|
3
|
+
Source: https://next.edgestore.dev/docs/configuration
|
|
4
|
+
|
|
5
|
+
## Bucket Types
|
|
6
|
+
|
|
7
|
+
There are two types of file buckets: `IMAGE` and `FILE`. Both types of buckets work basically the same way, but the `IMAGE` bucket only accepts [certain mime types](https://next.edgestore.dev/docs/configuration#image-bucket-accepted-mime-types).
|
|
8
|
+
|
|
9
|
+
IMAGE buckets automatically generate a thumbnail version of the image file if the file is bigger than 200px in width or height. In case a thumbnail was generated, the url will be included in the response of the upload request.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const router = es.router({
|
|
13
|
+
publicFiles: es.fileBucket(),
|
|
14
|
+
publicImages: es.imageBucket(),
|
|
15
|
+
});
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Basic File Validation
|
|
19
|
+
|
|
20
|
+
You can set the maximum file size and the accepted mime types for every file bucket.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const router = es.router({
|
|
24
|
+
publicFiles: es.fileBucket({
|
|
25
|
+
maxSize: 1024 * 1024 * 10, // 10MB
|
|
26
|
+
accept: ['image/jpeg', 'image/png'], // wildcard also works: ['image/*']
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Context
|
|
32
|
+
|
|
33
|
+
Many of the functions that you can use to configure your file buckets receive a `context` object as an argument. This object is generated by the `createContext` function that you pass to your router configuration.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { createEdgeStore, initEdgeStore } from '@edgestore/server';
|
|
37
|
+
import {
|
|
38
|
+
type CreateContextOptions,
|
|
39
|
+
createEdgeStoreNextHandler,
|
|
40
|
+
} from '@edgestore/server/adapters/next/app';
|
|
41
|
+
import { edgestore } from '@edgestore/server/providers/edgestore';
|
|
42
|
+
import { z } from 'zod';
|
|
43
|
+
|
|
44
|
+
type Context = {
|
|
45
|
+
userId: string;
|
|
46
|
+
userRole: 'admin' | 'user';
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
async function createContext({ req }: CreateContextOptions): Promise<Context> {
|
|
50
|
+
const { id, role } = await getUserSession(req); // replace with your own session logic
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
userId: id,
|
|
54
|
+
userRole: role,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const es = initEdgeStore.context<Context>().create();
|
|
59
|
+
|
|
60
|
+
// ...
|
|
61
|
+
|
|
62
|
+
const configuredEdgeStore = createEdgeStore({
|
|
63
|
+
router,
|
|
64
|
+
provider: edgestore(),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
export default createEdgeStoreNextHandler({
|
|
68
|
+
edgestore: configuredEdgeStore,
|
|
69
|
+
/**
|
|
70
|
+
* The context is generated and saved to a cookie
|
|
71
|
+
* in the first load of the page.
|
|
72
|
+
*/
|
|
73
|
+
createContext,
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
You might need to refresh the context (e.g. when the user logs in or logs out). You can do this by calling the `reset` function from the `useEdgeStore` hook.
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
const { edgestore, reset } = useEdgeStore();
|
|
81
|
+
|
|
82
|
+
async function runAfterAuthChange() {
|
|
83
|
+
await reset(); // this will re-run the createContext function
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Metadata & File Path
|
|
88
|
+
|
|
89
|
+
Every uploaded file can hold two types of data: `metadata` and `path`. You can use this data for access control or for filtering files. The `metadata` and `path` can be generated from the context (`ctx`) or from the `input` of the upload request.
|
|
90
|
+
|
|
91
|
+
> INFO
|
|
92
|
+
>
|
|
93
|
+
> The `.input()` method accepts any object schema that implements [Standard
|
|
94
|
+
> Schema](https://standardschema.dev/), including schemas from Zod, Valibot,
|
|
95
|
+
> and ArkType.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { initEdgeStore } from '@edgestore/server';
|
|
99
|
+
import {
|
|
100
|
+
type CreateContextOptions,
|
|
101
|
+
createEdgeStoreNextHandler,
|
|
102
|
+
} from '@edgestore/server/adapters/next/app';
|
|
103
|
+
import { z } from 'zod';
|
|
104
|
+
|
|
105
|
+
type Context = {
|
|
106
|
+
userId: string;
|
|
107
|
+
userRole: 'admin' | 'user';
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
async function createContext({ req }: CreateContextOptions): Promise<Context> {
|
|
111
|
+
const { id, role } = await getUserSession(req); // replace with your own session logic
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
userId: id,
|
|
115
|
+
userRole: role,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const es = initEdgeStore.context<Context>().create();
|
|
120
|
+
|
|
121
|
+
const router = es.router({
|
|
122
|
+
publicFiles: es
|
|
123
|
+
.fileBucket()
|
|
124
|
+
// this input will be required for every upload request
|
|
125
|
+
.input(
|
|
126
|
+
z.object({
|
|
127
|
+
category: z.string(),
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
// e.g. /publicFiles/{category}/{author}
|
|
131
|
+
.path(({ ctx, input }) => [
|
|
132
|
+
{ category: input.category },
|
|
133
|
+
{ author: ctx.userId },
|
|
134
|
+
])
|
|
135
|
+
// this metadata will be added to every file in this bucket
|
|
136
|
+
.metadata(({ ctx, input }) => ({
|
|
137
|
+
userRole: ctx.userRole,
|
|
138
|
+
})),
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Lifecycle Hooks
|
|
143
|
+
|
|
144
|
+
You can use the `beforeUpload` and `beforeDelete` hooks to allow or deny file uploads and deletions. The `beforeDelete` hook must be defined if you want to delete files directly from the client.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { initEdgeStore } from '@edgestore/server';
|
|
148
|
+
import {
|
|
149
|
+
type CreateContextOptions,
|
|
150
|
+
createEdgeStoreNextHandler,
|
|
151
|
+
} from '@edgestore/server/adapters/next/app';
|
|
152
|
+
import { z } from 'zod';
|
|
153
|
+
|
|
154
|
+
type Context = {
|
|
155
|
+
userId: string;
|
|
156
|
+
userRole: 'admin' | 'user';
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
async function createContext({ req }: CreateContextOptions): Promise<Context> {
|
|
160
|
+
const { id, role } = await getUserSession(req); // replace with your own session logic
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
userId: id,
|
|
164
|
+
userRole: role,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const es = initEdgeStore.context<Context>().create();
|
|
169
|
+
|
|
170
|
+
const router = es.router({
|
|
171
|
+
publicFiles: es
|
|
172
|
+
.fileBucket()
|
|
173
|
+
/**
|
|
174
|
+
* return `true` to allow upload
|
|
175
|
+
* By default every upload from your app is allowed.
|
|
176
|
+
*/
|
|
177
|
+
.beforeUpload(({ ctx, input, fileInfo }) => {
|
|
178
|
+
console.log('beforeUpload', ctx, input, fileInfo);
|
|
179
|
+
return true; // allow upload
|
|
180
|
+
})
|
|
181
|
+
/**
|
|
182
|
+
* return `true` to allow delete
|
|
183
|
+
* This function must be defined if you want to delete files directly from the client.
|
|
184
|
+
*/
|
|
185
|
+
.beforeDelete(({ ctx, fileInfo }) => {
|
|
186
|
+
console.log('beforeDelete', ctx, fileInfo);
|
|
187
|
+
return true; // allow delete
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Access Control (Experimental)
|
|
193
|
+
|
|
194
|
+
You can use the `accessControl` function to add bucket level logic to allow or deny access to files. If you have ever used Prisma, you will probably notice that the structure of the `accessControl` function is similar to how you would write a Prisma query.
|
|
195
|
+
|
|
196
|
+
If you set the `accessControl` function, your bucket will automatically be configured as a **protected bucket**. You cannot change a protected bucket to a public bucket after it has been created. The opposite is also true, you cannot change a public bucket to a protected bucket.
|
|
197
|
+
|
|
198
|
+
To access files from a **protected bucket** the user will need a specific encrypted cookie generated in your server by the EdgeStore package. Which means that they will only be able to access the files from within your app. Sharing the url of a protected file will not work.
|
|
199
|
+
|
|
200
|
+
The access control check is performed on an edge function without running any database queries, so you won't need to worry about bad performance on your protected files.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const filesBucket = es
|
|
204
|
+
.fileBucket()
|
|
205
|
+
.path(({ ctx }) => [{ author: ctx.userId }])
|
|
206
|
+
.accessControl({
|
|
207
|
+
OR: [
|
|
208
|
+
{
|
|
209
|
+
// this will make sure that only the author of the file can access it
|
|
210
|
+
userId: { path: 'author' },
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
// or if the user is an admin
|
|
214
|
+
userRole: {
|
|
215
|
+
eq: 'admin',
|
|
216
|
+
}, // same as { userRole: 'admin' }
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Other available operators are: `eq`, `not`, `gt`, `gte`, `lt`, `lte`, `in`, `contains`
|
|
223
|
+
|
|
224
|
+
> NOTE
|
|
225
|
+
>
|
|
226
|
+
> The access control functionality uses third party cookies. Since third party cookies are not supported in localhost (without https), in development, all the protected files will be proxied through your app's api so that the cookies can be forwarded to the file request.
|
|
227
|
+
>
|
|
228
|
+
> Also, the `<Image />` component from `next/image` does not forward the cookies in the request, so protected images won't be displayed. You will need ot use the `<img />` tag instead.
|
|
229
|
+
|
|
230
|
+
## Limit parallel uploads
|
|
231
|
+
|
|
232
|
+
When creating the provider, you can set the maximum number of concurrent uploads.
|
|
233
|
+
EdgeStore's context provider will take care of queuing the uploads and will automatically upload the next file when the previous one is finished.
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
const { EdgeStoreProvider, useEdgeStore } =
|
|
237
|
+
createEdgeStoreProvider<EdgeStoreRouter>({
|
|
238
|
+
maxConcurrentUploads: 5, // default is 5
|
|
239
|
+
});
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Base Path
|
|
243
|
+
|
|
244
|
+
In case your app is not hosted at the root of your domain, you can specify the base path.
|
|
245
|
+
If you set this, make sure to set the full path to the EdgeStore API.
|
|
246
|
+
e.g. `/my-app/api/edgestore` or `https://example.com/my-app/api/edgestore`
|
|
247
|
+
|
|
248
|
+
```tsx
|
|
249
|
+
export default function App({ Component, pageProps }: AppProps) {
|
|
250
|
+
return (
|
|
251
|
+
<EdgeStoreProvider basePath="/my-app/api/edgestore">
|
|
252
|
+
<Component {...pageProps} />
|
|
253
|
+
</EdgeStoreProvider>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## IMAGE bucket accepted mime types
|
|
259
|
+
|
|
260
|
+
| mime type |
|
|
261
|
+
| ------------- |
|
|
262
|
+
| image/jpeg |
|
|
263
|
+
| image/png |
|
|
264
|
+
| image/gif |
|
|
265
|
+
| image/webp |
|
|
266
|
+
| image/svg+xml |
|
|
267
|
+
| image/tiff |
|
|
268
|
+
| image/bmp |
|
|
269
|
+
| image/x-icon |
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Express
|
|
2
|
+
|
|
3
|
+
Source: https://next.edgestore.dev/docs/adapters/express
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
|
|
9
|
+
Packages to install (use the application’s package manager):
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
@edgestore/server @edgestore/react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Environment variables
|
|
16
|
+
|
|
17
|
+
Copy your project's keys from the [dashboard](https://dashboard.edgestore.dev/) to the backend env file:
|
|
18
|
+
|
|
19
|
+
Example: title=".env"
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
EDGE_STORE_ACCESS_KEY=your-access-key
|
|
23
|
+
EDGE_STORE_SECRET_KEY=your-secret-key
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> WARNING
|
|
27
|
+
>
|
|
28
|
+
> Add `.env` to `.gitignore`.
|
|
29
|
+
|
|
30
|
+
### Backend
|
|
31
|
+
|
|
32
|
+
This example uses Express 5. Keep your existing middleware and load the backend
|
|
33
|
+
env file before constructing the provider; Express does not load it automatically.
|
|
34
|
+
|
|
35
|
+
This bucket's files are accessible to anyone with the link.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createEdgeStore, initEdgeStore } from '@edgestore/server';
|
|
39
|
+
import { createEdgeStoreExpressHandler } from '@edgestore/server/adapters/express';
|
|
40
|
+
import { edgestore } from '@edgestore/server/providers/edgestore';
|
|
41
|
+
import cookieParser from 'cookie-parser';
|
|
42
|
+
import cors from 'cors';
|
|
43
|
+
import express from 'express';
|
|
44
|
+
|
|
45
|
+
const PORT = process.env.PORT ?? 3001;
|
|
46
|
+
|
|
47
|
+
const app = express();
|
|
48
|
+
|
|
49
|
+
app.use(
|
|
50
|
+
cors({
|
|
51
|
+
// Replace with your frontend origin.
|
|
52
|
+
origin: 'http://localhost:5173',
|
|
53
|
+
credentials: true,
|
|
54
|
+
}),
|
|
55
|
+
);
|
|
56
|
+
app.use(cookieParser());
|
|
57
|
+
app.use(express.json());
|
|
58
|
+
|
|
59
|
+
const es = initEdgeStore.create();
|
|
60
|
+
|
|
61
|
+
const router = es.router({
|
|
62
|
+
publicFiles: es.fileBucket(),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export type EdgeStoreRouter = typeof router;
|
|
66
|
+
|
|
67
|
+
const configuredEdgeStore = createEdgeStore({
|
|
68
|
+
router,
|
|
69
|
+
provider: edgestore(),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const handler = createEdgeStoreExpressHandler({
|
|
73
|
+
edgestore: configuredEdgeStore,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
app.get('/edgestore/*splat', handler);
|
|
77
|
+
app.post('/edgestore/*splat', handler);
|
|
78
|
+
|
|
79
|
+
app.listen(PORT, () => {
|
|
80
|
+
console.log(`Server running at http://localhost:${PORT}`);
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Express 5 requires a named wildcard. On Express 4, use `/edgestore/*` instead.
|
|
85
|
+
See [Express 5 route syntax](https://expressjs.com/en/guide/migrating-5.html#path-syntax).
|
|
86
|
+
|
|
87
|
+
### Frontend
|
|
88
|
+
|
|
89
|
+
Create the React provider:
|
|
90
|
+
|
|
91
|
+
Example: title="src/lib/edgestore.ts"
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
// Import the router type from your backend.
|
|
95
|
+
import type { EdgeStoreRouter } from '../../../path/to/express-backend/src';
|
|
96
|
+
import { createEdgeStoreProvider } from '@edgestore/react';
|
|
97
|
+
|
|
98
|
+
const { EdgeStoreProvider, useEdgeStore } =
|
|
99
|
+
createEdgeStoreProvider<EdgeStoreRouter>();
|
|
100
|
+
|
|
101
|
+
export { EdgeStoreProvider, useEdgeStore };
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Wrap your app with the provider:
|
|
105
|
+
|
|
106
|
+
Example: title="src/App.tsx"
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
// [!code ++]
|
|
110
|
+
import { EdgeStoreProvider } from '../lib/edgestore';
|
|
111
|
+
|
|
112
|
+
function App() {
|
|
113
|
+
return (
|
|
114
|
+
<EdgeStoreProvider basePath="http://localhost:3001/edgestore">
|
|
115
|
+
{/* Rest of your app */}
|
|
116
|
+
</EdgeStoreProvider>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Usage
|
|
122
|
+
|
|
123
|
+
See [Quick start](https://next.edgestore.dev/docs/quick-start) for the upload UI.
|
|
124
|
+
|
|
125
|
+
## Limitations
|
|
126
|
+
|
|
127
|
+
> WARNING
|
|
128
|
+
>
|
|
129
|
+
> In production, serve the frontend and backend on the same domain. Use
|
|
130
|
+
> [Vercel rewrites](https://vercel.com/docs/edge-network/rewrites) or a reverse proxy.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Fastify
|
|
2
|
+
|
|
3
|
+
Source: https://next.edgestore.dev/docs/adapters/fastify
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
|
|
9
|
+
Packages to install (use the application’s package manager):
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
@edgestore/server @edgestore/react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Environment variables
|
|
16
|
+
|
|
17
|
+
Copy your project's keys from the [dashboard](https://dashboard.edgestore.dev/) to the backend env file:
|
|
18
|
+
|
|
19
|
+
Example: title=".env"
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
EDGE_STORE_ACCESS_KEY=your-access-key
|
|
23
|
+
EDGE_STORE_SECRET_KEY=your-secret-key
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> WARNING
|
|
27
|
+
>
|
|
28
|
+
> Add `.env` to `.gitignore`.
|
|
29
|
+
|
|
30
|
+
### Backend
|
|
31
|
+
|
|
32
|
+
Load the backend env file before constructing the provider; Fastify does not
|
|
33
|
+
load it automatically. Reuse existing cookie and CORS registrations.
|
|
34
|
+
|
|
35
|
+
This bucket's files are accessible to anyone with the link.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createEdgeStore, initEdgeStore } from '@edgestore/server';
|
|
39
|
+
import { createEdgeStoreFastifyHandler } from '@edgestore/server/adapters/fastify';
|
|
40
|
+
import { edgestore } from '@edgestore/server/providers/edgestore';
|
|
41
|
+
import cors from '@fastify/cors';
|
|
42
|
+
import cookie from '@fastify/cookie';
|
|
43
|
+
import fastify from 'fastify';
|
|
44
|
+
|
|
45
|
+
const PORT = process.env.PORT ?? 3001;
|
|
46
|
+
|
|
47
|
+
const app = fastify();
|
|
48
|
+
|
|
49
|
+
await app.register(cors, {
|
|
50
|
+
// Replace with your frontend origin.
|
|
51
|
+
origin: 'http://localhost:5173',
|
|
52
|
+
credentials: true,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
await app.register(cookie);
|
|
56
|
+
|
|
57
|
+
const es = initEdgeStore.create();
|
|
58
|
+
|
|
59
|
+
const router = es.router({
|
|
60
|
+
publicFiles: es.fileBucket(),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export type EdgeStoreRouter = typeof router;
|
|
64
|
+
|
|
65
|
+
const configuredEdgeStore = createEdgeStore({
|
|
66
|
+
router,
|
|
67
|
+
provider: edgestore(),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const handler = createEdgeStoreFastifyHandler({
|
|
71
|
+
edgestore: configuredEdgeStore,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
app.get('/edgestore/*', handler);
|
|
75
|
+
app.post('/edgestore/*', handler);
|
|
76
|
+
|
|
77
|
+
app.listen({ port: PORT }, (err) => {
|
|
78
|
+
if (err) {
|
|
79
|
+
console.error(err);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
console.log(`Server running at http://localhost:${PORT}`);
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Frontend
|
|
87
|
+
|
|
88
|
+
Create the React provider:
|
|
89
|
+
|
|
90
|
+
Example: title="src/lib/edgestore.ts"
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
// Import the router type from your backend.
|
|
94
|
+
import type { EdgeStoreRouter } from '../../../path/to/fastify-backend/src';
|
|
95
|
+
import { createEdgeStoreProvider } from '@edgestore/react';
|
|
96
|
+
|
|
97
|
+
const { EdgeStoreProvider, useEdgeStore } =
|
|
98
|
+
createEdgeStoreProvider<EdgeStoreRouter>();
|
|
99
|
+
|
|
100
|
+
export { EdgeStoreProvider, useEdgeStore };
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Wrap your app with the provider:
|
|
104
|
+
|
|
105
|
+
Example: title="src/App.tsx"
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
// [!code ++]
|
|
109
|
+
import { EdgeStoreProvider } from '../lib/edgestore';
|
|
110
|
+
|
|
111
|
+
function App() {
|
|
112
|
+
return (
|
|
113
|
+
<EdgeStoreProvider basePath="http://localhost:3001/edgestore">
|
|
114
|
+
{/* Rest of your app */}
|
|
115
|
+
</EdgeStoreProvider>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Usage
|
|
121
|
+
|
|
122
|
+
See [Quick start](https://next.edgestore.dev/docs/quick-start) for the upload UI.
|
|
123
|
+
|
|
124
|
+
## Limitations
|
|
125
|
+
|
|
126
|
+
> WARNING
|
|
127
|
+
>
|
|
128
|
+
> In production, serve the frontend and backend on the same domain. Use
|
|
129
|
+
> [Vercel rewrites](https://vercel.com/docs/edge-network/rewrites) or a reverse proxy.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# Hono
|
|
2
|
+
|
|
3
|
+
Source: https://next.edgestore.dev/docs/adapters/hono
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
|
|
9
|
+
Install `@edgestore/server` in the backend and `@edgestore/react` in the frontend.
|
|
10
|
+
|
|
11
|
+
Packages to install (use the application’s package manager):
|
|
12
|
+
|
|
13
|
+
```text
|
|
14
|
+
@edgestore/server @edgestore/react
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Environment variables
|
|
18
|
+
|
|
19
|
+
Copy your project's keys from the [dashboard](https://dashboard.edgestore.dev/) to the backend env file:
|
|
20
|
+
|
|
21
|
+
Example: title=".env.local (backend only)"
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
EDGE_STORE_ACCESS_KEY=your-access-key
|
|
25
|
+
EDGE_STORE_SECRET_KEY=your-secret-key
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
> WARNING
|
|
29
|
+
>
|
|
30
|
+
> Load the env file in the backend before creating the provider, and keep it gitignored.
|
|
31
|
+
> The [Hono example](https://github.com/edgestorejs/edgestore/tree/main/examples/hono-basic) loads `.env.local` in its dev command.
|
|
32
|
+
|
|
33
|
+
### Backend
|
|
34
|
+
|
|
35
|
+
This bucket's files are accessible to anyone with the link.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createEdgeStore, initEdgeStore } from '@edgestore/server';
|
|
39
|
+
import { createEdgeStoreHonoHandler } from '@edgestore/server/adapters/hono';
|
|
40
|
+
import { edgestore } from '@edgestore/server/providers/edgestore';
|
|
41
|
+
import { serve } from '@hono/node-server';
|
|
42
|
+
import { Hono } from 'hono';
|
|
43
|
+
import { cors } from 'hono/cors';
|
|
44
|
+
|
|
45
|
+
const PORT = process.env.PORT ?? 3001;
|
|
46
|
+
const frontendOrigin = process.env.FRONTEND_ORIGIN ?? 'http://localhost:5173';
|
|
47
|
+
const app = new Hono();
|
|
48
|
+
|
|
49
|
+
app.use(
|
|
50
|
+
'/edgestore/*',
|
|
51
|
+
cors({
|
|
52
|
+
origin: frontendOrigin,
|
|
53
|
+
credentials: true,
|
|
54
|
+
}),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const es = initEdgeStore.create();
|
|
58
|
+
|
|
59
|
+
const router = es.router({
|
|
60
|
+
publicFiles: es.fileBucket(),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export type EdgeStoreRouter = typeof router;
|
|
64
|
+
|
|
65
|
+
const configuredEdgeStore = createEdgeStore({
|
|
66
|
+
router,
|
|
67
|
+
provider: edgestore(),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const handler = createEdgeStoreHonoHandler({
|
|
71
|
+
edgestore: configuredEdgeStore,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
app.all('/edgestore/*', handler);
|
|
75
|
+
|
|
76
|
+
serve(
|
|
77
|
+
{
|
|
78
|
+
fetch: app.fetch,
|
|
79
|
+
port: Number(PORT),
|
|
80
|
+
},
|
|
81
|
+
(info) => {
|
|
82
|
+
console.log(`Server running at http://localhost:${info.port}`);
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Frontend
|
|
88
|
+
|
|
89
|
+
Create the React provider:
|
|
90
|
+
|
|
91
|
+
Example: title="src/lib/edgestore.ts"
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
// Import the router type from your backend.
|
|
95
|
+
import { type EdgeStoreRouter } from '../../../path/to/hono-backend/src';
|
|
96
|
+
import { createEdgeStoreProvider } from '@edgestore/react';
|
|
97
|
+
|
|
98
|
+
const { EdgeStoreProvider, useEdgeStore } =
|
|
99
|
+
createEdgeStoreProvider<EdgeStoreRouter>();
|
|
100
|
+
|
|
101
|
+
export { EdgeStoreProvider, useEdgeStore };
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Wrap your app with the provider:
|
|
105
|
+
|
|
106
|
+
Example: title="src/App.tsx"
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
// [!code ++]
|
|
110
|
+
import { EdgeStoreProvider } from '../lib/edgestore';
|
|
111
|
+
|
|
112
|
+
function App() {
|
|
113
|
+
return (
|
|
114
|
+
<EdgeStoreProvider basePath={import.meta.env.VITE_EDGESTORE_BASE_PATH ?? '/edgestore'}>
|
|
115
|
+
{/* Rest of your app */}
|
|
116
|
+
</EdgeStoreProvider>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
For separate Vite and Hono dev servers, set these values to match your ports:
|
|
122
|
+
|
|
123
|
+
* Frontend: `VITE_EDGESTORE_BASE_PATH=http://localhost:3001/edgestore`
|
|
124
|
+
* Backend: `FRONTEND_ORIGIN=http://localhost:5173`
|
|
125
|
+
|
|
126
|
+
Across repositories, share the backend router's type through a package.
|
|
127
|
+
|
|
128
|
+
## Usage
|
|
129
|
+
|
|
130
|
+
See [Quick start](https://next.edgestore.dev/docs/quick-start) for the upload UI.
|
|
131
|
+
|
|
132
|
+
## Limitations
|
|
133
|
+
|
|
134
|
+
> WARNING
|
|
135
|
+
>
|
|
136
|
+
> In production, serve the frontend and backend on the same domain. Use
|
|
137
|
+
> [Vercel rewrites](https://vercel.com/docs/edge-network/rewrites) or a reverse proxy.
|