chargebee
Version:
A library for integrating with Chargebee.
57 lines (56 loc) • 2.09 kB
JavaScript
/**
* Lazy schema loader for opt-in Zod validation.
*
* Schemas are loaded on first use. Node.js automatically caches imported modules,
* so repeated imports of the same schema file are efficient.
*
* Each resource module lives at {@code schema/<resource_snake>.schema.ts}
* and exports one body schema per action.
*
* The body schema const name follows {@code ZodNamingStrategy.bodySchemaName} in sdk-generator:
* {@code {PascalAction}{PascalResource}BodySchema}
*
* Uses native dynamic import() - TypeScript transpiles appropriately for ESM/CJS targets.
*/
function toCamel(s) {
return s.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
}
function toPascal(s) {
const camel = toCamel(s);
return camel.charAt(0).toUpperCase() + camel.slice(1);
}
/** Matches ZodNamingStrategy.bodySchemaName() in sdk-generator. */
function schemaConstName(resourceKey, actionName) {
return toPascal(actionName) + toPascal(resourceKey) + 'BodySchema';
}
/** camelCase resource key → snake_case module basename (e.g. virtualBankAccount → virtual_bank_account). */
function resourceSchemaBasename(resourceKey) {
return resourceKey
.replace(/([A-Z])/g, '_$1')
.toLowerCase()
.replace(/^_/, '');
}
/**
* Load the Zod schema for a given resource + action.
* Returns null if no schema file exists for this combination.
*
* Node.js automatically caches the imported modules, so repeated calls
* for the same resource will reuse the cached module.
*
* TypeScript compiler handles module system differences:
* - ESM build: keeps await import() as-is
* - CJS build: transpiles to require() or Promise-based equivalent
*/
export async function getSchema(resourceKey, actionName) {
var _a;
const base = resourceSchemaBasename(resourceKey);
const constName = schemaConstName(resourceKey, actionName);
try {
const mod = await import(`./schema/${base}.schema.js`);
const schema = (_a = mod[constName]) !== null && _a !== void 0 ? _a : null;
return schema;
}
catch (_b) {
return null;
}
}