Skip to content

Commit

Permalink
fix: Fix unhandled promise rejection of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Apr 10, 2022
1 parent 603d56c commit d205bf8
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 32 deletions.
19 changes: 11 additions & 8 deletions examples/create-new-status-with-image.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import fs from 'fs';
import { login } from 'masto';

(async () => {
const main = async () => {
const masto = await login({
url: 'https://example.com',
accessToken: 'YOUR TOKEN',
});

// Upload the image
const attachment = await masto
.mediaAttachments.create({
file: fs.createReadStream('../some_image.png'),
description: 'Some image',
})
const attachment = await masto.mediaAttachments.create({
file: fs.createReadStream('../some_image.png'),
description: 'Some image',
});

// Toot!
const status = await masto.statuses.create({
status: 'Toot from TypeScript',
visibility: 'direct',
mediaIds: [attachment.id],
})
});

console.log(status);
})()
};

main().catch((error) => {
console.error(error);
});
10 changes: 7 additions & 3 deletions examples/create-new-status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { login } from 'masto';

(async () => {
const main = async () => {
const masto = await login({
url: 'https://example.com',
accessToken: 'YOUR TOKEN',
Expand All @@ -9,5 +9,9 @@ import { login } from 'masto';
await masto.statuses.create({
status: 'Toot from TypeScript',
visibility: 'direct',
})
})()
});
};

main().catch((error) => {
console.error(error);
});
10 changes: 7 additions & 3 deletions examples/moderate-reports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { login } from 'masto';

(async () => {
const main = async () => {
const masto = await login({
url: 'https://example.com',
accessToken: 'TOKEN',
Expand All @@ -13,6 +13,10 @@ import { login } from 'masto';
await masto.admin.account.createAction(reports[0].account.id, {
type: 'disable',
reportId: reports[0].id,
text: 'Your account has been disabled'
text: 'Your account has been disabled',
});
})();
};

main().catch((error) => {
console.error(error);
});
10 changes: 5 additions & 5 deletions examples/object-oriented.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MyBot {
private handleUpdate = (status: Status) => {
const { content, account } = status;
console.log(`${account.username} said ${content}`);
}
};

private handleNotification = async (notification: Notification) => {
// When your status got favourited, log
Expand All @@ -46,10 +46,10 @@ class MyBot {
if (notification.type === 'follow') {
await this.masto.accounts.follow(notification.account.id);
}
}
};
}

// main
(async () => {
await MyBot.init();
})();
MyBot.init().catch((error) => {
console.error(error);
});
8 changes: 6 additions & 2 deletions examples/register-new-app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { login } from 'masto';

(async () => {
const main = async () => {
const masto = await login({
url: 'https://example.com',
});
Expand All @@ -13,4 +13,8 @@ import { login } from 'masto';
});

console.log(app);
})();
};

main().catch((error) => {
console.error(error);
});
8 changes: 6 additions & 2 deletions examples/timeline-rxjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { login, Status, Notification } from 'masto';
import { fromEvent } from 'rxjs';
import { filter, map } from 'rxjs/operators';

(async () => {
const main = async () => {
const masto = await login({ url: 'https://example.com' });
const timeline = await masto.stream.streamPublicTimeline();

Expand All @@ -21,4 +21,8 @@ import { filter, map } from 'rxjs/operators';
.subscribe((body) => {
console.log(body);
});
})();
};

main().catch((error) => {
console.error(error);
});
8 changes: 6 additions & 2 deletions examples/timeline-with-iterable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { login } from 'masto';

(async () => {
const main = async () => {
const masto = await login({
url: 'https://example.com',
});
Expand All @@ -23,4 +23,8 @@ import { login } from 'masto';
masto.statuses.favourite(status.id);
});
}
})();
};

main().catch((error) => {
console.error(error);
});
13 changes: 8 additions & 5 deletions examples/timeline-with-streaming.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { login } from 'masto';

(async () => {
const main = async () => {
const masto = await login({
url: 'https://example.com',
accessToken: 'YOUR TOKEN',
Expand All @@ -10,13 +10,16 @@ import { login } from 'masto';
const stream = await masto.stream.streamPublicTimeline();

// Subscribe to updates
stream.on('update', status => {
stream.on('update', (status) => {
console.log(`${status.account.username}: ${status.content}`);
});

// Subscribe to notifications
stream.on('notification', notification => {
stream.on('notification', (notification) => {
console.log(`${notification.account.username}: ${notification.type}`);
})
})()
});
};

main().catch((error) => {
console.error(error);
});
8 changes: 6 additions & 2 deletions examples/update-profile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import { login } from 'masto';

(async () => {
const main = async () => {
const masto = await login({
url: 'https://example.com',
accessToken: 'YOUR TOKEN',
Expand All @@ -14,4 +14,8 @@ import { login } from 'masto';
});

console.log(newProfile);
})()
};

main().catch((error) => {
console.error(error);
});

0 comments on commit d205bf8

Please sign in to comment.