Skip to content

Instantly share code, notes, and snippets.

@nickmessing
Created April 19, 2018 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickmessing/cb961d03f5dd851933057add13809794 to your computer and use it in GitHub Desktop.
Save nickmessing/cb961d03f5dd851933057add13809794 to your computer and use it in GitHub Desktop.
export interface DefaultFields {
id: number
createdAt: string
updatedAt: string
}
type QuerySortSubField<T> = { [K in keyof T]?: -1 | 1 }
interface $InHelper<T> {
$in?: T[]
}
interface $NeHelper<T> {
$ne?: T
}
interface $LikeHelper<T> {
$like?: T
}
interface LtGt {
$lt?: number
$gt?: number
$lte?: number
$gte?: number
}
export type Optionize<T> = { [K in keyof T]?: T[K] | null }
export type QueryProps<T> = { [K in keyof T]?: T[K] | $InHelper<T[K]> | $NeHelper<T[K]> | $LikeHelper<T[K]> | LtGt }
export interface QuerySortField<T> {
$sort?: QuerySortSubField<T>
}
export interface CommonQueryFields {
$limit?: number
$skip?: number
}
export interface QueryOr<T> {
$or?: Array<Query<T>>
}
export type Query<T> = CommonQueryFields & QueryProps<T> & QuerySortField<T> & QueryOr<T>
export interface FindParams<T> {
query?: Query<T>
paginate?: boolean
}
export interface FindResponse<T> {
total: number
limit: number
skip: number
data: T[]
}
export type StateIds = number[]
export interface StateKeyedById<T> {
[K: number]: T
}
export type StateCurrentId = number
export type StateCopy<T> = T
export type StateServicePath = string
export type StateIdField = string
export type StatePaginate = boolean
export type StateIsFindPending = boolean
export type StateIsGetPending = boolean
export type StateIsCreatePending = boolean
export type StateIsUpdatePending = boolean
export type StateIsPatchPending = boolean
export type StateIsRemovePending = boolean
export type GetterList<T> = T[]
export type GetterFind<T> = (params?: FindParams<T>) => FindResponse<T>
export type GetterGet<T> = (id: number, params?: FindParams<T>) => T
export type GetterCurrent<T> = T
export type GetterGetCopy<T> = T
export type ActionFind<T> = (params?: FindParams<T>) => Promise<FindResponse<T>>
export type ActionGet<T> = (idOrParams: number | [number, FindParams<T>]) => Promise<T>
export type ActionCreate<T> = (entry: Optionize<T>) => Promise<T>
export type ActionUpdate<T> = (idEntryFindparams: [number, T, FindParams<T>]) => Promise<T>
export type ActionPatch<T> = (idEntryFindparams: [number, Optionize<T>, FindParams<T>]) => Promise<T>
export type ActionRemove<T> = (id: number) => Promise<T>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment