lonestone / nest-sdk-generator

A REST SDK generator for NestJS. Strictly type your frontend's API calls :rocket:
MIT License
92 stars 10 forks source link

SDK generator writes wrong path #2

Closed samchon closed 2 years ago

samchon commented 2 years ago

image

All of paths written in the controller files are omitting the path separator.

For referece, my device is using the Windows.

clement-estone commented 2 years ago

I unfortunately cannot reproduce this locally. The import path lines are generated as follows:

out.push(`import type { ${types.join(', ')} } from "../_types/${normalizeExternalFilePath(file.replace(/\\/g, '/'))}";`)

Where normalizeExternalFilePath is:

export function normalizeExternalFilePath(importedFilePath: string): string {
  importedFilePath = path.normalize(importedFilePath)

  if (!importedFilePath.startsWith('../')) {
    return importedFilePath
  }

  let level = 0

  while (importedFilePath.startsWith('../')) {
    level++
    importedFilePath = importedFilePath.substr(3)
  }

  return `_external${level}/${importedFilePath}`
}

As you can see the path separator (/) is never removed so I don't get why they don't appear in the generated SDK. I'll investigate to see if this is a platform-specific problem.

valamidev commented 2 years ago

I just faced the same problem, so I think it is origin from that under Windows path.normalize() is working a bit different than:

I made a flow what is happening,

input to normalizeExternalFilePath:  src/database/entities/chainData.entity
outoput from normalizeExternalFilePath: src\database\entities\chainData.entity
writen out to the generated file: import type { ChainData } from "../_types/src\database\entities\chainData.entity";

Which is modified by Prettier when it saved to:

import type { ChainData } from "../_types/srcdatabaseentitieschainData.entity";
clement-estone commented 2 years ago

Fixed by merging #7