Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Class,
name = Class.name,
options = {auto: false},
) => {
if (is.obj(name)) {
options = merge(options, name)
name = Class.name
}
const {preProcessSnapshot, postProcessSnapshot} = Class
// TS class property initializers and defaults from constructor
const values = new Class()
const {onSnapshot, onPatch, onAction} = values
let props = extractTaggedProps(Class, PropsKey)
props = pipe(
rdMap(args => args[0]),
convertValuesToMst,
)(props)
const propKeys = Object.keys(props)
const viewKeys = Object.keys(extractTaggedProps(Class, ViewsKey))
const ownKeys = Object.getOwnPropertyNames(values)
const omitKeys = ExcludeKeys.concat(propKeys).concat(viewKeys)
const descs = getOwnPropertyDescriptors(Class.prototype)
const views = pick(viewKeys, descs)
const volatile = omit(omitKeys, pick(ownKeys, values))
const actions = {}
pipe(
filter((desc, key) => !omitKeys.includes(key)),
export function getTimeByField(field = ''): (input: any) => number {
return R.ifElse(
R.has(field),
R.pipe(
R.path(field as any),
(input: any) => new Date(input).getTime()
),
() => Date.now()
);
}
export function shardsPath(index: string): (stats: any) => i.Shard[] {
return R.pathOr([], [index, 'shards']);
}
export const verifyIndexShards: (shards: i.Shard[]) => boolean = R.pipe(
// @ts-ignore
R.filter((shard: i.Shard) => shard.primary),
R.all((shard: i.Shard) => shard.stage === 'DONE')
);
export function timeseriesIndex(index: string, timeSeriesFormat: i.TimeSeriesFormat = 'monthly'): string {
const formatter = {
daily: 10,
monthly: 7,
yearly: 4,
};
const format = formatter[timeSeriesFormat];
if (!format) throw new Error(`Unsupported format "${timeSeriesFormat}"`);
const dateStr = new Date().toISOString();
R.has('message'),
R.path('message'),
R.path('msg'),
),
R.defaultTo('Unknown Error'),
);
export const getErrorMessages: (errors: i.ErrorLike[]) => string = R.pipe(
// @ts-ignore
R.map(getErrorMessage),
R.join(', '),
);
export const getErrorType = R.pathOr('', ['error', 'type']);
export const getStatusCode: (error: i.ErrorLike) => number = R.pipe(
R.ifElse(
R.has('statusCode'),
R.path('statusCode'),
R.path('status')
),
R.defaultTo(500)
);
type Shard = { primary: boolean, stage: string };
export function shardsPath(index: string): (stats: any) => Shard[] {
return R.pathOr([], [index, 'shards']);
}
export const verifyIndexShards: (shards: Shard[]) => boolean = R.pipe(
// @ts-ignore
export const getStatusCode: (error: i.ErrorLike) => number = R.pipe(
R.ifElse(
R.has('statusCode'),
R.path('statusCode'),
R.path('status')
),
R.defaultTo(500)
);
type Shard = { primary: boolean, stage: string };
export function shardsPath(index: string): (stats: any) => Shard[] {
return R.pathOr([], [index, 'shards']);
}
export const verifyIndexShards: (shards: Shard[]) => boolean = R.pipe(
// @ts-ignore
R.filter((shard: Shard) => shard.primary),
R.all((shard: Shard) => shard.stage === 'DONE')
);
export const getRolloverFrequency = R.pathOr('monthly', ['indexSchema', 'rollover_frequency']);
type indexFn = (config?: i.IndexSchema) => boolean;
export const isSimpleIndex: indexFn = R.both(
isNotNil,
R.both(
R.has('mapping'),
R.pipe(R.path('template'), R.isNil)
)
);
public parseAST(srcNode: AST, parsedFn: BooleanCB, field: string|null): AST {
const type = '__parsed';
let callback;
if (!field) {
callback = parsedFn;
} else {
const getField = getFieldValue(field);
callback = pipe(getField, parsedFn);
}
const resultingAST = {
type,
callback,
...srcNode.negated && { negated: true },
...srcNode.or && { or: true },
};
return resultingAST as AST;
}
export function getErrorMessage(err: ErrorLike): string {
const defaultErrorMsg = 'Unknown Error';
if (err && ts.isString(err)) {
return err;
}
const message: string = R.path(['message'], err) || R.pathOr(defaultErrorMsg, ['msg'], err);
const prefix = R.path(['dataPath'], err);
return `${prefix ? `${prefix} ` : ''}${message}`;
}
export const getErrorType = R.pathOr('', ['error', 'type']);
export const getStatusCode: (error: ErrorLike) => number = R.pipe(
R.ifElse(R.has('statusCode'), R.path(['statusCode']), R.path(['status'])),
R.defaultTo(500)
);
export type ErrorLike =
| {
message?: string;
msg?: string;
statusCode?: number;
status?: number;
}
| ajv.ErrorObject
| string;
import * as R from 'rambda';
import * as ts from '@terascope/utils';
import ajv from 'ajv';
export const getErrorMessages: (errors: ErrorLike[]) => string = R.pipe(
R.map(getErrorMessage),
R.join(', ')
);
export function throwValidationError(errors: ErrorLike[] | null | undefined): string | null {
if (errors == null) return null;
if (!errors.length) return null;
const errorMsg = getErrorMessages(errors);
const error = new ts.TSError(errorMsg, {
statusCode: 400,
});
Error.captureStackTrace(error, throwValidationError);
throw error;
export function getTimeByField(field: string = ''): (input: any) => number {
return R.ifElse(
R.has(field),
R.pipe(R.path(field), (input: any) => new Date(input).getTime()),
() => Date.now()
);
}
function modelProps(type) {
return pipe(
convertValuesToMst,
type.props,
)
}