From ccf435e54b1bfa899e5ea79330193cd9d068b3ac Mon Sep 17 00:00:00 2001 From: Younes Barrad Date: Fri, 23 Feb 2024 10:33:09 +0100 Subject: [PATCH] fix: updated props destruction --- client/server/api/emails.get.ts | 151 ++++++++++++++------------- src/runtime/server/api/emails.get.ts | 2 +- 2 files changed, 80 insertions(+), 73 deletions(-) diff --git a/client/server/api/emails.get.ts b/client/server/api/emails.get.ts index e084171..181df9f 100644 --- a/client/server/api/emails.get.ts +++ b/client/server/api/emails.get.ts @@ -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) @@ -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) => { @@ -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, diff --git a/src/runtime/server/api/emails.get.ts b/src/runtime/server/api/emails.get.ts index 62bb071..5ea631f 100644 --- a/src/runtime/server/api/emails.get.ts +++ b/src/runtime/server/api/emails.get.ts @@ -47,7 +47,7 @@ export default defineEventHandler(async () => { { extends: path.join(rootDir, '..', 'tsconfig.json'), skipLibCheck: true, - include: ['./emails/**/*.vue'], + include: ['**/*.vue'], exclude: [], }, {