0.0.1-alpha-8 • Published 6 months ago

@genzy.io/api v0.0.1-alpha-8

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

Genzy API

NPM

Getting Started

  1. npm init -y
  2. npm i -S @genzy.io/api
  3. 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;
  }
}
  1. 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();
  1. 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

  1. npm run prepublish
  2. npm version major/minor/patch
  3. npm publish
0.0.1-alpha-8

6 months ago

0.0.1-alpha-7

6 months ago

0.0.1-alpha-6

6 months ago

0.0.1-alpha-5

6 months ago

0.0.1-alpha-4

6 months ago

0.0.1-alpha-3

6 months ago

0.0.1-alpha-2

6 months ago

0.0.1-alpha-1

6 months ago

0.0.1-alpha

7 months ago