Skip to content

Commit

Permalink
fix: Fix paging
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Mar 21, 2021
1 parent fd70ac9 commit a1e499c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
25 changes: 14 additions & 11 deletions src/http/http-axios-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -76,46 +79,46 @@ export class HttpAxiosImpl implements Http {
}
}

get<T>(path: string, body?: Body, init: Request = {}): Promise<T> {
get<T>(url: string, body?: Body, init: Request = {}): Promise<T> {
return this.request({
method: 'get',
path,
url,
body,
...init,
}).then((response) => response.data as T);
}

post<T>(path: string, body?: Body, init: Request = {}): Promise<T> {
post<T>(url: string, body?: Body, init: Request = {}): Promise<T> {
return this.request({
method: 'post',
path,
url,
body,
...init,
}).then((response) => response.data as T);
}

delete<T>(path: string, body?: Body, init: Request = {}): Promise<T> {
delete<T>(url: string, body?: Body, init: Request = {}): Promise<T> {
return this.request({
method: 'delete',
path,
url,
body,
...init,
}).then((response) => response.data as T);
}

put<T>(path: string, body?: Body, init: Request = {}): Promise<T> {
put<T>(url: string, body?: Body, init: Request = {}): Promise<T> {
return this.request({
method: 'put',
path,
url,
body,
...init,
}).then((response) => response.data as T);
}

patch<T>(path: string, body?: Body, init: Request = {}): Promise<T> {
patch<T>(url: string, body?: Body, init: Request = {}): Promise<T> {
return this.request({
method: 'patch',
path,
url,
body,
...init,
}).then((response) => response.data as T);
Expand Down
2 changes: 1 addition & 1 deletion src/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions src/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ export class Paginator<Params, Result>
};

async next(params?: Params): Promise<IteratorResult<Result>> {
if (this.nextUrl == null) {
return { done: true, value: null };
}

const response: Response<Result> = 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,
};
}
Expand Down
2 changes: 0 additions & 2 deletions src/repositories/timelines-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ export interface FetchTimelineParams extends DefaultPaginationParams {
export class TimelinesRepository {
readonly home: Paginator<FetchTimelineParams, Status[]>;
readonly public: Paginator<FetchTimelineParams, Status[]>;
readonly direct: Paginator<FetchTimelineParams, Status[]>;

constructor(private readonly http: Http, readonly version: string) {
this.home = this.getHomeIterable();
this.public = this.getPublicIterable();
this.direct = this.getDirect();
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/serializers/serializer-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export class SerializerImpl implements Serializer {
}
}

deserialize<T = Record<string, unknown>>(_type: MimeType, data: string): T {
return transformKeys(JSON.parse(data), camelCase);
deserialize<T = Record<string, unknown>>(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}`);
}
}
}

0 comments on commit a1e499c

Please sign in to comment.