You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
See this code example which is a mixture of Wing and TypeScript. Showcases the use of theoretical generics to restrict the shape of the data a function accepts.
If the Cloud Function is used inside the app, there's no need to validate the input data since the type checking system will make sure no invalid data is used. Now, if we use a Cloud API instead, this function will be exposed to the outside and thus the data will need to be validated since it can't be trusted:
// ...letapi=newcloud.Api();api.post("/user",inflight(request: cloud.ApiRequest)=>{log(request.body);// ^^^^ type string | undefinedcreateUser(request.body);// ^^^^ Error: `string | undefined` is not assignable to `CreateUserOptions`// In TypeScript, I would use zod to validate the datalet options =zod.object({name: zod.string(),age: zod.number().optional(),}).parse(Json.parse(request.body));log(options);// ^^^^^^^ type { name: string; age?: number | undefined; }// Now, `options` matches the type `CreateUserOptions`createUser(options);});
Since Wing is the compiler, it should be technically possible to generate the following:
Validation code under the hood for simple types
A conditional version of every function: is called from within the app, there's no need to validate. Validate the input otherwise
This is just an example of a concept and the API design is not ideal. I like the DX that trpc offers, just check it out. But I'd like to highlight:
Integrates with zod
Allows creating reusable routers
Allows navigating to the definition and implementation of the router endpoints using cmd+click
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
See this code example which is a mixture of Wing and TypeScript. Showcases the use of theoretical generics to restrict the shape of the data a function accepts.
If the Cloud Function is used inside the app, there's no need to validate the input data since the type checking system will make sure no invalid data is used. Now, if we use a Cloud API instead, this function will be exposed to the outside and thus the data will need to be validated since it can't be trusted:
Since Wing is the compiler, it should be technically possible to generate the following:
This is just an example of a concept and the API design is not ideal. I like the DX that trpc offers, just check it out. But I'd like to highlight:
Beta Was this translation helpful? Give feedback.
All reactions