initial commit
This commit is contained in:
11
packages/db/utils/index.d.ts
vendored
Normal file
11
packages/db/utils/index.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Extract enum values from a Zod enum or native enum schema.
|
||||
* Supports z.enum([...]) and z.nativeEnum(SomeTsEnum).
|
||||
*/
|
||||
export declare function extractEnumValues<T extends string | number>(schema: any): T[];
|
||||
/**
|
||||
* Build a runtime map: { VAL: "VAL", ... } with proper typing
|
||||
* so callers can import paymentStatusOptions.VOID etc.
|
||||
*/
|
||||
export declare function makeEnumOptions<T extends string | number>(schema: any): { [K in T & (string | number)]: K; };
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
1
packages/db/utils/index.d.ts.map
Normal file
1
packages/db/utils/index.d.ts.map
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,EAAE,CAiB7E;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,GAMtD,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,GAAE,CAClD"}
|
||||
36
packages/db/utils/index.js
Normal file
36
packages/db/utils/index.js
Normal file
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.extractEnumValues = extractEnumValues;
|
||||
exports.makeEnumOptions = makeEnumOptions;
|
||||
/**
|
||||
* Extract enum values from a Zod enum or native enum schema.
|
||||
* Supports z.enum([...]) and z.nativeEnum(SomeTsEnum).
|
||||
*/
|
||||
function extractEnumValues(schema) {
|
||||
var _a, _b;
|
||||
// z.enum([...]) => schema.options exists
|
||||
if (Array.isArray(schema === null || schema === void 0 ? void 0 : schema.options)) {
|
||||
return schema.options;
|
||||
}
|
||||
// z.nativeEnum(SomeEnum) => schema._def?.values may exist or enum is in schema._def?.enum
|
||||
if (Array.isArray((_a = schema === null || schema === void 0 ? void 0 : schema._def) === null || _a === void 0 ? void 0 : _a.values)) {
|
||||
return schema._def.values;
|
||||
}
|
||||
if ((_b = schema === null || schema === void 0 ? void 0 : schema._def) === null || _b === void 0 ? void 0 : _b.enum) {
|
||||
// enum object -> values
|
||||
return Object.values(schema._def.enum);
|
||||
}
|
||||
throw new Error("Unsupported Zod schema type for enum extraction");
|
||||
}
|
||||
/**
|
||||
* Build a runtime map: { VAL: "VAL", ... } with proper typing
|
||||
* so callers can import paymentStatusOptions.VOID etc.
|
||||
*/
|
||||
function makeEnumOptions(schema) {
|
||||
const values = extractEnumValues(schema);
|
||||
const map = {};
|
||||
values.forEach((v) => {
|
||||
map[String(v)] = v;
|
||||
});
|
||||
return map;
|
||||
}
|
||||
35
packages/db/utils/index.ts
Executable file
35
packages/db/utils/index.ts
Executable file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Extract enum values from a Zod enum or native enum schema.
|
||||
* Supports z.enum([...]) and z.nativeEnum(SomeTsEnum).
|
||||
*/
|
||||
export function extractEnumValues<T extends string | number>(schema: any): T[] {
|
||||
// z.enum([...]) => schema.options exists
|
||||
if (Array.isArray(schema?.options)) {
|
||||
return schema.options as T[];
|
||||
}
|
||||
|
||||
// z.nativeEnum(SomeEnum) => schema._def?.values may exist or enum is in schema._def?.enum
|
||||
if (Array.isArray(schema?._def?.values)) {
|
||||
return schema._def.values as T[];
|
||||
}
|
||||
|
||||
if (schema?._def?.enum) {
|
||||
// enum object -> values
|
||||
return Object.values(schema._def.enum) as T[];
|
||||
}
|
||||
|
||||
throw new Error("Unsupported Zod schema type for enum extraction");
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a runtime map: { VAL: "VAL", ... } with proper typing
|
||||
* so callers can import paymentStatusOptions.VOID etc.
|
||||
*/
|
||||
export function makeEnumOptions<T extends string | number>(schema: any) {
|
||||
const values = extractEnumValues<T>(schema);
|
||||
const map = {} as Record<string, T>;
|
||||
values.forEach((v) => {
|
||||
map[String(v)] = v;
|
||||
});
|
||||
return map as { [K in T & (string | number)]: K };
|
||||
}
|
||||
Reference in New Issue
Block a user