@edgestore/server 1.0.0-next.3 → 1.0.0-next.5

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.
Files changed (115) hide show
  1. package/README.md +2 -8
  2. package/agent-docs/README.md +20 -0
  3. package/agent-docs/astro.md +147 -0
  4. package/agent-docs/backend-client.md +284 -0
  5. package/agent-docs/configuration.md +300 -0
  6. package/agent-docs/express.md +124 -0
  7. package/agent-docs/fastify.md +123 -0
  8. package/agent-docs/hono.md +131 -0
  9. package/agent-docs/next.md +82 -0
  10. package/agent-docs/provider-azure-blob.md +85 -0
  11. package/agent-docs/provider-custom.md +281 -0
  12. package/agent-docs/provider-edgestore.md +53 -0
  13. package/agent-docs/provider-s3.md +270 -0
  14. package/agent-docs/remix.md +117 -0
  15. package/agent-docs/tanstack-start.md +99 -0
  16. package/agent-docs/troubleshooting.md +83 -0
  17. package/dist/adapters/astro/index.d.ts +2 -2
  18. package/dist/adapters/astro/index.d.ts.map +1 -1
  19. package/dist/adapters/astro/index.js +1 -2
  20. package/dist/adapters/dispatcher.d.ts +2 -3
  21. package/dist/adapters/dispatcher.d.ts.map +1 -1
  22. package/dist/adapters/dispatcher.js +17 -22
  23. package/dist/adapters/express/index.d.ts +2 -2
  24. package/dist/adapters/express/index.d.ts.map +1 -1
  25. package/dist/adapters/express/index.js +1 -5
  26. package/dist/adapters/fastify/index.d.ts +2 -2
  27. package/dist/adapters/fastify/index.d.ts.map +1 -1
  28. package/dist/adapters/fastify/index.js +1 -5
  29. package/dist/adapters/hono/index.d.ts +2 -2
  30. package/dist/adapters/hono/index.d.ts.map +1 -1
  31. package/dist/adapters/hono/index.js +1 -2
  32. package/dist/adapters/next/app/index.d.ts +2 -2
  33. package/dist/adapters/next/app/index.d.ts.map +1 -1
  34. package/dist/adapters/next/app/index.js +1 -2
  35. package/dist/adapters/next/pages/index.d.ts +2 -2
  36. package/dist/adapters/next/pages/index.d.ts.map +1 -1
  37. package/dist/adapters/next/pages/index.js +1 -5
  38. package/dist/adapters/remix/index.d.ts +2 -2
  39. package/dist/adapters/remix/index.d.ts.map +1 -1
  40. package/dist/adapters/remix/index.js +1 -2
  41. package/dist/adapters/shared.d.ts +45 -67
  42. package/dist/adapters/shared.d.ts.map +1 -1
  43. package/dist/adapters/shared.js +60 -97
  44. package/dist/adapters/start/index.d.ts +2 -2
  45. package/dist/adapters/start/index.d.ts.map +1 -1
  46. package/dist/adapters/start/index.js +1 -2
  47. package/dist/core/client/bucketClient.d.ts +0 -1
  48. package/dist/core/client/bucketClient.d.ts.map +1 -1
  49. package/dist/core/client/bucketClient.js +6 -28
  50. package/dist/core/client/index.d.ts +29 -26
  51. package/dist/core/client/index.d.ts.map +1 -1
  52. package/dist/core/client/index.js +2 -3
  53. package/dist/core/index.d.ts +2 -20
  54. package/dist/core/index.d.ts.map +1 -1
  55. package/dist/core/index.js +2 -13
  56. package/dist/core/provider.d.ts +11 -1
  57. package/dist/core/provider.d.ts.map +1 -1
  58. package/dist/core/provider.js +21 -1
  59. package/dist/core/router.d.ts +25 -0
  60. package/dist/core/router.d.ts.map +1 -0
  61. package/dist/core/router.js +49 -0
  62. package/dist/core/routerRules.d.ts +1 -1
  63. package/dist/index.d.ts +2 -2
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +1 -2
  66. package/dist/providers/azure-blob/index.d.ts +7 -0
  67. package/dist/providers/azure-blob/index.d.ts.map +1 -1
  68. package/dist/providers/azure-blob/index.js +7 -0
  69. package/dist/providers/edgestore/index.d.ts +10 -11
  70. package/dist/providers/edgestore/index.d.ts.map +1 -1
  71. package/dist/providers/edgestore/index.js +26 -15
  72. package/dist/providers/s3/backendUpload.d.ts +13 -0
  73. package/dist/providers/s3/backendUpload.d.ts.map +1 -0
  74. package/dist/providers/s3/backendUpload.js +97 -0
  75. package/dist/providers/s3/index.d.ts +57 -93
  76. package/dist/providers/s3/index.d.ts.map +1 -1
  77. package/dist/providers/s3/index.js +241 -90
  78. package/dist/providers/s3/keys.d.ts +10 -0
  79. package/dist/providers/s3/keys.d.ts.map +1 -0
  80. package/dist/providers/s3/keys.js +40 -0
  81. package/dist/providers/s3/multipart.d.ts +25 -0
  82. package/dist/providers/s3/multipart.d.ts.map +1 -0
  83. package/dist/providers/s3/multipart.js +132 -0
  84. package/dist/providers/s3/objectHeaders.d.ts +4 -0
  85. package/dist/providers/s3/objectHeaders.d.ts.map +1 -0
  86. package/dist/providers/s3/objectHeaders.js +22 -0
  87. package/dist/providers/s3/options.d.ts +107 -0
  88. package/dist/providers/s3/options.d.ts.map +1 -0
  89. package/dist/version.d.ts +1 -1
  90. package/package.json +7 -5
  91. package/src/adapters/astro/index.ts +3 -4
  92. package/src/adapters/dispatcher.ts +21 -30
  93. package/src/adapters/express/index.ts +3 -9
  94. package/src/adapters/fastify/index.ts +3 -9
  95. package/src/adapters/hono/index.ts +3 -4
  96. package/src/adapters/next/app/index.ts +3 -4
  97. package/src/adapters/next/pages/index.ts +3 -9
  98. package/src/adapters/remix/index.ts +3 -4
  99. package/src/adapters/shared.ts +90 -163
  100. package/src/adapters/start/index.ts +3 -4
  101. package/src/core/client/bucketClient.ts +10 -41
  102. package/src/core/client/index.ts +32 -48
  103. package/src/core/index.ts +2 -36
  104. package/src/core/provider.ts +28 -5
  105. package/src/core/router.ts +97 -0
  106. package/src/index.ts +1 -3
  107. package/src/providers/azure-blob/index.ts +2 -0
  108. package/src/providers/edgestore/index.ts +28 -14
  109. package/src/providers/s3/backendUpload.ts +117 -0
  110. package/src/providers/s3/index.ts +302 -223
  111. package/src/providers/s3/keys.ts +59 -0
  112. package/src/providers/s3/multipart.ts +196 -0
  113. package/src/providers/s3/objectHeaders.ts +23 -0
  114. package/src/providers/s3/options.ts +125 -0
  115. package/src/version.ts +1 -1
