@edixon/css-battle-api
TypeScript icon, indicating that this package has built-in type declarations

0.7.8 • Public • Published

CSS Battle API

code style: prettier

First API client to get public information from the CSS Battle page. Created with NodeJS and Typescript, all types are exposed for use.   Logo typescript + 💗

📃 NOTE: For now can only get the profile information of users, but later methods will be created to obtain: "leader board", "battle targets", etc.

📃 NOTE: To be able to use api from a frontend app, you must use a proxy, the library comes with one by default, it is on a free server so don't expect quick results.

Example: shield to Github ➜

Installation

  • NPM
npm install @edixon/css-battle-api
  • CDN
<!-- Bundle optimized to production -->
<script src="https://cdn.jsdelivr.net/npm/@edixon/css-battle-api@0.7.3/dist/bundle/CSSBattleAPI.min.js"></script>

Usage

We require the library and we get the CSSBattleAPI class, then we create an instance to use the query methods.

const { CSSBattleAPI } = require('@edixon/css-battle-api')

const CBA = new CSSBattleAPI()

CBA.profile('USERNAME').then(profile => {
  console.log(profile.ranking.totalScore) // Total score obtained in CSS Battle
})

You can also pass a configuration object to the class to set a proxy and be able to use of the client side api. Can enter a URL or true to use the default proxy.

const CBA = new CSSBattleAPI({
  proxy: /* boolean | string */
})

Using typescript, async/await and try/catch.

When an error occurs a string error is returned, is a friendly message that describe the cause of the error in the query.

import { CSSBattleAPI, TRanking } from '@edixon/css-battle-api'

async function getRanking(username: string): Promise<TRanking> {
  try {
    const { ranking } = await new CSSBattleAPI().profile(username)

    return ranking
  } catch (error) {
    throw new Error(error)
  }
}

Types

type TProfile = {
  avatar: string
  name: string
  username: string
  job: string | null
  country: string | null
  networks: TNetworks
  ranking: TRanking
}

type TNetworks = {
  website: string | null
  github: string | null
  twitter: string | null
  codepen: string | null
} | null

type TRanking = {
  rank: number
  totalPlayers: number
  totalScore: number
  battlesPlayed: number
}

Contributing

See CONTRIBUTING.md

License

MIT © Edixon Piña

Package Sidebar

Install

npm i @edixon/css-battle-api

Weekly Downloads

7

Version

0.7.8

License

MIT

Unpacked Size

500 kB

Total Files

31

Last publish

Collaborators

  • edixonalberto