@platformatic/config
Advanced tools
Comparing version 0.45.1 to 0.46.1
@@ -1,5 +0,6 @@ | ||
import { type InstanceOptions } from 'ajv' | ||
import { type InstanceOptions } from 'ajv' | ||
import { type FastifyPluginAsync } from 'fastify' | ||
import { FastifyError } from '@fastify/error' | ||
interface IEnv { | ||
[key: string]: string | ||
[key: string]: string | ||
} | ||
@@ -28,3 +29,3 @@ interface IConfigManagerOptions { | ||
export default class ConfigManager<T=object> { | ||
export default class ConfigManager<T = object> { | ||
constructor(opts: IConfigManagerOptions) | ||
@@ -40,4 +41,26 @@ current: T | ||
update(config: JsonMap): Promise<boolean | undefined> | ||
save(): Promise<boolean|undefined> | ||
save(): Promise<boolean | undefined> | ||
load(): Promise<string> | ||
} | ||
/** | ||
* All the errors thrown by the plugin. | ||
*/ | ||
export module errors { | ||
export const CannotFindEntityError: (entityName: string) => FastifyError | ||
export const ConfigurationDoesNotValidateAgainstSchemaError: FastifyError | ||
export const SourceMissingError: FastifyError | ||
export const InvalidPlaceholderError: (placeholder: string, suggestion: string) => FastifyError | ||
export const EnvVarMissingError: (envVarName: string) => FastifyError | ||
export const CannotParseConfigFileError: (error: string) => FastifyError | ||
export const ValidationErrors: (errors: string) => FastifyError | ||
export const AppMustBeAFunctionError: FastifyError | ||
export const SchemaMustBeDefinedError: FastifyError | ||
export const SchemaIdMustBeAStringError: FastifyError | ||
export const ConfigTypeMustBeAStringError: FastifyError | ||
export const AddAModulePropertyToTheConfigOrAddAKnownSchemaError: FastifyError | ||
export const VersionMismatchError: (currentVersion: string, requiredVersion: string) => FastifyError | ||
export const NoConfigFileFoundError: FastifyError | ||
} | ||
@@ -6,2 +6,3 @@ 'use strict' | ||
const { loadConfig, printConfigValidationErrors, printAndExitLoadConfigError } = require('./lib/load-config') | ||
const errors = require('./lib/errors') | ||
@@ -14,1 +15,2 @@ module.exports = ConfigManager | ||
module.exports.printAndExitLoadConfigError = printAndExitLoadConfigError | ||
module.exports.errors = errors |
@@ -7,2 +7,3 @@ 'use strict' | ||
const deepmerge = require('@fastify/deepmerge') | ||
const errors = require('./errors') | ||
@@ -44,3 +45,3 @@ async function loadConfig (minimistConfig, _args, app, overrides = {}) { | ||
if (!parsingResult) { | ||
const err = new Error('The configuration does not validate against the configuration schema') | ||
const err = new errors.ConfigurationDoesNotValidateAgainstSchemaError() | ||
err.validationErrors = configManager.validationErrors | ||
@@ -47,0 +48,0 @@ throw err |
@@ -12,2 +12,3 @@ 'use strict' | ||
const { isFileAccessible } = require('./utils') | ||
const errors = require('./errors') | ||
@@ -20,3 +21,3 @@ class ConfigManager extends EventEmitter { | ||
if (!opts.source) { | ||
throw new Error('Source missing.') | ||
throw new errors.SourceMissingError() | ||
} | ||
@@ -155,8 +156,8 @@ | ||
if (!err.key.match(/^PLT_/) && !this.envWhitelist.includes(err.key)) { | ||
throw new Error(`${err.key} is an invalid placeholder. All placeholders must be prefixed with PLT_.\nDid you mean PLT_${err.key}?`) | ||
throw new errors.InvalidPlaceholderError(err.key, err.key) | ||
} else { | ||
throw new Error(`${err.key} env variable is missing.`) | ||
throw new errors.EnvVarMissingError(err.key) | ||
} | ||
} | ||
const newerr = new Error(`Cannot parse config file. ${err.message}`) | ||
const newerr = new errors.CannotParseConfigFileError(err.message) | ||
newerr.cause = err | ||
@@ -207,3 +208,3 @@ throw newerr | ||
if (!validationResult) { | ||
throw new Error(this.validationErrors.map((err) => { | ||
throw new errors.ValidationErrors(this.validationErrors.map((err) => { | ||
return err.message | ||
@@ -210,0 +211,0 @@ }).join('\n')) |
@@ -9,2 +9,3 @@ 'use strict' | ||
const { getParser, analyze, upgrade } = require('@platformatic/metaconfig') | ||
const errors = require('./errors') | ||
@@ -30,15 +31,15 @@ class Store { | ||
if (typeof app !== 'function') { | ||
throw new TypeError('app must be a function') | ||
throw new errors.AppMustBeAFunctionError() | ||
} | ||
if (app.schema === undefined) { | ||
throw new TypeError('schema must be defined') | ||
throw new errors.SchemaMustBeDefinedError() | ||
} | ||
if (typeof app.schema.$id !== 'string' || app.schema.$id.length === 0) { | ||
throw new TypeError('schema.$id must be a string with length > 0') | ||
throw new errors.SchemaIdMustBeAStringError() | ||
} | ||
if (typeof app.configType !== 'string') { | ||
throw new TypeError('configType must be a string') | ||
throw new errors.ConfigTypeMustBeAStringError() | ||
} | ||
@@ -83,5 +84,5 @@ // TODO validate configType being unique | ||
if (attemptedToRunVersion === null) { | ||
throw new Error('Add a module property to the config or add a known $schema.') | ||
throw new errors.AddAModulePropertyToTheConfigOrAddAKnownSchemaError() | ||
} else { | ||
throw new Error(`Version mismatch. You are running Platformatic ${this.#currentVersion} but your app requires ${attemptedToRunVersion}`) | ||
throw new errors.VersionMismatchError(this.#currentVersion, attemptedToRunVersion) | ||
} | ||
@@ -160,3 +161,3 @@ } | ||
if (!found) { | ||
const err = new Error('no config file found') | ||
const err = new errors.NoConfigFileFoundError() | ||
err.filenames = filenames | ||
@@ -163,0 +164,0 @@ throw err |
{ | ||
"name": "@platformatic/config", | ||
"version": "0.45.1", | ||
"version": "0.46.1", | ||
"description": "Platformatic DB Config Manager", | ||
@@ -27,2 +27,3 @@ "main": "index.js", | ||
"@fastify/deepmerge": "^1.3.0", | ||
"@fastify/error": "^3.2.1", | ||
"ajv": "^8.12.0", | ||
@@ -34,4 +35,4 @@ "dotenv": "^16.1.4", | ||
"undici": "^5.22.1", | ||
"@platformatic/metaconfig": "0.45.1", | ||
"@platformatic/utils": "0.45.1" | ||
"@platformatic/metaconfig": "0.46.1", | ||
"@platformatic/utils": "0.46.1" | ||
}, | ||
@@ -38,0 +39,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76955
41
2207
10
+ Added@fastify/error@^3.2.1
+ Added@platformatic/metaconfig@0.46.1(transitive)
+ Added@platformatic/utils@0.46.1(transitive)
+ Addedleven@3.1.0(transitive)
- Removed@platformatic/metaconfig@0.45.1(transitive)
- Removed@platformatic/utils@0.45.1(transitive)
Updated@platformatic/utils@0.46.1