-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
55 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {isInstanceOf, isObject} from "@typesafeunit/util"; | ||
import {IServiceResolver} from "../interfaces"; | ||
import {Service} from "./Service"; | ||
|
||
export const SERVICE_KIND = Symbol.for("SERVICE_KIND"); | ||
|
||
export function isService<T>(maybe: unknown): maybe is IServiceResolver<T> { | ||
if (!isObject(maybe)) { | ||
return false; | ||
} | ||
|
||
return isInstanceOf(maybe, Service) | ||
|| (SERVICE_KIND in maybe && "resolve" in maybe); | ||
} | ||
|
||
export const resolve: PropertyDecorator = (p, k) => { | ||
Service.resolve()(p, k); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./Service"; | ||
export * from "./ServiceFactory"; | ||
export * from "./resolve"; | ||
export * from "./fn"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
import {ServiceFactory} from "@typesafeunit/unit"; | ||
import {MemoryDb} from "../../../test/src/context/services/MemoryDb"; | ||
import {MemoryDbServiceResolver} from "../../../test/src/context/services/MemoryDbServiceResolver"; | ||
import {Context} from "../../src/Context"; | ||
import {Service, SERVICE_KIND} from "../../src/Service"; | ||
|
||
const table: [string, (v: string) => any][] = [ | ||
["p1", (p) => new MemoryDbServiceResolver(p)], | ||
["p2", (p) => new ServiceFactory(() => MemoryDb.connect(p))], | ||
["p3", (p) => ServiceFactory.create(() => MemoryDb.connect(p))], | ||
["p4", (p) => ServiceFactory.create(MemoryDb.connect(p))], | ||
]; | ||
describe("Service", () => { | ||
const table: [string, (v: string) => any][] = [ | ||
["p1", (p) => new MemoryDbServiceResolver(p)], | ||
["p2", (p) => new ServiceFactory(() => MemoryDb.connect(p))], | ||
["p3", (p) => ServiceFactory.create(() => MemoryDb.connect(p))], | ||
["p4", (p) => ServiceFactory.create(MemoryDb.connect(p))], | ||
]; | ||
|
||
test("Service", async () => { | ||
for (const [p, f] of table) { | ||
const service = await f(p).resolve(); | ||
expect(service).toBeInstanceOf(MemoryDb); | ||
expect(service.prefix).toBe(p); | ||
} | ||
test("Resolve", async () => { | ||
for (const [p, f] of table) { | ||
const service = await f(p).resolve(); | ||
expect(service).toBeInstanceOf(MemoryDb); | ||
expect(service.prefix).toBe(p); | ||
} | ||
}); | ||
|
||
test("Ref", async () => { | ||
const service = { | ||
[SERVICE_KIND]: true, | ||
async resolve() { | ||
return {service: "test"}; | ||
}, | ||
}; | ||
|
||
expect(await Context.resolve(service)).toEqual({service: "test"}); | ||
}); | ||
}); |