working on get api cus feature

This commit is contained in:
John Yeo
2025-10-06 14:16:51 +01:00
parent a997947db8
commit feca672fab
84 changed files with 2591 additions and 1984 deletions

View File

@@ -21,8 +21,8 @@ export enum AffectedResource {
*
* @example
* // Features changed from array to object in V1_2
* const V1_2_FeaturesSchema = z.record(z.string(), APICusFeatureSchema);
* const V1_1_FeaturesSchema = z.array(APICusFeatureSchema);
* const V1_2_FeaturesSchema = z.record(z.string(), ApiCusFeatureSchema);
* const V1_1_FeaturesSchema = z.array(ApiCusFeatureSchema);
*
* class V1_2_FeaturesArrayToObject extends VersionChange {
* version = ApiVersion.V1_2;

View File

@@ -1,5 +1,5 @@
import type { ApiVersion } from "../ApiVersion.js";
import type { ApiVersionClass } from "../ApiVersionClass.js";
import { type ApiVersion, LATEST_VERSION } from "../ApiVersion.js";
import { ApiVersionClass } from "../ApiVersionClass.js";
import { getVersionsBetween } from "../versionRegistryUtils.js";
import type { AffectedResource } from "./VersionChange.js";
import { VersionChangeRegistryClass } from "./VersionChangeRegistryClass.js";
@@ -12,12 +12,20 @@ import { VersionChangeRegistryClass } from "./VersionChangeRegistryClass.js";
*
* @param input - Data in the newest version format
* @param data - Additional context data for transformations (optional)
* @param currentVersion - Version of the input data
* @param currentVersion - Version of the input data (defaults to LATEST_VERSION)
* @param targetVersion - Version to transform to (older)
* @param resource - Resource being transformed
*
* @example
* // Data is in V1_2 format, transform to V1_1
* // Data is in latest format, transform to V1_1 (currentVersion defaults to latest)
* const v1_1_data = applyResponseVersionChanges({
* input: latestCustomer,
* targetVersion: new ApiVersionClass(ApiVersion.V1_1),
* resource: AffectedResource.Customer
* });
*
* @example
* // Explicitly specify currentVersion
* const v1_1_data = applyResponseVersionChanges({
* input: v1_2_customer,
* currentVersion: new ApiVersionClass(ApiVersion.V1_2),
@@ -35,19 +43,21 @@ export function applyResponseVersionChanges<T = any, TData = any>({
}: {
input: T;
data?: TData;
currentVersion: ApiVersionClass;
currentVersion?: ApiVersionClass;
targetVersion: ApiVersionClass;
resource: AffectedResource;
}): T {
// Default currentVersion to latest if not provided
const _currentVersion = currentVersion || new ApiVersionClass(LATEST_VERSION);
// If versions are equal, no transformation needed
if (currentVersion.eq(targetVersion)) {
if (_currentVersion.eq(targetVersion)) {
return input;
}
// If target is newer than current, throw error (can't transform forward)
if (targetVersion.gt(currentVersion)) {
if (targetVersion.gt(_currentVersion)) {
throw new Error(
`Cannot transform forward from ${currentVersion} to ${targetVersion}. ` +
`Cannot transform forward from ${_currentVersion} to ${targetVersion}. ` +
"Transforms only work backwards to older versions.",
);
}
@@ -55,7 +65,7 @@ export function applyResponseVersionChanges<T = any, TData = any>({
// Get all versions between current and target (exclusive of target, inclusive of current)
const versionsToApply = getVersionsBetween({
from: targetVersion.value,
to: currentVersion.value,
to: _currentVersion.value,
}).filter((v) => v !== targetVersion.value); // Exclude target itself
// Sort versions from newest to oldest (we apply backwards)
@@ -204,12 +214,14 @@ export function applyRequestVersionChanges<T = any, TData = any>({
* expandArray.push(CusExpand.Invoices);
* }
*/
export function isChangeActive(
targetVersion: ApiVersionClass,
// biome-ignore lint/suspicious/noExplicitAny: Generic version change class constructor
changeClass: new () => any,
): boolean {
const change = new changeClass();
export function isBeforeChange({
targetVersion,
versionChange,
}: {
targetVersion: ApiVersionClass;
versionChange: new () => any;
}): boolean {
const change = new versionChange();
return targetVersion.lt(change.version);
}
@@ -226,7 +238,7 @@ export function applyResponseVersionChangesToArray<T = any, TData = any>({
}: {
inputArray: T[];
data?: TData;
currentVersion: ApiVersionClass;
currentVersion?: ApiVersionClass;
targetVersion: ApiVersionClass;
resource: AffectedResource;
}): T[] {

View File

@@ -6,11 +6,45 @@
export type { ApiVersionString } from "./ApiVersion.js";
export { API_VERSIONS, ApiVersion, LATEST_VERSION } from "./ApiVersion.js";
export { ApiVersionClass } from "./ApiVersionClass.js";
// Conversion utilities
export {
calVerToSemVer,
legacyToSemVer,
parseVersion,
semVerToCalVer,
semVerToLegacy,
} from "./convertVersionUtils.js";
// Org-specific utilities (deprecated)
export { getOrgApiVersion, toLegacyVersion } from "./orgVersionUtils.js";
// Branching utilities
export {
ifVersion,
requireVersion,
versionRange,
versionSwitch,
versionTernary,
} from "./versionBranchUtils.js";
export {
applyRequestVersionChanges,
applyRequestVersionChangesToArray,
applyResponseVersionChanges,
// Deprecated aliases (for backward compatibility)
applyResponseVersionChanges as applyVersionChanges,
applyResponseVersionChangesToArray,
applyResponseVersionChangesToArray as applyVersionChangesToArray,
getChangesForResource,
isBeforeChange,
} from "./versionChangeUtils/applyVersionChanges.js";
// Version changes
export {
AffectedResource,
VersionChange,
type VersionChangeConstructor,
} from "./versionChangeUtils/VersionChange.js";
export { VersionChangeRegistryClass } from "./versionChangeUtils/VersionChangeRegistryClass.js";
// Version registry
export type { VersionMetadata } from "./versionRegistry.js";
export { VERSION_REGISTRY } from "./versionRegistry.js";
// Version registry utilities
export {
CALVER_TO_SEMVER_MAP,
@@ -20,45 +54,5 @@ export {
isValidVersion,
} from "./versionRegistryUtils.js";
// Conversion utilities
export {
calVerToSemVer,
legacyToSemVer,
parseVersion,
semVerToCalVer,
semVerToLegacy,
} from "./convertVersionUtils.js";
// Org-specific utilities (deprecated)
export { getOrgApiVersion, toLegacyVersion } from "./orgVersionUtils.js";
// Branching utilities
export {
ifVersion,
requireVersion,
versionRange,
versionSwitch,
versionTernary,
} from "./versionBranchUtils.js";
// Version changes
export {
AffectedResource,
VersionChange,
type VersionChangeConstructor,
} from "./versionChangeUtils/VersionChange.js";
export { VersionChangeRegistryClass } from "./versionChangeUtils/VersionChangeRegistryClass.js";
export {
applyResponseVersionChanges,
applyResponseVersionChangesToArray,
applyRequestVersionChanges,
applyRequestVersionChangesToArray,
getChangesForResource,
isChangeActive,
// Deprecated aliases (for backward compatibility)
applyResponseVersionChanges as applyVersionChanges,
applyResponseVersionChangesToArray as applyVersionChangesToArray,
} from "./versionChangeUtils/applyVersionChanges.js";
// Auto-register all version changes
import "./versionChangeUtils/versionChangeRegistry.js";