@edgestore/shared 0.8.0 → 1.0.0-next.1

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.
@@ -1,230 +0,0 @@
1
- import { z } from 'zod';
2
- import { EdgeStoreError } from '../errors/EdgeStoreError.mjs';
3
- import { createPathParamProxy } from './createPathParamProxy.mjs';
4
-
5
- const createNewBuilder = (initDef, newDef)=>{
6
- const mergedDef = {
7
- ...initDef,
8
- ...newDef
9
- };
10
- return createBuilder({
11
- type: mergedDef.type
12
- }, mergedDef);
13
- };
14
- function createBuilder(opts, initDef) {
15
- const _def = {
16
- type: opts.type,
17
- input: z.never(),
18
- path: [],
19
- metadata: ()=>({}),
20
- ...initDef
21
- };
22
- return {
23
- $config: {
24
- ctx: undefined
25
- },
26
- // @ts-expect-error - I think it would be too much work to make this type correct.
27
- _def,
28
- input (input) {
29
- return createNewBuilder(_def, {
30
- input
31
- });
32
- },
33
- path (pathResolver) {
34
- const pathParamProxy = createPathParamProxy();
35
- const params = pathResolver(pathParamProxy);
36
- const pathKeys = new Set();
37
- for (const param of params){
38
- const entries = Object.entries(param);
39
- if (entries.length !== 1) {
40
- const foundKeys = entries.map(([key])=>key);
41
- throw new EdgeStoreError({
42
- message: `Path params must have exactly one key. Found keys: ${foundKeys.length > 0 ? foundKeys.join(', ') : '(none)'}`,
43
- code: 'SERVER_ERROR'
44
- });
45
- }
46
- const key = entries[0]?.[0];
47
- if (key !== undefined && pathKeys.has(key)) {
48
- throw new EdgeStoreError({
49
- message: `Duplicate path param found: ${key}`,
50
- code: 'SERVER_ERROR'
51
- });
52
- }
53
- if (key !== undefined) {
54
- pathKeys.add(key);
55
- }
56
- }
57
- return createNewBuilder(_def, {
58
- path: params
59
- });
60
- },
61
- metadata (metadata) {
62
- return createNewBuilder(_def, {
63
- metadata
64
- });
65
- },
66
- accessControl (accessControl) {
67
- if (typeof accessControl === 'object' && Object.keys(accessControl).length === 0) {
68
- throw new EdgeStoreError({
69
- message: 'Empty accessControl objects are not allowed. Use accessControl("private") for signed-URL-only private files.',
70
- code: 'SERVER_ERROR'
71
- });
72
- }
73
- return createNewBuilder(_def, {
74
- accessControl: accessControl
75
- });
76
- },
77
- autoSignedUrls (config) {
78
- if (_def.accessControl === undefined) {
79
- throw new EdgeStoreError({
80
- message: 'autoSignedUrls requires a non-public bucket. Add accessControl("private") or an access-control schema first.',
81
- code: 'SERVER_ERROR'
82
- });
83
- }
84
- return createNewBuilder(_def, {
85
- autoSignedUrls: {
86
- expiresIn: config?.expiresIn,
87
- includeThumbnails: config?.includeThumbnails ?? (_def.type === 'IMAGE' ? true : false)
88
- }
89
- });
90
- },
91
- beforeUpload (beforeUpload) {
92
- return createNewBuilder(_def, {
93
- beforeUpload
94
- });
95
- },
96
- beforeDelete (beforeDelete) {
97
- return createNewBuilder(_def, {
98
- beforeDelete
99
- });
100
- }
101
- };
102
- }
103
- class EdgeStoreBuilder {
104
- context() {
105
- return new EdgeStoreBuilder();
106
- }
107
- create() {
108
- return createEdgeStoreInner()();
109
- }
110
- }
111
- function createRouterFactory() {
112
- return function createRouterInner(buckets) {
113
- return {
114
- $config: {
115
- ctx: undefined
116
- },
117
- buckets
118
- };
119
- };
120
- }
121
- function initBucket(type, config) {
122
- return createBuilder({
123
- type
124
- }, {
125
- bucketConfig: config
126
- });
127
- }
128
- function createEdgeStoreInner() {
129
- return function initEdgeStoreInner() {
130
- return {
131
- /**
132
- * Builder object for creating an image bucket
133
- */ imageBucket (config) {
134
- return initBucket('IMAGE', config);
135
- },
136
- /**
137
- * Builder object for creating a file bucket
138
- */ fileBucket (config) {
139
- return initBucket('FILE', config);
140
- },
141
- /**
142
- * Create a router
143
- */ router: createRouterFactory()
144
- };
145
- };
146
- }
147
- /**
148
- * Initialize EdgeStore - be done exactly once per backend
149
- */ const initEdgeStore = new EdgeStoreBuilder(); // ↓↓↓ TYPE TESTS ↓↓↓
150
- // type Context = {
151
- // userId: string;
152
- // userRole: 'admin' | 'visitor';
153
- // };
154
- // const es = initEdgeStore.context<Context>().create();
155
- // const imagesBucket = es.imageBucket()
156
- // .input(
157
- // z.object({
158
- // type: z.enum(['profile', 'post']),
159
- // extension: z.string().optional(),
160
- // }),
161
- // )
162
- // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
163
- // .metadata(({ ctx, input }) => ({
164
- // extension: input.extension,
165
- // role: ctx.userRole,
166
- // }))
167
- // .beforeUpload(() => {
168
- // return true;
169
- // });
170
- // const a = es.imageBucket()
171
- // .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
172
- // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
173
- // .metadata(({ ctx, input }) => ({
174
- // role: ctx.userRole,
175
- // someMeta: input.someMeta,
176
- // }))
177
- // .accessControl({
178
- // OR: [
179
- // {
180
- // userId: { path: 'author' }, // this will check if the userId is the same as the author in the path parameter
181
- // },
182
- // {
183
- // userRole: 'admin', // this is the same as { userRole: { eq: "admin" } }
184
- // },
185
- // ],
186
- // })
187
- // .beforeUpload(({ ctx, input }) => {
188
- // return true;
189
- // })
190
- // .beforeDelete(({ ctx, file }) => {
191
- // return true;
192
- // });
193
- // const b = es.imageBucket().path(({ ctx }) => [{ author: ctx.userId }]);
194
- // const router = es.router({
195
- // original: imagesBucket,
196
- // imageBucket: a,
197
- // imageBucket2: b,
198
- // });
199
- // export { router };
200
- // type ListFilesResponse<TBucket extends AnyRouter['buckets'][string]> = {
201
- // data: {
202
- // // url: string;
203
- // // size: number;
204
- // // uploadedAt: Date;
205
- // // metadata: InferMetadataObject<TBucket>;
206
- // path: InferBucketPathKeys<TBucket> extends string ? {
207
- // [key: string]: string;
208
- // } :{
209
- // [TKey in InferBucketPathKeys<TBucket>]: string;
210
- // };
211
- // }[];
212
- // pagination: {
213
- // currentPage: number;
214
- // totalPages: number;
215
- // totalCount: number;
216
- // };
217
- // };
218
- // type TPathKeys = 'author' | 'type';
219
- // type TPathKeys2 = InferBucketPathKeys<AnyBuilder>;
220
- // type ObjectWithKeys<TKeys extends string> = {
221
- // [TKey in TKeys]: string;
222
- // };
223
- // type Test1 = ObjectWithKeys<TPathKeys>;
224
- // type Test2 = ObjectWithKeys<TPathKeys2>;
225
- // type PathKeys = InferBucketPathKeys<typeof router.buckets.imageBucket>;
226
- // type MetadataKeys = InferMetadataObject<typeof router.buckets.imageBucket>;
227
- // type MyEdgeStoreRouter = typeof router;
228
- // type MyAccessControl = AccessControlSchema<Context, AnyDef>;
229
-
230
- export { initEdgeStore };
@@ -1,29 +0,0 @@
1
- /**
2
- * Creates a Proxy that prints the path to the property when called.
3
- *
4
- * Example:
5
- *
6
- * ```ts
7
- * const pathParamProxy = createPathParamProxy();
8
- * console.log(pathParamProxy.ctx.user.id());
9
- * // Logs: "ctx.user.id"
10
- * console.log(pathParamProxy.input.type());
11
- * // Logs: "input.type"
12
- * ```
13
- */ function createPathParamProxy() {
14
- const getPath = (target, _prop)=>{
15
- const proxyFunction = ()=>target;
16
- return new Proxy(proxyFunction, {
17
- get: (_target, propChild)=>{
18
- return getPath(`${target}.${String(propChild)}`);
19
- }
20
- });
21
- };
22
- return new Proxy(()=>'', {
23
- get: (_target, prop)=>{
24
- return getPath(String(prop));
25
- }
26
- });
27
- }
28
-
29
- export { createPathParamProxy };