Skip to content
This repository was archived by the owner on Jun 22, 2024. It is now read-only.

Commit 567bdb9

Browse files
authored
fix: updated props destruction (#31)
1 parent 1c46e5f commit 567bdb9

File tree

2 files changed

+80
-73
lines changed

2 files changed

+80
-73
lines changed

client/server/api/emails.get.ts

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ import { createComponentMetaCheckerByJsonConfig } from 'vue-component-meta'
44
import { destr } from 'destr'
55
import JSON5 from 'json5'
66
import type { Email } from '~/types/email'
7-
import { createError, defineEventHandler, useStorage } from '#imports'
8-
9-
const rootDir = process.cwd()
10-
const checker = createComponentMetaCheckerByJsonConfig(
11-
rootDir,
12-
{
13-
extends: `${rootDir}/tsconfig.json`,
14-
skipLibCheck: true,
15-
include: ['emails/**/*'],
16-
exclude: [],
17-
},
18-
{
19-
forceUseTs: true,
20-
printer: { newLine: 1 },
21-
},
22-
)
237

248
function stripeTypeScriptInternalTypesSchema(type: any): any {
259
if (!type)
@@ -55,6 +39,21 @@ function stripeTypeScriptInternalTypesSchema(type: any): any {
5539
export default defineEventHandler(async () => {
5640
try {
5741
const nitroEmails = await useStorage('assets:emails').getKeys()
42+
const rootDir = useRuntimeConfig().public.vueEmail.emailsDir || process.cwd()
43+
44+
const checker = createComponentMetaCheckerByJsonConfig(
45+
rootDir,
46+
{
47+
extends: path.join(rootDir, '..', 'tsconfig.json'),
48+
skipLibCheck: true,
49+
include: ['**/*.vue'],
50+
exclude: [],
51+
},
52+
{
53+
forceUseTs: true,
54+
printer: { newLine: 1 },
55+
},
56+
)
5857

5958
const emails: Email[] = await Promise.all(
6059
nitroEmails.map(async (email) => {
@@ -64,85 +63,93 @@ export default defineEventHandler(async () => {
6463
const emailData = JSON.parse(data)
6564
const emailPath = path.join(
6665
rootDir,
67-
'emails',
6866
email.replaceAll(':', '/'),
6967
)
70-
const { props } = checker.getComponentMeta(emailPath)
71-
let emailProps = (props).filter(prop => !prop.global).sort((a, b) => {
72-
if (!a.required && b.required)
73-
return 1
7468

75-
if (a.required && !b.required)
76-
return -1
69+
let destructuredProps: any[] = []
7770

78-
if (a.type === 'boolean' && b.type !== 'boolean')
79-
return 1
71+
try {
72+
const { props } = checker.getComponentMeta(emailPath)
73+
let emailProps = (props).filter(prop => !prop.global).sort((a, b) => {
74+
if (!a.required && b.required)
75+
return 1
8076

81-
if (a.type !== 'boolean' && b.type === 'boolean')
82-
return -1
77+
if (a.required && !b.required)
78+
return -1
8379

84-
return 0
85-
})
86-
emailProps = emailProps.map(stripeTypeScriptInternalTypesSchema)
87-
const destructuredProps = emailProps.map((prop) => {
88-
const destructuredType = prop.type.split('|').map((type) => {
89-
type = type.trim()
90-
const value = prop.default
80+
if (a.type === 'boolean' && b.type !== 'boolean')
81+
return 1
9182

92-
if (type === 'string') {
93-
return {
94-
type: 'string',
95-
value: destr(value) ?? '',
83+
if (a.type !== 'boolean' && b.type === 'boolean')
84+
return -1
85+
86+
return 0
87+
})
88+
89+
emailProps = emailProps.map(stripeTypeScriptInternalTypesSchema)
90+
destructuredProps = emailProps.map((prop) => {
91+
const destructuredType = prop.type.split('|').map((type) => {
92+
type = type.trim()
93+
const value = prop.default
94+
95+
if (type === 'string') {
96+
return {
97+
type: 'string',
98+
value: destr(value) ?? '',
99+
}
96100
}
97-
}
98101

99-
if (type === 'number') {
100-
return {
101-
type: 'number',
102-
value: destr(value) || 0,
102+
if (type === 'number') {
103+
return {
104+
type: 'number',
105+
value: destr(value) || 0,
106+
}
103107
}
104-
}
105108

106-
if (type === 'boolean') {
107-
return {
108-
type: 'boolean',
109-
value: destr(value) || false,
109+
if (type === 'boolean') {
110+
return {
111+
type: 'boolean',
112+
value: destr(value) || false,
113+
}
110114
}
111-
}
112115

113-
if (type === 'object' || type.includes('Record') || type.includes('Record<')) {
114-
return {
115-
type: 'object',
116-
value: value ? JSON5.parse(value) : {},
116+
if (type === 'object' || type.includes('Record') || type.includes('Record<')) {
117+
return {
118+
type: 'object',
119+
value: value ? JSON5.parse(value) : {},
120+
}
117121
}
118-
}
119122

120-
if (type === 'array' || type.includes('[]') || type.includes('Array') || type.includes('Array<')) {
121-
return {
122-
type: 'array',
123-
value: value ? JSON5.parse(value) : [],
123+
if (type === 'array' || type.includes('[]') || type.includes('Array') || type.includes('Array<')) {
124+
return {
125+
type: 'array',
126+
value: value ? JSON5.parse(value) : [],
127+
}
128+
}
129+
130+
if (type === 'Date') {
131+
return {
132+
type: 'date',
133+
value: value ? eval(value) : new Date().toISOString(),
134+
}
124135
}
125-
}
126136

127-
if (type === 'Date') {
128137
return {
129-
type: 'date',
130-
value: value ? eval(value) : new Date().toISOString(),
138+
type: 'string',
139+
value: value ?? '',
131140
}
132-
}
141+
})
133142

134143
return {
135-
type: 'string',
136-
value: value ?? '',
144+
label: prop.name,
145+
type: destructuredType[0].type,
146+
value: destructuredType[0].value,
137147
}
138148
})
139-
140-
return {
141-
label: prop.name,
142-
type: destructuredType[0].type,
143-
value: destructuredType[0].value,
144-
}
145-
})
149+
}
150+
catch (error) {
151+
console.warn('Error destructuring props', error)
152+
}
146153

147154
const content = (await useStorage('assets:emails').getItem(
148155
email,

src/runtime/server/api/emails.get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default defineEventHandler(async () => {
4747
{
4848
extends: path.join(rootDir, '..', 'tsconfig.json'),
4949
skipLibCheck: true,
50-
include: ['./emails/**/*.vue'],
50+
include: ['**/*.vue'],
5151
exclude: [],
5252
},
5353
{

0 commit comments

Comments
 (0)