@homer0/jimple 4.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ import type { Jimple } from '../jimple/index.js';
2
+ import { resourceFactory } from '../factories/index.js';
3
+
4
+ export type ProviderName = 'provider';
5
+ export type ProviderKey = 'register';
6
+ /**
7
+ * The function the container will call when the provider is registered.
8
+ *
9
+ * @param container A reference for container where the provider is being registered.
10
+ * @template ContainerType The type of the Jimple container (in case it gets
11
+ * subclassed).
12
+ */
13
+ export type ProviderRegisterFn<ContainerType extends Jimple = Jimple> = (
14
+ container: ContainerType,
15
+ ) => void;
16
+ /**
17
+ * Generates a function to create providers for a specific type of container.
18
+ *
19
+ * @returns A function to create providers.
20
+ * @template ContainerType The type of the Jimple container (in case it gets
21
+ * subclassed).
22
+ */
23
+ export const createProvider = <ContainerType extends Jimple = Jimple>() => {
24
+ type RegisterFn = ProviderRegisterFn<ContainerType>;
25
+ const factory = resourceFactory<RegisterFn>();
26
+ return (register: RegisterFn) => factory('provider', 'register', register);
27
+ };
28
+ /**
29
+ * Creates a resource to be registered in a Jimple container.
30
+ *
31
+ * @example
32
+ *
33
+ * export const myService = provider((c) => {
34
+ * c.set('myService', () => new MyService());
35
+ * });
36
+ *
37
+ */
38
+ export const provider = createProvider();
39
+ /**
40
+ * A resource that can be registered in a Jimple container.
41
+ */
42
+ export type Provider = ReturnType<typeof provider>;
@@ -0,0 +1,33 @@
1
+ import type { Jimple } from '../jimple/index.js';
2
+ import { resourceCreatorFactory, type GenericCurriedFn } from '../factories/index.js';
3
+ import type { ProviderRegisterFn } from './provider.js';
4
+ /**
5
+ * Generates a function to create "provider creators" for a specific type of container.
6
+ *
7
+ * @returns A function to generate provider creators.
8
+ * @template ContainerType The type of the Jimple container (in case it gets
9
+ * subclassed).
10
+ */
11
+ export const createProviderCreator = <ContainerType extends Jimple = Jimple>() => {
12
+ type RegisterFn = ProviderRegisterFn<ContainerType>;
13
+ type ProviderCreatorFn = GenericCurriedFn<RegisterFn>;
14
+ const factory = resourceCreatorFactory<RegisterFn>();
15
+ return <CreatorFn extends ProviderCreatorFn>(creator: CreatorFn) =>
16
+ factory('provider', 'register', creator);
17
+ };
18
+ /**
19
+ * Generates a "provider creator": a provider that can also be called as a function in
20
+ * order to create a configured provider.
21
+ *
22
+ * @example
23
+ *
24
+ * export const myService = providerCreator((options: Opts = {}) => (c) => {
25
+ * c.set('myService', () => new MyService(options));
26
+ * });
27
+ *
28
+ */
29
+ export const providerCreator = createProviderCreator();
30
+ /**
31
+ * A function that returns a provider registration function.
32
+ */
33
+ export type ProviderCreatorFn = Parameters<typeof providerCreator>[0];
@@ -0,0 +1,32 @@
1
+ import type { Jimple } from '../jimple/index.js';
2
+ import { resourcesCollectionFactory } from '../factories/index.js';
3
+ import type { ProviderRegisterFn, ProviderName } from './provider.js';
4
+ /**
5
+ * Generates a function to create a providers collection for a specific type of container.
6
+ *
7
+ * @returns A function to create providers collections.
8
+ * @template ContainerType The type of the Jimple container (in case it gets
9
+ * subclassed).
10
+ */
11
+ export const createProviders = <ContainerType extends Jimple = Jimple>() => {
12
+ type RegisterFn = ProviderRegisterFn<ContainerType>;
13
+ const factory = resourcesCollectionFactory<ProviderName, RegisterFn>();
14
+ return factory('provider', 'register');
15
+ };
16
+ /**
17
+ * Generates a collection of providers. A collection is a dictionary of providers that can
18
+ * also be used as a provider, and when it gets registered, it will register all of the
19
+ * providers in the collection.
20
+ *
21
+ * @param items A dictionary of providers.
22
+ * @example
23
+ *
24
+ * <caption>Assuming we already have two service providers...</caption>
25
+ *
26
+ * export const services = providers({
27
+ * myServiceProvider,
28
+ * myOtherServiceProvider,
29
+ * });
30
+ *
31
+ */
32
+ export const providers = createProviders();
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './jimple/index.js';
2
+ export * from './factories/index.js';
3
+ export * from './helpers/index.js';
@@ -0,0 +1,28 @@
1
+ import { Jimple as JimpleMod } from '../jimplemod/index.js';
2
+
3
+ export class Jimple extends JimpleMod {
4
+ /**
5
+ * This is a version of the `get` method that doesn't throw if the key doesn't exist. It
6
+ * will return `undefined` instead.
7
+ *
8
+ * @param key The key of the parameter or service to return.
9
+ * @returns The object related to the service or the value of the parameter
10
+ * associated with the given key.
11
+ * @template T The type of the returned object.
12
+ */
13
+ try<T = unknown>(key: string): T | undefined {
14
+ if (!this.has(key)) {
15
+ return undefined;
16
+ }
17
+
18
+ return this.get<T>(key);
19
+ }
20
+ }
21
+ /**
22
+ * Shorthand for `new Jimple()`.
23
+ *
24
+ * @param args The same parameters as the {@link Jimple} constructor.
25
+ * @returns A new instance of {@link Jimple}.
26
+ */
27
+ export const jimple = (...args: ConstructorParameters<typeof Jimple>): Jimple =>
28
+ new Jimple(...args);
@@ -0,0 +1,8 @@
1
+ // @ts-expect-error - the idea is to have `any` and assert on export.
2
+ import JimpleOriginal from 'jimple';
3
+ import { Container as JimpleContainer } from './jimple.type.js';
4
+
5
+ const Jimple = JimpleOriginal as typeof JimpleContainer;
6
+
7
+ export { Jimple };
8
+ export type { JimpleContainer };
@@ -0,0 +1,93 @@
1
+ /* eslint-disable */
2
+ // @ts-nocheck - This is just a workaround to get the type.
3
+ export class Container {
4
+ static provider<T extends (arg0: Container) => void>(
5
+ register: T,
6
+ ): {
7
+ register: T;
8
+ } {}
9
+ _items: Record<string, unknown>;
10
+ _instances: Map<string, unknown>;
11
+ _factories: Set<unknown>;
12
+ _protected: Set<unknown>;
13
+ /**
14
+ * Creates a Jimple Container.
15
+ *
16
+ * @param values An optional object whose keys and values will be associated in the
17
+ * container at initialization.
18
+ */
19
+ constructor(values?: Record<string, unknown>) {}
20
+ /**
21
+ * Return the specified parameter or service. If the service is not built yet, this
22
+ * function will construct the service injecting all the dependencies needed in it's
23
+ * definition so the returned object is totally usable on the fly.
24
+ *
25
+ * @param key The key of the parameter or service to return.
26
+ * @returns The object related to the service or the value of the parameter
27
+ * associated with the given key.
28
+ * @template T The type of the returned object.
29
+ * @throws If the key does not exist.
30
+ */
31
+ get<T = unknown>(key: string): T {}
32
+ /**
33
+ * Defines a new parameter or service.
34
+ *
35
+ * @param key The key of the parameter or service to be defined.
36
+ * @param value The value of the parameter or a function that receives the container
37
+ * as parameter and constructs the service.
38
+ */
39
+ set(key: string, value: unknown): void {}
40
+ /**
41
+ * Returns if a service or parameter with the informed key is already defined in the
42
+ * container.
43
+ *
44
+ * @param key The key of the parameter or service to be checked.
45
+ * @returns If the key exists in the container or not.
46
+ */
47
+ has(key: string): boolean {}
48
+ /**
49
+ * Defines a service as a factory, so the instances are not cached in the service and
50
+ * that function is always called.
51
+ *
52
+ * @param fn The function that constructs the service that is a factory.
53
+ * @returns The same function passed as parameter.
54
+ */
55
+ factory<C = this>(fn: (arg0: C) => unknown): (arg0: C) => unknown {}
56
+ /**
57
+ * Defines a function as a parameter, so that function is not considered a service.
58
+ *
59
+ * @param fn The function to not be considered a service.
60
+ * @returns The same function passed as parameter.
61
+ */
62
+ protect<T extends () => unknown>(fn: T): T {}
63
+ /**
64
+ * Return all the keys registered in the container.
65
+ */
66
+ keys(): string[] {}
67
+ /**
68
+ * Extends a service already registered in the container.
69
+ *
70
+ * @param key The key of the service to be extended.
71
+ * @param fn The function that will be used to extend the service.
72
+ * @throws If the key is not already defined.
73
+ * @throws If the key saved in the container does not correspond to a service.
74
+ * @throws If the function passed it not...well, a function.
75
+ */
76
+ extend<C = this>(key: string, fn: (arg0: unknown, arg1: C) => unknown): void {}
77
+ /**
78
+ * Uses an provider to extend the service, so it's easy to split the service and
79
+ * parameter definitions across the system.
80
+ *
81
+ * @param provider The provider to be used to register services and parameters in
82
+ * this container.
83
+ */
84
+ register<C = this>(provider: { register: (arg0: C) => void }): void {}
85
+ /**
86
+ * Returns the raw value of a service or parameter. So, in the case of a service,
87
+ * for example, the value returned is the function used to construct the service.
88
+ *
89
+ * @param key The key of the service or parameter to return.
90
+ * @throws If the key does not exist in the container.
91
+ */
92
+ raw<T = unknown>(key: string): T {}
93
+ }