@theogonic/zeus
v0.1.6
Published
Typescript Generator for Clean Architecture
Downloads
65
Readme
Zeus
Motivation
Zeus are a code generator tries to generated as much as types from perspective of clean architecture. Once using "JSON Schema-like" zeus YAML to define the shape of usecase, method and its request and response, Zeus can generated interface, glue code of controller layer like restful layer, graphql layer (schema and actual code), etc.
Taking benefits of Nestjs and its dependency injection, Zeus can even generate ready-to-use restful controller (including code of path/query/body parameter handling/formatting). Please see Generators too find more powerful features.
Full Example
Zeus Generator Configuration
examples/user-usecase/zeus.yaml
Zeus Definition
Generated Result
Commands to generate examples
$ ./zeus --proto user.proto
Generators
ts
ts generator generate related interfaces for usecase defined in Zeus Definition
file. Generated interfaces are following:
- usecase itself
- each method's request and response
Example
// !!! ALL CODE BELOW ARE GENERATED BY ZEUS !!!
export interface CreateUserRequest {
...
}
...
export interface UserUsercase {
createUser(request: CreateUserRequest): Promise<CreateUserResponse>;
...
}
rest-nestjs
rest-nestjs generator generates ready-to-use Nestjs controller which provide restful API to a usecase. It depends on interfaces generated by ts generator. The generated code is ready-to-use. Body, path, query these details are configurable in the Zeus Definition
file.
Example
// !!! ALL CODE BELOW ARE GENERATED BY ZEUS !!!
import { UserUsercase, USER_USERCASE, ... } from "./types";
@Controller("user")
@ApiTags("user")
export class UserUsercaseController {
constructor(
@Inject(USER_USERCASE)
private readonly userUsercase: UserUsercase) { }
@Post("users")
@ApiOkResponse({ type: CreateUserResponseDto })
@()
createUser(
@Body()
body: CreateUserRequestBodyDto,
@User()
user) {
const ucReq = {
id: body.id,
name: body.name,
age: body.age,
user
} as CreateUserRequest;
return this.userUsercase.createUser(ucReq);
}
...
}
gql
gql generator generates the GraphQL type regarding to the types
section in Zeus Definition
file
Example
# !!! ALL CODE BELOW ARE GENERATED BY ZEUS !!!
type JwtUser {
id: String
email: String
org: String
}
type Query {
hello(request: Request): Response
}
gql-nestjs
Example
# !!! ALL CODE BELOW ARE GENERATED BY ZEUS !!!
import { Inject, Logger } from "@nestjs/common";
import { Args, Context, Mutation, Parent, Query, ResolveField, Resolver, ResolveReference } from "@nestjs/graphql";
import { UserUsercase, USER_USERCASE } from "./types";
@Resolver()
export class UserUsercaseResolver {
constructor(
@Inject(USER_USERCASE)
private readonly userUsercase: UserUsercase) { }
@Mutation()
async createUser(
@Args("request")
request) {
return this.userUsercase.createUser({
...request
});
}
....
}
general-entity
general-entity generated the types for @theogonic/gaea. If your project used @theogonic/gaea as your entity library (handling database related operations), you have read-to-use Entitys and Daos!
Example
# !!! ALL CODE BELOW ARE GENERATED BY ZEUS !!!
import { BaseGeneralObject, BaseGeneralObjectDao } from '@theogonic/gaea';
import { User } from './types';
export class UserEntity extends BaseGeneralObject implements User {
constructor(meta: BaseGeneralObject['meta'], obj: Omit<User, 'meta'>) {
super(meta);
this.tags = obj.tags;
this.profile = obj.profile;
}
tags: User['tags'];
profile: User['profile'];
}
export class UserEntityDao extends BaseGeneralObjectDao<UserEntity> {
target = UserEntity;
}
Zeus Generator Configuration YAML Format
This yaml is to tell zeus which generator is/are going to be used, where it should output and detail/extra configs for specific generator.
Zeus Definition YAML Format
This yaml defines shape of usecase(s) and its method(s) including request and request.