Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xhelpers-api

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xhelpers-api

[![npm](https://nodei.co/npm/xhelpers-api.png)](https://www.npmjs.com/package/xhelpers-api)

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
72
increased by56.52%
Maintainers
1
Weekly downloads
 
Created
Source

XHelpers - API

npm

forthebadge forthebadgeforthebadge

PRs WelcomeAwesome

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 xhelpers-api

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: {}
      },
      sequelize: {
        sequelizeOptions: {
          host: process.env.SEQ_SQLDB_HOST,
          database: process.env.SEQ_SQLDB_DATABASE,
          username: process.env.SEQ_SQLDB_USER,
          password: process.env.SEQ_SQLDB_PASSWORD,
          models: [__dirname + "/model/**"],
          dialect: "postgres"
        },
      },
      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

//contract
export interface IBaseService {
  queryAll(
    user: any,
    filter: any,
    pagination: {
      page: number;
      limit: number;
      sort: any;
    },
    populateOptions?: {
      path: string | any;
      select?: string | any;
    }
  ): Promise<any[]>;
  getById(
    user: any,
    id: any,
    projection: any,
    populateOptions?: {
      path: string | any;
      select?: string | any;
    }
  ): Promise<any>;
  create(user: any, payload: any): Promise<any>;
  update(user: any, id: any, payload: any): Promise<any>;
  delete(user: any, id: any): Promise<void>;
}
import AccountLogin from "/model/account_login"; // mongoose or sequelize "Model"
import BaseServiceSequelize from "xhelpers-api/lib/base-service-sequelize";
import BaseServiceMongoose from "xhelpers-api/lib/base-service-mongoose";

// mongoose
export 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 class AccountLoginSqlService extends BaseServiceSequelize<
  AccountLogin
> {
  constructor() {
    super(AccountLogin);
  }
  sentitiveInfo: any = ["id"];
  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

# build tsc
$ npm run build

Test

[Pending]

Support

[Pending]

Stay in touch

License

Keywords

FAQs

Package last updated on 15 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc