lemon-core
Version:
Lemon Serverless Micro-Service Platform
283 lines (282 loc) • 7.94 kB
TypeScript
/**
* `helpers.ts`
* - helper functions used globally in project
*
*
* @author Steve Jung <steve@lemoncloud.io>
* @date 2020-12-22 initial version
* @date 2021-12-21 protocol support `//self` url.
* @date 2021-12-23 optimize types of $protocol.
* @date 2022-03-17 addition text processing. (S2, P)
*
* @copyright (C) 2021 LemonCloud Co Ltd. - All Rights Reserved.
*/
import { NextContext, SimpleSet } from '../cores/';
/**
* Helpers to transform data-types.
*/
export declare const $T: {
/**
* transform to string w/ trim()
*/
S: (val: any, def?: string) => string;
/**
* as string w/o white-space.
*/
S2: (val: any, def?: string, delim?: string) => string;
/**
* transform to string[]
*/
SS: (val: any, def?: string[]) => string[];
/**
* text to Plain text (remove html tag)
*/
P: (text: string, max?: number) => string;
/**
* transform to number(integer).
*/
N: (val: any, def?: number) => number;
/**
* number array
*/
NN: (val: any, def?: number[]) => number[];
/**
* transform to number(float)
*/
F: (val: any, def?: number) => number;
/**
* transform to number(float)[]
*/
FF: (val: any, def?: number[]) => number[];
/**
* float w/ fixed len=3
*/
F3: (n: number, e?: number) => number;
/**
* transform to boolean.
*/
B: (val: any, def?: 0 | 1) => 0 | 1;
/**
* transform to Time number via string | number.
*/
T: (val: any, def?: number) => number;
/**
* transform to Date formatted string
*/
D: (val: any, def?: string) => string;
/**
* date-time format
*/
DT: (val: any, def?: string) => string;
/**
* Extract Text
*/
EX: (data: string, txt1: string, txt2: string) => string;
/**
* transform to simple-set.
* @param val json object.
*/
simples: (val: any, throws?: boolean) => SimpleSet;
/**
* catch string between txt1 and txt2
* @param data string
* @param txt1 head
* @param txt2 tail
*/
catch: (data: any, txt1: string, txt2: string) => any;
/**
* merge simple-set from $org to $new
* @param $org the origin set
* @param $new the update set.
*/
merge: ($org: SimpleSet, $new: SimpleSet) => SimpleSet;
/**
* replace message with template.
*/
template: (msg: string, set: {
[key: string]: string | number;
}) => string;
/**
* make random-code by length
* @param size length of code
* @param rand flag to use random (0 => 0, 1 => max)
*/
makeRandomCode: (size?: number, rand?: boolean | number) => {
val: number;
min: number;
max: number;
};
/**
* 객체 정규화 시킴.
* - null 에 대해서는 특별히 처리.
*/
normal: <T = object>(N: T) => T;
/**
* transform list to map by `id`
*/
asMap: <T_1>(list: T_1[], id?: string) => {
[key: string]: T_1;
};
/**
* compare object, and extract the only diff properties.
*/
diff: <T_2 = any>(A: T_2, B: T_2, onlyValid?: boolean) => T_2;
/**
* get $perf instance.
* ```ts
* const p = $T.perf()
* const took = p.took();
*/
perf: () => {
readonly t0: number;
took: () => number;
};
/**
* parse `.meta` property as object.
* @param meta any
*/
parseMeta: <T_3 extends {
[key: string]: any;
type?: string;
value?: any;
error?: string;
list?: any[];
}>(meta: any) => T_3;
/**
* clear the undefined properties from the cloned object.
* - applied only to 1st depth.
*
* @param N object
* @param $def default if not valid object.
* @returns cloned object
*/
onlyDefined: <T_4 extends object>(N: T_4, $def?: T_4) => T_4;
};
/**
* random number generator
*/
export declare const $rand: {
/**
* list of number[] in n-size.
*/
range: (n: number) => number[];
/**
* generate random number
*/
float: (from: number, to: number) => number;
/**
* generate multiple float numbers
*/
floats: (from: number, to: number, n: number) => number[];
/**
* generate an integer
*/
integer: (from: number, to: number) => number;
/**
* generate multiple integers
*/
integers: (from: number, to: number, n: number) => number[];
};
/**
* builder to support protocol-service.
* @param context the current context (or service name).
* @param service service name
* @param options additional options.
*/
export declare const $protocol: (context?: NextContext | string, service?: string, options?: {
param?: any;
body?: any;
isProd?: boolean;
}) => {
hello: () => string;
asTargetUrl: () => string;
execute: <T = any>(param?: any, body?: any, mode?: string) => Promise<T>;
enqueue: <T_1 = any>(param?: any, body?: any, mode?: string, callback?: string, delaySeconds?: number) => Promise<string>;
notify: (param?: any, body?: any, mode?: string, callback?: string) => Promise<string>;
};
/**
* get the current config info
*/
export declare const $info: () => {
service: string;
version: string;
stage: import("../cores/core-services").STAGE;
};
/**
* send message to slack/public
*
* @param title 헤터 타이틀
* @param text object or 텍스트 내용
* @param pretext (optional) 텍스트 미리보기용.
* @param params (optional) customize more options.
*/
export declare const $slack: (title?: string, text?: string | object, pretext?: string, params?: {
channel?: string;
color?: string;
scope?: string;
fields?: {
title: string;
value: string;
short?: boolean;
}[];
footer?: string;
context?: NextContext;
ts?: number;
}) => Promise<string>;
/**
* event producer builder
* @param context current context
* @param defEndpoint (optional) the default endpoint.
*/
export declare const $event: (context: NextContext, defEndpoint?: string) => {
publish: (body: {
[key: string]: any;
}) => Promise<string>;
};
/**
* authentication helper - get identity-id from context
* @param context the current context
*/
export declare function getIdentityId(context: NextContext): string | undefined;
/**
* authentication helper - check user is authorized
* - 이 메서드는 AWS IAM 인증 여부만을 확인한다.
* - 따라서 true를 반환한다고 하여 회원 가입이 되어있다는 의미는 아니다.
*
* @param context the current context
* @params params (optional) to override `identity` when running local.
*/
export declare function isUserAuthorized(context: NextContext, params?: any): boolean;
/**
* parse range expression
* @param exp range expression (e.g. '[63100 TO 224000]' or '[* TO 150000}')
*/
export declare function parseRange(exp: string): any;
/**
* customized of `do_parrallel` for safe error-handling.
* - use `.error` to report the internal error.
*
* @param list list of model.
* @param func callback to process of each
* @param size (optional) size of parrallel (default 10)
*/
export declare const my_parrallel: <T extends {
id?: string;
error?: string;
}, U extends {
id?: string;
error?: string;
}>(list: T[], func: (item: T, index?: number) => Promise<U>, size?: number) => Promise<U[]>;
/**
* run in sequence order
* - same as `my_parrallel(list, func, 1)`;
*
* 주의) 내부 error를 throw 하지 않으니, list 를 전부 처리할때까지 안끝남.
*
* @param list list of model.
* @param func callback to process of each
*/
export declare const my_sequence: <T extends {
id?: string;
error?: string;
}, U = T>(list: T[], func: (item: T, index?: number) => Promise<U>) => Promise<U[]>;