Skip to content

Commit

Permalink
feat: Drop Axios support
Browse files Browse the repository at this point in the history
BREAKING CHANGE: headers option for login() now should be a WHATWG object. Proxy support is also dropped.
  • Loading branch information
neet committed Dec 23, 2022
1 parent 0f1f253 commit 903c09d
Show file tree
Hide file tree
Showing 65 changed files with 582 additions and 803 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./fetch": {
"types": "./dist/index.d.ts",
"require": "./dist/fetch.js",
"import": "./dist/fetch.mjs"
}
},
"scripts": {
Expand All @@ -31,6 +26,7 @@
"axios": "1.1.3",
"change-case": "^4.1.2",
"eventemitter3": "^5.0.0",
"isomorphic-fetch": "^3.0.0",
"isomorphic-form-data": "^2.0.0",
"isomorphic-ws": "^5.0.0",
"semver": "^7.3.7",
Expand Down
16 changes: 0 additions & 16 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ module.exports = [
},
plugins: [commonjs(), json(), typescript(), autoExternal()],
},
{
input: './src/entrypoints/fetch.ts',
output: {
file: packageJSON.exports['./fetch'].require,
format: 'cjs',
},
plugins: [json(), typescript(), autoExternal()],
},
{
input: './src/entrypoints/fetch.ts',
output: {
file: packageJSON.exports['./fetch'].import,
format: 'esm',
},
plugins: [commonjs(), json(), typescript(), autoExternal()],
},
{
input: './src/entrypoints/nodejs.ts',
output: {
Expand Down
3 changes: 0 additions & 3 deletions src/clients/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ export class MastoAdminClient {

constructor(
private readonly http: Http,
private readonly version: string,
private readonly config: MastoConfig,
) {
this.account = new AdminRepositories.AccountRepository(
this.http,
this.version,
this.config,
);

this.report = new AdminRepositories.ReportRepository(
this.http,
this.version,
this.config,
);

Expand Down
130 changes: 31 additions & 99 deletions src/clients/masto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,119 +98,51 @@ export class MastoClient {
constructor(
private readonly http: Http,
private readonly ws: Ws,
readonly version: string,
readonly config: MastoConfig,
) {
this.admin = new MastoAdminClient(this.http, this.version, this.config);
this.stream = new StreamRepository(this.ws, this.version, this.config);
this.accounts = new AccountRepository(this.http, this.version, this.config);
this.announcements = new AnnouncementRepository(
this.http,
this.version,
this.config,
);
this.apps = new AppRepository(this.http, this.version, this.config);
this.blocks = new BlockRepository(this.http, this.version, this.config);
this.bookmarks = new BookmarkRepository(
this.http,
this.version,
this.config,
);
this.conversations = new ConversationRepository(
this.http,
this.version,
this.config,
);
this.customEmojis = new CustomEmojiRepository(
this.http,
this.version,
this.config,
);
this.directory = new DirectoryRepository(
this.http,
this.version,
this.config,
);
this.domainBlocks = new DomainBlockRepository(
this.http,
this.version,
this.config,
);
this.endorsements = new EndorsementRepository(
this.http,
this.version,
this.config,
);
this.favourites = new FavouriteRepository(
this.http,
this.version,
this.config,
);
this.featuredTags = new FeaturedTagRepository(
this.http,
this.version,
this.config,
);
this.filters = new FilterRepository(this.http, this.version, this.config);
this.followRequests = new FollowRequestRepository(
this.http,
this.version,
this.config,
);
this.instances = new InstanceRepository(
this.http,
this.version,
this.config,
);
this.lists = new ListRepository(this.http, this.version, this.config);
this.markers = new MarkerRepository(this.http, this.version, this.config);
this.admin = new MastoAdminClient(this.http, this.config);
this.stream = new StreamRepository(this.ws, this.config);
this.accounts = new AccountRepository(this.http, this.config);
this.announcements = new AnnouncementRepository(this.http, this.config);
this.apps = new AppRepository(this.http, this.config);
this.blocks = new BlockRepository(this.http, this.config);
this.bookmarks = new BookmarkRepository(this.http, this.config);
this.conversations = new ConversationRepository(this.http, this.config);
this.customEmojis = new CustomEmojiRepository(this.http, this.config);
this.directory = new DirectoryRepository(this.http, this.config);
this.domainBlocks = new DomainBlockRepository(this.http, this.config);
this.endorsements = new EndorsementRepository(this.http, this.config);
this.favourites = new FavouriteRepository(this.http, this.config);
this.featuredTags = new FeaturedTagRepository(this.http, this.config);
this.filters = new FilterRepository(this.http, this.config);
this.followRequests = new FollowRequestRepository(this.http, this.config);
this.instances = new InstanceRepository(this.http, this.config);
this.lists = new ListRepository(this.http, this.config);
this.markers = new MarkerRepository(this.http, this.config);
this.mediaAttachments = new MediaAttachmentRepository(
this.http,
this.version,
this.config,
);
this.mutes = new MuteRepository(this.http, this.version, this.config);
this.notifications = new NotificationsRepository(
this.http,
this.version,
this.config,
);
this.poll = new PollRepository(this.http, this.version, this.config);
this.preferences = new PreferenceRepository(
this.http,
this.version,
this.config,
);
this.mutes = new MuteRepository(this.http, this.config);
this.notifications = new NotificationsRepository(this.http, this.config);
this.poll = new PollRepository(this.http, this.config);
this.preferences = new PreferenceRepository(this.http, this.config);
this.pushSubscriptions = new PushSubscriptionsRepository(
this.http,
this.version,
this.config,
);
this.reports = new ReportRepository(this.http, this.version, this.config);
this.reports = new ReportRepository(this.http, this.config);
this.scheduledStatuses = new ScheduledStatusesRepository(
this.http,
this.version,
this.config,
);
this.statuses = new StatusRepository(this.http, this.version, this.config);
this.suggestions = new SuggestionRepository(
this.http,
this.version,
this.config,
);
this.timelines = new TimelinesRepository(
this.http,
this.version,
this.config,
);
this.trends = new TrendRepository(this.http, this.version, this.config);
this.email = new EmailRepository(this.http, this.version, this.config);
this.tags = new TagRepository(this.http, this.version, this.config);
this.followedTags = new FollowedTagRepository(
this.http,
this.version,
this.config,
);
this.statuses = new StatusRepository(this.http, this.config);
this.suggestions = new SuggestionRepository(this.http, this.config);
this.timelines = new TimelinesRepository(this.http, this.config);
this.trends = new TrendRepository(this.http, this.config);
this.email = new EmailRepository(this.http, this.config);
this.tags = new TagRepository(this.http, this.config);
this.followedTags = new FollowedTagRepository(this.http, this.config);
}

/**
Expand Down
162 changes: 162 additions & 0 deletions src/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import assert from 'node:assert';

import { SemVer } from 'semver';

import { MastoConfig } from './config';
import { SerializerNativeImpl } from './serializers';

describe('Config', () => {
it('creates header', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('1.0.0'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);
const headers = config.createHeader({ extra: 'header' });

expect(headers.get('Authorization')).toBe('Bearer token');
expect(headers.get('Content-Type')).toBe('application/json');
expect(headers.get('extra')).toBe('header');
});

it('overrides content-type header', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('1.0.0'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);

const headers = config.createHeader({
'Content-Type': 'multipart/form-data',
});

expect(headers.get('Authorization')).toBe('Bearer token');
expect(headers.get('Content-Type')).toBe('multipart/form-data');
});

it('resolves HTTP path', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('1.0.0'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);

const url = config.resolveHttpPath('/api/v1/yay').toString();
expect(url).toEqual('https://mastodon.social/api/v1/yay');
});

it('resolves HTTP path with query', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('1.0.0'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);

const url = config
.resolveHttpPath('/api/v1/yay', { query: 'true' })
.toString();
expect(url).toEqual('https://mastodon.social/api/v1/yay?query=true');
});

it('resolves WS path with path', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('2.8.4'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);

const url = config.resolveWebsocketPath('/path/to/somewhere');
expect(url).toEqual('wss://mastodon.social/path/to/somewhere');
});

it('resolves WS path with path with token when Sec-Websocket-Protocols is not supported', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('0.0.1'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);

const url = config.resolveWebsocketPath('/path/to/somewhere');
expect(url).toEqual(
'wss://mastodon.social/path/to/somewhere?access_token=token',
);
});

it('creates websocket protocol with token when supported', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('2.8.4'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);

expect(config.createWebsocketProtocols()).toEqual(['token']);
});

it('creates websocket protocol without token when not supported', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('0.0.1'),
accessToken: 'token',
},
new SerializerNativeImpl(),
);

expect(config.createWebsocketProtocols()).toEqual([]);
});

it('creates timeout controller with specific limit', () => {
const config = new MastoConfig(
{
url: 'https://mastodon.social',
streamingApiUrl: 'wss://mastodon.social',
version: new SemVer('0.0.1'),
accessToken: 'token',
timeout: 3000,
},
new SerializerNativeImpl(),
);

jest.useFakeTimers();
const controller = config.createTimeoutController();
assert(controller != undefined);

const callback = jest.fn();
controller.signal.addEventListener('abort', callback);

jest.advanceTimersByTime(2900);
expect(callback).not.toBeCalled();
jest.advanceTimersByTime(100);
expect(callback).toBeCalled();
jest.clearAllTimers();
});
});
Loading

0 comments on commit 903c09d

Please sign in to comment.