-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.d.ts
More file actions
60 lines (46 loc) · 1.66 KB
/
index.d.ts
File metadata and controls
60 lines (46 loc) · 1.66 KB
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
type UserId = string
type DocumentId = string
type DocumentObj<T extends {} = {}> = T & {
_id: DocumentId
acl_read: UserId[]
acl_write: UserId[]
userId: UserId
created: number
edited?: number
}
declare module '@mathio28/next-api' {
type ApiSecurity = 0 | 1 | 2
interface ApiConfig {
mongoDbUrl?: string
authCookieName?: string
security?: ApiSecurity
sessionTime?: number
}
type GetResult<T> = T extends Array<infer I> ? DocumentObj<I>[] : DocumentObj<T>
type Database = {
find: <T extends {}>(collection: string, idOrQuery: string | {}, sort?: string) => GetResult<T>
save: <T extends {}>(collection: string, body: T, id?: string) => GetResult<T>
remove: (collection: string, id: string | string[]) => number
}
type ApiHandler = (req: any, res: any) => void
type ApiHandlerWithDb = (req: any, res: any, db: Database) => void
type MakeApi = (config: ApiConfig, before?: ApiHandlerWithDb, after?: ApiHandlerWithDb) => ApiHandler
const nextApi: MakeApi
export = nextApi
}
declare module '@mathio28/next-api/fetch' {
type EditedDocument<T extends {}> = DocumentObj<T> & {
edited: number
}
type DeleteBody = {
_id: string | string[]
}
interface DeleteResponse {
deleted: number
}
type GetResult<T> = T extends Array<infer I> ? DocumentObj<I>[] : DocumentObj<T>
export const get: <T extends {} | []>(path: string) => Promise<GetResult<T>>
export const post: <T extends {} | []>(path: string, body: T) => Promise<GetResult<T>>
export const put: <T extends {}>(path: string, body: T) => Promise<EditedDocument<T>>
export const del: (path: string, body?: DeleteBody) => Promise<DeleteResponse>
}