effortless-aws 0.28.0 → 0.30.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.
package/dist/index.js CHANGED
@@ -1,181 +1,365 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined") return require.apply(this, arguments);
5
- throw Error('Dynamic require of "' + x + '" is not supported');
6
- });
7
-
8
1
  // src/config.ts
9
2
  var defineConfig = (config) => config;
10
3
 
11
- // src/handlers/define-table.ts
12
- var defineTable = (options) => {
13
- const { onRecord, onBatchComplete, onBatch, onError, onAfterInvoke, schema, setup, deps, config, static: staticFiles, ...__spec } = options;
4
+ // src/handlers/handler-options.ts
5
+ var toSeconds = (d) => {
6
+ if (typeof d === "number") return d;
7
+ const match = d.match(/^(\d+(?:\.\d+)?)(s|m|h|d)$/);
8
+ if (!match) throw new Error(`Invalid duration: "${d}"`);
9
+ const n = Number(match[1]);
10
+ const unit = match[2];
11
+ if (unit === "d") return n * 86400;
12
+ if (unit === "h") return n * 3600;
13
+ if (unit === "m") return n * 60;
14
+ return n;
15
+ };
16
+ var defineSecret = (options) => {
14
17
  return {
15
- __brand: "effortless-table",
16
- __spec,
17
- ...schema ? { schema } : {},
18
- ...onError ? { onError } : {},
19
- ...onAfterInvoke ? { onAfterInvoke } : {},
20
- ...setup ? { setup } : {},
21
- ...deps ? { deps } : {},
22
- ...config ? { config } : {},
23
- ...staticFiles ? { static: staticFiles } : {},
24
- ...onRecord ? { onRecord } : {},
25
- ...onBatchComplete ? { onBatchComplete } : {},
26
- ...onBatch ? { onBatch } : {}
18
+ __brand: "effortless-secret",
19
+ ...options?.key ? { key: options.key } : {},
20
+ ...options?.generate ? { generate: options.generate } : {},
21
+ ..."transform" in (options ?? {}) ? { transform: options.transform } : {}
27
22
  };
28
23
  };
24
+ var configHelpers = { defineSecret };
25
+ var resolveConfigFactory = (config) => config(configHelpers);
26
+ var secret = defineSecret;
27
+ var param = (key, transform) => {
28
+ return {
29
+ __brand: "effortless-secret",
30
+ key,
31
+ ...transform ? { transform } : {}
32
+ };
33
+ };
34
+ var generateHex = (bytes) => `hex:${bytes}`;
35
+ var generateBase64 = (bytes) => `base64:${bytes}`;
36
+ var generateUuid = () => "uuid";
37
+ function unsafeAs() {
38
+ return (input) => input;
39
+ }
40
+
41
+ // src/handlers/define-table.ts
42
+ function defineTable(options) {
43
+ const {
44
+ memory,
45
+ timeout,
46
+ permissions,
47
+ logLevel,
48
+ schema,
49
+ static: staticFiles,
50
+ ...tableConfig
51
+ } = options ?? {};
52
+ const hasLambda = memory != null || timeout != null || permissions != null || logLevel != null;
53
+ const spec = {
54
+ ...tableConfig,
55
+ ...hasLambda ? { lambda: { ...memory != null ? { memory } : {}, ...timeout != null ? { timeout } : {}, ...permissions ? { permissions } : {}, ...logLevel ? { logLevel } : {} } } : {}
56
+ };
57
+ const state = {
58
+ spec,
59
+ ...schema ? { schema } : {},
60
+ ...staticFiles ? { static: staticFiles } : {}
61
+ };
62
+ const finalize = () => ({
63
+ __brand: "effortless-table",
64
+ __spec: state.spec,
65
+ ...state.schema ? { schema: state.schema } : {},
66
+ ...state.onError ? { onError: state.onError } : {},
67
+ ...state.onCleanup ? { onCleanup: state.onCleanup } : {},
68
+ ...state.setup ? { setup: state.setup } : {},
69
+ ...state.deps ? { deps: state.deps } : {},
70
+ ...state.config ? { config: state.config } : {},
71
+ ...state.static ? { static: state.static } : {},
72
+ ...state.onRecord ? { onRecord: state.onRecord } : {},
73
+ ...state.onRecordBatch ? { onRecordBatch: state.onRecordBatch } : {}
74
+ });
75
+ const builder = {
76
+ deps(fn) {
77
+ state.deps = fn;
78
+ return builder;
79
+ },
80
+ config(fn) {
81
+ state.config = resolveConfigFactory(fn);
82
+ return builder;
83
+ },
84
+ setup(fn) {
85
+ state.setup = fn;
86
+ return builder;
87
+ },
88
+ onRecord(fn) {
89
+ state.onRecord = fn;
90
+ return finalize();
91
+ },
92
+ onRecordBatch(fn) {
93
+ state.onRecordBatch = fn;
94
+ return finalize();
95
+ },
96
+ onError(fn) {
97
+ state.onError = fn;
98
+ return builder;
99
+ },
100
+ onCleanup(fn) {
101
+ state.onCleanup = fn;
102
+ return builder;
103
+ },
104
+ build() {
105
+ return finalize();
106
+ }
107
+ };
108
+ return builder;
109
+ }
29
110
 
30
111
  // src/handlers/define-app.ts
31
- var defineApp = (options) => ({
112
+ var defineApp = () => (options) => ({
32
113
  __brand: "effortless-app",
33
114
  __spec: options
34
115
  });
35
116
 
36
117
  // src/handlers/define-static-site.ts
37
- var defineStaticSite = (options) => ({
118
+ var defineStaticSite = () => (options) => ({
38
119
  __brand: "effortless-static-site",
39
120
  __spec: options
40
121
  });
41
122
 
42
123
  // src/handlers/define-fifo-queue.ts
43
- var defineFifoQueue = (options) => {
44
- const { onMessage, onBatch, onError, onAfterInvoke, schema, setup, deps, config, static: staticFiles, ...__spec } = options;
45
- return {
46
- __brand: "effortless-fifo-queue",
47
- __spec,
124
+ function defineFifoQueue(options) {
125
+ const {
126
+ memory,
127
+ timeout,
128
+ permissions,
129
+ logLevel,
130
+ schema,
131
+ static: staticFiles,
132
+ ...queueConfig
133
+ } = options ?? {};
134
+ const hasLambda = memory != null || timeout != null || permissions != null || logLevel != null;
135
+ const spec = {
136
+ ...queueConfig,
137
+ ...hasLambda ? { lambda: { ...memory != null ? { memory } : {}, ...timeout != null ? { timeout } : {}, ...permissions ? { permissions } : {}, ...logLevel ? { logLevel } : {} } } : {}
138
+ };
139
+ const state = {
140
+ spec,
48
141
  ...schema ? { schema } : {},
49
- ...onError ? { onError } : {},
50
- ...onAfterInvoke ? { onAfterInvoke } : {},
51
- ...setup ? { setup } : {},
52
- ...deps ? { deps } : {},
53
- ...config ? { config } : {},
54
- ...staticFiles ? { static: staticFiles } : {},
55
- ...onMessage ? { onMessage } : {},
56
- ...onBatch ? { onBatch } : {}
142
+ ...staticFiles ? { static: staticFiles } : {}
57
143
  };
58
- };
144
+ const finalize = () => ({
145
+ __brand: "effortless-fifo-queue",
146
+ __spec: state.spec,
147
+ ...state.schema ? { schema: state.schema } : {},
148
+ ...state.onError ? { onError: state.onError } : {},
149
+ ...state.onCleanup ? { onCleanup: state.onCleanup } : {},
150
+ ...state.setup ? { setup: state.setup } : {},
151
+ ...state.deps ? { deps: state.deps } : {},
152
+ ...state.config ? { config: state.config } : {},
153
+ ...state.static ? { static: state.static } : {},
154
+ ...state.onMessage ? { onMessage: state.onMessage } : {},
155
+ ...state.onMessageBatch ? { onMessageBatch: state.onMessageBatch } : {}
156
+ });
157
+ const builder = {
158
+ deps(fn) {
159
+ state.deps = fn;
160
+ return builder;
161
+ },
162
+ config(fn) {
163
+ state.config = resolveConfigFactory(fn);
164
+ return builder;
165
+ },
166
+ setup(fn) {
167
+ state.setup = fn;
168
+ return builder;
169
+ },
170
+ onMessage(fn) {
171
+ state.onMessage = fn;
172
+ return finalize();
173
+ },
174
+ onMessageBatch(fn) {
175
+ state.onMessageBatch = fn;
176
+ return finalize();
177
+ },
178
+ onError(fn) {
179
+ state.onError = fn;
180
+ return builder;
181
+ },
182
+ onCleanup(fn) {
183
+ state.onCleanup = fn;
184
+ return builder;
185
+ }
186
+ };
187
+ return builder;
188
+ }
59
189
 
60
190
  // src/handlers/define-bucket.ts
61
- var defineBucket = (options) => {
62
- const { onObjectCreated, onObjectRemoved, onError, onAfterInvoke, setup, deps, config, static: staticFiles, ...__spec } = options;
63
- return {
191
+ function defineBucket(options) {
192
+ const {
193
+ memory,
194
+ timeout,
195
+ permissions,
196
+ logLevel,
197
+ static: staticFiles,
198
+ ...bucketConfig
199
+ } = options ?? {};
200
+ const hasLambda = memory != null || timeout != null || permissions != null || logLevel != null;
201
+ const spec = {
202
+ ...bucketConfig,
203
+ ...hasLambda ? { lambda: { ...memory != null ? { memory } : {}, ...timeout != null ? { timeout } : {}, ...permissions ? { permissions } : {}, ...logLevel ? { logLevel } : {} } } : {}
204
+ };
205
+ const state = {
206
+ spec,
207
+ ...staticFiles ? { static: staticFiles } : {}
208
+ };
209
+ const finalize = () => ({
64
210
  __brand: "effortless-bucket",
65
- __spec,
66
- ...onError ? { onError } : {},
67
- ...onAfterInvoke ? { onAfterInvoke } : {},
68
- ...setup ? { setup } : {},
69
- ...deps ? { deps } : {},
70
- ...config ? { config } : {},
71
- ...staticFiles ? { static: staticFiles } : {},
72
- ...onObjectCreated ? { onObjectCreated } : {},
73
- ...onObjectRemoved ? { onObjectRemoved } : {}
211
+ __spec: state.spec,
212
+ ...state.onError ? { onError: state.onError } : {},
213
+ ...state.onCleanup ? { onCleanup: state.onCleanup } : {},
214
+ ...state.setup ? { setup: state.setup } : {},
215
+ ...state.deps ? { deps: state.deps } : {},
216
+ ...state.config ? { config: state.config } : {},
217
+ ...state.static ? { static: state.static } : {},
218
+ ...state.onObjectCreated ? { onObjectCreated: state.onObjectCreated } : {},
219
+ ...state.onObjectRemoved ? { onObjectRemoved: state.onObjectRemoved } : {}
220
+ });
221
+ const builder = {
222
+ deps(fn) {
223
+ state.deps = fn;
224
+ return builder;
225
+ },
226
+ config(fn) {
227
+ state.config = resolveConfigFactory(fn);
228
+ return builder;
229
+ },
230
+ setup(fn) {
231
+ state.setup = fn;
232
+ return builder;
233
+ },
234
+ onObjectCreated(fn) {
235
+ state.onObjectCreated = fn;
236
+ return finalize();
237
+ },
238
+ onObjectRemoved(fn) {
239
+ state.onObjectRemoved = fn;
240
+ return finalize();
241
+ },
242
+ onError(fn) {
243
+ state.onError = fn;
244
+ return builder;
245
+ },
246
+ onCleanup(fn) {
247
+ state.onCleanup = fn;
248
+ return builder;
249
+ },
250
+ build() {
251
+ return finalize();
252
+ }
74
253
  };
75
- };
254
+ return builder;
255
+ }
76
256
 
77
257
  // src/handlers/define-mailer.ts
78
- var defineMailer = (options) => ({
258
+ var defineMailer = () => (options) => ({
79
259
  __brand: "effortless-mailer",
80
260
  __spec: options
81
261
  });
82
262
 
83
263
  // src/handlers/define-api.ts
84
- var defineApi = (options) => {
85
- const { get, post, schema, onError, onAfterInvoke, setup, deps, config, auth: authConfig, apiToken, static: staticFiles, ...__spec } = options;
86
- return {
87
- __brand: "effortless-api",
88
- __spec,
89
- ...get ? { get } : {},
90
- ...post ? { post } : {},
91
- ...schema ? { schema } : {},
92
- ...onError ? { onError } : {},
93
- ...onAfterInvoke ? { onAfterInvoke } : {},
94
- ...setup ? { setup } : {},
95
- ...deps ? { deps } : {},
96
- ...config ? { config } : {},
264
+ function defineApi(options) {
265
+ const { basePath, stream, static: staticFiles, ...lambdaConfig } = options;
266
+ const hasLambda = Object.keys(lambdaConfig).length > 0;
267
+ const state = {
268
+ spec: {
269
+ basePath,
270
+ ...hasLambda ? { lambda: lambdaConfig } : {},
271
+ ...stream ? { stream } : {}
272
+ },
97
273
  ...staticFiles ? { static: staticFiles } : {},
98
- ...authConfig ? { auth: authConfig } : {},
99
- ...apiToken ? { apiToken } : {}
274
+ routes: []
100
275
  };
101
- };
102
-
103
- // src/handlers/handler-options.ts
104
- var toSeconds = (d) => {
105
- if (typeof d === "number") return d;
106
- const match = d.match(/^(\d+(?:\.\d+)?)(s|m|h|d)$/);
107
- if (!match) throw new Error(`Invalid duration: "${d}"`);
108
- const n = Number(match[1]);
109
- const unit = match[2];
110
- if (unit === "d") return n * 86400;
111
- if (unit === "h") return n * 3600;
112
- if (unit === "m") return n * 60;
113
- return n;
114
- };
115
- function secret(options) {
116
- return {
117
- __brand: "effortless-secret",
118
- ...options?.key ? { key: options.key } : {},
119
- ...options?.generate ? { generate: options.generate } : {},
120
- ..."transform" in (options ?? {}) ? { transform: options.transform } : {}
276
+ const addRoute = (method, path, handler, opts) => {
277
+ state.routes.push({
278
+ method,
279
+ path,
280
+ onRequest: handler,
281
+ ...opts?.public ? { public: true } : {}
282
+ });
121
283
  };
122
- }
123
- var generateHex = (bytes) => () => {
124
- const crypto = __require("crypto");
125
- return crypto.randomBytes(bytes).toString("hex");
126
- };
127
- var generateBase64 = (bytes) => () => {
128
- const crypto = __require("crypto");
129
- return crypto.randomBytes(bytes).toString("base64url");
130
- };
131
- var generateUuid = () => () => {
132
- const crypto = __require("crypto");
133
- return crypto.randomUUID();
134
- };
135
- function param(key, transform) {
136
- return {
137
- __brand: "effortless-secret",
138
- key,
139
- ...transform ? { transform } : {}
284
+ const finalize = () => {
285
+ const handler = {
286
+ __brand: "effortless-api",
287
+ __spec: state.spec,
288
+ routes: state.routes,
289
+ ...state.onError ? { onError: state.onError } : {},
290
+ ...state.onCleanup ? { onCleanup: state.onCleanup } : {},
291
+ ...state.setup ? { setup: state.setup } : {},
292
+ ...state.deps ? { deps: state.deps } : {},
293
+ ...state.config ? { config: state.config } : {},
294
+ ...state.static ? { static: state.static } : {}
295
+ };
296
+ for (const m of ["get", "post", "put", "patch", "delete"]) {
297
+ handler[m] = (path, fn, opts) => {
298
+ addRoute(m.toUpperCase(), path, fn, opts);
299
+ handler.routes = state.routes;
300
+ return handler;
301
+ };
302
+ }
303
+ return handler;
140
304
  };
305
+ const builder = {
306
+ deps(fn) {
307
+ state.deps = fn;
308
+ return builder;
309
+ },
310
+ config(fn) {
311
+ state.config = resolveConfigFactory(fn);
312
+ return builder;
313
+ },
314
+ setup(fn) {
315
+ state.setup = fn;
316
+ return builder;
317
+ },
318
+ onError(fn) {
319
+ state.onError = fn;
320
+ return builder;
321
+ },
322
+ onCleanup(fn) {
323
+ state.onCleanup = fn;
324
+ return builder;
325
+ },
326
+ get(path, handler, opts) {
327
+ addRoute("GET", path, handler, opts);
328
+ return finalize();
329
+ },
330
+ post(path, handler, opts) {
331
+ addRoute("POST", path, handler, opts);
332
+ return finalize();
333
+ },
334
+ put(path, handler, opts) {
335
+ addRoute("PUT", path, handler, opts);
336
+ return finalize();
337
+ },
338
+ patch(path, handler, opts) {
339
+ addRoute("PATCH", path, handler, opts);
340
+ return finalize();
341
+ },
342
+ delete(path, handler, opts) {
343
+ addRoute("DELETE", path, handler, opts);
344
+ return finalize();
345
+ }
346
+ };
347
+ return builder;
141
348
  }
142
- function unsafeAs() {
143
- return (input) => input;
144
- }
145
-
146
- // src/handlers/auth.ts
147
- var defineAuth = (options) => ({
148
- __brand: "effortless-auth",
149
- ...options
150
- });
151
-
152
- // src/handlers/shared.ts
153
- var result = {
154
- /** Return a JSON response */
155
- json: (body, status = 200) => ({ status, body }),
156
- /** Return a binary response. Accepts a Buffer and converts to base64 automatically. */
157
- binary: (body, contentType, headers) => ({
158
- status: 200,
159
- body: body.toString("base64"),
160
- binary: true,
161
- headers: { "content-type": contentType, ...headers }
162
- })
163
- };
164
349
  export {
165
350
  defineApi,
166
351
  defineApp,
167
- defineAuth,
168
352
  defineBucket,
169
353
  defineConfig,
170
354
  defineFifoQueue,
171
355
  defineMailer,
356
+ defineSecret,
172
357
  defineStaticSite,
173
358
  defineTable,
174
359
  generateBase64,
175
360
  generateHex,
176
361
  generateUuid,
177
362
  param,
178
- result,
179
363
  secret,
180
364
  toSeconds,
181
365
  unsafeAs