build: Use prettier for typescript
Stop ignoring .ts files in prettier and fix existing issues.
This commit is contained in:
@@ -16,7 +16,7 @@ export class ApiPrefixInterceptor implements HttpInterceptor {
|
||||
/**
|
||||
* @param {SettingsService} settingsService Settings Service
|
||||
*/
|
||||
constructor(private settingsService: SettingsService) { }
|
||||
constructor(private settingsService: SettingsService) {}
|
||||
|
||||
/**
|
||||
* Intercepts a Http request and prefixes it with `serverUrl`.
|
||||
@@ -40,5 +40,4 @@ export class ApiPrefixInterceptor implements HttpInterceptor {
|
||||
}
|
||||
return next.handle(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,10 +14,9 @@ import { HttpCacheService } from './http-cache.service';
|
||||
*/
|
||||
@Injectable()
|
||||
export class CacheInterceptor implements HttpInterceptor {
|
||||
|
||||
private forceUpdate = false;
|
||||
|
||||
constructor(private httpCacheService: HttpCacheService) { }
|
||||
constructor(private httpCacheService: HttpCacheService) {}
|
||||
|
||||
/**
|
||||
* Configures interceptor options
|
||||
@@ -44,19 +43,17 @@ export class CacheInterceptor implements HttpInterceptor {
|
||||
subscriber.next(new HttpResponse(cachedData as Object));
|
||||
subscriber.complete();
|
||||
} else {
|
||||
next.handle(request)
|
||||
.subscribe(
|
||||
event => {
|
||||
if (event instanceof HttpResponse) {
|
||||
this.httpCacheService.setCacheData(request.urlWithParams, event);
|
||||
}
|
||||
subscriber.next(event);
|
||||
},
|
||||
error => subscriber.error(error),
|
||||
() => subscriber.complete()
|
||||
);
|
||||
next.handle(request).subscribe(
|
||||
(event) => {
|
||||
if (event instanceof HttpResponse) {
|
||||
this.httpCacheService.setCacheData(request.urlWithParams, event);
|
||||
}
|
||||
subscriber.next(event);
|
||||
},
|
||||
(error) => subscriber.error(error),
|
||||
() => subscriber.complete()
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,17 +21,16 @@ const log = new Logger('ErrorHandlerInterceptor');
|
||||
*/
|
||||
@Injectable()
|
||||
export class ErrorHandlerInterceptor implements HttpInterceptor {
|
||||
|
||||
/**
|
||||
* @param {AlertService} alertService Alert Service.
|
||||
*/
|
||||
constructor(private alertService: AlertService) { }
|
||||
constructor(private alertService: AlertService) {}
|
||||
|
||||
/**
|
||||
* Intercepts a Http request and adds a default error handler.
|
||||
*/
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
return next.handle(request).pipe(catchError(error => this.handleError(error)));
|
||||
return next.handle(request).pipe(catchError((error) => this.handleError(error)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,7 +38,7 @@ export class ErrorHandlerInterceptor implements HttpInterceptor {
|
||||
*/
|
||||
private handleError(response: HttpErrorResponse): Observable<HttpEvent<any>> {
|
||||
const status = response.status;
|
||||
let errorMessage = (response.error.developerMessage || response.message);
|
||||
let errorMessage = response.error.developerMessage || response.message;
|
||||
if (response.error.errors) {
|
||||
if (response.error.errors[0]) {
|
||||
errorMessage = response.error.errors[0].defaultUserMessage || response.error.errors[0].developerMessage;
|
||||
@@ -55,18 +54,26 @@ export class ErrorHandlerInterceptor implements HttpInterceptor {
|
||||
} else if (status === 403 && errorMessage === 'The provided one time token is invalid') {
|
||||
this.alertService.alert({ type: 'Invalid Token', message: 'Invalid Token. Please try again!' });
|
||||
} else if (status === 400) {
|
||||
this.alertService.alert({ type: 'Bad Request', message: errorMessage || 'Invalid parameters were passed in the request!' });
|
||||
this.alertService.alert({
|
||||
type: 'Bad Request',
|
||||
message: errorMessage || 'Invalid parameters were passed in the request!'
|
||||
});
|
||||
} else if (status === 403) {
|
||||
this.alertService.alert({ type: 'Unauthorized Request', message: errorMessage || 'You are not authorized for this request!' });
|
||||
this.alertService.alert({
|
||||
type: 'Unauthorized Request',
|
||||
message: errorMessage || 'You are not authorized for this request!'
|
||||
});
|
||||
} else if (status === 404) {
|
||||
this.alertService.alert({ type: 'Resource does not exist', message: errorMessage || 'Resource does not exist!' });
|
||||
} else if (status === 500) {
|
||||
this.alertService.alert({ type: 'Internal Server Error', message: 'Internal Server Error. Please try again later.' });
|
||||
} else if (status === 500) {
|
||||
this.alertService.alert({
|
||||
type: 'Internal Server Error',
|
||||
message: 'Internal Server Error. Please try again later.'
|
||||
});
|
||||
} else {
|
||||
this.alertService.alert({ type: 'Unknown Error', message: 'Unknown Error. Please try again later.' });
|
||||
}
|
||||
|
||||
throw response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,11 +22,10 @@ export interface HttpCacheEntry {
|
||||
*/
|
||||
@Injectable()
|
||||
export class HttpCacheService {
|
||||
|
||||
/** Key to cache Http Requests in storage. */
|
||||
private cachePersistenceStorageKey = 'mifosXHttpCache';
|
||||
|
||||
private cachedData: { [key: string]: HttpCacheEntry; } = {};
|
||||
private cachedData: { [key: string]: HttpCacheEntry } = {};
|
||||
private storage: Storage | null = null;
|
||||
|
||||
constructor() {
|
||||
@@ -122,5 +121,4 @@ export class HttpCacheService {
|
||||
const data = this.storage ? this.storage[this.cachePersistenceStorageKey] : null;
|
||||
this.cachedData = data ? JSON.parse(data) : {};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,13 +15,11 @@ import { ApiPrefixInterceptor } from './api-prefix.interceptor';
|
||||
* (see https://github.com/Microsoft/TypeScript/issues/13897)
|
||||
*/
|
||||
declare module '@angular/common/http' {
|
||||
|
||||
/**
|
||||
* Augment HttpClient with the added configuration methods from HttpService, to allow in-place replacement of
|
||||
* HttpClient with HttpService using dependency injection.
|
||||
*/
|
||||
export interface HttpClient {
|
||||
|
||||
/**
|
||||
* Enables caching for this request.
|
||||
* @param {boolean} forceUpdate Forces request to be made and updates cache entry.
|
||||
@@ -40,22 +38,21 @@ declare module '@angular/common/http' {
|
||||
* @returns {HttpClient} The new instance.
|
||||
*/
|
||||
disableApiPrefix(): HttpClient;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* From @angular/common/http/src/interceptor: allows to chain interceptors
|
||||
*/
|
||||
class HttpInterceptorHandler implements HttpHandler {
|
||||
|
||||
constructor(private next: HttpHandler, private interceptor: HttpInterceptor) { }
|
||||
constructor(
|
||||
private next: HttpHandler,
|
||||
private interceptor: HttpInterceptor
|
||||
) {}
|
||||
|
||||
handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
|
||||
return this.interceptor.intercept(request, this.next);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,18 +70,18 @@ export const HTTP_DYNAMIC_INTERCEPTORS = new InjectionToken<HttpInterceptor>('HT
|
||||
*/
|
||||
@Injectable()
|
||||
export class HttpService extends HttpClient {
|
||||
|
||||
constructor(private httpHandler: HttpHandler,
|
||||
private injector: Injector,
|
||||
@Optional() @Inject(HTTP_DYNAMIC_INTERCEPTORS) private interceptors: HttpInterceptor[] = []) {
|
||||
constructor(
|
||||
private httpHandler: HttpHandler,
|
||||
private injector: Injector,
|
||||
@Optional() @Inject(HTTP_DYNAMIC_INTERCEPTORS) private interceptors: HttpInterceptor[] = []
|
||||
) {
|
||||
super(httpHandler);
|
||||
|
||||
if (!this.interceptors) {
|
||||
// Configure default interceptors that can be disabled here
|
||||
this.interceptors = [
|
||||
this.injector.get(ApiPrefixInterceptor),
|
||||
this.injector.get(ErrorHandlerInterceptor)
|
||||
];
|
||||
this.injector.get(ErrorHandlerInterceptor)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,16 +113,11 @@ export class HttpService extends HttpClient {
|
||||
return new HttpService(
|
||||
this.httpHandler,
|
||||
this.injector,
|
||||
this.interceptors.filter(i => !(i instanceof interceptorType))
|
||||
this.interceptors.filter((i) => !(i instanceof interceptorType))
|
||||
);
|
||||
}
|
||||
|
||||
private addInterceptor(interceptor: HttpInterceptor): HttpService {
|
||||
return new HttpService(
|
||||
this.httpHandler,
|
||||
this.injector,
|
||||
this.interceptors.concat([interceptor])
|
||||
);
|
||||
return new HttpService(this.httpHandler, this.injector, this.interceptors.concat([interceptor]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user