mirror of
https://github.com/Unleash/unleash.git
synced 2025-05-31 01:16:01 +02:00
Feat/format base path (#828)
* chore: update changelog * feat: add formatBaseUri helper * feat: call formatBaseUri on server options * feat: call formatBaseUri on user options * fix: update test * fix: disable consistent return
This commit is contained in:
parent
410e6ca18c
commit
85a544bbd6
@ -35,6 +35,10 @@
|
||||
- fix: change default admin password
|
||||
- fix: add types for node-fetch
|
||||
|
||||
# 4.0.0-alpha.5
|
||||
|
||||
- chore: update frontend
|
||||
|
||||
# 4.0.0-alpha.4
|
||||
|
||||
- feat: add option for LOG_LEVEL (#803)
|
||||
|
@ -16,6 +16,7 @@ import {
|
||||
} from './types/option';
|
||||
import { getDefaultLogProvider, LogLevel, validateLogProvider } from './logger';
|
||||
import { defaultCustomAuthDenyAll } from './default-custom-auth-deny-all';
|
||||
import { formatBaseUri } from './util/format-base-uri';
|
||||
|
||||
const safeToUpper = (s: string) => (s ? s.toUpperCase() : s);
|
||||
|
||||
@ -82,7 +83,7 @@ const defaultServerOption: IServerOption = {
|
||||
pipe: undefined,
|
||||
host: process.env.HTTP_HOST,
|
||||
port: safeNumber(process.env.HTTP_PORT || process.env.PORT, 4242),
|
||||
baseUriPath: process.env.BASE_URI_PATH || '',
|
||||
baseUriPath: formatBaseUri(process.env.BASE_URI_PATH),
|
||||
unleashUrl: process.env.UNLEASH_URL || 'http://localhost:4242',
|
||||
serverMetrics: true,
|
||||
keepAliveTimeout: 60 * 1000,
|
||||
@ -136,8 +137,21 @@ const removeUndefinedKeys = (o: object): object =>
|
||||
return a;
|
||||
}, {});
|
||||
|
||||
const formatServerOptions = (
|
||||
serverOptions?: Partial<IServerOption>,
|
||||
): Partial<IServerOption> | undefined => {
|
||||
if (!serverOptions) return;
|
||||
|
||||
/* eslint-disable-next-line */
|
||||
return {
|
||||
...serverOptions,
|
||||
baseUriPath: formatBaseUri(serverOptions.baseUriPath),
|
||||
};
|
||||
};
|
||||
|
||||
export function createConfig(options: IUnleashOptions): IUnleashConfig {
|
||||
let extraDbOptions = {};
|
||||
|
||||
if (options.databaseUrl) {
|
||||
extraDbOptions = parse(options.databaseUrl);
|
||||
} else if (process.env.DATABASE_URL) {
|
||||
@ -161,7 +175,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
|
||||
|
||||
const server: IServerOption = mergeAll([
|
||||
defaultServerOption,
|
||||
options.server,
|
||||
formatServerOptions(options.server),
|
||||
]);
|
||||
|
||||
const versionCheck: IVersionOption = mergeAll([
|
||||
|
32
src/lib/util/format-base-uri.test.ts
Normal file
32
src/lib/util/format-base-uri.test.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { formatBaseUri } from './format-base-uri';
|
||||
import test from 'ava';
|
||||
|
||||
test('formatBaseUri returns the correct path when the path is the right format', t => {
|
||||
const result = formatBaseUri('/hosted');
|
||||
t.true(result === '/hosted');
|
||||
});
|
||||
|
||||
test('formatBaseUri returns the correct path when the path lacking initial slash', t => {
|
||||
const result = formatBaseUri('hosted');
|
||||
t.true(result === '/hosted');
|
||||
});
|
||||
|
||||
test('formatBaseUri returns the correct path when the path has both initial and trailing slash', t => {
|
||||
const result = formatBaseUri('/hosted/');
|
||||
t.true(result === '/hosted');
|
||||
});
|
||||
|
||||
test('formatBaseUri returns the correct path when the path has only trailing slash', t => {
|
||||
const result = formatBaseUri('hosted/');
|
||||
t.true(result === '/hosted');
|
||||
});
|
||||
|
||||
test('formatBaseUri returns empty string when called without input', t => {
|
||||
const result = formatBaseUri(undefined);
|
||||
t.true(result === '');
|
||||
});
|
||||
|
||||
test('formatBaseUri handles levels of paths', t => {
|
||||
const result = formatBaseUri('hosted/multi/path');
|
||||
t.true(result === '/hosted/multi/path');
|
||||
});
|
19
src/lib/util/format-base-uri.ts
Normal file
19
src/lib/util/format-base-uri.ts
Normal file
@ -0,0 +1,19 @@
|
||||
export const formatBaseUri = (input: string): string => {
|
||||
if (!input) return '';
|
||||
const firstChar = input[0];
|
||||
const lastChar = input[input.length - 1];
|
||||
|
||||
if (firstChar === '/' && lastChar === '/') {
|
||||
return input.substr(0, input.length - 1);
|
||||
}
|
||||
|
||||
if (firstChar !== '/' && lastChar === '/') {
|
||||
return `/${input.substr(0, input.length - 1)}`;
|
||||
}
|
||||
|
||||
if (firstChar !== '/') {
|
||||
return `/${input}`;
|
||||
}
|
||||
|
||||
return input;
|
||||
};
|
@ -67,7 +67,7 @@ test('should allow setting pool size', t => {
|
||||
});
|
||||
|
||||
test('Can set baseUriPath', t => {
|
||||
const baseUriPath = 'some';
|
||||
const baseUriPath = '/some';
|
||||
const config = createConfig({ server: { baseUriPath } });
|
||||
t.is(config.server.baseUriPath, baseUriPath);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user