UNPKG

2.14 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/fold
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.inject = void 0;
12/**
13 * Injects bindings to the class constructor
14 */
15function inject(value) {
16 // eslint-disable-next-line no-redeclare
17 function decorator(target, propertyKey) {
18 /**
19 * Consturctor injections
20 */
21 if (!propertyKey) {
22 if (!target.hasOwnProperty('inject')) {
23 Object.defineProperty(target, 'inject', {
24 value: {},
25 });
26 }
27 target.inject.instance = target.inject.instance || [];
28 const constructorParams = Reflect.getMetadata('design:paramtypes', target);
29 if (constructorParams) {
30 constructorParams.forEach((param, index) => {
31 if (value && value[index]) {
32 target.inject.instance.push(value[index]);
33 }
34 else {
35 target.inject.instance.push(param);
36 }
37 });
38 }
39 return;
40 }
41 /**
42 * Parameter injections
43 */
44 if (!target.constructor.hasOwnProperty('inject')) {
45 Object.defineProperty(target.constructor, 'inject', {
46 value: {},
47 });
48 }
49 target.constructor.inject[propertyKey] = target.constructor.inject[propertyKey] || [];
50 const methodParams = Reflect.getMetadata('design:paramtypes', target, propertyKey);
51 if (methodParams) {
52 methodParams.forEach((param, index) => {
53 if (value && value[index]) {
54 target.constructor.inject[propertyKey].push(value[index]);
55 }
56 else {
57 target.constructor.inject[propertyKey].push(param);
58 }
59 });
60 }
61 }
62 return decorator;
63}
64exports.inject = inject;