@contractspec/lib.files 1.56.1 → 1.58.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.
Files changed (44) hide show
  1. package/dist/contracts/index.d.ts +1080 -1086
  2. package/dist/contracts/index.d.ts.map +1 -1
  3. package/dist/contracts/index.js +575 -854
  4. package/dist/docs/files.docblock.d.ts +2 -1
  5. package/dist/docs/files.docblock.d.ts.map +1 -0
  6. package/dist/docs/files.docblock.js +17 -22
  7. package/dist/docs/index.d.ts +2 -1
  8. package/dist/docs/index.d.ts.map +1 -0
  9. package/dist/docs/index.js +66 -1
  10. package/dist/entities/index.d.ts +134 -139
  11. package/dist/entities/index.d.ts.map +1 -1
  12. package/dist/entities/index.js +228 -257
  13. package/dist/events.d.ts +357 -363
  14. package/dist/events.d.ts.map +1 -1
  15. package/dist/events.js +217 -400
  16. package/dist/files.capability.d.ts +2 -7
  17. package/dist/files.capability.d.ts.map +1 -1
  18. package/dist/files.capability.js +29 -25
  19. package/dist/files.feature.d.ts +1 -7
  20. package/dist/files.feature.d.ts.map +1 -1
  21. package/dist/files.feature.js +50 -131
  22. package/dist/index.d.ts +7 -6
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +1411 -8
  25. package/dist/node/contracts/index.js +576 -0
  26. package/dist/node/docs/files.docblock.js +65 -0
  27. package/dist/node/docs/index.js +65 -0
  28. package/dist/node/entities/index.js +235 -0
  29. package/dist/node/events.js +219 -0
  30. package/dist/node/files.capability.js +28 -0
  31. package/dist/node/files.feature.js +51 -0
  32. package/dist/node/index.js +1410 -0
  33. package/dist/node/storage/index.js +268 -0
  34. package/dist/storage/index.d.ts +163 -166
  35. package/dist/storage/index.d.ts.map +1 -1
  36. package/dist/storage/index.js +266 -266
  37. package/package.json +104 -30
  38. package/dist/contracts/index.js.map +0 -1
  39. package/dist/docs/files.docblock.js.map +0 -1
  40. package/dist/entities/index.js.map +0 -1
  41. package/dist/events.js.map +0 -1
  42. package/dist/files.capability.js.map +0 -1
  43. package/dist/files.feature.js.map +0 -1
  44. package/dist/storage/index.js.map +0 -1
@@ -0,0 +1,1410 @@
1
+ // src/contracts/index.ts
2
+ import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
3
+ import { defineCommand, defineQuery } from "@contractspec/lib.contracts";
4
+ var OWNERS = ["platform.files"];
5
+ var FileModel = defineSchemaModel({
6
+ name: "File",
7
+ description: "Represents an uploaded file",
8
+ fields: {
9
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
10
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
11
+ mimeType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
12
+ size: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
13
+ storageProvider: {
14
+ type: ScalarTypeEnum.String_unsecure(),
15
+ isOptional: false
16
+ },
17
+ storagePath: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
18
+ checksum: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
19
+ status: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
20
+ isPublic: { type: ScalarTypeEnum.Boolean(), isOptional: false },
21
+ ownerId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
22
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
23
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
24
+ width: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
25
+ height: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
26
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
27
+ updatedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
28
+ }
29
+ });
30
+ var FileVersionModel = defineSchemaModel({
31
+ name: "FileVersion",
32
+ description: "Represents a file version",
33
+ fields: {
34
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
35
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
36
+ version: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
37
+ size: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
38
+ storagePath: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
39
+ checksum: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
40
+ comment: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
41
+ createdBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
42
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
43
+ }
44
+ });
45
+ var AttachmentModel = defineSchemaModel({
46
+ name: "Attachment",
47
+ description: "Represents an attachment",
48
+ fields: {
49
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
50
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
51
+ entityType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
52
+ entityId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
53
+ attachmentType: {
54
+ type: ScalarTypeEnum.String_unsecure(),
55
+ isOptional: true
56
+ },
57
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
58
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
59
+ order: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
60
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
61
+ createdBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
62
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
63
+ file: { type: FileModel, isOptional: true }
64
+ }
65
+ });
66
+ var PresignedUrlModel = defineSchemaModel({
67
+ name: "PresignedUrl",
68
+ description: "A presigned URL for file operations",
69
+ fields: {
70
+ url: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
71
+ fields: { type: ScalarTypeEnum.JSON(), isOptional: true },
72
+ expiresAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
73
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
74
+ sessionId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
75
+ }
76
+ });
77
+ var UploadFileInput = defineSchemaModel({
78
+ name: "UploadFileInput",
79
+ description: "Input for uploading a file",
80
+ fields: {
81
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
82
+ mimeType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
83
+ size: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
84
+ content: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
85
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
86
+ isPublic: { type: ScalarTypeEnum.Boolean(), isOptional: true },
87
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
88
+ tags: { type: ScalarTypeEnum.JSON(), isOptional: true }
89
+ }
90
+ });
91
+ var UpdateFileInput = defineSchemaModel({
92
+ name: "UpdateFileInput",
93
+ description: "Input for updating a file",
94
+ fields: {
95
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
96
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
97
+ isPublic: { type: ScalarTypeEnum.Boolean(), isOptional: true },
98
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
99
+ tags: { type: ScalarTypeEnum.JSON(), isOptional: true }
100
+ }
101
+ });
102
+ var DeleteFileInput = defineSchemaModel({
103
+ name: "DeleteFileInput",
104
+ description: "Input for deleting a file",
105
+ fields: {
106
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
107
+ }
108
+ });
109
+ var GetFileInput = defineSchemaModel({
110
+ name: "GetFileInput",
111
+ description: "Input for getting a file",
112
+ fields: {
113
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
114
+ }
115
+ });
116
+ var ListFilesInput = defineSchemaModel({
117
+ name: "ListFilesInput",
118
+ description: "Input for listing files",
119
+ fields: {
120
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
121
+ ownerId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
122
+ mimeType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
123
+ status: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
124
+ tags: { type: ScalarTypeEnum.JSON(), isOptional: true },
125
+ limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
126
+ offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
127
+ }
128
+ });
129
+ var ListFilesOutput = defineSchemaModel({
130
+ name: "ListFilesOutput",
131
+ description: "Output for listing files",
132
+ fields: {
133
+ files: { type: FileModel, isArray: true, isOptional: false },
134
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
135
+ }
136
+ });
137
+ var GetDownloadUrlInput = defineSchemaModel({
138
+ name: "GetDownloadUrlInput",
139
+ description: "Input for getting a download URL",
140
+ fields: {
141
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
142
+ expiresInSeconds: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
143
+ }
144
+ });
145
+ var CreateVersionInput = defineSchemaModel({
146
+ name: "CreateVersionInput",
147
+ description: "Input for creating a file version",
148
+ fields: {
149
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
150
+ content: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
151
+ comment: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
152
+ }
153
+ });
154
+ var GetVersionsInput = defineSchemaModel({
155
+ name: "GetVersionsInput",
156
+ description: "Input for getting file versions",
157
+ fields: {
158
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
159
+ limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
160
+ offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
161
+ }
162
+ });
163
+ var GetVersionsOutput = defineSchemaModel({
164
+ name: "GetVersionsOutput",
165
+ description: "Output for getting file versions",
166
+ fields: {
167
+ versions: { type: FileVersionModel, isArray: true, isOptional: false },
168
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
169
+ }
170
+ });
171
+ var AttachFileInput = defineSchemaModel({
172
+ name: "AttachFileInput",
173
+ description: "Input for attaching a file to an entity",
174
+ fields: {
175
+ fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
176
+ entityType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
177
+ entityId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
178
+ attachmentType: {
179
+ type: ScalarTypeEnum.String_unsecure(),
180
+ isOptional: true
181
+ },
182
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
183
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
184
+ order: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
185
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true }
186
+ }
187
+ });
188
+ var DetachFileInput = defineSchemaModel({
189
+ name: "DetachFileInput",
190
+ description: "Input for detaching a file",
191
+ fields: {
192
+ attachmentId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
193
+ }
194
+ });
195
+ var ListAttachmentsInput = defineSchemaModel({
196
+ name: "ListAttachmentsInput",
197
+ description: "Input for listing attachments",
198
+ fields: {
199
+ entityType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
200
+ entityId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
201
+ attachmentType: {
202
+ type: ScalarTypeEnum.String_unsecure(),
203
+ isOptional: true
204
+ }
205
+ }
206
+ });
207
+ var ListAttachmentsOutput = defineSchemaModel({
208
+ name: "ListAttachmentsOutput",
209
+ description: "Output for listing attachments",
210
+ fields: {
211
+ attachments: { type: AttachmentModel, isArray: true, isOptional: false },
212
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
213
+ }
214
+ });
215
+ var CreatePresignedUrlInput = defineSchemaModel({
216
+ name: "CreatePresignedUrlInput",
217
+ description: "Input for creating a presigned upload URL",
218
+ fields: {
219
+ fileName: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
220
+ mimeType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
221
+ size: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
222
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
223
+ expiresInSeconds: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
224
+ }
225
+ });
226
+ var SuccessOutput = defineSchemaModel({
227
+ name: "SuccessOutput",
228
+ description: "Generic success output",
229
+ fields: {
230
+ success: { type: ScalarTypeEnum.Boolean(), isOptional: false }
231
+ }
232
+ });
233
+ var UploadFileContract = defineCommand({
234
+ meta: {
235
+ key: "file.upload",
236
+ version: "1.0.0",
237
+ stability: "stable",
238
+ owners: [...OWNERS],
239
+ tags: ["files", "upload"],
240
+ description: "Upload a new file.",
241
+ goal: "Store a file and create a file record.",
242
+ context: "Called when uploading files directly."
243
+ },
244
+ io: {
245
+ input: UploadFileInput,
246
+ output: FileModel,
247
+ errors: {
248
+ FILE_TOO_LARGE: {
249
+ description: "File exceeds size limit",
250
+ http: 413,
251
+ gqlCode: "FILE_TOO_LARGE",
252
+ when: "File size exceeds configured limit"
253
+ },
254
+ INVALID_MIME_TYPE: {
255
+ description: "MIME type not allowed",
256
+ http: 415,
257
+ gqlCode: "INVALID_MIME_TYPE",
258
+ when: "File type is not in allowed list"
259
+ }
260
+ }
261
+ },
262
+ policy: {
263
+ auth: "user"
264
+ }
265
+ });
266
+ var UpdateFileContract = defineCommand({
267
+ meta: {
268
+ key: "file.update",
269
+ version: "1.0.0",
270
+ stability: "stable",
271
+ owners: [...OWNERS],
272
+ tags: ["files", "update"],
273
+ description: "Update file metadata.",
274
+ goal: "Modify file properties without replacing content.",
275
+ context: "Called when renaming or updating file metadata."
276
+ },
277
+ io: {
278
+ input: UpdateFileInput,
279
+ output: FileModel,
280
+ errors: {
281
+ FILE_NOT_FOUND: {
282
+ description: "File does not exist",
283
+ http: 404,
284
+ gqlCode: "FILE_NOT_FOUND",
285
+ when: "File ID is invalid"
286
+ }
287
+ }
288
+ },
289
+ policy: {
290
+ auth: "user"
291
+ }
292
+ });
293
+ var DeleteFileContract = defineCommand({
294
+ meta: {
295
+ key: "file.delete",
296
+ version: "1.0.0",
297
+ stability: "stable",
298
+ owners: [...OWNERS],
299
+ tags: ["files", "delete"],
300
+ description: "Delete a file.",
301
+ goal: "Remove a file and all its versions and attachments.",
302
+ context: "Called when removing a file permanently."
303
+ },
304
+ io: {
305
+ input: DeleteFileInput,
306
+ output: SuccessOutput,
307
+ errors: {
308
+ FILE_NOT_FOUND: {
309
+ description: "File does not exist",
310
+ http: 404,
311
+ gqlCode: "FILE_NOT_FOUND",
312
+ when: "File ID is invalid"
313
+ }
314
+ }
315
+ },
316
+ policy: {
317
+ auth: "user"
318
+ }
319
+ });
320
+ var GetFileContract = defineQuery({
321
+ meta: {
322
+ key: "file.get",
323
+ version: "1.0.0",
324
+ stability: "stable",
325
+ owners: [...OWNERS],
326
+ tags: ["files", "get"],
327
+ description: "Get a file by ID.",
328
+ goal: "Retrieve file metadata.",
329
+ context: "Called to inspect file details."
330
+ },
331
+ io: {
332
+ input: GetFileInput,
333
+ output: FileModel,
334
+ errors: {
335
+ FILE_NOT_FOUND: {
336
+ description: "File does not exist",
337
+ http: 404,
338
+ gqlCode: "FILE_NOT_FOUND",
339
+ when: "File ID is invalid"
340
+ }
341
+ }
342
+ },
343
+ policy: {
344
+ auth: "user"
345
+ }
346
+ });
347
+ var ListFilesContract = defineQuery({
348
+ meta: {
349
+ key: "file.list",
350
+ version: "1.0.0",
351
+ stability: "stable",
352
+ owners: [...OWNERS],
353
+ tags: ["files", "list"],
354
+ description: "List files with filtering.",
355
+ goal: "Browse uploaded files.",
356
+ context: "Called to browse file library."
357
+ },
358
+ io: {
359
+ input: ListFilesInput,
360
+ output: ListFilesOutput
361
+ },
362
+ policy: {
363
+ auth: "user"
364
+ }
365
+ });
366
+ var GetDownloadUrlContract = defineQuery({
367
+ meta: {
368
+ key: "file.downloadUrl",
369
+ version: "1.0.0",
370
+ stability: "stable",
371
+ owners: [...OWNERS],
372
+ tags: ["files", "download"],
373
+ description: "Get a presigned download URL.",
374
+ goal: "Generate a temporary URL for downloading.",
375
+ context: "Called when user wants to download a file."
376
+ },
377
+ io: {
378
+ input: GetDownloadUrlInput,
379
+ output: PresignedUrlModel,
380
+ errors: {
381
+ FILE_NOT_FOUND: {
382
+ description: "File does not exist",
383
+ http: 404,
384
+ gqlCode: "FILE_NOT_FOUND",
385
+ when: "File ID is invalid"
386
+ }
387
+ }
388
+ },
389
+ policy: {
390
+ auth: "user"
391
+ }
392
+ });
393
+ var CreateVersionContract = defineCommand({
394
+ meta: {
395
+ key: "file.version.create",
396
+ version: "1.0.0",
397
+ stability: "stable",
398
+ owners: [...OWNERS],
399
+ tags: ["files", "version", "create"],
400
+ description: "Create a new version of a file.",
401
+ goal: "Upload a new version while preserving history.",
402
+ context: "Called when updating a document."
403
+ },
404
+ io: {
405
+ input: CreateVersionInput,
406
+ output: FileVersionModel,
407
+ errors: {
408
+ FILE_NOT_FOUND: {
409
+ description: "File does not exist",
410
+ http: 404,
411
+ gqlCode: "FILE_NOT_FOUND",
412
+ when: "File ID is invalid"
413
+ }
414
+ }
415
+ },
416
+ policy: {
417
+ auth: "user"
418
+ }
419
+ });
420
+ var GetVersionsContract = defineQuery({
421
+ meta: {
422
+ key: "file.version.list",
423
+ version: "1.0.0",
424
+ stability: "stable",
425
+ owners: [...OWNERS],
426
+ tags: ["files", "version", "list"],
427
+ description: "Get file version history.",
428
+ goal: "View all versions of a file.",
429
+ context: "Called to browse file history."
430
+ },
431
+ io: {
432
+ input: GetVersionsInput,
433
+ output: GetVersionsOutput,
434
+ errors: {
435
+ FILE_NOT_FOUND: {
436
+ description: "File does not exist",
437
+ http: 404,
438
+ gqlCode: "FILE_NOT_FOUND",
439
+ when: "File ID is invalid"
440
+ }
441
+ }
442
+ },
443
+ policy: {
444
+ auth: "user"
445
+ }
446
+ });
447
+ var AttachFileContract = defineCommand({
448
+ meta: {
449
+ key: "attachment.attach",
450
+ version: "1.0.0",
451
+ stability: "stable",
452
+ owners: [...OWNERS],
453
+ tags: ["files", "attachment", "attach"],
454
+ description: "Attach a file to an entity.",
455
+ goal: "Link a file to a business entity.",
456
+ context: "Called when associating files with entities."
457
+ },
458
+ io: {
459
+ input: AttachFileInput,
460
+ output: AttachmentModel,
461
+ errors: {
462
+ FILE_NOT_FOUND: {
463
+ description: "File does not exist",
464
+ http: 404,
465
+ gqlCode: "FILE_NOT_FOUND",
466
+ when: "File ID is invalid"
467
+ },
468
+ ATTACHMENT_EXISTS: {
469
+ description: "Attachment already exists",
470
+ http: 409,
471
+ gqlCode: "ATTACHMENT_EXISTS",
472
+ when: "File is already attached to this entity"
473
+ }
474
+ }
475
+ },
476
+ policy: {
477
+ auth: "user"
478
+ }
479
+ });
480
+ var DetachFileContract = defineCommand({
481
+ meta: {
482
+ key: "attachment.detach",
483
+ version: "1.0.0",
484
+ stability: "stable",
485
+ owners: [...OWNERS],
486
+ tags: ["files", "attachment", "detach"],
487
+ description: "Detach a file from an entity.",
488
+ goal: "Remove a file association.",
489
+ context: "Called when removing file from entity."
490
+ },
491
+ io: {
492
+ input: DetachFileInput,
493
+ output: SuccessOutput,
494
+ errors: {
495
+ ATTACHMENT_NOT_FOUND: {
496
+ description: "Attachment does not exist",
497
+ http: 404,
498
+ gqlCode: "ATTACHMENT_NOT_FOUND",
499
+ when: "Attachment ID is invalid"
500
+ }
501
+ }
502
+ },
503
+ policy: {
504
+ auth: "user"
505
+ }
506
+ });
507
+ var ListAttachmentsContract = defineQuery({
508
+ meta: {
509
+ key: "attachment.list",
510
+ version: "1.0.0",
511
+ stability: "stable",
512
+ owners: [...OWNERS],
513
+ tags: ["files", "attachment", "list"],
514
+ description: "List attachments for an entity.",
515
+ goal: "Get all files attached to an entity.",
516
+ context: "Called to display attached files."
517
+ },
518
+ io: {
519
+ input: ListAttachmentsInput,
520
+ output: ListAttachmentsOutput
521
+ },
522
+ policy: {
523
+ auth: "user"
524
+ }
525
+ });
526
+ var CreatePresignedUrlContract = defineCommand({
527
+ meta: {
528
+ key: "file.presignedUrl.create",
529
+ version: "1.0.0",
530
+ stability: "stable",
531
+ owners: [...OWNERS],
532
+ tags: ["files", "presigned", "upload"],
533
+ description: "Create a presigned URL for direct upload.",
534
+ goal: "Enable direct-to-storage uploads.",
535
+ context: "Called for large file uploads."
536
+ },
537
+ io: {
538
+ input: CreatePresignedUrlInput,
539
+ output: PresignedUrlModel,
540
+ errors: {
541
+ FILE_TOO_LARGE: {
542
+ description: "File exceeds size limit",
543
+ http: 413,
544
+ gqlCode: "FILE_TOO_LARGE",
545
+ when: "Requested file size exceeds limit"
546
+ },
547
+ INVALID_MIME_TYPE: {
548
+ description: "MIME type not allowed",
549
+ http: 415,
550
+ gqlCode: "INVALID_MIME_TYPE",
551
+ when: "File type is not in allowed list"
552
+ }
553
+ }
554
+ },
555
+ policy: {
556
+ auth: "user"
557
+ }
558
+ });
559
+
560
+ // src/docs/files.docblock.ts
561
+ import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
562
+ var filesDocBlocks = [
563
+ {
564
+ id: "docs.files.attachments",
565
+ title: "Files, Versions & Attachments",
566
+ summary: "Spec-first file management with storage adapters, versioning, presigned URLs, and polymorphic attachments for any entity.",
567
+ kind: "reference",
568
+ visibility: "public",
569
+ route: "/docs/files/attachments",
570
+ tags: ["files", "attachments", "storage", "versions"],
571
+ body: `## Capabilities
572
+
573
+ - **Entities**: File, FileVersion, Attachment, UploadSession with retention, checksum, ACLs.
574
+ - **Contracts**: upload/update/delete/list/get files; presigned upload/download; version create/list; attach/detach/list attachments.
575
+ - **Storage**: pluggable adapters (Local, S3 placeholder + interface), in-memory for tests.
576
+ - **Events**: file.uploaded/deleted, attachment.added/removed (see events export).
577
+
578
+ ## Usage
579
+
580
+ 1) Compose schema
581
+ - Include \`filesSchemaContribution\` in your schema composition.
582
+
583
+ 2) Register contracts/events
584
+ - Import contracts and events from \`@contractspec/lib.files\` in your spec registry.
585
+
586
+ 3) Wire storage
587
+ - Provide a \`StorageAdapter\` implementation (local/in-memory or S3 via custom impl).
588
+ - Use \`createStorageAdapter\` with config to instantiate.
589
+
590
+ 4) Attach to domain entities
591
+ - Use \`attachment.attach\` with \`entityType/entityId\` to link files to deals, orders, runs, etc.
592
+
593
+ ## Example
594
+
595
+ ${"```"}ts
596
+ import {
597
+ UploadFileContract,
598
+ AttachFileContract,
599
+ InMemoryStorageAdapter,
600
+ } from '@contractspec/lib.files';
601
+
602
+ // storage
603
+ const storage = new InMemoryStorageAdapter();
604
+
605
+ // upload
606
+ const file = await storage.upload({
607
+ path: 'org-1/reports/r1.pdf',
608
+ content: Buffer.from(pdfBytes),
609
+ mimeType: 'application/pdf',
610
+ });
611
+
612
+ // attach
613
+ await AttachFileContract; // register in spec to enable attach flows
614
+ ${"```"},
615
+
616
+ ## Guardrails
617
+
618
+ - Enforce size/MIME limits in your handlers; avoid storing PII in paths.
619
+ - Keep \`orgId\` scoped for multi-tenant isolation; prefer presigned URLs for public delivery.
620
+ - Persist checksums for integrity; emit audit + events for access/retention changes.
621
+ `
622
+ }
623
+ ];
624
+ registerDocBlocks(filesDocBlocks);
625
+ // src/entities/index.ts
626
+ import {
627
+ defineEntity,
628
+ defineEntityEnum,
629
+ field,
630
+ index
631
+ } from "@contractspec/lib.schema";
632
+ var StorageProviderEnum = defineEntityEnum({
633
+ name: "StorageProvider",
634
+ values: ["LOCAL", "S3", "GCS", "AZURE", "CLOUDFLARE"],
635
+ schema: "lssm_files",
636
+ description: "Storage backend provider."
637
+ });
638
+ var FileStatusEnum = defineEntityEnum({
639
+ name: "FileStatus",
640
+ values: [
641
+ "PENDING",
642
+ "UPLOADED",
643
+ "PROCESSING",
644
+ "READY",
645
+ "ERROR",
646
+ "DELETED"
647
+ ],
648
+ schema: "lssm_files",
649
+ description: "File processing status."
650
+ });
651
+ var FileEntity = defineEntity({
652
+ name: "File",
653
+ description: "An uploaded file.",
654
+ schema: "lssm_files",
655
+ map: "file",
656
+ fields: {
657
+ id: field.id({ description: "Unique file identifier" }),
658
+ name: field.string({ description: "Original file name" }),
659
+ mimeType: field.string({ description: "MIME type" }),
660
+ size: field.int({ description: "File size in bytes" }),
661
+ storageProvider: field.enum("StorageProvider", {
662
+ default: "LOCAL",
663
+ description: "Storage backend"
664
+ }),
665
+ storagePath: field.string({ description: "Path in storage backend" }),
666
+ storageKey: field.string({
667
+ isOptional: true,
668
+ description: "Storage key/bucket"
669
+ }),
670
+ checksum: field.string({
671
+ isOptional: true,
672
+ description: "SHA-256 checksum"
673
+ }),
674
+ etag: field.string({ isOptional: true, description: "Storage ETag" }),
675
+ status: field.enum("FileStatus", {
676
+ default: "PENDING",
677
+ description: "File status"
678
+ }),
679
+ isPublic: field.boolean({
680
+ default: false,
681
+ description: "Whether file is publicly accessible"
682
+ }),
683
+ expiresAt: field.dateTime({
684
+ isOptional: true,
685
+ description: "Auto-delete timestamp"
686
+ }),
687
+ ownerId: field.string({ description: "User who uploaded" }),
688
+ orgId: field.string({
689
+ isOptional: true,
690
+ description: "Organization scope"
691
+ }),
692
+ metadata: field.json({
693
+ isOptional: true,
694
+ description: "Additional metadata"
695
+ }),
696
+ tags: field.json({
697
+ isOptional: true,
698
+ description: "Tags for categorization"
699
+ }),
700
+ width: field.int({
701
+ isOptional: true,
702
+ description: "Image width in pixels"
703
+ }),
704
+ height: field.int({
705
+ isOptional: true,
706
+ description: "Image height in pixels"
707
+ }),
708
+ createdAt: field.createdAt(),
709
+ updatedAt: field.updatedAt(),
710
+ versions: field.hasMany("FileVersion"),
711
+ attachments: field.hasMany("Attachment")
712
+ },
713
+ indexes: [
714
+ index.on(["ownerId"]),
715
+ index.on(["orgId"]),
716
+ index.on(["status"]),
717
+ index.on(["mimeType"]),
718
+ index.on(["storageProvider", "storagePath"])
719
+ ],
720
+ enums: [StorageProviderEnum, FileStatusEnum]
721
+ });
722
+ var FileVersionEntity = defineEntity({
723
+ name: "FileVersion",
724
+ description: "A version of a file.",
725
+ schema: "lssm_files",
726
+ map: "file_version",
727
+ fields: {
728
+ id: field.id({ description: "Unique version identifier" }),
729
+ fileId: field.foreignKey({ description: "Parent file" }),
730
+ version: field.int({ description: "Version number" }),
731
+ size: field.int({ description: "Version size in bytes" }),
732
+ storagePath: field.string({ description: "Path in storage backend" }),
733
+ checksum: field.string({
734
+ isOptional: true,
735
+ description: "SHA-256 checksum"
736
+ }),
737
+ comment: field.string({ isOptional: true, description: "Version comment" }),
738
+ changes: field.json({
739
+ isOptional: true,
740
+ description: "Change description"
741
+ }),
742
+ createdBy: field.string({ description: "User who created version" }),
743
+ createdAt: field.createdAt(),
744
+ file: field.belongsTo("File", ["fileId"], ["id"], { onDelete: "Cascade" })
745
+ },
746
+ indexes: [
747
+ index.on(["fileId", "version"]),
748
+ index.unique(["fileId", "version"], { name: "file_version_unique" })
749
+ ]
750
+ });
751
+ var AttachmentEntity = defineEntity({
752
+ name: "Attachment",
753
+ description: "Links a file to an entity.",
754
+ schema: "lssm_files",
755
+ map: "attachment",
756
+ fields: {
757
+ id: field.id({ description: "Unique attachment identifier" }),
758
+ fileId: field.foreignKey({ description: "Attached file" }),
759
+ entityType: field.string({
760
+ description: "Target entity type (deal, listing, etc.)"
761
+ }),
762
+ entityId: field.string({ description: "Target entity ID" }),
763
+ attachmentType: field.string({
764
+ isOptional: true,
765
+ description: "Type of attachment (document, image, avatar, etc.)"
766
+ }),
767
+ name: field.string({
768
+ isOptional: true,
769
+ description: "Display name (overrides file name)"
770
+ }),
771
+ description: field.string({
772
+ isOptional: true,
773
+ description: "Attachment description"
774
+ }),
775
+ order: field.int({ default: 0, description: "Display order" }),
776
+ metadata: field.json({
777
+ isOptional: true,
778
+ description: "Attachment-specific metadata"
779
+ }),
780
+ createdBy: field.string({ description: "User who created attachment" }),
781
+ createdAt: field.createdAt(),
782
+ updatedAt: field.updatedAt(),
783
+ file: field.belongsTo("File", ["fileId"], ["id"], { onDelete: "Cascade" })
784
+ },
785
+ indexes: [
786
+ index.on(["entityType", "entityId"]),
787
+ index.on(["fileId"]),
788
+ index.on(["entityType", "entityId", "attachmentType"]),
789
+ index.unique(["fileId", "entityType", "entityId"], {
790
+ name: "attachment_unique"
791
+ })
792
+ ]
793
+ });
794
+ var UploadSessionEntity = defineEntity({
795
+ name: "UploadSession",
796
+ description: "Tracks a multipart upload session.",
797
+ schema: "lssm_files",
798
+ map: "upload_session",
799
+ fields: {
800
+ id: field.id({ description: "Unique session identifier" }),
801
+ fileName: field.string({ description: "Target file name" }),
802
+ mimeType: field.string({ description: "Expected MIME type" }),
803
+ totalSize: field.int({ description: "Total file size" }),
804
+ uploadId: field.string({
805
+ isOptional: true,
806
+ description: "Storage upload ID"
807
+ }),
808
+ uploadedBytes: field.int({
809
+ default: 0,
810
+ description: "Bytes uploaded so far"
811
+ }),
812
+ uploadedParts: field.json({
813
+ isOptional: true,
814
+ description: "Completed part info"
815
+ }),
816
+ status: field.string({
817
+ default: '"pending"',
818
+ description: "Session status"
819
+ }),
820
+ error: field.string({
821
+ isOptional: true,
822
+ description: "Error message if failed"
823
+ }),
824
+ fileId: field.string({
825
+ isOptional: true,
826
+ description: "Resulting file ID"
827
+ }),
828
+ ownerId: field.string({ description: "User who initiated upload" }),
829
+ orgId: field.string({
830
+ isOptional: true,
831
+ description: "Organization scope"
832
+ }),
833
+ expiresAt: field.dateTime({ description: "Session expiry time" }),
834
+ createdAt: field.createdAt(),
835
+ updatedAt: field.updatedAt()
836
+ },
837
+ indexes: [index.on(["status", "expiresAt"]), index.on(["ownerId"])]
838
+ });
839
+ var fileEntities = [
840
+ FileEntity,
841
+ FileVersionEntity,
842
+ AttachmentEntity,
843
+ UploadSessionEntity
844
+ ];
845
+ var filesSchemaContribution = {
846
+ moduleId: "@contractspec/lib.files",
847
+ entities: fileEntities,
848
+ enums: [StorageProviderEnum, FileStatusEnum]
849
+ };
850
+
851
+ // src/events.ts
852
+ import { ScalarTypeEnum as ScalarTypeEnum2, defineSchemaModel as defineSchemaModel2 } from "@contractspec/lib.schema";
853
+ import { defineEvent } from "@contractspec/lib.contracts";
854
+ var FileUploadedPayload = defineSchemaModel2({
855
+ name: "FileUploadedEventPayload",
856
+ description: "Payload when a file is uploaded",
857
+ fields: {
858
+ fileId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
859
+ name: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
860
+ mimeType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
861
+ size: { type: ScalarTypeEnum2.Int_unsecure(), isOptional: false },
862
+ storageProvider: {
863
+ type: ScalarTypeEnum2.String_unsecure(),
864
+ isOptional: false
865
+ },
866
+ ownerId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
867
+ orgId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
868
+ uploadedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
869
+ }
870
+ });
871
+ var FileUpdatedPayload = defineSchemaModel2({
872
+ name: "FileUpdatedEventPayload",
873
+ description: "Payload when a file is updated",
874
+ fields: {
875
+ fileId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
876
+ name: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
877
+ changes: { type: ScalarTypeEnum2.JSON(), isOptional: false },
878
+ updatedBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
879
+ updatedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
880
+ }
881
+ });
882
+ var FileDeletedPayload = defineSchemaModel2({
883
+ name: "FileDeletedEventPayload",
884
+ description: "Payload when a file is deleted",
885
+ fields: {
886
+ fileId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
887
+ name: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
888
+ storageProvider: {
889
+ type: ScalarTypeEnum2.String_unsecure(),
890
+ isOptional: false
891
+ },
892
+ storagePath: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
893
+ deletedBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
894
+ deletedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
895
+ }
896
+ });
897
+ var FileVersionCreatedPayload = defineSchemaModel2({
898
+ name: "FileVersionCreatedEventPayload",
899
+ description: "Payload when a file version is created",
900
+ fields: {
901
+ fileId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
902
+ versionId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
903
+ version: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
904
+ size: { type: ScalarTypeEnum2.Int_unsecure(), isOptional: false },
905
+ createdBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
906
+ comment: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
907
+ createdAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
908
+ }
909
+ });
910
+ var AttachmentAttachedPayload = defineSchemaModel2({
911
+ name: "AttachmentAttachedEventPayload",
912
+ description: "Payload when a file is attached to an entity",
913
+ fields: {
914
+ attachmentId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
915
+ fileId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
916
+ entityType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
917
+ entityId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
918
+ attachmentType: {
919
+ type: ScalarTypeEnum2.String_unsecure(),
920
+ isOptional: true
921
+ },
922
+ attachedBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
923
+ attachedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
924
+ }
925
+ });
926
+ var AttachmentDetachedPayload = defineSchemaModel2({
927
+ name: "AttachmentDetachedEventPayload",
928
+ description: "Payload when a file is detached from an entity",
929
+ fields: {
930
+ attachmentId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
931
+ fileId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
932
+ entityType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
933
+ entityId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
934
+ detachedBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
935
+ detachedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
936
+ }
937
+ });
938
+ var UploadSessionStartedPayload = defineSchemaModel2({
939
+ name: "UploadSessionStartedEventPayload",
940
+ description: "Payload when an upload session starts",
941
+ fields: {
942
+ sessionId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
943
+ fileName: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
944
+ mimeType: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
945
+ totalSize: { type: ScalarTypeEnum2.Int_unsecure(), isOptional: false },
946
+ ownerId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
947
+ startedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
948
+ }
949
+ });
950
+ var UploadSessionCompletedPayload = defineSchemaModel2({
951
+ name: "UploadSessionCompletedEventPayload",
952
+ description: "Payload when an upload session completes",
953
+ fields: {
954
+ sessionId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
955
+ fileId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
956
+ fileName: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
957
+ size: { type: ScalarTypeEnum2.Int_unsecure(), isOptional: false },
958
+ completedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
959
+ }
960
+ });
961
+ var FileUploadedEvent = defineEvent({
962
+ meta: {
963
+ key: "file.uploaded",
964
+ version: "1.0.0",
965
+ description: "A file has been uploaded.",
966
+ stability: "stable",
967
+ owners: ["@platform.files"],
968
+ tags: ["files", "upload"]
969
+ },
970
+ payload: FileUploadedPayload
971
+ });
972
+ var FileUpdatedEvent = defineEvent({
973
+ meta: {
974
+ key: "file.updated",
975
+ version: "1.0.0",
976
+ description: "A file has been updated.",
977
+ stability: "stable",
978
+ owners: ["@platform.files"],
979
+ tags: ["files", "update"]
980
+ },
981
+ payload: FileUpdatedPayload
982
+ });
983
+ var FileDeletedEvent = defineEvent({
984
+ meta: {
985
+ key: "file.deleted",
986
+ version: "1.0.0",
987
+ description: "A file has been deleted.",
988
+ stability: "stable",
989
+ owners: ["@platform.files"],
990
+ tags: ["files", "delete"]
991
+ },
992
+ payload: FileDeletedPayload
993
+ });
994
+ var FileVersionCreatedEvent = defineEvent({
995
+ meta: {
996
+ key: "file.version_created",
997
+ version: "1.0.0",
998
+ description: "A new file version has been created.",
999
+ stability: "stable",
1000
+ owners: ["@platform.files"],
1001
+ tags: ["files", "version", "create"]
1002
+ },
1003
+ payload: FileVersionCreatedPayload
1004
+ });
1005
+ var AttachmentAttachedEvent = defineEvent({
1006
+ meta: {
1007
+ key: "attachment.attached",
1008
+ version: "1.0.0",
1009
+ description: "A file has been attached to an entity.",
1010
+ stability: "stable",
1011
+ owners: ["@platform.files"],
1012
+ tags: ["files", "attachment", "attach"]
1013
+ },
1014
+ payload: AttachmentAttachedPayload
1015
+ });
1016
+ var AttachmentDetachedEvent = defineEvent({
1017
+ meta: {
1018
+ key: "attachment.detached",
1019
+ version: "1.0.0",
1020
+ description: "A file has been detached from an entity.",
1021
+ stability: "stable",
1022
+ owners: ["@platform.files"],
1023
+ tags: ["files", "attachment", "detach"]
1024
+ },
1025
+ payload: AttachmentDetachedPayload
1026
+ });
1027
+ var UploadSessionStartedEvent = defineEvent({
1028
+ meta: {
1029
+ key: "upload.session_started",
1030
+ version: "1.0.0",
1031
+ description: "An upload session has started.",
1032
+ stability: "stable",
1033
+ owners: ["@platform.files"],
1034
+ tags: ["files", "upload", "session", "start"]
1035
+ },
1036
+ payload: UploadSessionStartedPayload
1037
+ });
1038
+ var UploadSessionCompletedEvent = defineEvent({
1039
+ meta: {
1040
+ key: "upload.session_completed",
1041
+ version: "1.0.0",
1042
+ description: "An upload session has completed.",
1043
+ stability: "stable",
1044
+ owners: ["@platform.files"],
1045
+ tags: ["files", "upload", "session", "complete"]
1046
+ },
1047
+ payload: UploadSessionCompletedPayload
1048
+ });
1049
+ var FileEvents = {
1050
+ FileUploadedEvent,
1051
+ FileUpdatedEvent,
1052
+ FileDeletedEvent,
1053
+ FileVersionCreatedEvent,
1054
+ AttachmentAttachedEvent,
1055
+ AttachmentDetachedEvent,
1056
+ UploadSessionStartedEvent,
1057
+ UploadSessionCompletedEvent
1058
+ };
1059
+
1060
+ // src/files.feature.ts
1061
+ import { defineFeature } from "@contractspec/lib.contracts";
1062
+ var FilesFeature = defineFeature({
1063
+ meta: {
1064
+ key: "files",
1065
+ version: "1.0.0",
1066
+ title: "File Management",
1067
+ description: "File storage, attachments, and media processing with presigned URLs",
1068
+ domain: "platform",
1069
+ owners: ["@platform.files"],
1070
+ tags: ["files", "upload", "attachments", "storage"],
1071
+ stability: "stable"
1072
+ },
1073
+ operations: [
1074
+ { key: "file.upload", version: "1.0.0" },
1075
+ { key: "file.update", version: "1.0.0" },
1076
+ { key: "file.delete", version: "1.0.0" },
1077
+ { key: "file.get", version: "1.0.0" },
1078
+ { key: "file.list", version: "1.0.0" },
1079
+ { key: "file.downloadUrl", version: "1.0.0" },
1080
+ { key: "file.presignedUrl.create", version: "1.0.0" },
1081
+ { key: "file.version.create", version: "1.0.0" },
1082
+ { key: "file.version.list", version: "1.0.0" },
1083
+ { key: "attachment.attach", version: "1.0.0" },
1084
+ { key: "attachment.detach", version: "1.0.0" },
1085
+ { key: "attachment.list", version: "1.0.0" }
1086
+ ],
1087
+ events: [
1088
+ { key: "file.uploaded", version: "1.0.0" },
1089
+ { key: "file.updated", version: "1.0.0" },
1090
+ { key: "file.deleted", version: "1.0.0" },
1091
+ { key: "file.version_created", version: "1.0.0" },
1092
+ { key: "attachment.attached", version: "1.0.0" },
1093
+ { key: "attachment.detached", version: "1.0.0" },
1094
+ { key: "upload.session_started", version: "1.0.0" },
1095
+ { key: "upload.session_completed", version: "1.0.0" }
1096
+ ],
1097
+ presentations: [],
1098
+ opToPresentation: [],
1099
+ presentationsTargets: [],
1100
+ capabilities: {
1101
+ provides: [
1102
+ { key: "files", version: "1.0.0" },
1103
+ { key: "attachments", version: "1.0.0" }
1104
+ ],
1105
+ requires: [{ key: "identity", version: "1.0.0" }]
1106
+ }
1107
+ });
1108
+
1109
+ // src/storage/index.ts
1110
+ import * as fs from "node:fs/promises";
1111
+ import * as path from "node:path";
1112
+ import * as crypto from "node:crypto";
1113
+
1114
+ class LocalStorageAdapter {
1115
+ provider = "LOCAL";
1116
+ basePath;
1117
+ baseUrl;
1118
+ constructor(options) {
1119
+ this.basePath = options.basePath;
1120
+ this.baseUrl = options.baseUrl;
1121
+ }
1122
+ async upload(options) {
1123
+ const fullPath = path.join(this.basePath, options.path);
1124
+ const dir = path.dirname(fullPath);
1125
+ await fs.mkdir(dir, { recursive: true });
1126
+ const content = typeof options.content === "string" ? Buffer.from(options.content, "base64") : options.content;
1127
+ await fs.writeFile(fullPath, content);
1128
+ const checksum = crypto.createHash("sha256").update(content).digest("hex");
1129
+ return {
1130
+ path: options.path,
1131
+ size: content.length,
1132
+ mimeType: options.mimeType,
1133
+ checksum,
1134
+ metadata: options.metadata
1135
+ };
1136
+ }
1137
+ async download(filePath) {
1138
+ const fullPath = path.join(this.basePath, filePath);
1139
+ return fs.readFile(fullPath);
1140
+ }
1141
+ async delete(filePath) {
1142
+ const fullPath = path.join(this.basePath, filePath);
1143
+ await fs.unlink(fullPath);
1144
+ }
1145
+ async exists(filePath) {
1146
+ const fullPath = path.join(this.basePath, filePath);
1147
+ try {
1148
+ await fs.access(fullPath);
1149
+ return true;
1150
+ } catch {
1151
+ return false;
1152
+ }
1153
+ }
1154
+ async getMetadata(filePath) {
1155
+ const fullPath = path.join(this.basePath, filePath);
1156
+ try {
1157
+ const stat2 = await fs.stat(fullPath);
1158
+ return {
1159
+ path: filePath,
1160
+ size: stat2.size,
1161
+ mimeType: "application/octet-stream"
1162
+ };
1163
+ } catch {
1164
+ return null;
1165
+ }
1166
+ }
1167
+ async list(options) {
1168
+ const dir = options?.prefix ? path.join(this.basePath, options.prefix) : this.basePath;
1169
+ try {
1170
+ const entries = await fs.readdir(dir, { withFileTypes: true });
1171
+ const files = [];
1172
+ for (const entry of entries) {
1173
+ if (entry.isFile()) {
1174
+ const filePath = options?.prefix ? path.join(options.prefix, entry.name) : entry.name;
1175
+ const stat2 = await fs.stat(path.join(dir, entry.name));
1176
+ files.push({
1177
+ path: filePath,
1178
+ size: stat2.size,
1179
+ mimeType: "application/octet-stream"
1180
+ });
1181
+ }
1182
+ }
1183
+ const limit = options?.limit || files.length;
1184
+ return {
1185
+ files: files.slice(0, limit),
1186
+ hasMore: files.length > limit
1187
+ };
1188
+ } catch {
1189
+ return { files: [], hasMore: false };
1190
+ }
1191
+ }
1192
+ async createPresignedUpload(options) {
1193
+ const expiresIn = options.expiresIn || 3600;
1194
+ const expiresAt = new Date(Date.now() + expiresIn * 1000);
1195
+ return {
1196
+ url: this.baseUrl ? `${this.baseUrl}/upload?path=${encodeURIComponent(options.path)}` : `/upload?path=${encodeURIComponent(options.path)}`,
1197
+ fields: {
1198
+ path: options.path,
1199
+ mimeType: options.mimeType
1200
+ },
1201
+ expiresAt
1202
+ };
1203
+ }
1204
+ async createPresignedDownload(options) {
1205
+ const expiresIn = options.expiresIn || 3600;
1206
+ const expiresAt = new Date(Date.now() + expiresIn * 1000);
1207
+ return {
1208
+ url: this.baseUrl ? `${this.baseUrl}/download/${options.path}` : `/download/${options.path}`,
1209
+ expiresAt
1210
+ };
1211
+ }
1212
+ getPublicUrl(filePath) {
1213
+ if (!this.baseUrl)
1214
+ return null;
1215
+ return `${this.baseUrl}/${filePath}`;
1216
+ }
1217
+ async copy(sourcePath, destinationPath) {
1218
+ const sourceFullPath = path.join(this.basePath, sourcePath);
1219
+ const destFullPath = path.join(this.basePath, destinationPath);
1220
+ const destDir = path.dirname(destFullPath);
1221
+ await fs.mkdir(destDir, { recursive: true });
1222
+ await fs.copyFile(sourceFullPath, destFullPath);
1223
+ const stat2 = await fs.stat(destFullPath);
1224
+ return {
1225
+ path: destinationPath,
1226
+ size: stat2.size,
1227
+ mimeType: "application/octet-stream"
1228
+ };
1229
+ }
1230
+ }
1231
+
1232
+ class S3StorageAdapter {
1233
+ provider = "S3";
1234
+ config;
1235
+ constructor(options) {
1236
+ this.config = options;
1237
+ }
1238
+ async upload(_options) {
1239
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the upload method.");
1240
+ }
1241
+ async download(_filePath) {
1242
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the download method.");
1243
+ }
1244
+ async delete(_filePath) {
1245
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the delete method.");
1246
+ }
1247
+ async exists(_filePath) {
1248
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the exists method.");
1249
+ }
1250
+ async getMetadata(_filePath) {
1251
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the getMetadata method.");
1252
+ }
1253
+ async list(_options) {
1254
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the list method.");
1255
+ }
1256
+ async createPresignedUpload(_options) {
1257
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the createPresignedUpload method.");
1258
+ }
1259
+ async createPresignedDownload(_options) {
1260
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the createPresignedDownload method.");
1261
+ }
1262
+ getPublicUrl(filePath) {
1263
+ const { bucket, region, endpoint } = this.config;
1264
+ if (endpoint) {
1265
+ return `${endpoint}/${bucket}/${filePath}`;
1266
+ }
1267
+ return `https://${bucket}.s3.${region}.amazonaws.com/${filePath}`;
1268
+ }
1269
+ async copy(_sourcePath, _destinationPath) {
1270
+ throw new Error("S3 adapter requires @aws-sdk/client-s3. Install it and implement the copy method.");
1271
+ }
1272
+ }
1273
+
1274
+ class InMemoryStorageAdapter {
1275
+ provider = "LOCAL";
1276
+ files = new Map;
1277
+ async upload(options) {
1278
+ const content = typeof options.content === "string" ? Buffer.from(options.content, "base64") : options.content;
1279
+ const checksum = crypto.createHash("sha256").update(content).digest("hex");
1280
+ const metadata = {
1281
+ path: options.path,
1282
+ size: content.length,
1283
+ mimeType: options.mimeType,
1284
+ checksum,
1285
+ metadata: options.metadata
1286
+ };
1287
+ this.files.set(options.path, { content, metadata });
1288
+ return metadata;
1289
+ }
1290
+ async download(filePath) {
1291
+ const file = this.files.get(filePath);
1292
+ if (!file) {
1293
+ throw new Error(`File not found: ${filePath}`);
1294
+ }
1295
+ return file.content;
1296
+ }
1297
+ async delete(filePath) {
1298
+ this.files.delete(filePath);
1299
+ }
1300
+ async exists(filePath) {
1301
+ return this.files.has(filePath);
1302
+ }
1303
+ async getMetadata(filePath) {
1304
+ const file = this.files.get(filePath);
1305
+ return file?.metadata || null;
1306
+ }
1307
+ async list(options) {
1308
+ const prefix = options?.prefix || "";
1309
+ const files = [];
1310
+ for (const [path2, file] of this.files) {
1311
+ if (path2.startsWith(prefix)) {
1312
+ files.push(file.metadata);
1313
+ }
1314
+ }
1315
+ const limit = options?.limit || files.length;
1316
+ return {
1317
+ files: files.slice(0, limit),
1318
+ hasMore: files.length > limit
1319
+ };
1320
+ }
1321
+ async createPresignedUpload(options) {
1322
+ const expiresAt = new Date(Date.now() + (options.expiresIn || 3600) * 1000);
1323
+ return {
1324
+ url: `/upload?path=${encodeURIComponent(options.path)}`,
1325
+ fields: { path: options.path },
1326
+ expiresAt
1327
+ };
1328
+ }
1329
+ async createPresignedDownload(options) {
1330
+ const expiresAt = new Date(Date.now() + (options.expiresIn || 3600) * 1000);
1331
+ return {
1332
+ url: `/download/${options.path}`,
1333
+ expiresAt
1334
+ };
1335
+ }
1336
+ getPublicUrl(filePath) {
1337
+ return `/files/${filePath}`;
1338
+ }
1339
+ async copy(sourcePath, destinationPath) {
1340
+ const source = this.files.get(sourcePath);
1341
+ if (!source) {
1342
+ throw new Error(`Source file not found: ${sourcePath}`);
1343
+ }
1344
+ const metadata = {
1345
+ ...source.metadata,
1346
+ path: destinationPath
1347
+ };
1348
+ this.files.set(destinationPath, { content: source.content, metadata });
1349
+ return metadata;
1350
+ }
1351
+ clear() {
1352
+ this.files.clear();
1353
+ }
1354
+ }
1355
+ function createStorageAdapter(config) {
1356
+ switch (config.provider) {
1357
+ case "LOCAL":
1358
+ if (!config.local) {
1359
+ throw new Error("Local storage configuration required");
1360
+ }
1361
+ return new LocalStorageAdapter(config.local);
1362
+ case "S3":
1363
+ if (!config.s3) {
1364
+ throw new Error("S3 storage configuration required");
1365
+ }
1366
+ return new S3StorageAdapter(config.s3);
1367
+ default:
1368
+ throw new Error(`Unsupported storage provider: ${config.provider}`);
1369
+ }
1370
+ }
1371
+ export {
1372
+ filesSchemaContribution,
1373
+ fileEntities,
1374
+ createStorageAdapter,
1375
+ UploadSessionStartedEvent,
1376
+ UploadSessionEntity,
1377
+ UploadSessionCompletedEvent,
1378
+ UploadFileContract,
1379
+ UpdateFileContract,
1380
+ StorageProviderEnum,
1381
+ S3StorageAdapter,
1382
+ PresignedUrlModel,
1383
+ LocalStorageAdapter,
1384
+ ListFilesContract,
1385
+ ListAttachmentsContract,
1386
+ InMemoryStorageAdapter,
1387
+ GetVersionsContract,
1388
+ GetFileContract,
1389
+ GetDownloadUrlContract,
1390
+ FilesFeature,
1391
+ FileVersionModel,
1392
+ FileVersionEntity,
1393
+ FileVersionCreatedEvent,
1394
+ FileUploadedEvent,
1395
+ FileUpdatedEvent,
1396
+ FileStatusEnum,
1397
+ FileModel,
1398
+ FileEvents,
1399
+ FileEntity,
1400
+ FileDeletedEvent,
1401
+ DetachFileContract,
1402
+ DeleteFileContract,
1403
+ CreateVersionContract,
1404
+ CreatePresignedUrlContract,
1405
+ AttachmentModel,
1406
+ AttachmentEntity,
1407
+ AttachmentDetachedEvent,
1408
+ AttachmentAttachedEvent,
1409
+ AttachFileContract
1410
+ };