-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Implementing restful route generator and api server according to api schema #8
Conversation
- use "nx generate @nrwl/js:library" command to create "serve" package based on typescript
- add "DateTypeValidator", "IntegerTypeValidator", "StringTypeValidator", "UUIDTypeValidator" and its test cases. - add "loadedValidators" for loading all built-in and user-defined validator dynamically. - install joi, moment plugins at package.json in core folder. - install faker, ts-sinon, uuid plugins at vulcan package.json.
…r to generate routes for handling and check request - add "ReuqestTransformer", "RequestValidator", for converting request data format and validating. - add "BaseRoute", "RestfulRoute", "GraphQLRoute" to create route. - add "RouteGenerator" to generate Route according api route type. - add "VulcanApplication" to add routes and create server. - add test cases for "ReuqestTransformer", "RequestValidator", "RouteGenerator" and "VulcanApplication" - install "suepertest" in package.json for testing koa server. - add "start" command to run serve package directly for testing in project.json. - update tsconfig.json for making alias path could be run by ts-node in nx.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other part LGTM.
// validator name | ||
readonly name: string; | ||
// validate Schema format | ||
validateSchema(args: T): boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that we throw error when finding invalid values, how about we change the return type to void?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing! Yes, void will be better~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has been changed to return void
, please check it, thanks ~
) { | ||
await Promise.all( | ||
schemaValidators.map((schemaValidator) => { | ||
if (!(schemaValidator.name in loadedValidators)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to use type guard instead.
https://www.typescriptlang.org/docs/handbook/advanced-types.html
const isIValidator(obj: any): any is IValidator => {
return !!(obj.validateSchema) && !!(obj.validateData)
}
if (!isIValidator(schemaValidator))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing!
But the schemaValidater
is ValidatorDefinition
and the ValidatorDefinition
does not contain validateSchema
and validateData
method, here is only checked for the validator name in APISchem
exist or not in the dynamic loader.
Btw, this part will be changed in PR #14
} = { | ||
[FieldDataType.NUMBER]: (value: string) => Number(value), | ||
[FieldDataType.STRING]: (value: string) => String(value), | ||
[FieldDataType.BOOLEAN]: (value: string) => Boolean(value), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't turn the value to boolean via Boolean() function, Boolean('false')
will be true XD
Here are some examples we wrote in order to normalize string.
https://github.com/Canner/canner-ui/blob/develop/server/restful/restful-utility/decorators/normalizeStringValue.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I miss the detail. Thanks for reviewing and finding the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue has been fixed, please check it, thanks ~
- remove "esModuleInterop" in core, serve packages. - change from "moment" to "dayjs" to use in "DateTypeValidator" - update "validateSchema" and "validateData" return void type in "IValidator". - create "normalizedStringValue" module to handle string value. - add "../../types/*.d.ts" in tsconfig of serve. - rename to "RequestParameter" from "RequestSchema" in middleware. - rename "route-generator" to "route" package
Description
This PR includes some components for two parts:
1. Implementing validators from IValidator
IntegerTypeValidator
,UUIDTypeValidator
,StringTypeValidator
andDateTypeValidator
which implementing IValidator interface to validate input data according validator fields of APISchema.2. Getting API schemas and receiving requests to validate and respond to results:
RequestTransformer
: transform and check data type according totype
in schema from koa context request.RequestValidator
: validate transformed request according tovalidators
in schema.RestfulRoute
: handling the transformed request ( it extendsBaseRoute
which handles transformer and validator ) for restful API, the handler does not implement.GraphQLRoute
: handling the transformed request ( it extendsBaseRoute
which handles transformer and validator ) graphQL, the handler does not implement.RouteGenerator
: a factory pattern to create graphQL or restful route according to API type.VulcanApplication
: array ofGraphQLRoute
orRestfulRoute
and run the server bylisten
.Below is class relationship :
Finish Content mapping to the original Ivan drew architecture
Below is the finished par which maps to the original Ivan drew architecture:
How To Test / Expected Results
For the test result, please see the below test cases passed of unit test:
You could check
app.spec.ts
for knowing how to run theVulcanApplication
.Commit Messages
9887e16 - add serve package.
nx generate @nrwl/js:library
command to createserve
package based on typescript44512c3 - feature(core): add built-in data type validators and testcase
DateTypeValidator
,IntegerTypeValidator
,StringTypeValidator
,UUIDTypeValidator
and its test cases.loadedValidators
for loading all built-in and user-defined validators dynamically.e5e8eef - feature(serve): add request transformer, validator, and route generator to generate routes for handling and check request
ReuqestTransformer
,RequestValidator
, for converting request data format and validating.BaseRoute
,RestfulRoute
,GraphQLRoute
to create route.RouteGenerator
to generate Route according to api route type.VulcanApplication
to add routes and create the server.ReuqestTransformer
,RequestValidator
,RouteGenerator
andVulcanApplication
.suepertest
in package.json for testing the koa server.start
command to run serve package directly for testing in project.json.