XHelpers - API
Description
This project was made for personal use, it should simplify the process of creating an new api using node js + typescript + singledb(mongo or mysql).
Stacks:
Installation
$ npm i
Examples of usage
Hapi Server
import createServer from "xhelpers-api/lib/server";
server = await createServer({
serverOptions: {
port: process.env.PORT,
host: process.env.HOST
},
options: {
enableSSL: process.env.SSL === "true",
swaggerOptions: {
jsonPath: "/api/documentation/swagger.json",
documentationPath: "/api/documentation",
swaggerUIPath: "/api/swaggerui/",
info: {
title: "API",
version: "1.3"
},
grouping: "tags",
tags: [
{
name: "account",
description: "Account API operations"
}
]
},
routeOptions: {
dir: `${__dirname}/routes/**`
},
jwt_secret: "v3ryH4rdS3cr3t",
mongodb: {
uri: "",
connectionOptions: {}
},
mysql: {
sequelizeOptions: {
host: process.env.MYSQLDB_HOST,
database: process.env.MYSQLDB_DATABASE,
username: process.env.MYSQLDB_USER,
password: process.env.MYSQLDB_PASSWORD,
storage: process.env.MYSQLDB_STORAGE,
models: [__dirname + "/model/**"]
},
},
enableSSO: false,
ssoCallback: null
}
});
await server.start();
Routes
import Service from "/services/account-login";
import BaseRoute from "xhelpers-api/lib/base-route";
const httpResourcePath = "account-login";
class RouteAccountLogin extends BaseRoute<Service> {
constructor() {
super(new Service(), [httpResourcePath]);
this.route("POST", `/api/${httpResourcePath}`)
.validate({ payload: createPayload })
.handler(async (r, h, u) => {
const entity = await this.service.create(u, r.payload);
return h.response(entity).code(200);
})
.build();
this.route("GET", `/api/${httpResourcePath}`)
.validate({ query: this.defaultSearchQuery })
.handler(async (r, h, u) => {
return await this.service
.queryAll(
u,
{
filter: r.query.filter,
fields: r.query.fields
},
{
page: r.query.page,
limit: r.query.limit,
sort: r.query.sort
}
)
.then(entities => h.response(entities).code(200));
})
.build();
this.route("GET", `/api/${httpResourcePath}/{id}`)
.validate({ params: this.defaultIdProperty })
.handler(async (r, h, u) => {
return await this.service
.getById(u, r.params.id)
.then(entity =>
entity ? h.response(entity).code(200) : Boom.notFound()
);
})
.build();
this.route("PUT", `/api/${httpResourcePath}/{id}`)
.validate({ params: this.defaultIdProperty, payload: createPayload })
.handler(async (r, h, u) => {
return await this.service
.update(u, r.params.id, r.payload)
.then(() => h.response({}).code(200));
})
.build();
this.route("DELETE", `/api/${httpResourcePath}/{id}`)
.validate({ params: this.defaultIdProperty })
.handler(async (r, h, u) => {
return await this.service
.delete(u, r.params.id)
.then(() => h.response({}).code(200));
})
.build();
}
}
exports.routes = server => server.route(new RouteAccountLogin().buildRoutes());
Service
import AccountLogin from "../../model/account/account_login";
import BaseServiceSequelize from "xhelpers-api/lib/base-service-sequelize";
// mongoose
export default class AccountLoginService extends BaseServiceMongoose<
AccountLogin
> {
constructor() {
super(AccountLogin);
}
sentitiveInfo: any = ["-__v", "password"];
protected async validate(entity: AccountLogin, payload: AccountLogin): Promise<boolean> {
const invalid = false;
if (invalid) throw new Error("Invalid payload.");
return Promise.resolve(true);
}
}
// sequelize
export default class AccountStatusService extends BaseServiceSequelize<
AccountLogin
> {
constructor() {
super(AccountLogin);
}
sentitiveInfo: any = ["password"];
protected async validate(
entity: AccountLogin,
payload: AccountLogin
): Promise<boolean> {
const invalid = false;
if (invalid) throw new Error("Invalid payload.");
return Promise.resolve(true);
}
}
Safe call
// safeCall(
// request: { method: any; path: any; auth: { credentials: { user: any } } },
// action: { (user: any): Promise<any>; (arg0: any): Promise<any> }
// )
safeCall(request, async (user: any) => {
return await action(request, h, user);
})
Building
$ npm run build
Test
[Pending]
Support
[Pending]
Stay in touch
License