Wroclaw
eebf25198d
also I updated packages, and properly typed api input a lot of time was spent, I don't remeber what really I did x3 but everything was related to replacing mysql2 with prisma
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { Decimal } from "@prisma/client/runtime/library";
|
|
|
|
type func = (...args: any[]) => any | Function;
|
|
|
|
export type replaceJsonUnparsableToString<T> =
|
|
T extends Array<infer E> ? Array<replaceJsonUnparsableToString<E>>
|
|
: {
|
|
[K in keyof T]:
|
|
T[K] extends null ? null
|
|
: T[K] extends func ? never
|
|
: T[K] extends Decimal ? `${number}`
|
|
: T[K] extends Array<infer E> ? Array<replaceJsonUnparsableToString<E>>
|
|
: T[K] extends object ? replaceJsonUnparsableToString<T[K]>
|
|
: T[K] extends bigint ? `${bigint}`
|
|
: T[K]
|
|
};
|
|
|
|
type exactToInterface = (...args: any[]) => any extends Function ? true : false;
|
|
|
|
function arrayPrismaToWeb<T>(array: Array<T>) {
|
|
return array.reduce(
|
|
(pV, cV) => {
|
|
pV.push(prismaToWeb(cV));
|
|
return pV;
|
|
},
|
|
[] as Array<replaceJsonUnparsableToString<T>>,
|
|
);
|
|
}
|
|
|
|
export function prismaToWeb<T>(ivalue: T): replaceJsonUnparsableToString<T> {
|
|
const rvalue: any = ivalue instanceof Array ? [] : {};
|
|
|
|
for (const i in ivalue) {
|
|
const current = ivalue[i];
|
|
if (current === null) rvalue[i] = null;
|
|
else if (typeof current === 'function') continue;
|
|
else if (current instanceof Decimal) rvalue[i] = current.toString();
|
|
else if (current instanceof Array) rvalue[i] = arrayPrismaToWeb(current);
|
|
else if (typeof current === 'object') rvalue[i] = prismaToWeb(current);
|
|
else if (typeof current === 'bigint') rvalue[i] = current.toString();
|
|
else rvalue[i] = current;
|
|
}
|
|
return rvalue;
|
|
}
|