Skip to content

Commit

Permalink
fix: Change searchParams type to Record
Browse files Browse the repository at this point in the history
Closes #672
  • Loading branch information
neet committed Jan 4, 2023
1 parent 7ce11fd commit 2ae64ed
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
11 changes: 5 additions & 6 deletions src/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
Expand Down
6 changes: 2 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ export class MastoConfig {
: protocols;
}

resolveHttpPath(path: string, params?: URLSearchParams): URL {
resolveHttpPath(path: string, params?: Record<string, unknown>): 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;
Expand Down
2 changes: 1 addition & 1 deletion src/http/base-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export abstract class BaseHttp implements Http {
get<T>(path: string, data?: unknown, init: RequestInit = {}): Promise<T> {
return this.request({
path,
searchParams: new URLSearchParams(data as Record<string, string>),
searchParams: data as Record<string, unknown>,
requestInit: {
method: 'GET',
...init,
Expand Down
2 changes: 1 addition & 1 deletion src/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type HttpMethod = <T>(

export type HttpRequestParams = {
readonly path: string;
readonly searchParams?: URLSearchParams;
readonly searchParams?: Record<string, unknown>;
readonly body?: Record<string, unknown>;
readonly requestInit?: Omit<RequestInit, 'body'>;
};
Expand Down
2 changes: 1 addition & 1 deletion src/mastodon/v1/repositories/account-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class AccountRepository
* @see https://docs.joinmastodon.org/methods/accounts/
*/
@version({ since: '0.0.0' })
fetchRelationships(id: string[]): Promise<Relationship[]> {
fetchRelationships(id: readonly string[]): Promise<Relationship[]> {
return this.http.get('/api/v1/accounts/relationships', {
id,
});
Expand Down
6 changes: 3 additions & 3 deletions src/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
});
});

Expand All @@ -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' },
});
});

Expand All @@ -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',
});
});
Expand Down
4 changes: 1 addition & 3 deletions src/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export class Paginator<Entity, Params = never>
const response = await this.http.request({
requestInit: { method: 'GET' },
path: this.nextPath,
searchParams: new URLSearchParams(
this.nextParams as Record<string, string>,
),
searchParams: this.nextParams as Record<string, unknown>,
});

const next = this.pluckNext(response.headers.get('link'))?.split('?');
Expand Down
9 changes: 9 additions & 0 deletions tests/accounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 2ae64ed

Please sign in to comment.