Skip to content
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

Merged
merged 4 commits into from
Jul 4, 2022

Conversation

kokokuo
Copy link
Contributor

@kokokuo kokokuo commented May 16, 2022

Description

This PR includes some components for two parts:

1. Implementing validators from IValidator

  • IntegerTypeValidator, UUIDTypeValidator, StringTypeValidator and DateTypeValidator 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 to type in schema from koa context request.
  • RequestValidator: validate transformed request according to validators in schema.
  • RestfulRoute: handling the transformed request ( it extends BaseRoute which handles transformer and validator ) for restful API, the handler does not implement.
  • GraphQLRoute: handling the transformed request ( it extends BaseRoute 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 or RestfulRoute and run the server by listen.

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:

oss drawio

How To Test / Expected Results

For the test result, please see the below test cases passed of unit test:

Unit-test

You could check app.spec.ts for knowing how to run the VulcanApplication.

Commit Messages

  • 9887e16 - add serve package.

    • use nx generate @nrwl/js:library command to create serve package based on typescript
  • 44512c3 - feature(core): add built-in data type validators and testcase

    • add DateTypeValidator, IntegerTypeValidator, StringTypeValidator, UUIDTypeValidator and its test cases.
    • add loadedValidators for loading all built-in and user-defined validators dynamically.
    • install joi, moment plugins at package.json in the core folder.
    • install faker, ts-sinon, uuid plugins at vulcan package.json.
  • e5e8eef - feature(serve): add request transformer, validator, and route generator 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 to api route type.
    • add VulcanApplication to add routes and create the server.
    • add test cases for ReuqestTransformer, RequestValidator, RouteGenerator and VulcanApplication.
    • install suepertest in package.json for testing the 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.

@kokokuo kokokuo requested review from oscar60310 and wwwy3y3 May 16, 2022 03:23
kokokuo added 3 commits May 19, 2022 11:56
- 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.
@kokokuo kokokuo changed the base branch from develop to feature/pipe-extensions June 9, 2022 03:32
@kokokuo kokokuo self-assigned this Jun 9, 2022
Copy link
Contributor

@oscar60310 oscar60310 left a 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;
Copy link
Contributor

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?

Copy link
Contributor Author

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~

Copy link
Contributor Author

@kokokuo kokokuo Jun 16, 2022

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)) {
Copy link
Contributor

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))

Copy link
Contributor Author

@kokokuo kokokuo Jun 16, 2022

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),
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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
@kokokuo kokokuo changed the base branch from feature/pipe-extensions to develop July 4, 2022 01:21
@kokokuo kokokuo changed the base branch from develop to feature/serve-ioc-container July 4, 2022 01:24
@kokokuo kokokuo changed the base branch from feature/serve-ioc-container to develop July 4, 2022 01:38
@kokokuo kokokuo changed the base branch from develop to feature/serve-ioc-container July 4, 2022 01:39
@kokokuo kokokuo merged commit 7bec191 into feature/serve-ioc-container Jul 4, 2022
@kokokuo kokokuo deleted the feature/serve branch July 4, 2022 01:57
@kokokuo kokokuo restored the feature/serve branch July 4, 2022 02:52
@kokokuo kokokuo deleted the feature/serve branch July 4, 2022 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants