@edgestore/server 0.0.0-alpha.12 → 0.0.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/next/app/index.d.ts +4 -1
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +4 -4
- package/dist/adapters/next/app/index.mjs +4 -4
- package/dist/adapters/next/pages/index.d.ts +4 -1
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +4 -4
- package/dist/adapters/next/pages/index.mjs +4 -4
- package/dist/adapters/shared.d.ts +4 -3
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/core/client/index.d.ts +10 -2
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/index.mjs +2 -2
- package/dist/core/internals/bucketBuilder.d.ts +52 -25
- package/dist/core/internals/bucketBuilder.d.ts.map +1 -1
- package/dist/core/sdk/index.d.ts +4 -4
- package/dist/core/sdk/index.d.ts.map +1 -1
- package/dist/{index-3cc4d530.js → index-0a168903.js} +4 -2
- package/dist/{index-ca41982b.mjs → index-579b8015.mjs} +4 -2
- package/dist/{index-3999aae6.js → index-62d969e5.js} +4 -2
- package/dist/index.js +18 -9
- package/dist/index.mjs +18 -9
- package/dist/providers/edgestore/index.js +2 -2
- package/dist/providers/edgestore/index.mjs +2 -2
- package/dist/providers/types.d.ts +2 -4
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/{shared-6bef8919.js → shared-10685370.js} +33 -1
- package/dist/{shared-43667670.mjs → shared-59c74ca7.mjs} +32 -1
- package/dist/{shared-f7607e44.js → shared-5ed6e18e.js} +32 -1
- package/package.json +3 -3
- package/src/adapters/next/app/index.ts +14 -6
- package/src/adapters/next/pages/index.ts +14 -6
- package/src/adapters/shared.ts +39 -7
- package/src/core/client/index.ts +20 -12
- package/src/core/internals/bucketBuilder.ts +75 -37
- package/src/core/sdk/index.ts +8 -6
- package/src/providers/edgestore/index.ts +1 -1
- package/src/providers/types.ts +6 -4
package/dist/index.js
CHANGED
|
@@ -46,12 +46,14 @@ function createBuilder(opts, initDef) {
|
|
|
46
46
|
type: opts.type,
|
|
47
47
|
input: zod.z.never(),
|
|
48
48
|
path: [],
|
|
49
|
+
metadata: ()=>({}),
|
|
49
50
|
...initDef
|
|
50
51
|
};
|
|
51
52
|
return {
|
|
52
53
|
$config: {
|
|
53
54
|
ctx: undefined
|
|
54
55
|
},
|
|
56
|
+
// @ts-expect-error - I think it would be too much work to make this type correct.
|
|
55
57
|
_def,
|
|
56
58
|
input (input) {
|
|
57
59
|
return createNewBuilder(_def, {
|
|
@@ -108,19 +110,26 @@ function createRouterFactory() {
|
|
|
108
110
|
};
|
|
109
111
|
};
|
|
110
112
|
}
|
|
113
|
+
function initBucket(type, config) {
|
|
114
|
+
return createBuilder({
|
|
115
|
+
type
|
|
116
|
+
}, {
|
|
117
|
+
bucketConfig: config
|
|
118
|
+
});
|
|
119
|
+
}
|
|
111
120
|
function createEdgeStoreInner() {
|
|
112
121
|
return function initEdgeStoreInner() {
|
|
113
122
|
return {
|
|
114
123
|
/**
|
|
115
124
|
* Builder object for creating an image bucket
|
|
116
|
-
*/ imageBucket
|
|
117
|
-
|
|
118
|
-
}
|
|
125
|
+
*/ imageBucket (config) {
|
|
126
|
+
return initBucket('IMAGE', config);
|
|
127
|
+
},
|
|
119
128
|
/**
|
|
120
129
|
* Builder object for creating a file bucket
|
|
121
|
-
*/ fileBucket
|
|
122
|
-
|
|
123
|
-
}
|
|
130
|
+
*/ fileBucket (config) {
|
|
131
|
+
return initBucket('FILE', config);
|
|
132
|
+
},
|
|
124
133
|
/**
|
|
125
134
|
* Create a router
|
|
126
135
|
*/ router: createRouterFactory()
|
|
@@ -135,7 +144,7 @@ function createEdgeStoreInner() {
|
|
|
135
144
|
// userRole: 'admin' | 'visitor';
|
|
136
145
|
// };
|
|
137
146
|
// const es = initEdgeStore.context<Context>().create();
|
|
138
|
-
// const imagesBucket = es.imageBucket
|
|
147
|
+
// const imagesBucket = es.imageBucket()
|
|
139
148
|
// .input(
|
|
140
149
|
// z.object({
|
|
141
150
|
// type: z.enum(['profile', 'post']),
|
|
@@ -150,7 +159,7 @@ function createEdgeStoreInner() {
|
|
|
150
159
|
// .beforeUpload(() => {
|
|
151
160
|
// return true;
|
|
152
161
|
// });
|
|
153
|
-
// const a = es.imageBucket
|
|
162
|
+
// const a = es.imageBucket()
|
|
154
163
|
// .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
|
|
155
164
|
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
156
165
|
// .metadata(({ ctx, input }) => ({
|
|
@@ -173,7 +182,7 @@ function createEdgeStoreInner() {
|
|
|
173
182
|
// .beforeDelete(({ ctx, file }) => {
|
|
174
183
|
// return true;
|
|
175
184
|
// });
|
|
176
|
-
// const b = es.imageBucket.path(({ ctx }) => [{ author: ctx.userId }]);
|
|
185
|
+
// const b = es.imageBucket().path(({ ctx }) => [{ author: ctx.userId }]);
|
|
177
186
|
// const router = es.router({
|
|
178
187
|
// original: imagesBucket,
|
|
179
188
|
// imageBucket: a,
|
package/dist/index.mjs
CHANGED
|
@@ -42,12 +42,14 @@ function createBuilder(opts, initDef) {
|
|
|
42
42
|
type: opts.type,
|
|
43
43
|
input: z.never(),
|
|
44
44
|
path: [],
|
|
45
|
+
metadata: ()=>({}),
|
|
45
46
|
...initDef
|
|
46
47
|
};
|
|
47
48
|
return {
|
|
48
49
|
$config: {
|
|
49
50
|
ctx: undefined
|
|
50
51
|
},
|
|
52
|
+
// @ts-expect-error - I think it would be too much work to make this type correct.
|
|
51
53
|
_def,
|
|
52
54
|
input (input) {
|
|
53
55
|
return createNewBuilder(_def, {
|
|
@@ -104,19 +106,26 @@ function createRouterFactory() {
|
|
|
104
106
|
};
|
|
105
107
|
};
|
|
106
108
|
}
|
|
109
|
+
function initBucket(type, config) {
|
|
110
|
+
return createBuilder({
|
|
111
|
+
type
|
|
112
|
+
}, {
|
|
113
|
+
bucketConfig: config
|
|
114
|
+
});
|
|
115
|
+
}
|
|
107
116
|
function createEdgeStoreInner() {
|
|
108
117
|
return function initEdgeStoreInner() {
|
|
109
118
|
return {
|
|
110
119
|
/**
|
|
111
120
|
* Builder object for creating an image bucket
|
|
112
|
-
*/ imageBucket
|
|
113
|
-
|
|
114
|
-
}
|
|
121
|
+
*/ imageBucket (config) {
|
|
122
|
+
return initBucket('IMAGE', config);
|
|
123
|
+
},
|
|
115
124
|
/**
|
|
116
125
|
* Builder object for creating a file bucket
|
|
117
|
-
*/ fileBucket
|
|
118
|
-
|
|
119
|
-
}
|
|
126
|
+
*/ fileBucket (config) {
|
|
127
|
+
return initBucket('FILE', config);
|
|
128
|
+
},
|
|
120
129
|
/**
|
|
121
130
|
* Create a router
|
|
122
131
|
*/ router: createRouterFactory()
|
|
@@ -131,7 +140,7 @@ function createEdgeStoreInner() {
|
|
|
131
140
|
// userRole: 'admin' | 'visitor';
|
|
132
141
|
// };
|
|
133
142
|
// const es = initEdgeStore.context<Context>().create();
|
|
134
|
-
// const imagesBucket = es.imageBucket
|
|
143
|
+
// const imagesBucket = es.imageBucket()
|
|
135
144
|
// .input(
|
|
136
145
|
// z.object({
|
|
137
146
|
// type: z.enum(['profile', 'post']),
|
|
@@ -146,7 +155,7 @@ function createEdgeStoreInner() {
|
|
|
146
155
|
// .beforeUpload(() => {
|
|
147
156
|
// return true;
|
|
148
157
|
// });
|
|
149
|
-
// const a = es.imageBucket
|
|
158
|
+
// const a = es.imageBucket()
|
|
150
159
|
// .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
|
|
151
160
|
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
152
161
|
// .metadata(({ ctx, input }) => ({
|
|
@@ -169,7 +178,7 @@ function createEdgeStoreInner() {
|
|
|
169
178
|
// .beforeDelete(({ ctx, file }) => {
|
|
170
179
|
// return true;
|
|
171
180
|
// });
|
|
172
|
-
// const b = es.imageBucket.path(({ ctx }) => [{ author: ctx.userId }]);
|
|
181
|
+
// const b = es.imageBucket().path(({ ctx }) => [{ author: ctx.userId }]);
|
|
173
182
|
// const router = es.router({
|
|
174
183
|
// original: imagesBucket,
|
|
175
184
|
// imageBucket: a,
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var index = require('../../index-
|
|
5
|
+
var index = require('../../index-62d969e5.js');
|
|
6
6
|
|
|
7
|
-
const DEFAULT_BASE_URL = process.env.EDGE_STORE_BASE_URL ?? 'https://files.
|
|
7
|
+
const DEFAULT_BASE_URL = process.env.EDGE_STORE_BASE_URL ?? 'https://files.edgestore.dev';
|
|
8
8
|
function EdgeStoreProvider(options) {
|
|
9
9
|
const { accessKey = process.env.EDGE_STORE_ACCESS_KEY, secretKey = process.env.EDGE_STORE_SECRET_KEY, baseUrl = process.env.EDGE_STORE_BASE_URL ?? DEFAULT_BASE_URL } = options ?? {};
|
|
10
10
|
if (!accessKey || !secretKey) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { E as EdgeStoreCredentialsError, i as initEdgeStoreSdk } from '../../index-
|
|
1
|
+
import { E as EdgeStoreCredentialsError, i as initEdgeStoreSdk } from '../../index-579b8015.mjs';
|
|
2
2
|
|
|
3
|
-
const DEFAULT_BASE_URL = process.env.EDGE_STORE_BASE_URL ?? 'https://files.
|
|
3
|
+
const DEFAULT_BASE_URL = process.env.EDGE_STORE_BASE_URL ?? 'https://files.edgestore.dev';
|
|
4
4
|
function EdgeStoreProvider(options) {
|
|
5
5
|
const { accessKey = process.env.EDGE_STORE_ACCESS_KEY, secretKey = process.env.EDGE_STORE_SECRET_KEY, baseUrl = process.env.EDGE_STORE_BASE_URL ?? DEFAULT_BASE_URL } = options ?? {};
|
|
6
6
|
if (!accessKey || !secretKey) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyBuilder, EdgeStoreRouter } from '../core/internals/bucketBuilder';
|
|
1
|
+
import { AnyBuilder, AnyMetadata, EdgeStoreRouter } from '../core/internals/bucketBuilder';
|
|
2
2
|
import { MaybePromise } from '../types';
|
|
3
3
|
export type InitParams = {
|
|
4
4
|
ctx: any;
|
|
@@ -36,9 +36,7 @@ export type RequestUploadParams = {
|
|
|
36
36
|
key: string;
|
|
37
37
|
value: string;
|
|
38
38
|
}[];
|
|
39
|
-
metadata
|
|
40
|
-
[key: string]: string;
|
|
41
|
-
};
|
|
39
|
+
metadata: AnyMetadata;
|
|
42
40
|
replaceTargetUrl?: string;
|
|
43
41
|
};
|
|
44
42
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/providers/types.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/providers/types.ts"],"names":[],"mappings":"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"}
|
|
@@ -48,7 +48,7 @@ async function init(params) {
|
|
|
48
48
|
maxAge: DEFAULT_MAX_AGE,
|
|
49
49
|
}));
|
|
50
50
|
}
|
|
51
|
-
const baseUrl = provider.getBaseUrl();
|
|
51
|
+
const baseUrl = await provider.getBaseUrl();
|
|
52
52
|
return {
|
|
53
53
|
newCookies,
|
|
54
54
|
token,
|
|
@@ -75,6 +75,7 @@ async function requestUpload(params) {
|
|
|
75
75
|
fileInfo: {
|
|
76
76
|
size: fileInfo.size,
|
|
77
77
|
type: fileInfo.type,
|
|
78
|
+
fileName: fileInfo.fileName,
|
|
78
79
|
extension: fileInfo.extension,
|
|
79
80
|
replaceTargetUrl: fileInfo.replaceTargetUrl,
|
|
80
81
|
},
|
|
@@ -91,6 +92,37 @@ async function requestUpload(params) {
|
|
|
91
92
|
});
|
|
92
93
|
}
|
|
93
94
|
}
|
|
95
|
+
if (bucket._def.bucketConfig?.maxSize) {
|
|
96
|
+
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
97
|
+
throw new EdgeStoreError({
|
|
98
|
+
code: 'BAD_REQUEST',
|
|
99
|
+
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (bucket._def.bucketConfig?.accept) {
|
|
104
|
+
const accept = bucket._def.bucketConfig.accept;
|
|
105
|
+
let accepted = false;
|
|
106
|
+
for (const acceptedMimeType of accept) {
|
|
107
|
+
if (acceptedMimeType.endsWith('/*')) {
|
|
108
|
+
const mimeType = acceptedMimeType.replace('/*', '');
|
|
109
|
+
if (fileInfo.type.startsWith(mimeType)) {
|
|
110
|
+
accepted = true;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else if (fileInfo.type === acceptedMimeType) {
|
|
115
|
+
accepted = true;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (!accepted) {
|
|
120
|
+
throw new EdgeStoreError({
|
|
121
|
+
code: 'BAD_REQUEST',
|
|
122
|
+
message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
94
126
|
const path = buildPath({
|
|
95
127
|
fileInfo,
|
|
96
128
|
bucket,
|
|
@@ -51,7 +51,7 @@ async function init(params) {
|
|
|
51
51
|
maxAge: DEFAULT_MAX_AGE
|
|
52
52
|
}));
|
|
53
53
|
}
|
|
54
|
-
const baseUrl = provider.getBaseUrl();
|
|
54
|
+
const baseUrl = await provider.getBaseUrl();
|
|
55
55
|
return {
|
|
56
56
|
newCookies,
|
|
57
57
|
token,
|
|
@@ -78,6 +78,7 @@ async function requestUpload(params) {
|
|
|
78
78
|
fileInfo: {
|
|
79
79
|
size: fileInfo.size,
|
|
80
80
|
type: fileInfo.type,
|
|
81
|
+
fileName: fileInfo.fileName,
|
|
81
82
|
extension: fileInfo.extension,
|
|
82
83
|
replaceTargetUrl: fileInfo.replaceTargetUrl
|
|
83
84
|
}
|
|
@@ -94,6 +95,36 @@ async function requestUpload(params) {
|
|
|
94
95
|
});
|
|
95
96
|
}
|
|
96
97
|
}
|
|
98
|
+
if (bucket._def.bucketConfig?.maxSize) {
|
|
99
|
+
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
100
|
+
throw new EdgeStoreError({
|
|
101
|
+
code: 'BAD_REQUEST',
|
|
102
|
+
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (bucket._def.bucketConfig?.accept) {
|
|
107
|
+
const accept = bucket._def.bucketConfig.accept;
|
|
108
|
+
let accepted = false;
|
|
109
|
+
for (const acceptedMimeType of accept){
|
|
110
|
+
if (acceptedMimeType.endsWith('/*')) {
|
|
111
|
+
const mimeType = acceptedMimeType.replace('/*', '');
|
|
112
|
+
if (fileInfo.type.startsWith(mimeType)) {
|
|
113
|
+
accepted = true;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
} else if (fileInfo.type === acceptedMimeType) {
|
|
117
|
+
accepted = true;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (!accepted) {
|
|
122
|
+
throw new EdgeStoreError({
|
|
123
|
+
code: 'BAD_REQUEST',
|
|
124
|
+
message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
97
128
|
const path = buildPath({
|
|
98
129
|
fileInfo,
|
|
99
130
|
bucket,
|
|
@@ -53,7 +53,7 @@ async function init(params) {
|
|
|
53
53
|
maxAge: DEFAULT_MAX_AGE
|
|
54
54
|
}));
|
|
55
55
|
}
|
|
56
|
-
const baseUrl = provider.getBaseUrl();
|
|
56
|
+
const baseUrl = await provider.getBaseUrl();
|
|
57
57
|
return {
|
|
58
58
|
newCookies,
|
|
59
59
|
token,
|
|
@@ -80,6 +80,7 @@ async function requestUpload(params) {
|
|
|
80
80
|
fileInfo: {
|
|
81
81
|
size: fileInfo.size,
|
|
82
82
|
type: fileInfo.type,
|
|
83
|
+
fileName: fileInfo.fileName,
|
|
83
84
|
extension: fileInfo.extension,
|
|
84
85
|
replaceTargetUrl: fileInfo.replaceTargetUrl
|
|
85
86
|
}
|
|
@@ -96,6 +97,36 @@ async function requestUpload(params) {
|
|
|
96
97
|
});
|
|
97
98
|
}
|
|
98
99
|
}
|
|
100
|
+
if (bucket._def.bucketConfig?.maxSize) {
|
|
101
|
+
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
102
|
+
throw new EdgeStoreError({
|
|
103
|
+
code: 'BAD_REQUEST',
|
|
104
|
+
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (bucket._def.bucketConfig?.accept) {
|
|
109
|
+
const accept = bucket._def.bucketConfig.accept;
|
|
110
|
+
let accepted = false;
|
|
111
|
+
for (const acceptedMimeType of accept){
|
|
112
|
+
if (acceptedMimeType.endsWith('/*')) {
|
|
113
|
+
const mimeType = acceptedMimeType.replace('/*', '');
|
|
114
|
+
if (fileInfo.type.startsWith(mimeType)) {
|
|
115
|
+
accepted = true;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
} else if (fileInfo.type === acceptedMimeType) {
|
|
119
|
+
accepted = true;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (!accepted) {
|
|
124
|
+
throw new EdgeStoreError({
|
|
125
|
+
code: 'BAD_REQUEST',
|
|
126
|
+
message: `"${fileInfo.type}" is not allowed. Accepted types are ${JSON.stringify(accept)}`
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
99
130
|
const path = buildPath({
|
|
100
131
|
fileInfo,
|
|
101
132
|
bucket,
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edgestore/server",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.13",
|
|
4
4
|
"description": "Image Handling for React/Next.js",
|
|
5
|
-
"homepage": "https://
|
|
5
|
+
"homepage": "https://edgestore.dev",
|
|
6
6
|
"repository": "https://github.com/edgestorejs/edge-store.git",
|
|
7
7
|
"author": "Ravi <me@ravi.com>",
|
|
8
8
|
"main": "dist/index.js",
|
|
@@ -92,5 +92,5 @@
|
|
|
92
92
|
"typescript": "^5.1.6",
|
|
93
93
|
"zod": "^3.21.4"
|
|
94
94
|
},
|
|
95
|
-
"gitHead": "
|
|
95
|
+
"gitHead": "f89bcb0360feec96fb842ad447d86a0e5c58900d"
|
|
96
96
|
}
|
|
@@ -20,18 +20,26 @@ export type CreateContextOptions = {
|
|
|
20
20
|
req: NextRequest;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export type Config<TCtx> =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
23
|
+
export type Config<TCtx> = TCtx extends Record<string, never>
|
|
24
|
+
? {
|
|
25
|
+
provider?: Provider;
|
|
26
|
+
router: EdgeStoreRouter<TCtx>;
|
|
27
|
+
}
|
|
28
|
+
: {
|
|
29
|
+
provider?: Provider;
|
|
30
|
+
router: EdgeStoreRouter<TCtx>;
|
|
31
|
+
createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
|
|
32
|
+
};
|
|
28
33
|
|
|
29
34
|
export function createEdgeStoreNextHandler<TCtx>(config: Config<TCtx>) {
|
|
30
35
|
const { provider = EdgeStoreProvider() } = config;
|
|
31
36
|
return async (req: NextRequest) => {
|
|
32
37
|
try {
|
|
33
38
|
if (req.nextUrl.pathname === '/api/edgestore/init') {
|
|
34
|
-
const ctx =
|
|
39
|
+
const ctx =
|
|
40
|
+
'createContext' in config
|
|
41
|
+
? await config.createContext({ req })
|
|
42
|
+
: ({} as TCtx);
|
|
35
43
|
const { newCookies, token, baseUrl } = await init({
|
|
36
44
|
ctx,
|
|
37
45
|
provider,
|
|
@@ -21,18 +21,26 @@ export type CreateContextOptions = {
|
|
|
21
21
|
res: NextApiResponse;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export type Config<TCtx> =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
24
|
+
export type Config<TCtx> = TCtx extends Record<string, never>
|
|
25
|
+
? {
|
|
26
|
+
provider?: Provider;
|
|
27
|
+
router: EdgeStoreRouter<TCtx>;
|
|
28
|
+
}
|
|
29
|
+
: {
|
|
30
|
+
provider?: Provider;
|
|
31
|
+
router: EdgeStoreRouter<TCtx>;
|
|
32
|
+
createContext: (opts: CreateContextOptions) => MaybePromise<TCtx>;
|
|
33
|
+
};
|
|
29
34
|
|
|
30
35
|
export function createEdgeStoreNextHandler<TCtx>(config: Config<TCtx>) {
|
|
31
36
|
const { provider = EdgeStoreProvider() } = config;
|
|
32
37
|
return async (req: NextApiRequest, res: NextApiResponse) => {
|
|
33
38
|
try {
|
|
34
39
|
if (req.url === '/api/edgestore/init') {
|
|
35
|
-
const ctx =
|
|
40
|
+
const ctx =
|
|
41
|
+
'createContext' in config
|
|
42
|
+
? await config.createContext({ req, res })
|
|
43
|
+
: ({} as TCtx);
|
|
36
44
|
const { newCookies, token, baseUrl } = await init({
|
|
37
45
|
ctx,
|
|
38
46
|
provider,
|
package/src/adapters/shared.ts
CHANGED
|
@@ -2,11 +2,7 @@ import { hkdf } from '@panva/hkdf';
|
|
|
2
2
|
import { serialize } from 'cookie';
|
|
3
3
|
import { EncryptJWT, jwtDecrypt } from 'jose';
|
|
4
4
|
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
-
import {
|
|
6
|
-
AnyBuilder,
|
|
7
|
-
AnyPath,
|
|
8
|
-
EdgeStoreRouter,
|
|
9
|
-
} from '../core/internals/bucketBuilder';
|
|
5
|
+
import { AnyBuilder, EdgeStoreRouter } from '../core/internals/bucketBuilder';
|
|
10
6
|
import EdgeStoreError from '../libs/errors/EdgeStoreError';
|
|
11
7
|
import { Provider } from '../providers/types';
|
|
12
8
|
import { IMAGE_MIME_TYPES } from './imageTypes';
|
|
@@ -39,7 +35,7 @@ export async function init<TCtx>(params: {
|
|
|
39
35
|
}),
|
|
40
36
|
);
|
|
41
37
|
}
|
|
42
|
-
const baseUrl = provider.getBaseUrl();
|
|
38
|
+
const baseUrl = await provider.getBaseUrl();
|
|
43
39
|
|
|
44
40
|
return {
|
|
45
41
|
newCookies,
|
|
@@ -55,6 +51,7 @@ export type RequestUploadBody = {
|
|
|
55
51
|
size: number;
|
|
56
52
|
type: string;
|
|
57
53
|
extension: string;
|
|
54
|
+
fileName?: string;
|
|
58
55
|
replaceTargetUrl?: string;
|
|
59
56
|
};
|
|
60
57
|
};
|
|
@@ -90,6 +87,7 @@ export async function requestUpload<TCtx>(params: {
|
|
|
90
87
|
fileInfo: {
|
|
91
88
|
size: fileInfo.size,
|
|
92
89
|
type: fileInfo.type,
|
|
90
|
+
fileName: fileInfo.fileName,
|
|
93
91
|
extension: fileInfo.extension,
|
|
94
92
|
replaceTargetUrl: fileInfo.replaceTargetUrl,
|
|
95
93
|
},
|
|
@@ -108,6 +106,40 @@ export async function requestUpload<TCtx>(params: {
|
|
|
108
106
|
}
|
|
109
107
|
}
|
|
110
108
|
|
|
109
|
+
if (bucket._def.bucketConfig?.maxSize) {
|
|
110
|
+
if (fileInfo.size > bucket._def.bucketConfig.maxSize) {
|
|
111
|
+
throw new EdgeStoreError({
|
|
112
|
+
code: 'BAD_REQUEST',
|
|
113
|
+
message: `File size is too big. Max size is ${bucket._def.bucketConfig.maxSize}`,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (bucket._def.bucketConfig?.accept) {
|
|
119
|
+
const accept = bucket._def.bucketConfig.accept;
|
|
120
|
+
let accepted = false;
|
|
121
|
+
for (const acceptedMimeType of accept) {
|
|
122
|
+
if (acceptedMimeType.endsWith('/*')) {
|
|
123
|
+
const mimeType = acceptedMimeType.replace('/*', '');
|
|
124
|
+
if (fileInfo.type.startsWith(mimeType)) {
|
|
125
|
+
accepted = true;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
} else if (fileInfo.type === acceptedMimeType) {
|
|
129
|
+
accepted = true;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!accepted) {
|
|
134
|
+
throw new EdgeStoreError({
|
|
135
|
+
code: 'BAD_REQUEST',
|
|
136
|
+
message: `"${
|
|
137
|
+
fileInfo.type
|
|
138
|
+
}" is not allowed. Accepted types are ${JSON.stringify(accept)}`,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
111
143
|
const path = buildPath({
|
|
112
144
|
fileInfo,
|
|
113
145
|
bucket,
|
|
@@ -274,7 +306,7 @@ function buildPath(params: {
|
|
|
274
306
|
};
|
|
275
307
|
}) {
|
|
276
308
|
const { bucket } = params;
|
|
277
|
-
const pathParams = bucket._def.path
|
|
309
|
+
const pathParams = bucket._def.path;
|
|
278
310
|
const path = pathParams.map((param) => {
|
|
279
311
|
const paramEntries = Object.entries(param);
|
|
280
312
|
if (paramEntries[0] === undefined) {
|
package/src/core/client/index.ts
CHANGED
|
@@ -38,18 +38,26 @@ export type ListFilesRequest<TBucket extends AnyBuilder> = {
|
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
export type ListFilesResponse<TBucket extends AnyBuilder> = {
|
|
41
|
-
data:
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
41
|
+
data: TBucket['_def']['type'] extends 'IMAGE'
|
|
42
|
+
? {
|
|
43
|
+
url: string;
|
|
44
|
+
thumbnailUrl: string | null;
|
|
45
|
+
size: number;
|
|
46
|
+
uploadedAt: Date;
|
|
47
|
+
metadata: InferMetadataObject<TBucket>;
|
|
48
|
+
path: {
|
|
49
|
+
[TKey in InferBucketPathKeys<TBucket>]: string;
|
|
50
|
+
};
|
|
51
|
+
}[]
|
|
52
|
+
: {
|
|
53
|
+
url: string;
|
|
54
|
+
size: number;
|
|
55
|
+
uploadedAt: Date;
|
|
56
|
+
metadata: InferMetadataObject<TBucket>;
|
|
57
|
+
path: {
|
|
58
|
+
[TKey in InferBucketPathKeys<TBucket>]: string;
|
|
59
|
+
};
|
|
60
|
+
}[];
|
|
53
61
|
pagination: {
|
|
54
62
|
currentPage: number;
|
|
55
63
|
totalPages: number;
|