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

Commit

Permalink
fix: use proper types for providers
Browse files Browse the repository at this point in the history
This is actually a 'docs:', but I need to trigger a release.
  • Loading branch information
homer0 committed Aug 8, 2020
1 parent c0525f9 commit ab5691e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 29 deletions.
5 changes: 2 additions & 3 deletions node/appConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const { providerCreator } = require('../shared/jimpleFns');
*/

/**
* @typedef {import('../shared/jimpleFns').ProviderCreatorWithOptions<O>}
* ProviderCreatorWithOptions
* @typedef {import('../shared/jimpleFns').ProviderCreator<O>} ProviderCreator
* @template O
*/

Expand Down Expand Up @@ -423,7 +422,7 @@ class AppConfiguration {
/**
* The service provider to register an instance of {@link AppConfiguration} on the container.
*
* @type {ProviderCreatorWithOptions<AppConfigurationProviderOptions>}
* @type {ProviderCreator<AppConfigurationProviderOptions>}
* @tutorial appConfiguration
*/
const appConfiguration = providerCreator((options = {}) => (app) => {
Expand Down
5 changes: 2 additions & 3 deletions node/environmentUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const { providerCreator } = require('../shared/jimpleFns');
*/

/**
* @typedef {import('../shared/jimpleFns').ProviderCreatorWithOptions<O>}
* ProviderCreatorWithOptions
* @typedef {import('../shared/jimpleFns').ProviderCreator<O>} ProviderCreator
* @template O
*/

Expand Down Expand Up @@ -131,7 +130,7 @@ class EnvironmentUtils {
/**
* The service provider to register an instance of {@link EnvironmentUtils} on the container.
*
* @type {ProviderCreatorWithOptions<EnvironmentUtilsProviderOptions>}
* @type {ProviderCreator<EnvironmentUtilsProviderOptions>}
* @tutorial environmentUtils
*/
const environmentUtils = providerCreator((options = {}) => (app) => {
Expand Down
5 changes: 2 additions & 3 deletions node/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const { deepAssignWithShallowMerge } = require('../shared/deepAssign');
*/

/**
* @typedef {import('../shared/jimpleFns').ProviderCreatorWithOptions<O>}
* ProviderCreatorWithOptions
* @typedef {import('../shared/jimpleFns').ProviderCreator<O>} ProviderCreator
* @template O
*/

Expand Down Expand Up @@ -142,7 +141,7 @@ class ErrorHandler {
*
* @throws {Error} If `services.logger` specifies a service that doesn't exist or if it's a falsy
* value.
* @type {ProviderCreatorWithOptions<ErrorHandlerProviderOptions>}
* @type {ProviderCreator<ErrorHandlerProviderOptions>}
* @tutorial errorHandler
*/
const errorHandler = providerCreator((options = {}) => (app) => {
Expand Down
7 changes: 3 additions & 4 deletions node/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const { deepAssign } = require('../shared/deepAssign');
*/

/**
* @typedef {import('../shared/jimpleFns').ProviderCreatorWithOptions<O>}
* ProviderCreatorWithOptions
* @typedef {import('../shared/jimpleFns').ProviderCreator<O>} ProviderCreator
* @template O
*/

Expand Down Expand Up @@ -265,7 +264,7 @@ class Logger {
/**
* The service provider to register an instance of {@link Logger} on the container.
*
* @type {ProviderCreatorWithOptions<LoggerProviderOptions>}
* @type {ProviderCreator<LoggerProviderOptions>}
* @tutorial logger
*/
const logger = providerCreator((options = {}) => (app) => {
Expand All @@ -278,7 +277,7 @@ const logger = providerCreator((options = {}) => (app) => {
* The service provider to register an instance of {@link Logger} with the package name as
* messages prefix on the container.
*
* @type {ProviderCreatorWithOptions<AppLoggerProviderOptions>}
* @type {ProviderCreator<AppLoggerProviderOptions>}
* @tutorial logger
*/
const appLogger = providerCreator((options = {}) => (app) => {
Expand Down
5 changes: 2 additions & 3 deletions node/packageInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const { deepAssign } = require('../shared/deepAssign');
*/

/**
* @typedef {import('../shared/jimpleFns').ProviderCreatorWithOptions<O>}
* ProviderCreatorWithOptions
* @typedef {import('../shared/jimpleFns').ProviderCreator<O>} ProviderCreator
* @template O
*/

Expand Down Expand Up @@ -46,7 +45,7 @@ const packageInfo = (pathUtils) => fs.readJsonSync(pathUtils.join('package.json'
* The service provider that once registered on the app container will set the result of
* {@link module:node/packageInfo~packageInfo|packageInfo} as a service.
*
* @type {ProviderCreatorWithOptions<PackageInfoProviderOptions>}
* @type {ProviderCreator<PackageInfoProviderOptions>}
* @tutorial packageInfo
*/
const packageInfoProvider = providerCreator((options = {}) => (app) => {
Expand Down
5 changes: 2 additions & 3 deletions node/pathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const { providerCreator } = require('../shared/jimpleFns');
*/

/**
* @typedef {import('../shared/jimpleFns').ProviderCreatorWithOptions<O>}
* ProviderCreatorWithOptions
* @typedef {import('../shared/jimpleFns').ProviderCreator<O>} ProviderCreator
* @template O
*/

Expand Down Expand Up @@ -167,7 +166,7 @@ class PathUtils {
/**
* The service provider to register an instance of {@link PathUtils} on the container.
*
* @type {ProviderCreatorWithOptions<PathUtilsProviderOptions>}
* @type {ProviderCreator<PathUtilsProviderOptions>}
* @tutorial pathUtils
*/
const pathUtils = providerCreator((options = {}) => (app) => {
Expand Down
5 changes: 2 additions & 3 deletions node/rootRequire.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const { deepAssign } = require('../shared/deepAssign');
*/

/**
* @typedef {import('../shared/jimpleFns').ProviderCreatorWithOptions<O>}
* ProviderCreatorWithOptions
* @typedef {import('../shared/jimpleFns').ProviderCreator<O>} ProviderCreator
* @template O
*/

Expand Down Expand Up @@ -56,7 +55,7 @@ const rootRequire = (pathUtils) => (path) =>
* The service provider that once registered on the app container will set the result of
* {@link module:node/rootRequire~rootRequire|rootRequire} as a service.
*
* @type {ProviderCreatorWithOptions<RootRequireProviderOptions>}
* @type {ProviderCreator<RootRequireProviderOptions>}
* @tutorial rootRequire
*/
const rootRequireProvider = providerCreator((options = {}) => (app) => {
Expand Down
83 changes: 76 additions & 7 deletions shared/jimpleFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,68 @@
* @property {ProviderRegisterFn} register The method that gets called when registering the
* provider.
* @parent module:shared/jimpleFns
* @example
* container.register(myProvider);
*/

/**
* A function called in order to generate a {@link Provider}. They usually have different options
* that will be sent to the provider creation.
*
* @callback ProviderCreatorFn
* @returns {Provider}
* @parent module:shared/jimpleFns
*/

/**
* @callback ProviderCreatorWithOptions
* A special kind of {@link Provider} that can be used as a regular provider, or it can also be
* called as a function with custom options in order to obtain a "configured {@link Provider}".
*
* @callback ProviderCreator
* @param {Partial<O>} [options={}] The options to create the provider.
* @returns {Provider}
* @template O
* @property {ProviderRegisterFn} register The method that gets called when registering the
* provider.
* @parent module:shared/jimpleFns
* @example
* // Register it with its default options.
* container.register(myProvider);
* // Register it with custom options.
* container.register(myProvider({ enabled: true }));
*/

/**
* @typedef {Object.<string,Provider>} ProvidersDictionary
*/

/**
* @typedef {ProvidersDictionary & ProvidersProperties} Providers
*/

/**
* @typedef {Object} ProvidersProperties
* @property {ProviderRegisterFn} register The function that will register all the providers on the
* container.
* @augments ProvidersDictionary
*/

/**
* Generates a collection of {@link Provider} objects that can be used to register all of them
* at once.
*
* @example
* // Generate the collection
* const myProviders = providers({ oneProvider, otherProvider });
* // Register all of them
* container.register(myProviders);
* // Register only one
* container.register(myProviders.otherProvider);
*
* @callback ProvidersCreator
* @param {Object.<string,Provider>} providers The dictionary of providers to add to the
* collection.
* @returns {Providers}
*/

/**
Expand Down Expand Up @@ -80,7 +128,7 @@
* make use of.
*
* @example
* <caption>The `provider` shorthand functino is an _entity_ with a `register` function:</caption>
* <caption>The `provider` shorthand function is an _entity_ with a `register` function:</caption>
* const someProvider = resource('provider', 'register', (app) => {
* // ...
* });
Expand Down Expand Up @@ -231,24 +279,45 @@ const resourcesCollection = (name, key, fn = null) => (items) => {
};
};
/**
* Creates a provider resource.
* Creates a resource provider.
*
* @example
* // Define the provider
* const myService = provider((app) => {
* app.set('myService', () => new MyService());
* });
*
* // Register it on the container
* container.register(myService);
*
* @param {ProviderRegisterFn} registerFn The function the container will call in order to
* register the provider.
* @returns {Provider}
*/
const provider = (registerFn) => resource('provider', 'register', registerFn);
/**
* Creates a configurable provider.
* Creates a configurable provider. It's configurable because the creator, instead of just
* being sent to the container to register, it can also be called as a function with custom
* options and generate a new provider.
*
* @param {ProviderCreatorFn} creatorFn The function that will be used to create the provider.
* @returns {ResourceCreator}
* @example
* // Define the provider creator
* const myProvider = providerCreator((options = {}) => (app) => {
* app.set('myService', () => new MyService(options));
* });
* // Register it with the default options
* container.register(myProvider);
* // Register it with custom options
* container.register(myProvider({ enabled: true }));
*
* @param {ProviderCreatorFn} creatorFn The function that generates the provider.
* @returns {ProviderCreator<*>}
*/
const providerCreator = (creatorFn) => resourceCreator('provider', 'register', creatorFn);
/**
* Creates a collection of providers.
*
* @type {ResourcesCollectionCreator<Provider>}
* @type {ProvidersCreator}
*/
const providers = resourcesCollection('providers', 'register');

Expand Down

0 comments on commit ab5691e

Please sign in to comment.