Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: 📝 readme clarity #1928

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 54 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
# Megalodon

[![Test](https://github.com/h3poteto/megalodon/workflows/Test/badge.svg)](https://github.com/h3poteto/megalodon/actions?query=branch%3Amaster+workflow%3ATest)
[![NPM Version](https://img.shields.io/npm/v/megalodon.svg)](https://www.npmjs.com/package/megalodon)
[![GitHub release](https://img.shields.io/github/release/h3poteto/megalodon.svg)](https://github.com/h3poteto/megalodon/releases)
[![npm](https://img.shields.io/npm/dm/megalodon)](https://www.npmjs.com/package/megalodon)
[![NPM](https://img.shields.io/npm/l/megalodon)](/LICENSE.txt)

A Fediverse API Client library for node.js and browser. It provides REST API and streaming methods.
By using this library, you can take Mastodon, Pleroma, Friendica, and Firefish with the same interface.
Megalodon is a Fediverse API client library for [NodeJS](https://nodejs.org) and browsers.
This library allows for interfacing with [Mastodon](https://joinmastodon.org), [Pleroma](https://pleroma.social), [Friendica](https://friendi.ca), and [Firefish](https://joinfirefish.org) servers all with the same interface, providing REST API and streaming methods.

The Rust version is [megalodon-rs](https://github.com/h3poteto/megalodon-rs).

## Supporting
- [x] Mastodon
- [x] Pleroma
## Supports

- [x] Mastodon <img src="https://cdn.simpleicons.org/mastodon" alt="Mastodon" width=16 height=16>
ThatOneCalculator marked this conversation as resolved.
Show resolved Hide resolved
- [x] Pleroma <img src="https://cdn.simpleicons.org/pleroma" alt="Pleroma" width=16 height=16>
- [x] Friendica
- [x] Firefish
- [x] Firefish <img src="https://cdn.simpleicons.org/firefish" alt="Firefish" width=16 height=16>
- [x] Akkoma (Unofficial)
- [x] Widlebeest (Unofficial)

## Features

- [x] REST API
- [ ] Admin API
- [x] WebSocket for Streamings
- [x] WebSocket for streaming
- [x] Promisified methods
- [x] Proxy support
- [x] Support node.js and browser
- [x] Written in typescript
- [x] NodeJS and browser support
- [x] Written in TypeScript

## Install

```
$ npm install -S megalodon
```
```sh
# npm
npm install -S megalodon

or
# pnpm
pnpm add megalodon

```
$ yarn add megalodon
# yarn
yarn add megalodon
```

### Build for browser
**Important**: In browser, you can not use proxy.
### Build for browsers

If you want to build for browser, please use Webpack and set empty value for some libraries which are not supported in Node.js.
[Here](example/browser/webpack.config.js) is example Webpack configuration.
**Important**: In browsers, you cannot use the proxy.

If you want to build for browser, please use Webpack and set empty value for some libraries which are not supported in NodeJS.
[Here](https://github.com/h3poteto/megalodon/tree/master/example/browser/webpack.config.js) is example Webpack configuration.

## Usage
I prepared [examples](example), and please refer [documents](https://h3poteto.github.io/megalodon/) about each methods.

There are code [examples](https://github.com/h3poteto/megalodon/tree/master/example), abd please refer to the [documentation](https://h3poteto.github.io/megalodon/) about each method.

I explain some typical methods.
At first, please get your access token for a fediverse server.
If you don't have access token, or you want to register applications and get access token programmably, please refer [Authorization section](#authorization).


### Home timeline

```typescript
```ts
import generator, { Entity, Response } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
Expand All @@ -68,32 +72,33 @@ client.getHomeTimeline()
})
```

### Post toot
### Make a post

```typescript
```ts
import generator, { Entity, Response } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'
const toot: string = 'test toot'
const post: string = 'test post'

const client = generator('mastodon', BASE_URL, access_token)
client.postStatus(toot)
client.postStatus(post)
.then((res: Response<Entity.Status>) => {
console.log(res.data)
})
```

### Post medias
### Post media

Please provide a file to the argument.

```typescript
```ts
import generator, { Entity, Response } from 'megalodon'
import fs from 'fs'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'
const image = fs.readFileSync("test.image")
const image = fs.readFileSync("test-image.png")

const client = generator('mastodon', BASE_URL, access_token)
client.uploadMedia(image)
Expand All @@ -104,7 +109,7 @@ client.uploadMedia(image)

### WebSocket streaming

```typescript
```ts
import generator, { Entity, WebSocketInterface } from 'megalodon'

const BASE_URL: string = 'wss://pleroma.io'
Expand Down Expand Up @@ -146,11 +151,11 @@ stream.on('parser-error', (err: Error) => {
})
```


### Authorization
You can register applications, and get access tokens to use this method.

```typescript
You can register applications and/or get access tokens to use this method.

```ts
import generator, { OAuth } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
Expand All @@ -169,34 +174,38 @@ client.registerApp('Test App')
})
```

Please open `Autorhization URL` in your browser, and authorize this app.
Please open `Authorization URL` in your browser, and authorize this app.
In this time, you can get authorization code.

After that, get an access token.

```typescript
```ts
const code = '...' // Authorization code

client.fetchAccessToken(client_id, client_secret, code)
})
client.fetchAccessToken(clientId, clientSecret, code)
.then((tokenData: OAuth.TokenData) => {
console.log(tokenData.access_token)
console.log(tokenData.refresh_token)
})
.catch((err: Error) => console.error(err))
```

### Detect each SNS
You have to provide SNS name (e.g. `mastodon`, `pleroma`) to `generator` function.
But when you only know the URL and not the SNS, `detector` function can detect the SNS.
### Detect each server's software

You have to provide the server's software name (e.g. `mastodon`, `pleroma`, `firefish`) to the `generator` function.
But when you only know the URL and not the software the server runs on, the `detector` function can detect the server's software.

```typescript
```ts
import { detector } from 'megalodon'

const URL = 'https://mastodon.social'
const FIRST_URL = 'https://mastodon.social'
const SECOND_URL = 'https://firefish.social'

const first_server = await detector(MASTODON_URL)
const second_server = await detector(FIREFISH_URL)

const sns = await detector(URL)
console.log(sns)
console.log(first_server) // mastodon
console.log(second_server) // firefish
```

## License
Expand Down
Loading