Skip to content

Commit

Permalink
refactor(directus module) add error handling (#97)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ComfortablyCoding authored Jul 18, 2024
1 parent 435d091 commit 019fd3c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
64 changes: 39 additions & 25 deletions modules/directus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,38 +160,52 @@ export default defineNuxtModule({
const directus = createDirectus<Schema>(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<Omit<Globals, 'id' | 'url'>>(readSingleton('globals'));
nuxt.options.appConfig.globals = defu(nuxt.options.appConfig.globals, globals);

Check failure on line 197 in modules/directus/index.ts

View workflow job for this annotation

GitHub Actions / Typecheck

Argument of type 'unknown' is not assignable to parameter of type 'Input | IgnoredInput'.
log.success('Globals loaded into appConfig');

// Add Globals
const globals = await directus.request<Omit<Globals, 'id' | 'url'>>(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`);
},
Expand Down
2 changes: 1 addition & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 019fd3c

Please sign in to comment.