@edgestore/shared 0.7.0 → 1.0.0-next.0

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,191 +0,0 @@
1
- import { z } from 'zod';
2
- import { createPathParamProxy } from './createPathParamProxy.mjs';
3
-
4
- const createNewBuilder = (initDef, newDef)=>{
5
- const mergedDef = {
6
- ...initDef,
7
- ...newDef
8
- };
9
- return createBuilder({
10
- type: mergedDef.type
11
- }, mergedDef);
12
- };
13
- function createBuilder(opts, initDef) {
14
- const _def = {
15
- type: opts.type,
16
- input: z.never(),
17
- path: [],
18
- metadata: ()=>({}),
19
- ...initDef
20
- };
21
- return {
22
- $config: {
23
- ctx: undefined
24
- },
25
- // @ts-expect-error - I think it would be too much work to make this type correct.
26
- _def,
27
- input (input) {
28
- return createNewBuilder(_def, {
29
- input
30
- });
31
- },
32
- path (pathResolver) {
33
- // TODO: Should throw a runtime error in the following cases:
34
- // 1. in case of multiple keys in one object
35
- // 2. in case of duplicate keys
36
- const pathParamProxy = createPathParamProxy();
37
- const params = pathResolver(pathParamProxy);
38
- return createNewBuilder(_def, {
39
- path: params
40
- });
41
- },
42
- metadata (metadata) {
43
- return createNewBuilder(_def, {
44
- metadata
45
- });
46
- },
47
- accessControl (accessControl) {
48
- return createNewBuilder(_def, {
49
- accessControl: accessControl
50
- });
51
- },
52
- beforeUpload (beforeUpload) {
53
- return createNewBuilder(_def, {
54
- beforeUpload
55
- });
56
- },
57
- beforeDelete (beforeDelete) {
58
- return createNewBuilder(_def, {
59
- beforeDelete
60
- });
61
- }
62
- };
63
- }
64
- class EdgeStoreBuilder {
65
- context() {
66
- return new EdgeStoreBuilder();
67
- }
68
- create() {
69
- return createEdgeStoreInner()();
70
- }
71
- }
72
- function createRouterFactory() {
73
- return function createRouterInner(buckets) {
74
- return {
75
- $config: {
76
- ctx: undefined
77
- },
78
- buckets
79
- };
80
- };
81
- }
82
- function initBucket(type, config) {
83
- return createBuilder({
84
- type
85
- }, {
86
- bucketConfig: config
87
- });
88
- }
89
- function createEdgeStoreInner() {
90
- return function initEdgeStoreInner() {
91
- return {
92
- /**
93
- * Builder object for creating an image bucket
94
- */ imageBucket (config) {
95
- return initBucket('IMAGE', config);
96
- },
97
- /**
98
- * Builder object for creating a file bucket
99
- */ fileBucket (config) {
100
- return initBucket('FILE', config);
101
- },
102
- /**
103
- * Create a router
104
- */ router: createRouterFactory()
105
- };
106
- };
107
- }
108
- /**
109
- * Initialize EdgeStore - be done exactly once per backend
110
- */ const initEdgeStore = new EdgeStoreBuilder(); // ↓↓↓ TYPE TESTS ↓↓↓
111
- // type Context = {
112
- // userId: string;
113
- // userRole: 'admin' | 'visitor';
114
- // };
115
- // const es = initEdgeStore.context<Context>().create();
116
- // const imagesBucket = es.imageBucket()
117
- // .input(
118
- // z.object({
119
- // type: z.enum(['profile', 'post']),
120
- // extension: z.string().optional(),
121
- // }),
122
- // )
123
- // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
124
- // .metadata(({ ctx, input }) => ({
125
- // extension: input.extension,
126
- // role: ctx.userRole,
127
- // }))
128
- // .beforeUpload(() => {
129
- // return true;
130
- // });
131
- // const a = es.imageBucket()
132
- // .input(z.object({ type: z.string(), someMeta: z.string().optional() }))
133
- // .path(({ ctx, input }) => [{ author: ctx.userId }, { type: input.type }])
134
- // .metadata(({ ctx, input }) => ({
135
- // role: ctx.userRole,
136
- // someMeta: input.someMeta,
137
- // }))
138
- // .accessControl({
139
- // OR: [
140
- // {
141
- // userId: { path: 'author' }, // this will check if the userId is the same as the author in the path parameter
142
- // },
143
- // {
144
- // userRole: 'admin', // this is the same as { userRole: { eq: "admin" } }
145
- // },
146
- // ],
147
- // })
148
- // .beforeUpload(({ ctx, input }) => {
149
- // return true;
150
- // })
151
- // .beforeDelete(({ ctx, file }) => {
152
- // return true;
153
- // });
154
- // const b = es.imageBucket().path(({ ctx }) => [{ author: ctx.userId }]);
155
- // const router = es.router({
156
- // original: imagesBucket,
157
- // imageBucket: a,
158
- // imageBucket2: b,
159
- // });
160
- // export { router };
161
- // type ListFilesResponse<TBucket extends AnyRouter['buckets'][string]> = {
162
- // data: {
163
- // // url: string;
164
- // // size: number;
165
- // // uploadedAt: Date;
166
- // // metadata: InferMetadataObject<TBucket>;
167
- // path: InferBucketPathKeys<TBucket> extends string ? {
168
- // [key: string]: string;
169
- // } :{
170
- // [TKey in InferBucketPathKeys<TBucket>]: string;
171
- // };
172
- // }[];
173
- // pagination: {
174
- // currentPage: number;
175
- // totalPages: number;
176
- // totalCount: number;
177
- // };
178
- // };
179
- // type TPathKeys = 'author' | 'type';
180
- // type TPathKeys2 = InferBucketPathKeys<AnyBuilder>;
181
- // type ObjectWithKeys<TKeys extends string> = {
182
- // [TKey in TKeys]: string;
183
- // };
184
- // type Test1 = ObjectWithKeys<TPathKeys>;
185
- // type Test2 = ObjectWithKeys<TPathKeys2>;
186
- // type PathKeys = InferBucketPathKeys<typeof router.buckets.imageBucket>;
187
- // type MetadataKeys = InferMetadataObject<typeof router.buckets.imageBucket>;
188
- // type MyEdgeStoreRouter = typeof router;
189
- // type MyAccessControl = AccessControlSchema<Context, AnyDef>;
190
-
191
- 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 };