@edgestore/server 0.1.5-alpha.9 → 0.1.6

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