mirror of
https://github.com/requarks/wiki.git
synced 2026-08-26 13:17:10 -05:00
184 lines
4.5 KiB
TypeScript
184 lines
4.5 KiB
TypeScript
import { isNil, isPlainObject } from 'es-toolkit/predicate'
|
|
import { startCase } from 'es-toolkit/string'
|
|
import crypto from 'node:crypto'
|
|
import mime from 'mime'
|
|
import fs from 'node:fs'
|
|
import type { FastifyReply } from 'fastify'
|
|
|
|
export interface Deferred<T = void> {
|
|
resolve: (value: T) => void
|
|
reject: (reason?: unknown) => void
|
|
promise: Promise<T>
|
|
}
|
|
|
|
/* eslint-disable promise/param-names */
|
|
export function createDeferred<T = void>(): Deferred<T> {
|
|
let result: Promise<T> | undefined
|
|
let resolve: ((value: T | PromiseLike<T>) => void) | undefined
|
|
let reject: ((reason?: unknown) => void) | undefined
|
|
return {
|
|
resolve: function (value: T) {
|
|
if (resolve) {
|
|
resolve(value)
|
|
} else {
|
|
result =
|
|
result ||
|
|
new Promise<T>(function (r) {
|
|
r(value)
|
|
})
|
|
}
|
|
},
|
|
reject: function (reason?: unknown) {
|
|
if (reject) {
|
|
reject(reason)
|
|
} else {
|
|
result =
|
|
result ||
|
|
new Promise<T>(function (x, j) {
|
|
j(reason)
|
|
})
|
|
}
|
|
},
|
|
promise: new Promise<T>(function (r, j) {
|
|
if (result) {
|
|
r(result)
|
|
} else {
|
|
resolve = r
|
|
reject = j
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Decode a tree path
|
|
*
|
|
* @param str String to decode
|
|
* @returns Decoded tree path
|
|
*/
|
|
export function decodeTreePath(str?: string | null): string | undefined {
|
|
return str?.replaceAll('.', '/')
|
|
}
|
|
|
|
/**
|
|
* Encode a tree path
|
|
*
|
|
* @param str String to encode
|
|
* @returns Encoded tree path
|
|
*/
|
|
export function encodeTreePath(str?: string | null): string {
|
|
return str?.toLowerCase()?.replaceAll('/', '.') || ''
|
|
}
|
|
|
|
/**
|
|
* Generate SHA-1 Hash of a string
|
|
*
|
|
* @param str String to hash
|
|
* @returns Hashed string
|
|
*/
|
|
export function generateHash(str: string): string {
|
|
return crypto.createHash('sha1').update(str).digest('hex')
|
|
}
|
|
|
|
/**
|
|
* Get default value of type
|
|
*
|
|
* @param type primitive type name
|
|
* @returns Default value
|
|
*/
|
|
export function getTypeDefaultValue(type: string): string | number | boolean | undefined {
|
|
switch (type.toLowerCase()) {
|
|
case 'string':
|
|
return ''
|
|
case 'number':
|
|
return 0
|
|
case 'boolean':
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A single prop, as declared in a module `definition.yml`. Either the bare primitive type name
|
|
* (e.g. `String`) or an object describing the prop in full.
|
|
*/
|
|
export type ModulePropDeclaration = ModulePropDefinition | string
|
|
|
|
export interface ModulePropDefinition {
|
|
type: string
|
|
default?: unknown
|
|
title?: string
|
|
hint?: string
|
|
enum?: string[] | false
|
|
enumDisplay?: string
|
|
multiline?: boolean
|
|
sensitive?: boolean
|
|
icon?: string
|
|
order?: number
|
|
if?: unknown[]
|
|
}
|
|
|
|
/** A prop after normalization, with every field resolved to a concrete value. */
|
|
export interface ModuleProp {
|
|
default: unknown
|
|
type: string
|
|
title: string
|
|
hint: string
|
|
enum: string[] | false
|
|
enumDisplay: string
|
|
multiline: boolean
|
|
sensitive: boolean
|
|
icon: string
|
|
order: number
|
|
if: unknown[]
|
|
}
|
|
|
|
export function parseModuleProps(
|
|
props: Record<string, ModulePropDeclaration>
|
|
): Record<string, ModuleProp> {
|
|
const result: Record<string, ModuleProp> = {}
|
|
for (const [key, value] of Object.entries(props)) {
|
|
const def: Partial<ModulePropDefinition> = isPlainObject(value) ? value : {}
|
|
const type = def.type || (value as string)
|
|
const defaultValue = !isNil(def.default) ? def.default : getTypeDefaultValue(type)
|
|
result[key] = {
|
|
default: defaultValue,
|
|
type: type.toLowerCase(),
|
|
title: def.title || startCase(key),
|
|
hint: def.hint || '',
|
|
enum: def.enum || false,
|
|
enumDisplay: def.enumDisplay || 'select',
|
|
multiline: def.multiline || false,
|
|
sensitive: def.sensitive || false,
|
|
icon: def.icon || 'rename',
|
|
order: def.order || 100,
|
|
if: def.if ?? []
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function getDictNameFromLocale(locale: string): string {
|
|
const loc = locale.length > 2 ? locale.substring(0, 2) : locale
|
|
if (loc in WIKI.config.search.dictOverrides) {
|
|
return WIKI.config.search.dictOverrides[loc]
|
|
} else {
|
|
return WIKI.data.tsDictMappings[loc] ?? 'simple'
|
|
}
|
|
}
|
|
|
|
export function replyWithFile(reply: FastifyReply, filePath: string): FastifyReply {
|
|
const stream = fs.createReadStream(filePath)
|
|
reply.header('Content-Type', mime.getType(filePath))
|
|
return reply.send(stream)
|
|
}
|
|
|
|
export class CustomError extends Error {
|
|
statusCode: number
|
|
|
|
constructor(name: string, message: string, statusCode = 400) {
|
|
super(message)
|
|
this.name = name
|
|
this.statusCode = statusCode
|
|
}
|
|
}
|