🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More
Socket
Sign inDemoInstall
Socket

tsraw

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsraw

TypeScript Reddit API Wrapper

1.0.8
latest
Source
npm
Version published
Weekly downloads
15
87.5%
Maintainers
0
Weekly downloads
 
Created
Source

TypeScript Reddit API Wrapper (TSRAW)

This is a proof of concept for a TypeScript wrapper for the Reddit API.

Example bootstrap code:


require('dotenv').config()
import { TSRAW } from "./TSRAW.js"
import { SubmitArgs } from "./types.js"

const { REDDIT_USER_AGENT, REDDIT_CLIENT_ID, REDDIT_CLIENT_SECRET, REDDIT_USERNAME, REDDIT_PASSWORD } = process.env

const CREDENTIALS = {
  user_agent: REDDIT_USER_AGENT!,
  client_id: REDDIT_CLIENT_ID!,
  client_secret: REDDIT_CLIENT_SECRET!,
  username: REDDIT_USERNAME!,
  password: REDDIT_PASSWORD!,
}

export class RedditBot {
  private r: TSRAW

  constructor(r: TSRAW) {
    this.r = r
  }

  async start() {
    const thread = await this.createThread('test', dailyThread())
  }

  async createThread(subreddit: string, content: ThreadContent) {
    const submission: SubmitArgs = {
      sr: subreddit,
      title: content.title,
      text: content.body,
      kind: 'self',
      sendreplies: false
    }

    return this.r.submit(submission)
  }
}

interface ThreadContent { title: string, body: string }

const dailyThread = (): ThreadContent => {
  const title =
    `Daily Discussion Thread (${new Date().toISOString().split('T')[0]})`

  const body =
    `Talk about anything your heart desires. Be polite and upvote everything!\n\n`+
    `How has your day been?`

  return { title, body }
}

const main = async () => {
  try {
    const r = await TSRAW.init(CREDENTIALS)
    const bot = new RedditBot(r)
    await bot.start()
  } catch (cause) {
    console.error(new Date().toISOString(), cause)
    process.exit(1312)
  }
}

main()

Interaces, see src/types.d.ts for more details

  constructor(token: string)

  static init(credentials: TSRAWCredentials): Promise<TSRAW>

  private rq<T extends RedditRes>(request: AxiosRequestConfig, raw?: boolean): Promise<T>

  submit (submission: SubmitArgs): Promise<SubmitResp>

  edit (post: EditArgs): Promise<SubmitResp>

  sticky (id: string): Promise<RedditRes> // Returns 500 (https://redd.it/1h41n06)

  getSettings (subreddit: string): Promise<SettingsResp>

  getMe (): Promise<AboutResp>

  updateSidebar (subreddit: string, description: string): Promise<RedditRes>

  threads (params: ThreadsReq): Promise<Thread[]>

  comments (params: CommentsReq): Promise<Comment[]>

Keywords

reddit

FAQs

Package last updated on 14 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts