first commit

This commit is contained in:
编码猿
2024-09-27 01:24:57 +08:00
commit b6fac0aad8
16 changed files with 252 additions and 0 deletions

23
.gitignore vendored Normal file
View File

@@ -0,0 +1,23 @@
.DS_Store
node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

3
.npmignore Normal file
View File

@@ -0,0 +1,3 @@
src/
tsconfig.json
test.ts

1
.pydio Normal file
View File

@@ -0,0 +1 @@
b387b77e-e586-4b21-911b-b40e25519cbd

38
README.md Normal file
View File

@@ -0,0 +1,38 @@
> 一款适用于Typescript项目的简易依赖注入插件
**只支持类的属性进行注入,本人使用最多的也是这种方式**
## 安装
```bash
npm i --save bmydi
```
## 使用
- @Injectable():加在需要被注入的类上,主要作用是收集依赖
- @Inject():放在类的属性上,会把`@Injectable()`的类注入到属性上
示例:
```typescript
import { Inject, Injectable } from "bmydi";
@Injectable()
class Demo1 {
public hello: string = "我是Demo1类"
}
class Demo2 {
@Inject()
public test!: Demo1;
public GetTest(): string {
return this.test.hello
}
}
var a = new Demo2();
console.log(a.GetTest()); // 返回:"我是Demo1类"
```

6
index.js Normal file
View File

@@ -0,0 +1,6 @@
const { Injectable, Inject } = require("./dist/annotation/Ioc");
module.exports = {
Injectable,
Inject
};

34
package-lock.json generated Normal file
View File

@@ -0,0 +1,34 @@
{
"name": "bmydi",
"version": "1.2.5",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/node": {
"version": "13.13.52",
"resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.52.tgz",
"integrity": "sha512-s3nugnZumCC//n4moGGe6tkNMyYEdaDBitVjwPxXmR5lnMG5dHePinH2EdxkG3Rh1ghFHHixAG4NJhpJW1rthQ==",
"dev": true
},
"bmydi": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/bmydi/-/bmydi-1.2.5.tgz",
"integrity": "sha512-yT9vPNYTO4XaUrkxw5GsXHPDVRG6altCG/aEnZhoYMkEelqoFk6TC7ekIJQrJkIXdNPUvZsCKC/m91E55+YVug==",
"requires": {
"bmydi": "^1.0.0",
"reflect-metadata": "^0.1.13",
"tslib": "^1.10.0"
}
},
"reflect-metadata": {
"version": "0.1.13",
"resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz",
"integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg=="
},
"tslib": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg=="
}
}
}

23
package.json Normal file
View File

@@ -0,0 +1,23 @@
{
"name": "bmydi",
"version": "1.2.5",
"description": "一款适用于Typescript项目的简易依赖注入插件",
"main": "index.js",
"typings": "types/Ioc.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"依赖注入di"
],
"author": "bmy",
"license": "ISC",
"dependencies": {
"bmydi": "^1.0.0",
"reflect-metadata": "^0.1.13",
"tslib": "^1.10.0"
},
"devDependencies": {
"@types/node": "^13.7.1"
}
}

1
src/.pydio Normal file
View File

@@ -0,0 +1 @@
3da6b690-a18b-4ac9-9765-ae048d66fb04

1
src/annotation/.pydio Normal file
View File

@@ -0,0 +1 @@
858a4c99-abd2-4088-a94f-6c26d93f15e6

36
src/annotation/Ioc.ts Normal file
View File

@@ -0,0 +1,36 @@
import "reflect-metadata";
import IocModel from "../model/Ioc.model";
/**
* 收集类依赖
* @constructor
*/
export function Injectable() {
return (_constructor: { new (...args: any[]): {}; }) => {
if(IocModel.classPool.indexOf(_constructor) !== -1) {
throw new Error('无需重复收集类');
} else {
//注册
IocModel.classPool = [_constructor]
}
}
}
/**
* 将类依赖实例化然后注入到被装饰的属性中
* @constructor
*/
export function Inject() {
return function (target: any, propertyName: string) {
/**
* 使用 reflect-metadata 提供的内置 类型元数据键 design:type 通过反射拿到被装饰属性的类型
* 也就是 类属性要实例化 的 service 类
*/
const propertyType: any = Reflect.getMetadata('design:type', target, propertyName);
if (IocModel.classPool.indexOf(propertyType) == -1) {
throw new Error('被装饰的属性所属的变量类型类,没有被装饰器@Injectable()注入,请检查!');
} else {
// 从存取器的数组中通过下标取出被装饰属性对应的service类然后实例化这个类在放入被装饰的属性中
target[propertyName] = new (IocModel.classPool[IocModel.classPool.indexOf(propertyType)])()
}
}
}

1
src/model/.pydio Normal file
View File

@@ -0,0 +1 @@
13fa22d6-8bcf-42e8-9e69-2357f64cdd2c

13
src/model/Ioc.model.ts Normal file
View File

@@ -0,0 +1,13 @@
class IocModel {
private _classPool: Array<{ new (...args: any[]): {}; }> = [];
get classPool(): Array<{ new(...args: any[]): {} }> {
return this._classPool;
}
set classPool(value: Array<{ new(...args: any[]): {} }>) {
this._classPool = [...value, ...this._classPool]
}
}
export default new IocModel();

20
test.ts Normal file
View File

@@ -0,0 +1,20 @@
import { Inject, Injectable } from "bmydi";
@Injectable()
class Demo1 {
public hello: string = "我是Demo1类"
}
class Demo2 {
@Inject()
public test!: Demo1;
public GetTest(): string {
return this.test.hello // 返回:"我是Demo1类"
}
}
var a = new Demo2();
console.log(a.GetTest());

40
tsconfig.json Normal file
View File

@@ -0,0 +1,40 @@
{
"compilerOptions": {
"outDir": "dist/",
"target": "es5",
"module": "commonjs",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"declarationDir": "types",
"sourceMap": false,
"baseUrl": ".",
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx", "test.ts"
],
"exclude": [
"node_modules"
]
}

1
types/.pydio Normal file
View File

@@ -0,0 +1 @@
72be7c1c-833c-4abc-95de-91a6d1b19824

11
types/Ioc.d.ts vendored Normal file
View File

@@ -0,0 +1,11 @@
import "reflect-metadata";
/**
* 收集类依赖
* @constructor
*/
export declare function Injectable(): (_constructor: new (...args: any[]) => {}) => void;
/**
* 将类依赖实例化然后注入到被装饰的属性中
* @constructor
*/
export declare function Inject(): (target: any, propertyName: string) => void;