@datastax/astra-db-ts
Version:
Data API TypeScript client
110 lines (100 loc) • 3.44 kB
JavaScript
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
import { BigNumber } from 'bignumber.js';
import { isBigNumber } from '../../../lib/utils.js';
export const assertHasSerializeFor = (clazz, sym, synName) => {
if (!(sym in clazz.prototype)) {
throw new Error(`Invalid codec class: '${clazz.name}' - missing ${clazz.name}.prototype.[${synName}]
Did you define [${synName}] as a class property instead of a prototype method?
Don't do this:
> class Bad { [${synName}] = () => ...; }
Or this:
> Bad[${synName}] = () => ...;
Do this:
> class Good { [${synName}]() { ... } }
Or this:
> Good.prototype[${synName}] = () => ...;`);
}
};
export const assertHasDeserializeFor = (clazz, sym, synName) => {
if (!(sym in clazz)) {
throw new Error(`Invalid codec class: '${clazz.name}' - missing ${clazz.name}.[${synName}]
Did you forget to define [${synName}] on the class?
Don't do this:
> class Bad { [${synName}] = () => ...; }
Do this:
> class Good { [${synName}]() { ... } }
Or this:
> Good[${synName}] = () => ...;`);
}
};
export const mkTypeUnsupportedForCollectionsError = (type, fauxTypeName, alternatives) => {
return new Error([
`${type} may not be used with collections by default.`,
'',
'Please use one of the following alternatives:',
...[...alternatives, 'Write a custom codec for ${type} (beta)'].map((alt, i) => `${i + 1}. ${alt}`),
'',
'See the `CollectionCodecs` class for more information about writing your own collection codec.',
'',
`Keep in mind that you may need to use CollectionCodecs.forType(...) to create a faux custom type (e.g. { ${fauxTypeName}: <${type}> }) representing a ${type} so that the value may be identifiable as needing to be deserialized back into a ${type} as well`,
].join('\n'));
};
export const mkTypeUnsupportedForTablesError = (type, alternatives) => {
return new Error([
`${type} may not be used with tables by default.`,
'',
'Please use one of the following alternatives:',
...[...alternatives, 'Write a custom codec for ${type} (beta)'].map((alt, i) => `${i + 1}. ${alt}`),
'',
'See the `TablesCodec` class for more information about writing your own table codec.',
].join('\n'));
};
export function withJbiNullProtoFix(jbi) {
return {
parse: (str) => nullProtoFix(jbi.parse(str)),
stringify: jbi.stringify,
};
}
function nullProtoFix(doc) {
if (!doc || typeof doc !== 'object') {
return doc;
}
if (isBigNumber(doc)) {
return BigNumber(doc);
}
if (Array.isArray(doc)) {
for (let i = 0; i < doc.length; i++) {
doc[i] = nullProtoFix(doc[i]);
}
}
else {
Object.setPrototypeOf(doc, Object.prototype);
for (const key of Object.keys(doc)) {
doc[key] = nullProtoFix(doc[key]);
}
}
return doc;
}
export function pathArraysEqual(a, b) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
export function pathMatches(exp, acc) {
if (exp.length !== acc.length) {
return false;
}
for (let i = 0; i < acc.length; i++) {
if (exp[i] !== '*' && exp[i] !== acc[i]) {
return false;
}
}
return true;
}