forked from cms/tibi-svelte-starter
248 lines
5.0 KiB
TypeScript
248 lines
5.0 KiB
TypeScript
export interface CollectionDocument {
|
|
id?: string
|
|
insertTime?: Date
|
|
updateTime?: Date
|
|
[key: string]: any
|
|
}
|
|
|
|
export interface ReadCollectionOptions {
|
|
filter?: {
|
|
[key: string]: any
|
|
}
|
|
selector?: {
|
|
[key: string]: any
|
|
}
|
|
projection?: string
|
|
offset?: number
|
|
limit?: number
|
|
sort?: string[]
|
|
}
|
|
|
|
interface GetHookData {
|
|
/**
|
|
* true if only one document was requested via /COLLECTION/ID
|
|
*/
|
|
one?: boolean
|
|
/**
|
|
* get list of documents (only valid after stage "read" in "get" hook)
|
|
*/
|
|
results(): CollectionDocument[]
|
|
/**
|
|
* filter map only valid for "get" hooks
|
|
*/
|
|
filter?: {
|
|
[key: string]: any
|
|
}
|
|
/**
|
|
* selector map only valid for "get" hooks
|
|
*/
|
|
selector?: {
|
|
[key: string]: any
|
|
}
|
|
/**
|
|
* offset only valid for "get" hooks
|
|
*/
|
|
offset?: number
|
|
/**
|
|
* limit only valid for "get" hooks
|
|
*/
|
|
limit?: number
|
|
/**
|
|
* sort only valid for "get" hooks
|
|
*/
|
|
sort?: string[] | string
|
|
}
|
|
|
|
interface PostHookData {
|
|
/**
|
|
* post data only valid in "post" and "put" hooks
|
|
*/
|
|
data?: CollectionDocument
|
|
}
|
|
|
|
export interface HookContext extends GetHookData, PostHookData {
|
|
request(): {
|
|
method: string
|
|
remoteAddr: string
|
|
host: string
|
|
url: string
|
|
path: string
|
|
param(p: string): string
|
|
query(q: string): string
|
|
header(h: string): string
|
|
body(): string
|
|
}
|
|
|
|
/**
|
|
* read results from a collection
|
|
*
|
|
* @param colName collection name
|
|
* @param options options map
|
|
*/
|
|
readCollection(
|
|
colName: string,
|
|
options?: ReadCollectionOptions
|
|
): CollectionDocument[]
|
|
|
|
/**
|
|
* read count of documents for filter from a collection
|
|
*
|
|
* @param colName collection name
|
|
* @param options options map (only filter is valid)
|
|
*/
|
|
readCollectionCount(
|
|
colName: string,
|
|
options?: ReadCollectionOptions
|
|
): number
|
|
|
|
/**
|
|
* create a document in a collection
|
|
*
|
|
* @param colName collection name
|
|
* @param data data map
|
|
*/
|
|
createDocument(
|
|
colName: string,
|
|
data: CollectionDocument
|
|
): CollectionDocument
|
|
|
|
/**
|
|
* update a document in a collection
|
|
*
|
|
* @param colName collection name
|
|
* @param id id of entry
|
|
* @param data new/changed data
|
|
*/
|
|
updateDocument(
|
|
colName: string,
|
|
id: string,
|
|
data: CollectionDocument
|
|
): CollectionDocument
|
|
|
|
/**
|
|
* deletes one document by id from collection
|
|
*
|
|
* @param colName collection name
|
|
* @param id id of entry
|
|
*/
|
|
deleteDocument(colName: string, id: string): { message: "ok" }
|
|
|
|
/**
|
|
* deletes documents by filter from collection
|
|
*
|
|
* @param colName collection name
|
|
* @param options options map, only filter valid
|
|
*/
|
|
deleteDocuments(
|
|
colName: string,
|
|
options?: ReadCollectionOptions
|
|
): { message: "ok"; removed: number }
|
|
|
|
/**
|
|
* send an email
|
|
*
|
|
* @param options email options map
|
|
*/
|
|
mail(options: {
|
|
to: string | string[]
|
|
cc?: string | string[]
|
|
bcc?: string | string[]
|
|
subject?: string
|
|
from: string
|
|
fromName?: string
|
|
plain?: string
|
|
html?: string
|
|
attach?: string | string[]
|
|
}): void
|
|
|
|
/**
|
|
* execute a template code and return result
|
|
*
|
|
* @param code template code
|
|
* @param contextData template context map
|
|
*/
|
|
template(
|
|
code: string,
|
|
contextData?: {
|
|
[key: string]: any
|
|
}
|
|
): string
|
|
|
|
/**
|
|
* read a file relative to config dir and return its content
|
|
*
|
|
* @param path relative file path
|
|
*/
|
|
file(path: string): string
|
|
|
|
/**
|
|
* http request
|
|
*
|
|
* @param url url for request
|
|
* @param options request options
|
|
*/
|
|
fetch(
|
|
url: string,
|
|
options?: {
|
|
method?: string
|
|
headers?: { [key: string]: string }
|
|
body?: string
|
|
}
|
|
): {
|
|
status: number
|
|
statusText: string
|
|
headers: { [key: string]: string }
|
|
trailer: { [key: string]: string }
|
|
url: string
|
|
body: {
|
|
text(): string
|
|
json(): any
|
|
}
|
|
}
|
|
|
|
/**
|
|
* dumps data to header and server log
|
|
*
|
|
* @param toDump data to dump
|
|
*/
|
|
dump(...toDump: any): void
|
|
|
|
/**
|
|
* set response header
|
|
*
|
|
* @param name header name
|
|
* @param value value
|
|
*/
|
|
header(name: string, value: any): void
|
|
|
|
/**
|
|
* get JWT authentication
|
|
*/
|
|
auth(): {
|
|
id: string
|
|
username: string
|
|
role: number
|
|
permissions: string[]
|
|
}
|
|
|
|
/**
|
|
* get Sentry trace id
|
|
*/
|
|
sentryTraceId(): string
|
|
}
|
|
|
|
export interface HookException {
|
|
status?: number
|
|
html?: string
|
|
[key: string]: any
|
|
}
|
|
|
|
export interface HookResponse extends GetHookData, PostHookData {
|
|
data?: CollectionDocument
|
|
results?: any
|
|
}
|
|
|
|
declare global {
|
|
var context: HookContext
|
|
}
|