From 019fd3c7e0aa227c13df9ac5f46f4d734cc52e6b Mon Sep 17 00:00:00 2001 From: daedalus <44623501+ComfortablyCoding@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:31:19 -0400 Subject: [PATCH] refactor(directus module) add error handling (#97) * default baseUrl to directus default * add error handling for redirect and global fetch * default directus host is `0.0.0.0` * prefer localhost over 0.0.0.0 --- modules/directus/index.ts | 64 ++++++++++++++++++++++++--------------- nuxt.config.ts | 2 +- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/modules/directus/index.ts b/modules/directus/index.ts index 3521493..2f5d53e 100644 --- a/modules/directus/index.ts +++ b/modules/directus/index.ts @@ -160,38 +160,52 @@ export default defineNuxtModule({ const directus = createDirectus(moduleOptions.rest.baseUrl).with(rest()); // Handle Redirects - const redirects = await directus.request(readItems('redirects')); + try { + const redirects = await directus.request(readItems('redirects')); + + for (const redirect of redirects) { + let responseCode = redirect.response_code ? parseInt(redirect.response_code as any) : 301; + + if (responseCode !== 301 && responseCode !== 302) { + responseCode = 301; + } + + // Add the redirect to the route rules + // https://nuxt.com/docs/guide/concepts/rendering#route-rules + extendRouteRules(redirect.url_old as string, { + redirect: { + to: redirect.url_new, + statusCode: responseCode as 301 | 302, + }, + }); + } - for (const redirect of redirects) { - let responseCode = redirect.response_code ? parseInt(redirect.response_code as any) : 301; + log.success(`${redirects.length} Redirects loaded`); - if (responseCode !== 301 && responseCode !== 302) { - responseCode = 301; + for (const redirect of redirects) { + log.info(` • ${redirect.response_code}`, `From: ${redirect.url_old}`, `To: ${redirect.url_new}`); } - - // Add the redirect to the route rules - // https://nuxt.com/docs/guide/concepts/rendering#route-rules - extendRouteRules(redirect.url_old as string, { - redirect: { - to: redirect.url_new, - statusCode: responseCode as 301 | 302, - }, - }); + } catch (error) { + log.warn('Unable to load redirects due to the following error'); + log.error(error); + log.warn(`Please ensure the directus instance is reachable at ${moduleOptions.rest.baseUrl}.`); } - log.success(`${redirects.length} Redirects loaded`); - - for (const redirect of redirects) { - log.info(` • ${redirect.response_code}`, `From: ${redirect.url_old}`, `To: ${redirect.url_new}`); - } + try { + // Add Globals + const globals = await directus.request>(readSingleton('globals')); + nuxt.options.appConfig.globals = defu(nuxt.options.appConfig.globals, globals); + log.success('Globals loaded into appConfig'); - // Add Globals - const globals = await directus.request>(readSingleton('globals')); - nuxt.options.appConfig.globals = defu(nuxt.options.appConfig.globals, globals); - log.success('Globals loaded into appConfig'); + // Add title template to the app head for use with useHead composable + nuxt.options.app.head.titleTemplate = `%s - ${globals?.title ?? 'Agency OS'}`; + } catch (error) { + nuxt.options.app.head.titleTemplate = '%s - Agency OS'; - // Add title template to the app head for use with useHead composable - nuxt.options.app.head.titleTemplate = `%s - ${globals?.title ?? 'Agency OS'}`; + log.warn('Unable to load redirects due to the following error'); + log.error(error); + log.warn(`Please ensure the directus instance is reachable at ${moduleOptions.rest.baseUrl}.`); + } log.success(`Directus Module Loaded`); }, diff --git a/nuxt.config.ts b/nuxt.config.ts index ae6c7d2..1159566 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -52,7 +52,7 @@ export default defineNuxtConfig({ // Directus Configuration directus: { rest: { - baseUrl: process.env.DIRECTUS_URL, + baseUrl: process.env.DIRECTUS_URL || 'http://localhost:8055', nuxtBaseUrl: process.env.NUXT_PUBLIC_SITE_URL || 'http://localhost:3000', }, auth: {