@genzy.io/api
v0.0.1-alpha-8
Published
Genzy - focused on the domain.
Downloads
4
Readme
Genzy API
Getting Started
npm init -y
npm i -S @genzy.io/api
- Implement services
class UserService {
async createUser(user) {
// logic for adding the user
return user;
}
}
class AccountService {
// UserService is automatically injected
constructor({ userService }) {
this.userService = userService;
}
async getAllAccounts() {
return [];
}
// take accountInfo object as parameter
async createAccount({username, firstName, lastName, email}) {
// logic for adding the account
const newAccount = {id: 1, username};
// call another service
this.userService.createUser({
accountId: newAccount.id,
firstName,
lastName,
email
})
return newAccount;
}
}
- Create a GenzyContainer of services
import { GenzyContainer } from '@genzy.io/api';
const usersGenzyContainer = new GenzyContainer()
.ofLocal(UserService)
.andLocal(AccountService);
// The instances are available for custom usage
const { userService, accountService } = usersGenzyContainer.services();
- Create the GenzyApi
import { GenzyApi } from '@genzy.io/api';
const app = new GenzyApi().from(usersGenzyContainer);
app.listen(3000);
# 3 routes are registered
# POST /api/user-service/create-user
# GET /api/account-service/get-all-accounts
# POST /api/account-service/create-account
Interceptors
// Intercept all service handlers before they are called
const usersGenzyContainer = new GenzyContainer().ofLocal(UserService);
const app = new GenzyApi()
.interceptAll((req: Request, res: Response, next: NextFunction) => {
if(isTokenValid(req.headers.Authorization)) next();
else res.sendStatus(401);
})
.from(usersGenzyContainer);
// Intercept specific service handlers before they are called
const usersGenzyContainer = new GenzyContainer().ofLocal(UserService);
const app = new GenzyApi()
.intercept({
userService: {
createUser: (req: Request, res: Response, next: NextFunction) => {
if(isAdminUser(req.headers.Authorization)) next();
else res.sendStatus(401);
}
}
})
.from(usersGenzyContainer);
// Intercept specific service handlers before they are called with Interceptor class
class UserServiceInterceptor {
createUser(req: Request, res: Response, next: NextFunction) {
if(isAdminUser(req.headers.Authorization)) next();
else res.sendStatus(401);
}
}
const usersGenzyContainer = new GenzyContainer().ofLocal(UserService);
const app = new GenzyApi()
.intercept({
userService: {
createUser: UserServiceInterceptor
}
})
.from(usersGenzyContainer);
// Intercept all service handlers after they are called
const usersGenzyContainer = new GenzyContainer().ofLocal(UserService);
const app = new GenzyApi()
.interceptAllAfter((req: Request, res: Response, next: NextFunction) => {
res.body({ message: "Hello from Genzy." });
})
.from(usersGenzyContainer);
// Intercept specific service handlers after they are called
const usersGenzyContainer = new GenzyContainer().ofLocal(UserService);
const app = new GenzyApi()
.interceptAfter({
userService: {
createUser: (req: Request, res: Response, next: NextFunction) => {
res.status(201);
next();
}
}
})
.from(usersGenzyContainer);
// Intercept specific service handlers after they are called with Interceptor class
class UserServiceInterceptor {
createUser(req: Request, res: Response, next: NextFunction) {
res.status(201);
next();
}
}
const usersGenzyContainer = new GenzyContainer().ofLocal(UserService);
const app = new GenzyApi()
.interceptAfter({
userService: {
createUser: UserServiceInterceptor
}
})
.from(usersGenzyContainer);
Error Status Code Mappings
class BadLogicError extends Error {
name = "BadLogicError";
constructor(message?: string) {
super(message);
}
}
class InternalServerError extends Error {
name = "InternalServerError";
constructor(message?: string) {
super(message);
}
}
const app = new GenzyApi()
.withErrors({
[BadLogicError.name]: 400,
[InternalServerError.name]: 500,
})
.from(usersGenzyContainer);
Steps for publishing new version
npm run prepublish
npm version major/minor/patch
npm publish