@open-mercato/shared 0.4.2-canary-c02407ff85
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/build.mjs +101 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +7 -0
- package/dist/lib/api/crud.js +47 -0
- package/dist/lib/api/crud.js.map +7 -0
- package/dist/lib/api/scoped.js +140 -0
- package/dist/lib/api/scoped.js.map +7 -0
- package/dist/lib/auth/jwt.js +34 -0
- package/dist/lib/auth/jwt.js.map +7 -0
- package/dist/lib/auth/server.js +157 -0
- package/dist/lib/auth/server.js.map +7 -0
- package/dist/lib/boolean.js +22 -0
- package/dist/lib/boolean.js.map +7 -0
- package/dist/lib/bootstrap/appResolver.js +43 -0
- package/dist/lib/bootstrap/appResolver.js.map +7 -0
- package/dist/lib/bootstrap/dynamicLoader.js +108 -0
- package/dist/lib/bootstrap/dynamicLoader.js.map +7 -0
- package/dist/lib/bootstrap/factory.js +59 -0
- package/dist/lib/bootstrap/factory.js.map +7 -0
- package/dist/lib/bootstrap/index.js +11 -0
- package/dist/lib/bootstrap/index.js.map +7 -0
- package/dist/lib/bootstrap/types.js +1 -0
- package/dist/lib/bootstrap/types.js.map +7 -0
- package/dist/lib/cache/segments.js +36 -0
- package/dist/lib/cache/segments.js.map +7 -0
- package/dist/lib/cli/progress.js +46 -0
- package/dist/lib/cli/progress.js.map +7 -0
- package/dist/lib/commands/command-bus.js +285 -0
- package/dist/lib/commands/command-bus.js.map +7 -0
- package/dist/lib/commands/customFieldSnapshots.js +66 -0
- package/dist/lib/commands/customFieldSnapshots.js.map +7 -0
- package/dist/lib/commands/helpers.js +98 -0
- package/dist/lib/commands/helpers.js.map +7 -0
- package/dist/lib/commands/index.js +8 -0
- package/dist/lib/commands/index.js.map +7 -0
- package/dist/lib/commands/operationMetadata.js +32 -0
- package/dist/lib/commands/operationMetadata.js.map +7 -0
- package/dist/lib/commands/registry.js +43 -0
- package/dist/lib/commands/registry.js.map +7 -0
- package/dist/lib/commands/scope.js +44 -0
- package/dist/lib/commands/scope.js.map +7 -0
- package/dist/lib/commands/types.js +8 -0
- package/dist/lib/commands/types.js.map +7 -0
- package/dist/lib/crud/cache-stats.js +98 -0
- package/dist/lib/crud/cache-stats.js.map +7 -0
- package/dist/lib/crud/cache.js +175 -0
- package/dist/lib/crud/cache.js.map +7 -0
- package/dist/lib/crud/custom-fields-client.js +52 -0
- package/dist/lib/crud/custom-fields-client.js.map +7 -0
- package/dist/lib/crud/custom-fields.js +467 -0
- package/dist/lib/crud/custom-fields.js.map +7 -0
- package/dist/lib/crud/errors.js +24 -0
- package/dist/lib/crud/errors.js.map +7 -0
- package/dist/lib/crud/exporters.js +154 -0
- package/dist/lib/crud/exporters.js.map +7 -0
- package/dist/lib/crud/factory.js +1311 -0
- package/dist/lib/crud/factory.js.map +7 -0
- package/dist/lib/crud/types.js +1 -0
- package/dist/lib/crud/types.js.map +7 -0
- package/dist/lib/custom-fields/normalize.js +36 -0
- package/dist/lib/custom-fields/normalize.js.map +7 -0
- package/dist/lib/data/engine.js +396 -0
- package/dist/lib/data/engine.js.map +7 -0
- package/dist/lib/db/escapeLikePattern.js +5 -0
- package/dist/lib/db/escapeLikePattern.js.map +7 -0
- package/dist/lib/db/mikro.js +82 -0
- package/dist/lib/db/mikro.js.map +7 -0
- package/dist/lib/di/container.js +94 -0
- package/dist/lib/di/container.js.map +7 -0
- package/dist/lib/email/send.js +12 -0
- package/dist/lib/email/send.js.map +7 -0
- package/dist/lib/encryption/aes.js +58 -0
- package/dist/lib/encryption/aes.js.map +7 -0
- package/dist/lib/encryption/customFieldValues.js +49 -0
- package/dist/lib/encryption/customFieldValues.js.map +7 -0
- package/dist/lib/encryption/entityFields.js +26 -0
- package/dist/lib/encryption/entityFields.js.map +7 -0
- package/dist/lib/encryption/entityIds.js +80 -0
- package/dist/lib/encryption/entityIds.js.map +7 -0
- package/dist/lib/encryption/find.js +45 -0
- package/dist/lib/encryption/find.js.map +7 -0
- package/dist/lib/encryption/indexDoc.js +69 -0
- package/dist/lib/encryption/indexDoc.js.map +7 -0
- package/dist/lib/encryption/kms.js +282 -0
- package/dist/lib/encryption/kms.js.map +7 -0
- package/dist/lib/encryption/subscriber.js +330 -0
- package/dist/lib/encryption/subscriber.js.map +7 -0
- package/dist/lib/encryption/tenantDataEncryptionService.js +252 -0
- package/dist/lib/encryption/tenantDataEncryptionService.js.map +7 -0
- package/dist/lib/encryption/toggles.js +18 -0
- package/dist/lib/encryption/toggles.js.map +7 -0
- package/dist/lib/entities/naming.js +9 -0
- package/dist/lib/entities/naming.js.map +7 -0
- package/dist/lib/entities/system-entities.js +43 -0
- package/dist/lib/entities/system-entities.js.map +7 -0
- package/dist/lib/frontend/organizationEvents.js +41 -0
- package/dist/lib/frontend/organizationEvents.js.map +7 -0
- package/dist/lib/frontend/useOrganizationScope.js +32 -0
- package/dist/lib/frontend/useOrganizationScope.js.map +7 -0
- package/dist/lib/hotkeys/index.js +128 -0
- package/dist/lib/hotkeys/index.js.map +7 -0
- package/dist/lib/i18n/app-dictionaries.js +17 -0
- package/dist/lib/i18n/app-dictionaries.js.map +7 -0
- package/dist/lib/i18n/config.js +7 -0
- package/dist/lib/i18n/config.js.map +7 -0
- package/dist/lib/i18n/context.js +50 -0
- package/dist/lib/i18n/context.js.map +7 -0
- package/dist/lib/i18n/server.js +68 -0
- package/dist/lib/i18n/server.js.map +7 -0
- package/dist/lib/i18n/translate.js +45 -0
- package/dist/lib/i18n/translate.js.map +7 -0
- package/dist/lib/indexers/error-log.js +82 -0
- package/dist/lib/indexers/error-log.js.map +7 -0
- package/dist/lib/indexers/status-log.js +80 -0
- package/dist/lib/indexers/status-log.js.map +7 -0
- package/dist/lib/lib/auth/jwt.js +34 -0
- package/dist/lib/lib/auth/jwt.js.map +7 -0
- package/dist/lib/lib/auth/server.js +77 -0
- package/dist/lib/lib/auth/server.js.map +7 -0
- package/dist/lib/lib/email/send.js +12 -0
- package/dist/lib/lib/email/send.js.map +7 -0
- package/dist/lib/lib/i18n/config.js +7 -0
- package/dist/lib/lib/i18n/config.js.map +7 -0
- package/dist/lib/lib/i18n/context.js +31 -0
- package/dist/lib/lib/i18n/context.js.map +7 -0
- package/dist/lib/lib/utils.js +9 -0
- package/dist/lib/lib/utils.js.map +7 -0
- package/dist/lib/location/countries.js +68 -0
- package/dist/lib/location/countries.js.map +7 -0
- package/dist/lib/modules/index.js +6 -0
- package/dist/lib/modules/index.js.map +7 -0
- package/dist/lib/modules/registry.js +18 -0
- package/dist/lib/modules/registry.js.map +7 -0
- package/dist/lib/openapi/crud.js +137 -0
- package/dist/lib/openapi/crud.js.map +7 -0
- package/dist/lib/openapi/generator.js +1131 -0
- package/dist/lib/openapi/generator.js.map +7 -0
- package/dist/lib/openapi/index.js +10 -0
- package/dist/lib/openapi/index.js.map +7 -0
- package/dist/lib/openapi/sanitize.js +110 -0
- package/dist/lib/openapi/sanitize.js.map +7 -0
- package/dist/lib/openapi/types.js +1 -0
- package/dist/lib/openapi/types.js.map +7 -0
- package/dist/lib/profiler/index.js +258 -0
- package/dist/lib/profiler/index.js.map +7 -0
- package/dist/lib/query/engine.js +729 -0
- package/dist/lib/query/engine.js.map +7 -0
- package/dist/lib/query/join-utils.js +195 -0
- package/dist/lib/query/join-utils.js.map +7 -0
- package/dist/lib/query/types.js +9 -0
- package/dist/lib/query/types.js.map +7 -0
- package/dist/lib/search/config.js +32 -0
- package/dist/lib/search/config.js.map +7 -0
- package/dist/lib/search/tokenize.js +34 -0
- package/dist/lib/search/tokenize.js.map +7 -0
- package/dist/lib/slugify.js +24 -0
- package/dist/lib/slugify.js.map +7 -0
- package/dist/lib/testing/bootstrap.js +51 -0
- package/dist/lib/testing/bootstrap.js.map +7 -0
- package/dist/lib/testing/index.js +17 -0
- package/dist/lib/testing/index.js.map +7 -0
- package/dist/lib/testing/renderWithProviders.js +15 -0
- package/dist/lib/testing/renderWithProviders.js.map +7 -0
- package/dist/lib/url.js +12 -0
- package/dist/lib/url.js.map +7 -0
- package/dist/lib/utils.js +13 -0
- package/dist/lib/utils.js.map +7 -0
- package/dist/lib/version.js +7 -0
- package/dist/lib/version.js.map +7 -0
- package/dist/modules/dashboard/widgets.js +1 -0
- package/dist/modules/dashboard/widgets.js.map +7 -0
- package/dist/modules/dsl.js +30 -0
- package/dist/modules/dsl.js.map +7 -0
- package/dist/modules/entities/kinds.js +22 -0
- package/dist/modules/entities/kinds.js.map +7 -0
- package/dist/modules/entities/options.js +26 -0
- package/dist/modules/entities/options.js.map +7 -0
- package/dist/modules/entities/validation.js +102 -0
- package/dist/modules/entities/validation.js.map +7 -0
- package/dist/modules/entities/validators.js +88 -0
- package/dist/modules/entities/validators.js.map +7 -0
- package/dist/modules/entities.js +1 -0
- package/dist/modules/entities.js.map +7 -0
- package/dist/modules/navigation/sidebarPreferences.js +50 -0
- package/dist/modules/navigation/sidebarPreferences.js.map +7 -0
- package/dist/modules/perspectives/types.js +1 -0
- package/dist/modules/perspectives/types.js.map +7 -0
- package/dist/modules/registry.js +96 -0
- package/dist/modules/registry.js.map +7 -0
- package/dist/modules/search.js +15 -0
- package/dist/modules/search.js.map +7 -0
- package/dist/modules/vector.js +1 -0
- package/dist/modules/vector.js.map +7 -0
- package/dist/modules/widgets/injection-loader.js +180 -0
- package/dist/modules/widgets/injection-loader.js.map +7 -0
- package/dist/modules/widgets/injection.js +1 -0
- package/dist/modules/widgets/injection.js.map +7 -0
- package/dist/security/features.js +23 -0
- package/dist/security/features.js.map +7 -0
- package/dist/types/pg.d.js +1 -0
- package/dist/types/pg.d.js.map +7 -0
- package/dist/types/react-email.d.js +1 -0
- package/dist/types/react-email.d.js.map +7 -0
- package/dist/types/resend.d.js +1 -0
- package/dist/types/resend.d.js.map +7 -0
- package/jest.config.cjs +22 -0
- package/package.json +88 -0
- package/src/index.ts +0 -0
- package/src/lib/api/__tests__/scoped.test.ts +38 -0
- package/src/lib/api/crud.ts +59 -0
- package/src/lib/api/scoped.ts +239 -0
- package/src/lib/auth/jwt.ts +39 -0
- package/src/lib/auth/server.ts +199 -0
- package/src/lib/boolean.ts +17 -0
- package/src/lib/bootstrap/appResolver.ts +85 -0
- package/src/lib/bootstrap/dynamicLoader.ts +177 -0
- package/src/lib/bootstrap/factory.ts +108 -0
- package/src/lib/bootstrap/index.ts +23 -0
- package/src/lib/bootstrap/types.ts +31 -0
- package/src/lib/cache/segments.ts +56 -0
- package/src/lib/cli/progress.ts +55 -0
- package/src/lib/commands/__tests__/command-bus.test.ts +84 -0
- package/src/lib/commands/__tests__/helpers.test.ts +42 -0
- package/src/lib/commands/command-bus.ts +349 -0
- package/src/lib/commands/customFieldSnapshots.ts +86 -0
- package/src/lib/commands/helpers.ts +143 -0
- package/src/lib/commands/index.ts +4 -0
- package/src/lib/commands/operationMetadata.ts +40 -0
- package/src/lib/commands/registry.ts +46 -0
- package/src/lib/commands/scope.ts +59 -0
- package/src/lib/commands/types.ts +63 -0
- package/src/lib/crud/__tests__/crud-factory.test.ts +333 -0
- package/src/lib/crud/__tests__/custom-fields.test.ts +150 -0
- package/src/lib/crud/cache-stats.ts +127 -0
- package/src/lib/crud/cache.ts +205 -0
- package/src/lib/crud/custom-fields-client.ts +54 -0
- package/src/lib/crud/custom-fields.ts +607 -0
- package/src/lib/crud/errors.ts +23 -0
- package/src/lib/crud/exporters.ts +188 -0
- package/src/lib/crud/factory.ts +1622 -0
- package/src/lib/crud/types.ts +29 -0
- package/src/lib/custom-fields/normalize.ts +45 -0
- package/src/lib/data/engine.ts +562 -0
- package/src/lib/db/escapeLikePattern.ts +2 -0
- package/src/lib/db/mikro.ts +100 -0
- package/src/lib/di/container.ts +105 -0
- package/src/lib/email/send.ts +18 -0
- package/src/lib/encryption/__tests__/customFieldValues.test.ts +63 -0
- package/src/lib/encryption/__tests__/indexDoc.test.ts +115 -0
- package/src/lib/encryption/aes.ts +64 -0
- package/src/lib/encryption/customFieldValues.ts +67 -0
- package/src/lib/encryption/entityFields.ts +39 -0
- package/src/lib/encryption/entityIds.ts +107 -0
- package/src/lib/encryption/find.ts +81 -0
- package/src/lib/encryption/indexDoc.ts +104 -0
- package/src/lib/encryption/kms.ts +337 -0
- package/src/lib/encryption/subscriber.ts +416 -0
- package/src/lib/encryption/tenantDataEncryptionService.ts +313 -0
- package/src/lib/encryption/toggles.ts +15 -0
- package/src/lib/entities/naming.ts +6 -0
- package/src/lib/entities/system-entities.ts +43 -0
- package/src/lib/frontend/organizationEvents.ts +55 -0
- package/src/lib/frontend/useOrganizationScope.ts +30 -0
- package/src/lib/hotkeys/index.ts +168 -0
- package/src/lib/i18n/app-dictionaries.ts +18 -0
- package/src/lib/i18n/config.ts +4 -0
- package/src/lib/i18n/context.tsx +66 -0
- package/src/lib/i18n/server.ts +74 -0
- package/src/lib/i18n/translate.ts +54 -0
- package/src/lib/indexers/error-log.ts +106 -0
- package/src/lib/indexers/status-log.ts +119 -0
- package/src/lib/lib/auth/jwt.ts +39 -0
- package/src/lib/lib/auth/server.ts +94 -0
- package/src/lib/lib/email/send.ts +18 -0
- package/src/lib/lib/i18n/config.ts +4 -0
- package/src/lib/lib/i18n/context.tsx +38 -0
- package/src/lib/lib/utils.ts +6 -0
- package/src/lib/location/countries.ts +97 -0
- package/src/lib/modules/index.ts +1 -0
- package/src/lib/modules/registry.ts +18 -0
- package/src/lib/openapi/crud.ts +218 -0
- package/src/lib/openapi/generator.ts +1311 -0
- package/src/lib/openapi/index.ts +4 -0
- package/src/lib/openapi/sanitize.ts +137 -0
- package/src/lib/openapi/types.ts +79 -0
- package/src/lib/profiler/index.ts +371 -0
- package/src/lib/query/__tests__/engine.test.ts +274 -0
- package/src/lib/query/engine.ts +837 -0
- package/src/lib/query/join-utils.ts +238 -0
- package/src/lib/query/types.ts +121 -0
- package/src/lib/search/config.ts +49 -0
- package/src/lib/search/tokenize.ts +45 -0
- package/src/lib/slugify.ts +28 -0
- package/src/lib/testing/bootstrap.ts +124 -0
- package/src/lib/testing/index.ts +15 -0
- package/src/lib/testing/renderWithProviders.tsx +31 -0
- package/src/lib/url.ts +12 -0
- package/src/lib/utils.ts +17 -0
- package/src/lib/version.ts +5 -0
- package/src/modules/__tests__/dsl.test.ts +35 -0
- package/src/modules/__tests__/registry.test.ts +300 -0
- package/src/modules/dashboard/widgets.ts +57 -0
- package/src/modules/dsl.ts +32 -0
- package/src/modules/entities/__tests__/validation.test.ts +52 -0
- package/src/modules/entities/kinds.ts +20 -0
- package/src/modules/entities/options.ts +36 -0
- package/src/modules/entities/validation.ts +118 -0
- package/src/modules/entities/validators.ts +93 -0
- package/src/modules/entities.ts +102 -0
- package/src/modules/navigation/sidebarPreferences.ts +62 -0
- package/src/modules/perspectives/types.ts +40 -0
- package/src/modules/registry.ts +249 -0
- package/src/modules/search.ts +325 -0
- package/src/modules/vector.ts +122 -0
- package/src/modules/widgets/__tests__/injection.test.ts +48 -0
- package/src/modules/widgets/injection-loader.ts +235 -0
- package/src/modules/widgets/injection.ts +120 -0
- package/src/security/features.ts +22 -0
- package/src/types/pg.d.ts +2 -0
- package/src/types/react-email.d.ts +2 -0
- package/src/types/resend.d.ts +2 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +9 -0
- package/watch.mjs +6 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
export type CrudExportFormat = 'csv' | 'json' | 'xml' | 'markdown'
|
|
2
|
+
|
|
3
|
+
export type CrudExportColumn = {
|
|
4
|
+
field: string
|
|
5
|
+
header: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type PreparedExport = {
|
|
9
|
+
columns: CrudExportColumn[]
|
|
10
|
+
rows: Array<Record<string, unknown>>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type SerializedExport = {
|
|
14
|
+
body: string
|
|
15
|
+
contentType: string
|
|
16
|
+
fileExtension: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const CSV_CONTENT_TYPE = 'text/csv; charset=utf-8'
|
|
20
|
+
const JSON_CONTENT_TYPE = 'application/json; charset=utf-8'
|
|
21
|
+
const XML_CONTENT_TYPE = 'application/xml; charset=utf-8'
|
|
22
|
+
const MARKDOWN_CONTENT_TYPE = 'text/markdown; charset=utf-8'
|
|
23
|
+
|
|
24
|
+
function normalizeValue(value: unknown): string {
|
|
25
|
+
if (value == null) return ''
|
|
26
|
+
if (value instanceof Date) return value.toISOString()
|
|
27
|
+
if (typeof value === 'bigint') return value.toString()
|
|
28
|
+
if (typeof value === 'object') {
|
|
29
|
+
if (Array.isArray(value)) return value.map((v) => normalizeValue(v)).filter(Boolean).join(', ')
|
|
30
|
+
return JSON.stringify(value)
|
|
31
|
+
}
|
|
32
|
+
return String(value)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function escapeCsv(value: string): string {
|
|
36
|
+
if (/[",\n\r]/.test(value)) {
|
|
37
|
+
return `"${value.replace(/"/g, '""')}"`
|
|
38
|
+
}
|
|
39
|
+
return value
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function escapeMarkdown(value: string): string {
|
|
43
|
+
const escaped = value
|
|
44
|
+
.replace(/\\/g, '\\\\')
|
|
45
|
+
.replace(/\|/g, '\\|')
|
|
46
|
+
.replace(/\r?\n/g, '<br />')
|
|
47
|
+
return escaped || ' '
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function escapeXmlTag(tag: string, fallbackIndex: number): string {
|
|
51
|
+
const sanitized = tag.replace(/[^A-Za-z0-9_:-]/g, '_')
|
|
52
|
+
const normalized = sanitized.length > 0 ? sanitized : `field_${fallbackIndex}`
|
|
53
|
+
if (/^[^A-Za-z_]/.test(normalized)) {
|
|
54
|
+
return `f_${normalized}`
|
|
55
|
+
}
|
|
56
|
+
return normalized
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function escapeXmlValue(value: string): string {
|
|
60
|
+
return value
|
|
61
|
+
.replace(/&/g, '&')
|
|
62
|
+
.replace(/</g, '<')
|
|
63
|
+
.replace(/>/g, '>')
|
|
64
|
+
.replace(/"/g, '"')
|
|
65
|
+
.replace(/'/g, ''')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function serializeCsv(prepared: PreparedExport): SerializedExport {
|
|
69
|
+
const headers = prepared.columns.map((col) => col.header)
|
|
70
|
+
const lines = [headers.join(',')]
|
|
71
|
+
for (const row of prepared.rows) {
|
|
72
|
+
const values = prepared.columns.map((col) => {
|
|
73
|
+
const raw = normalizeValue(row[col.field])
|
|
74
|
+
return escapeCsv(raw)
|
|
75
|
+
})
|
|
76
|
+
lines.push(values.join(','))
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
body: lines.join('\n'),
|
|
80
|
+
contentType: CSV_CONTENT_TYPE,
|
|
81
|
+
fileExtension: 'csv',
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function serializeJson(prepared: PreparedExport): SerializedExport {
|
|
86
|
+
// For JSON we return objects keyed by exported field names with human headers as metadata
|
|
87
|
+
const payload = prepared.rows.map((row) => {
|
|
88
|
+
const obj: Record<string, unknown> = {}
|
|
89
|
+
for (const column of prepared.columns) {
|
|
90
|
+
obj[column.header] = row[column.field]
|
|
91
|
+
}
|
|
92
|
+
return obj
|
|
93
|
+
})
|
|
94
|
+
return {
|
|
95
|
+
body: JSON.stringify(payload, null, 2),
|
|
96
|
+
contentType: JSON_CONTENT_TYPE,
|
|
97
|
+
fileExtension: 'json',
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function serializeXml(prepared: PreparedExport): SerializedExport {
|
|
102
|
+
const lines: string[] = ['<?xml version="1.0" encoding="UTF-8"?>', '<records>']
|
|
103
|
+
for (const row of prepared.rows) {
|
|
104
|
+
lines.push(' <record>')
|
|
105
|
+
prepared.columns.forEach((column, index) => {
|
|
106
|
+
const tag = escapeXmlTag(column.field || column.header, index)
|
|
107
|
+
const value = escapeXmlValue(normalizeValue(row[column.field]))
|
|
108
|
+
lines.push(` <${tag}>${value}</${tag}>`)
|
|
109
|
+
})
|
|
110
|
+
lines.push(' </record>')
|
|
111
|
+
}
|
|
112
|
+
lines.push('</records>')
|
|
113
|
+
return {
|
|
114
|
+
body: lines.join('\n'),
|
|
115
|
+
contentType: XML_CONTENT_TYPE,
|
|
116
|
+
fileExtension: 'xml',
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function serializeMarkdown(prepared: PreparedExport): SerializedExport {
|
|
121
|
+
const headers = prepared.columns.map((col) => escapeMarkdown(col.header))
|
|
122
|
+
const headerLine = `| ${headers.join(' | ')} |`
|
|
123
|
+
const dividerLine = `| ${prepared.columns.map(() => '---').join(' | ')} |`
|
|
124
|
+
const rows = prepared.rows.map((row) => {
|
|
125
|
+
const cells = prepared.columns.map((col) => escapeMarkdown(normalizeValue(row[col.field])))
|
|
126
|
+
return `| ${cells.join(' | ')} |`
|
|
127
|
+
})
|
|
128
|
+
const body = [headerLine, dividerLine, ...rows].join('\n')
|
|
129
|
+
return {
|
|
130
|
+
body,
|
|
131
|
+
contentType: MARKDOWN_CONTENT_TYPE,
|
|
132
|
+
fileExtension: 'md',
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function serializeExport(prepared: PreparedExport, format: CrudExportFormat): SerializedExport {
|
|
137
|
+
switch (format) {
|
|
138
|
+
case 'csv':
|
|
139
|
+
return serializeCsv(prepared)
|
|
140
|
+
case 'json':
|
|
141
|
+
return serializeJson(prepared)
|
|
142
|
+
case 'xml':
|
|
143
|
+
return serializeXml(prepared)
|
|
144
|
+
case 'markdown':
|
|
145
|
+
return serializeMarkdown(prepared)
|
|
146
|
+
default:
|
|
147
|
+
return serializeJson(prepared)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function normalizeExportFormat(raw: unknown): CrudExportFormat | null {
|
|
152
|
+
if (typeof raw !== 'string' || raw.trim() === '') return null
|
|
153
|
+
const value = raw.toLowerCase()
|
|
154
|
+
if (value === 'csv') return 'csv'
|
|
155
|
+
if (value === 'json' || value === 'application/json') return 'json'
|
|
156
|
+
if (value === 'xml' || value === 'application/xml') return 'xml'
|
|
157
|
+
if (value === 'markdown' || value === 'md' || value === 'text/markdown') return 'markdown'
|
|
158
|
+
return null
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function defaultExportFilename(base: string | undefined | null, format: CrudExportFormat): string {
|
|
162
|
+
const safeBase = (base && base.trim().length > 0 ? base.trim() : 'export')
|
|
163
|
+
.replace(/[^a-z0-9_\-]/gi, '_')
|
|
164
|
+
const suffix = format === 'markdown' ? 'md' : format
|
|
165
|
+
return `${safeBase}.${suffix}`
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function toHeaderLabel(key: string): string {
|
|
169
|
+
const withoutPrefix = key.replace(/^cf[:_\-\s]+/i, '')
|
|
170
|
+
const normalized = withoutPrefix.replace(/[_\-\s]+/g, ' ').trim()
|
|
171
|
+
if (!normalized) return 'Field'
|
|
172
|
+
return normalized.replace(/\b\w/g, (char) => char.toUpperCase())
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function ensureColumns(rows: Array<Record<string, unknown>>, hint?: CrudExportColumn[]): CrudExportColumn[] {
|
|
176
|
+
if (hint && hint.length > 0) return hint
|
|
177
|
+
const used = new Map<string, string>()
|
|
178
|
+
rows.forEach((row) => {
|
|
179
|
+
Object.keys(row || {}).forEach((key) => {
|
|
180
|
+
if (!used.has(key)) used.set(key, key)
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
if (used.size === 0) return [{ field: 'id', header: 'ID' }]
|
|
184
|
+
return Array.from(used.keys()).map((key) => ({
|
|
185
|
+
field: key,
|
|
186
|
+
header: toHeaderLabel(key),
|
|
187
|
+
}))
|
|
188
|
+
}
|