Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(directus module) add error handling #97

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
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
Loading