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

feat: support global prefix #1326

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions lib/interfaces/serve-static-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export interface ServeStaticModuleOptions {
* Path to render static app (concatenated with the `serveRoot` value). Default: * (wildcard - all paths). Note: `RegExp` is not supported by the `@nestjs/platform-fastify`.
*/
renderPath?: string | RegExp;
/**
* If `true`, static app will be prefixed by the global prefix set through `setGlobalPrefix()`.
* Default: `false`.
* @see https://docs.nestjs.com/faq/global-prefix
*/
useGlobalPrefix?: boolean;
/**
* Root path under which static app will be served. Default: ""
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/loaders/abstract.loader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Injectable } from '@nestjs/common';
import { AbstractHttpAdapter } from '@nestjs/core';
import { AbstractHttpAdapter, ApplicationConfig } from '@nestjs/core';
import { join } from 'path';
import { ServeStaticModuleOptions } from '../interfaces/serve-static-options.interface';

@Injectable()
export abstract class AbstractLoader {
public abstract register(
httpAdapter: AbstractHttpAdapter,
config: ApplicationConfig,
options: ServeStaticModuleOptions[]
);

Expand Down
14 changes: 12 additions & 2 deletions lib/loaders/express.loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import * as fs from 'fs';
import { AbstractHttpAdapter } from '@nestjs/core';
import { AbstractHttpAdapter, ApplicationConfig } from '@nestjs/core';
import { ServeStaticModuleOptions } from '../interfaces/serve-static-options.interface';
import {
DEFAULT_RENDER_PATH,
Expand All @@ -10,18 +10,21 @@ import {
import { isRouteExcluded } from '../utils/is-route-excluded.util';
import { validatePath } from '../utils/validate-path.util';
import { AbstractLoader } from './abstract.loader';
import { validateGlobalPrefix } from '../utils/validate-global-prefix.util';

@Injectable()
export class ExpressLoader extends AbstractLoader {
public register(
httpAdapter: AbstractHttpAdapter,
config: ApplicationConfig,
optionsArr: ServeStaticModuleOptions[]
) {
const app = httpAdapter.getInstance();
const globalPrefix = config.getGlobalPrefix();
const express = loadPackage('express', 'ServeStaticModule', () =>
require('express')
);
optionsArr.forEach(options => {
optionsArr.forEach((options) => {
options.renderPath = options.renderPath || DEFAULT_RENDER_PATH;
const clientPath = options.rootPath || DEFAULT_ROOT_PATH;
const indexFilePath = this.getIndexFilePath(clientPath);
Expand All @@ -41,6 +44,13 @@ export class ExpressLoader extends AbstractLoader {
}
};

if (
globalPrefix &&
options.useGlobalPrefix &&
validateGlobalPrefix(globalPrefix)
) {
options.serveRoot = `/${globalPrefix}${options.serveRoot || ''}`;
}
if (options.serveRoot) {
app.use(
options.serveRoot,
Expand Down
13 changes: 12 additions & 1 deletion lib/loaders/fastify.loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { AbstractHttpAdapter } from '@nestjs/core';
import { AbstractHttpAdapter, ApplicationConfig } from '@nestjs/core';
import * as fs from 'fs';
import { ServeStaticModuleOptions } from '../interfaces/serve-static-options.interface';
import {
Expand All @@ -9,14 +9,17 @@ import {
} from '../serve-static.constants';
import { validatePath } from '../utils/validate-path.util';
import { AbstractLoader } from './abstract.loader';
import { validateGlobalPrefix } from '../utils/validate-global-prefix.util';

@Injectable()
export class FastifyLoader extends AbstractLoader {
public register(
httpAdapter: AbstractHttpAdapter,
config: ApplicationConfig,
optionsArr: ServeStaticModuleOptions[]
) {
const app = httpAdapter.getInstance();
const globalPrefix = config.getGlobalPrefix();
const fastifyStatic = loadPackage(
'@fastify/static',
'ServeStaticModule',
Expand All @@ -29,6 +32,14 @@ export class FastifyLoader extends AbstractLoader {
const clientPath = options.rootPath || DEFAULT_ROOT_PATH;
const indexFilePath = this.getIndexFilePath(clientPath);

if (
globalPrefix &&
options.useGlobalPrefix &&
validateGlobalPrefix(globalPrefix)
) {
options.serveRoot = `/${globalPrefix}${options.serveRoot || ''}`;
}

if (options.serveRoot) {
app.register(fastifyStatic, {
root: clientPath,
Expand Down
3 changes: 2 additions & 1 deletion lib/loaders/noop.loader.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */
import { Injectable } from '@nestjs/common';
import { AbstractHttpAdapter } from '@nestjs/core';
import { AbstractHttpAdapter, ApplicationConfig } from '@nestjs/core';
import { ServeStaticModuleOptions } from '../interfaces/serve-static-options.interface';
import { AbstractLoader } from './abstract.loader';

@Injectable()
export class NoopLoader extends AbstractLoader {
public register(
httpAdapter: AbstractHttpAdapter,
config: ApplicationConfig,
options: ServeStaticModuleOptions[]
) {}
}
5 changes: 3 additions & 2 deletions lib/serve-static.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
OnModuleInit,
Provider
} from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
import {
ServeStaticModuleAsyncOptions,
ServeStaticModuleOptions,
Expand All @@ -23,6 +23,7 @@ export class ServeStaticModule implements OnModuleInit {
@Inject(SERVE_STATIC_MODULE_OPTIONS)
private readonly ngOptions: ServeStaticModuleOptions[],
private readonly loader: AbstractLoader,
private readonly config: ApplicationConfig,
private readonly httpAdapterHost: HttpAdapterHost
) {}

Expand Down Expand Up @@ -87,6 +88,6 @@ export class ServeStaticModule implements OnModuleInit {

public async onModuleInit() {
const httpAdapter = this.httpAdapterHost.httpAdapter;
this.loader.register(httpAdapter, this.ngOptions);
this.loader.register(httpAdapter, this.config, this.ngOptions);
}
}
7 changes: 7 additions & 0 deletions lib/utils/validate-global-prefix.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Validates a global prefix string. This will not allow string with only slashes.
* @param {string} globalPrefix - The global prefix to validate.
* @returns {boolean} Returns true if the prefix is valid, false otherwise.
*/
export const validateGlobalPrefix = (globalPrefix: string): boolean =>
!globalPrefix.match(/^(\/?)$/);