hook packages

This commit is contained in:
Sebastian Frank 2021-08-19 14:22:42 +02:00
parent f8e16879dd
commit c600d6baf7
Signed by: apairon
GPG Key ID: A0E05A8199CE3F57

206
index.d.ts vendored
View File

@ -5,7 +5,7 @@ export interface CollectionDocument {
[key: string]: any
}
export interface ReadCollectionOptions {
export interface DbReadOptions {
filter?: {
[key: string]: any
}
@ -63,32 +63,16 @@ interface PostHookData {
data?: CollectionDocument
}
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
}
interface DbPackage {
/**
* read results from a collection
*
* @param colName collection name
* @param options options map
*/
readCollection(
find(
colName: string,
options?: ReadCollectionOptions
options?: DbReadOptions
): CollectionDocument[]
/**
@ -97,9 +81,9 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param colName collection name
* @param options options map (only filter is valid)
*/
readCollectionCount(
count(
colName: string,
options?: ReadCollectionOptions
options?: DbReadOptions
): number
/**
@ -108,7 +92,7 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param colName collection name
* @param data data map
*/
createDocument(
create(
colName: string,
data: CollectionDocument
): CollectionDocument
@ -120,7 +104,7 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param id id of entry
* @param data new/changed data
*/
updateDocument(
update(
colName: string,
id: string,
data: CollectionDocument
@ -132,7 +116,7 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param colName collection name
* @param id id of entry
*/
deleteDocument(colName: string, id: string): { message: "ok" }
delete(colName: string, id: string): { message: "ok" }
/**
* deletes documents by filter from collection
@ -140,11 +124,14 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param colName collection name
* @param options options map, only filter valid
*/
deleteDocuments(
deleteMany(
colName: string,
options?: ReadCollectionOptions
options?: DbReadOptions
): { message: "ok"; removed: number }
}
interface MailPackage {
/**
* send an email
*
@ -162,7 +149,57 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
html?: string
attach?: string | string[]
}): void
}
interface FsPackage {
/**
* read a file relative to config dir and return its content
*
* @param path relative file path
*/
readFile(path: string): string
/**
* 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
}
interface TemplatePackage {
/**
* execute a template code and return result
*
@ -175,14 +212,9 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
[key: string]: any
}
): string
}
/**
* read a file relative to config dir and return its content
*
* @param path relative file path
*/
file(path: string): string
interface HttpPackage {
/**
* http request
*
@ -207,7 +239,9 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
json(): any
}
}
}
interface DebugPackage {
/**
* dumps data to header and server log
*
@ -215,6 +249,13 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
*/
dump(...toDump: any): void
/**
* get Sentry trace id
*/
sentryTraceId(): string
}
interface ResponsePackage {
/**
* set response header
*
@ -222,7 +263,9 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param value value
*/
header(name: string, value: any): void
}
interface UserPackage {
/**
* get JWT authentication
*/
@ -232,19 +275,16 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
role: number
permissions: string[]
}
}
/**
* get Sentry trace id
*/
sentryTraceId(): string
interface BcryptPackage {
/**
* hash password via bcrypt algo
*
* @param password clear text password
* @param options hashing options
*/
bcryptHash(
hash(
password: string,
options?: {
cost?: number
@ -257,8 +297,10 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param password clear text password
* @param hash hashed password
*/
bcryptCheck(password: string, hash: string): boolean
check(password: string, hash: string): boolean
}
interface JwtPackage {
/**
* create a jwt signed token string
*
@ -302,14 +344,18 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
[key: string]: any
}
}
}
interface CharsetPackage {
/**
* convert iso8859 to utf8
*
* @param iso8859 iso string
*/
iso8859ToUtf8(iso8859: string): string
}
interface ImagePackage {
/**
* convert image from source file to target file with filters
*
@ -317,7 +363,7 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param targetFile
* @param filters
*/
imageFilter(
filter(
sourceFile: string,
targetFile: string,
filters: {
@ -336,45 +382,16 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
quality?: number
}[]
): void
}
/**
* stat file or directory
*
* @param path
*/
fsStat(path: string): {
name: string
size: number
isDir: boolean
modTime: string
}
/**
* list directory entries
*
* @param path
*/
fsReadDir(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
*/
fsMkDir(path: string): void
interface XmlPackage {
/**
* create xml string
*
* @param data object or array
* @param options options
*/
xmlCreate(data: any, options?: {}): string
create(data: any, options?: {}): string
/**
* parse xml string to json
@ -382,14 +399,16 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param xml xml string
* @param options options
*/
xmlParse(xml:string, options?: {}): any
parse(xml:string, options?: {}): any
}
interface CookiePackage {
/**
* get cookie from http header
*
* @param name cookie name
*/
cookieGet(name: string): string
get(name: string): string
/**
* set cookie via http header
@ -398,7 +417,7 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
* @param value cookie value
* @param options cookie options
*/
cookieSet(name: string, value: string, options?:{
set(name: string, value: string, options?:{
maxAge?: number
path?:string
domain?:string
@ -407,6 +426,39 @@ export interface HookContext extends GetHookData, GetHookGetOnlyData, PostHookDa
}): void
}
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
mail: MailPackage
fs: FsPackage
template: TemplatePackage
http: HttpPackage
debug: DebugPackage
reponse: ResponsePackage
user: UserPackage
bcrypt: BcryptPackage
jwt: JwtPackage
charset: CharsetPackage
image: ImagePackage
xml: XmlPackage
cookie: CookiePackage
}
export interface HookException {
status?: number
html?: string