-
Notifications
You must be signed in to change notification settings - Fork 101
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, butHttpClient
instead; - The
HttpClient
module is automatically exported by the generatedApiModule
; - We previously generated the
ApiResponse
class, to provide access to the response, specially the headers. However the standardHttpResponse
class holds the same data. Hence, we have removed theApiResponse
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 withResponse
, will return theHttpResponse
; - Previously the
ApiConfiguration
class had a static attribute to hold the root URL, plus static callbacksprepareRequestOptions
andhandleError
, 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 oldHttp
module.HttpClient
, in the other hand, provides interceptors, which can be used on both cases. Hence, theApiConfiguration
class now only has therootUrl
atribute. However...; - ... the
ApiConfiguration
class no longer has the staticrootUrl
attribute. This class is now@Injectable()
, and provided by the generatedApiModule
. which means that your application should inject it and initialize therootUrl
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
Promise
s. Hence we've adapted to the standard Angular API and now returnObservable
s instead. Those needing a promise are just aObservable.toPromise
away :).
So, here is a checklist for the migration from previous versions:
- Instead of setting the static
ApiConfiguration.rootUrl
, inject theApiConfiguration
in some other service in your application and initialize it from there. Alternatively, define a provider forAPP_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 { }
- If you were using the
ApiConfiguration.prepareRequestOptions
orApiConfiguration.handleError
, create an HttpInterceptor instead. In the given link you'll find examples on how to set request headers. To handle errors, just pipe thetap
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}`);
})
);
}
}
-
You can either change your code to handle
Observable
s (and subscribe to them) or call theObservable.toPromise()
method to keep the previous semantics; -
The
errorHandler
property inng-swagger-gen.json
is no longer used, as the error handler has been removed in favor ofHttpClient
's interceptors. If that property is inng-swagger-gen.json
, remove it.