Skip to content

Commit

Permalink
Refactor import paths and add makeAPIRoute function
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Dec 13, 2024
1 parent 894507c commit 67bf513
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 180 deletions.
8 changes: 3 additions & 5 deletions packages/studiocms_auth/src/utils/routeBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { integrationLogger } from '@matthiesenxyz/integration-utils/astroUtils';
import { removeLeadingTrailingSlashes } from '@studiocms/core/lib';
import { makeAPIRoute, removeLeadingTrailingSlashes } from '@studiocms/core/lib';
import { DashboardStrings } from '@studiocms/core/strings';
import { defineUtility } from 'astro-integration-kit';
import type { StudioCMSAuthOptions } from '../schema';

export const makeStudioCMSAuthAPIRoute = (route: string) => {
return `studiocms_api/auth/${route}`;
};
const apiRoute = makeAPIRoute('auth');

export const makeDashboardRoute = (route: string, options: StudioCMSAuthOptions) => {
// Get the dashboard route override from the options
Expand Down Expand Up @@ -80,7 +78,7 @@ export const injectAuthAPIRoutes = defineUtility('astro:config:setup')(
}

injectRoute({
pattern: makeStudioCMSAuthAPIRoute(pattern),
pattern: apiRoute(pattern),
entrypoint,
prerender: false,
});
Expand Down
1 change: 1 addition & 0 deletions packages/studiocms_core/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './jsonUtils';
export * from './pathGenerators';
export * from './removeLeadingTrailingSlashes';
export * from './urlGen';
export * from './makeAPIRoute';
5 changes: 5 additions & 0 deletions packages/studiocms_core/src/lib/makeAPIRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function makeAPIRoute(path: 'auth' | 'dashboard') {
return function api(route: string) {
return `studiocms_api/${path}/${route}`;
};
}
63 changes: 35 additions & 28 deletions packages/studiocms_core/src/lib/routeMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Config from 'studiocms:config';
// import { dashboardPageLinks } from 'virtual:studiocms/pluginSystem';
import type { SideBarLink } from '../schemas';
import { makeAPIRoute } from './makeAPIRoute';
import urlGenFactory from './urlGen';

const {
Expand All @@ -27,48 +28,47 @@ export function makeDashboardRoute(route?: string | undefined): string {
return urlGenFactory(true, route, dashboardRouteOverride);
}

export function makeStudioCMSAPIRoute(route: string): string {
return urlGenFactory(false, `studiocms_api/${route}`);
}
const authAPIRoute = makeAPIRoute('auth');
const dashboardAPIRoute = makeAPIRoute('dashboard');

export const StudioCMSRoutes = {
mainLinks: {
baseSiteURL: makeNonDashboardRoute(),
dashboardIndex: makeDashboardRoute(),
userProfile: makeDashboardRoute('profile/'),
pageNew: makeDashboardRoute('new/page/'),
pageEdit: makeDashboardRoute('page-list/'),
contentManagement: makeDashboardRoute('content-management/'),
createPage: makeDashboardRoute('create-page/'),
siteConfiguration: makeDashboardRoute('configuration/'),
configurationAdmins: makeDashboardRoute('configuration/admins/'),
userManagement: makeDashboardRoute('user-management/'),
},
authLinks: {
loginURL: makeDashboardRoute('login'),
logoutURL: makeDashboardRoute('logout'),
signupURL: makeDashboardRoute('signup'),
loginAPI: makeStudioCMSAPIRoute('auth/login'), // /studiocms_api/auth/login
logoutAPI: makeStudioCMSAPIRoute('auth/logout'), // /studiocms_api/auth/logout
registerAPI: makeStudioCMSAPIRoute('auth/register'), // /studiocms_api/auth/register
githubIndex: makeStudioCMSAPIRoute('auth/github'), // /studiocms_api/auth/github
githubCallback: makeStudioCMSAPIRoute('auth/github/callback'), // /studiocms_api/auth/github/callback
discordIndex: makeStudioCMSAPIRoute('auth/discord'), // /studiocms_api/auth/discord
discordCallback: makeStudioCMSAPIRoute('auth/discord/callback'), // /studiocms_api/auth/discord/callback
googleIndex: makeStudioCMSAPIRoute('auth/google'), // /studiocms_api/auth/google
googleCallback: makeStudioCMSAPIRoute('auth/google/callback'), // /studiocms_api/auth/google/callback
auth0Index: makeStudioCMSAPIRoute('auth/auth0'), // /studiocms_api/auth/auth0
auth0Callback: makeStudioCMSAPIRoute('auth/auth0/callback'), // /studiocms_api/auth/auth0/callback
loginAPI: authAPIRoute('login'),
logoutAPI: authAPIRoute('logout'),
registerAPI: authAPIRoute('register'),
githubIndex: authAPIRoute('github'),
githubCallback: authAPIRoute('github/callback'),
discordIndex: authAPIRoute('discord'),
discordCallback: authAPIRoute('discord/callback'),
googleIndex: authAPIRoute('google'),
googleCallback: authAPIRoute('google/callback'),
auth0Index: authAPIRoute('auth0'),
auth0Callback: authAPIRoute('auth0/callback'),
},
endpointLinks: {
partials: {
livePreviewBox: makeStudioCMSAPIRoute('dashboard/liverender'), // /studiocms_api/dashboard/liverender
livePreviewBox: dashboardAPIRoute('liverender'),
},
config: {
siteConfig: makeStudioCMSAPIRoute('dashboard/config/site'), // /studiocms_api/dashboard/config/site
adminConfig: makeStudioCMSAPIRoute('dashboard/config/admin'), // /studiocms_api/dashboard/config/admin
siteConfig: dashboardAPIRoute('config/site'),
adminConfig: dashboardAPIRoute('config/admin'),
},
pages: {
createPages: makeStudioCMSAPIRoute('dashboard/pages/create'), // /studiocms_api/dashboard/pages/create
editPages: makeStudioCMSAPIRoute('dashboard/pages/edit'), // /studiocms_api/dashboard/pages/edit
deletePages: makeStudioCMSAPIRoute('dashboard/pages/delete'), // /studiocms_api/dashboard/pages/delete
createPages: dashboardAPIRoute('pages/create'),
editPages: dashboardAPIRoute('pages/edit'),
deletePages: dashboardAPIRoute('pages/delete'),
},
},
};
Expand Down Expand Up @@ -97,16 +97,16 @@ const defaultDashboardPageLinks: SideBarLink[] = [
icon: 'user',
},
{
id: 'new-page',
href: StudioCMSRoutes.mainLinks.pageNew,
text: 'Create New Page',
id: 'content-management',
href: StudioCMSRoutes.mainLinks.contentManagement,
text: 'Content Management',
minPermissionLevel: 'editor',
icon: 'plus',
},
{
id: 'edit-pages',
href: StudioCMSRoutes.mainLinks.pageEdit,
text: 'Edit Pages',
href: StudioCMSRoutes.mainLinks.createPage,
text: 'Create Page',
minPermissionLevel: 'editor',
icon: 'pencil-square',
},
Expand All @@ -117,6 +117,13 @@ const defaultDashboardPageLinks: SideBarLink[] = [
minPermissionLevel: 'admin',
icon: 'cog-6-tooth',
},
{
id: 'user-management',
href: StudioCMSRoutes.mainLinks.userManagement,
text: 'User Management',
minPermissionLevel: 'admin',
icon: 'cog-6-tooth',
},
];

// Side bar links map
Expand Down
4 changes: 0 additions & 4 deletions packages/studiocms_core/src/stubs/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ dtsFile.addModule('studiocms:lib', {
name: 'makeDashboardRoute',
typeDef: `typeof import('${resolve('../lib/routeMap.ts')}').makeDashboardRoute`,
},
{
name: 'makeStudioCMSAPIRoute',
typeDef: `typeof import('${resolve('../lib/routeMap.ts')}').makeStudioCMSAPIRoute`,
},
{
name: 'StudioCMSRoutes',
typeDef: `typeof import('${resolve('../lib/routeMap.ts')}').StudioCMSRoutes`,
Expand Down
73 changes: 28 additions & 45 deletions packages/studiocms_dashboard/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default defineIntegration({
const { resolve } = createResolver(import.meta.url);

// Declaration for Web Vitals DTS File
let WEBVITALSDTSFILE: InjectedType;
let WebVitalsDtsFile: InjectedType;

return {
hooks: {
Expand Down Expand Up @@ -119,63 +119,46 @@ export default defineIntegration({
pattern: '/test',
entrypoint: resolve('./routes/test.astro'),
},
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: '/content-management',
// entrypoint: resolve('./routes/content-management.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: '/create-page',
// entrypoint: resolve('./routes/create-page.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: '/profile',
// entrypoint: resolve('./routes/profile.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: '/configuration',
// entrypoint: resolve('./routes/configuration.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: '/user-management',
// entrypoint: resolve('./routes/user-management.astro'),
// },
],
});

// // OLD ROUTES - TO BE REMOVED
// injectRouteArray(params, {
// options,
// routes: [
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: 'profile/',
// entrypoint: resolve('./routes/profile.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: 'configuration',
// entrypoint: resolve('./routes/configuration/index.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: 'configuration/admins',
// entrypoint: resolve('./routes/configuration/admins.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: 'new/page',
// entrypoint: resolve('./routes/create-page.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: 'page-list',
// entrypoint: resolve('./routes/page-list.astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: 'edit/pages/[...id]',
// entrypoint: resolve('./routes/edit-pages/[...id].astro'),
// },
// {
// enabled: options.dashboardConfig.dashboardEnabled && !options.dbStartPage,
// pattern: 'delete/pages/[...id]',
// entrypoint: resolve('./routes/delete-pages/[...id].astro'),
// },
// ],
// });

// Check for `@astrojs/web-vitals` Integration
const { webVitalDtsFile } = checkForWebVitals(params, { name, verbose });

// Set the Web Vitals DTS File
WEBVITALSDTSFILE = webVitalDtsFile;
WebVitalsDtsFile = webVitalDtsFile;

// Log that the setup is complete
integrationLogger({ logger, logLevel: 'info', verbose }, DashboardStrings.SetupComplete);
},
'astro:config:done': async ({ injectTypes }) => {
// Inject the Web Vitals DTS File
injectTypes(WEBVITALSDTSFILE);
injectTypes(WebVitalsDtsFile);
},
'astro:server:start': async ({ logger }) => {
// Display Console Message if dbStartPage(First Time DB Initialization) is enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const t = useTranslations(lang, '@studiocms/dashboard:index');
// const webVitals = await getWebVitals();
---

<LoginChecker server:defer requiredPermission='editor' />
<LoginChecker server:defer requiredPermission='owner' />

<Layout
sideBarActiveItemID="dashboard"
Expand Down
12 changes: 6 additions & 6 deletions packages/studiocms_dashboard/src/routes/content-management.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { useTranslations } from 'studiocms:i18n';
import studioCMS_SDK from 'studiocms:sdk';
import { Layout } from '../components';
import PageHeader from '../components/DashboardPageHeader.astro';
import LoginChecker from '../components/islands/LoginChecker.astro';
import TestIsland from '../components/islands/TestIsland.astro';
import { makePageTitle } from '../utils/makePageTitle';
const dbConfig = (await studioCMS_SDK.GET.database.config()) || {
Expand All @@ -14,9 +14,6 @@ const dbConfig = (await studioCMS_SDK.GET.database.config()) || {
const lang = 'en-us';
const t = useTranslations(lang, '@studiocms/dashboard:index');
// Get the web vitals
// const webVitals = await getWebVitals();
---

<LoginChecker server:defer requiredPermission='editor' />
Expand All @@ -26,6 +23,9 @@ const t = useTranslations(lang, '@studiocms/dashboard:index');
title={makePageTitle(t('title'), dbConfig)}
description={dbConfig.description}
>
<PageHeader />
<span>{t('sub-header')}</span>
Sus

<div>
<TestIsland server:defer />
</div>
</Layout>
2 changes: 1 addition & 1 deletion packages/studiocms_dashboard/src/routes/profile.astro
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const t = useTranslations(lang, '@studiocms/dashboard:index');
// const webVitals = await getWebVitals();
---

<LoginChecker server:defer requiredPermission='editor' />
<LoginChecker server:defer requiredPermission="visitor" />

<Layout
sideBarActiveItemID="dashboard"
Expand Down
Loading

0 comments on commit 67bf513

Please sign in to comment.