Skip to main content

GenzyApi

What GenzyApi is

GenzyApi is responsible for automatically creating a RestAPI from a set of GenzyContainers.

It is also responsible for automatically building OpenAPI documentation, and serving SwaggerUI on route /explorer.

Creating an API

From Plain Local Services

const { GenzyContainer, GenzyApi } = require("@genzy/api");

class UserService {
async createUser(user) {
return user;
}
}

class AccountService {
async getAllAccounts() {
return [];
}
}

const usersGenzyContainer = new GenzyContainer().addLocalServices(
UserService,
AccountService
);

const app = new GenzyApi().from(usersGenzyContainer);
app.listen(3000);

img

From Plain Remote Services

info

An API can also be generated from a set of remote services, so that the API acts as an API Gateway.

const { GenzyContainer, GenzyApi } = require("@genzy/api");

class UserService {
async createUser(user) {}
}

class AccountService {
async getAllAccounts() {}
}

const usersGenzyContainer = new GenzyContainer().addRemoteServices(
"http://localhost:3000",
UserService,
AccountService
);

const app = new GenzyApi().from(usersGenzyContainer);
app.listen(3000);

img

From Configured Local Services

const { GenzyContainer, GenzyApi } = require("@genzy/api");

class UserService {
$genzy = {
path: "/users",
createUser: {
httpMethod: "POST",
path: "/",
params: [{ name: "user", source: "body" }],
},
};
async createUser(user) {
return user;
}
}

class AccountService {
$genzy = {
path: "/accounts",
getAllAccounts: {
method: "GET",
path: "/",
},
};
async getAllAccounts() {
return [];
}
}

const usersGenzyContainer = new GenzyContainer().addLocalServices(
UserService,
AccountService
);

const app = new GenzyApi().from(usersGenzyContainer);
app.listen(3000);

img