added return structure

This commit is contained in:
2026-03-29 13:37:14 +02:00
parent 951fe36da3
commit 1f32935a29
4 changed files with 243 additions and 17 deletions

View File

@ -33,6 +33,65 @@ $mapUnionType = function (mixed $type) use ($mapType): string {
return $mapType(is_string($type) ? $type : null);
};
$normalizeTypeStructures = function (mixed $structure): array {
if (!is_array($structure)) {
return array();
}
if (array_key_exists('type', $structure) && array_key_exists('properties', $structure)) {
return array($structure);
}
return array_values(array_filter($structure, 'is_array'));
};
$findTypeStructure = function (?string $type, mixed $structure) use ($normalizeTypeStructures): array|null {
if (!is_string($type) || $type === '') {
return null;
}
$type = ltrim($type, '\\');
foreach ($normalizeTypeStructures($structure) as $one_structure) {
if (($one_structure['type'] ?? null) === $type) {
return $one_structure;
}
}
return null;
};
$mapTypeWithStructure = null;
$mapUnionTypeWithStructure = null;
$mapTypeWithStructure = function (?string $type, mixed $structure) use ($mapType, &$mapUnionTypeWithStructure): string {
if (is_array($structure) && array_key_exists('type', $structure) && array_key_exists('properties', $structure)) {
$properties = array();
foreach (($structure['properties'] ?? array()) as $property) {
if (!is_array($property) || !isset($property['name'])) {
continue;
}
$propertyType = $mapUnionTypeWithStructure($property['type'] ?? null, $property['type_structure'] ?? null);
if (!empty($property['nullable']) && strpos($propertyType, 'null') === false) {
$propertyType .= ' | null';
}
$properties[] = $property['name'] . ': ' . $propertyType;
}
return empty($properties) ? 'Record<string, unknown>' : '{ ' . implode('; ', $properties) . ' }';
}
return $mapType($type);
};
$mapUnionTypeWithStructure = function (mixed $type, mixed $structure) use ($mapType, $findTypeStructure, &$mapTypeWithStructure): string {
if (is_array($type)) {
$parts = array();
foreach ($type as $singleType) {
$typeName = is_string($singleType) ? $singleType : null;
$parts[] = $mapTypeWithStructure($typeName, $findTypeStructure($typeName, $structure));
}
$parts = array_values(array_unique($parts));
return empty($parts) ? 'unknown' : implode(' | ', $parts);
}
return $mapTypeWithStructure(is_string($type) ? $type : null, $structure);
};
?>
/**
* Generated by APIlite
@ -66,6 +125,20 @@ export interface APIliteMethodDoc {
description: string | null;
params: APIliteMethodParam[];
return: string | string[] | null;
return_structure: APIliteTypeStructure | APIliteTypeStructure[] | null;
}
export interface APIliteTypeStructureProperty {
name: string;
type: string | string[] | null;
nullable: boolean;
type_structure: APIliteTypeStructure | APIliteTypeStructure[] | null;
}
export interface APIliteTypeStructure {
type: string;
recursive?: boolean;
properties: APIliteTypeStructureProperty[];
}
export interface APIliteHelpResponse {
@ -149,7 +222,7 @@ class <?php echo $this->apiName; ?> {
$paramsSignature[] = $param['name'] . ($param['optional'] ? '?' : '') . ': ' . $paramType;
$paramsPayload[] = $param['name'];
}
$returnType = $mapUnionType($method['return']);
$returnType = $mapUnionTypeWithStructure($method['return'], $method['return_structure'] ?? null);
?>
<?php echo $method['name']; ?>(<?php echo implode(', ', $paramsSignature); ?>): Promise<APIliteActionResponse<<?php echo $returnType; ?>>> {
return this.callPromise<APIliteActionResponse<<?php echo $returnType; ?>>>('<?php echo $method['name']; ?>', { <?php echo implode(', ', $paramsPayload); ?> });