npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@elsikora/nestjs-crud-automator

v1.5.0

Published

A library for automating the creation of CRUD operations in NestJS.

Downloads

582

Readme

API Controller Configuration Documentation

Overview

The API Controller configuration is a TypeScript-based configuration system that provides a comprehensive way to define API endpoints with authentication, request/response handling, validation, and data transformation capabilities.

Table of Contents

Basic Configuration Structure

The base configuration object follows this interface:

interface IApiControllerProperties<E> {
    entity: IApiBaseEntity;
    name?: string;
    path?: string;
    routes: {
        [R in EApiRouteType]?: TApiControllerPropertiesRoute<E, R>;
    };
}

Properties:

  • entity: The base entity class for the controller
  • name: Optional name for the controller
  • path: Optional URL path for the controller
  • routes: Object containing route configurations

Route Configuration

Routes are defined using the EApiRouteType enum, which includes:

  • CREATE
  • GET
  • GET_LIST
  • UPDATE
  • PARTIAL_UPDATE
  • DELETE

Each route can be configured with:

  • Authentication settings
  • Request handling
  • Response handling
  • Data transformations
  • Validation rules

Example:

{
    [EApiRouteType.CREATE]: {
        authentication: {...},
        request: {...},
        response: {...}
    }
}

Authentication

Authentication configuration allows you to specify:

  • Bearer token strategies
  • Security strategies
  • Authentication guards
  • Authentication type
interface IApiControllerPropertiesRouteAuthentication {
    bearerStrategies?: Array<string>;
    guard: Type<IAuthGuard>;
    securityStrategies?: Array<string>;
    type: EApiAuthenticationType;
}

Example:

authentication: {
    bearerStrategies: ["accountAuthorization"],
    guard: AccountGuard,
    securityStrategies: ["accountRequestSignature", "accountRequestTimestamp"],
    type: EApiAuthenticationType.ACCOUNT
}

Request Handling

Request configuration includes:

  • Relations loading
  • Data transformation
  • Request validation
interface IApiControllerPropertiesRouteBaseRequest<E, R> {
    relations?: TApiControllerPropertiesRouteBaseRequestRelations<E>;
    transformers?: TApiControllerPropertiesRouteBaseRequestTransformers<E, R>;
    validators?: Array<IApiRequestValidator<E>>;
}

Relations Loading Strategies

Two strategies are available:

  1. AUTO: Automatically loads relations
  2. MANUAL: Manually specify relations to load
relations: {
    loadRelations: true,
    relationsLoadStrategy: EApiControllerLoadRelationsStrategy.MANUAL,
    relationsToLoad: ["bank"],
    servicesLoadStrategy: EApiControllerLoadRelationsStrategy.MANUAL,
    relationsServices: {
        bank: "bankService"
    }
}

Response Handling

Response configuration allows you to:

  • Specify relations to include
  • Transform response data
interface IApiControllerPropertiesRouteBaseResponse<E, R> {
    relations?: FindOptionsRelations<E>;
    transformers?: TApiControllerPropertiesRouteBaseResponseTransformers<E, R>;
}

Example:

response: {
    relations: {
        bank: true,
        currency: true
    },
    transformers: {
        [EApiDtoType.RESPONSE]: [
            {
                key: "user",
                type: EApiControllerRequestTransformerType.DYNAMIC,
                value: TRANSFORMER_VALUE_DTO_CONSTANT.REQUEST_IP
            }
        ]
    }
}

Data Transformation

Transformers can be configured for different DTO types:

  • BODY
  • QUERY
  • REQUEST
  • RESPONSE

Two types of transformers are available:

  1. DYNAMIC: Uses predefined constants
  2. STATIC: Uses static values
