Skip to content

Commit

Permalink
Merge pull request #417 from mocks-server/release
Browse files Browse the repository at this point in the history
Release v3.11.0
  • Loading branch information
javierbrea authored Aug 25, 2022
2 parents 48d00d2 + 7f1d0bf commit eeb9b1e
Show file tree
Hide file tree
Showing 120 changed files with 4,539 additions and 85 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
/mocks/*/build
/mocks/admin-api-client-data-provider-e2e-vanilla-app/public/js
/packages/*/dist
/packages/*/dist-tsc
/packages/*/coverage
/test/*/dist
/test/core-e2e/src/fixtures
/test/core-e2e-legacy/src/fixtures
/test/main-e2e/src/fixtures
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
branches:
- master
- release
- feat-384-openapi
pull_request:
jobs:
get-affected:
Expand Down
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ To check out docs, visit [www.mocks-server.org][website-url].
| --- | --- | --- |
| [main] | [![main-status]][main-package] | Main distribution. It includes all plugins preinstalled |
| [core] | [![core-status]][core-package] | Pluggable core. It can be used programmatically also |
| [plugin-proxy] | [![plugin-proxy-status]][plugin-proxy-package] | Plugin providing Proxy route handler |
| [plugin-inquirer-cli] | [![plugin-inquirer-cli-status]][plugin-inquirer-cli-package] | Plugin providing an administration interactive CLI |
| [plugin-admin-api] | [![plugin-admin-api-status]][plugin-admin-api-package] | Plugin providing an administration REST API |
| [plugin-inquirer-cli] | [![plugin-inquirer-cli-status]][plugin-inquirer-cli-package] | Plugin providing an administration interactive CLI |
| [plugin-openapi] | [![plugin-openapi-status]][plugin-openapi-package] | Plugin allowing to create routes and collections from OpenApi definitions |
| [plugin-proxy] | [![plugin-proxy-status]][plugin-proxy-package] | Plugin providing Proxy route handler |
| [admin-api-client] | [![admin-api-client-status]][admin-api-client-package] | API client for [plugin-admin-api] |
| [admin-api-client-data-provider] | [![admin-api-client-data-provider-status]][admin-api-client-data-provider-package] | API client for [plugin-admin-api] built using [data-provider] |
| [admin-api-paths] | [![admin-api-paths-status]][admin-api-paths-package] | Definition of [plugin-admin-api] routes |
Expand All @@ -67,17 +68,21 @@ Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of
[core-status]: https://img.shields.io/npm/v/@mocks-server/core.svg
[core-package]: https://npmjs.com/package/@mocks-server/core

[plugin-proxy]: https://github.com/mocks-server/main/tree/master/packages/plugin-proxy
[plugin-proxy-status]: https://img.shields.io/npm/v/@mocks-server/plugin-proxy.svg
[plugin-proxy-package]: https://npmjs.com/package/@mocks-server/plugin-proxy
[plugin-admin-api]: https://github.com/mocks-server/main/tree/master/packages/plugin-admin-api
[plugin-admin-api-status]: https://img.shields.io/npm/v/@mocks-server/plugin-admin-api.svg
[plugin-admin-api-package]: https://npmjs.com/package/@mocks-server/plugin-admin-api

[plugin-inquirer-cli]: https://github.com/mocks-server/main/tree/master/packages/plugin-inquirer-cli
[plugin-inquirer-cli-status]: https://img.shields.io/npm/v/@mocks-server/plugin-inquirer-cli.svg
[plugin-inquirer-cli-package]: https://npmjs.com/package/@mocks-server/plugin-inquirer-cli

[plugin-admin-api]: https://github.com/mocks-server/main/tree/master/packages/plugin-admin-api
[plugin-admin-api-status]: https://img.shields.io/npm/v/@mocks-server/plugin-admin-api.svg
[plugin-admin-api-package]: https://npmjs.com/package/@mocks-server/plugin-admin-api
[plugin-openapi]: https://github.com/mocks-server/main/tree/master/packages/plugin-openapi
[plugin-openapi-status]: https://img.shields.io/npm/v/@mocks-server/plugin-openapi.svg
[plugin-openapi-package]: https://npmjs.com/package/@mocks-server/plugin-openapi

[plugin-proxy]: https://github.com/mocks-server/main/tree/master/packages/plugin-proxy
[plugin-proxy-status]: https://img.shields.io/npm/v/@mocks-server/plugin-proxy.svg
[plugin-proxy-package]: https://npmjs.com/package/@mocks-server/plugin-proxy

[admin-api-client]: https://github.com/mocks-server/main/tree/master/packages/admin-api-client
[admin-api-client-status]: https://img.shields.io/npm/v/@mocks-server/admin-api-client.svg
Expand Down
7 changes: 7 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ coverage:
plugin-inquirer-cli:
flags:
- plugin-inquirer-cli
plugin-openapi:
flags:
- plugin-openapi
plugin-proxy:
flags:
- plugin-proxy
Expand Down Expand Up @@ -95,6 +98,10 @@ flags:
paths:
- packages/plugin-inquirer-cli/**
carryforward: true
plugin-openapi:
paths:
- packages/plugin-openapi/**
carryforward: true
plugin-proxy:
paths:
- packages/plugin-proxy/**
Expand Down
5 changes: 5 additions & 0 deletions packages/config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
### Removed

## [1.3.0] - 2022-08-25

### Added
- feat: Add 'nullable' property to option. Nullable types are 'string', 'number' and 'boolean'

## [1.2.1] - 2022-08-11

### Fixed
Expand Down
19 changes: 19 additions & 0 deletions packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Options can be added to a namespace, or to the root config object. Both `config`

Options can be of one of next types: `string`, `boolean`, `number`, `object` or `array`. This library automatically converts the values from command line arguments and environment variables to the expected type when possible. If the conversion is not possible or the validation fails an error is thrown. Validation errors provide enough context to users to let them know the option that failed. This library uses [`ajv`](https://github.com/ajv-validator) and [`better-ajv-errors`](https://github.com/atlassian/better-ajv-errors) for validations.

Types `string`, `boolean`, `number` can be nullable using the option `nullable` property.

Here is an example of how to add an option to the root config object, and then you have information about how the option would be set from different sources:

```js
Expand Down Expand Up @@ -279,6 +281,21 @@ Examples about how to define options of type `object` from sources:

__The contents of the array are also converted to its correspondent type when the `itemsType` option is provided.__

### __Nullable types__

An option can be null when it is set as `nullable`. Nullable types are `string`, `boolean` and `number`. Types `object` and `array` can't be nullable, their value should be set to empty array or empty object instead.

```js
const config = new Config({ moduleName: "moduleName" });
const option = config.addOption({
name: "optionA",
type: "string",
nullable: true,
default: null,
});
await config.load();
```

## Built-in options

The library registers some options that can be used to determine the behavior of the library itself. As the rest of the configuration created by the library, these options can be set using configuration file, environment variables, command line arguments, etc. But there are some of them that can be defined only in some specific sources because they affect to reading that sources or not.
Expand Down Expand Up @@ -356,6 +373,7 @@ const namespace = config.addNamespace("name");
* __`name`__ _(String)_: Name for the option.
* __`description`__ _(String)_: _Optional_. Used in help, traces, etc.
* __`type`__ _(String)_. One of _`string`_, _`boolean`_, _`number`_, _`array`_ or _`object`_. Used to apply type validation when loading configuration and in `option.value` setter.
* __`nullable`__ _(Boolean)_. _Optional_. Default is `false`. When `true`, the option value can be set to `null`. It is only supported in types `string`, `number` and `boolean`.
* __`itemsType`__ _(String)_. Can be defined only when `type` is `array`. It must be one of _`string`_, _`boolean`_, _`number`_ or _`object`_.
* __`default`__ - _Optional_. Default value. Its type depends on the `type` option.
* __`extraData`__ - _(Object)_. _Optional_. Useful to store any extra data you want in the option. For example, Mocks Server uses it to define whether an option must be written when creating the configuration scaffold or not.
Expand Down Expand Up @@ -384,6 +402,7 @@ const rootOption = config.addOption("name2");
* `callback(value)` _(Function)_: Callback to be executed whenever the option value changes. It receives the new value as first argument.
* __`name`__: Getter returning the option name.
* __`type`__: Getter returning the option type.
* __`nullable`__: Getter returning whether the option is nullable or not.
* __`description`__: Getter returning the option description.
* __`extraData`__: Getter returning the option extra data.
* __`default`__: Getter returning the option default value.
Expand Down
2 changes: 1 addition & 1 deletion packages/config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {

// The glob patterns Jest uses to detect test files
testMatch: ["<rootDir>/test/**/*.spec.js"],
// testMatch: ["<rootDir>/test/src/getValidationSchema.spec.js"],
// testMatch: ["<rootDir>/test/src/validate.spec.js"],

// The test environment that will be used for testing
testEnvironment: "node",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mocks-server/config",
"version": "1.2.1",
"version": "1.3.0",
"description": "Modular configuration provider. Read it from file, environment and arguments",
"keywords": [
"configuration",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.organization=mocks-server
sonar.projectKey=mocks-server_main_config
sonar.projectName=config
sonar.projectVersion=1.2.1
sonar.projectVersion=1.3.0

sonar.javascript.file.suffixes=.js
sonar.sourceEncoding=UTF-8
Expand Down
10 changes: 8 additions & 2 deletions packages/config/src/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const { types, avoidArraysMerge } = require("./types");
class Option {
constructor(properties) {
this._eventEmitter = new EventEmitter();
validateOptionAndThrow(properties);
this._name = properties.name;
this._nullable = Boolean(properties.nullable);
this._extraData = properties.extraData;
this._type = properties.type;
this._description = properties.description;
Expand All @@ -20,6 +20,8 @@ class Option {
this._value = this._default;
this._eventsStarted = false;
this._hasBeenSet = false;

validateOptionAndThrow({ ...properties, nullable: this._nullable });
}

get extraData() {
Expand All @@ -38,6 +40,10 @@ class Option {
return this._type;
}

get nullable() {
return this._nullable;
}

get itemsType() {
return this._itemsType;
}
Expand All @@ -64,7 +70,7 @@ class Option {
}

_validateAndThrow(value) {
validateValueTypeAndThrow(value, this._type, this._itemsType);
validateValueTypeAndThrow(value, this._type, this._nullable, this._itemsType);
}

_emitChange(previousValue, value) {
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const types = {
BOOLEAN: "boolean",
OBJECT: "object",
ARRAY: "array",
NULL: "null",
};

const FALSY_VALUES = ["false", "0", 0];
Expand Down
51 changes: 35 additions & 16 deletions packages/config/src/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const ajv = new Ajv({ allErrors: true });

const { types } = require("./types");

function enforceDefaultTypeSchema(type, itemsType) {
function enforceDefaultTypeSchema({ type, itemsType, nullable }) {
const schema = {
properties: {
name: { type: types.STRING },
type: { enum: [type] },
nullable: { enum: [false] },
description: { type: types.STRING },
default: {
type,
Expand All @@ -21,15 +22,20 @@ function enforceDefaultTypeSchema(type, itemsType) {
},
},
additionalProperties: false,
required: ["name", "type"],
required: ["name", "type", "nullable"],
};

if (nullable) {
schema.properties.default.type = [type, types.NULL];
schema.properties.nullable = { enum: [true] };
}

if (itemsType) {
schema.properties.itemsType = { enum: [itemsType] };
schema.properties.default.items = {
type: itemsType,
};
schema.required = ["name", "type", "itemsType"];
schema.required = ["name", "type", "nullable", "itemsType"];
}

return schema;
Expand All @@ -38,15 +44,18 @@ function enforceDefaultTypeSchema(type, itemsType) {
const optionSchema = {
type: types.OBJECT,
oneOf: [
enforceDefaultTypeSchema(types.NUMBER),
enforceDefaultTypeSchema(types.STRING),
enforceDefaultTypeSchema(types.BOOLEAN),
enforceDefaultTypeSchema(types.OBJECT),
enforceDefaultTypeSchema(types.ARRAY),
enforceDefaultTypeSchema(types.ARRAY, types.NUMBER),
enforceDefaultTypeSchema(types.ARRAY, types.STRING),
enforceDefaultTypeSchema(types.ARRAY, types.BOOLEAN),
enforceDefaultTypeSchema(types.ARRAY, types.OBJECT),
enforceDefaultTypeSchema({ type: types.NUMBER }),
enforceDefaultTypeSchema({ type: types.NUMBER, nullable: true }),
enforceDefaultTypeSchema({ type: types.STRING }),
enforceDefaultTypeSchema({ type: types.STRING, nullable: true }),
enforceDefaultTypeSchema({ type: types.BOOLEAN }),
enforceDefaultTypeSchema({ type: types.BOOLEAN, nullable: true }),
enforceDefaultTypeSchema({ type: types.OBJECT }),
enforceDefaultTypeSchema({ type: types.ARRAY }),
enforceDefaultTypeSchema({ type: types.ARRAY, itemsType: types.NUMBER }),
enforceDefaultTypeSchema({ type: types.ARRAY, itemsType: types.STRING }),
enforceDefaultTypeSchema({ type: types.ARRAY, itemsType: types.BOOLEAN }),
enforceDefaultTypeSchema({ type: types.ARRAY, itemsType: types.OBJECT }),
],
};

Expand Down Expand Up @@ -133,9 +142,16 @@ function validateSchemaAndThrow(config, schema, validator) {
function addNamespaceSchema(namespace, { rootSchema, allowAdditionalProperties }) {
const initialSchema = rootSchema || emptySchema({ allowAdditionalProperties });
const schema = namespace.options.reduce((currentSchema, option) => {
currentSchema.properties[option.name] = {
type: option.type,
};
if (option.nullable) {
currentSchema.properties[option.name] = {
type: [option.type, types.NULL],
};
} else {
currentSchema.properties[option.name] = {
type: option.type,
};
}

if (option.itemsType) {
currentSchema.properties[option.name].items = {
type: option.itemsType,
Expand Down Expand Up @@ -192,7 +208,10 @@ function validateOptionAndThrow(properties) {
validateSchemaAndThrow(properties, optionSchema, optionValidator);
}

function validateValueTypeAndThrow(value, type, itemsType) {
function validateValueTypeAndThrow(value, type, nullable, itemsType) {
if (nullable && value === null) {
return;
}
typeAndThrowValidators[type](value, itemsType);
}

Expand Down
Loading

0 comments on commit eeb9b1e

Please sign in to comment.