tibi-types/index.d.ts

742 lines
19 KiB
TypeScript
Raw Permalink Normal View History

2022-11-28 06:25:11 +01:00
declare global {
export interface CollectionDocument {
id?: string
insertTime?: Date
updateTime?: Date
2021-08-16 10:23:45 +02:00
[key: string]: any
}
2022-11-28 06:25:11 +01:00
export interface DbReadOptions {
filter?: {
[key: string]: any
}
selector?: {
[key: string]: any
}
projection?: string
offset?: number
limit?: number
sort?: string[]
pipelineMod?: (
pipe: { [key: string]: any }[]
) => { [key: string]: any }[]
2021-08-16 10:23:45 +02:00
}
2022-11-28 06:25:11 +01:00
interface GetHookGetOnlyData {
/**
* 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
2022-11-28 06:25:11 +01:00
interface GetHookData {
/**
* 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
/**
* pipelineMod is a function to modify the mongodb query pipeline
*/
pipelineMod?: (
pipe: { [key: string]: any }[]
) => { [key: string]: any }[]
2021-08-16 10:23:45 +02:00
}
2022-11-28 06:25:11 +01:00
interface PostHookData {
/**
* post data only valid in "post" and "put" hooks
*/
data?: CollectionDocument
2021-08-16 10:23:45 +02:00
}
2022-11-28 06:25:11 +01:00
interface JobConfig {
/**
* meta object
*/
meta?: any
2021-08-16 10:23:45 +02:00
2022-11-28 06:25:11 +01:00
/**
* cron job interval specification
*/
cron?: string
2022-02-21 10:23:11 +01:00
2022-11-28 06:25:11 +01:00
/**
* job program type
*/
type: "javascript"
2022-02-21 10:23:11 +01:00
2022-11-28 06:25:11 +01:00
/**
* jobs program file
*/
file?: string
}
2021-08-19 14:22:42 +02:00
2022-11-28 06:25:11 +01:00
interface JobData {
/**
* job is object of job config
*/
job?: JobConfig
}
2021-08-19 14:22:42 +02:00
interface ConfigPackage {
/**
* get current namespace string
*
*/
namespace(): string
/**
* get current project object
2024-08-23 16:01:15 +02:00
*
*/
project(): {
[key: string]: any
}
/**
* get server config object
2024-08-23 16:01:15 +02:00
*
*/
server(): {
api: {
port: number
}
security: {
allowAbsolutePaths: boolean
allowUpperPaths: boolean
}
}
}
2022-11-28 06:25:11 +01:00
interface DbPackage {
/**
* read results from a collection
*
* @param colName collection name
* @param options options map
*/
find(colName: string, options?: DbReadOptions): CollectionDocument[]
/**
* read count of documents for filter from a collection
*
* @param colName collection name
* @param options options map (only filter is valid)
*/
count(colName: string, options?: DbReadOptions): number
/**
* create a document in a collection
*
* @param colName collection name
* @param data data map
*/
create(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
*/
update(
colName: string,
id: string,
data: CollectionDocument
): CollectionDocument
/**
* deletes one document by id from collection
*
* @param colName collection name
* @param id id of entry
*/
delete(colName: string, id: string): { message: "ok" }
/**
* deletes documents by filter from collection
*
* @param colName collection name
* @param options options map, only filter valid
*/
deleteMany(
colName: string,
options?: DbReadOptions
): { message: "ok"; removed: number }
2021-08-19 14:22:42 +02:00
}
2022-11-28 06:25:11 +01:00
interface SmtpPackage {
/**
* send an email
*
* @param options email options map
*/
sendMail(options: {
2023-02-24 10:20:04 +01:00
host?: string
username?: string
password?: string
2022-11-28 06:25:11 +01: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
2022-11-28 06:25:11 +01:00
interface FsPackage {
/**
* get directory path of api config file
*
*/
configDir(): string
/**
* read a file relative to config dir and return its content
*
* @param path relative file path
* @param options optional options
*/
readFile(
path: string,
options?: {
bytes: boolean // if true return []byte instead of string
}
): string | any
/**
* write data to a file relative to config dir
*
* @param path relative file path
* @param data string or []byte data
*/
writeFile(path: string, data: string | any): null
2021-08-16 10:23:45 +02:00
2022-11-28 06:25:11 +01:00
/**
* stat file or directory
*
* @param path
*/
stat(path: string): {
name: string
size: number
isDir: boolean
modTime: string
2021-08-16 10:23:45 +02:00
}
2022-11-28 06:25:11 +01:00
/**
* 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
/**
* copy file or directory
*
* @param src
* @param dest
*/
copy(src: string, dest: string): void
2022-11-28 06:25:11 +01:00
}
interface TplPackage {
/**
* execute a template code and return result
*
* @param code template code
* @param contextData template context map
*/
execute(
code: string,
contextData?: {
[key: string]: any
}
): string
}
interface HttpPackage {
/**
* http request
*
* @param url url for request
* @param options request options
*/
fetch(
url: string,
options?: {
method?: string
headers?: { [key: string]: string }
body?: string
// timeout in seconds
timeout?: number
2022-11-28 06:25:11 +01:00
}
): {
status: number
statusText: string
headers: { [key: string]: string }
trailer: { [key: string]: string }
url: string
body: {
text(): string
json(): any
}
2021-08-16 10:23:45 +02:00
}
}
2022-11-28 06:25:11 +01:00
interface DebugPackage {
/**
* dumps data to header and server log
*
* @param toDump data to dump
*/
dump(...toDump: any): void
2021-08-19 14:22:42 +02:00
2022-11-28 06:25:11 +01:00
/**
* get Sentry trace id
*/
sentryTraceId(): string
2024-08-23 16:01:15 +02:00
/**
* sleep
*
* @param ms sleep time in milliseconds
*/
sleep(ms: number): void
2022-11-28 06:25:11 +01:00
}
2021-08-16 10:23:45 +02:00
2022-11-28 06:25:11 +01:00
interface ResponsePackage {
/**
* set response header
*
* @param name header name
* @param value value
*/
header(name: string, value: any): void
2021-08-16 10:23:45 +02:00
}
2022-11-28 06:25:11 +01:00
interface UserPackage {
/**
* get JWT authentication
*/
auth(): {
id: string
username: string
role: number
permissions: string[]
2021-08-16 10:23:45 +02:00
}
2022-11-28 06:25:11 +01:00
}
2021-08-16 10:23:45 +02:00
2022-11-28 06:25:11 +01:00
interface BcryptPackage {
/**
* hash password via bcrypt algo
*
* @param password clear text password
* @param options hashing options
*/
hash(
password: string,
options?: {
cost?: number
}
): string
/**
* check password against hashed password via bcrypt algo
*
* @param password clear text password
* @param hash hashed password
*/
check(password: string, hash: string): boolean
}
interface JwtPackage {
/**
* create a jwt signed token string
*
* @param claims data object
* @param options options (secret, validityDuration = expiry in X seconds)
*/
create(
claims: {
[key: string]: any
},
options?: {
secret?: string
validityDuration?: number
}
): string
/**
* parse jwt token string
*
* @param token token string
* @param options options (secret)
*/
parse(
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-16 10:23:45 +02:00
}
}
2022-11-28 06:25:11 +01:00
interface CharsetPackage {
/**
* convert iso8859 to utf8
*
* @param iso8859 iso string
*/
iso8859ToUtf8(iso8859: string): string
}
2021-08-16 10:35:56 +02:00
2022-11-28 06:25:11 +01:00
interface ImagePackage {
/**
* convert image from source file to target file with filters
*
* @param sourceFile
* @param targetFile
* @param filters
*/
filter(
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-16 10:35:56 +02:00
2022-11-28 06:25:11 +01:00
interface XmlPackage {
/**
* create xml string
*
* @param data object or array
* @param options options
*/
create(data: any, options?: {}): string
2021-08-17 13:26:39 +02:00
2022-11-28 06:25:11 +01:00
/**
* parse xml string to json
*
* @param xml xml string
* @param options options
*/
parse(xml: string, options?: {}): any
}
2022-01-13 18:38:02 +01:00
2022-11-28 06:25:11 +01:00
interface CookiePackage {
/**
* get cookie from http header
*
* @param name cookie name
*/
get(name: string): string
/**
* set cookie via http header
*
* @param name cookie name
* @param value cookie value
* @param options cookie options
*/
set(
name: string,
value: string,
options?: {
maxAge?: number
path?: string
domain?: string
secure?: boolean
httpOnly?: boolean
sameSite?: "strict" | "lax" | "none"
2022-11-28 06:25:11 +01:00
}
): void
}
interface PdfPackage {
/**
* generate pdf from html
*
* @param html html string
* @param options options
*
* @returns []byte of pdf data
*/
fromHTML(
html: string,
options?: {
copies?: number // Number of copies to print into the pdf file (default 1)
dpi?: number // Change the dpi explicitly (this has no effect on X11 based systems)
grayscale?: boolean // PDF will be generated in grayscale
imageDpi?: number // When embedding images scale them down to this dpi (default 600)
imageQuality?: number // When jpeg compressing images use this quality (default 94)
lowQuality?: boolean // Generates lower quality pdf/ps. Useful to shrink the result document space
marginBottom?: number // Set the page bottom margin
marginLeft?: number // Set the page left margin (default 10mm)
marginRight?: number // Set the page right margin (default 10mm)
marginTop?: number // Set the page top margin
noCollate?: boolean // Do not collate when printing multiple copies (default collate)
noPdfCompression?: boolean // Do not use lossless compression on pdf objects
orientation?: "Portrait" | "Landscape" // Set orientation to Landscape or Portrait (default Portrait)
pageHeight?: number // Page height
pageSize?: string // Set paper size to: A4, Letter, etc. (default A4)
pageWidth?: number // Page width
title?: string // The title of the generated pdf file (The title of the first document is used if not specified)
// page settings
printMediaType?: boolean // Use print media-type instead of screen
footerCenter?: string // Centered footer text
footerFontName?: string // Set footer font name (default Arial)
footerFontSize?: number // Set footer font size (default 12)
footerLeft?: string // Left aligned footer text
footerLine?: boolean // Display line above the footer
footerRight?: string // Right aligned footer text
footerSpacing?: number // Spacing between footer and content in mm (default 0)
footerHTML?: string // URL to footer html
headerCenter?: string // Centered header text
headerFontName?: string // Set header font name (default Arial)
headerFontSize?: number // Set header font size (default 12)
headerLeft?: string // Left aligned header text
headerLine?: boolean // Display line below the header
headerRight?: string // Right aligned header text
headerSpacing?: number // Spacing between header and content in mm (default 0)
headerHTML?: string // URL to header html
}
): any
/**
* process existing pdf data
*
* @param command pdfcpu command
* @param pdfData []byte of pdf data, multiple []byte as array of pdf's to merge or object with description to create
* @param options options
*
* @returns []byte of new pdf data
*/
cpu(
command: "watermark" | "stamp" | "merge" | "rotate" | "create",
pdfData: any | any[],
options?: {
pages?: (string | number)[]
description?: {
fontname?: string
points?: number
rtl?: boolean
position?:
| "full"
| "tl"
| "tc"
| "tr"
| "l"
| "c"
| "r"
| "bl"
| "bc"
| "br"
offset?: string
scalefactor?: number | string
aligntext?: "left" | "center" | "right" | "justified"
strokecolor?: string
fillcolor?: string
backgroundcolor?: string
rotation?: number
diagonal?: 1 | 2
opacity?: number
rendermode?: 0 | 1 | 2
margins?: number | string
border?: number | string
url?: string
}
mode?: "text" | "image" | "pdf"
bytes?: any // []byte of watermark image
file?: string // file for pdf watermark
text?: string // text for text watermark
2022-02-08 16:06:09 +01:00
rotation?: number
2022-02-04 10:38:35 +01:00
}
2022-11-28 06:25:11 +01:00
): any
}
2022-12-07 15:53:01 +01:00
interface CryptoPackage {
/**
* get hex of sha256 hash
*
* @param sata string
*
* @returns hex hash
*/
sha256(data: string): string
2024-06-26 16:58:39 +02:00
/**
* get hex of md5 hash
*
* @param sata string
*
* @returns hex hash
*/
2024-08-23 16:01:15 +02:00
md5(data: string): string
2022-12-07 15:53:01 +01:00
}
interface JsonPackage {
/**
* parse json string to json
* @param str json string
*/
parse(str: string): any
/**
* stringify json string
* @param json json object
*/
stringify(json: any): string
}
2022-11-28 06:25:11 +01:00
export interface HookContext
extends GetHookData,
GetHookGetOnlyData,
PostHookData,
JobData {
request(): {
method: string
remoteAddr: string
2022-12-07 15:53:01 +01:00
clientIp(): string
2022-11-28 06:25:11 +01:00
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
2022-02-03 17:20:13 +01:00
}
2021-08-16 10:23:45 +02:00
api(): {
2024-08-23 16:01:15 +02:00
[key: string]: any
}
project(): {
[key: string]: any
}
collection(): {
[key: string]: any
}
config: ConfigPackage
2022-11-28 06:25:11 +01:00
db: DbPackage
smtp: SmtpPackage
fs: FsPackage
tpl: TplPackage
http: HttpPackage
debug: DebugPackage
response: ResponsePackage
user: UserPackage
bcrypt: BcryptPackage
jwt: JwtPackage
charset: CharsetPackage
image: ImagePackage
xml: XmlPackage
cookie: CookiePackage
pdf: PdfPackage
2022-12-07 15:53:01 +01:00
crypto: CryptoPackage
json: JsonPackage
2021-08-19 14:22:42 +02:00
}
2022-11-28 06:25:11 +01:00
export interface HookException {
status?: number
html?: string
string?: string
bytes?: any
json?: any
file?: string
log?: boolean
[key: string]: any
}
2021-08-16 10:23:45 +02:00
2022-11-28 06:25:11 +01:00
export interface HookResponse extends GetHookData, PostHookData {
data?: CollectionDocument
results?: any
}
2021-08-16 10:23:45 +02:00
var context: HookContext
2021-08-16 10:35:56 +02:00
}
2022-11-28 06:25:11 +01:00
export { HookResponse, HookException, HookContext, CollectionDocument }