Skip to content

Commit

Permalink
chore: Auto-set encoding
Browse files Browse the repository at this point in the history
auto set encoding
  • Loading branch information
neet committed Jul 25, 2023
1 parent fe47102 commit 604c3d6
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 144 deletions.
11 changes: 6 additions & 5 deletions src/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { snakeCase } from 'change-case';

import type { Http, HttpMetaParams } from '../http';
import { Paginator } from '../paginator';

function noop(): void {
//
}
import { noop } from '../utils/noop';
import { inferEncoding } from './encoding';

const get =
<T>(http: Http, context: string[]) =>
Expand All @@ -32,8 +30,11 @@ const apply =
}

const data = args[0];
const meta = args[1] as HttpMetaParams;
const path = '/' + context.map((name) => snakeCase(name)).join('/');
const meta = {
encoding: inferEncoding(action, path),
...(args[1] as HttpMetaParams),
} as HttpMetaParams;

switch (action) {
case 'fetch': {
Expand Down
16 changes: 16 additions & 0 deletions src/builder/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Encoding } from '../serializers';

export const inferEncoding = (action: string, path: string): Encoding => {
if (
(action === 'create' && path === '/api/v1/accounts') ||
(action === 'update' && path === '/api/v1/update_credentials') ||
(action === 'create' && path === '/api/v1/email') ||
(action === 'create' && path === '/api/v1/featured_tag') ||
(action === 'create' && path === '/api/v1/media') ||
(action === 'create' && path === '/api/v2/media')
) {
return 'multipart-form';
}

return 'json';
};
18 changes: 6 additions & 12 deletions src/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import type { AbortSignal, Headers } from '@mastojs/ponyfills';

import type { Encoding } from '../serializers';

// prettier-ignore
type HttpEncodingParam<T extends Encoding>
= T extends 'none' ? { readonly encoding: never }
: T extends 'json' ? { readonly encoding?: 'json' }
: { readonly encoding: T };

export type HttpMetaParams<T extends Encoding = 'none'> =
HttpEncodingParam<T> & {
readonly headers?: Headers;
readonly signal?: AbortSignal;
readonly timeout?: number;
};
export type HttpMetaParams<T extends Encoding = 'none'> = {
readonly headers?: Headers;
readonly encoding?: T;
readonly signal?: AbortSignal;
readonly timeout?: number;
};

export type HttpMethod = <T>(
path: string,
Expand Down
2 changes: 1 addition & 1 deletion src/mastodon/oauth/oauth-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export type CreateTokenParams = CreateTokenParamsWithPassword;
export interface OAuthRepository {
create(
params: CreateTokenParams,
meta: HttpMetaParams<'multipart-form'>,
meta?: HttpMetaParams<'multipart-form'>,
): Promise<Token>;
}
4 changes: 2 additions & 2 deletions src/mastodon/v1/repositories/account-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export interface AccountRepository {
*/
create(
params: CreateAccountParams,
meta: HttpMetaParams<'multipart-form'>,
meta?: HttpMetaParams<'multipart-form'>,
): Promise<Token>;

verifyCredentials: {
Expand All @@ -310,7 +310,7 @@ export interface AccountRepository {
*/
update(
params: UpdateCredentialsParams,
meta: HttpMetaParams<'multipart-form'>,
meta?: HttpMetaParams<'multipart-form'>,
): Promise<AccountCredentials>;
};

Expand Down
2 changes: 1 addition & 1 deletion src/mastodon/v1/repositories/email-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface EmailRepository {
*/
create(
params: CreateConfirmationParams,
meta: HttpMetaParams<'multipart-form'>,
meta?: HttpMetaParams<'multipart-form'>,
): Promise<void>;
};
}
2 changes: 1 addition & 1 deletion src/mastodon/v1/repositories/featured-tag-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface FeaturedTagRepository {
*/
create(
params: CreateFeaturedTagParams,
meta: HttpMetaParams<'multipart-form'>,
meta?: HttpMetaParams<'multipart-form'>,
): Promise<FeaturedTag>;

suggestions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export interface MediaAttachmentRepository {
*/
create(
params: CreateMediaAttachmentParams,
meta: HttpMetaParams<'multipart-form'>,
meta?: HttpMetaParams<'multipart-form'>,
): Promise<MediaAttachment>;
}
3 changes: 3 additions & 0 deletions src/utils/noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function noop(): void {
//
}
19 changes: 8 additions & 11 deletions test-utils/jest-global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ export default async (): Promise<void> => {
const container = process.env.MASTODON_CONTAINER ?? 'mastodon';
const tokenPool = new TokenPoolImpl(container, oauth, app);

const adminToken = await oauth.token.create(
{
grantType: 'password',
clientId: app.clientId!,
clientSecret: app.clientSecret!,
username: 'admin@localhost:3000',
password: 'mastodonadmin',
scope: 'read write follow push admin:read admin:write',
},
{ encoding: 'multipart-form' },
);
const adminToken = await oauth.token.create({
grantType: 'password',
clientId: app.clientId!,
clientSecret: app.clientSecret!,
username: 'admin@localhost:3000',
password: 'mastodonadmin',
scope: 'read write follow push admin:read admin:write',
});

globalThis.__misc__ = {
url,
Expand Down
19 changes: 8 additions & 11 deletions test-utils/pools/token-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,14 @@ export class TokenPoolImpl implements TokenPool {
confirmed: true,
});

const token = await this.oauth.token.create(
{
grantType: 'password',
clientId: this.app.clientId!,
clientSecret: this.app.clientSecret!,
username: email,
password,
scope: 'read write follow push admin:read admin:write',
},
{ encoding: 'multipart-form' },
);
const token = await this.oauth.token.create({
grantType: 'password',
clientId: this.app.clientId!,
clientSecret: this.app.clientSecret!,
username: email,
password,
scope: 'read write follow push admin:read admin:write',
});

this.cache.set(token);

Expand Down
31 changes: 13 additions & 18 deletions tests/v1/accounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ describe('account', () => {
const username = crypto.randomBytes(8).toString('hex');
const email = `${username}@example.com`;

const token = await session.rest.v1.accounts.create(
{
username,
email,
password: 'password',
agreement: true,
locale: 'en',
},
{ encoding: 'multipart-form' },
);
const token = await session.rest.v1.accounts.create({
username,
email,
password: 'password',
agreement: true,
locale: 'en',
});

expect(token.accessToken).toEqual(expect.any(String));
});
Expand All @@ -31,10 +28,9 @@ describe('account', () => {
it('updates credential', () => {
return sessions.use(async (session) => {
const random = Math.random().toString();
const me = await session.rest.v1.accounts.updateCredentials.update(
{ displayName: random },
{ encoding: 'multipart-form' },
);
const me = await session.rest.v1.accounts.updateCredentials.update({
displayName: random,
});
expect(me.displayName).toBe(random);
});
});
Expand Down Expand Up @@ -174,10 +170,9 @@ describe('account', () => {

it('lists featured tags', () => {
return sessions.use(async (client) => {
const featuredTag = await client.rest.v1.featuredTags.create(
{ name: 'mastodon' },
{ encoding: 'multipart-form' },
);
const featuredTag = await client.rest.v1.featuredTags.create({
name: 'mastodon',
});

const tags = await client.rest.v1.accounts
.select(client.id)
Expand Down
22 changes: 8 additions & 14 deletions tests/v1/emails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@ it('can create a confirmation', async () => {
const username = crypto.randomBytes(8).toString('hex');
let email = `${username}@example.com`;

const token = await admin.v1.accounts.create(
{
username,
email,
password: 'password',
agreement: true,
locale: 'en',
},
{ encoding: 'multipart-form' },
);
const token = await admin.v1.accounts.create({
username,
email,
password: 'password',
agreement: true,
locale: 'en',
});

const client = createClient({
url: __misc__.url,
accessToken: token.accessToken,
});

email = `${username}@example2.com`;
await client.v1.emails.confirmations.create(
{ email },
{ encoding: 'multipart-form' },
);
await client.v1.emails.confirmations.create({ email });
});
5 changes: 1 addition & 4 deletions tests/v1/featured-tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ it('lists suggestions', () => {
it('lists featured tags', () => {
return sessions.use(async (client) => {
const name = 'mastodon';
const featuredTag = await client.rest.v1.featuredTags.create(
{ name },
{ encoding: 'multipart-form' },
);
const featuredTag = await client.rest.v1.featuredTags.create({ name });

try {
const featuredTags = await client.rest.v1.featuredTags.list();
Expand Down
20 changes: 4 additions & 16 deletions tests/v1/follow-requests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
it('authorize follow requests', () => {
return sessions.use(2, async ([alice, bob]) => {
await alice.rest.v1.accounts.updateCredentials.update(
{ locked: true },
{ encoding: 'multipart-form' },
);
await alice.rest.v1.accounts.updateCredentials.update({ locked: true });

try {
let relationship = await bob.rest.v1.accounts.select(alice.id).follow();
Expand All @@ -18,21 +15,15 @@ it('authorize follow requests', () => {
});
expect(relationship.following).toBe(true);
} finally {
await alice.rest.v1.accounts.updateCredentials.update(
{ locked: false },
{ encoding: 'multipart-form' },
);
await alice.rest.v1.accounts.updateCredentials.update({ locked: false });
await bob.rest.v1.accounts.select(alice.id).unfollow();
}
});
});

it('reject follow requests', () => {
return sessions.use(2, async ([alice, bob]) => {
await alice.rest.v1.accounts.updateCredentials.update(
{ locked: true },
{ encoding: 'multipart-form' },
);
await alice.rest.v1.accounts.updateCredentials.update({ locked: true });

try {
let relationship = await bob.rest.v1.accounts.select(alice.id).follow();
Expand All @@ -47,10 +38,7 @@ it('reject follow requests', () => {
});
expect(relationship.following).toBe(false);
} finally {
await alice.rest.v1.accounts.updateCredentials.update(
{ locked: false },
{ encoding: 'multipart-form' },
);
await alice.rest.v1.accounts.updateCredentials.update({ locked: false });
}
});
});
10 changes: 2 additions & 8 deletions tests/v2/media.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const createFile = async () => {
describe('media', () => {
it('creates a media attachment', async () => {
const file = await createFile();
let media = await admin.v2.media.create(
{ file },
{ encoding: 'multipart-form' },
);
let media = await admin.v2.media.create({ file });
media = await waitForMediaAttachment(admin, media.id);

media = await admin.v1.media.select(media.id).fetch();
Expand All @@ -32,10 +29,7 @@ describe('media', () => {
it('creates media attachment without polling', () => {
return sessions.use(async (session) => {
const file = await createFile();
let media = await session.rest.v2.media.create(
{ file },
{ encoding: 'multipart-form' },
);
let media = await session.rest.v2.media.create({ file });
media = await waitForMediaAttachment(session.rest, media.id);
expect(media.type).toBe('image');
});
Expand Down
Loading

0 comments on commit 604c3d6

Please sign in to comment.