Skip to content

Commit

Permalink
feat(dedicated): added baseUrl for the Dedicated Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
eddienubes committed Mar 1, 2024
1 parent c6cca78 commit 06a6818
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/Sage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/SageConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const request = (
*/
export const dedicated = (
serverSource: ServerSource,
config: Omit<DeepPartial<SageConfig>, 'dedicated'>
config?: Omit<DeepPartial<SageConfig>, 'dedicated'>
): HttpCallable<Sage> => {
return request(serverSource, {
...config,
Expand Down
20 changes: 20 additions & 0 deletions test/dedicated.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
File renamed without changes.

0 comments on commit 06a6818

Please sign in to comment.