@@ -0,0 +1,300 @@
1
+ # Configuration
2
+
3
+ Source: https://next.edgestore.dev/docs/configuration
4
+
5
+ ## Router and Provider
6
+
7
+ Import `initEdgeStore` from `@edgestore/server`. The router uses the hosted
8
+ EdgeStore provider by default and exposes its backend client as `router.client`:
9
+
10
+ ```ts
11
+ const es = initEdgeStore.create();
12
+ const router = es.router({
13
+ publicFiles: es.fileBucket(),
14
+ });
15
+
16
+ const handler = createEdgeStoreNextHandler({ router });
17
+ const backendClient = router.client;
18
+ ```
19
+
20
+ Chain `.provider()` to use another provider or custom credentials:
21
+
22
+ ```ts
23
+ import { s3 } from '@edgestore/server/providers/s3';
24
+
25
+ const router = es.router({
26
+ publicFiles: es.fileBucket(),
27
+ }).provider(s3());
28
+ ```
29
+
30
+ `.provider()` returns a new router. It preserves bucket definitions, while
31
+ leaving existing routers, clients, and handlers bound to their original
32
+ provider. The backend client exposes only methods supported by the
33
+ selected provider.
34
+
35
+ The default hosted provider is initialized when handling requests or when
36
+ `router.client` is first accessed. Defining a router or overriding its provider
37
+ does not require hosted EdgeStore credentials. Each router reuses its provider
38
+ and backend client after initialization.
39
+
40
+ Keep the router object on the server; import only its type into frontend code.
41
+
42
+ ## Bucket Types
43
+
44
+ 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).
45
+
46
+ 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.
47
+
48
+ ```ts
49
+ const router = es.router({
50
+ publicFiles: es.fileBucket(),
51
+ publicImages: es.imageBucket(),
52
+ });
53
+ ```
54
+
55
+ ## Basic File Validation
56
+
57
+ You can set the maximum file size and the accepted mime types for every file bucket.
58
+
59
+ ```ts
60
+ const router = es.router({
61
+ publicFiles: es.fileBucket({
62
+ maxSize: 1024 * 1024 * 10, // 10MB
63
+ accept: ['image/jpeg', 'image/png'], // wildcard also works: ['image/*']
64
+ }),
65
+ });
66
+ ```
67
+
68
+ ## Context
69
+
70
+ 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.
71
+
72
+ ```ts
73
+ import { initEdgeStore } from '@edgestore/server';
74
+ import {
75
+ type CreateContextOptions,
76
+ createEdgeStoreNextHandler,
77
+ } from '@edgestore/server/adapters/next/app';
78
+ import { z } from 'zod';
79
+
80
+ type Context = {
81
+ userId: string;
82
+ userRole: 'admin' | 'user';
83
+ };
84
+
85
+ async function createContext({ req }: CreateContextOptions): Promise<Context> {
86
+ const { id, role } = await getUserSession(req); // replace with your own session logic
87
+
88
+ return {
89
+ userId: id,
90
+ userRole: role,
91
+ };
92
+ }
93
+
94
+ const es = initEdgeStore.context<Context>().create();
95
+
96
+ // ...
97
+
98
+ export default createEdgeStoreNextHandler({
99
+ router,
100
+ /**
101
+ * The context is generated and saved to a cookie
102
+ * in the first load of the page.
103
+ */
104
+ createContext,
105
+ });
106
+ ```
107
+
108
+ 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.
109
+
110
+ ```tsx
111
+ const { edgestore, reset } = useEdgeStore();
112
+
113
+ async function runAfterAuthChange() {
114
+ await reset(); // this will re-run the createContext function
115
+ }
116
+ ```
117
+
118
+ ## Metadata & File Path
119
+
120
+ 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.
121
+
122
+ > INFO
123
+ >
124
+ > The `.input()` method accepts any object schema that implements [Standard
125
+ > Schema](https://standardschema.dev/), including schemas from Zod, Valibot,
126
+ > and ArkType.
127
+
128
+ ```ts
129
+ import { initEdgeStore } from '@edgestore/server';
130
+ import {
131
+ type CreateContextOptions,
132
+ createEdgeStoreNextHandler,
133
+ } from '@edgestore/server/adapters/next/app';
134
+ import { z } from 'zod';
135
+
136
+ type Context = {
137
+ userId: string;
138
+ userRole: 'admin' | 'user';
139
+ };
140
+
141
+ async function createContext({ req }: CreateContextOptions): Promise<Context> {
142
+ const { id, role } = await getUserSession(req); // replace with your own session logic
143
+
144
+ return {
145
+ userId: id,
146
+ userRole: role,
147
+ };
148
+ }
149
+
150
+ const es = initEdgeStore.context<Context>().create();
151
+
152
+ const router = es.router({
153
+ publicFiles: es
154
+ .fileBucket()
155
+ // this input will be required for every upload request
156
+ .input(
157
+ z.object({
158
+ category: z.string(),
159
+ }),
160
+ )
161
+ // e.g. /publicFiles/{category}/{author}
162
+ .path(({ ctx, input }) => [
163
+ { category: input.category },
164
+ { author: ctx.userId },
165
+ ])
166
+ // this metadata will be added to every file in this bucket
167
+ .metadata(({ ctx, input }) => ({
168
+ userRole: ctx.userRole,
169
+ })),
170
+ });
171
+ ```
172
+
173
+ ## Lifecycle Hooks
174
+
175
+ 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.
176
+
177
+ ```ts
178
+ import { initEdgeStore } from '@edgestore/server';
179
+ import {
180
+ type CreateContextOptions,
181
+ createEdgeStoreNextHandler,
182
+ } from '@edgestore/server/adapters/next/app';
183
+ import { z } from 'zod';
184
+
185
+ type Context = {
186
+ userId: string;
187
+ userRole: 'admin' | 'user';
188
+ };
189
+
190
+ async function createContext({ req }: CreateContextOptions): Promise<Context> {
191
+ const { id, role } = await getUserSession(req); // replace with your own session logic
192
+
193
+ return {
194
+ userId: id,
195
+ userRole: role,
196
+ };
197
+ }
198
+
199
+ const es = initEdgeStore.context<Context>().create();
200
+
201
+ const router = es.router({
202
+ publicFiles: es
203
+ .fileBucket()
204
+ /**
205
+ * return `true` to allow upload
206
+ * By default every upload from your app is allowed.
207
+ */
208
+ .beforeUpload(({ ctx, input, fileInfo }) => {
209
+ console.log('beforeUpload', ctx, input, fileInfo);
210
+ return true; // allow upload
211
+ })
212
+ /**
213
+ * return `true` to allow delete
214
+ * This function must be defined if you want to delete files directly from the client.
215
+ */
216
+ .beforeDelete(({ ctx, fileInfo }) => {
217
+ console.log('beforeDelete', ctx, fileInfo);
218
+ return true; // allow delete
219
+ }),
220
+ });
221
+ ```
222
+
223
+ ## Access Control (Experimental)
224
+
225
+ 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.
226
+
227
+ 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.
228
+
229
+ 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.
230
+
231
+ 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.
232
+
233
+ ```ts
234
+ const filesBucket = es
235
+ .fileBucket()
236
+ .path(({ ctx }) => [{ author: ctx.userId }])
237
+ .accessControl({
238
+ OR: [
239
+ {
240
+ // this will make sure that only the author of the file can access it
241
+ userId: { path: 'author' },
242
+ },
243
+ {
244
+ // or if the user is an admin
245
+ userRole: {
246
+ eq: 'admin',
247
+ }, // same as { userRole: 'admin' }
248
+ },
249
+ ],
250
+ });
251
+ ```
252
+
253
+ Other available operators are: `eq`, `not`, `gt`, `gte`, `lt`, `lte`, `in`, `contains`
254
+
255
+ > NOTE
256
+ >
257
+ > The access control functionality uses a partitioned cookie that EdgeStore sets on the file origin when the provider initializes. Protected files load directly from that origin, including in local development.
258
+ >
259
+ > The `<Image />` component from `next/image` does not forward the cookies in the request, so protected images won't be displayed. You will need to use the `<img />` tag instead.
260
+
261
+ ## Limit parallel uploads
262
+
263
+ When creating the provider, you can set the maximum number of concurrent uploads.
264
+ EdgeStore's context provider will take care of queuing the uploads and will automatically upload the next file when the previous one is finished.
265
+
266
+ ```ts
267
+ const { EdgeStoreProvider, useEdgeStore } =
268
+ createEdgeStoreProvider<EdgeStoreRouter>({
269
+ maxConcurrentUploads: 5, // default is 5
270
+ });
271
+ ```
272
+
273
+ ## Base Path
274
+
275
+ In case your app is not hosted at the root of your domain, you can specify the base path.
276
+ If you set this, make sure to set the full path to the EdgeStore API.
277
+ e.g. `/my-app/api/edgestore` or `https://example.com/my-app/api/edgestore`
278
+
279
+ ```tsx
280
+ export default function App({ Component, pageProps }: AppProps) {
281
+ return (
282
+ <EdgeStoreProvider basePath="/my-app/api/edgestore">
283
+ <Component {...pageProps} />
284
+ </EdgeStoreProvider>
285
+ );
286
+ }
287
+ ```
288
+
289
+ ## IMAGE bucket accepted mime types
290
+
291
+ | mime type |
292
+ | ------------- |
293
+ | image/jpeg |
294
+ | image/png |
295
+ | image/gif |
296
+ | image/webp |
297
+ | image/svg+xml |
298
+ | image/tiff |
299
+ | image/bmp |
300
+ | image/x-icon |
@@ -0,0 +1,124 @@
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 { initEdgeStore } from '@edgestore/server';
39
+ import { createEdgeStoreExpressHandler } from '@edgestore/server/adapters/express';
40
+ import cookieParser from 'cookie-parser';
41
+ import cors from 'cors';
42
+ import express from 'express';
43
+
44
+ const PORT = process.env.PORT ?? 3001;
45
+
46
+ const app = express();
47
+
48
+ app.use(
49
+ cors({
50
+ // Replace with your frontend origin.
51
+ origin: 'http://localhost:5173',
52
+ credentials: true,
53
+ }),
54
+ );
55
+ app.use(cookieParser());
56
+ app.use(express.json());
57
+
58
+ const es = initEdgeStore.create();
59
+
60
+ const router = es.router({
61
+ publicFiles: es.fileBucket(),
62
+ });
63
+
64
+ export type EdgeStoreRouter = typeof router;
65
+
66
+ const handler = createEdgeStoreExpressHandler({
67
+ router,
68
+ });
69
+
70
+ app.get('/edgestore/*splat', handler);
71
+ app.post('/edgestore/*splat', handler);
72
+
73
+ app.listen(PORT, () => {
74
+ console.log(`Server running at http://localhost:${PORT}`);
75
+ });
76
+ ```
77
+
78
+ Express 5 requires a named wildcard. On Express 4, use `/edgestore/*` instead.
79
+ See [Express 5 route syntax](https://expressjs.com/en/guide/migrating-5.html#path-syntax).
80
+
81
+ ### Frontend
82
+
83
+ Create the React provider:
84
+
85
+ Example: title="src/lib/edgestore.ts"
86
+
87
+ ```tsx
88
+ // Import the router type from your backend.
89
+ import type { EdgeStoreRouter } from '../../../path/to/express-backend/src';
90
+ import { createEdgeStoreProvider } from '@edgestore/react';
91
+
92
+ const { EdgeStoreProvider, useEdgeStore } =
93
+ createEdgeStoreProvider<EdgeStoreRouter>();
94
+
95
+ export { EdgeStoreProvider, useEdgeStore };
96
+ ```
97
+
98
+ Wrap your app with the provider:
99
+
100
+ Example: title="src/App.tsx"
101
+
102
+ ```tsx
103
+ // [!code ++]
104
+ import { EdgeStoreProvider } from '../lib/edgestore';
105
+
106
+ function App() {
107
+ return (
108
+ <EdgeStoreProvider basePath="http://localhost:3001/edgestore">
109
+ {/* Rest of your app */}
110
+ </EdgeStoreProvider>
111
+ );
112
+ }
113
+ ```
114
+
115
+ ## Usage
116
+
117
+ See [Quick start](https://next.edgestore.dev/docs/quick-start) for the upload UI.
118
+
119
+ ## Limitations
120
+
121
+ > WARNING
122
+ >
123
+ > In production, serve the frontend and backend on the same domain. Use
124
+ > [Vercel rewrites](https://vercel.com/docs/edge-network/rewrites) or a reverse proxy.
@@ -0,0 +1,123 @@
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 { initEdgeStore } from '@edgestore/server';
39
+ import { createEdgeStoreFastifyHandler } from '@edgestore/server/adapters/fastify';
40
+ import cors from '@fastify/cors';
41
+ import cookie from '@fastify/cookie';
42
+ import fastify from 'fastify';
43
+
44
+ const PORT = process.env.PORT ?? 3001;
45
+
46
+ const app = fastify();
47
+
48
+ await app.register(cors, {
49
+ // Replace with your frontend origin.
50
+ origin: 'http://localhost:5173',
51
+ credentials: true,
52
+ });
53
+
54
+ await app.register(cookie);
55
+
56
+ const es = initEdgeStore.create();
57
+
58
+ const router = es.router({
59
+ publicFiles: es.fileBucket(),
60
+ });
61
+
62
+ export type EdgeStoreRouter = typeof router;
63
+
64
+ const handler = createEdgeStoreFastifyHandler({
65
+ router,
66
+ });
67
+
68
+ app.get('/edgestore/*', handler);
69
+ app.post('/edgestore/*', handler);
70
+
71
+ app.listen({ port: PORT }, (err) => {
72
+ if (err) {
73
+ console.error(err);
74
+ process.exit(1);
75
+ }
76
+ console.log(`Server running at http://localhost:${PORT}`);
77
+ });
78
+ ```
79
+
80
+ ### Frontend
81
+
82
+ Create the React provider:
83
+
84
+ Example: title="src/lib/edgestore.ts"
85
+
86
+ ```tsx
87
+ // Import the router type from your backend.
88
+ import type { EdgeStoreRouter } from '../../../path/to/fastify-backend/src';
89
+ import { createEdgeStoreProvider } from '@edgestore/react';
90
+
91
+ const { EdgeStoreProvider, useEdgeStore } =
92
+ createEdgeStoreProvider<EdgeStoreRouter>();
93
+
94
+ export { EdgeStoreProvider, useEdgeStore };
95
+ ```
96
+
97
+ Wrap your app with the provider:
98
+
99
+ Example: title="src/App.tsx"
100
+
101
+ ```tsx
102
+ // [!code ++]
103
+ import { EdgeStoreProvider } from '../lib/edgestore';
104
+
105
+ function App() {
106
+ return (
107
+ <EdgeStoreProvider basePath="http://localhost:3001/edgestore">
108
+ {/* Rest of your app */}
109
+ </EdgeStoreProvider>
110
+ );
111
+ }
112
+ ```
113
+
114
+ ## Usage
115
+
116
+ See [Quick start](https://next.edgestore.dev/docs/quick-start) for the upload UI.
117
+
118
+ ## Limitations
119
+
120
+ > WARNING
121
+ >
122
+ > In production, serve the frontend and backend on the same domain. Use
123
+ > [Vercel rewrites](https://vercel.com/docs/edge-network/rewrites) or a reverse proxy.
@@ -0,0 +1,131 @@
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 { initEdgeStore } from '@edgestore/server';
39
+ import { createEdgeStoreHonoHandler } from '@edgestore/server/adapters/hono';
40
+ import { serve } from '@hono/node-server';
41
+ import { Hono } from 'hono';
42
+ import { cors } from 'hono/cors';
43
+
44
+ const PORT = process.env.PORT ?? 3001;
45
+ const frontendOrigin = process.env.FRONTEND_ORIGIN ?? 'http://localhost:5173';
46
+ const app = new Hono();
47
+
48
+ app.use(
49
+ '/edgestore/*',
50
+ cors({
51
+ origin: frontendOrigin,
52
+ credentials: true,
53
+ }),
54
+ );
55
+
56
+ const es = initEdgeStore.create();
57
+
58
+ const router = es.router({
59
+ publicFiles: es.fileBucket(),
60
+ });
61
+
62
+ export type EdgeStoreRouter = typeof router;
63
+
64
+ const handler = createEdgeStoreHonoHandler({
65
+ router,
66
+ });
67
+
68
+ app.all('/edgestore/*', handler);
69
+
70
+ serve(
71
+ {
72
+ fetch: app.fetch,
73
+ port: Number(PORT),
74
+ },
75
+ (info) => {
76
+ console.log(`Server running at http://localhost:${info.port}`);
77
+ },
78
+ );
79
+ ```
80
+
81
+ ### Frontend
82
+
83
+ Create the React provider:
84
+
85
+ Example: title="src/lib/edgestore.ts"
86
+
87
+ ```tsx
88
+ // Import the router type from your backend.
89
+ import { type EdgeStoreRouter } from '../../../path/to/hono-backend/src';
90
+ import { createEdgeStoreProvider } from '@edgestore/react';
91
+
92
+ const { EdgeStoreProvider, useEdgeStore } =
93
+ createEdgeStoreProvider<EdgeStoreRouter>();
94
+
95
+ export { EdgeStoreProvider, useEdgeStore };
96
+ ```
97
+
98
+ Wrap your app with the provider:
99
+
100
+ Example: title="src/App.tsx"
101
+
102
+ ```tsx
103
+ // [!code ++]
104
+ import { EdgeStoreProvider } from '../lib/edgestore';
105
+
106
+ function App() {
107
+ return (
108
+ <EdgeStoreProvider basePath={import.meta.env.VITE_EDGESTORE_BASE_PATH ?? '/edgestore'}>
109
+ {/* Rest of your app */}
110
+ </EdgeStoreProvider>
111
+ );
112
+ }
113
+ ```
114
+
115
+ For separate Vite and Hono dev servers, set these values to match your ports:
116
+
117
+ * Frontend: `VITE_EDGESTORE_BASE_PATH=http://localhost:3001/edgestore`
118
+ * Backend: `FRONTEND_ORIGIN=http://localhost:5173`
119
+
120
+ Across repositories, share the backend router's type through a package.
121
+
122
+ ## Usage
123
+
124
+ See [Quick start](https://next.edgestore.dev/docs/quick-start) for the upload UI.
125
+
126
+ ## Limitations
127
+
128
+ > WARNING
129
+ >
130
+ > In production, serve the frontend and backend on the same domain. Use
131
+ > [Vercel rewrites](https://vercel.com/docs/edge-network/rewrites) or a reverse proxy.