swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17k stars 6.03k forks source link

[typescript-angular] Error providing BASE_PATH from NPM module #6730

Open martinmcwhorter opened 7 years ago

martinmcwhorter commented 7 years ago
Description

After generating a proxy for typescript-angular as an NPM module, I cannot provide the BASE_PATH without getting the following error:

BASE_PATH Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler (positi on 2:22 in the original .ts file), resolving symbol BASE_PATH

If I include the generated proxy as source typescript within the project, I can provide the BASE_PATH without this error.

Ideally I would like to separate the generated proxy into its own package.

The angular project versions:

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.4.4
node: 6.9.2
os: win32 x64
@angular/animations: 4.4.4
@angular/cdk: 2.0.0-beta.12
@angular/common: 4.4.4
@angular/compiler: 4.4.4
@angular/core: 4.4.4
@angular/forms: 4.4.4
@angular/http: 4.4.4
@angular/material: 2.0.0-beta.12
@angular/platform-browser: 4.4.4
@angular/platform-browser-dynamic: 4.4.4
@angular/router: 4.4.4
@angular/cli: 1.4.4
@angular/compiler-cli: 4.4.4
@angular/language-service: 4.4.4
typescript: 2.3.4
Swagger-codegen version

2.3.0-SNAPSHOT

Swagger declaration file content or url
Command line used for generation
$ java -jar swagger-codegen-cli.jar generate -i http://localhost:8080/foo/v2/api-docs -l typescript-angular -o lib/src/foo-proxy -c foo-proxy-config.json
cd lib/src/foo-proxy
yarn build
cd dist
yarn pack
cd ../../../
mv lib/src/foo-proxy/foo-proxy-v0.0.0-SNAPSHOT.201710180947.tgz lib/
yarn install ./lib/foo-proxy-v0.0.0-SNAPSHOT.201710180947.tgz
Steps to reproduce
import { BASE_PATH } from 'foo-proxy';

export function basePath(): string { return '.'; }

@NgModule({
  { provide: BASE_PATH, useFactory: basePath },
  // . . . 
})
export class CoreModule { }

On ng serve I get the reported error:

BASE_PATH Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler (positi on 2:22 in the original .ts file), resolving symbol BASE_PATH

If instead of packaging as a Node Module, I include as source -- I can provide the BASE_PATH without the compile error.

java -jar swagger-codegen-cli.jar generate -i http://localhost:8080/foo/v2/api-docs -l typescript-angular -o src/foo-proxy

Related issues/PRs
Suggest a fix/enhancement
macjohnny commented 7 years ago

could this be related to https://github.com/swagger-api/swagger-codegen/issues/6722?

kenisteward commented 7 years ago

@martinmcwhorter we are currently working on making the generated library AOT compiled into a dist that you can publish. Currently if you want it to work you can publish the entire uncompiled typescript generated library as my team has done here.

https://www.npmjs.com/package/@herd/angular-client

Basically we currently don't compile using NGC. the original template only used tsc which doesn't produce metadata.json

You can watch this pr https://github.com/swagger-api/swagger-codegen/pull/6735

The idea would be at the end the client supports UMD / ES2015 / ES5 all in FESM AOT pased on the Angular Library Format 4/5

Sorry for the inconvenience.

kenisteward commented 7 years ago

@martinmcwhorter The other alternative is just to setup the tsconfig.json / package.json / rollup yourself based on the Angular Libarary Format