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

Commit

Permalink
fix: updated props destruction (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowko authored Feb 23, 2024
1 parent 1c46e5f commit 567bdb9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 73 deletions.
151 changes: 79 additions & 72 deletions client/server/api/emails.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ import { createComponentMetaCheckerByJsonConfig } from 'vue-component-meta'
import { destr } from 'destr'
import JSON5 from 'json5'
import type { Email } from '~/types/email'
import { createError, defineEventHandler, useStorage } from '#imports'

const rootDir = process.cwd()
const checker = createComponentMetaCheckerByJsonConfig(
rootDir,
{
extends: `${rootDir}/tsconfig.json`,
skipLibCheck: true,
include: ['emails/**/*'],
exclude: [],
},
{
forceUseTs: true,
printer: { newLine: 1 },
},
)

function stripeTypeScriptInternalTypesSchema(type: any): any {
if (!type)
Expand Down Expand Up @@ -55,6 +39,21 @@ function stripeTypeScriptInternalTypesSchema(type: any): any {
export default defineEventHandler(async () => {
try {
const nitroEmails = await useStorage('assets:emails').getKeys()
const rootDir = useRuntimeConfig().public.vueEmail.emailsDir || process.cwd()

const checker = createComponentMetaCheckerByJsonConfig(
rootDir,
{
extends: path.join(rootDir, '..', 'tsconfig.json'),
skipLibCheck: true,
include: ['**/*.vue'],
exclude: [],
},
{
forceUseTs: true,
printer: { newLine: 1 },
},
)

const emails: Email[] = await Promise.all(
nitroEmails.map(async (email) => {
Expand All @@ -64,85 +63,93 @@ export default defineEventHandler(async () => {
const emailData = JSON.parse(data)
const emailPath = path.join(
rootDir,
'emails',
email.replaceAll(':', '/'),
)
const { props } = checker.getComponentMeta(emailPath)
let emailProps = (props).filter(prop => !prop.global).sort((a, b) => {
if (!a.required && b.required)
return 1

if (a.required && !b.required)
return -1
let destructuredProps: any[] = []

if (a.type === 'boolean' && b.type !== 'boolean')
return 1
try {
const { props } = checker.getComponentMeta(emailPath)
let emailProps = (props).filter(prop => !prop.global).sort((a, b) => {
if (!a.required && b.required)
return 1

if (a.type !== 'boolean' && b.type === 'boolean')
return -1
if (a.required && !b.required)
return -1

return 0
})
emailProps = emailProps.map(stripeTypeScriptInternalTypesSchema)
const destructuredProps = emailProps.map((prop) => {
const destructuredType = prop.type.split('|').map((type) => {
type = type.trim()
const value = prop.default
if (a.type === 'boolean' && b.type !== 'boolean')
return 1

if (type === 'string') {
return {
type: 'string',
value: destr(value) ?? '',
if (a.type !== 'boolean' && b.type === 'boolean')
return -1

return 0
})

emailProps = emailProps.map(stripeTypeScriptInternalTypesSchema)
destructuredProps = emailProps.map((prop) => {
const destructuredType = prop.type.split('|').map((type) => {
type = type.trim()
const value = prop.default

if (type === 'string') {
return {
type: 'string',
value: destr(value) ?? '',
}
}
}

if (type === 'number') {
return {
type: 'number',
value: destr(value) || 0,
if (type === 'number') {
return {
type: 'number',
value: destr(value) || 0,
}
}
}

if (type === 'boolean') {
return {
type: 'boolean',
value: destr(value) || false,
if (type === 'boolean') {
return {
type: 'boolean',
value: destr(value) || false,
}
}
}

if (type === 'object' || type.includes('Record') || type.includes('Record<')) {
return {
type: 'object',
value: value ? JSON5.parse(value) : {},
if (type === 'object' || type.includes('Record') || type.includes('Record<')) {
return {
type: 'object',
value: value ? JSON5.parse(value) : {},
}
}
}

if (type === 'array' || type.includes('[]') || type.includes('Array') || type.includes('Array<')) {
return {
type: 'array',
value: value ? JSON5.parse(value) : [],
if (type === 'array' || type.includes('[]') || type.includes('Array') || type.includes('Array<')) {
return {
type: 'array',
value: value ? JSON5.parse(value) : [],
}
}

if (type === 'Date') {
return {
type: 'date',
value: value ? eval(value) : new Date().toISOString(),
}
}
}

if (type === 'Date') {
return {
type: 'date',
value: value ? eval(value) : new Date().toISOString(),
type: 'string',
value: value ?? '',
}
}
})

return {
type: 'string',
value: value ?? '',
label: prop.name,
type: destructuredType[0].type,
value: destructuredType[0].value,
}
})

return {
label: prop.name,
type: destructuredType[0].type,
value: destructuredType[0].value,
}
})
}
catch (error) {
console.warn('Error destructuring props', error)
}

const content = (await useStorage('assets:emails').getItem(
email,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/api/emails.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default defineEventHandler(async () => {
{
extends: path.join(rootDir, '..', 'tsconfig.json'),
skipLibCheck: true,
include: ['./emails/**/*.vue'],
include: ['**/*.vue'],
exclude: [],
},
{
Expand Down

0 comments on commit 567bdb9

Please sign in to comment.