Skip to main content

Getting Started

NPM NPM NPM

Setting up the server

  1. Initialize the project
npm init -y
  1. Install @genzy/api library
npm i -S @genzy/api
  1. Implement Example service
example.ts
import {
Controller,
Get,
Post,
Put,
Delete,
Query,
Path,
Body,
string,
number,
boolean,
type,
Returns,
ReturnsArrayOf,
} from "@genzy/api";

class Example {
@string() name: string;
@int() age: number;
}

@Controller("/examples")
export class ExampleService {
@Get()
@ReturnsArrayOf(Example)
async getAll(
@Query("pageNumber") @int() pageNumber: number,
@Query("pageSize") @int() pageSize: number
): Promise<Example[]> {
return [];
}
@Get("/:id")
@Returns(Example)
async getById(
@Query("includeDetails") @boolean() includeDetails: boolean,
@Path("id") @string() id: string
): Promise<Example> {
return;
}
@Post()
@Returns(Example)
async add(@Body() @type(Example) example: Example): Promise<Example> {
return example;
}
@Put("/:id")
@Returns(Example)
async update(
@Path("id") @string() id: string,
@Body() @type(Example) example: Example
): Promise<Example> {
return example;
}
@Delete("/:id")
@Returns(Example)
async delete(@Path("id") @string() id: string): Promise<Example> {
return;
}
}
  1. Create a GenzyContainer of services and the GenzyApi
index.ts
import { GenzyContainer, GenzyApi } from "@genzy/api";
import { ExampleService } from "./example";

const container = new GenzyContainer().addLocalService(ExampleService);

const api = new GenzyApi({
genzyInfo: {
name: "Example Microservice",
version: "1.0.0",
description: "This is an example microservice.",
},
}).from(container);

const PORT = 3000;
api.listen(PORT, () => console.log(`Started listening on ${PORT}`));

type GenzyContainerServices = {
exampleService: ExampleService;
};

// The instances are available for custom usage
const { exampleService } = usersGenzyContainer.services();
5 routes have been registered
  • GET /api/examples
  • GET /api/examples/{id}
  • POST /api/examples
  • PUT /api/examples/{id}
  • DELETE /api/examples/{id} :::

img

Setting up the client

  1. Initialize the project
npm init -y
  1. Install @genzy/client library
npm i -S @genzy/client
  1. Install @genzy/cli CLI
npm i -g @genzy/cli
  1. Generate TypeScript client code
genzy -l ts -h http://localhost:3000 -o ./services/example
  • services/example/index.ts
  • services/example/ExampleService.ts :::
services/example/index.ts
// Auto-generated by Genzy Client CLI
import { GenzyContainer } from "@genzy/client";
import { ExampleService as ExampleServiceLocal } from "./ExampleService";

const host = "http://localhost:3000";

export type GenzyContainerServices = {
exampleService: ExampleServiceLocal;
};

export const { exampleService }: GenzyContainerServices = new GenzyContainer()
.ofRemote(ExampleServiceLocal, host)
.services();
services/example/ExampleService.ts
// Auto-generated by Genzy Client CLI
import {
Controller,
Get,
Post,
Put,
Delete,
Query,
Path,
Body,
} from "@genzy/client";

export class Example {
name: string;
age: number;
}

@Controller("/examples")
export class ExampleService {
@Get("/")
async getAll(
@Query("pageNumber") pageNumber: number,
@Query("pageSize") pageSize: number
) {}

@Get("/:id")
async getById(
@Query("includeDetails") includeDetails: string,
@Path("id") id: boolean
) {}

@Post("/")
async add(@Body() body: Example) {}

@Put("/:id")
async update(@Path("id") id: string, @Body() body: Example) {}

@Delete("/:id")
async delete(@Path("id") id: string) {}
}
  1. Use the ExampleService elsewhere
somewhere.ts
import { exampleService } from "./services/example";

exampleService.getAll().then(console.log).catch(console.log);