@edgestore/shared 0.1.5-alpha.14 → 0.1.5-alpha.15
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/errors/EdgeStoreError.d.ts +47 -0
- package/dist/errors/EdgeStoreError.d.ts.map +1 -0
- package/dist/errors/index.d.ts +9 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +268 -0
- package/dist/index.mjs +262 -0
- package/dist/internals/bucketBuilder.d.ts +270 -0
- package/dist/internals/bucketBuilder.d.ts.map +1 -0
- package/dist/internals/createPathParamProxy.d.ts +21 -0
- package/dist/internals/createPathParamProxy.d.ts.map +1 -0
- package/dist/internals/providerTypes.d.ts +114 -0
- package/dist/internals/providerTypes.d.ts.map +1 -0
- package/dist/internals/sharedFuncTypes.d.ts +18 -0
- package/dist/internals/sharedFuncTypes.d.ts.map +1 -0
- package/dist/internals/types.d.ts +30 -0
- package/dist/internals/types.d.ts.map +1 -0
- package/dist/types.d.ts +94 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +11 -4
- package/src/errors/EdgeStoreError.ts +90 -0
- package/src/errors/index.ts +14 -0
- package/src/index.ts +6 -1
- package/src/internals/bucketBuilder.ts +553 -0
- package/src/internals/createPathParamProxy.ts +40 -0
- package/src/internals/providerTypes.ts +140 -0
- package/src/internals/sharedFuncTypes.ts +24 -0
- package/src/internals/types.ts +29 -0
- package/src/types.ts +148 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type Simplify } from '../types';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
4
|
+
export const EDGE_STORE_ERROR_CODES = {
|
|
5
|
+
BAD_REQUEST: 400,
|
|
6
|
+
FILE_TOO_LARGE: 400,
|
|
7
|
+
MIME_TYPE_NOT_ALLOWED: 400,
|
|
8
|
+
UNAUTHORIZED: 401,
|
|
9
|
+
UPLOAD_NOT_ALLOWED: 403,
|
|
10
|
+
DELETE_NOT_ALLOWED: 403,
|
|
11
|
+
CREATE_CONTEXT_ERROR: 500,
|
|
12
|
+
SERVER_ERROR: 500,
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
export type EdgeStoreErrorCodeKey = keyof typeof EDGE_STORE_ERROR_CODES;
|
|
16
|
+
|
|
17
|
+
export type EdgeStoreErrorDetails<TCode extends EdgeStoreErrorCodeKey> =
|
|
18
|
+
TCode extends 'FILE_TOO_LARGE'
|
|
19
|
+
? {
|
|
20
|
+
maxFileSize: number;
|
|
21
|
+
fileSize: number;
|
|
22
|
+
}
|
|
23
|
+
: TCode extends 'MIME_TYPE_NOT_ALLOWED'
|
|
24
|
+
? {
|
|
25
|
+
allowedMimeTypes: string[];
|
|
26
|
+
mimeType: string;
|
|
27
|
+
}
|
|
28
|
+
: never;
|
|
29
|
+
|
|
30
|
+
export type EdgeStoreJsonResponse = Simplify<
|
|
31
|
+
| {
|
|
32
|
+
message: string;
|
|
33
|
+
code: 'FILE_TOO_LARGE';
|
|
34
|
+
details: EdgeStoreErrorDetails<'FILE_TOO_LARGE'>;
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
message: string;
|
|
38
|
+
code: 'MIME_TYPE_NOT_ALLOWED';
|
|
39
|
+
details: EdgeStoreErrorDetails<'MIME_TYPE_NOT_ALLOWED'>;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
message: string;
|
|
43
|
+
code: Exclude<
|
|
44
|
+
EdgeStoreErrorCodeKey,
|
|
45
|
+
'FILE_TOO_LARGE' | 'MIME_TYPE_NOT_ALLOWED'
|
|
46
|
+
>;
|
|
47
|
+
}
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
export class EdgeStoreError<TCode extends EdgeStoreErrorCodeKey> extends Error {
|
|
51
|
+
public readonly cause?: Error;
|
|
52
|
+
public readonly code: TCode;
|
|
53
|
+
public readonly level: 'error' | 'warn';
|
|
54
|
+
public readonly details: EdgeStoreErrorDetails<TCode>;
|
|
55
|
+
|
|
56
|
+
constructor(
|
|
57
|
+
opts: {
|
|
58
|
+
message: string;
|
|
59
|
+
code: TCode;
|
|
60
|
+
cause?: Error;
|
|
61
|
+
} & (EdgeStoreErrorDetails<TCode> extends undefined
|
|
62
|
+
? object
|
|
63
|
+
: {
|
|
64
|
+
details: EdgeStoreErrorDetails<TCode>;
|
|
65
|
+
}),
|
|
66
|
+
) {
|
|
67
|
+
super(opts.message);
|
|
68
|
+
this.name = 'EdgeStoreError';
|
|
69
|
+
|
|
70
|
+
this.code = opts.code;
|
|
71
|
+
this.cause = opts.cause;
|
|
72
|
+
this.level = EDGE_STORE_ERROR_CODES[opts.code] >= 500 ? 'error' : 'warn';
|
|
73
|
+
this.details = 'details' in opts ? opts.details : undefined!;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
formattedMessage(): string {
|
|
77
|
+
return `${this.message}${
|
|
78
|
+
this.details ? `\n Details: ${JSON.stringify(this.details)}` : ''
|
|
79
|
+
}${this.cause ? `\n Caused by: ${this.cause.message}` : ''}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
formattedJson(): EdgeStoreJsonResponse {
|
|
83
|
+
return {
|
|
84
|
+
message:
|
|
85
|
+
this.code === 'SERVER_ERROR' ? 'Internal server error' : this.message,
|
|
86
|
+
code: this.code,
|
|
87
|
+
details: this.details as any,
|
|
88
|
+
} satisfies EdgeStoreJsonResponse;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type EdgeStoreJsonResponse } from './EdgeStoreError';
|
|
2
|
+
|
|
3
|
+
export class EdgeStoreApiClientError extends Error {
|
|
4
|
+
public readonly data: EdgeStoreJsonResponse;
|
|
5
|
+
|
|
6
|
+
constructor(opts: { response: EdgeStoreJsonResponse }) {
|
|
7
|
+
super(opts.response.message);
|
|
8
|
+
this.name = 'EdgeStoreApiClientError';
|
|
9
|
+
|
|
10
|
+
this.data = opts.response;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export * from './EdgeStoreError';
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type KeysOfUnion, type MaybePromise, type Simplify } from '../types';
|
|
3
|
+
import { createPathParamProxy } from './createPathParamProxy';
|
|
4
|
+
|
|
5
|
+
type Merge<TType, TWith> = {
|
|
6
|
+
[TKey in keyof TType | keyof TWith]?: TKey extends keyof TType
|
|
7
|
+
? TKey extends keyof TWith
|
|
8
|
+
? TType[TKey] & TWith[TKey]
|
|
9
|
+
: TType[TKey]
|
|
10
|
+
: TWith[TKey & keyof TWith];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type ConvertStringToFunction<TType> = {
|
|
14
|
+
[K in keyof TType]: TType[K] extends object
|
|
15
|
+
? Simplify<ConvertStringToFunction<TType[K]>>
|
|
16
|
+
: () => string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type UnionToIntersection<TType> = (
|
|
20
|
+
TType extends any ? (k: TType) => void : never
|
|
21
|
+
) extends (k: infer I) => void
|
|
22
|
+
? I
|
|
23
|
+
: never;
|
|
24
|
+
|
|
25
|
+
export type InferBucketPathKeys<TBucket extends Builder<any, AnyDef>> =
|
|
26
|
+
KeysOfUnion<TBucket['_def']['path'][number]>;
|
|
27
|
+
|
|
28
|
+
type InferBucketPathKeysFromDef<TDef extends AnyDef> = KeysOfUnion<
|
|
29
|
+
TDef['path'][number]
|
|
30
|
+
>;
|
|
31
|
+
|
|
32
|
+
export type InferBucketPathObject<TBucket extends Builder<any, AnyDef>> =
|
|
33
|
+
InferBucketPathKeys<TBucket> extends never
|
|
34
|
+
? Record<string, never>
|
|
35
|
+
: {
|
|
36
|
+
[TKey in InferBucketPathKeys<TBucket>]: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type InferBucketPathObjectFromDef<TDef extends AnyDef> =
|
|
40
|
+
InferBucketPathKeysFromDef<TDef> extends never
|
|
41
|
+
? Record<string, never>
|
|
42
|
+
: {
|
|
43
|
+
[TKey in InferBucketPathKeysFromDef<TDef>]: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type InferMetadataObject<TBucket extends Builder<any, AnyDef>> =
|
|
47
|
+
TBucket['_def']['metadata'] extends (...args: any) => any
|
|
48
|
+
? Awaited<ReturnType<TBucket['_def']['metadata']>>
|
|
49
|
+
: Record<string, never>;
|
|
50
|
+
|
|
51
|
+
type InferMetadataObjectFromDef<TDef extends AnyDef> =
|
|
52
|
+
TDef['metadata'] extends (...args: any) => any
|
|
53
|
+
? Awaited<ReturnType<TDef['metadata']>>
|
|
54
|
+
: Record<string, never>;
|
|
55
|
+
|
|
56
|
+
export type AnyContext = Record<string, string | undefined | null>;
|
|
57
|
+
|
|
58
|
+
export type AnyInput = z.AnyZodObject | z.ZodNever;
|
|
59
|
+
|
|
60
|
+
export type AnyPath = Record<string, () => string>[];
|
|
61
|
+
|
|
62
|
+
type PathParam<TPath extends AnyPath> = {
|
|
63
|
+
path: keyof UnionToIntersection<TPath[number]>;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
type Conditions<TPath extends AnyPath> = {
|
|
67
|
+
eq?: string | PathParam<TPath>;
|
|
68
|
+
lt?: string | PathParam<TPath>;
|
|
69
|
+
lte?: string | PathParam<TPath>;
|
|
70
|
+
gt?: string | PathParam<TPath>;
|
|
71
|
+
gte?: string | PathParam<TPath>;
|
|
72
|
+
contains?: string | PathParam<TPath>;
|
|
73
|
+
in?: string | PathParam<TPath> | (string | PathParam<TPath>)[];
|
|
74
|
+
not?: string | PathParam<TPath> | Conditions<TPath>;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type AccessControlSchema<TCtx, TDef extends AnyDef> = Merge<
|
|
78
|
+
{
|
|
79
|
+
[TKey in keyof TCtx]?:
|
|
80
|
+
| string
|
|
81
|
+
| PathParam<TDef['path']>
|
|
82
|
+
| Conditions<TDef['path']>;
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
OR?: AccessControlSchema<TCtx, TDef>[];
|
|
86
|
+
AND?: AccessControlSchema<TCtx, TDef>[];
|
|
87
|
+
NOT?: AccessControlSchema<TCtx, TDef>[];
|
|
88
|
+
}
|
|
89
|
+
>;
|
|
90
|
+
|
|
91
|
+
type BucketConfig = {
|
|
92
|
+
/**
|
|
93
|
+
* Maximum size for a single file in bytes
|
|
94
|
+
*
|
|
95
|
+
* e.g. 1024 * 1024 * 10 = 10MB
|
|
96
|
+
*/
|
|
97
|
+
maxSize?: number;
|
|
98
|
+
/**
|
|
99
|
+
* Accepted MIME types
|
|
100
|
+
*
|
|
101
|
+
* e.g. ['image/jpeg', 'image/png']
|
|
102
|
+
*
|
|
103
|
+
* You can also use wildcards after the slash:
|
|
104
|
+
*
|
|
105
|
+
* e.g. ['image/*']
|
|
106
|
+
*/
|
|
107
|
+
accept?: string[];
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
type FileInfo = {
|
|
111
|
+
size: number;
|
|
112
|
+
type: string;
|
|
113
|
+
extension: string;
|
|
114
|
+
fileName?: string;
|
|
115
|
+
replaceTargetUrl?: string;
|
|
116
|
+
temporary: boolean;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
type BeforeUploadFn<TCtx, TDef extends AnyDef> = (params: {
|
|
120
|
+
ctx: TCtx;
|
|
121
|
+
input: z.infer<TDef['input']>;
|
|
122
|
+
fileInfo: FileInfo;
|
|
123
|
+
}) => MaybePromise<boolean>;
|
|
124
|
+
|
|
125
|
+
type BeforeDeleteFn<TCtx, TDef extends AnyDef> = (params: {
|
|
126
|
+
ctx: TCtx;
|
|
127
|
+
fileInfo: {
|
|
128
|
+
url: string;
|
|
129
|
+
size: number;
|
|
130
|
+
uploadedAt: Date;
|
|
131
|
+
path: InferBucketPathObjectFromDef<TDef>;
|
|
132
|
+
metadata: InferMetadataObjectFromDef<TDef>;
|
|
133
|
+
};
|
|
134
|
+
}) => MaybePromise<boolean>;
|
|
135
|
+
|
|
136
|
+
export type AnyMetadata = Record<string, string | undefined | null>;
|
|
137
|
+
|
|
138
|
+
type MetadataFn<
|
|
139
|
+
TCtx,
|
|
140
|
+
TInput extends AnyInput,
|
|
141
|
+
TMetadata extends AnyMetadata,
|
|
142
|
+
> = (params: { ctx: TCtx; input: z.infer<TInput> }) => MaybePromise<TMetadata>;
|
|
143
|
+
|
|
144
|
+
export type AnyMetadataFn = MetadataFn<any, AnyInput, AnyMetadata>;
|
|
145
|
+
|
|
146
|
+
type BucketType = 'IMAGE' | 'FILE';
|
|
147
|
+
|
|
148
|
+
type Def<
|
|
149
|
+
TInput extends AnyInput,
|
|
150
|
+
TPath extends AnyPath,
|
|
151
|
+
TMetadata extends AnyMetadataFn,
|
|
152
|
+
> = {
|
|
153
|
+
type: BucketType;
|
|
154
|
+
input: TInput;
|
|
155
|
+
path: TPath;
|
|
156
|
+
metadata: TMetadata;
|
|
157
|
+
bucketConfig?: BucketConfig;
|
|
158
|
+
accessControl?: AccessControlSchema<any, any>;
|
|
159
|
+
beforeUpload?: BeforeUploadFn<any, any>;
|
|
160
|
+
beforeDelete?: BeforeDeleteFn<any, any>;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
type AnyDef = Def<AnyInput, AnyPath, AnyMetadataFn>;
|
|
164
|
+
|
|
165
|
+
type Builder<TCtx, TDef extends AnyDef> = {
|
|
166
|
+
/** only used for types */
|
|
167
|
+
$config: {
|
|
168
|
+
ctx: TCtx;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* @internal
|
|
172
|
+
*/
|
|
173
|
+
_def: TDef;
|
|
174
|
+
/**
|
|
175
|
+
* You can set an input that will be required in every upload from the client.
|
|
176
|
+
*
|
|
177
|
+
* This can be used to add additional information to the file, like choose the file path or add metadata.
|
|
178
|
+
*/
|
|
179
|
+
input<TInput extends AnyInput>(
|
|
180
|
+
input: TInput,
|
|
181
|
+
): Builder<
|
|
182
|
+
TCtx,
|
|
183
|
+
{
|
|
184
|
+
type: TDef['type'];
|
|
185
|
+
input: TInput;
|
|
186
|
+
path: TDef['path'];
|
|
187
|
+
metadata: TDef['metadata'];
|
|
188
|
+
bucketConfig: TDef['bucketConfig'];
|
|
189
|
+
accessControl: TDef['accessControl'];
|
|
190
|
+
beforeUpload: TDef['beforeUpload'];
|
|
191
|
+
beforeDelete: TDef['beforeDelete'];
|
|
192
|
+
}
|
|
193
|
+
>;
|
|
194
|
+
/**
|
|
195
|
+
* The `path` is similar to folders in a file system.
|
|
196
|
+
* But in this case, every segment of the path must have a meaning.
|
|
197
|
+
*
|
|
198
|
+
* ```
|
|
199
|
+
* // e.g. 123/profile/file.jpg
|
|
200
|
+
* {
|
|
201
|
+
* author: '123',
|
|
202
|
+
* type: 'profile',
|
|
203
|
+
* }
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
path<TParams extends AnyPath>(
|
|
207
|
+
pathResolver: (params: {
|
|
208
|
+
ctx: Simplify<ConvertStringToFunction<TCtx>>;
|
|
209
|
+
input: Simplify<ConvertStringToFunction<z.infer<TDef['input']>>>;
|
|
210
|
+
}) => [...TParams],
|
|
211
|
+
): Builder<
|
|
212
|
+
TCtx,
|
|
213
|
+
{
|
|
214
|
+
type: TDef['type'];
|
|
215
|
+
input: TDef['input'];
|
|
216
|
+
path: TParams;
|
|
217
|
+
metadata: TDef['metadata'];
|
|
218
|
+
bucketConfig: TDef['bucketConfig'];
|
|
219
|
+
accessControl: TDef['accessControl'];
|
|
220
|
+
beforeUpload: TDef['beforeUpload'];
|
|
221
|
+
beforeDelete: TDef['beforeDelete'];
|
|
222
|
+
}
|
|
223
|
+
>;
|
|
224
|
+
/**
|
|
225
|
+
* This metadata will be added to every file uploaded to this bucket.
|
|
226
|
+
*
|
|
227
|
+
* This can be used, for example, to filter files.
|
|
228
|
+
*/
|
|
229
|
+
metadata<TMetadata extends AnyMetadata>(
|
|
230
|
+
metadata: MetadataFn<TCtx, TDef['input'], TMetadata>,
|
|
231
|
+
): Builder<
|
|
232
|
+
TCtx,
|
|
233
|
+
{
|
|
234
|
+
type: TDef['type'];
|
|
235
|
+
input: TDef['input'];
|
|
236
|
+
path: TDef['path'];
|
|
237
|
+
metadata: MetadataFn<any, any, TMetadata>;
|
|
238
|
+
bucketConfig: TDef['bucketConfig'];
|
|
239
|
+
accessControl: TDef['accessControl'];
|
|
240
|
+
beforeUpload: TDef['beforeUpload'];
|
|
241
|
+
beforeDelete: TDef['beforeDelete'];
|
|
242
|
+
}
|
|
243
|
+
>;
|
|
244
|
+
/**
|
|
245
|
+
* If you set this, your bucket will automatically be configured as a protected bucket.
|
|
246
|
+
*
|
|
247
|
+
* This means that images will only be accessible from within your app.
|
|
248
|
+
* And only if it passes the check set in this function.
|
|
249
|
+
*/
|
|
250
|
+
accessControl(accessControl: AccessControlSchema<TCtx, TDef>): Builder<
|
|
251
|
+
TCtx,
|
|
252
|
+
{
|
|
253
|
+
type: TDef['type'];
|
|
254
|
+
input: TDef['input'];
|
|
255
|
+
path: TDef['path'];
|
|
256
|
+
metadata: TDef['metadata'];
|
|
257
|
+
bucketConfig: TDef['bucketConfig'];
|
|
258
|
+
accessControl: AccessControlSchema<any, any>;
|
|
259
|
+
beforeUpload: TDef['beforeUpload'];
|
|
260
|
+
beforeDelete: TDef['beforeDelete'];
|
|
261
|
+
}
|
|
262
|
+
>;
|
|
263
|
+
/**
|
|
264
|
+
* return `true` to allow upload
|
|
265
|
+
*
|
|
266
|
+
* By default every upload from your app is allowed.
|
|
267
|
+
*/
|
|
268
|
+
beforeUpload(beforeUpload: BeforeUploadFn<TCtx, TDef>): Builder<
|
|
269
|
+
TCtx,
|
|
270
|
+
{
|
|
271
|
+
type: TDef['type'];
|
|
272
|
+
input: TDef['input'];
|
|
273
|
+
path: TDef['path'];
|
|
274
|
+
metadata: TDef['metadata'];
|
|
275
|
+
bucketConfig: TDef['bucketConfig'];
|
|
276
|
+
accessControl: TDef['accessControl'];
|
|
277
|
+
beforeUpload: BeforeUploadFn<any, any>;
|
|
278
|
+
beforeDelete: TDef['beforeDelete'];
|
|
279
|
+
}
|
|
280
|
+
>;
|
|
281
|
+
/**
|
|
282
|
+
* return `true` to allow delete
|
|
283
|
+
*
|
|
284
|
+
* This function must be defined if you want to delete files directly from the client.
|
|
285
|
+
*/
|
|
286
|
+
beforeDelete(beforeDelete: BeforeDeleteFn<TCtx, TDef>): Builder<
|
|
287
|
+
TCtx,
|
|
288
|
+
{
|
|
289
|
+
type: TDef['type'];
|
|
290
|
+
input: TDef['input'];
|
|
291
|
+
path: TDef['path'];
|
|
292
|
+
metadata: TDef['metadata'];
|
|
293
|
+
bucketConfig: TDef['bucketConfig'];
|
|
294
|
+
accessControl: TDef['accessControl'];
|
|
295
|
+
beforeUpload: TDef['beforeUpload'];
|
|
296
|
+
beforeDelete: BeforeDeleteFn<any, any>;
|
|
297
|
+
}
|
|
298
|
+
>;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
export type AnyBuilder = Builder<any, AnyDef>;
|
|
302
|
+
|
|
303
|
+
const createNewBuilder = (initDef: AnyDef, newDef: Partial<AnyDef>) => {
|
|
304
|
+
const mergedDef = {
|
|
305
|
+
...initDef,
|
|
306
|
+
...newDef,
|
|
307
|
+
};
|
|
308
|
+
return createBuilder(
|
|
309
|
+
{
|
|
310
|
+
type: mergedDef.type,
|
|
311
|
+
},
|
|
312
|
+
mergedDef,
|
|
313
|
+
);
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
function createBuilder<
|
|
317
|
+
TCtx,
|
|
318
|
+
TType extends BucketType,
|
|
319
|
+
TInput extends AnyInput = z.ZodNever,
|
|
320
|
+
TPath extends AnyPath = [],
|
|
321
|
+
TMetadata extends AnyMetadataFn = () => Record<string, never>,
|
|
322
|
+
>(
|
|
323
|
+
opts: { type: TType },
|
|
324
|
+
initDef?: Partial<AnyDef>,
|
|
325
|
+
): Builder<
|
|
326
|
+
TCtx,
|
|
327
|
+
{
|
|
328
|
+
type: TType;
|
|
329
|
+
input: TInput;
|
|
330
|
+
path: TPath;
|
|
331
|
+
metadata: TMetadata;
|
|
332
|
+
bucketConfig?: BucketConfig;
|
|
333
|
+
accessControl?: AccessControlSchema<any, any>;
|
|
334
|
+
beforeUpload?: BeforeUploadFn<any, any>;
|
|
335
|
+
beforeDelete?: BeforeDeleteFn<any, any>;
|
|
336
|
+
}
|
|
337
|
+
> {
|
|
338
|
+
const _def: AnyDef = {
|
|
339
|
+
type: opts.type,
|
|
340
|
+
input: z.never(),
|
|
341
|
+
path: [],
|
|
342
|
+
metadata: () => ({}),
|
|
343
|
+
...initDef,
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
return {
|
|
347
|
+
$config: {
|
|
348
|
+
ctx: undefined as TCtx,
|
|
349
|
+
},
|
|
350
|
+
// @ts-expect-error - I think it would be too much work to make this type correct.
|
|
351
|
+
_def,
|
|
352
|
+
input(input) {
|
|
353
|
+
return createNewBuilder(_def, {
|
|
354
|
+
input,
|
|
355
|
+
}) as any;
|
|
356
|
+
},
|
|
357
|
+
path(pathResolver) {
|
|
358
|
+
// TODO: Should throw a runtime error in the following cases:
|
|
359
|
+
// 1. in case of multiple keys in one object
|
|
360
|
+
// 2. in case of duplicate keys
|
|
361
|
+
const pathParamProxy = createPathParamProxy();
|
|
362
|
+
const params = pathResolver(pathParamProxy);
|
|
363
|
+
return createNewBuilder(_def, {
|
|
364
|
+
path: params,
|
|
365
|
+
}) as any;
|
|
366
|
+
},
|
|
367
|
+
metadata(metadata) {
|
|
368
|
+
return createNewBuilder(_def, {
|
|
369
|
+
metadata,
|
|
370
|
+
}) as any;
|
|
371
|
+
},
|
|
372
|
+
accessControl(accessControl) {
|
|
373
|
+
return createNewBuilder(_def, {
|
|
374
|
+
accessControl: accessControl,
|
|
375
|
+
}) as any;
|
|
376
|
+
},
|
|
377
|
+
beforeUpload(beforeUpload) {
|
|
378
|
+
return createNewBuilder(_def, {
|
|
379
|
+
beforeUpload,
|
|
380
|
+
}) as any;
|
|
381
|
+
},
|
|
382
|
+
beforeDelete(beforeDelete) {
|
|
383
|
+
return createNewBuilder(_def, {
|
|
384
|
+
beforeDelete,
|
|
385
|
+
}) as any;
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
class EdgeStoreBuilder<TCtx = Record<string, never>> {
|
|
391
|
+
context<TNewContext extends AnyContext>() {
|
|
392
|
+
return new EdgeStoreBuilder<TNewContext>();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
create() {
|
|
396
|
+
return createEdgeStoreInner<TCtx>()();
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
export type EdgeStoreRouter<TCtx> = {
|
|
401
|
+
/**
|
|
402
|
+
* Only used for types
|
|
403
|
+
* @internal
|
|
404
|
+
*/
|
|
405
|
+
$config: {
|
|
406
|
+
ctx: TCtx;
|
|
407
|
+
};
|
|
408
|
+
buckets: Record<string, Builder<TCtx, AnyDef>>;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
export type AnyRouter = EdgeStoreRouter<any>;
|
|
412
|
+
|
|
413
|
+
function createRouterFactory<TCtx>() {
|
|
414
|
+
return function createRouterInner<
|
|
415
|
+
TBuckets extends EdgeStoreRouter<TCtx>['buckets'],
|
|
416
|
+
>(buckets: TBuckets) {
|
|
417
|
+
return {
|
|
418
|
+
$config: {
|
|
419
|
+
ctx: undefined as TCtx,
|
|
420
|
+
},
|
|
421
|
+
buckets,
|
|
422
|
+
} satisfies EdgeStoreRouter<TCtx>;
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function initBucket<TCtx, TType extends BucketType>(
|
|
427
|
+
type: TType,
|
|
428
|
+
config?: BucketConfig,
|
|
429
|
+
) {
|
|
430
|
+
return createBuilder<TCtx, TType>({ type }, { bucketConfig: config });
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function createEdgeStoreInner<TCtx>() {
|
|
434
|
+
return function initEdgeStoreInner() {
|
|
435
|
+
return {
|
|
436
|
+
/**
|
|
437
|
+
* Builder object for creating an image bucket
|
|
438
|
+
*/
|
|
439
|
+
imageBucket(config?: BucketConfig) {
|
|
440
|
+
return initBucket<TCtx, 'IMAGE'>('IMAGE', config);
|
|
441
|
+
},
|
|
442
|
+
/**
|
|
443
|
+
* Builder object for creating a file bucket
|
|
444
|
+
*/
|
|
445
|
+
fileBucket(config?: BucketConfig) {
|
|
446
|
+
return initBucket<TCtx, 'FILE'>('FILE', config);
|
|
447
|
+
},
|
|
448
|
+
/**
|
|
449
|
+
* Create a router
|
|
450
|
+
*/
|
|
451
|
+
router: createRouterFactory<TCtx>(),
|
|
452
|
+
};
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Initialize EdgeStore - be done exactly once per backend
|
|
458
|
+
*/
|
|
459
|
+
export const initEdgeStore = new EdgeStoreBuilder();
|
|
460
|
+
|
|
461
|
+
// ↓↓↓ TYPE TESTS ↓↓↓
|
|
462
|
+
|
|
463
|
+
// type Context = {
|
|
464
|
+
// userId: string;
|
|
465
|
+
// userRole: 'admin' | 'visitor';
|
|
466
|
+
// };
|
|
467
|
+
|
|
468
|
+
// const es = initEdgeStore.context<Context>().create();
|
|
469
|
+
|
|
470
|
+
// const imagesBucket = es.imageBucket()
|
|
471
|
+
// .input(
|
|
472
|
+
// z.object({
|
|
473
|
+
// type: z.enum(['profile', 'post']),
|
|
474
|
+
// extension: z.string().optional(),
|
|
475
|
+
// }),
|
|
476
|
+
// )
|
|
477
|
+
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
478
|
+
// .metadata(({ ctx, input }) => ({
|
|
479
|
+
// extension: input.extension,
|
|
480
|
+
// role: ctx.userRole,
|
|
481
|
+
// }))
|
|
482
|
+
// .beforeUpload(() => {
|
|
483
|
+
// return true;
|
|
484
|
+
// });
|
|
485
|
+
// const a = es.imageBucket()
|
|
486
|
+
// .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
|
|
487
|
+
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
488
|
+
// .metadata(({ ctx, input }) => ({
|
|
489
|
+
// role: ctx.userRole,
|
|
490
|
+
// someMeta: input.someMeta,
|
|
491
|
+
// }))
|
|
492
|
+
// .accessControl({
|
|
493
|
+
// OR: [
|
|
494
|
+
// {
|
|
495
|
+
// userId: { path: 'author' }, // this will check if the userId is the same as the author in the path parameter
|
|
496
|
+
// },
|
|
497
|
+
// {
|
|
498
|
+
// userRole: 'admin', // this is the same as { userRole: { eq: "admin" } }
|
|
499
|
+
// },
|
|
500
|
+
// ],
|
|
501
|
+
// })
|
|
502
|
+
// .beforeUpload(({ ctx, input }) => {
|
|
503
|
+
// return true;
|
|
504
|
+
// })
|
|
505
|
+
// .beforeDelete(({ ctx, file }) => {
|
|
506
|
+
// return true;
|
|
507
|
+
// });
|
|
508
|
+
|
|
509
|
+
// const b = es.imageBucket().path(({ ctx }) => [{ author: ctx.userId }]);
|
|
510
|
+
|
|
511
|
+
// const router = es.router({
|
|
512
|
+
// original: imagesBucket,
|
|
513
|
+
// imageBucket: a,
|
|
514
|
+
// imageBucket2: b,
|
|
515
|
+
// });
|
|
516
|
+
|
|
517
|
+
// export { router };
|
|
518
|
+
|
|
519
|
+
// type ListFilesResponse<TBucket extends AnyRouter['buckets'][string]> = {
|
|
520
|
+
// data: {
|
|
521
|
+
// // url: string;
|
|
522
|
+
// // size: number;
|
|
523
|
+
// // uploadedAt: Date;
|
|
524
|
+
// // metadata: InferMetadataObject<TBucket>;
|
|
525
|
+
// path: InferBucketPathKeys<TBucket> extends string ? {
|
|
526
|
+
// [key: string]: string;
|
|
527
|
+
// } :{
|
|
528
|
+
// [TKey in InferBucketPathKeys<TBucket>]: string;
|
|
529
|
+
// };
|
|
530
|
+
// }[];
|
|
531
|
+
// pagination: {
|
|
532
|
+
// currentPage: number;
|
|
533
|
+
// totalPages: number;
|
|
534
|
+
// totalCount: number;
|
|
535
|
+
// };
|
|
536
|
+
// };
|
|
537
|
+
|
|
538
|
+
// type TPathKeys = 'author' | 'type';
|
|
539
|
+
// type TPathKeys2 = InferBucketPathKeys<AnyBuilder>;
|
|
540
|
+
|
|
541
|
+
// type ObjectWithKeys<TKeys extends string> = {
|
|
542
|
+
// [TKey in TKeys]: string;
|
|
543
|
+
// };
|
|
544
|
+
|
|
545
|
+
// type Test1 = ObjectWithKeys<TPathKeys>;
|
|
546
|
+
// type Test2 = ObjectWithKeys<TPathKeys2>;
|
|
547
|
+
// type PathKeys = InferBucketPathKeys<typeof router.buckets.imageBucket>;
|
|
548
|
+
|
|
549
|
+
// type MetadataKeys = InferMetadataObject<typeof router.buckets.imageBucket>;
|
|
550
|
+
|
|
551
|
+
// type MyEdgeStoreRouter = typeof router;
|
|
552
|
+
|
|
553
|
+
// type MyAccessControl = AccessControlSchema<Context, AnyDef>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type RecursivePathProxy = {
|
|
2
|
+
(): string;
|
|
3
|
+
ctx: any;
|
|
4
|
+
input: any;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Proxy that prints the path to the property when called.
|
|
9
|
+
*
|
|
10
|
+
* Example:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const pathParamProxy = createPathParamProxy();
|
|
14
|
+
* console.log(pathParamProxy.ctx.user.id());
|
|
15
|
+
* // Logs: "ctx.user.id"
|
|
16
|
+
* console.log(pathParamProxy.input.type());
|
|
17
|
+
* // Logs: "input.type"
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function createPathParamProxy(): RecursivePathProxy {
|
|
21
|
+
const getPath = (
|
|
22
|
+
target: string,
|
|
23
|
+
_prop: string | symbol,
|
|
24
|
+
): RecursivePathProxy => {
|
|
25
|
+
const proxyFunction: RecursivePathProxy = (() =>
|
|
26
|
+
target) as RecursivePathProxy;
|
|
27
|
+
|
|
28
|
+
return new Proxy(proxyFunction, {
|
|
29
|
+
get: (_target, propChild) => {
|
|
30
|
+
return getPath(`${target}.${String(propChild)}`, propChild);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return new Proxy((() => '') as RecursivePathProxy, {
|
|
36
|
+
get: (_target, prop) => {
|
|
37
|
+
return getPath(String(prop), String(prop));
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|