Skip to content

Commit

Permalink
Merge pull request #9 from zgid123/core-feat/build-simple-ioc
Browse files Browse the repository at this point in the history
[core]: build simple ioc container for di
  • Loading branch information
zgid123 authored Jul 1, 2024
2 parents af4f9a0 + 32b63ee commit 51cacbb
Show file tree
Hide file tree
Showing 14 changed files with 661 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
node_modules
.turbo
lib
dist
docs/.vitepress/cache
docs/.vitepress/dist
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@config/tsconfig": "workspace:^",
"@swc/core": "^1.6.6",
"@types/globals": "workspace:^",
"@vitest/coverage-istanbul": "^1.6.0",
"globals": "^15.6.0",
"globals": "^15.7.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.7",
"rimraf": "^5.0.7",
Expand Down
4 changes: 2 additions & 2 deletions packages/@config/tsconfig/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"isolatedModules": true,
"module": "ESNext",
"moduleResolution": "node",
"noEmit": true,
"moduleResolution": "Node",
"noUncheckedIndexedAccess": true,
"removeComments": true,
"resolveJsonModule": true,
Expand Down
10 changes: 7 additions & 3 deletions packages/@core/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"name": "@abyss.ts/core",
"version": "0.0.1",
"version": "0.0.2",
"author": "Alpha",
"repository": {
"type": "git",
"url": "git+https://github.com/zgid123/abyss-ts.git"
},
"license": "MIT",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "src/index.ts",
"types": "src/index.ts",
"publishConfig": {
"main": "lib/index.js",
"types": "lib/index.d.ts"
},
"keywords": ["abyss", "abyssts", "abyssjs", "abyss.js", "abyss.ts", "node"],
"scripts": {
"prepublish": "pnpm build",
Expand Down
21 changes: 20 additions & 1 deletion packages/@core/src/__tests__/constants/metadata.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { describe, expect, it } from 'vitest';

import { ACTIONS, CONTROLLER, PARAMS } from '../../constants/metadata';
import {
INJECT,
PARAMS,
ACTIONS,
INJECTABLE,
CONTROLLER,
IOC_CONTAINER,
} from '../../constants/metadata';

describe('[constants]: metadata', () => {
it('has CONTROLLER as __controllerMetadata__', () => {
Expand All @@ -14,4 +21,16 @@ describe('[constants]: metadata', () => {
it('has PARAMS as __actionParamsMetadata__', () => {
expect(PARAMS).toBe('__actionParamsMetadata__');
});

it('has INJECT as __injectMetadata__', () => {
expect(INJECT).toBe('__injectMetadata__');
});

it('has INJECTABLE as __injectableMetadata__', () => {
expect(INJECTABLE).toBe('__injectableMetadata__');
});

it('has IOC_CONTAINER as __iocContainer__', () => {
expect(IOC_CONTAINER).toBe('__iocContainer__');
});
});
127 changes: 127 additions & 0 deletions packages/@core/src/__tests__/decorators/injection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { describe, expect, it, suite, afterEach } from 'vitest';

import { IoC } from '../../core/IoC';
import { INJECT, INJECTABLE } from '../../constants/metadata';
import { Inject, Injectable } from '../../decorators/injection';

describe('[decorators]: injection', () => {
suite('Injectable', () => {
afterEach(() => {
Reflect.deleteMetadata(INJECTABLE, IoC);
});

suite('without scope', () => {
it('sets metadata for MyService class', () => {
@Injectable()
class MyService {}

expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([
{
scope: 'singleton',
target: MyService,
},
]);
});
});

suite('with scope scoped', () => {
it('sets metadata for MyService class', () => {
@Injectable({
scope: 'scoped',
})
class MyService {}

expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([
{
scope: 'scoped',
target: MyService,
},
]);
});
});

suite('with scope singleton', () => {
it('sets metadata for MyService class', () => {
@Injectable({
scope: 'singleton',
})
class MyService {}

expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([
{
scope: 'singleton',
target: MyService,
},
]);
});
});

suite('with scope transient', () => {
it('sets metadata for MyService class', () => {
@Injectable({
scope: 'transient',
})
class MyService {}

expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([
{
scope: 'transient',
target: MyService,
},
]);
});
});
});

suite('Inject', () => {
class InjectService {}

class AnotherInjectService {}

suite('with one param', () => {
it("sets metadata for MyService class's constructor", () => {
class MyService {
constructor(
@Inject(InjectService)
// biome-ignore lint/correctness/noUnusedPrivateClassMembers: <explanation>
private readonly injectService: InjectService,
) {}
}

expect(
Reflect.getMetadata(INJECT, MyService, 'constructor'),
).toStrictEqual([
{
extractor: InjectService,
},
]);
});
});

suite('with two params', () => {
it("sets metadata for MyService class's constructor", () => {
class MyService {
constructor(
@Inject(InjectService)
// biome-ignore lint/correctness/noUnusedPrivateClassMembers: <explanation>
private readonly injectService: InjectService,
@Inject(AnotherInjectService)
// biome-ignore lint/correctness/noUnusedPrivateClassMembers: <explanation>
private readonly anotherInjectService: AnotherInjectService,
) {}
}

expect(
Reflect.getMetadata(INJECT, MyService, 'constructor'),
).toStrictEqual([
{
extractor: InjectService,
},
{
extractor: AnotherInjectService,
},
]);
});
});
});
});
Loading

0 comments on commit 51cacbb

Please sign in to comment.