2021-08-16 10:23:45 +02:00
|
|
|
export interface CollectionDocument {
|
|
|
|
id?: string
|
|
|
|
insertTime?: Date
|
|
|
|
updateTime?: Date
|
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
export interface DbReadOptions {
|
2021-08-16 10:23:45 +02:00
|
|
|
filter?: {
|
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
selector?: {
|
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
projection?: string
|
|
|
|
offset?: number
|
|
|
|
limit?: number
|
|
|
|
sort?: string[]
|
|
|
|
}
|
|
|
|
|
2021-08-16 10:48:03 +02:00
|
|
|
interface GetHookGetOnlyData {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* 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[]
|
2021-08-16 10:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface GetHookData {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface DbPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* read results from a collection
|
|
|
|
*
|
|
|
|
* @param colName collection name
|
|
|
|
* @param options options map
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
find(
|
2021-08-16 10:23:45 +02:00
|
|
|
colName: string,
|
2021-08-19 14:22:42 +02:00
|
|
|
options?: DbReadOptions
|
2021-08-16 10:23:45 +02:00
|
|
|
): CollectionDocument[]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read count of documents for filter from a collection
|
|
|
|
*
|
|
|
|
* @param colName collection name
|
|
|
|
* @param options options map (only filter is valid)
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
count(
|
2021-08-16 10:23:45 +02:00
|
|
|
colName: string,
|
2021-08-19 14:22:42 +02:00
|
|
|
options?: DbReadOptions
|
2021-08-16 10:23:45 +02:00
|
|
|
): number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a document in a collection
|
|
|
|
*
|
|
|
|
* @param colName collection name
|
|
|
|
* @param data data map
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
create(
|
2021-08-16 10:23:45 +02:00
|
|
|
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
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
update(
|
2021-08-16 10:23:45 +02:00
|
|
|
colName: string,
|
|
|
|
id: string,
|
|
|
|
data: CollectionDocument
|
|
|
|
): CollectionDocument
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deletes one document by id from collection
|
|
|
|
*
|
|
|
|
* @param colName collection name
|
|
|
|
* @param id id of entry
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
delete(colName: string, id: string): { message: "ok" }
|
2021-08-16 10:23:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* deletes documents by filter from collection
|
|
|
|
*
|
|
|
|
* @param colName collection name
|
|
|
|
* @param options options map, only filter valid
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
deleteMany(
|
2021-08-16 10:23:45 +02:00
|
|
|
colName: string,
|
2021-08-19 14:22:42 +02:00
|
|
|
options?: DbReadOptions
|
2021-08-16 10:23:45 +02:00
|
|
|
): { message: "ok"; removed: number }
|
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 16:00:35 +02:00
|
|
|
interface SmtpPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* send an email
|
|
|
|
*
|
|
|
|
* @param options email options map
|
|
|
|
*/
|
2021-08-19 16:00:35 +02:00
|
|
|
sendMail(options: {
|
2021-08-16 10:23:45 +02:00
|
|
|
to: string | string[]
|
|
|
|
cc?: string | string[]
|
|
|
|
bcc?: string | string[]
|
|
|
|
subject?: string
|
|
|
|
from: string
|
|
|
|
fromName?: string
|
|
|
|
replyTo?: string
|
|
|
|
plain?: string
|
|
|
|
html?: string
|
|
|
|
attach?: string | string[]
|
|
|
|
}): void
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface FsPackage {
|
|
|
|
/**
|
|
|
|
* read a file relative to config dir and return its content
|
|
|
|
*
|
|
|
|
* @param path relative file path
|
|
|
|
*/
|
|
|
|
readFile(path: string): string
|
2021-08-16 10:23:45 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
/**
|
|
|
|
* stat file or directory
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
*/
|
|
|
|
stat(path: string): {
|
|
|
|
name: string
|
|
|
|
size: number
|
|
|
|
isDir: boolean
|
|
|
|
modTime: string
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list directory entries
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
*/
|
|
|
|
readDir(path: string): {
|
|
|
|
name: string
|
|
|
|
size: number
|
|
|
|
isDir: boolean
|
|
|
|
modTime: string
|
|
|
|
}[]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make directory and all missing sub directories, no error if directory already exists
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
*/
|
|
|
|
mkDir(path: string): void
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove file or empty directory
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
*/
|
|
|
|
remove(path: string): void
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:00:35 +02:00
|
|
|
interface TplPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* execute a template code and return result
|
|
|
|
*
|
|
|
|
* @param code template code
|
|
|
|
* @param contextData template context map
|
|
|
|
*/
|
2021-08-19 15:02:01 +02:00
|
|
|
execute(
|
2021-08-16 10:23:45 +02:00
|
|
|
code: string,
|
|
|
|
contextData?: {
|
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
): string
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
2021-08-16 10:23:45 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface HttpPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
2021-08-16 10:23:45 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface DebugPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* dumps data to header and server log
|
|
|
|
*
|
|
|
|
* @param toDump data to dump
|
|
|
|
*/
|
|
|
|
dump(...toDump: any): void
|
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
/**
|
|
|
|
* get Sentry trace id
|
|
|
|
*/
|
|
|
|
sentryTraceId(): string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ResponsePackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* set response header
|
|
|
|
*
|
|
|
|
* @param name header name
|
|
|
|
* @param value value
|
|
|
|
*/
|
|
|
|
header(name: string, value: any): void
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
2021-08-16 10:23:45 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface UserPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* get JWT authentication
|
|
|
|
*/
|
|
|
|
auth(): {
|
|
|
|
id: string
|
|
|
|
username: string
|
|
|
|
role: number
|
|
|
|
permissions: string[]
|
|
|
|
}
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
2021-08-16 10:23:45 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface BcryptPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* hash password via bcrypt algo
|
|
|
|
*
|
|
|
|
* @param password clear text password
|
|
|
|
* @param options hashing options
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
hash(
|
2021-08-16 10:23:45 +02:00
|
|
|
password: string,
|
|
|
|
options?: {
|
|
|
|
cost?: number
|
|
|
|
}
|
|
|
|
): string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check password against hashed password via bcrypt algo
|
|
|
|
*
|
|
|
|
* @param password clear text password
|
|
|
|
* @param hash hashed password
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
check(password: string, hash: string): boolean
|
|
|
|
}
|
2021-08-16 10:23:45 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface JwtPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* create a jwt signed token string
|
|
|
|
*
|
|
|
|
* @param claims data object
|
|
|
|
* @param options options (secret, validityDuration = expiry in X seconds)
|
|
|
|
*/
|
2021-08-19 14:32:38 +02:00
|
|
|
create(
|
2021-08-16 10:23:45 +02:00
|
|
|
claims: {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
|
|
|
options?: {
|
|
|
|
secret?: string
|
|
|
|
validityDuration?: number
|
|
|
|
}
|
|
|
|
): string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse jwt token string
|
|
|
|
*
|
|
|
|
* @param token token string
|
|
|
|
* @param options options (secret)
|
|
|
|
*/
|
2021-08-19 14:32:38 +02:00
|
|
|
parse(
|
2021-08-16 10:23:45 +02:00
|
|
|
token: string,
|
|
|
|
options?: {
|
|
|
|
secret?: string
|
|
|
|
}
|
|
|
|
): {
|
|
|
|
error?: string
|
|
|
|
valid: boolean
|
|
|
|
method: {
|
|
|
|
Name: string
|
|
|
|
Hash: number
|
|
|
|
}
|
|
|
|
header: {
|
|
|
|
alg: string
|
|
|
|
typ: string
|
|
|
|
}
|
|
|
|
claims: {
|
|
|
|
exp?: number
|
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
2021-08-16 10:23:45 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface CharsetPackage {
|
2021-08-16 10:23:45 +02:00
|
|
|
/**
|
|
|
|
* convert iso8859 to utf8
|
|
|
|
*
|
|
|
|
* @param iso8859 iso string
|
|
|
|
*/
|
|
|
|
iso8859ToUtf8(iso8859: string): string
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
2021-08-16 10:35:56 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface ImagePackage {
|
2021-08-16 10:35:56 +02:00
|
|
|
/**
|
|
|
|
* convert image from source file to target file with filters
|
|
|
|
*
|
|
|
|
* @param sourceFile
|
|
|
|
* @param targetFile
|
|
|
|
* @param filters
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
filter(
|
2021-08-16 10:35:56 +02:00
|
|
|
sourceFile: string,
|
|
|
|
targetFile: string,
|
|
|
|
filters: {
|
|
|
|
fit?: boolean
|
|
|
|
fill?: boolean
|
|
|
|
width?: number
|
|
|
|
height?: number
|
|
|
|
brightness?: number
|
|
|
|
saturation?: number
|
|
|
|
contrast?: number
|
|
|
|
gamma?: number
|
|
|
|
blur?: number
|
|
|
|
sharpen?: number
|
|
|
|
invert?: boolean
|
|
|
|
grayscale?: boolean
|
|
|
|
quality?: number
|
|
|
|
}[]
|
|
|
|
): void
|
2021-08-19 14:22:42 +02:00
|
|
|
}
|
2021-08-16 10:35:56 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface XmlPackage {
|
2021-08-16 17:32:34 +02:00
|
|
|
/**
|
|
|
|
* create xml string
|
|
|
|
*
|
|
|
|
* @param data object or array
|
|
|
|
* @param options options
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
create(data: any, options?: {}): string
|
2021-08-16 17:32:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* parse xml string to json
|
|
|
|
*
|
|
|
|
* @param xml xml string
|
|
|
|
* @param options options
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
parse(xml:string, options?: {}): any
|
|
|
|
}
|
2021-08-17 13:26:39 +02:00
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
interface CookiePackage {
|
2021-08-17 13:26:39 +02:00
|
|
|
/**
|
|
|
|
* get cookie from http header
|
|
|
|
*
|
|
|
|
* @param name cookie name
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
get(name: string): string
|
2021-08-17 13:26:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set cookie via http header
|
|
|
|
*
|
|
|
|
* @param name cookie name
|
|
|
|
* @param value cookie value
|
|
|
|
* @param options cookie options
|
|
|
|
*/
|
2021-08-19 14:22:42 +02:00
|
|
|
set(name: string, value: string, options?:{
|
2021-08-17 13:26:39 +02:00
|
|
|
maxAge?: number
|
|
|
|
path?:string
|
|
|
|
domain?:string
|
|
|
|
secure?:boolean
|
|
|
|
httpOnly?:boolean
|
|
|
|
}): void
|
2021-08-16 10:23:45 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:22:42 +02:00
|
|
|
export interface HookContext extends GetHookData, GetHookGetOnlyData, 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
|
|
|
|
/**
|
|
|
|
* bodyBytes return []byte go slice of post body for later use pe. in iso8859ToUtf8
|
|
|
|
*/
|
|
|
|
bodyBytes(): string
|
|
|
|
}
|
|
|
|
|
|
|
|
db: DbPackage
|
2021-08-19 16:00:35 +02:00
|
|
|
smtp: SmtpPackage
|
2021-08-19 14:22:42 +02:00
|
|
|
fs: FsPackage
|
2021-08-19 16:00:35 +02:00
|
|
|
tpl: TplPackage
|
2021-08-19 14:22:42 +02:00
|
|
|
http: HttpPackage
|
|
|
|
debug: DebugPackage
|
2021-08-19 14:30:37 +02:00
|
|
|
response: ResponsePackage
|
2021-08-19 14:22:42 +02:00
|
|
|
user: UserPackage
|
|
|
|
bcrypt: BcryptPackage
|
|
|
|
jwt: JwtPackage
|
|
|
|
charset: CharsetPackage
|
|
|
|
image: ImagePackage
|
|
|
|
xml: XmlPackage
|
|
|
|
cookie: CookiePackage
|
|
|
|
}
|
|
|
|
|
2021-08-16 10:23:45 +02:00
|
|
|
export interface HookException {
|
|
|
|
status?: number
|
|
|
|
html?: string
|
2021-08-16 10:35:56 +02:00
|
|
|
file?: string
|
2021-08-16 10:23:45 +02:00
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface HookResponse extends GetHookData, PostHookData {
|
|
|
|
data?: CollectionDocument
|
2021-08-16 10:48:03 +02:00
|
|
|
results?: any
|
2021-08-16 10:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
var context: HookContext
|
2021-08-16 10:35:56 +02:00
|
|
|
}
|