1.0.0 • Published 2 months ago
restfuncs-transformer v1.0.0
restfuncs-transformer
This transformer prepares ServerSession
subclasses for Typia and adds jsDoc info for the (upcoming) API browser.
Usage
How the transformer chain works
Here is, how the restfuncs-transformer, typia transformer and typescript-rtti work together:
1. The restfuncs-transformer
scans for all @remote
methods and, at the bottom of inside their class, it adds the following code (here for "myMethod"):
/**
* Code is generated by the restfuncs-transformer during compile-time
*/
static getRemoteMethodsMeta(): (typeof this.type_remoteMethodsMeta) {
this.__hello_developer__make_sure_your_class_is_a_subclass_of_ServerSession // Give a friendly error message when this is not the case. Otherwise the following statement "const typia = ..." would fail and leaves the user wondering.
let typia = this.typiaRuntime; // We need a "typia" defined in the scope, but let restfuncs manage where that dependency comes from
return {
transformerVersion: {major: XX, feature: XX },
instanceMethods: {
"myMethod": {
arguments: {
validateEquals: (args: unknown) => typia.validateEquals<Parameters<typeof this.prototype["myMethod"]>>(args),
validatePrune: ...
},
result: {
validateEquals: (value: unknown) => typia.validateEquals<Awaited<ReturnType<typeof this.prototype["myMethod"]>>>(value),
...
},
jsDoc: {
comment: "text with <strong>markup</strong>",
params: {a: "text..."},
tags: [{name:"returns", comment: "text..."}]
}
}
}
}
}
- The Typia transformer will then replace all
typia.validate<T>
expressions with the actual validation code body. Typia calls this "ahead of time validation". At runtime this is used to validate evil input and serialize/deserialize from json/protobuf in an ultra fast way (future). - The typescript-rtti transformer adds type-info decorations for all compiled symbols (that's all this
__RΦ
stuff in the .js). At runtime, this allows restfuncs to easily navigate through- and inspect all the type hierarchy. I.e when mapping REST parameter names to the remote methods arguments, for better error diagnosis, or for the upcoming API browser.