From a1e499cab860b4c6ce550cab02551d2974d9c941 Mon Sep 17 00:00:00 2001 From: Ryo Igarashi Date: Sun, 21 Mar 2021 15:16:52 +0900 Subject: [PATCH] fix: Fix paging --- src/http/http-axios-impl.ts | 25 +++++++++++++----------- src/http/http.ts | 2 +- src/paginator.ts | 9 +++++++-- src/repositories/timelines-repository.ts | 2 -- src/serializers/serializer-impl.ts | 9 +++++++-- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/http/http-axios-impl.ts b/src/http/http-axios-impl.ts index 5702d33ae..4663a15dc 100644 --- a/src/http/http-axios-impl.ts +++ b/src/http/http-axios-impl.ts @@ -31,7 +31,10 @@ export class HttpAxiosImpl implements Http { transformRequest: (data, headers) => this.serializer.serialize(headers['Content-Type'], data), transformResponse: (data, headers) => - this.serializer.deserialize(headers['Content-Type'], data), + this.serializer.deserialize( + headers['Content-Type'] ?? 'application/json', + data, + ), paramsSerializer: (params) => this.serializer.serialize('application/json', params) as string, ...config, @@ -76,46 +79,46 @@ export class HttpAxiosImpl implements Http { } } - get(path: string, body?: Body, init: Request = {}): Promise { + get(url: string, body?: Body, init: Request = {}): Promise { return this.request({ method: 'get', - path, + url, body, ...init, }).then((response) => response.data as T); } - post(path: string, body?: Body, init: Request = {}): Promise { + post(url: string, body?: Body, init: Request = {}): Promise { return this.request({ method: 'post', - path, + url, body, ...init, }).then((response) => response.data as T); } - delete(path: string, body?: Body, init: Request = {}): Promise { + delete(url: string, body?: Body, init: Request = {}): Promise { return this.request({ method: 'delete', - path, + url, body, ...init, }).then((response) => response.data as T); } - put(path: string, body?: Body, init: Request = {}): Promise { + put(url: string, body?: Body, init: Request = {}): Promise { return this.request({ method: 'put', - path, + url, body, ...init, }).then((response) => response.data as T); } - patch(path: string, body?: Body, init: Request = {}): Promise { + patch(url: string, body?: Body, init: Request = {}): Promise { return this.request({ method: 'patch', - path, + url, body, ...init, }).then((response) => response.data as T); diff --git a/src/http/http.ts b/src/http/http.ts index ede3cbf4d..d52561065 100644 --- a/src/http/http.ts +++ b/src/http/http.ts @@ -2,7 +2,7 @@ export type Headers = { readonly [key: string]: unknown }; export type Body = unknown; export type Request = { - readonly path?: string; + readonly url?: string; readonly method?: 'get' | 'post' | 'patch' | 'delete' | 'put' | 'options'; readonly headers?: Headers; readonly body?: Body; diff --git a/src/paginator.ts b/src/paginator.ts index 7c054a1e9..b4ed5f306 100644 --- a/src/paginator.ts +++ b/src/paginator.ts @@ -20,16 +20,21 @@ export class Paginator }; async next(params?: Params): Promise> { + if (this.nextUrl == null) { + return { done: true, value: null }; + } + const response: Response = await this.http.request({ method: 'get', - path: params ? this.nextUrl : this.initialUrl, + // if no params specified, use link header + url: params ? this.initialUrl : this.nextUrl, body: params ?? this.nextParams, }); this.nextUrl = this.pluckNext(response.headers?.link as string); return { - done: this.nextUrl != null, + done: false, value: response.data, }; } diff --git a/src/repositories/timelines-repository.ts b/src/repositories/timelines-repository.ts index 8798bc8ac..ffdb86634 100644 --- a/src/repositories/timelines-repository.ts +++ b/src/repositories/timelines-repository.ts @@ -16,12 +16,10 @@ export interface FetchTimelineParams extends DefaultPaginationParams { export class TimelinesRepository { readonly home: Paginator; readonly public: Paginator; - readonly direct: Paginator; constructor(private readonly http: Http, readonly version: string) { this.home = this.getHomeIterable(); this.public = this.getPublicIterable(); - this.direct = this.getDirect(); } /** diff --git a/src/serializers/serializer-impl.ts b/src/serializers/serializer-impl.ts index be35ba899..d39c58e3c 100644 --- a/src/serializers/serializer-impl.ts +++ b/src/serializers/serializer-impl.ts @@ -28,7 +28,12 @@ export class SerializerImpl implements Serializer { } } - deserialize>(_type: MimeType, data: string): T { - return transformKeys(JSON.parse(data), camelCase); + deserialize>(type: MimeType, data: string): T { + switch (type) { + case 'application/json': + return transformKeys(JSON.parse(data), camelCase); + default: + throw new Error(`Unknown content type ${type}, ${data}`); + } } }