type TApiRequestTransformer<E> = {
    key: keyof IApiGetListResponseResult<E> | keyof Partial<E> | keyof TApiFunctionGetListProperties<E>;
} & (
    | {
        type: EApiControllerRequestTransformerType.DYNAMIC;
        value: (typeof TRANSFORMER_VALUE_DTO_CONSTANT)[keyof typeof TRANSFORMER_VALUE_DTO_CONSTANT];
    }
    | {
        type: EApiControllerRequestTransformerType.STATIC;
        value: string;
    }
);

Validation

Validators can be configured for requests with:

  • Error type
  • Exception class
  • Validation function
interface IApiRequestValidator<E> {
    errorType: EErrorStringAction;
    exception: TApiException;
    validationFunction: (entity: DeepPartial<E> | Partial<E> | TApiFunctionGetListProperties<E>) => boolean | Promise<boolean>;
}

Example:

validators: [
    {
        errorType: EErrorStringAction.ADDRESS_NOT_MATCH_PATTERN,
        exception: BadRequestException,
        validationFunction: (account: Partial<Account>): boolean => 
            (account.id?.includes("7") ? account.id.includes("AAAA") : true)
    }
]

Auto DTO Configuration

Auto DTO allows automatic Data Transfer Object configuration:

autoDto?: {
    [key in EApiDtoType]?: IApiControllerPropertiesRouteAutoDtoConfig;
};

Example:

autoDto: {
    [EApiDtoType.REQUEST]: {
        validators: [
            {
                constraintClass: HasAtLeastOneOfListedProperties,
                options: ["hybrid", "currencyId"]
            }
        ]
    }
}

Usage Example

Here's a complete example of a route configuration:

[EApiRouteType.CREATE]: {
    authentication: {
        bearerStrategies: ["accountAuthorization"],
        guard: AccountGuard,
        securityStrategies: ["accountRequestSignature", "accountRequestTimestamp"],
        type: EApiAuthenticationType.ACCOUNT
    },
    request: {
        relations: {
            loadRelations: true,
            relationsLoadStrategy: EApiControllerLoadRelationsStrategy.MANUAL,
            relationsServices: {
                bank: "bankService"
            },
            relationsToLoad: ["bank"],
            servicesLoadStrategy: EApiControllerLoadRelationsStrategy.MANUAL
        },
        transformers: {
            [EApiDtoType.BODY]: [
                {
                    key: "user",
                    type: EApiControllerRequestTransformerType.DYNAMIC,
                    value: TRANSFORMER_VALUE_DTO_CONSTANT.AUTHORIZED_ENTITY
                }
            ]
        },
        validators: [
            {
                errorType: EErrorStringAction.ADDRESS_NOT_MATCH_PATTERN,
                exception: BadRequestException,
                validationFunction: (account: Partial<Account>): boolean => 
                    (account.id?.includes("7") ? account.id.includes("AAAA") : true)
            }
        ]
    },
    response: {
        relations: {
            bank: true,
            currency: true
        },
        transformers: {
            [EApiDtoType.RESPONSE]: [
                {
                    key: "user",
                    type: EApiControllerRequestTransformerType.DYNAMIC,
                    value: TRANSFORMER_VALUE_DTO_CONSTANT.REQUEST_IP
                }
            ]
        }
    }
}

Best Practices

  1. Authentication

    • Always specify appropriate bearer and security strategies
    • Use proper guards for route protection
    • Choose the correct authentication type
  2. Relations

    • Use MANUAL strategy when you need precise control over relation loading
    • Use AUTO strategy for simpler cases
    • Always specify required services when using MANUAL strategy
  3. Validation

    • Implement comprehensive validation rules
    • Use appropriate error types and exceptions
    • Consider both sync and async validation functions
  4. Transformation

    • Use DYNAMIC transformers for runtime values
    • Use STATIC transformers for constant values
    • Consider the DTO type when applying transformations

Notes

  • All configurations are type-safe with TypeScript
  • Relations loading can be controlled at a granular level
  • Transformers can be applied at multiple stages of request/response handling
  • Validation can be both synchronous and asynchronous
  • Auto DTO configuration provides a simplified way to handle data transfer objects