@rocketmakers/ioc 0.1.6 → 0.2.6

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
@@ -10,12 +10,16 @@ This package provides helper methods for inversify (https://inversify.io/) to:
10
10
 
11
11
  You should first create a type that describes your Container items, then create a `containerRegister` to help you to register those types.
12
12
 
13
+ ## Application
13
14
 
14
15
  `service/admin.ts`
15
16
  ```TypeScript
16
17
  export class AdminService {
17
- constructor() {}
18
+ doSomething(value: number) {
19
+ return Math.round(value);
20
+ }
18
21
  }
22
+
19
23
  ```
20
24
 
21
25
  `controller/admin.ts`
@@ -23,68 +27,135 @@ export class AdminService {
23
27
  import { AdminService } from '../service/admin';
24
28
 
25
29
  export class AdminController {
26
- constructor(private service: AdminService) {}
30
+ constructor(private adminService: AdminService) {}
27
31
  }
32
+
28
33
  ```
29
34
 
30
35
  `service/external-api.ts`
31
36
  ```TypeScript
37
+ import { fetchUserData, IUser } from './external';
38
+
32
39
  // NOTE: we want the apiKey param to be provided (curried) from the container - so we never need to explicitly pass this when consuming this function!
33
- export function getExternalData(apiKey: string, id: number): number {
34
- // ...
40
+ export function getExternalUserData(apiKey: string, id: number): IUser {
41
+ return fetchUserData(apiKey, id);
35
42
  }
36
- ```
37
43
 
38
- `ioc/containerRegistration.ts`
44
+ ```
45
+ `ioc/types.ts`
39
46
  ```TypeScript
40
- import type { AdminService } from '../service/admin';
41
- import type { getExternalData } from '../service/external-api';
42
- import type { AdminController } from '../controller/admin';
47
+ import { Curried1 } from '@rocketmakers/ioc';
43
48
 
44
- import { containerRegister, containerRegisterFunctionResolver, containerFactory, Curried1 } from '@rocketmakers/ioc';
49
+ // Application Types
50
+ import type { AdminController } from '../controller/admin';
51
+ import type { AdminService } from '../service/admin';
52
+ import type { getExternalUserData } from '../service/external-api';
45
53
 
54
+ /** Define the types registered within the container */
46
55
  export type ControllerContainerTypes = {
47
56
  adminService: AdminService;
48
57
  adminController: AdminController;
49
58
  apiKey: string;
50
- getExternalData: Curried1<typeof getExternalData>
59
+ getExternalUserData: Curried1<typeof getExternalUserData>;
51
60
  };
52
61
 
53
- export const register = containerRegister<ControllerContainerTypes>();
54
- export const functionResolver = containerRegisterFunctionResolver<ContainerTypes>();
55
- export const container = containerFactory<ContainerTypes>();
56
62
  ```
57
63
 
58
- ## Registration
64
+ ## Registration (IoC Container Modules)
59
65
 
66
+ ### Building the container
60
67
 
61
68
  `ioc/register.ts`
62
69
  ```TypeScript
70
+ import { containerRegisterFunctionResolver, IContainer, register } from '@rocketmakers/ioc';
71
+
72
+ import { AdminController } from '../controller/admin';
63
73
  import { AdminService } from '../service/admin';
64
- import { getExternalData } from '../service/external-api';
74
+ import { getExternalUserData } from '../service/external-api';
75
+ import { ControllerContainerTypes } from './types';
76
+
77
+ export const functionResolver = containerRegisterFunctionResolver<ControllerContainerTypes>();
78
+
79
+ export function makeContainer(): IContainer<ControllerContainerTypes> {
80
+ // Register all the keys of the 'ControllerContainerTypes'
81
+ return register<ControllerContainerTypes>({
82
+ adminController: b => b.class(AdminController).construct(['adminService']),
83
+ adminService: b => b.class(AdminService).construct([]),
84
+ apiKey: b => b.constant('xyz...'),
85
+ getExternalUserData: b => b.functionResolver(functionResolver.curry1(getExternalUserData, 'apiKey')),
86
+ });
87
+ }
88
+
89
+ ```
90
+
91
+ ### Resolving with the container
92
+
93
+ ```TypeScript
94
+ import { makeContainer } from './ioc/register-new';
95
+
96
+ export function boot() {
97
+ // Register the container
98
+ const container = makeContainer();
99
+
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);
104
+ return userData;
105
+ }
106
+
107
+ boot();
108
+
109
+ ```
110
+
111
+
112
+ ## Registration (Legacy)
113
+
114
+ ### Building the container
115
+
116
+ `ioc/register.ts`
117
+ ```TypeScript
118
+ import { containerFactory, containerRegister, containerRegisterFunctionResolver } from '@rocketmakers/ioc';
119
+
120
+ // Application Types
65
121
  import { AdminController } from '../controller/admin';
122
+ import { AdminService } from '../service/admin';
123
+ import { getExternalUserData } from '../service/external-api';
124
+ import { ControllerContainerTypes } from './types';
66
125
 
67
- import { register, functionResolver } from './containerRegistration';
126
+ // Create legacy helper functions
127
+ export const register = containerRegister<ControllerContainerTypes>();
128
+ export const functionResolver = containerRegisterFunctionResolver<ControllerContainerTypes>();
129
+ export const container = containerFactory<ControllerContainerTypes>();
68
130
 
69
- export function registerIoC() {
131
+ export function registerIoC(): void {
132
+ // Register all the keys of the 'ControllerContainerTypes'
70
133
  register.class('adminService', AdminService).construct([]).inSingletonScope();
71
134
  register.class('adminController', AdminController).construct(['adminService']).inSingletonScope();
72
-
73
135
  register.constant('apiKey', 'xyz...');
74
-
75
- register.function('getExternalData', functionResolver.curry1(getExternalData, 'apiKey'));
136
+ register.function('getExternalUserData', functionResolver.curry1(getExternalUserData, 'apiKey'));
76
137
  }
138
+
77
139
  ```
78
140
 
79
- ## Resolving with the container
141
+ ### Resolving with the container
80
142
 
81
143
  ```TypeScript
82
- import { container } from '../ioc/containerRegistration';
144
+ import { container, registerIoC } from './ioc/register';
83
145
 
84
- export function doSomething() {
146
+ export function boot() {
147
+ // Register the container
148
+ registerIoC();
85
149
 
86
- const fn = container.get('getExternalData'); // fn has type "(id: number): number" as we curried out the apiKey param
87
- const extData = fn(23); // returns a "number"
88
- // ...
150
+ // getExternalUserData has type "(id: number): IUser" as we curried out the apiKey param
151
+ const getExternalUserData = container.get('getExternalUserData');
152
+
153
+ const userData = getExternalUserData(23);
154
+ return userData;
89
155
  }
156
+
157
+ boot();
158
+
90
159
  ```
160
+
161
+
@@ -0,0 +1,143 @@
1
+ /** Returns a mapped type that removes the first element from the type array */
2
+ export declare type Tail1<T extends any[]> = ((...t: T) => any) extends (_1: any, ...tail: infer TTail) => any ? TTail : [
3
+ ];
4
+ /** Returns a mapped type that removes the first 2 elements from the type array */
5
+ export declare type Tail2<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, ...tail: infer TTail) => any ? TTail : [
6
+ ];
7
+ /** Returns a mapped type that removes the first 3 elements from the type array */
8
+ export declare type Tail3<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, ...tail: infer TTail) => any ? TTail : [
9
+ ];
10
+ /** Returns a mapped type that removes the first 4 elements from the type array */
11
+ export declare type Tail4<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, ...tail: infer TTail) => any ? TTail : [
12
+ ];
13
+ /** Returns a mapped type that removes the first 5 elements from the type array */
14
+ export declare type Tail5<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, ...tail: infer TTail) => any ? TTail : [
15
+ ];
16
+ /** Returns a mapped type that removes the first 6 elements from the type array */
17
+ export declare type Tail6<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, ...tail: infer TTail) => any ? TTail : [
18
+ ];
19
+ /** Returns a mapped type that removes the first 7 elements from the type array */
20
+ export declare type Tail7<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, ...tail: infer TTail) => any ? TTail : [
21
+ ];
22
+ /** Returns a mapped type that removes the first 8 elements from the type array */
23
+ export declare type Tail8<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, ...tail: infer TTail) => any ? TTail : [
24
+ ];
25
+ /** Returns a mapped type that removes the first 9 elements from the type array */
26
+ export declare type Tail9<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, ...tail: infer TTail) => any ? TTail : [
27
+ ];
28
+ /** Returns a mapped type that removes the first 10 elements from the type array */
29
+ export declare type Tail10<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, ...tail: infer TTail) => any ? TTail : [
30
+ ];
31
+ /** Returns a mapped type that removes the first 11 elements from the type array */
32
+ export declare type Tail11<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, ...tail: infer TTail) => any ? TTail : [
33
+ ];
34
+ /** Returns a mapped type that removes the first 12 elements from the type array */
35
+ export declare type Tail12<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, ...tail: infer TTail) => any ? TTail : [
36
+ ];
37
+ /** Returns a mapped type that removes the first 13 elements from the type array */
38
+ export declare type Tail13<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, ...tail: infer TTail) => any ? TTail : [
39
+ ];
40
+ /** Returns a mapped type that removes the first 14 elements from the type array */
41
+ export declare type Tail14<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, _14: any, ...tail: infer TTail) => any ? TTail : [
42
+ ];
43
+ /** Returns a mapped type that removes the first 15 elements from the type array */
44
+ export declare type Tail15<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, _14: any, _15: any, ...tail: infer TTail) => any ? TTail : [
45
+ ];
46
+ /** Returns a mapped type that removes the first 16 elements from the type array */
47
+ export declare type Tail16<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, _14: any, _15: any, _16: any, ...tail: infer TTail) => any ? TTail : [
48
+ ];
49
+ /** Returns a mapped type that removes the first 17 elements from the type array */
50
+ export declare type Tail17<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, _14: any, _15: any, _16: any, _17: any, ...tail: infer TTail) => any ? TTail : [
51
+ ];
52
+ /** Returns a mapped type that removes the first 18 elements from the type array */
53
+ export declare type Tail18<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, _14: any, _15: any, _16: any, _17: any, _18: any, ...tail: infer TTail) => any ? TTail : [
54
+ ];
55
+ /** Returns a mapped type that removes the first 19 elements from the type array */
56
+ export declare type Tail19<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, _14: any, _15: any, _16: any, _17: any, _18: any, _19: any, ...tail: infer TTail) => any ? TTail : [
57
+ ];
58
+ /** Returns a mapped type that removes the first 20 elements from the type array */
59
+ export declare type Tail20<T extends any[]> = ((...t: T) => any) extends (_1: any, _2: any, _3: any, _4: any, _5: any, _6: any, _7: any, _8: any, _9: any, _10: any, _11: any, _12: any, _13: any, _14: any, _15: any, _16: any, _17: any, _18: any, _19: any, _20: any, ...tail: infer TTail) => any ? TTail : [
60
+ ];
61
+ /** Returns a mapped type of the function that removes the first parameter from the method */
62
+ export declare type Curried1<T extends (...args: any[]) => any> = (...tail: Tail1<Parameters<T>>) => ReturnType<T>;
63
+ /** Returns a mapped type of the function that removes the first 2 parameters from the method */
64
+ export declare type Curried2<T extends (...args: any[]) => any> = (...tail: Tail2<Parameters<T>>) => ReturnType<T>;
65
+ /** Returns a mapped type of the function that removes the first 3 parameters from the method */
66
+ export declare type Curried3<T extends (...args: any[]) => any> = (...tail: Tail3<Parameters<T>>) => ReturnType<T>;
67
+ /** Returns a mapped type of the function that removes the first 4 parameters from the method */
68
+ export declare type Curried4<T extends (...args: any[]) => any> = (...tail: Tail4<Parameters<T>>) => ReturnType<T>;
69
+ /** Returns a mapped type of the function that removes the first 5 parameters from the method */
70
+ export declare type Curried5<T extends (...args: any[]) => any> = (...tail: Tail5<Parameters<T>>) => ReturnType<T>;
71
+ /** Returns a mapped type of the function that removes the first 6 parameters from the method */
72
+ export declare type Curried6<T extends (...args: any[]) => any> = (...tail: Tail6<Parameters<T>>) => ReturnType<T>;
73
+ /** Returns a mapped type of the function that removes the first 7 parameters from the method */
74
+ export declare type Curried7<T extends (...args: any[]) => any> = (...tail: Tail7<Parameters<T>>) => ReturnType<T>;
75
+ /** Returns a mapped type of the function that removes the first 8 parameters from the method */
76
+ export declare type Curried8<T extends (...args: any[]) => any> = (...tail: Tail8<Parameters<T>>) => ReturnType<T>;
77
+ /** Returns a mapped type of the function that removes the first 9 parameters from the method */
78
+ export declare type Curried9<T extends (...args: any[]) => any> = (...tail: Tail9<Parameters<T>>) => ReturnType<T>;
79
+ /** Returns a mapped type of the function that removes the first 10 parameters from the method */
80
+ export declare type Curried10<T extends (...args: any[]) => any> = (...tail: Tail10<Parameters<T>>) => ReturnType<T>;
81
+ /** Returns a mapped type of the function that removes the first 11 parameters from the method */
82
+ export declare type Curried11<T extends (...args: any[]) => any> = (...tail: Tail11<Parameters<T>>) => ReturnType<T>;
83
+ /** Returns a mapped type of the function that removes the first 12 parameters from the method */
84
+ export declare type Curried12<T extends (...args: any[]) => any> = (...tail: Tail12<Parameters<T>>) => ReturnType<T>;
85
+ /** Returns a mapped type of the function that removes the first 13 parameters from the method */
86
+ export declare type Curried13<T extends (...args: any[]) => any> = (...tail: Tail13<Parameters<T>>) => ReturnType<T>;
87
+ /** Returns a mapped type of the function that removes the first 14 parameters from the method */
88
+ export declare type Curried14<T extends (...args: any[]) => any> = (...tail: Tail14<Parameters<T>>) => ReturnType<T>;
89
+ /** Returns a mapped type of the function that removes the first 15 parameters from the method */
90
+ export declare type Curried15<T extends (...args: any[]) => any> = (...tail: Tail15<Parameters<T>>) => ReturnType<T>;
91
+ /** Returns a mapped type of the function that removes the first 16 parameters from the method */
92
+ export declare type Curried16<T extends (...args: any[]) => any> = (...tail: Tail16<Parameters<T>>) => ReturnType<T>;
93
+ /** Returns a mapped type of the function that removes the first 17 parameters from the method */
94
+ export declare type Curried17<T extends (...args: any[]) => any> = (...tail: Tail17<Parameters<T>>) => ReturnType<T>;
95
+ /** Returns a mapped type of the function that removes the first 18 parameters from the method */
96
+ export declare type Curried18<T extends (...args: any[]) => any> = (...tail: Tail18<Parameters<T>>) => ReturnType<T>;
97
+ /** Returns a mapped type of the function that removes the first 19 parameters from the method */
98
+ export declare type Curried19<T extends (...args: any[]) => any> = (...tail: Tail19<Parameters<T>>) => ReturnType<T>;
99
+ /** Returns a mapped type of the function that removes the first 20 parameters from the method */
100
+ export declare type Curried20<T extends (...args: any[]) => any> = (...tail: Tail20<Parameters<T>>) => ReturnType<T>;
101
+ /** Returns the type in position 1 of the type array */
102
+ export declare type Param1<T extends any[]> = T extends [
103
+ any,
104
+ ...any[]
105
+ ] ? T[0] : never;
106
+ /** Returns the type in position 2 of the type array */
107
+ export declare type Param2<T extends any[]> = Param1<Tail1<T>>;
108
+ /** Returns the type in position 3 of the type array */
109
+ export declare type Param3<T extends any[]> = Param1<Tail2<T>>;
110
+ /** Returns the type in position 4 of the type array */
111
+ export declare type Param4<T extends any[]> = Param1<Tail3<T>>;
112
+ /** Returns the type in position 5 of the type array */
113
+ export declare type Param5<T extends any[]> = Param1<Tail4<T>>;
114
+ /** Returns the type in position 6 of the type array */
115
+ export declare type Param6<T extends any[]> = Param1<Tail5<T>>;
116
+ /** Returns the type in position 7 of the type array */
117
+ export declare type Param7<T extends any[]> = Param1<Tail6<T>>;
118
+ /** Returns the type in position 8 of the type array */
119
+ export declare type Param8<T extends any[]> = Param1<Tail7<T>>;
120
+ /** Returns the type in position 9 of the type array */
121
+ export declare type Param9<T extends any[]> = Param1<Tail8<T>>;
122
+ /** Returns the type in position 10 of the type array */
123
+ export declare type Param10<T extends any[]> = Param1<Tail9<T>>;
124
+ /** Returns the type in position 11 of the type array */
125
+ export declare type Param11<T extends any[]> = Param1<Tail10<T>>;
126
+ /** Returns the type in position 12 of the type array */
127
+ export declare type Param12<T extends any[]> = Param1<Tail11<T>>;
128
+ /** Returns the type in position 13 of the type array */
129
+ export declare type Param13<T extends any[]> = Param1<Tail12<T>>;
130
+ /** Returns the type in position 14 of the type array */
131
+ export declare type Param14<T extends any[]> = Param1<Tail13<T>>;
132
+ /** Returns the type in position 15 of the type array */
133
+ export declare type Param15<T extends any[]> = Param1<Tail14<T>>;
134
+ /** Returns the type in position 16 of the type array */
135
+ export declare type Param16<T extends any[]> = Param1<Tail15<T>>;
136
+ /** Returns the type in position 17 of the type array */
137
+ export declare type Param17<T extends any[]> = Param1<Tail16<T>>;
138
+ /** Returns the type in position 18 of the type array */
139
+ export declare type Param18<T extends any[]> = Param1<Tail17<T>>;
140
+ /** Returns the type in position 19 of the type array */
141
+ export declare type Param19<T extends any[]> = Param1<Tail18<T>>;
142
+ /** Returns the type in position 20 of the type array */
143
+ export declare type Param20<T extends any[]> = Param1<Tail19<T>>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/error.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare class IocDuplicateKeyError extends Error {
2
+ duplicateKeys: string[];
3
+ constructor(duplicateKeys: string[]);
4
+ }
package/lib/error.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IocDuplicateKeyError = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var IocDuplicateKeyError = /** @class */ (function (_super) {
6
+ tslib_1.__extends(IocDuplicateKeyError, _super);
7
+ function IocDuplicateKeyError(duplicateKeys) {
8
+ var _newTarget = this.constructor;
9
+ var _this = _super.call(this) || this;
10
+ _this.duplicateKeys = duplicateKeys;
11
+ Object.setPrototypeOf(_this, _newTarget.prototype);
12
+ _this.message = "Duplicate keys found\n".concat(duplicateKeys.join("\n"));
13
+ if (Error.captureStackTrace !== undefined) {
14
+ Error.captureStackTrace(_this, _this.constructor);
15
+ }
16
+ return _this;
17
+ }
18
+ return IocDuplicateKeyError;
19
+ }(Error));
20
+ exports.IocDuplicateKeyError = IocDuplicateKeyError;
@@ -0,0 +1,9 @@
1
+ import { Container } from "inversify";
2
+ declare global {
3
+ namespace NodeJS {
4
+ interface Global {
5
+ _rocketmakers_ioc: Container;
6
+ }
7
+ }
8
+ }
9
+ export declare function getGlobalContainer(): Container;
package/lib/global.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getGlobalContainer = void 0;
4
+ /* eslint-disable no-underscore-dangle */
5
+ /* eslint-disable @typescript-eslint/no-namespace */
6
+ var inversify_1 = require("inversify");
7
+ function getGlobalContainer() {
8
+ if (!global._rocketmakers_ioc) {
9
+ global._rocketmakers_ioc = new inversify_1.Container();
10
+ }
11
+ return global._rocketmakers_ioc;
12
+ }
13
+ exports.getGlobalContainer = getGlobalContainer;