Skip to content

Commit 18fa826

Browse files
committed
feat: 🎸 improve ObjValue as a router API
1 parent d6a0e0e commit 18fa826

File tree

6 files changed

+81
-9
lines changed

6 files changed

+81
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@jsonjoy.com/util": "^1.9.0",
6565
"sonic-forest": "^1.2.1",
6666
"thingies": "^2.5.0",
67-
"tree-dump": "^1.0.3"
67+
"tree-dump": "^1.1.0"
6868
},
6969
"devDependencies": {
7070
"@biomejs/biome": "^1.9.3",

src/value/ObjValue.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {printTree} from 'tree-dump/lib/printTree';
1+
import {ModuleType} from '../type/classes/ModuleType';
2+
import {Value} from './Value';
3+
import {FnValue} from './FnValue';
24
import type {Printable} from 'tree-dump/lib/types';
35
import type * as classes from '../type';
46
import type {TypeBuilder} from '../type/TypeBuilder';
5-
import {ModuleType} from '../type/classes/ModuleType';
6-
import {Value} from './Value';
77

88
export type UnObjType<T> = T extends classes.ObjType<infer U> ? U : never;
99
export type UnObjValue<T> = T extends ObjValue<infer U> ? U : never;
@@ -14,6 +14,8 @@ export type ObjValueToTypeMap<F> = ToObject<{
1414
[K in keyof F]: ObjFieldToTuple<F[K]>;
1515
}>;
1616

17+
export type Ensure<T, X> = T extends X ? T : X;
18+
1719
export class ObjValue<T extends classes.ObjType<any>> extends Value<T> implements Printable {
1820
public static new = (system: ModuleType = new ModuleType()) => new ObjValue({}, system.t.obj);
1921

@@ -41,6 +43,15 @@ export class ObjValue<T extends classes.ObjType<any>> extends Value<T> implement
4143
return new Value(data, field.val) as any;
4244
}
4345

46+
public fn<K extends keyof ObjValueToTypeMap<UnObjType<T>>>(
47+
key: K,
48+
): FnValue<
49+
Ensure<ObjValueToTypeMap<UnObjType<T>>[K] extends classes.Type ? ObjValueToTypeMap<UnObjType<T>>[K] : classes.Type, classes.FnType<any, any, any>>
50+
> {
51+
const val = this.get(key);
52+
return new FnValue(val.data, val.type as any);
53+
}
54+
4455
public field<F extends classes.KeyType<any, any>>(
4556
field: F | ((t: TypeBuilder) => F),
4657
data: classes.ResolveType<UnObjFieldTypeVal<F>>,
@@ -78,7 +89,7 @@ export class ObjValue<T extends classes.ObjType<any>> extends Value<T> implement
7889
return this as any;
7990
}
8091

81-
public toString(tab: string = ''): string {
82-
return 'ObjValue' + printTree(tab, [(tab) => this.type!.toString(tab)]);
92+
public name(): string {
93+
return 'ObjValue';
8394
}
8495
}

src/value/Value.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {printTree} from 'tree-dump/lib/printTree';
2+
import {printJson} from 'tree-dump/lib/printJson';
23
import type {Printable} from 'tree-dump';
34
import type {ResolveType, Type} from '../type/types';
45

@@ -27,7 +28,7 @@ export class Value<T extends Type = Type> implements Printable {
2728
const type = this.type;
2829
return this.name() + (type ? printTree(tab, [
2930
(tab) => type.toString(tab),
30-
(tab) => (JSON.stringify(copyForPrint(this.data), null, 2) || 'und').replace(/"__fN---"/g, 'fn()').split('\n').join('\n' + tab),
31+
(tab) => printJson(tab, copyForPrint(this.data)).replace(/"__fN---"/g, 'fn()'),
3132
]) : '');
3233
}
3334
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {createRouter} from './ObjValue.fixtures';
2+
3+
test('can retrieve field as Value', async () => {
4+
const log = jest.fn();
5+
const router = createRouter({log});
6+
console.log(router + '');
7+
console.log(router.fn('log.message') + '');
8+
console.log(router.fn('log.message').data);
9+
console.log(router.fn('log.message').type + '');
10+
const result = await router.fn('log.message').exec({message: 'asdf'});
11+
expect(result.data).toEqual({time: expect.any(Number)});
12+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {ObjType} from "../../type";
2+
import {TypeBuilder} from "../../type/TypeBuilder";
3+
import {ObjValue} from "../ObjValue";
4+
5+
interface Services {
6+
log: (msg: string) => void;
7+
}
8+
9+
export interface RouteDeps {
10+
svc: Services;
11+
t: TypeBuilder;
12+
}
13+
export type RouterBase = ObjType<any>;
14+
export type Router<R extends RouterBase> = ObjValue<R>;
15+
16+
const addLogMessageRoute = ({t, svc}: RouteDeps) =>
17+
<R extends RouterBase>(r: Router<R>) => {
18+
return r.set('log.message', t.fn
19+
.inp(t.Object(
20+
t.Key('message', t.str),
21+
))
22+
.out(t.object({
23+
time: t.num,
24+
}))
25+
.value(({message}) => {
26+
svc.log(message);
27+
return {time: Date.now()};
28+
})
29+
)
30+
};
31+
32+
export const createRouter = (svc: Services) => {
33+
const base = ObjValue.new();
34+
const t = base.system.t;
35+
const deps: RouteDeps = {svc, t};
36+
const router = addLogMessageRoute(deps)(base);
37+
return router;
38+
};
39+

yarn.lock

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ __metadata:
889889
rxjs: "npm:^7.8.2"
890890
sonic-forest: "npm:^1.2.1"
891891
thingies: "npm:^2.5.0"
892-
tree-dump: "npm:^1.0.3"
892+
tree-dump: "npm:^1.1.0"
893893
ts-jest: "npm:^29.1.2"
894894
tslib: "npm:^2.7.0"
895895
typescript: "npm:^5.6.2"
@@ -3530,7 +3530,7 @@ __metadata:
35303530
languageName: node
35313531
linkType: hard
35323532

3533-
"tree-dump@npm:^1.0.0, tree-dump@npm:^1.0.3":
3533+
"tree-dump@npm:^1.0.0":
35343534
version: 1.0.3
35353535
resolution: "tree-dump@npm:1.0.3"
35363536
peerDependencies:
@@ -3539,6 +3539,15 @@ __metadata:
35393539
languageName: node
35403540
linkType: hard
35413541

3542+
"tree-dump@npm:^1.1.0":
3543+
version: 1.1.0
3544+
resolution: "tree-dump@npm:1.1.0"
3545+
peerDependencies:
3546+
tslib: 2
3547+
checksum: 10c0/079f0f0163b68ee2eedc65cab1de6fb121487eba9ae135c106a8bc5e4ab7906ae0b57d86016e4a7da8c0ee906da1eae8c6a1490cd6e2a5e5ccbca321e1f959ca
3548+
languageName: node
3549+
linkType: hard
3550+
35423551
"ts-jest@npm:^29.1.2":
35433552
version: 29.4.1
35443553
resolution: "ts-jest@npm:29.4.1"

0 commit comments

Comments
 (0)