@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,47 @@
|
|
|
1
|
+
import { type Simplify } from '../types';
|
|
2
|
+
export declare const EDGE_STORE_ERROR_CODES: {
|
|
3
|
+
readonly BAD_REQUEST: 400;
|
|
4
|
+
readonly FILE_TOO_LARGE: 400;
|
|
5
|
+
readonly MIME_TYPE_NOT_ALLOWED: 400;
|
|
6
|
+
readonly UNAUTHORIZED: 401;
|
|
7
|
+
readonly UPLOAD_NOT_ALLOWED: 403;
|
|
8
|
+
readonly DELETE_NOT_ALLOWED: 403;
|
|
9
|
+
readonly CREATE_CONTEXT_ERROR: 500;
|
|
10
|
+
readonly SERVER_ERROR: 500;
|
|
11
|
+
};
|
|
12
|
+
export type EdgeStoreErrorCodeKey = keyof typeof EDGE_STORE_ERROR_CODES;
|
|
13
|
+
export type EdgeStoreErrorDetails<TCode extends EdgeStoreErrorCodeKey> = TCode extends 'FILE_TOO_LARGE' ? {
|
|
14
|
+
maxFileSize: number;
|
|
15
|
+
fileSize: number;
|
|
16
|
+
} : TCode extends 'MIME_TYPE_NOT_ALLOWED' ? {
|
|
17
|
+
allowedMimeTypes: string[];
|
|
18
|
+
mimeType: string;
|
|
19
|
+
} : never;
|
|
20
|
+
export type EdgeStoreJsonResponse = Simplify<{
|
|
21
|
+
message: string;
|
|
22
|
+
code: 'FILE_TOO_LARGE';
|
|
23
|
+
details: EdgeStoreErrorDetails<'FILE_TOO_LARGE'>;
|
|
24
|
+
} | {
|
|
25
|
+
message: string;
|
|
26
|
+
code: 'MIME_TYPE_NOT_ALLOWED';
|
|
27
|
+
details: EdgeStoreErrorDetails<'MIME_TYPE_NOT_ALLOWED'>;
|
|
28
|
+
} | {
|
|
29
|
+
message: string;
|
|
30
|
+
code: Exclude<EdgeStoreErrorCodeKey, 'FILE_TOO_LARGE' | 'MIME_TYPE_NOT_ALLOWED'>;
|
|
31
|
+
}>;
|
|
32
|
+
export declare class EdgeStoreError<TCode extends EdgeStoreErrorCodeKey> extends Error {
|
|
33
|
+
readonly cause?: Error;
|
|
34
|
+
readonly code: TCode;
|
|
35
|
+
readonly level: 'error' | 'warn';
|
|
36
|
+
readonly details: EdgeStoreErrorDetails<TCode>;
|
|
37
|
+
constructor(opts: {
|
|
38
|
+
message: string;
|
|
39
|
+
code: TCode;
|
|
40
|
+
cause?: Error;
|
|
41
|
+
} & (EdgeStoreErrorDetails<TCode> extends undefined ? object : {
|
|
42
|
+
details: EdgeStoreErrorDetails<TCode>;
|
|
43
|
+
}));
|
|
44
|
+
formattedMessage(): string;
|
|
45
|
+
formattedJson(): EdgeStoreJsonResponse;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=EdgeStoreError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EdgeStoreError.d.ts","sourceRoot":"","sources":["../../src/errors/EdgeStoreError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGzC,eAAO,MAAM,sBAAsB;;;;;;;;;CASzB,CAAC;AAEX,MAAM,MAAM,qBAAqB,GAAG,MAAM,OAAO,sBAAsB,CAAC;AAExE,MAAM,MAAM,qBAAqB,CAAC,KAAK,SAAS,qBAAqB,IACnE,KAAK,SAAS,gBAAgB,GAC1B;IACE,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CAClB,GACD,KAAK,SAAS,uBAAuB,GACrC;IACE,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC;CAClB,GACD,KAAK,CAAC;AAEZ,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CACxC;IACE,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,gBAAgB,CAAC;IACvB,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,GACD;IACE,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,uBAAuB,CAAC;IAC9B,OAAO,EAAE,qBAAqB,CAAC,uBAAuB,CAAC,CAAC;CACzD,GACD;IACE,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CACX,qBAAqB,EACrB,gBAAgB,GAAG,uBAAuB,CAC3C,CAAC;CACH,CACJ,CAAC;AAEF,qBAAa,cAAc,CAAC,KAAK,SAAS,qBAAqB,CAAE,SAAQ,KAAK;IAC5E,SAAgB,KAAK,CAAC,EAAE,KAAK,CAAC;IAC9B,SAAgB,IAAI,EAAE,KAAK,CAAC;IAC5B,SAAgB,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC;IACxC,SAAgB,OAAO,EAAE,qBAAqB,CAAC,KAAK,CAAC,CAAC;gBAGpD,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE,KAAK,CAAC;QACZ,KAAK,CAAC,EAAE,KAAK,CAAC;KACf,GAAG,CAAC,qBAAqB,CAAC,KAAK,CAAC,SAAS,SAAS,GAC/C,MAAM,GACN;QACE,OAAO,EAAE,qBAAqB,CAAC,KAAK,CAAC,CAAC;KACvC,CAAC;IAWR,gBAAgB,IAAI,MAAM;IAM1B,aAAa,IAAI,qBAAqB;CAQvC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type EdgeStoreJsonResponse } from './EdgeStoreError';
|
|
2
|
+
export declare class EdgeStoreApiClientError extends Error {
|
|
3
|
+
readonly data: EdgeStoreJsonResponse;
|
|
4
|
+
constructor(opts: {
|
|
5
|
+
response: EdgeStoreJsonResponse;
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
export * from './EdgeStoreError';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAE9D,qBAAa,uBAAwB,SAAQ,KAAK;IAChD,SAAgB,IAAI,EAAE,qBAAqB,CAAC;gBAEhC,IAAI,EAAE;QAAE,QAAQ,EAAE,qBAAqB,CAAA;KAAE;CAMtD;AAED,cAAc,kBAAkB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './errors';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export * from './internals/bucketBuilder';
|
|
4
|
+
export * from './internals/types';
|
|
5
|
+
export * from './internals/providerTypes';
|
|
6
|
+
export * from './internals/sharedFuncTypes';
|
|
2
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,270 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _define_property = require('@swc/helpers/_/_define_property');
|
|
6
|
+
var zod = require('zod');
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */ const EDGE_STORE_ERROR_CODES = {
|
|
9
|
+
BAD_REQUEST: 400,
|
|
10
|
+
FILE_TOO_LARGE: 400,
|
|
11
|
+
MIME_TYPE_NOT_ALLOWED: 400,
|
|
12
|
+
UNAUTHORIZED: 401,
|
|
13
|
+
UPLOAD_NOT_ALLOWED: 403,
|
|
14
|
+
DELETE_NOT_ALLOWED: 403,
|
|
15
|
+
CREATE_CONTEXT_ERROR: 500,
|
|
16
|
+
SERVER_ERROR: 500
|
|
17
|
+
};
|
|
18
|
+
class EdgeStoreError extends Error {
|
|
19
|
+
formattedMessage() {
|
|
20
|
+
return `${this.message}${this.details ? `\n Details: ${JSON.stringify(this.details)}` : ''}${this.cause ? `\n Caused by: ${this.cause.message}` : ''}`;
|
|
21
|
+
}
|
|
22
|
+
formattedJson() {
|
|
23
|
+
return {
|
|
24
|
+
message: this.code === 'SERVER_ERROR' ? 'Internal server error' : this.message,
|
|
25
|
+
code: this.code,
|
|
26
|
+
details: this.details
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
constructor(opts){
|
|
30
|
+
super(opts.message);
|
|
31
|
+
_define_property._(this, "cause", void 0);
|
|
32
|
+
_define_property._(this, "code", void 0);
|
|
33
|
+
_define_property._(this, "level", void 0);
|
|
34
|
+
_define_property._(this, "details", void 0);
|
|
35
|
+
this.name = 'EdgeStoreError';
|
|
36
|
+
this.code = opts.code;
|
|
37
|
+
this.cause = opts.cause;
|
|
38
|
+
this.level = EDGE_STORE_ERROR_CODES[opts.code] >= 500 ? 'error' : 'warn';
|
|
39
|
+
this.details = 'details' in opts ? opts.details : undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class EdgeStoreApiClientError extends Error {
|
|
44
|
+
constructor(opts){
|
|
45
|
+
super(opts.response.message);
|
|
46
|
+
_define_property._(this, "data", void 0);
|
|
47
|
+
this.name = 'EdgeStoreApiClientError';
|
|
48
|
+
this.data = opts.response;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a Proxy that prints the path to the property when called.
|
|
54
|
+
*
|
|
55
|
+
* Example:
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* const pathParamProxy = createPathParamProxy();
|
|
59
|
+
* console.log(pathParamProxy.ctx.user.id());
|
|
60
|
+
* // Logs: "ctx.user.id"
|
|
61
|
+
* console.log(pathParamProxy.input.type());
|
|
62
|
+
* // Logs: "input.type"
|
|
63
|
+
* ```
|
|
64
|
+
*/ function createPathParamProxy() {
|
|
65
|
+
const getPath = (target, _prop)=>{
|
|
66
|
+
const proxyFunction = ()=>target;
|
|
67
|
+
return new Proxy(proxyFunction, {
|
|
68
|
+
get: (_target, propChild)=>{
|
|
69
|
+
return getPath(`${target}.${String(propChild)}`);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
return new Proxy(()=>'', {
|
|
74
|
+
get: (_target, prop)=>{
|
|
75
|
+
return getPath(String(prop));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const createNewBuilder = (initDef, newDef)=>{
|
|
81
|
+
const mergedDef = {
|
|
82
|
+
...initDef,
|
|
83
|
+
...newDef
|
|
84
|
+
};
|
|
85
|
+
return createBuilder({
|
|
86
|
+
type: mergedDef.type
|
|
87
|
+
}, mergedDef);
|
|
88
|
+
};
|
|
89
|
+
function createBuilder(opts, initDef) {
|
|
90
|
+
const _def = {
|
|
91
|
+
type: opts.type,
|
|
92
|
+
input: zod.z.never(),
|
|
93
|
+
path: [],
|
|
94
|
+
metadata: ()=>({}),
|
|
95
|
+
...initDef
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
$config: {
|
|
99
|
+
ctx: undefined
|
|
100
|
+
},
|
|
101
|
+
// @ts-expect-error - I think it would be too much work to make this type correct.
|
|
102
|
+
_def,
|
|
103
|
+
input (input) {
|
|
104
|
+
return createNewBuilder(_def, {
|
|
105
|
+
input
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
path (pathResolver) {
|
|
109
|
+
// TODO: Should throw a runtime error in the following cases:
|
|
110
|
+
// 1. in case of multiple keys in one object
|
|
111
|
+
// 2. in case of duplicate keys
|
|
112
|
+
const pathParamProxy = createPathParamProxy();
|
|
113
|
+
const params = pathResolver(pathParamProxy);
|
|
114
|
+
return createNewBuilder(_def, {
|
|
115
|
+
path: params
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
metadata (metadata) {
|
|
119
|
+
return createNewBuilder(_def, {
|
|
120
|
+
metadata
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
accessControl (accessControl) {
|
|
124
|
+
return createNewBuilder(_def, {
|
|
125
|
+
accessControl: accessControl
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
beforeUpload (beforeUpload) {
|
|
129
|
+
return createNewBuilder(_def, {
|
|
130
|
+
beforeUpload
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
beforeDelete (beforeDelete) {
|
|
134
|
+
return createNewBuilder(_def, {
|
|
135
|
+
beforeDelete
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
class EdgeStoreBuilder {
|
|
141
|
+
context() {
|
|
142
|
+
return new EdgeStoreBuilder();
|
|
143
|
+
}
|
|
144
|
+
create() {
|
|
145
|
+
return createEdgeStoreInner()();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function createRouterFactory() {
|
|
149
|
+
return function createRouterInner(buckets) {
|
|
150
|
+
return {
|
|
151
|
+
$config: {
|
|
152
|
+
ctx: undefined
|
|
153
|
+
},
|
|
154
|
+
buckets
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function initBucket(type, config) {
|
|
159
|
+
return createBuilder({
|
|
160
|
+
type
|
|
161
|
+
}, {
|
|
162
|
+
bucketConfig: config
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
function createEdgeStoreInner() {
|
|
166
|
+
return function initEdgeStoreInner() {
|
|
167
|
+
return {
|
|
168
|
+
/**
|
|
169
|
+
* Builder object for creating an image bucket
|
|
170
|
+
*/ imageBucket (config) {
|
|
171
|
+
return initBucket('IMAGE', config);
|
|
172
|
+
},
|
|
173
|
+
/**
|
|
174
|
+
* Builder object for creating a file bucket
|
|
175
|
+
*/ fileBucket (config) {
|
|
176
|
+
return initBucket('FILE', config);
|
|
177
|
+
},
|
|
178
|
+
/**
|
|
179
|
+
* Create a router
|
|
180
|
+
*/ router: createRouterFactory()
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Initialize EdgeStore - be done exactly once per backend
|
|
186
|
+
*/ const initEdgeStore = new EdgeStoreBuilder(); // ↓↓↓ TYPE TESTS ↓↓↓
|
|
187
|
+
// type Context = {
|
|
188
|
+
// userId: string;
|
|
189
|
+
// userRole: 'admin' | 'visitor';
|
|
190
|
+
// };
|
|
191
|
+
// const es = initEdgeStore.context<Context>().create();
|
|
192
|
+
// const imagesBucket = es.imageBucket()
|
|
193
|
+
// .input(
|
|
194
|
+
// z.object({
|
|
195
|
+
// type: z.enum(['profile', 'post']),
|
|
196
|
+
// extension: z.string().optional(),
|
|
197
|
+
// }),
|
|
198
|
+
// )
|
|
199
|
+
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
200
|
+
// .metadata(({ ctx, input }) => ({
|
|
201
|
+
// extension: input.extension,
|
|
202
|
+
// role: ctx.userRole,
|
|
203
|
+
// }))
|
|
204
|
+
// .beforeUpload(() => {
|
|
205
|
+
// return true;
|
|
206
|
+
// });
|
|
207
|
+
// const a = es.imageBucket()
|
|
208
|
+
// .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
|
|
209
|
+
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
210
|
+
// .metadata(({ ctx, input }) => ({
|
|
211
|
+
// role: ctx.userRole,
|
|
212
|
+
// someMeta: input.someMeta,
|
|
213
|
+
// }))
|
|
214
|
+
// .accessControl({
|
|
215
|
+
// OR: [
|
|
216
|
+
// {
|
|
217
|
+
// userId: { path: 'author' }, // this will check if the userId is the same as the author in the path parameter
|
|
218
|
+
// },
|
|
219
|
+
// {
|
|
220
|
+
// userRole: 'admin', // this is the same as { userRole: { eq: "admin" } }
|
|
221
|
+
// },
|
|
222
|
+
// ],
|
|
223
|
+
// })
|
|
224
|
+
// .beforeUpload(({ ctx, input }) => {
|
|
225
|
+
// return true;
|
|
226
|
+
// })
|
|
227
|
+
// .beforeDelete(({ ctx, file }) => {
|
|
228
|
+
// return true;
|
|
229
|
+
// });
|
|
230
|
+
// const b = es.imageBucket().path(({ ctx }) => [{ author: ctx.userId }]);
|
|
231
|
+
// const router = es.router({
|
|
232
|
+
// original: imagesBucket,
|
|
233
|
+
// imageBucket: a,
|
|
234
|
+
// imageBucket2: b,
|
|
235
|
+
// });
|
|
236
|
+
// export { router };
|
|
237
|
+
// type ListFilesResponse<TBucket extends AnyRouter['buckets'][string]> = {
|
|
238
|
+
// data: {
|
|
239
|
+
// // url: string;
|
|
240
|
+
// // size: number;
|
|
241
|
+
// // uploadedAt: Date;
|
|
242
|
+
// // metadata: InferMetadataObject<TBucket>;
|
|
243
|
+
// path: InferBucketPathKeys<TBucket> extends string ? {
|
|
244
|
+
// [key: string]: string;
|
|
245
|
+
// } :{
|
|
246
|
+
// [TKey in InferBucketPathKeys<TBucket>]: string;
|
|
247
|
+
// };
|
|
248
|
+
// }[];
|
|
249
|
+
// pagination: {
|
|
250
|
+
// currentPage: number;
|
|
251
|
+
// totalPages: number;
|
|
252
|
+
// totalCount: number;
|
|
253
|
+
// };
|
|
254
|
+
// };
|
|
255
|
+
// type TPathKeys = 'author' | 'type';
|
|
256
|
+
// type TPathKeys2 = InferBucketPathKeys<AnyBuilder>;
|
|
257
|
+
// type ObjectWithKeys<TKeys extends string> = {
|
|
258
|
+
// [TKey in TKeys]: string;
|
|
259
|
+
// };
|
|
260
|
+
// type Test1 = ObjectWithKeys<TPathKeys>;
|
|
261
|
+
// type Test2 = ObjectWithKeys<TPathKeys2>;
|
|
262
|
+
// type PathKeys = InferBucketPathKeys<typeof router.buckets.imageBucket>;
|
|
263
|
+
// type MetadataKeys = InferMetadataObject<typeof router.buckets.imageBucket>;
|
|
264
|
+
// type MyEdgeStoreRouter = typeof router;
|
|
265
|
+
// type MyAccessControl = AccessControlSchema<Context, AnyDef>;
|
|
266
|
+
|
|
267
|
+
exports.EDGE_STORE_ERROR_CODES = EDGE_STORE_ERROR_CODES;
|
|
268
|
+
exports.EdgeStoreApiClientError = EdgeStoreApiClientError;
|
|
269
|
+
exports.EdgeStoreError = EdgeStoreError;
|
|
270
|
+
exports.initEdgeStore = initEdgeStore;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,263 @@
|
|
|
1
|
+
import { _ } from '@swc/helpers/_/_define_property';
|
|
2
|
+
import { z } from 'zod';
|
|
1
3
|
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */ 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
|
+
};
|
|
14
|
+
class EdgeStoreError extends Error {
|
|
15
|
+
formattedMessage() {
|
|
16
|
+
return `${this.message}${this.details ? `\n Details: ${JSON.stringify(this.details)}` : ''}${this.cause ? `\n Caused by: ${this.cause.message}` : ''}`;
|
|
17
|
+
}
|
|
18
|
+
formattedJson() {
|
|
19
|
+
return {
|
|
20
|
+
message: this.code === 'SERVER_ERROR' ? 'Internal server error' : this.message,
|
|
21
|
+
code: this.code,
|
|
22
|
+
details: this.details
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
constructor(opts){
|
|
26
|
+
super(opts.message);
|
|
27
|
+
_(this, "cause", void 0);
|
|
28
|
+
_(this, "code", void 0);
|
|
29
|
+
_(this, "level", void 0);
|
|
30
|
+
_(this, "details", void 0);
|
|
31
|
+
this.name = 'EdgeStoreError';
|
|
32
|
+
this.code = opts.code;
|
|
33
|
+
this.cause = opts.cause;
|
|
34
|
+
this.level = EDGE_STORE_ERROR_CODES[opts.code] >= 500 ? 'error' : 'warn';
|
|
35
|
+
this.details = 'details' in opts ? opts.details : undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class EdgeStoreApiClientError extends Error {
|
|
40
|
+
constructor(opts){
|
|
41
|
+
super(opts.response.message);
|
|
42
|
+
_(this, "data", void 0);
|
|
43
|
+
this.name = 'EdgeStoreApiClientError';
|
|
44
|
+
this.data = opts.response;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a Proxy that prints the path to the property when called.
|
|
50
|
+
*
|
|
51
|
+
* Example:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* const pathParamProxy = createPathParamProxy();
|
|
55
|
+
* console.log(pathParamProxy.ctx.user.id());
|
|
56
|
+
* // Logs: "ctx.user.id"
|
|
57
|
+
* console.log(pathParamProxy.input.type());
|
|
58
|
+
* // Logs: "input.type"
|
|
59
|
+
* ```
|
|
60
|
+
*/ function createPathParamProxy() {
|
|
61
|
+
const getPath = (target, _prop)=>{
|
|
62
|
+
const proxyFunction = ()=>target;
|
|
63
|
+
return new Proxy(proxyFunction, {
|
|
64
|
+
get: (_target, propChild)=>{
|
|
65
|
+
return getPath(`${target}.${String(propChild)}`);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
return new Proxy(()=>'', {
|
|
70
|
+
get: (_target, prop)=>{
|
|
71
|
+
return getPath(String(prop));
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const createNewBuilder = (initDef, newDef)=>{
|
|
77
|
+
const mergedDef = {
|
|
78
|
+
...initDef,
|
|
79
|
+
...newDef
|
|
80
|
+
};
|
|
81
|
+
return createBuilder({
|
|
82
|
+
type: mergedDef.type
|
|
83
|
+
}, mergedDef);
|
|
84
|
+
};
|
|
85
|
+
function createBuilder(opts, initDef) {
|
|
86
|
+
const _def = {
|
|
87
|
+
type: opts.type,
|
|
88
|
+
input: z.never(),
|
|
89
|
+
path: [],
|
|
90
|
+
metadata: ()=>({}),
|
|
91
|
+
...initDef
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
$config: {
|
|
95
|
+
ctx: undefined
|
|
96
|
+
},
|
|
97
|
+
// @ts-expect-error - I think it would be too much work to make this type correct.
|
|
98
|
+
_def,
|
|
99
|
+
input (input) {
|
|
100
|
+
return createNewBuilder(_def, {
|
|
101
|
+
input
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
path (pathResolver) {
|
|
105
|
+
// TODO: Should throw a runtime error in the following cases:
|
|
106
|
+
// 1. in case of multiple keys in one object
|
|
107
|
+
// 2. in case of duplicate keys
|
|
108
|
+
const pathParamProxy = createPathParamProxy();
|
|
109
|
+
const params = pathResolver(pathParamProxy);
|
|
110
|
+
return createNewBuilder(_def, {
|
|
111
|
+
path: params
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
metadata (metadata) {
|
|
115
|
+
return createNewBuilder(_def, {
|
|
116
|
+
metadata
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
accessControl (accessControl) {
|
|
120
|
+
return createNewBuilder(_def, {
|
|
121
|
+
accessControl: accessControl
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
beforeUpload (beforeUpload) {
|
|
125
|
+
return createNewBuilder(_def, {
|
|
126
|
+
beforeUpload
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
beforeDelete (beforeDelete) {
|
|
130
|
+
return createNewBuilder(_def, {
|
|
131
|
+
beforeDelete
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
class EdgeStoreBuilder {
|
|
137
|
+
context() {
|
|
138
|
+
return new EdgeStoreBuilder();
|
|
139
|
+
}
|
|
140
|
+
create() {
|
|
141
|
+
return createEdgeStoreInner()();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function createRouterFactory() {
|
|
145
|
+
return function createRouterInner(buckets) {
|
|
146
|
+
return {
|
|
147
|
+
$config: {
|
|
148
|
+
ctx: undefined
|
|
149
|
+
},
|
|
150
|
+
buckets
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function initBucket(type, config) {
|
|
155
|
+
return createBuilder({
|
|
156
|
+
type
|
|
157
|
+
}, {
|
|
158
|
+
bucketConfig: config
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function createEdgeStoreInner() {
|
|
162
|
+
return function initEdgeStoreInner() {
|
|
163
|
+
return {
|
|
164
|
+
/**
|
|
165
|
+
* Builder object for creating an image bucket
|
|
166
|
+
*/ imageBucket (config) {
|
|
167
|
+
return initBucket('IMAGE', config);
|
|
168
|
+
},
|
|
169
|
+
/**
|
|
170
|
+
* Builder object for creating a file bucket
|
|
171
|
+
*/ fileBucket (config) {
|
|
172
|
+
return initBucket('FILE', config);
|
|
173
|
+
},
|
|
174
|
+
/**
|
|
175
|
+
* Create a router
|
|
176
|
+
*/ router: createRouterFactory()
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Initialize EdgeStore - be done exactly once per backend
|
|
182
|
+
*/ const initEdgeStore = new EdgeStoreBuilder(); // ↓↓↓ TYPE TESTS ↓↓↓
|
|
183
|
+
// type Context = {
|
|
184
|
+
// userId: string;
|
|
185
|
+
// userRole: 'admin' | 'visitor';
|
|
186
|
+
// };
|
|
187
|
+
// const es = initEdgeStore.context<Context>().create();
|
|
188
|
+
// const imagesBucket = es.imageBucket()
|
|
189
|
+
// .input(
|
|
190
|
+
// z.object({
|
|
191
|
+
// type: z.enum(['profile', 'post']),
|
|
192
|
+
// extension: z.string().optional(),
|
|
193
|
+
// }),
|
|
194
|
+
// )
|
|
195
|
+
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
196
|
+
// .metadata(({ ctx, input }) => ({
|
|
197
|
+
// extension: input.extension,
|
|
198
|
+
// role: ctx.userRole,
|
|
199
|
+
// }))
|
|
200
|
+
// .beforeUpload(() => {
|
|
201
|
+
// return true;
|
|
202
|
+
// });
|
|
203
|
+
// const a = es.imageBucket()
|
|
204
|
+
// .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
|
|
205
|
+
// .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
|
|
206
|
+
// .metadata(({ ctx, input }) => ({
|
|
207
|
+
// role: ctx.userRole,
|
|
208
|
+
// someMeta: input.someMeta,
|
|
209
|
+
// }))
|
|
210
|
+
// .accessControl({
|
|
211
|
+
// OR: [
|
|
212
|
+
// {
|
|
213
|
+
// userId: { path: 'author' }, // this will check if the userId is the same as the author in the path parameter
|
|
214
|
+
// },
|
|
215
|
+
// {
|
|
216
|
+
// userRole: 'admin', // this is the same as { userRole: { eq: "admin" } }
|
|
217
|
+
// },
|
|
218
|
+
// ],
|
|
219
|
+
// })
|
|
220
|
+
// .beforeUpload(({ ctx, input }) => {
|
|
221
|
+
// return true;
|
|
222
|
+
// })
|
|
223
|
+
// .beforeDelete(({ ctx, file }) => {
|
|
224
|
+
// return true;
|
|
225
|
+
// });
|
|
226
|
+
// const b = es.imageBucket().path(({ ctx }) => [{ author: ctx.userId }]);
|
|
227
|
+
// const router = es.router({
|
|
228
|
+
// original: imagesBucket,
|
|
229
|
+
// imageBucket: a,
|
|
230
|
+
// imageBucket2: b,
|
|
231
|
+
// });
|
|
232
|
+
// export { router };
|
|
233
|
+
// type ListFilesResponse<TBucket extends AnyRouter['buckets'][string]> = {
|
|
234
|
+
// data: {
|
|
235
|
+
// // url: string;
|
|
236
|
+
// // size: number;
|
|
237
|
+
// // uploadedAt: Date;
|
|
238
|
+
// // metadata: InferMetadataObject<TBucket>;
|
|
239
|
+
// path: InferBucketPathKeys<TBucket> extends string ? {
|
|
240
|
+
// [key: string]: string;
|
|
241
|
+
// } :{
|
|
242
|
+
// [TKey in InferBucketPathKeys<TBucket>]: string;
|
|
243
|
+
// };
|
|
244
|
+
// }[];
|
|
245
|
+
// pagination: {
|
|
246
|
+
// currentPage: number;
|
|
247
|
+
// totalPages: number;
|
|
248
|
+
// totalCount: number;
|
|
249
|
+
// };
|
|
250
|
+
// };
|
|
251
|
+
// type TPathKeys = 'author' | 'type';
|
|
252
|
+
// type TPathKeys2 = InferBucketPathKeys<AnyBuilder>;
|
|
253
|
+
// type ObjectWithKeys<TKeys extends string> = {
|
|
254
|
+
// [TKey in TKeys]: string;
|
|
255
|
+
// };
|
|
256
|
+
// type Test1 = ObjectWithKeys<TPathKeys>;
|
|
257
|
+
// type Test2 = ObjectWithKeys<TPathKeys2>;
|
|
258
|
+
// type PathKeys = InferBucketPathKeys<typeof router.buckets.imageBucket>;
|
|
259
|
+
// type MetadataKeys = InferMetadataObject<typeof router.buckets.imageBucket>;
|
|
260
|
+
// type MyEdgeStoreRouter = typeof router;
|
|
261
|
+
// type MyAccessControl = AccessControlSchema<Context, AnyDef>;
|
|
262
|
+
|
|
263
|
+
export { EDGE_STORE_ERROR_CODES, EdgeStoreApiClientError, EdgeStoreError, initEdgeStore };
|