@docusign/iam-sdk
Version:
Developer-friendly & type-safe Typescript SDK specifically catered to leverage *@docusign/iam-sdk* API.
152 lines • 5.83 kB
JavaScript
;
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.smartUnion = smartUnion;
// Not needed if lax mode
const z = __importStar(require("zod/v3"));
const defaultToZeroValue_js_1 = require("./defaultToZeroValue.js");
const rfcdate_js_1 = require("./rfcdate.js");
const unrecognized_js_1 = require("./unrecognized.js");
/**
* Smart union parser that tries all schemas and returns the best match
* based on the number of populated fields.
*/
function smartUnion(options) {
return z.unknown().transform((input, ctx) => {
const candidates = [];
const errors = options.map(() => []);
const parentUnrecognizedCtr = (0, unrecognized_js_1.startCountingUnrecognized)();
const parentZeroDefaultCtr = (0, defaultToZeroValue_js_1.startCountingDefaultToZeroValue)();
// Filter out invalid options
for (const [i, option] of options.entries()) {
const unrecognizedCtr = (0, unrecognized_js_1.startCountingUnrecognized)();
const zeroDefaultCtr = (0, defaultToZeroValue_js_1.startCountingDefaultToZeroValue)();
const result = option.safeParse(input);
const inexactCount = unrecognizedCtr.end();
const zeroDefaultCount = zeroDefaultCtr.end();
if (result.success) {
candidates.push({
data: result.data,
inexactCount,
zeroDefaultCount,
fieldCount: -1, // We'll count this later if needed
});
continue;
}
errors[i].push(...result.error.issues);
}
// No valid options
if (candidates.length === 0) {
parentUnrecognizedCtr.end(0);
parentZeroDefaultCtr.end(0);
ctx.addIssue({
code: "invalid_union",
unionErrors: errors.map(issues => new z.ZodError(issues)),
});
return z.NEVER;
}
let best = candidates[0];
// Find the best option
for (const candidate of candidates) {
// Minor optimization to avoid counting fields if there's only one candidate
if (candidates.length > 1) {
candidate.fieldCount = countFieldsRecursive(candidate.data);
}
best = better(candidate, best);
}
// The cost of this union should be the cost of the best candidate not all the candidates
parentUnrecognizedCtr.end(best.inexactCount);
parentZeroDefaultCtr.end(best.zeroDefaultCount);
return best.data;
});
}
function better(a, b) {
// First prefer exact matches over inexact ones
const aIsExact = a.inexactCount === 0;
const bIsExact = b.inexactCount === 0;
if (aIsExact !== bIsExact) {
return aIsExact ? a : b;
}
// Then compare field counts
const actualFieldCountA = a.fieldCount - a.zeroDefaultCount;
const actualFieldCountB = b.fieldCount - b.zeroDefaultCount;
if (actualFieldCountA !== actualFieldCountB) {
return actualFieldCountA > actualFieldCountB ? a : b;
}
return a.inexactCount < b.inexactCount ? a : b;
}
/**
* Counts the number of fields in a parsed value recursively.
* @param `parsed` assumed to *not* contain cycles
* fieldCount: total number of fields found
* inexactCount: number of primitive values that are not unrecognized enum values
*/
function countFieldsRecursive(parsed) {
let fieldCount = 0;
const queue = [parsed];
let index = 0;
while (index < queue.length) {
const value = queue[index++];
if (value === undefined) {
continue;
}
// Check if it's a primitive value
const type = typeof value;
if (value === null
|| type === "number"
|| type === "string"
|| type === "boolean"
|| type === "bigint"
|| value instanceof Date
|| value instanceof rfcdate_js_1.RFCDate) {
fieldCount++;
continue;
}
// Handle arrays
if (Array.isArray(value)) {
queue.push(...value);
continue;
}
// Handle objects
if (type === "object") {
queue.push(...Object.values(value));
}
}
return fieldCount;
}
//# sourceMappingURL=smartUnion.js.map