HackerNews TS(6) - api class,상속

ha·2022년 8월 3일
0

HackerNews JS

목록 보기
6/6

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();

0개의 댓글