class Api + extends 사용
class Api {
url: string;
ajax: XMLHttpRequest;
constructor(url: string) {
this.url = url;
this.ajax = new XMLHttpRequest();
}
protected getRequest<AjaxResponse>(): AjaxResponse {
this.ajax.open('GET', this.url, false);
this.ajax.send();
return JSON.parse(this.ajax.response);
}
}
class NewsFeedApi extends Api {
getData(): NewsFeed[] {
return this.getRequest<NewsFeed[]>();
}
}
class NewsDetailApi extends Api {
getData(): NewsDetail {
return this.getRequest<NewsDetail>();
}
}
function newsDetail()에서 적용
const api = new NewsDetailApi(CONTENT_URL.replace('@id', id));
const newsContent = api.getData();