All files globalTypes.ts

100% Statements 64/64
100% Branches 2/2
100% Functions 1/1
100% Lines 64/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 651x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 173x  
import { ObjectType, Validation, ValidationResult } from 'yaschva'
 
export type AuthenticationDefinition = (string | {createdBy: boolean})[] | boolean
export type ManageableFields = { createdBy?: boolean, id?:boolean }
 
export type HttpMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
export type SearchTypes = 'textSearch' | 'full' | 'idOnly' | ObjectType;
 
export type AuthInput = { permissions? : string [], sub?: string }
 
export type KeyValueStoreTypes ='memory' | 'workerKV' | {custom:string}
export type BackEndTypes = 'manual' | 'elasticsearch' | 'key-value'
export namespace Implementations {
  /** Expect manual implementation, nothing is generated**/
  export type manual = {type:'manual'}
  /** Generate generic elasticsearch backend **/
  export type elasticsearch = { type: 'elasticsearch', index: string, maxResults?: number }
  /** Generate a generic key value store implementation use either:
   *  - in memory : only for testing / dev, no persistance
   *  - workerKV : using the HTTP API for cloudflare worker key value store.
   *               requires ENV vars to be set
   *  - custom : Use the implementation provided under global.customKV[custom]
   * **/
  export type keyValue = { type: 'key-value', prefix:string, backend: KeyValueStoreTypes, allowGetAll?: true}
}
export type Implementation =
  {type:string} & (
  Implementations.manual |
  Implementations.elasticsearch |
  Implementations.keyValue)
 
export type ContractType<METHOD extends HttpMethods, IMPL extends Implementation, IN, OUT> = {
  name: string
  type: METHOD
  manageFields: ManageableFields,
  handle?: (input: IN, auth: AuthInput, contract:ContractType<METHOD, IMPL, IN, OUT>, id?:string) => Promise<HandleResult<OUT>>
  arguments: Validation
  returns: Validation
  authentication: AuthenticationDefinition
  implementation: IMPL
}
export type AnyContract = ContractType<any, any, any, any>
 
export type HandleErrorResponse ={
  errorType: string
  data?: any
  status: number
  errors: ValidationResult| string[]
  result?: void
}
 
export type HandleResultSuccess<OUT> = {
  result: OUT
  status?:number,
  cursor?:string
  more?:boolean
  errors?: void
  errorType?:void
}
 
export type HandleResult<OUT> = HandleErrorResponse | HandleResultSuccess<OUT>;
 
export const isContractInError = (tbd: HandleResult<any>): tbd is HandleErrorResponse =>
  Boolean(tbd.errors)