shared/jimpleFns

Members

(inner, constant) providers :ProvidersCreator

Source:

Creates a collection of providers.

Type:
  • ProvidersCreator

Methods

(inner) provider(registerFn) → {Provider}

Source:

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);
Parameters:
Name Type Description
registerFn ProviderRegisterFn

The function the container will call in order to register the provider.

Returns:
Type
Provider

(inner) providerCreator(creatorFn) → {ProviderCreator.<any>}

Source:

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.

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 }));
Parameters:
Name Type Description
creatorFn ProviderCreatorFn

The function that generates the provider.

Returns:
Type
ProviderCreator.<any>

(inner) proxyContainer(container) → {Jimple}

Source:

Takes a Jimple container and creates a proxy for it so resouces can be accessed and registered like if they were its properties.

Example
const container = proxyContainer(new Jimple());
  container.service = () => new MyService();
  container.service.doSomething();
Parameters:
Name Type Description
container Jimple

The Jimpex container the proxy will be created for.

Returns:

The proxied version of the container.

Type
Jimple

(inner) resource(name, key, fn) → {Resource}

Source:

Generates a resource entity with a specific function Jimple or an abstraction of jimple can make use of.

Example

The `provider` shorthand function is an _entity_ with a `register` function:

  const someProvider = resource('provider', 'register', (app) => {
    // ...
  });
Parameters:
Name Type Description
name string

The name of the resource. The generated object will have a property with its name and the value true.

key string

The name of the key that will have the function on the generated object.

fn function

The function to interact with the resource.

Returns:
Type
Resource

(inner) resourceCreator(name, key, creatorFn) → {ResourceCreator}

Source:

This is a helper to dynamically configure a resource before creating it. The idea here is that the returned object can be used as a function, to configure the resource, or as a resource. The difference with resource is that, instead of providing the function to interact with the generated resource, the creatorFn parameter is a function that returns a function like the one you would use on resource. What this function actually returns is a Proxy, that when used as a function, it will return a resource; but when used as a resource, it will internally call creatorFn (and cache it) without parameters. It's very important that all the parameters of the creatorFn are optional, otherwise it will cause an error if called as a resource.

Example

Let's use `provider` again, that requires a `register` function:

  const someProvider = resourceCreator(
    'provider',
    'register',
    (options = {}) => (app) => {
      // ...
    },
  );

  // Register it as a resource
  container.register(someProvider);

  // Register it after creating a configured resource
  container.register(someProvider({ enabled: false }));
Parameters:
Name Type Description
name string

The name of the resource. The generated object will have a property with its name and the value true.

key string

The name of the key that will have the function on the generated object.

creatorFn function

The function that will generate the 'resource function'.

Returns:
Type
ResourceCreator

(inner) resourcesCollection(name, key, fnopt, nullable) → {ResourcesCollectionCreator.<Resource>}

Source:

Generates a collection of resources that can be called individually or all together via the key function.

Example

Following all the other examples(and the implementations), let's create a a providers collection.

  const firstProvider = resource('provider', 'register', (app) => {
    // ...
  });
  const secondProvider = resourceCreator(
    'provider',
    'register',
    (options = {}) => (app) => {
      // ...
    },
  );

  const bothProviders = resourcesCollection(
    'providers',
    'register',
  )({
    firstProvider,
    secondProvider,
  });

  // Register all at once
  container.register(bothProviders);
  // Register only one
  container.register(bothProviders.firstProvider);
  // Register only one, after configuring it
  container.register(bothProviders.secondProvider({ enabled: false }));
Parameters:
Name Type Attributes Default Description
name string

The name of the collection. When a collection is generated, the returned object will have a property with its name and the value true.

key string

The name of the key that will have the function to interact with the collection on the final object.

fn ResourcesCollectionFn <optional>
<nullable>
null

By default, if the key function of the collection gets called, all the items of the collection will be called with the same arguments. But you can specify a function that will receive all the items and all the arguments to customize the interaction.

Returns:
Type
ResourcesCollectionCreator.<Resource>

Type Definitions

Provider

Source:
Properties:
Name Type Description
register ProviderRegisterFn

The method that gets called when registering the provider.

Type:
Example
container.register(myProvider);

ProviderCreator(optionsopt) → {Provider}

Source:

A special kind of 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 Provider".

Example
// Register it with its default options.
  container.register(myProvider);
  // Register it with custom options.
  container.register(myProvider({ enabled: true }));
Parameters:
Name Type Attributes Default Description
options Partial.<O> <optional>
{}

The options to create the provider.

Returns:
Type
Provider

ProviderCreatorFn() → {Provider}

Source:

A function called in order to generate a Provider. They usually have different options that will be sent to the provider creation.

Returns:
Type
Provider

ProviderRegisterFn(app) → {Void}

Source:
Parameters:
Name Type Description
app Jimple

A reference to the dependency injection container.

Returns:
Type
Void

Resource

Source:
Properties:
Name Type Description
key function

The function (fn) sent to the resource function.

name boolean

This will always be true and it's the name sent to the resource function.

Type:

ResourceCreator()

Source:
Properties:
Name Type Description
key function

The result of the creatorFn sent to the resourceCreator function.

name boolean

This will always be true and it's the name sent to the resourceCreator function.

ResourcesCollectionCreator(items) → {Object.<string, R>}

Source:
Parameters:
Name Type Description
items Object.<string, R>

The dictionary of items to add to the collection.

Throws:
  • If one of the items key is the collection name or key.

    Type
    Error
  • If one of the items doesn't have a key function.

    Type
    Error
Returns:

In additions to the original dictionary of items, the returned object will have the collection flag (name), and the key function (fn) to interact with the collection.

Type
Object.<string, R>

ResourcesCollectionFn(items, …args)

Source:
Parameters:
Name Type Attributes Description
items Object.<string, R>

The dictionary of items to add to the collection.

args * <repeatable>

The arguments that were sent to the collection key function.

Providers

Source:
Type:
  • ProvidersDictionary | ProvidersProperties

ProvidersCreator(providers) → {Providers}

Source:

Generates a collection of 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);
Parameters:
Name Type Description
providers Object.<string, Provider>

The dictionary of providers to add to the collection.

Returns:
Type
Providers

ProvidersDictionary

Source:
Type:

ProvidersProperties

Source:
Properties:
Name Type Description
register ProviderRegisterFn

The function that will register all the providers on the container.

Type: