2.0.1 • Published 8 months ago

saweria v2.0.1

Weekly downloads
26
License
MIT
Repository
github
Last release
8 months ago

Saweria API

Node.js API Wrapper for Saweria.co

Check the Changelog

Installation

npm i saweria

Example

const { Client } = require("saweria");
// or
import { Client } from "saweria";

const client = new Client();

client.on("login", (user) => {
	console.log("Logged in as: ", user.username);
});

client.on("donations", (donations) => {
	console.log(donations);
});

client.login("email", "password");
// or with otp
client.login("email", "password", "otp");

or only for donation event listener

const { Client } = require("saweria");
// or
import { Client } from "saweria";

const client = new Client();

client.setStreamKey("your-stream-key");

client.on("donations", (donations) => {
	console.log(donations);
});

Client API

async login(jwt)

async login(email, password, otp = "")

Login to Saweria using JWT or email and password. If the account has no 2FA enabled, otp will be ignored.

This will set the default header authorization value for the future requests with user's JWT

Login with JWT is useful if your account has 2FA enabled and don't want to get your 2FA token every time you login. JWT generated by Saweria lasts for 72 hours, so you have to get a new JWT for your account every 72 hours. You can get your JWT by:

  1. Using this library
    1. Log in to your account using client.login(username, password)
    2. Get the account JWT from client.jwt
  2. Using the web client
    1. Go to saweria.co and login to your account
    2. Open browser developer console, and execute console.log(JSON.parse(localStorage["saweria-user-info"]).token);
  3. Sending a request to login endpoint
    1. Send a POST request to https://api.saweria.co/auth/login with these JSON body payload:
    {
    	"email": "example@email.com",
    	"password": "your-password",
    	"otp": "your otp code, optional if your account doesn't have 2FA enabled"
    }
    1. Get the JWT token from Authorization response header.

logout()

Removes authorization header from HTTP client, preventing future requests.


async setStreamKey(streamKey)

Set the client's stream key, this can be used to listen to donation event without logging in.


async getStreamKey()

Get user's stream key


async getUser()

Get user profile information


async getBalance()

Get user's balance


async getAvailableBalance()

Get user's available balance to disburse


async getTransaction(page = 1, pageSize = 15)

Get user's transaction list. Accepts these parameters:

  • page: What page of transaction to get (Default = 1)
  • pageSize: How many transactions per page (Default = 15)

async getMilestoneProgress(fromDate)

Get milestone progress from passed date with dd-mm-yyyy format until now.


async getLeaderboard(period = "all")

Get donation leaderboard from given time period.

period can be "all", "year", "month", or "week" (Default = "all")


async getVote()

Get currently active vote data


async sendFakeDonation()

Send a fake donation


on(eventName, callback)

Listen to client events and execute callback when emitted

Client Events

login

Emitted when client successfully logged in. Callback accepts User as the first parameter

Example:

client.on("login", (user) => {
	console.log("Logged in as: ", user.username);
});

donations

Emitted when client received a donation. Callback accepts array of EmittedDonation or EmittedMedia as the first parameter

Example:

client.on("donations", (donations) => {
	console.log(donations); // array of donations

	for (const donation of donations) {
		if (donation.type === "normal") {
			// normal donation
		} else if (donation.type === "media") {
			// donation with media
		}
	}
});

Normal Donation data example:

[
	{
		amount: "69420",
		donator: "Someguy",
		media: {
			src: [
				"https://media2.giphy.com/media/gw3IWyGkC0rsazTi/giphy.webp",
				"https://media2.giphy.com/media/gw3IWyGkC0rsazTi/giphy.mp4",
				"https://media2.giphy.com/media/gw3IWyGkC0rsazTi/giphy.gif",
			],
			tag: "picture",
		},
		message: "THIS IS A FAKE MESSAGE! HAVE A GOOD ONE",
		sound: {
			"1547679809default.ogg":
				"https://saweria-space.sgp1.cdn.digitaloceanspaces.com/prd/sound/836d7a85-dd70-4028-85fb-00fd785f0928-c527b4f6bd6282e21e78c85343d496fa.ogg",
		},
		tts: "...",
	},
];

Media Donation data example:

[
	{
		amount: "69420",
		donator: "Someguy",
		media: { end: 10, id: "RRKPkwBkz_0", start: 0, type: "yt" },
		message: "THIS IS A FAKE MESSAGE! HAVE A GOOD ONE",
		sound: null,
	},
];

donation

Similar to donations, but emits donation one by one when donations are received

2.0.1

8 months ago

2.0.0

8 months ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.5

3 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago