|
| 1 | +import { RequestHandler, Response, Request, NextFunction, Application } from 'express'; |
| 2 | +import * as Sequelize from 'sequelize'; |
| 3 | + |
| 4 | +// Everything related to Forest initialization |
| 5 | + |
| 6 | +export interface LianaOptions { |
| 7 | + objectMapping: typeof Sequelize; |
| 8 | + envSecret: string; |
| 9 | + authSecret: string; |
| 10 | + connections: Record<string, Sequelize.Sequelize>; |
| 11 | + includedModels?: string[]; |
| 12 | + excludedModels?: string[]; |
| 13 | + configDir?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export function init(options: LianaOptions): Promise<Application>; |
| 17 | + |
| 18 | +export interface DatabaseConfiguration { |
| 19 | + name: string, |
| 20 | + modelsDir: string, |
| 21 | + connection: { |
| 22 | + url: string, |
| 23 | + options: Sequelize.Options, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Everything related to Forest Authentication |
| 28 | + |
| 29 | +export function ensureAuthenticated(request: Request, response: Response, next: NextFunction): void; |
| 30 | + |
| 31 | +// Everything related to Forest constants |
| 32 | + |
| 33 | +export const PUBLIC_ROUTES: string[]; |
| 34 | + |
| 35 | +// Everything related to record manipulation |
| 36 | + |
| 37 | +interface RecordsSerialized { |
| 38 | + data: Record<string, unknown>[], |
| 39 | + included: Record<string, unknown>[], |
| 40 | +} |
| 41 | + |
| 42 | +export class AbstractRecordTool<M extends Sequelize.Model> { |
| 43 | + constructor(model: Sequelize.ModelCtor<M>) |
| 44 | + serialize(records: M | M[]): Promise<RecordsSerialized>; |
| 45 | +} |
| 46 | + |
| 47 | +export class RecordGetter<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 48 | + get(recordId: string): Promise<M>; |
| 49 | +} |
| 50 | + |
| 51 | +export class RecordsGetter<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 52 | + getAll(query: Query): Promise<M[]>; |
| 53 | + getIdsFromRequest(request: Request): Promise<string[]>; |
| 54 | +} |
| 55 | + |
| 56 | +export class RecordsCounter<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 57 | + count(query: Query): Promise<number>; |
| 58 | +} |
| 59 | + |
| 60 | +export class RecordsExporter<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 61 | + streamExport(response: Response, query: Query): Promise<void>; |
| 62 | +} |
| 63 | + |
| 64 | +export class RecordUpdater<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 65 | + deserialize(body: Record<string, unknown>): Promise<Record<string, unknown>>; |
| 66 | + update(record: Record<string, unknown>, recordId: string): Promise<M>; |
| 67 | +} |
| 68 | + |
| 69 | +export class RecordCreator<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 70 | + deserialize(body: Record<string, unknown>): Promise<Record<string, unknown>>; |
| 71 | + create(record: Record<string, unknown>): Promise<M>; |
| 72 | +} |
| 73 | + |
| 74 | +export class RecordRemover<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 75 | + remove(recordId: string | number): Promise<void>; |
| 76 | +} |
| 77 | + |
| 78 | +export class RecordsRemover<M extends Sequelize.Model> extends AbstractRecordTool<M> { |
| 79 | + remove(recordIds: string[] | number[]): Promise<void>; |
| 80 | +} |
| 81 | + |
| 82 | +// Everything related to Forest permissions |
| 83 | + |
| 84 | +export class PermissionMiddlewareCreator { |
| 85 | + constructor(collectionName: string) |
| 86 | + list(): RequestHandler; |
| 87 | + export(): RequestHandler; |
| 88 | + details(): RequestHandler; |
| 89 | + create(): RequestHandler; |
| 90 | + update(): RequestHandler; |
| 91 | + delete(): RequestHandler; |
| 92 | + smartAction(): RequestHandler; |
| 93 | +} |
| 94 | + |
| 95 | +// Everything related to Forest Charts |
| 96 | + |
| 97 | +export interface StatSerialized { |
| 98 | + data: { |
| 99 | + type: string, |
| 100 | + id: string, |
| 101 | + attributes: { |
| 102 | + value: any[] |
| 103 | + } |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +export class StatSerializer { |
| 108 | + constructor(stats: { value: any[] }) |
| 109 | + perform(): StatSerialized; |
| 110 | +} |
| 111 | + |
| 112 | +// Everything related to Forest request params |
| 113 | + |
| 114 | +export interface Page { |
| 115 | + number: number; |
| 116 | + size: number; |
| 117 | +} |
| 118 | + |
| 119 | +export interface Filter { |
| 120 | + field: string; |
| 121 | + operator: string; |
| 122 | + value: string; |
| 123 | +} |
| 124 | + |
| 125 | +export enum Aggregator { |
| 126 | + AND = 'and', |
| 127 | + OR = 'or' |
| 128 | +} |
| 129 | + |
| 130 | +export interface AggregatedFilters { |
| 131 | + aggregator: Aggregator; |
| 132 | + conditions: Filter[]; |
| 133 | +} |
| 134 | + |
| 135 | +export interface Query { |
| 136 | + timezone?: string; |
| 137 | + search?: string; |
| 138 | + fields?: {[key: string]: string}; |
| 139 | + sort?: string; |
| 140 | + filters?: Filter|AggregatedFilters; |
| 141 | + page?: Page; |
| 142 | + searchExtended?: string; |
| 143 | +} |
| 144 | + |
| 145 | +// Everything related to Forest collection configuration |
| 146 | + |
| 147 | +export interface SmartFieldValueGetter<M extends Sequelize.Model = any> { |
| 148 | + (record: M): any; |
| 149 | +} |
| 150 | + |
| 151 | +export interface SmartFieldValueSetter<M extends Sequelize.Model = any> { |
| 152 | + (record: M, fieldValue: any): any; |
| 153 | +} |
| 154 | + |
| 155 | +export interface SmartFieldSearchQuery { |
| 156 | + include: string[], |
| 157 | + where: Sequelize.WhereOptions, |
| 158 | +} |
| 159 | + |
| 160 | +export interface SmartFieldSearcher { |
| 161 | + (query: SmartFieldSearchQuery, search: string): SmartFieldSearchQuery; |
| 162 | +} |
| 163 | + |
| 164 | +export interface SmartFieldFiltererFilter { |
| 165 | + condition: Filter, |
| 166 | + where: Record<symbol, Record<symbol, any> | any>, |
| 167 | +} |
| 168 | + |
| 169 | +export interface SmartFieldFilterer { |
| 170 | + (filter: SmartFieldFiltererFilter): Sequelize.WhereOptions; |
| 171 | +} |
| 172 | + |
| 173 | +export interface SegmentAggregationCreator<M extends Sequelize.Model = any> { |
| 174 | + (model: M): Sequelize.WhereOptions; |
| 175 | +} |
| 176 | + |
| 177 | +type FieldType = 'Boolean' | 'Date' | 'Dateonly' | 'Enum' | 'File' | 'Number' | 'String' | ['Enum'] | ['Number'] | ['String']; |
| 178 | + |
| 179 | +type FieldEnumsType = string[] | number[] | Date[] | boolean[]; |
| 180 | + |
| 181 | +export interface SmartFieldOptions { |
| 182 | + field: string; |
| 183 | + description?: string; |
| 184 | + type: FieldType; |
| 185 | + isFilterable?: boolean; |
| 186 | + isReadOnly?: boolean; |
| 187 | + isRequired?: boolean; |
| 188 | + reference?: string; |
| 189 | + enums?: FieldEnumsType; |
| 190 | + defaultValue?: any; |
| 191 | + get?: SmartFieldValueGetter; |
| 192 | + set?: SmartFieldValueSetter; |
| 193 | + search?: SmartFieldSearcher; |
| 194 | + filter?: SmartFieldFilterer; |
| 195 | +} |
| 196 | + |
| 197 | +export interface SmartActionField { |
| 198 | + field: string, |
| 199 | + description?: string, |
| 200 | + type: FieldType, |
| 201 | + isRequired?: boolean, |
| 202 | + isReadOnly?: boolean, |
| 203 | + enums?: FieldEnumsType, |
| 204 | + defaultValue?: any, |
| 205 | + reference?: string, |
| 206 | +} |
| 207 | + |
| 208 | +export interface SmartActionHookField extends SmartActionField { |
| 209 | + value: any, |
| 210 | +} |
| 211 | + |
| 212 | +export interface SmartActionLoadHookField extends SmartActionHookField { |
| 213 | + position: number, |
| 214 | +} |
| 215 | + |
| 216 | +export interface SmartActionLoadHook<M extends Sequelize.Model = any> { |
| 217 | + (context: { fields: Record<string, SmartActionLoadHookField>, record: M }): Record<string, SmartActionLoadHookField> |
| 218 | +} |
| 219 | + |
| 220 | +export interface SmartActionChangeHookField extends SmartActionHookField { |
| 221 | + previousValue: any, |
| 222 | +} |
| 223 | + |
| 224 | +export interface SmartActionChangeHook<M extends Sequelize.Model = any> { |
| 225 | + (context: { fields: Record<string, SmartActionChangeHookField>, record: M }): Record<string, SmartActionChangeHookField> |
| 226 | +} |
| 227 | + |
| 228 | +export interface SmartActionHooks { |
| 229 | + load: SmartActionLoadHook; |
| 230 | + change: Record<string, SmartActionChangeHook>; |
| 231 | +} |
| 232 | + |
| 233 | +export interface SmartActionOptions { |
| 234 | + name: string; |
| 235 | + type?: 'global' | 'bulk' | 'single'; |
| 236 | + fields?: SmartActionField[]; |
| 237 | + download?: boolean; |
| 238 | + endpoint?: string; |
| 239 | + httpMethod?: string; |
| 240 | + hooks?: SmartActionHooks; |
| 241 | +} |
| 242 | + |
| 243 | +export interface SmartSegmentOptions { |
| 244 | + name: string; |
| 245 | + where: SegmentAggregationCreator; |
| 246 | +} |
| 247 | + |
| 248 | +export interface CollectionOptions { |
| 249 | + fields?: SmartFieldOptions[]; |
| 250 | + actions?: SmartActionOptions[]; |
| 251 | + segments?: SmartSegmentOptions[]; |
| 252 | +} |
| 253 | + |
| 254 | +export function collection(name: string, options: CollectionOptions): void; |
| 255 | + |
| 256 | +export function errorHandler(): RequestHandler; |
0 commit comments