UNPKG

@expressive-analytics/deep-thought-service

Version:

Typescript conversion of Deep Thought Services (formerly providers)

174 lines (173 loc) 6.84 kB
import { DTVerifier } from "./DTVerifier"; import { DTResponse } from './DTResponse'; import { DTModel, DTParams, DTStore, DTQueryBuilder } from '@expressive-analytics/deep-thought-js'; export declare type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; export declare type DTAction = "get" | "list" | "count" | "create" | "create_many" | "update" | "update_many" | "upsert" | "remove" | "remove_all"; export declare type DTRequest = { method: HTTPMethod; params: Record<string, any>; headers: Record<string, any>; }; export declare class DTHTTPError extends Error { code: number; header: string; message: string; constructor(code: any, header: any, message?: string); } export declare class DTService { request: DTRequest; protected $meta_model?: string; protected $model?: typeof DTModel; protected $entity_model: typeof DTModel; protected $public?: string[]; /** * determines which automatic actions are available, defaults to "read-only" * valid values include: get, list, count, create, create_many, * update, update_many, upsert, remove, remove_all */ protected automatic_actions: DTAction[]; protected archive_column?: string; protected lookup_column: string; protected creation_column: any; protected modified_column: any; params: DTParams; session?: Record<string, any>; response?: DTResponse; verifier?: DTVerifier; db?: DTStore; constructor(verifier?: DTVerifier); actionSessionDestroy(): void; setParams(params: Record<string, any>): void; get model(): typeof DTModel; set model(val: typeof DTModel); /** @name Request Handling * Session setup and action delivery */ /** * A convenience method for handling a standard request and sending a response */ handleRequest(req: DTRequest, rsp: DTResponse): void; verifyRequest(action: any): boolean; /** * performs an action by name * @param action - the action to perform (uses 'act' param key, if null) * @NOTE if action is the name of a method, the appropriate method is called */ protected performAction(action?: any): void; setResponse(response: any): void; responseCode(): number; /** @return returns null so that it may be chained with an action's return statement */ setResponseCode(code: any): any; protected recordRequest(action: any): void; /** @internal */ protected actionExists(action: any): boolean; /** @name Automatic Actions * Default behavior for providers with a defined model */ /** * get a MODEL * @http_method get * @param object dtf a querybuilder filter * @return ENTITY_MODEL returns an object */ actionGet(): DTModel | "unauthorized action (get)"; /** * get a random set of MODEL objects * @http_method GET * @param integer draws the number of objects to return * @param string wt_col the column or value to use for weighing picks * @param integer seed a seed for the random generator * @param object dtf a querybuilder filter * @return array[MODEL] returns a list of random objects */ actionPick(): DTModel[] | "unauthorized action (pick)"; /** * get a list of MODEL objects * @http_method get * @param object dtf a querybuilder filter * @param integer dt_ct number of records per page, triggers pagination mode * @param integer dt_pg page number for paginated results, triggers pagination mode * @param string dt_order the column to order by * @param boolean dt_desc determines whether records are in descending order * @param boolean dt_proxy determines whether has-many properties are expanded * @param string dt_q a basic search string * @param string dt_col the column to apply the basic search string dt_q * @return array[MODEL] returns a list of objects, possibly paginated */ actionList(): DTModel[] | "unauthorized action (list)" | { total: number; items: DTModel[]; }; /** override to customize simple filter behavior */ protected filterBy(qb: any, col: any, str: any): any; /** override to customize advanced filter behavior */ protected advancedFilterBy(qb: any): DTQueryBuilder; protected enforce(qb: any, action: any, model?: any): any; /** * @param order an array of order/desc pairs * @param desc string determines whether to sort in descending order * override to customize sorting behavior * @return returns the query builder with a new orderBy setting */ protected orderBy(qb: DTQueryBuilder, order: any): DTQueryBuilder; protected orderCol(qb: DTQueryBuilder, order: any, uniq: any): string; /** * get a count of MODEL objects * @http_method GET * @param object dtf a querybuilder filter * @return integer returns the total count of matching objects */ actionCount(): number | "unauthorized action (count)"; /** * create a new MODEL * @http_method POST * @param ENTITY_MODEL any valid model properties * @return ENTITY_MODEL the newly created object */ actionCreate(): readonly [DTModel, import("@expressive-analytics/deep-thought-js").dataDelta?] | "unauthorized action (create)"; /** * create multiple MODEL objects * @http_method PUT * @param array a list of objects to create */ actionCreateMany(): string; /** * update a MODEL * @http_method POST * @param ENTITY_MODEL a subset of properties of the model to update * @return ENTITY_MODEL returns the updated object, or 404 if not found */ actionUpdate(): DTModel | "unauthorized action (update)" | "record does not exist"; /** * update many MODEL objects * @http_method POST * @return array[ENTITY_MODEL] returns the updated objects */ actionUpdateMany(): any[] | "unauthorized action (update_many)"; /** * update or create a MODEL, as necessary * @http_method PATCH * @return MODEL returns the existing or newly created object */ actionUpsert(): readonly [DTModel, import("@expressive-analytics/deep-thought-js").dataDelta?] | "unauthorized action (upsert)"; /** * loop through the param +dt_items+ and upsert * @http_method PATCH * @return array[MODEL] returns the upserted list */ actionUpsertMany(): any[] | "unauthorized action (upsert_many)"; /** * remove a MODEL * @http_method DELETE * @param integer id the record to remove */ actionRemove(): string; /** * remove multiple MODEL * @http_method DELETE * @param array the list of records to delete */ actionRemoveMany(): string; static factory(p: any, db?: DTStore, verifier?: DTVerifier): any; isAutomatic(action: any): boolean; }