diff --git a/package.json b/package.json index 39e0be3..4b9aff0 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "build:dev": "tsc -p tsconfig.json", "build:prod": "rm -rf ./build && pnpm rollup -c && tsc -p tsconfig.build.json && pnpm tsc-alias -p tsconfig.build.json", "lint:check": "eslint --max-warnings=50 .", - "lint:fix": "eslint --max-warnings=50 --fix ." + "lint:fix": "eslint --max-warnings=50 --fix .", + "valid": "pnpm lint:fix && pnpm build:dev" }, "keywords": [ "http", diff --git a/src/Sage.ts b/src/Sage.ts index a24fa92..9c5b0b3 100644 --- a/src/Sage.ts +++ b/src/Sage.ts @@ -273,6 +273,10 @@ export class Sage { // Wait for all deferred promises to resolve await Promise.all(this.deferredPromises); + if (this.config.baseUrl) { + this.request.path = `${this.config.baseUrl}${this.request.path}`; + } + try { const res = await this.client.request({ method: this.request.method as HttpMethod, diff --git a/src/SageConfig.ts b/src/SageConfig.ts index 21d55d3..24316de 100644 --- a/src/SageConfig.ts +++ b/src/SageConfig.ts @@ -21,4 +21,14 @@ export interface SageConfig { * @default true */ keepAlive: boolean; + + /** + * Base URL for the server. + * Prefix for all requests. + * Useful for common API paths like /api/v1. + * Please also remember that all HTTP paths have to start with a slash. + * E.g. your request URL may be /users/:userId and the base URL is /api/v1, so the final URL will be /api/v1/users/:userId. + * @default null + */ + baseUrl: string | null; } diff --git a/src/constants.ts b/src/constants.ts index ab9f98b..0736aee 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -80,7 +80,8 @@ export type HttpStatusText = export const SAGE_DEFAULT_CONFIG: SageConfig = { dedicated: false, port: 0, - keepAlive: true + keepAlive: true, + baseUrl: null }; export const MIME_TYPES = { diff --git a/src/index.ts b/src/index.ts index 8880d85..ff6bb70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -86,7 +86,7 @@ export const request = ( */ export const dedicated = ( serverSource: ServerSource, - config: Omit, 'dedicated'> + config?: Omit, 'dedicated'> ): HttpCallable => { return request(serverSource, { ...config, diff --git a/test/dedicated.spec.ts b/test/dedicated.spec.ts new file mode 100644 index 0000000..01347a8 --- /dev/null +++ b/test/dedicated.spec.ts @@ -0,0 +1,20 @@ +import { getExpressApp } from './utils.js'; +import { dedicated } from '../src/index.js'; + +const app = getExpressApp(); + +describe('Generic Cases Related to Dedicated Mode', () => { + describe('baseUrl', () => { + it('should append baseUrl to the beginning of the path request', async () => { + // literally calls the endpoint /redirect + const request = dedicated(app, { + baseUrl: '/redirect' + }); + + const res = await request.get('/'); + + expect(res.statusCode).toBe(301); + expect(res.redirect).toBe(true); + }); + }); +}); diff --git a/test/index.spec.ts b/test/request.spec.ts similarity index 100% rename from test/index.spec.ts rename to test/request.spec.ts