Skip to content

Commit 4e64181

Browse files
committed
fix: 🐛 improve "fn" types
1 parent 63deffb commit 4e64181

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/type/__tests__/TypeBuilder.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {of} from 'rxjs';
12
import {type SchemaOf, t} from '..';
23
import type {NumSchema, TypeOf} from '../../schema';
34
import {validateSchema} from '../../schema/validate';
@@ -28,6 +29,27 @@ describe('"fn" kind', () => {
2829
const type2 = t.Function$(t.str, t.num, {title: 'My Function'});
2930
expect(type1.getSchema()).toEqual(type2.getSchema());
3031
});
32+
33+
test('can set function implementation', () => {
34+
const fn1 = t.fn
35+
.input(t.object({id: t.str}))
36+
.output(t.num)
37+
.title('My Function')
38+
.default(async ({id}) => {
39+
return 42;
40+
})
41+
.default(({id}) => 42)
42+
.description('This is a function that returns a number based on the input id.');
43+
const fn2 = t.fn$
44+
.input(t.object({id: t.str}))
45+
.output(t.num)
46+
.title('My Rx Function')
47+
.default(() => {
48+
return of(42);
49+
});
50+
// console.log(fn1 + '');
51+
// console.log(fn2 + '');
52+
});
3153
});
3254

3355
test('can construct a array type', () => {

src/type/classes/FnType.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const toStringTree = (tab: string = '', type: FnType<Type, Type, any> | FnRxType
1717
export class FnType<Req extends Type, Res extends Type, Ctx = unknown> extends AbsType<
1818
schema.FnSchema<SchemaOf<Req>, SchemaOf<Res>, Ctx>
1919
> {
20-
public fn: schema.FunctionValue<schema.TypeOf<SchemaOf<Req>>, schema.TypeOf<SchemaOf<Res>>> = fnNotImplemented;
21-
2220
constructor(
2321
public readonly req: Req,
2422
public readonly res: Res,
@@ -68,6 +66,11 @@ export class FnType<Req extends Type, Res extends Type, Ctx = unknown> extends A
6866
};
6967
}
7068

69+
public default(value: schema.FunctionValue<schema.TypeOf<SchemaOf<Req>>, schema.TypeOf<SchemaOf<Res>>>): this {
70+
this.schema.default = value;
71+
return this;
72+
}
73+
7174
public toString(tab: string = ''): string {
7275
return super.toString(tab) + toStringTree(tab, this);
7376
}
@@ -89,22 +92,34 @@ export class FnRxType<Req extends Type, Res extends Type, Ctx = unknown> extends
8992
} as any);
9093
}
9194

92-
public request<T extends Type>(req: T): FnType<T, Res> {
95+
public input<T extends Type>(req: T): FnRxType<T, Res> {
96+
return this.inp(req);
97+
}
98+
99+
public inp<T extends Type>(req: T): FnRxType<T, Res> {
93100
(this as any).req = req;
94101
return this as any;
95102
}
96103

97-
public inp<T extends Type>(req: T): FnType<T, Res> {
98-
return this.request(req);
104+
public output<T extends Type>(res: T): FnRxType<Req, T> {
105+
return this.out(res);
99106
}
100107

101-
public response<T extends Type>(res: T): FnType<Req, T> {
108+
public out<T extends Type>(res: T): FnRxType<Req, T> {
102109
(this as any).res = res;
103110
return this as any;
104111
}
105112

106-
public out<T extends Type>(res: T): FnType<Req, T> {
107-
return this.response(res);
113+
public io<I extends Type, O extends Type>(request: I, response: O): FnRxType<I, O, Ctx> {
114+
return this.inp(request).out(response) as FnRxType<I, O, Ctx>;
115+
}
116+
117+
public signature<I extends Type, O extends Type>(request: I, response: O): FnRxType<I, O, Ctx> {
118+
return this.io(request, response) as FnRxType<I, O, Ctx>;
119+
}
120+
121+
public ctx<T>(): FnRxType<Req, Res, T> {
122+
return this as any;
108123
}
109124

110125
public getSchema(): schema.FnRxSchema<SchemaOf<Req>, SchemaOf<Res>, Ctx> {
@@ -115,6 +130,11 @@ export class FnRxType<Req extends Type, Res extends Type, Ctx = unknown> extends
115130
};
116131
}
117132

133+
public default(value: schema.FnStreamingValue<schema.TypeOf<SchemaOf<Req>>, schema.TypeOf<SchemaOf<Res>>>): this {
134+
this.schema.default = value;
135+
return this;
136+
}
137+
118138
public toString(tab: string = ''): string {
119139
return super.toString(tab) + toStringTree(tab, this);
120140
}

0 commit comments

Comments
 (0)