Skip to content

Upgrading from previous versions to 0.9

Luis Fernando Planella Gonzalez edited this page Nov 24, 2017 · 3 revisions

Version 0.9 has introduced breaking changes, mostly motivated by Angular 5 release, which deprecated the Http module, in favor of the HttpClient module.

As such, the following changes were done in ng-swagger-gen:

  • The Http module is no longer used, but HttpClient instead;
  • The HttpClient module is automatically exported by the generated ApiModule;
  • We previously generated the ApiResponse class, to provide access to the response, specially the headers. However the standard HttpResponse class holds the same data. Hence, we have removed the ApiResponse class;
  • Previously all generated methods returned the ApiResponse. In most cases, however, access to the response is not needed. So, now we generate 2 versions of each method: one returning the body directly, and the other, suffixed with Response, will return the HttpResponse;
  • Previously the ApiConfiguration class had a static attribute to hold the root URL, plus static callbacks prepareRequestOptions and handleError, which were callback functions called before performing a request and after getting an error, respectively. This was needed because there were no callbacks in the old Http module. HttpClient, in the other hand, provides interceptors, which can be used on both cases. Hence, the ApiConfiguration class now only has the rootUrl atribute. However...;
  • ... the ApiConfiguration class no longer has the static rootUrl attribute. This class is now @Injectable(), and provided by the generated ApiModule. which means that your application should inject it and initialize the rootUrl before performing the first call. The default value for it is still taken from the Swagger definition, so if that is fine for the application, there's no need to customize it;
  • We think it was a bad initial decision to return Promises. Hence we've adapted to the standard Angular API and now return Observables instead. Those needing a promise are just a Observable.toPromise away :).

So, here is a checklist for the migration from previous versions:

  1. Instead of setting the static ApiConfiguration.rootUrl, inject the ApiConfiguration in some other service in your application and initialize it from there. Alternatively, define a provider for APP_INITIALIZER in your root module, like this:
export function initApiConfiguration(config: ApiConfiguration): Function {
  return () => {
    config.rootUrl = 'https://some-root-url.com';
  };
}
export const INIT_API_CONFIGURATION: Provider = {
  provide: APP_INITIALIZER,
  useFactory: initApiConfiguration,
  deps: [ApiConfiguration],
  multi: true
};

/**
 * Then declare the provider. In this example, the AppModule is also
 * importing the ApiModule, which is important to get access to generated services
 */
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    ApiModule
  ],
  providers: [
    INIT_API_CONFIGURATION
  ],
  bootstrap: [
    AppComponent
  ]
})
export class AppModule { }
  1. If you were using the ApiConfiguration.prepareRequestOptions or ApiConfiguration.handleError, create an HttpInterceptor instead. In the given link you'll find examples on how to set request headers. To handle errors, just pipe the tap operator and handle it in the interceptor. An example:
@Injectable()
export class ApiInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Apply the headers
    req = req.clone({
      setHeaders: {
        'ApiToken': '234567890'
      }
    });
    
    // Also handle errors globally
    return next.handle(req).pipe(
      tap(x => x, err => {
        // Handle this err
        console.error(`Error performing request, status code = ${err.status}`);
      })
    );
  }
}
  1. You can either change your code to handle Observables (and subscribe to them) or call the Observable.toPromise() method to keep the previous semantics;

  2. The errorHandler property in ng-swagger-gen.json is no longer used, as the error handler has been removed in favor of HttpClient's interceptors. If that property is in ng-swagger-gen.json, remove it.

Clone this wiki locally