Skip to content

Commit 0a0d3f4

Browse files
committed
feat: adds 'Common Pages' functionality
re micheleriva#31
1 parent f0296a5 commit 0a0d3f4

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

packages/express-krabs/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Request, Response } from 'express';
22
import { parse } from 'url';
33
import * as path from 'path';
44
import { getTenantConfig } from '../utils/config';
5-
import { Config } from '../utils/config/config';
5+
import { Config, Options } from '../utils/config/config';
66
import findTenant from '../utils/tenants/findTenant';
77
import resolveRoutes from '../utils/routes/resolve';
88
import { currentEnv, environmentWarningMessage } from '../utils/env';
@@ -12,11 +12,12 @@ if (!currentEnv) {
1212
}
1313

1414
async function krabs(
15-
req: Request,
16-
res: Response,
17-
handle: any,
18-
app: any,
19-
config?: Config,
15+
req: Request,
16+
res: Response,
17+
handle: any,
18+
app: any,
19+
config?: Config,
20+
options?: Options
2021
): Promise<void> {
2122
const { tenants, enableVhostHeader } = config ?? (await getTenantConfig());
2223

@@ -44,8 +45,8 @@ async function krabs(
4445
try {
4546
const APIPath = pathname.replace(/^\/api\//, '');
4647
const { default: APIhandler } = require(path.join(
47-
process.cwd(),
48-
`pages/${tenant.name}/api/${APIPath}`,
48+
process.cwd(),
49+
`pages/${tenant.name}/api/${APIPath}`,
4950
));
5051
APIhandler(req, res);
5152
} catch (_) {
@@ -54,7 +55,7 @@ async function krabs(
5455
return;
5556
}
5657

57-
const route = resolveRoutes(tenant.name, String(pathname));
58+
const route = resolveRoutes(tenant.name, String(pathname), options?.commonPages);
5859

5960
if (route) {
6061
// @ts-ignore

packages/fastify-krabs/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FastifyRequest, FastifyReply } from 'fastify';
22
import { getTenantConfig } from '../utils/config';
3-
import { Config } from '../utils/config/config';
3+
import { Config, Options } from '../utils/config/config';
44
import findTenant from '../utils/tenants/findTenant';
55
import resolveRoutes from '../utils/routes/resolve';
66
import { currentEnv, environmentWarningMessage } from '../utils/env';
@@ -10,14 +10,16 @@ if (!currentEnv) {
1010
}
1111

1212
export default async function krabs(
13-
request: FastifyRequest,
14-
reply: FastifyReply,
15-
handle: any,
16-
app: any,
17-
config?: Config): Promise<void> {
13+
request: FastifyRequest,
14+
reply: FastifyReply,
15+
handle: any,
16+
app: any,
17+
config?: Config,
18+
options?: Options
19+
): Promise<void> {
1820

1921
const { tenants, enableVhostHeader } = config ?? (await getTenantConfig());
20-
22+
2123
const vhostHeader = enableVhostHeader && request.headers['x-vhost'] as string;
2224
const rawHostname = request.hostname;
2325
const pathName = request.url;
@@ -33,7 +35,7 @@ export default async function krabs(
3335
});
3436
}
3537

36-
const route = resolveRoutes(tenant?.name as string, pathName);
38+
const route = resolveRoutes(tenant?.name as string, pathName, options?.commonPages);
3739

3840
if (route) {
3941
// @ts-ignore
@@ -44,4 +46,4 @@ export default async function krabs(
4446

4547
handle(request, reply);
4648
return;
47-
};
49+
};

packages/utils/config/config.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export type Config = {
1616
enableVhostHeader: boolean;
1717
};
1818

19+
export type Options = {
20+
commonPages: boolean;
21+
};
22+
1923
export type ConfigEntry = (() => Config) | (() => Promise<Config>) | Config;
2024

2125
export default Config;

packages/utils/routes/resolve.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
export function resolveRoutes(tenantName: string, pathname: string): string {
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
export function resolveRoutes(
5+
tenantName: string,
6+
pathname: string,
7+
commonPages: boolean | undefined,
8+
): string {
9+
const pageExistsInTenant = fs.existsSync(
10+
path.join(process.cwd(), 'pages', tenantName, `${pathname}.js`),
11+
);
12+
13+
if (commonPages && !pageExistsInTenant && pathname !== '/') {
14+
return `${pathname}`;
15+
}
16+
217
return pathname === '/' ? `/${tenantName}` : `/${tenantName}${pathname}`;
318
}
419

0 commit comments

Comments
 (0)