Open2
tsの復習
この人のtsの書き方勉強になるな
/**
* Google Books APIのレスポンス
*/
export interface Result {
items: BookItem[];
kind: string;
totalItems: number;
}
/**
* 本の情報
*/
export interface BookItem {
id: string;
volumeInfo: {
title: string;
authors?: string[];
publishedDate?: string;
description?: string;
publisher?: string;
imageLinks?: {
smallThumbnail: string;
thumbnail: string;
};
pageCount: number;
previewLink?: string;
};
saleInfo?: {
listPrice: {
amount: number;
};
};
}
/**
* query parameters
*/
export interface Params {
q: string;
startIndex?: number;
}
export interface BookRepositoryInterface {
get(params: Params): Promise<Result>;
find(id: string): Promise<BookItem>;
}
import type { BookItem, BookRepositoryInterface, Params, Result } from './types'
import { httpClient } from '../httpClient'
export class BookRepository implements BookRepositoryInterface {
async get(params: Params) {
const { data } = await httpClient.get<Result>('/', { params })
return data
}
async find(id: string) {
const { data } = await httpClient.get<BookItem>(`/${id}`)
return data
}
}
tsではinterfaceもtype(型エイリアス)も型の継承やオーバーライドは可能。
同じもの定義した時エラーなるとか細かい違いはある