From da86b659f32972be0d3f50fc0e191ce68b1bcbcd Mon Sep 17 00:00:00 2001 From: CptSchnitz <12687466+CptSchnitz@users.noreply.github.com> Date: Sun, 13 Oct 2024 11:26:56 +0300 Subject: [PATCH 1/2] feat: changed all options except schema to be optional --- README.md | 34 ++++++---------------------------- src/constants.ts | 1 + src/options.ts | 12 +++++++++--- src/types.ts | 2 +- tests/options.spec.ts | 3 +-- tests/schemas.spec.ts | 2 +- 6 files changed, 19 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index c32a13c..701e2fc 100644 --- a/README.md +++ b/README.md @@ -76,19 +76,22 @@ This package allows you to configure various options for loading and managing co ### `configName` - **Type**: `string` -- **Optional**: `false` +- **Optional**: `true` +- - **Description**: The name of the remote configuration. - **Environment Variable**: `CONFIG_NAME` ### `version` - **Type**: `'latest' | number` -- **Optional**: `false` +- **Optional**: `true` +- **Default**: `latest` - **Description**: The version of the remote configuration. It can be either `'latest'` or a number. - **Environment Variable**: `CONFIG_VERSION` ### `configServerUrl` - **Type**: `string` -- **Optional**: `false` +- **Optional**: `true` +- **Default**: `http://localhost:8080` - **Description**: The URL of the configuration server. - **Environment Variable**: `CONFIG_SERVER_URL` @@ -111,31 +114,6 @@ This package allows you to configure various options for loading and managing co - **Default**: `./config` - **Description**: The path to the local configuration folder. -## JSON Schema - -The options are validated against the following JSON schema: - -```json -{ -  "required": ["configName", "configServerUrl", "version"], -  "additionalProperties": false, -  "type": "object", -  "properties": { -    "configName": { "type": "string" }, -    "version": { -      "oneOf": [ -        { "type": "string", "const": "latest" }, -        { "type": "integer", "minimum": 1 } -      ] -    }, -    "configServerUrl": { "type": "string" }, -    "offlineMode": { "type": "boolean", "nullable": true }, -    "ignoreServerIsOlderVersionError": { "type": "boolean", "nullable": true }, -    "localConfigPath": { "type": "string", "default": "./config" } -  } -} -``` - ## Environment Variable Configuration The following environment variables can be used to configure the options: diff --git a/src/constants.ts b/src/constants.ts index 23aa374..e762322 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -3,6 +3,7 @@ import { readPackageJsonSync } from '@map-colonies/read-pkg'; const schemasPackagePathBuildPath = require.resolve('@map-colonies/schemas').substring(0, require.resolve('@map-colonies/schemas').indexOf('build')); export const LOCAL_SCHEMAS_PACKAGE_VERSION = readPackageJsonSync(path.join(schemasPackagePathBuildPath, 'package.json')).version as string; +export const PACKAGE_NAME = readPackageJsonSync('package.json').name ?? 'package-name-not-defined'; export const SCHEMA_DOMAIN = 'https://mapcolonies.com/'; diff --git a/src/options.ts b/src/options.ts index a1ec48b..5b1796c 100644 --- a/src/options.ts +++ b/src/options.ts @@ -3,9 +3,16 @@ import { BaseOptions, optionsSchema } from './types'; import { ajvOptionsValidator, validate } from './validator'; import { createDebug } from './utils/debug'; import { createConfigError } from './errors'; +import { PACKAGE_NAME } from './constants'; const debug = createDebug('options'); +const defaultOptions: BaseOptions = { + configName: PACKAGE_NAME, + configServerUrl: 'http://localhost:8080', + version: 'latest', +}; + const envOptions: Partial> = { configName: process.env.CONFIG_NAME, configServerUrl: process.env.CONFIG_SERVER_URL, @@ -24,9 +31,8 @@ for (const key in envOptions) { let baseOptions: BaseOptions | undefined = undefined; export function initializeOptions(options: Partial): BaseOptions { - debug('initializing options with %j and env %j', options, envOptions); - const mergedOptions = deepmerge(options, envOptions); - + debug('initializing options with default options: %j function input: %j and environment variables: %j', defaultOptions, options, envOptions); + const mergedOptions = deepmerge.all([defaultOptions, options, envOptions]); debug('merged options: %j', mergedOptions); const [errors, validatedOptions] = validate(ajvOptionsValidator, optionsSchema, mergedOptions); diff --git a/src/types.ts b/src/types.ts index a2d376f..c1d28b9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -72,7 +72,7 @@ export interface BaseOptions { * Represents the options for configuration. */ export type ConfigOptions = Prettify< - BaseOptions & { + Partial & { /** * The schema of the configuration object. */ diff --git a/tests/options.spec.ts b/tests/options.spec.ts index fd797f9..d8ca644 100644 --- a/tests/options.spec.ts +++ b/tests/options.spec.ts @@ -60,13 +60,12 @@ describe('options', () => { }); it('should throw an error if options are invalid', () => { - process.env.CONFIG_VERSION = 'latest'; process.env.CONFIG_SERVER_URL = 'http://localhost:8080'; const { initializeOptions } = require('../src/options'); expect(() => { - initializeOptions({}); + initializeOptions({ version: 'avi' }); }).toThrow(); }); diff --git a/tests/schemas.spec.ts b/tests/schemas.spec.ts index 3320515..19c0253 100644 --- a/tests/schemas.spec.ts +++ b/tests/schemas.spec.ts @@ -51,7 +51,7 @@ describe('schemas', () => { const dereferencedSchema = await loadSchema(schema); - expect(dereferencedSchema).toHaveProperty('allOf[0].$id', 'https://mapcolonies.com/common/db/partial/v1'); + expect(dereferencedSchema).toHaveProperty('allOf[0].title', 'commonDbPartialV1'); }); it('should throw an error if the schema is not found', async () => { From c9e096407134c3e91c29dd2b92210f1ffd08b439 Mon Sep 17 00:00:00 2001 From: CptSchnitz <12687466+CptSchnitz@users.noreply.github.com> Date: Sun, 13 Oct 2024 12:03:51 +0300 Subject: [PATCH 2/2] fix: eslint errors --- tests/config.spec.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/config.spec.ts b/tests/config.spec.ts index 8f50c67..2692734 100644 --- a/tests/config.spec.ts +++ b/tests/config.spec.ts @@ -1,5 +1,5 @@ import { Interceptable, MockAgent, setGlobalDispatcher } from 'undici'; -import { commonDbPartialV1, commonS3PartialV2 } from '@map-colonies/schemas'; +import { commonDbPartialV1, commonS3PartialV1 } from '@map-colonies/schemas'; import { StatusCodes } from 'http-status-codes'; import { config } from '../src/config'; @@ -138,13 +138,14 @@ describe('config', () => { jest.resetModules(); process.env.S3_ACCESS_KEY = 'access'; process.env.S3_SECRET_KEY = 'secret'; + process.env.S3_ENDPOINT = 'http://localhost:9000'; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires const { config } = require('../src/config') as typeof import('../src/config'); const configInstance = await config({ configName: 'name', version: 1, - schema: commonS3PartialV2, + schema: commonS3PartialV1, configServerUrl: URL, localConfigPath: './tests/config', offlineMode: true, @@ -236,7 +237,7 @@ describe('config', () => { const promise = config({ configName: 'name', version: 1, - schema: commonS3PartialV2, + schema: commonS3PartialV1, configServerUrl: URL, localConfigPath: './tests/config', });