swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Getting error "An identifier or keyword cannot immediately follow a numeric literal" on api routes containing numbers (api/1/endpoint)

Open samzmann opened this issue 4 years ago • 5 comments

Some of my API routes look like this: /api/3/ContainerHistory/GetUserContainers This leads to the following error when generating types:

SyntaxError: An identifier or keyword cannot immediately follow a numeric literal. (1740:2)
  1738 |  * @request GET:/api/3/ContainerHistory/GetUserContainers
  1739 |  */
> 1740 | 3ContainerHistoryGetUserContainersList: (query?: { showCounts?: boolean, showActive?: boolean, showSold?: boolean, showReturned?: boolean, skip?: number, limit?: number }, params: RequestParams = {}) =>
       |  ^
  1741 |     this.request<VytalConsumerAppApiControllersV2ContainerHistoryControllerContainerHistoryResponse, any>({
  1742 |         path: `/api/3/ContainerHistory/GetUserContainers`,
  1743 |         method: 'GET',
    at e (/Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/parser-typescript.js:1:322)
    at /Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/parser-typescript.js:22:3328487
    at Object.parse (/Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/parser-typescript.js:22:3328520)
    at Object.parse$a [as parse] (/Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/index.js:12513:19)
    at coreFormat (/Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/index.js:14044:16)
    at formatWithCursor$1 (/Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/index.js:14284:14)
    at /Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/index.js:59304:12
    at Object.format (/Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/node_modules/prettier/index.js:59324:12)
    at prettierFormat (/Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/src/formatFileContent.js:68:19)
    at /Users/sam/vytal/partner-app/node_modules/swagger-typescript-api/src/formatFileContent.js:74:50 {
  loc: { start: { line: 1740, column: 2 } },
  codeFrame: '\x1B[0m \x1B[90m 1738 |\x1B[39m \x1B[90m * @request GET:/api/3/ContainerHistory/GetUserContainers\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 1739 |\x1B[39m \x1B[90m */\x1B[39m\x1B[0m\n' +
    '\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 1740 |\x1B[39m \x1B[35m3\x1B[39m\x1B[33mContainerHistoryGetUserContainersList\x1B[39m\x1B[33m:\x1B[39m (query\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m { showCounts\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m boolean\x1B[33m,\x1B[39m showActive\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m boolean\x1B[33m,\x1B[39m showSold\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m boolean\x1B[33m,\x1B[39m showReturned\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m boolean\x1B[33m,\x1B[39m skip\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m number\x1B[33m,\x1B[39m limit\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m number }\x1B[33m,\x1B[39m params\x1B[33m:\x1B[39m \x1B[33mRequestParams\x1B[39m \x1B[33m=\x1B[39m {}) \x1B[33m=>\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m      |\x1B[39m  \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 1741 |\x1B[39m     \x1B[36mthis\x1B[39m\x1B[33m.\x1B[39mrequest\x1B[33m<\x1B[39m\x1B[33mVytalConsumerAppApiControllersV2ContainerHistoryControllerContainerHistoryResponse\x1B[39m\x1B[33m,\x1B[39m any\x1B[33m>\x1B[39m({\x1B[0m\n' +
    '\x1B[0m \x1B[90m 1742 |\x1B[39m         path\x1B[33m:\x1B[39m \x1B[32m`/api/3/ContainerHistory/GetUserContainers`\x1B[39m\x1B[33m,\x1B[39m\x1B[0m\n' +
    "\x1B[0m \x1B[90m 1743 |\x1B[39m         method\x1B[33m:\x1B[39m \x1B[32m'GET'\x1B[39m\x1B[33m,\x1B[39m\x1B[0m"
}

This is the command I'm using to run the script:

yarn swagger-typescript-api -p https://xxx.json -o ./src/helpers/ -n apiGenerated.ts"

After examining the swagger-typescript-api options, I don't see anything that would fix this. Any idea how to resolve this?

samzmann avatar Nov 25 '21 11:11 samzmann

I'm facing same issue

SyntaxError: Unexpected token. A constructor, method, accessor, or property was expected. (6745:31)
  6743 |     }
  6744 |
> 6745 |     public request = async <T = any, E = any>({
       |                               ^
  6746 |         body,
  6747 |         secure,
  6748 |         path,
    at r (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/parser-typescript.js:1:557)
    at yx (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/parser-typescript.js:13:3304376)
    at Object.parse (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/parser-typescript.js:13:3304817)
    at Object.parse$d [as parse] (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/index.js:12975:19)
    at coreFormat (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/index.js:14525:16)
    at formatWithCursor$1 (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/index.js:14765:14)
    at /Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/index.js:60959:12
    at Object.format (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/prettier/index.js:60979:12)
    at prettierFormat (/Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/swagger-typescript-api/src/formatFileContent.js:68:19)
    at /Users/vitalijnizankovskij/.npm/_npx/ad2f6472e51cc62f/node_modules/swagger-typescript-api/src/formatFileContent.js:74:50 {
  loc: { start: { line: 6745, column: 31 } },
  codeFrame: '\x1B[0m \x1B[90m 6743 |\x1B[39m     }\x1B[0m\n' +
    '\x1B[0m \x1B[90m 6744 |\x1B[39m\x1B[0m\n' +
    '\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 6745 |\x1B[39m     \x1B[36mpublic\x1B[39m request \x1B[33m=\x1B[39m \x1B[36masync\x1B[39m \x1B[33m<\x1B[39m\x1B[33mT\x1B[39m \x1B[33m=\x1B[39m any\x1B[33m,\x1B[39m \x1B[33mE\x1B[39m \x1B[33m=\x1B[39m any\x1B[33m>\x1B[39m({\x1B[0m\n' +
    '\x1B[0m \x1B[90m      |\x1B[39m                               \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 6746 |\x1B[39m         body\x1B[33m,\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 6747 |\x1B[39m         secure\x1B[33m,\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 6748 |\x1B[39m         path\x1B[33m,\x1B[39m\x1B[0m'
}

vvitto avatar Dec 15 '21 21:12 vvitto

@vvitto I also have similar issue with operationId, I had to resolve this issue by removing operationId started with number, like operationId: 2ndChance I think it's because JavaScript cannot make method starting with number and this library also doesn't know how to deal with those cases.

ZeroCho avatar Dec 27 '21 09:12 ZeroCho

we have a similar issue, I guess that some backend libs (in my case swagger-php https://github.com/zircote/swagger-php/blob/master/src/Processors/OperationId.php) might generate "operationId": e.g. "e42e57b0ba92f23833e2cb4ebff10a3c" to add unique ID to each version of endpoint+method... Then sometimes it starts with a number and this ends in a similar error while regenerating TS code from API doc - number can't be used to start name of TS const

dkocich avatar Apr 19 '22 18:04 dkocich