mirror of
https://gh-proxy.org/https://github.com/klinsmaya/gateway.git
synced 2026-11-10 18:17:19 +08:00
* feat(hooks): wip - hooks with span management * feat(hooks): wip - hooks with span management * feat(hooks): wip - use hooks spans for before and after request hooks execution
43 lines
944 B
TypeScript
43 lines
944 B
TypeScript
import {
|
|
HookEventType,
|
|
PluginContext,
|
|
PluginHandler,
|
|
PluginParameters,
|
|
} from '../types';
|
|
import { getText } from '../utils';
|
|
|
|
function countCharacters(text: string): number {
|
|
return text.length;
|
|
}
|
|
|
|
export const handler: PluginHandler = async (
|
|
context: PluginContext,
|
|
parameters: PluginParameters,
|
|
eventType: HookEventType
|
|
) => {
|
|
let error = null;
|
|
let verdict = false;
|
|
let data = null;
|
|
|
|
try {
|
|
const minCount = parameters.minCharacters;
|
|
const maxCount = parameters.maxCharacters;
|
|
let text = getText(context, eventType);
|
|
|
|
if (
|
|
Number.isInteger(minCount) &&
|
|
Number.isInteger(maxCount) &&
|
|
text.length >= 0
|
|
) {
|
|
let count = countCharacters(text);
|
|
verdict = count >= minCount && count <= maxCount;
|
|
} else {
|
|
error = error || new Error('Missing character count range or text');
|
|
}
|
|
} catch (e) {
|
|
error = e as Error;
|
|
}
|
|
|
|
return { error, verdict, data };
|
|
};
|