@alfercom/default-types
v0.1.12
Published
<div align="center"> <img src="./assets/alfercom-logo.png"> <img width="100" src="./assets/cardinal-logo.png"> </div>
Downloads
581
Readme
This repo provide a series of default types commonly used in alfercom/cardinal projects.
Index
Installation
npm i --save @alfercom/default-typesPrimitive types
This library exposes a set of extended primitive types useful for quick implementation when declaring types that may not be immediately valued. These extensions help the developer to respect the syntax rules imposed by the project linter and to maintain a correct and simple legibility of the code.
Normal primitive in typescript extended by this package are
string | number | booleanThese can be called with their extension ' _ ', which adds the definitions of null and undefined, such as:
_string | _number | _booleanUsage example
We want to define a user class that has as parameters: username, age and premium, in that case our class would be:
class User extends UserInterface {
// Wrong declaration emitted by linter and refused by compiler
public username: string;
public age: number;
public premium: boolean;
public getUsername(): string {
return this.username
}
}If we have configurated our linter and tsconfig correctly, they will throw exceptions on our declarations cause we have not initialized our variables in any way. In order to eliminate these errors we should make the following changes to our code:
class User extends UserInterface {
// These statements will accepted by linter and compiler
public readonly username: string | undefined;
public readonly age: number | undefined;
public readonly premium: boolean | undefined;
public getUsername(): string | undefined {
return this.username;
}
}That's right, but not very nice is it? So we can use our extended primitives to make this look prettier
class User extends UserInterface {
public readonly username: _string;
public readonly age: _number;
public readonly premium: _boolean;
public getUsername(): _string {
return this.username;
}
}Javascript interfaces
As like the extendend primitives this library provides olso some extensions of Javascript interfaces.
Normal javascript interfaces extended by this package are
Data | Array | Record(Object)These can be called with their extension ' _ ', which adds the definitions of null and undefined, such as:
_Date | _Array | _ObjectCustom types
/**
* Declare that a type can be Null or Undefined
*/
type _nullable = null | undefined;
/**
* Type that provides 184 ISO 639-1 languages
*/
type _Locale = IsoLang | _nullable;
/**
* Extends mongo ObjectId instance and declare it nullable
*/
type _ObjectId = ObjectId | _nullable;
/**
* Basic event data
*/
type EventData = {
/**
* The event name
*/
event: DefaultRecordEvent;
/**
* The user that generate the event
*/
accountId: _ObjectId;
/**
* The Date of the event
*/
date: _Date;
/**
* Message associated to the event
*/
message: _string;
} | _nullable;
/**
* 184 ISO 639-1 languages
*/
type IsoLang = "en" | "it" |... ;Interfaces
Error
/** * Error interface */ interface _error { /** * Error Type */ errorType: string; /** * The operation that generates the exception */ operation?: string; /** * The source that call the failed operation */ onRequest?: string; /** * Contraints */ constraints?: Contraints[]; /** * Optional error messages */ messages: string[]; } /** * Contraints */ interface Contraints { /** * Type */ type: string; /** * Raw message */ rawMessage: string; /** * Message */ message: string; /** * Priority */ priority: string; }Query object
/** * This interface describe the model that should be used as params for dynamic query */ interface BaseQueryObject { /** * Filter operations based on entity dictionary */ filters: FilterOperation[]; /** * Pagination offset */ offset: _number /** * Pagination limit */ limit: _number /** * Sort param */ order: _string | _Object; }request - response
/** * This interface describe how an api request body should be at least. */ interface BaseAPIRequest { /** * */ readonly [key: string]: any; /** * */ readonly extra?: any; } /** * This interface describe how a query request api should be at least */ interface BaseQueryRequest extends BaseAPIRequest, BaseQueryObject { } /** * An interface that describe and provide base properties of an API operation response */ interface BaseAPIResponse { /** * Provide interface for operation result */ result: boolean; /** * Optional messages to attach at response */ messages?: _string[]; /** * Error validation catched by operation execution */ validations?: _error[]; /** * Provide next operation instruction to execute after operation result */ nextOperation?: NextOperation; } /** * An interface that describe and provide base properties of a query API operation response */ interface BaseQueryResponse<T> extends BaseAPIResponse, BaseQueryObject { /** * Number of results */ total: _number /** * Query results */ data: T | T[]; }next operation
/** * This interface describe a way to trigger an event/function after an api response */ interface NextOperation { /** * The event/function to trigger */ event: any; /** * The params to pass as arguments to the triggered event/function */ params?: any; }
Enumerations
Error
/** * Base event enumeration that enum the events of CREATION | UPDATE | DELETE */ export enum DefaultRecordEvent { CREATED = "CREATED", UPDATED = "UPDATED", DELETED = "DELETED", }
Classes
Database record
/** * Standard Database Record */ export class DatabaseRecord { /** * Record's realm ID */ public realmId: _ObjectId; /** * Record's creation event */ public creation: EventData; /** * Record's last edit event */ public lastEdit: EventData; /** * Record's delete flag */ public deleted: boolean; /** * Record's delete event */ public deleteInfo: EventData; /** * Record's version */ public version: _string; constructor(arg: DatabaseRecord) { } /** * Initialize db record on instance creation * @param account * @param message */ public initializeRecordData(account?: any, message?: string): void /** * Initialize delete properties of an entity in order to declare it deleted * ATTENTION: This operation doesn't write on db! * @param args */ public declareDeleted(args?: { accountId: _ObjectId, message: _string }): void }
