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
// Signature
createServer({
serverOptions,
options
}: {
serverOptions: any;
options: {
enableSSL: boolean;
swaggerOptions: any;
routeOptions: any;
jwt_secret: any;
mongodb: {
connectionOptions: any;
};
mysql: {
sequelizeOptions: any;
};
enableSSO: boolean;
ssoCallback: Function;
};
})
server = await createServer(
{
port: process.env.PORT,
host: process.env.HOST
},
{
enableSSL: true,
enableSSO: false,
mongodb: {
uri: process.env.MONGODB_URI,
connectionOptions: {
useCreateIndex: true,
useNewUrlParser: true,
}
},
ssoCallback: (
user: { email: any; name: any; avatar: any; token: string },
userData: { userType: any; meta: any }
) => {
return null;
},
swaggerOptions: {
jsonPath: "/api/documentation/swagger.json",
documentationPath: "/api/documentation",
swaggerUIPath: "/api/swaggerui/",
info: {
title: "API",
version: "1.0"
},
grouping: "tags",
tags: [
{
name: "account",
description: "Account api operations"
}
]
}
}
);
await server.start();
Routes
import Service from "/services/account-login";
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
// mongoose
export default class AccountLoginService extends BaseServiceMongoose<
AccountLogin
> {
constructor() {
super(AccountLogin);
}
sentitiveInfo = ["-__v", "password"];
protected async validate(entity: AccountLogin, payload: AccountLogin) {
const invalid = false;
if (invalid) throw new Error("Invalid payload.");
return Promise.resolve();
}
}
// sequelize
export default class AccountLoginService extends BaseServiceSequelize<
AccountLogin
> {
constructor() {
super(AccountLogin);
}
sentitiveInfo = ["password"];
protected async validate(entity: AccountLogin, payload: AccountLogin) {
const invalid = false;
if (invalid) throw new Error("Invalid payload.");
return Promise.resolve();
}
}
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