nestia icon indicating copy to clipboard operation
nestia copied to clipboard

Change import statement construction algorithm

Open samchon opened this issue 4 years ago • 0 comments

When nestia generates an SDK library, nestia implements the import statements by analyzing which types are used in the paramters and return value of the methods who are declared in the controller classes. Iterating the parameters and return types of the controller methods, nestia memorizes where those types are defined in.

After those analyses, nestia archives the import statements converting the import path to be adequate. If the target instance is capsuled in a namespace, the parent namespace would be imported. If the parent namespace is also being capsuled in another grandparent namespace, the grandparent would be imported.

Such import statement construction algorithm are correct in most case, however, there're some exceptional cases in the @types/node/index.d.ts, a typical types library for the NodeJS. As its object construction structure is very abnormal, when generating an SDK library function targeting a NestJS controller who are using the NodeJS object directly, such terrible output would be generated.

Therefore, to resolve such abnormal case, I need to change import statement construction algorithm in the basic level.

NestJS Controller

@nest.Controller("performance")
export class PerformanceController
{
    @helper.EncryptedRoute.Get("memory")
    public memory(): NodeJS.MemoryUsage
    {
        return process.memoryUsage();
    }
}

SDK function generated by the Nestia

/**
 * @controller PerformanceController.memory()
 * @path GET performance/memory
 */
export function memory(connection: IConnection): Promise<memory.Output>
{
    return Fetcher.fetch
    (
        connection,
        {"input_encrypted":false,"output_encrypted":true},
        "GET",
        `performance/memory`
    );
}
export namespace memory
{
    export type Output = Primitive<"process".global.NodeJS.MemoryUsage>;
}

samchon avatar Feb 28 '21 14:02 samchon