@rocketmakers/ioc 0.2.6 → 0.3.3

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/README.md CHANGED
@@ -44,7 +44,7 @@ export function getExternalUserData(apiKey: string, id: number): IUser {
44
44
  ```
45
45
  `ioc/types.ts`
46
46
  ```TypeScript
47
- import { Curried1 } from '@rocketmakers/ioc';
47
+ import { Curried1, Curried2 } from '@rocketmakers/ioc';
48
48
 
49
49
  // Application Types
50
50
  import type { AdminController } from '../controller/admin';
@@ -53,10 +53,13 @@ import type { getExternalUserData } from '../service/external-api';
53
53
 
54
54
  /** Define the types registered within the container */
55
55
  export type ControllerContainerTypes = {
56
+ id: number;
57
+ apiKey: string;
56
58
  adminService: AdminService;
57
59
  adminController: AdminController;
58
- apiKey: string;
59
- getExternalUserData: Curried1<typeof getExternalUserData>;
60
+ getExternalUserData: typeof getExternalUserData;
61
+ getExternalUserData1: Curried1<typeof getExternalUserData>;
62
+ getExternalUserData2: Curried2<typeof getExternalUserData>;
60
63
  };
61
64
 
62
65
  ```
@@ -67,40 +70,82 @@ export type ControllerContainerTypes = {
67
70
 
68
71
  `ioc/register.ts`
69
72
  ```TypeScript
70
- import { containerRegisterFunctionResolver, IContainer, register } from '@rocketmakers/ioc';
73
+ import { IContainer, register } from '@rocketmakers/ioc';
71
74
 
72
75
  import { AdminController } from '../controller/admin';
73
76
  import { AdminService } from '../service/admin';
74
77
  import { getExternalUserData } from '../service/external-api';
75
78
  import { ControllerContainerTypes } from './types';
76
79
 
77
- export const functionResolver = containerRegisterFunctionResolver<ControllerContainerTypes>();
78
-
79
80
  export function makeContainer(): IContainer<ControllerContainerTypes> {
80
81
  // Register all the keys of the 'ControllerContainerTypes'
81
82
  return register<ControllerContainerTypes>({
82
- adminController: b => b.class(AdminController).construct(['adminService']),
83
- adminService: b => b.class(AdminService).construct([]),
83
+ id: b => b.constant(31313),
84
84
  apiKey: b => b.constant('xyz...'),
85
- getExternalUserData: b => b.functionResolver(functionResolver.curry1(getExternalUserData, 'apiKey')),
85
+ adminService: b => b.class(AdminService, []),
86
+ adminController: b => b.class(AdminController, ['adminService']),
87
+ getExternalUserData: b => b.function(getExternalUserData),
88
+ getExternalUserData1: b => b.functionCurry(getExternalUserData, ['apiKey']),
89
+ getExternalUserData2: b => b.functionCurry(getExternalUserData, ['apiKey', 'id']),
90
+ });
91
+ }
92
+
93
+ ```
94
+
95
+ `ioc/register-with-prefix.ts`
96
+ ```TypeScript
97
+ import { registerWithPrefix } from '@rocketmakers/ioc';
98
+
99
+ import { AdminController } from '../controller/admin';
100
+ import { AdminService } from '../service/admin';
101
+ import { getExternalUserData } from '../service/external-api';
102
+ import { ControllerContainerTypes } from './types';
103
+
104
+ export function makeContainerPrefixed<TPrefix extends string>(prefix: TPrefix, id: number, apiKey: string) {
105
+ // Register all the keys of the 'ControllerContainerTypes'
106
+ return registerWithPrefix<TPrefix, ControllerContainerTypes>('none', prefix, {
107
+ id: b => b.constant(id),
108
+ apiKey: b => b.constant(apiKey),
109
+ adminService: b => b.class(AdminService, []),
110
+ adminController: b => b.class(AdminController, ['adminService']),
111
+ getExternalUserData: b => b.function(getExternalUserData),
112
+ getExternalUserData1: b => b.functionCurry(getExternalUserData, ['apiKey']),
113
+ getExternalUserData2: b => b.functionCurry(getExternalUserData, ['apiKey', 'id']),
86
114
  });
87
115
  }
88
116
 
117
+ const v1 = makeContainerPrefixed('this-', 31313, 'xyz...');
118
+ v1.get('this-id');
119
+ v1.get('this-apiKey');
120
+ v1.get('this-adminService');
121
+ v1.get('this-adminController');
122
+ v1.get('this-getExternalUserData');
123
+ v1.get('this-getExternalUserData1');
124
+ v1.get('this-getExternalUserData2');
125
+
126
+ const v2 = makeContainerPrefixed('that-', 13131, 'abc...');
127
+ v2.get('that-id');
128
+ v2.get('that-apiKey');
129
+ v2.get('that-adminService');
130
+ v2.get('that-adminController');
131
+ v2.get('that-getExternalUserData');
132
+ v2.get('that-getExternalUserData1');
133
+ v2.get('that-getExternalUserData2');
134
+
89
135
  ```
90
136
 
91
137
  ### Resolving with the container
92
138
 
93
139
  ```TypeScript
94
- import { makeContainer } from './ioc/register-new';
140
+ import { makeContainer } from './ioc/register';
95
141
 
96
142
  export function boot() {
97
143
  // Register the container
98
144
  const container = makeContainer();
99
145
 
100
- // getExternalUserData has type "(id: number): IUser" as we curried out the apiKey param
101
- const getExternalUserData = container.get('getExternalUserData');
102
-
103
- const userData = getExternalUserData(23);
146
+ // getExternalUserData1 has type "(id: number): IUser" as we curried out the apiKey param
147
+ const getExternalUserData1 = container.get('getExternalUserData1');
148
+ const userData = getExternalUserData1(23);
104
149
  return userData;
105
150
  }
106
151
 
@@ -123,17 +168,22 @@ import { AdminService } from '../service/admin';
123
168
  import { getExternalUserData } from '../service/external-api';
124
169
  import { ControllerContainerTypes } from './types';
125
170
 
126
- // Create legacy helper functions
171
+ /** @deprecated LEGACY: Create register helper */
127
172
  export const register = containerRegister<ControllerContainerTypes>();
173
+ /** @deprecated LEGACY: Create function resolver */
128
174
  export const functionResolver = containerRegisterFunctionResolver<ControllerContainerTypes>();
175
+ /** @deprecated LEGACY: Create register container */
129
176
  export const container = containerFactory<ControllerContainerTypes>();
130
177
 
131
178
  export function registerIoC(): void {
132
179
  // Register all the keys of the 'ControllerContainerTypes'
180
+ register.constant('id', 31313);
181
+ register.constant('apiKey', 'xyz...');
133
182
  register.class('adminService', AdminService).construct([]).inSingletonScope();
134
183
  register.class('adminController', AdminController).construct(['adminService']).inSingletonScope();
135
- register.constant('apiKey', 'xyz...');
136
- register.function('getExternalUserData', functionResolver.curry1(getExternalUserData, 'apiKey'));
184
+ register.function('getExternalUserData', getExternalUserData);
185
+ register.function('getExternalUserData1', functionResolver.curry1(getExternalUserData, 'apiKey'));
186
+ register.function('getExternalUserData2', functionResolver.curry2(getExternalUserData, 'apiKey', 'id'));
137
187
  }
138
188
 
139
189
  ```
@@ -141,16 +191,16 @@ export function registerIoC(): void {
141
191
  ### Resolving with the container
142
192
 
143
193
  ```TypeScript
144
- import { container, registerIoC } from './ioc/register';
194
+ import { container, registerIoC } from './ioc/register-legacy';
145
195
 
146
196
  export function boot() {
147
197
  // Register the container
148
198
  registerIoC();
149
199
 
150
200
  // getExternalUserData has type "(id: number): IUser" as we curried out the apiKey param
151
- const getExternalUserData = container.get('getExternalUserData');
201
+ const getExternalUserData1 = container.get('getExternalUserData1');
152
202
 
153
- const userData = getExternalUserData(23);
203
+ const userData = getExternalUserData1(23);
154
204
  return userData;
155
205
  }
156
206
 
@@ -0,0 +1,9 @@
1
+ import { Binder } from './binder';
2
+ import { NewableOf, TupleToKeysOfMapped } from './types';
3
+ /** @deprecated LEGACY: Using older method of class binding */
4
+ export declare class LegacyBinder<T, TItem, TDep> extends Binder<T, TItem, TDep> {
5
+ /** Register a class */
6
+ classOld<C extends NewableOf<TItem>>(cls: C): {
7
+ construct(keys: TupleToKeysOfMapped<ConstructorParameters<C>, T & TDep>): import("./inversify").BindingInWhenOnSyntax<InstanceType<C>>;
8
+ };
9
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LegacyBinder = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var binder_1 = require("./binder");
6
+ var inversify_1 = require("./inversify");
7
+ /** @deprecated LEGACY: Using older method of class binding */
8
+ var LegacyBinder = /** @class */ (function (_super) {
9
+ tslib_1.__extends(LegacyBinder, _super);
10
+ function LegacyBinder() {
11
+ return _super !== null && _super.apply(this, arguments) || this;
12
+ }
13
+ /** Register a class */
14
+ LegacyBinder.prototype.classOld = function (cls) {
15
+ var k = this.key;
16
+ var t = this.keyTransformer;
17
+ return {
18
+ construct: function (keys) {
19
+ var keys1 = keys;
20
+ return (0, inversify_1.registerClass)(k, cls, keys1.map(t));
21
+ },
22
+ };
23
+ };
24
+ return LegacyBinder;
25
+ }(binder_1.Binder));
26
+ exports.LegacyBinder = LegacyBinder;
@@ -0,0 +1,22 @@
1
+ import { Curried1, Param1 } from './curry.generated';
2
+ import { Func, IFunctionResolver } from './tail.generated';
3
+ import { NewableOf, TupleToKeysOfMapped } from './types';
4
+ declare type CollectCurriedParams<TOriginal extends Func, TCurried, TCollect extends any[] = []> = TOriginal extends TCurried ? TCollect : CollectCurriedParams<Curried1<TOriginal>, TCurried, [...TCollect, Param1<Parameters<TOriginal>>]>;
5
+ export declare class Binder<T, TItem, TDep> {
6
+ protected key: string;
7
+ protected keyTransformer: (keyName: string) => string;
8
+ constructor(key: string, keyTransformer: (keyName: string) => string);
9
+ /** Gets the underlying container binding to the specified key */
10
+ get binding(): import("inversify/dts/interfaces/interfaces").interfaces.BindingToSyntax<TItem>;
11
+ /** Register a function */
12
+ function(item: TItem): import("inversify/dts/interfaces/interfaces").interfaces.BindingWhenOnSyntax<TItem>;
13
+ /** Register a function resolver */
14
+ functionResolver(item: IFunctionResolver<TItem>): import("inversify/dts/interfaces/interfaces").interfaces.BindingWhenOnSyntax<TItem>;
15
+ /** Register a constant */
16
+ constant(item: TItem): import("inversify/dts/interfaces/interfaces").interfaces.BindingWhenOnSyntax<TItem>;
17
+ /** Register a curried function */
18
+ functionCurry<TFn extends Func>(item: TFn, params: TupleToKeysOfMapped<CollectCurriedParams<TFn, TItem>, T & TDep>): import("inversify/dts/interfaces/interfaces").interfaces.BindingWhenOnSyntax<TItem>;
19
+ /** Register a class */
20
+ class<C extends NewableOf<TItem>>(cls: C, params: TupleToKeysOfMapped<ConstructorParameters<C>, T & TDep>): import("./inversify").BindingInWhenOnSyntax<InstanceType<C>>;
21
+ }
22
+ export {};
package/lib/binder.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Binder = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var curry_1 = require("./curry");
6
+ var inversify_1 = require("./inversify");
7
+ var internalContainer = (0, inversify_1.getGlobalContainer)();
8
+ var Binder = /** @class */ (function () {
9
+ function Binder(key, keyTransformer) {
10
+ this.key = key;
11
+ this.keyTransformer = keyTransformer;
12
+ }
13
+ Object.defineProperty(Binder.prototype, "binding", {
14
+ /** Gets the underlying container binding to the specified key */
15
+ get: function () {
16
+ return internalContainer.bind(this.key);
17
+ },
18
+ enumerable: false,
19
+ configurable: true
20
+ });
21
+ /** Register a function */
22
+ Binder.prototype.function = function (item) {
23
+ return this.binding.toFunction(item);
24
+ };
25
+ /** Register a function resolver */
26
+ Binder.prototype.functionResolver = function (item) {
27
+ return this.binding.toFactory(function () { return item.resolved(); });
28
+ };
29
+ /** Register a constant */
30
+ Binder.prototype.constant = function (item) {
31
+ return this.binding.toConstantValue(item);
32
+ };
33
+ /** Register a curried function */
34
+ Binder.prototype.functionCurry = function (item, params) {
35
+ var keys = params;
36
+ return this.functionResolver(curry_1.curryInner.apply(void 0, tslib_1.__spreadArray([item], keys.map(this.keyTransformer), false)));
37
+ };
38
+ /** Register a class */
39
+ Binder.prototype.class = function (cls, params) {
40
+ var keys = params;
41
+ return (0, inversify_1.registerClass)(this.key, cls, keys.map(this.keyTransformer));
42
+ };
43
+ return Binder;
44
+ }());
45
+ exports.Binder = Binder;
package/lib/curry.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { Func, IFunctionResolver } from './tail.generated';
2
+ export declare function curryInner<TFn extends Func>(fn: TFn, ...keys: PropertyKey[]): IFunctionResolver<any>;
package/lib/curry.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.curryInner = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var inversify_1 = require("./inversify");
6
+ var internalContainer = (0, inversify_1.getGlobalContainer)();
7
+ function curryInner(fn) {
8
+ var keys = [];
9
+ for (var _i = 1; _i < arguments.length; _i++) {
10
+ keys[_i - 1] = arguments[_i];
11
+ }
12
+ var curried;
13
+ return {
14
+ resolved: function () {
15
+ if (!curried) {
16
+ var values_1 = keys.map(function (k) { return internalContainer.get(k); });
17
+ curried = function () {
18
+ var p = [];
19
+ for (var _i = 0; _i < arguments.length; _i++) {
20
+ p[_i] = arguments[_i];
21
+ }
22
+ return fn.apply(fn, tslib_1.__spreadArray(tslib_1.__spreadArray([], values_1, true), p, true));
23
+ };
24
+ }
25
+ return curried;
26
+ },
27
+ };
28
+ }
29
+ exports.curryInner = curryInner;