From 2ae64ed14044cb08eb69e5ec5f9e52e54c7d7f62 Mon Sep 17 00:00:00 2001 From: Ryo Igarashi Date: Wed, 4 Jan 2023 11:32:12 +0900 Subject: [PATCH] fix: Change searchParams type to Record Closes #672 --- src/config.spec.ts | 11 +++++------ src/config.ts | 6 ++---- src/http/base-http.ts | 2 +- src/http/http.ts | 2 +- src/mastodon/v1/repositories/account-repository.ts | 2 +- src/paginator.spec.ts | 6 +++--- src/paginator.ts | 4 +--- tests/accounts.spec.ts | 9 +++++++++ 8 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/config.spec.ts b/src/config.spec.ts index dedd79cb6..0c4506ed5 100644 --- a/src/config.spec.ts +++ b/src/config.spec.ts @@ -67,9 +67,11 @@ describe('Config', () => { ); const url = config - .resolveHttpPath('/api/v1/yay', new URLSearchParams({ query: 'true' })) + .resolveHttpPath('/api/v1/yay', { query: 'true', list: ['1', '2', '3'] }) .toString(); - expect(url).toEqual('https://mastodon.social/api/v1/yay?query=true'); + expect(url).toEqual( + 'https://mastodon.social/api/v1/yay?query=true&list[]=1&list[]=2&list[]=3', + ); }); it('resolves WS path with path', () => { @@ -114,10 +116,7 @@ describe('Config', () => { ); const url = config - .resolveHttpPath( - '/path/to/somewhere?foo=bar', - new URLSearchParams({ foo2: 'bar2' }), - ) + .resolveHttpPath('/path/to/somewhere?foo=bar', { foo2: 'bar2' }) .toString(); expect(url).toEqual('https://mastodon.social/path/to/somewhere?foo2=bar2'); }); diff --git a/src/config.ts b/src/config.ts index c10c5573a..881beca49 100644 --- a/src/config.ts +++ b/src/config.ts @@ -54,13 +54,11 @@ export class MastoConfig { : protocols; } - resolveHttpPath(path: string, params?: URLSearchParams): URL { + resolveHttpPath(path: string, params?: Record): URL { const url = new URL(path, this.props.url); if (params) { - url.search = this.serializer.serializeQueryString( - Object.fromEntries(params.entries()), - ); + url.search = this.serializer.serializeQueryString(params); } return url; diff --git a/src/http/base-http.ts b/src/http/base-http.ts index 632915602..01ef702a4 100644 --- a/src/http/base-http.ts +++ b/src/http/base-http.ts @@ -8,7 +8,7 @@ export abstract class BaseHttp implements Http { get(path: string, data?: unknown, init: RequestInit = {}): Promise { return this.request({ path, - searchParams: new URLSearchParams(data as Record), + searchParams: data as Record, requestInit: { method: 'GET', ...init, diff --git a/src/http/http.ts b/src/http/http.ts index 44a2e39cd..4f531e539 100644 --- a/src/http/http.ts +++ b/src/http/http.ts @@ -8,7 +8,7 @@ export type HttpMethod = ( export type HttpRequestParams = { readonly path: string; - readonly searchParams?: URLSearchParams; + readonly searchParams?: Record; readonly body?: Record; readonly requestInit?: Omit; }; diff --git a/src/mastodon/v1/repositories/account-repository.ts b/src/mastodon/v1/repositories/account-repository.ts index e8b67c13a..12dae0f3c 100644 --- a/src/mastodon/v1/repositories/account-repository.ts +++ b/src/mastodon/v1/repositories/account-repository.ts @@ -232,7 +232,7 @@ export class AccountRepository * @see https://docs.joinmastodon.org/methods/accounts/ */ @version({ since: '0.0.0' }) - fetchRelationships(id: string[]): Promise { + fetchRelationships(id: readonly string[]): Promise { return this.http.get('/api/v1/accounts/relationships', { id, }); diff --git a/src/paginator.spec.ts b/src/paginator.spec.ts index 42b172003..c084e49e7 100644 --- a/src/paginator.spec.ts +++ b/src/paginator.spec.ts @@ -19,7 +19,7 @@ describe('Paginator', () => { expect(http.request).toBeCalledWith({ requestInit: { method: 'GET' }, path: '/v1/api/timelines', - searchParams: new URLSearchParams({ foo: 'bar' }), + searchParams: { foo: 'bar' }, }); }); @@ -32,7 +32,7 @@ describe('Paginator', () => { expect(http.request).toBeCalledWith({ requestInit: { method: 'GET' }, path: '/v1/api/timelines', - searchParams: new URLSearchParams({ foo: 'bar' }), + searchParams: { foo: 'bar' }, }); }); @@ -47,7 +47,7 @@ describe('Paginator', () => { await paginator.next(); expect(http.request).toBeCalledWith({ requestInit: { method: 'GET' }, - searchParams: new URLSearchParams({ max_id: '109382006402042919' }), + searchParams: { max_id: '109382006402042919' }, path: '/api/v1/timelines/home', }); }); diff --git a/src/paginator.ts b/src/paginator.ts index 3a6d877e5..5be81ffe2 100644 --- a/src/paginator.ts +++ b/src/paginator.ts @@ -24,9 +24,7 @@ export class Paginator const response = await this.http.request({ requestInit: { method: 'GET' }, path: this.nextPath, - searchParams: new URLSearchParams( - this.nextParams as Record, - ), + searchParams: this.nextParams as Record, }); const next = this.pluckNext(response.headers.get('link'))?.split('?'); diff --git a/tests/accounts.spec.ts b/tests/accounts.spec.ts index a4ee58d70..f92df1cc6 100644 --- a/tests/accounts.spec.ts +++ b/tests/accounts.spec.ts @@ -84,4 +84,13 @@ describe('account', () => { .every((status) => status.inReplyToId == undefined), ).toBe(true); }); + + it('fetches relationships', async () => { + const accountIds = await client.v1.timelines + .listPublic() + .then((ar) => [ar[0], ar[1], ar[2]].map((s) => s.account.id)); + + const res = await client.v1.accounts.fetchRelationships(accountIds); + expect(res).toHaveLength(accountIds.length); + }); });