@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.
package/dist/index.d.ts DELETED
@@ -1,371 +0,0 @@
1
- declare class Container {
2
- static provider<T extends (arg0: Container) => void>(register: T): {
3
- register: T;
4
- };
5
- _items: Record<string, unknown>;
6
- _instances: Map<string, unknown>;
7
- _factories: Set<unknown>;
8
- _protected: Set<unknown>;
9
- /**
10
- * Creates a Jimple Container.
11
- *
12
- * @param values An optional object whose keys and values will be associated in the
13
- * container at initialization.
14
- */
15
- constructor(values?: Record<string, unknown>);
16
- /**
17
- * Return the specified parameter or service. If the service is not built yet, this
18
- * function will construct the service injecting all the dependencies needed in it's
19
- * definition so the returned object is totally usable on the fly.
20
- *
21
- * @param key The key of the parameter or service to return.
22
- * @returns The object related to the service or the value of the parameter
23
- * associated with the given key.
24
- * @template T The type of the returned object.
25
- * @throws If the key does not exist.
26
- */
27
- get<T = unknown>(key: string): T;
28
- /**
29
- * Defines a new parameter or service.
30
- *
31
- * @param key The key of the parameter or service to be defined.
32
- * @param value The value of the parameter or a function that receives the container
33
- * as parameter and constructs the service.
34
- */
35
- set(key: string, value: unknown): void;
36
- /**
37
- * Returns if a service or parameter with the informed key is already defined in the
38
- * container.
39
- *
40
- * @param key The key of the parameter or service to be checked.
41
- * @returns If the key exists in the container or not.
42
- */
43
- has(key: string): boolean;
44
- /**
45
- * Defines a service as a factory, so the instances are not cached in the service and
46
- * that function is always called.
47
- *
48
- * @param fn The function that constructs the service that is a factory.
49
- * @returns The same function passed as parameter.
50
- */
51
- factory<C = this>(fn: (arg0: C) => unknown): (arg0: C) => unknown;
52
- /**
53
- * Defines a function as a parameter, so that function is not considered a service.
54
- *
55
- * @param fn The function to not be considered a service.
56
- * @returns The same function passed as parameter.
57
- */
58
- protect<T extends () => unknown>(fn: T): T;
59
- /**
60
- * Return all the keys registered in the container.
61
- */
62
- keys(): string[];
63
- /**
64
- * Extends a service already registered in the container.
65
- *
66
- * @param key The key of the service to be extended.
67
- * @param fn The function that will be used to extend the service.
68
- * @throws If the key is not already defined.
69
- * @throws If the key saved in the container does not correspond to a service.
70
- * @throws If the function passed it not...well, a function.
71
- */
72
- extend<C = this>(key: string, fn: (arg0: unknown, arg1: C) => unknown): void;
73
- /**
74
- * Uses an provider to extend the service, so it's easy to split the service and
75
- * parameter definitions across the system.
76
- *
77
- * @param provider The provider to be used to register services and parameters in
78
- * this container.
79
- */
80
- register<C = this>(provider: {
81
- register: (arg0: C) => void;
82
- }): void;
83
- /**
84
- * Returns the raw value of a service or parameter. So, in the case of a service,
85
- * for example, the value returned is the function used to construct the service.
86
- *
87
- * @param key The key of the service or parameter to return.
88
- * @throws If the key does not exist in the container.
89
- */
90
- raw<T = unknown>(key: string): T;
91
- }
92
-
93
- declare const Jimple$1: typeof Container;
94
-
95
- declare class Jimple extends Jimple$1 {
96
- /**
97
- * This is a version of the `get` method that doesn't throw if the key doesn't exist. It
98
- * will return `undefined` instead.
99
- *
100
- * @param key The key of the parameter or service to return.
101
- * @returns The object related to the service or the value of the parameter
102
- * associated with the given key.
103
- * @template T The type of the returned object.
104
- */
105
- try<T = unknown>(key: string): T | undefined;
106
- }
107
- /**
108
- * Shorthand for `new Jimple()`.
109
- *
110
- * @param args The same parameters as the {@link Jimple} constructor.
111
- * @returns A new instance of {@link Jimple}.
112
- */
113
- declare const jimple: (...args: ConstructorParameters<typeof Jimple>) => Jimple;
114
-
115
- type GenericFn = (...args: any[]) => any;
116
- type GenericCurriedFn<ReturnFn = GenericFn> = (...args: any[]) => ReturnFn;
117
- type Resource<Name extends string, Key extends string, Fn extends GenericFn> = {
118
- [NameItem in Name]: true;
119
- } & {
120
- [KeyItem in Key]: Fn;
121
- } & Record<string, unknown>;
122
- type ResourceCreatorCurriedFn<Name extends string, Key extends string, Fn extends GenericCurriedFn> = (...args: any[]) => Resource<Name, Key, ReturnType<Fn>>;
123
- type ResourceCreatorHandler<Name extends string, Key extends string, Fn extends GenericCurriedFn> = ProxyHandler<ResourceCreatorCurriedFn<Name, Key, Fn>> & {
124
- name: Name;
125
- resource: ReturnType<Fn> | null;
126
- };
127
- type ResourceCreator<Name extends string, Key extends string, CreatorFn extends GenericCurriedFn, ResourceFn extends GenericFn> = ((...args: Parameters<CreatorFn>) => Resource<Name, Key, ResourceFn>) & Resource<Name, Key, ResourceFn>;
128
-
129
- /**
130
- * Generates a function to create resource creators of an specified type. This function
131
- * itself doesn't have logic, but it's just in charge of creating the constraint for the
132
- * resource creator function.
133
- *
134
- * As all the other _"factory functions"_, this is meant to be used as a building block
135
- * when extending the container.
136
- *
137
- * @returns A function that can be used to create "resource creators".
138
- * @template ResourceFn The type of function the resource creator needs to have.
139
- * @example
140
- *
141
- * type ActionFn = (c: Jimple) => void;
142
- * const factory = resourceCreatorFactory<ActionFn>();
143
- * const myAction = factory('action', 'fn', (name = 'foo') => (c) => {
144
- * c.set(name, 'bar');
145
- * });
146
- *
147
- */
148
- declare const resourceCreatorFactory: <ResourceFn extends GenericFn>() => <Name extends string, Key extends string, ResFn extends ResourceFn, CreatorFn extends GenericCurriedFn<ResFn>>(name: Name, key: Key, creatorFn: CreatorFn) => ResourceCreator<Name, Key, CreatorFn, ResourceFn>;
149
-
150
- /**
151
- * Generates a function to create resources of an specified type. This function itself
152
- * doesn't have logic, but it's just in charge of creating the constraint for the resource
153
- * function.
154
- *
155
- * As all the other _"factory functions"_, this is meant to be used as a building block
156
- * when extending the container.
157
- *
158
- * @returns A function that can be used to create a resource with a specified type of
159
- * function.
160
- * @template ResourceFn The type of function the resource needs to have.
161
- * @example
162
- *
163
- * type ActionFn = (c: Jimple) => void;
164
- * const factory = resourceFactory<ActionFn>();
165
- * const myAction = factory('action', 'fn', (c) => {
166
- * c.set('foo', 'bar');
167
- * });
168
- *
169
- */
170
- declare const resourceFactory: <ResourceFn extends GenericFn>() => <Name extends string, Key extends string, Fn extends ResourceFn>(name: Name, key: Key, fn: Fn) => Resource<Name, Key, Fn>;
171
-
172
- /**
173
- * Generates a function to configure a collection of resources of an specified type. This
174
- * function itself doesn't have logic, but it's just in charge of creating the constraint
175
- * for the resource name and function.
176
- *
177
- * As all the other _"factory functions"_, this is meant to be used as a building block
178
- * when extending the container.
179
- *
180
- * @returns A function that can be used to configure a collection of resources.
181
- * @template ResourceName The literal type of the name the resources needs to have.
182
- * @template ResourceFn The type of function the resources needs to have.
183
- * @example
184
- *
185
- * type ActionFn = (c: Jimple) => void;
186
- * const factory = resourcesCollectionFactory<'action', ActionFn>();
187
- * const myActions = factory('action', 'fn')({ myActionA, myActionB });
188
- *
189
- */
190
- declare const resourcesCollectionFactory: <ResourceName extends string, ResourceFn extends GenericFn>() => <Name extends string, Key extends string, ItemKey extends string, Items extends Record<ItemKey, Resource<ResourceName, Key, ResourceFn>>, CustomFn extends (items: Items, ...rest: Parameters<ResourceFn>) => void>(name: Name, key: Key, fn?: CustomFn) => <ItemsParam>(items: ItemsParam & Items) => typeof items & Resource<Name, Key, ResourceFn>;
191
-
192
- type GenericDict = Record<any, any>;
193
- /**
194
- * A helper that reusable services can use to resolve dependencies in constructors and/or
195
- * providers.
196
- *
197
- * @template InjectDictionary A dictionary of the dependencies and their types.
198
- * @template InjectKey The literal type of the dictionary types.
199
- */
200
- declare class InjectHelper<InjectDictionary extends GenericDict, InjectKey = keyof InjectDictionary> {
201
- /**
202
- * This method is meant to be used to validate the dependencies a service receives, and
203
- * needs.
204
- * It will check on the received dependencies, if a specific dependency exists, it will
205
- * return it, otherwise, it will create a new instance.
206
- *
207
- * @param dependencies The dependencies received by the implementation.
208
- * @param key The key of the dependency to validate.
209
- * @param init A function to create a dependency in case it doesn't exist in
210
- * the dictionary.
211
- * @returns An instance of the dependency.
212
- * @template DepKey The literal key of the dependency to validate.
213
- * @template DepType The type of the dependency, obtained from the dictionary sent
214
- * to the constructor.
215
- * @example
216
- *
217
- * type Dependencies = {
218
- * dep: string;
219
- * };
220
- * const helper = new InjectHelper<Dependencies>();
221
- *
222
- * type MyServiceOptions = {
223
- * inject?: Partial<Dependencies>;
224
- * };
225
- * const myService = ({ inject = {} }: MyServiceOptions) => {
226
- * const dep = helper.get(inject, 'dep', () => 'default');
227
- * console.log('dep:', dep);
228
- * };
229
- *
230
- */
231
- get<DepKey extends InjectKey, DepType = InjectDictionary[DepKey]>(dependencies: Partial<InjectDictionary>, key: InjectKey, init: () => DepType): DepType;
232
- /**
233
- * This is meant to be used in a provider creator to resolve dependencies' names sent as
234
- * options. For each dependency, the method will first check if a new name was
235
- * specified, and try to get it with that name, otherwise, it will try to get it with
236
- * the default name, and if it doesn't exist, it will just keep it as `undefined` and
237
- * expect the service implements {@link InjectHelper.get} to ensure the dependency.
238
- *
239
- * @param dependencies The dependencies needed by the service.
240
- * @param container A reference to the Jimple container.
241
- * @param inject A dictionary of dependencies names.
242
- * @returns A dictionary of dependencies to send to the service.
243
- * @template DepKey The literal key of the dependencies to validate.
244
- * @template Container The type of the Jimple container.
245
- * @example
246
- *
247
- * type Dependencies = {
248
- * dep: string;
249
- * };
250
- * const helper = new InjectHelper<Dependencies>();
251
- *
252
- * type MyProviderOptions = {
253
- * services?: {
254
- * [key in keyof Dependencies]?: string;
255
- * };
256
- * };
257
- *
258
- * const myProvider = providerCreator(
259
- * ({ services = {} }: MyProviderOptions) =>
260
- * (container) => {
261
- * container.set('myService', () => {
262
- * const inject = helper.resolve(['dep'], container, services);
263
- * return myService({ inject });
264
- * });
265
- * },
266
- * );
267
- *
268
- */
269
- resolve<DepKey extends InjectKey, Container extends Jimple>(dependencies: DepKey[], container: Container, inject: Partial<Record<keyof InjectDictionary, string>>): Partial<InjectDictionary>;
270
- }
271
- /**
272
- * Shorthand for `new InjectHelper()`.
273
- *
274
- * @returns A new instance of {@link InjectHelper}.
275
- */
276
- declare const injectHelper: <InjectDictionary extends GenericDict>() => InjectHelper<InjectDictionary, keyof InjectDictionary>;
277
-
278
- type ProviderName = 'provider';
279
- type ProviderKey = 'register';
280
- /**
281
- * The function the container will call when the provider is registered.
282
- *
283
- * @param container A reference for container where the provider is being registered.
284
- * @template ContainerType The type of the Jimple container (in case it gets
285
- * subclassed).
286
- */
287
- type ProviderRegisterFn<ContainerType extends Jimple = Jimple> = (container: ContainerType) => void;
288
- /**
289
- * Generates a function to create providers for a specific type of container.
290
- *
291
- * @returns A function to create providers.
292
- * @template ContainerType The type of the Jimple container (in case it gets
293
- * subclassed).
294
- */
295
- declare const createProvider: <ContainerType extends Jimple = Jimple>() => (register: ProviderRegisterFn<ContainerType>) => Resource<"provider", "register", ProviderRegisterFn<ContainerType>>;
296
- /**
297
- * Creates a resource to be registered in a Jimple container.
298
- *
299
- * @example
300
- *
301
- * export const myService = provider((c) => {
302
- * c.set('myService', () => new MyService());
303
- * });
304
- *
305
- */
306
- declare const provider: (register: ProviderRegisterFn<Jimple>) => Resource<"provider", "register", ProviderRegisterFn<Jimple>>;
307
- /**
308
- * A resource that can be registered in a Jimple container.
309
- */
310
- type Provider = ReturnType<typeof provider>;
311
-
312
- /**
313
- * Generates a function to create "provider creators" for a specific type of container.
314
- *
315
- * @returns A function to generate provider creators.
316
- * @template ContainerType The type of the Jimple container (in case it gets
317
- * subclassed).
318
- */
319
- declare const createProviderCreator: <ContainerType extends Jimple = Jimple>() => <CreatorFn extends GenericCurriedFn<ProviderRegisterFn<ContainerType>>>(creator: CreatorFn) => ResourceCreator<"provider", "register", CreatorFn, ProviderRegisterFn<ContainerType>>;
320
- /**
321
- * Generates a "provider creator": a provider that can also be called as a function in
322
- * order to create a configured provider.
323
- *
324
- * @example
325
- *
326
- * export const myService = providerCreator((options: Opts = {}) => (c) => {
327
- * c.set('myService', () => new MyService(options));
328
- * });
329
- *
330
- */
331
- declare const providerCreator: <CreatorFn extends GenericCurriedFn<ProviderRegisterFn<Jimple>>>(creator: CreatorFn) => ResourceCreator<"provider", "register", CreatorFn, ProviderRegisterFn<Jimple>>;
332
- /**
333
- * A function that returns a provider registration function.
334
- */
335
- type ProviderCreatorFn = Parameters<typeof providerCreator>[0];
336
-
337
- /**
338
- * Generates a function to create a providers collection for a specific type of container.
339
- *
340
- * @returns A function to create providers collections.
341
- * @template ContainerType The type of the Jimple container (in case it gets
342
- * subclassed).
343
- */
344
- declare const createProviders: <ContainerType extends Jimple = Jimple>() => <ItemsParam>(items: ItemsParam & Record<string, Resource<"provider", "register", ProviderRegisterFn<ContainerType>>>) => ItemsParam & Record<string, Resource<"provider", "register", ProviderRegisterFn<ContainerType>>> & {
345
- provider: true;
346
- } & {
347
- register: ProviderRegisterFn<ContainerType>;
348
- } & Record<string, unknown>;
349
- /**
350
- * Generates a collection of providers. A collection is a dictionary of providers that can
351
- * also be used as a provider, and when it gets registered, it will register all of the
352
- * providers in the collection.
353
- *
354
- * @param items A dictionary of providers.
355
- * @example
356
- *
357
- * <caption>Assuming we already have two service providers...</caption>
358
- *
359
- * export const services = providers({
360
- * myServiceProvider,
361
- * myOtherServiceProvider,
362
- * });
363
- *
364
- */
365
- declare const providers: <ItemsParam>(items: ItemsParam & Record<string, Resource<"provider", "register", ProviderRegisterFn<Jimple>>>) => ItemsParam & Record<string, Resource<"provider", "register", ProviderRegisterFn<Jimple>>> & {
366
- provider: true;
367
- } & {
368
- register: ProviderRegisterFn<Jimple>;
369
- } & Record<string, unknown>;
370
-
371
- export { type GenericCurriedFn, type GenericFn, InjectHelper, Jimple, type Provider, type ProviderCreatorFn, type ProviderKey, type ProviderName, type ProviderRegisterFn, type Resource, type ResourceCreator, type ResourceCreatorCurriedFn, type ResourceCreatorHandler, createProvider, createProviderCreator, createProviders, injectHelper, jimple, provider, providerCreator, providers, resourceCreatorFactory, resourceFactory, resourcesCollectionFactory };