Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

hans-sequelize-api

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hans-sequelize-api - npm Package Compare versions

Comparing version
1.0.25
to
1.0.26
+1
-0
lib/index.d.ts

@@ -9,2 +9,3 @@ import { IRouter } from 'express';

initializeAPI(options: InitializeAPIOptions): (modelName: PostgreModelName, options?: SetAPIOptions<PostgreModelName> | undefined) => IRouter;
private _mapIsAfterMethods;
private _getEntities;

@@ -11,0 +12,0 @@ private _getEntity;

+44
-18

@@ -51,5 +51,6 @@ "use strict";

const additionalMiddlewares = options === null || options === void 0 ? void 0 : options.additionalMiddlewares;
const afterMethods = options === null || options === void 0 ? void 0 : options.afterMethods;
const defaultFields = options === null || options === void 0 ? void 0 : options.defaultFields;
const defaultRelationFields = options === null || options === void 0 ? void 0 : options.defaultRelationFields;
const controllers = self._getApiControllers(modelName, defaultFields, defaultRelationFields);
const controllers = self._getApiControllers(modelName, self._mapIsAfterMethods(afterMethods), defaultFields, defaultRelationFields);
self._extendedMethods.forEach((myMethod, index) => {

@@ -68,3 +69,10 @@ if (possibleMethods.includes(myMethod)) {

const [method, path] = self._getMethodAndPath(myMethod);
router[method](path, ...middlewares, controllers[myMethod]);
let afterMethodsForAdd = [];
if (afterMethods && afterMethods[myMethod]) {
if (Array.isArray(afterMethods[myMethod]))
afterMethodsForAdd = afterMethods[myMethod];
else
afterMethodsForAdd = [afterMethods[myMethod]];
}
router[method](path, ...middlewares, controllers[myMethod], ...afterMethodsForAdd);
}

@@ -75,5 +83,13 @@ });

}
_getEntities(modelName, defaultFields, defaultRelationFields) {
_mapIsAfterMethods(afterMethods) {
if (afterMethods) {
let isAfterMethods = {};
this._extendedMethods.forEach(method => isAfterMethods[method] = !!afterMethods[method]);
return isAfterMethods;
}
return { gets: false, get: false, post: false, put: false, delete: false };
}
_getEntities(modelName, isAfterMethod, defaultFields, defaultRelationFields) {
const self = this;
return function (req, res) {
return function (req, res, next) {
return __awaiter(this, void 0, void 0, function* () {

@@ -102,2 +118,4 @@ try {

});
if (isAfterMethod)
next();
}

@@ -110,5 +128,5 @@ catch (e) {

}
_getEntity(modelName, defaultFields, defaultRelationFields) {
_getEntity(modelName, isAfterMethod, defaultFields, defaultRelationFields) {
const self = this;
return function (req, res) {
return function (req, res, next) {
return __awaiter(this, void 0, void 0, function* () {

@@ -132,2 +150,4 @@ try {

(0, hans_http_handlers_1.status200)(res, entity);
if (isAfterMethod)
next();
}

@@ -140,5 +160,5 @@ catch (e) {

}
_postEntity(modelName, defaultFields, defaultRelationFields) {
_postEntity(modelName, isAfterMethod, defaultFields, defaultRelationFields) {
const self = this;
return function (req, res) {
return function (req, res, next) {
return __awaiter(this, void 0, void 0, function* () {

@@ -179,2 +199,4 @@ try {

(0, hans_http_handlers_1.status201)(res, newEntity);
if (isAfterMethod)
next();
}

@@ -187,5 +209,5 @@ catch (e) {

}
_putEntity(modelName, defaultFields, defaultRelationFields) {
_putEntity(modelName, isAfterMethod, defaultFields, defaultRelationFields) {
const self = this;
return function (req, res) {
return function (req, res, next) {
return __awaiter(this, void 0, void 0, function* () {

@@ -230,2 +252,4 @@ try {

(0, hans_http_handlers_1.status200)(res, newEntity);
if (isAfterMethod)
next();
}

@@ -238,5 +262,5 @@ catch (e) {

}
_deleteEntity(modelName, defaultFields, defaultRelationFields) {
_deleteEntity(modelName, isAfterMethod, defaultFields, defaultRelationFields) {
const self = this;
return function (req, res) {
return function (req, res, next) {
return __awaiter(this, void 0, void 0, function* () {

@@ -263,2 +287,4 @@ try {

(0, hans_http_handlers_1.status200)(res, entity);
if (isAfterMethod)
next();
}

@@ -297,9 +323,9 @@ catch (e) {

}
_getApiControllers(modelName, defaultFields, defaultRelationFields) {
_getApiControllers(modelName, isAfterMethods, defaultFields, defaultRelationFields) {
return {
gets: this._getEntities(modelName, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.gets, defaultRelationFields),
get: this._getEntity(modelName, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.get, defaultRelationFields),
post: this._postEntity(modelName, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.post, defaultRelationFields),
put: this._putEntity(modelName, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.put, defaultRelationFields),
delete: this._deleteEntity(modelName, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.delete, defaultRelationFields)
gets: this._getEntities(modelName, isAfterMethods.gets, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.gets, defaultRelationFields),
get: this._getEntity(modelName, isAfterMethods.get, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.get, defaultRelationFields),
post: this._postEntity(modelName, isAfterMethods.post, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.post, defaultRelationFields),
put: this._putEntity(modelName, isAfterMethods.put, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.put, defaultRelationFields),
delete: this._deleteEntity(modelName, isAfterMethods.delete, defaultFields === null || defaultFields === void 0 ? void 0 : defaultFields.delete, defaultRelationFields)
};

@@ -306,0 +332,0 @@ }

import { NextFunction, Request, Response } from 'express';
import { Model, ModelCtor, ModelStatic, WhereOptions } from 'sequelize';
export declare type Handler = (req: Request, res: Response, next?: NextFunction) => Promise<any>;
export declare type Controller = (req: Request, res: Response) => Promise<void>;
export declare type Controller = (req: Request, res: Response, next: NextFunction) => Promise<void>;
export declare type Method = 'get' | 'post' | 'put' | 'delete';

@@ -26,2 +26,3 @@ export declare type ExtendedMethod = 'gets' | Method;

defaultRelationFields?: Record<PostgreModelName, string[]>;
afterMethods?: Partial<Record<ExtendedMethod, Handler | Handler[]>>;
} | void;

@@ -28,0 +29,0 @@ export declare type Sort = 'ASC' | 'DESC';

{
"name": "hans-sequelize-api",
"version": "1.0.25",
"version": "1.0.26",
"description": "rest-api form sequelize-express stack",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -1,2 +0,2 @@

import {IRouter, Request, Response, Router} from 'express'
import {IRouter, NextFunction, Request, Response, Router} from 'express'
import {

@@ -61,5 +61,6 @@ Controllers,

const additionalMiddlewares = options?.additionalMiddlewares
const afterMethods = options?.afterMethods
const defaultFields = options?.defaultFields
const defaultRelationFields = options?.defaultRelationFields
const controllers = self._getApiControllers(modelName, defaultFields, defaultRelationFields)
const controllers = self._getApiControllers(modelName, self._mapIsAfterMethods(afterMethods), defaultFields, defaultRelationFields)
self._extendedMethods.forEach((myMethod, index) => {

@@ -74,3 +75,8 @@ if (possibleMethods.includes(myMethod)) {

const [method, path] = self._getMethodAndPath(myMethod)
router[method](path, ...middlewares, controllers[myMethod])
let afterMethodsForAdd: Handler[] = []
if (afterMethods && afterMethods[myMethod]) {
if (Array.isArray(afterMethods[myMethod])) afterMethodsForAdd = afterMethods[myMethod] as Handler[]
else afterMethodsForAdd = [afterMethods[myMethod] as Handler]
}
router[method](path, ...middlewares, controllers[myMethod], ...afterMethodsForAdd)
}

@@ -82,5 +88,14 @@ })

private _getEntities(modelName: PostgreModelName, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
private _mapIsAfterMethods(afterMethods: Partial<Record<ExtendedMethod, Handler | Handler[]>> | undefined): Record<ExtendedMethod, boolean> {
if (afterMethods) {
let isAfterMethods: Partial<Record<ExtendedMethod, boolean>> = {}
this._extendedMethods.forEach(method => isAfterMethods[method] = !!afterMethods[method])
return isAfterMethods as Record<ExtendedMethod, boolean>
}
return {gets: false, get: false, post: false, put: false, delete: false}
}
private _getEntities(modelName: PostgreModelName, isAfterMethod: boolean, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
const self = this
return async function (req: Request, res: Response): Promise<any> {
return async function (req: Request, res: Response, next: NextFunction): Promise<any> {
try {

@@ -120,2 +135,3 @@ const {

})
if (isAfterMethod) next()
} catch (e: any) {

@@ -127,5 +143,5 @@ error500('api get entities', res, e, __filename)

private _getEntity(modelName: PostgreModelName, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
private _getEntity(modelName: PostgreModelName, isAfterMethod: boolean, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
const self = this
return async function (req: Request, res: Response): Promise<any> {
return async function (req: Request, res: Response, next: NextFunction): Promise<any> {
try {

@@ -155,2 +171,3 @@ const {

status200(res, entity)
if (isAfterMethod) next()
} catch (e: any) {

@@ -162,5 +179,5 @@ error500('api get entity', res, e, __filename)

private _postEntity(modelName: PostgreModelName, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
private _postEntity(modelName: PostgreModelName, isAfterMethod: boolean, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
const self = this
return async function (req: Request, res: Response): Promise<any> {
return async function (req: Request, res: Response, next: NextFunction): Promise<any> {
try {

@@ -207,2 +224,3 @@ const {query, body} = req

status201(res, newEntity)
if (isAfterMethod) next()
} catch (e: any) {

@@ -214,5 +232,5 @@ error500('api post entity', res, e, __filename)

private _putEntity(modelName: PostgreModelName, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
private _putEntity(modelName: PostgreModelName, isAfterMethod: boolean, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
const self = this
return async function (req: Request, res: Response): Promise<any> {
return async function (req: Request, res: Response, next: NextFunction): Promise<any> {
try {

@@ -262,2 +280,3 @@ const {query, body, params} = req

status200(res, newEntity)
if (isAfterMethod) next()
} catch (e: any) {

@@ -269,5 +288,5 @@ error500('api put entity', res, e, __filename)

private _deleteEntity(modelName: PostgreModelName, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
private _deleteEntity(modelName: PostgreModelName, isAfterMethod: boolean, defaultFields?: string[], defaultRelationFields?: Record<PostgreModelName, string[]>) {
const self = this
return async function (req: Request, res: Response): Promise<any> {
return async function (req: Request, res: Response, next: NextFunction): Promise<any> {
try {

@@ -299,2 +318,3 @@ const {

status200(res, entity)
if (isAfterMethod) next()
} catch (e: any) {

@@ -333,9 +353,9 @@ error500('api delete entity', res, e, __filename)

private _getApiControllers(modelName: PostgreModelName, defaultFields?: Partial<Record<ExtendedMethod, string[]>>, defaultRelationFields?: Record<PostgreModelName, string[]>): Controllers {
private _getApiControllers(modelName: PostgreModelName, isAfterMethods: Record<ExtendedMethod, boolean>, defaultFields?: Partial<Record<ExtendedMethod, string[]>>, defaultRelationFields?: Record<PostgreModelName, string[]>): Controllers {
return {
gets: this._getEntities(modelName, defaultFields?.gets, defaultRelationFields),
get: this._getEntity(modelName, defaultFields?.get, defaultRelationFields),
post: this._postEntity(modelName, defaultFields?.post, defaultRelationFields),
put: this._putEntity(modelName, defaultFields?.put, defaultRelationFields),
delete: this._deleteEntity(modelName, defaultFields?.delete, defaultRelationFields)
gets: this._getEntities(modelName, isAfterMethods.gets, defaultFields?.gets, defaultRelationFields),
get: this._getEntity(modelName, isAfterMethods.get, defaultFields?.get, defaultRelationFields),
post: this._postEntity(modelName, isAfterMethods.post, defaultFields?.post, defaultRelationFields),
put: this._putEntity(modelName, isAfterMethods.put, defaultFields?.put, defaultRelationFields),
delete: this._deleteEntity(modelName, isAfterMethods.delete, defaultFields?.delete, defaultRelationFields)
}

@@ -342,0 +362,0 @@ }

@@ -6,3 +6,3 @@ import {NextFunction, Request, Response} from 'express'

export type Handler = (req: Request, res: Response, next?: NextFunction) => Promise<any>
export type Controller = (req: Request, res: Response) => Promise<void>
export type Controller = (req: Request, res: Response, next: NextFunction) => Promise<void>

@@ -40,2 +40,3 @@ export type Method =

defaultRelationFields?: Record<PostgreModelName, string[]>
afterMethods?: Partial<Record<ExtendedMethod, Handler | Handler[]>>
} | void

@@ -42,0 +43,0 @@