Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Commit

Permalink
fix(node): use provider creators
Browse files Browse the repository at this point in the history
Merge pull request #67 from homer0/homer0_providers
  • Loading branch information
homer0 authored Jul 18, 2020
2 parents 67a8e6e + 4261b0f commit 7f54d4e
Show file tree
Hide file tree
Showing 25 changed files with 785 additions and 397 deletions.
40 changes: 22 additions & 18 deletions documents/node/appConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,32 @@ app.register(environmentUtils);
app.register(rootRequire);
app.register(pathUtils);
```
Now, to generate the `appConfiguration` service with the required settings to work with the scenario described above:

To configure the `appConfiguration` provider so the service will work with the scenario described above, we just need to use it as a function:

```js
const myConfiguration = appConfiguration(
'myApp',
{},
{
app.register(appConfiguration({
appName: 'myApp',
defaultConfiguration: {},
options: {
environmentVariable: 'CONFIG',
path: './config/',
filenameFormat: '[name].js',
},
);
app.register(myConfiguration);
}));
```

Done, the service is registered in the container. Let's explain a little bit why _'those'_ parameters:
> To fully understand the settings, you should read the techincal documentation for {@link AppConfigurationProviderOptions}.
1. `myApp` is the name of the app, the service uses it on the default `path` and `filenameFormat` options, but in this case, we don't really need it for this scenario.
2. `{}` That's the default configuration all the others will _'extend'_, in this case is not needed.
3. The options:
- `environmentVariable`: The name of the variable the service will check to determine which configuration to use.
- `path`: The location of your configuration files.
- `filenameFormat`: The name format your files use. `[name]` will be replaced with the name of the configuration you want to use.
Done, the service is registered in the container.

You can also use the provider as it is and it will use the default settings:

```js
app.register(appConfiguration);
```

> `appConfiguration` is a "provider crator", so it can be used as both, a function that generates a provider, and as provider.
Now, there's only one thing to do:

Expand Down Expand Up @@ -212,7 +215,7 @@ const { valueOne, valueTwo } = appConfiguration.get(['valueOne', 'valueTwo']);
// Writing a single setting
appConfiguration.set('something', 'value');
// Write multiple settings
appConfiguration.write({
appConfiguration.set({
valueOne: 'one',
valueTwo: 'two',
});
Expand Down Expand Up @@ -244,7 +247,7 @@ There's a special rule behind this feature: The default configuration and/or the

## ES Modules

If you are using ESM, you can import the class and the provider generator from the `/esm` sub path:
If you are using ESM, you can import the class and the provider from the `/esm` sub path:

```js
import {
Expand All @@ -256,15 +259,16 @@ import {

import { AppConfiguration } from 'wootils/esm/node';

// just the provider generator
// just the provider

import { appConfiguration } from 'wootils/esm/node/providers';
```

## Technical documentation

- Module: {@link module:node/appConfiguration|node/appConfiguration}
- Class: {@link AppConfiguration}
- Provider generator: {@link module:node/appConfiguration~appConfiguration|appConfiguration}
- Provider: {@link module:node/appConfiguration~appConfiguration|appConfiguration}

> If you are reading this form the markdown document, you can go to the [online version](https://homer0.github.io/wootils); or you can generate the documentation site yourself by running the `docs` command:
>
Expand Down
1 change: 1 addition & 0 deletions documents/node/environmentUtils.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ import { environmentUtils } from 'wootils/esm/node/providers';

## Technical documentation

- Module: {@link module:node/environmentUtils|node/environmentUtils}
- Class: {@link EnvironmentUtils}
- Provider: {@link module:node/environmentUtils~environmentUtils|environmentUtils}

Expand Down
25 changes: 15 additions & 10 deletions documents/node/errorHandler.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ You get the date and time when it happened and the full stack trace of the error

### With Jimple

When used with Jimple, instead of using the `logger` service, it uses the `logger` service:

> It uses `logger` by default, but if the service is not available, it will try to fallback to `appLogger`, the alternative version of `logger` that prefixes all the messages with the project name.
> It uses `logger` by default, but if the service is not available, it will try to fallback to `appLogger`, the version of `logger` that prefixes all the messages with the package name.
```js
// Import all the required modules
Expand All @@ -85,6 +83,17 @@ app.register(logger);
// Register the ErrorHandler
app.register(errorHandler);
```

You could also configure it so it would exit the process when an error is caught:

```js
app.register(errorHandler({
exitOnError: true,
}));
```

> `errorHandler` is a "provider crator", so it can be used as both, a function that generates a provider, and as provider.
Now, we should tell the service to start listening for errors:

```js
Expand All @@ -109,13 +118,12 @@ Done! If you run the same code now, this is the kind of logged information you'l

## ES Modules

If you are using ESM, you can import the class, the provider and the provider generator from the `/esm` sub path:
If you are using ESM, you can import the class and the provider from the `/esm` sub path:

```js
import {
ErrorHandler,
errorHandler,
errorHandlerWithOptions,
} from 'wootils/esm/node/errorHandler';

// just the class
Expand All @@ -124,17 +132,14 @@ import { ErrorHandler } from 'wootils/esm/node';

// just the provider and/or the generator

import {
errorHandler,
errorHandlerWithOptions,
} from 'wootils/esm/node/providers';
import { errorHandler } from 'wootils/esm/node/providers';
```

## Technical documentation

- Module: {@link module:node/errorHandler|node/errorHandler}
- Class: {@link ErrorHandler}
- Provider: {@link module:node/errorHandler~errorHandler|errorHandler}
- Provider generator: {@link module:node/errorHandler~errorHandlerWithOptions|errorHandlerWithOptions}

> If you are reading this form the markdown document, you can go to the [online version](https://homer0.github.io/wootils); or you can generate the documentation site yourself by running the `docs` command:
>
Expand Down
39 changes: 20 additions & 19 deletions documents/node/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ const { Logger } = require('wootils/node');
// Create an instance
const logger = new Logger();
```

Now let's update the code:

```js
logger.log('Starting the app');
// Will log the message the same way `console.log` would.

if (usingExperimentalFeature()) {
logger.warning('WARNING: This feature is experimental');
logger.warn('WARNING: This feature is experimental');
// Will log a yellow message.
}

Expand Down Expand Up @@ -102,7 +103,7 @@ log.log('Starting the app');
// Will log the message the same way `console.log` would.

if (usingExperimentalFeature()) {
log.warning('WARNING: This feature is experimental');
log.warn('WARNING: This feature is experimental');
// Will log a yellow message.
}

Expand Down Expand Up @@ -134,13 +135,13 @@ Done, with that your app is now logging the messages with a color referencing th
This was demonstrated on the example above:

1. `success(message)` will log a green message.
2. `warning(message)` will log a yellow message.
2. `warn(message)` will log a yellow message.
3. `error(message)` will log a red message.
4. `info(message)` will log a gray message.

But they all depend on this method:
But they all depend on this method: `log(message, color)`; it allows you to specify one of the colors available on the [`colors`](https://yarnpkg.com/package/colors) package.

`log(message, color)` allows you to specify one of the colors available on the [`colors`](https://yarnpkg.com/en/package/colors) package. By default, it uses the console default text color.
By default, it uses the console default text color.

### Multiple messages at once

Expand All @@ -164,7 +165,7 @@ logger.success([

### Prefix

When constructing the service or when generating the provider, you can specify a `prefix` that will be added to every message:
When constructing the service or when registering the provider, you can specify a `prefix` that will be added to every message:

#### Without Jimple

Expand All @@ -182,11 +183,13 @@ logger.success('The instance was created!');
```js
// Import all the required modules
const Jimple = require('jimple');
const { loggerWithOptions } = require('wootils/node/providers');
const { logger } = require('wootils/node/providers');
// Create a dummy app
const app = new Jimple();
// Register the logger
app.register(loggerWithOptions('my-app'));
app.register(logger({
messagesPrefix: 'my-app',
}));

app.get('logger').success('The instance was created!');
// This will log `[my-app] The instance was created!` on green.
Expand All @@ -212,11 +215,14 @@ logger.success('The instance was created!');
```js
// Import all the required modules
const Jimple = require('jimple');
const { loggerWithOptions } = require('wootils/node/providers');
const { logger } = require('wootils/node/providers');
// Create a dummy app
const app = new Jimple();
// Register the logger
app.register(loggerWithOptions('my-app', true));
app.register(logger({
messagesPrefix: 'my-app',
showTime: true,
}));

app.get('logger').success('The instance was created!');
// This will log `[my-app][YYYY-MM-DD HH:MM:SS] The instance was created!` on green.
Expand All @@ -241,42 +247,37 @@ app.get('appLogger').success('The instance was created!');
// This will log `[skynet] The instance was created!` on green.
```

There's also `appLoggerWithOptions` that allows you to enable or disabled the date and time for the messages.
`appLogger` can also be used as a function, like `logger`, so you can send an options object to enable the `showTime`.

## ES Modules

If you are using ESM, you can import the class, the providers and the provider generators from the `/esm` sub path:
If you are using ESM, you can import the class and the providers from the `/esm` sub path:

```js
import {
Logger,
logger,
loggerWithOptions,
appLogger,
appLoggerWithOptions,
} from 'wootils/esm/node/logger';

// just the class

import { Logger } from 'wootils/esm/node';

// just the providers and/or the generators
// just the providers

import {
logger,
loggerWithOptions,
appLogger,
appLoggerWithOptions,
} from 'wootils/esm/node/providers';
```

## Technical documentation

- Module: {@link module:node/logger|node/logger}
- Class: {@link Logger}
- Provider: {@link module:node/logger~logger|logger}
- Provider generator: {@link module:node/logger~loggerWithOptions|loggerWithOptions}
- Provider with app name as prefix: {@link module:node/logger~appLogger|appLogger}
- Provider generator for {@link module:node/logger~appLogger|appLogger}: {@link module:node/logger~appLoggerWithOptions|appLoggerWithOptions}

> If you are reading this form the markdown document, you can go to the [online version](https://homer0.github.io/wootils); or you can generate the documentation site yourself by running the `docs` command:
>
Expand Down
1 change: 1 addition & 0 deletions documents/node/packageInfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { packageInfoProvider } from 'wootils/esm/node/providers';

## Technical documentation

- Module: {@link module:node/packageInfo|node/packageInfo}
- Function: {@link module:node/packageInfo~packageInfo|packageInfo}
- Provider: {@link module:node/packageInfo~packageInfoProvider|packageInfoProvider}

Expand Down
12 changes: 4 additions & 8 deletions documents/node/pathUtils.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,28 @@ const pathToFile = pathUtils.joinFrom('my-location', 'some-file.js');

## ES Modules

If you are using ESM, you can import the class, the provider and the provider generator from the `/esm` sub path:
If you are using ESM, you can import the class and the provider from the `/esm` sub path:

```js
import {
PathUtils,
pathUtils,
pathUtilsWithHome,
} from 'wootils/esm/node/pathUtils';

// just the class

import { PathUtils } from 'wootils/esm/node';

// just the provider and/or the generator
// just the provider

import {
pathUtils,
pathUtilsWithHome,
} from 'wootils/esm/node/providers';
import { pathUtils } from 'wootils/esm/node/providers';
```

## Technical documentation

- Module: {@link module:node/pathUtils|node/pathUtils}
- Class: {@link PathUtils}
- Provider: {@link module:node/pathUtils~pathUtils|pathUtils}
- Provider generator: {@link module:node/pathUtils~pathUtilsWithHome|pathUtilsWithHome}

> If you are reading this form the markdown document, you can go to the [online version](https://homer0.github.io/wootils); or you can generate the documentation site yourself by running the `docs` command:
>
Expand Down
1 change: 1 addition & 0 deletions documents/node/rootRequire.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import { rootRequireProvider } from 'wootils/esm/node/providers';

## Technical documentation

- Module: {@link module:node/rootRequire|node/rootRequire}
- Function: {@link module:node/rootRequire~rootRequire|rootRequire}
- Provider: {@link module:node/rootRequire~rootRequireProvider|rootRequireProvider}

Expand Down
Loading

0 comments on commit 7f54d4e

Please sign in to comment.