As I was developing multiple angular REST-services for my frontend, I came up with the idea of creating a base class BaseRestService
to handle common functionalities like headers and helper functions.
However, I encountered TypeErrors
when trying to call certain functions in my child service that extends the BaseRestService
. Upon debugging, I noticed that the child service was being treated as an instance of BaseRestService
instead of SomeExampleChildSerivce
. Can anyone shed light on this behavior or point out where I might be going wrong?
Below is the code for my BaseRestService
and a sample ChildService
:
BaseRestService:
@Injectable({
providedIn: 'root'
})
export abstract class BaseRestService {
headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `JWT ${this.authService.getToken()}`
});
constructor(
public httpClient: HttpClient,
public logger: LoggingService,
public authService: AuthenticationService,
) { }
public refreshToken(): void {
this.headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `${this.authService.getToken()}`
});
}
}
ChildService:
@Injectable({
providedIn: 'root'
})
export class DimensionService extends BaseRestService{
private url: string = `${BASE_URL}/dimensions`;
constructor(
public httpClient: HttpClient,
public logger: LoggingService,
public authService: AuthenticationService,
){
super(httpClient, logger, authService);
}
get(url: string): Observable<DimensionAttribute> {
return this.httpClient.get<DimensionAttribute>(url, { headers: this.headers })
}
list(url?: string, page = 0, size = 20): Observable<Page<DimensionAttributeWrapper>> {
if (!url) url = `${this.url}?page=${page}&size=${size}`;
return this.httpClient.get<Page<DimensionAttributeWrapper>>(url, { headers: this.headers })
}
}