UNPKG

codeforces-plus

Version:

A Typescript library to simplify Codeforces API

226 lines (171 loc) β€’ 8.62 kB
# **Codeforces Plus** - A Simplified Codeforces API Client in TypeScript [![npm version](https://img.shields.io/npm/v/codeforces-plus.svg?style=flat-square&color=brightgreen)](https://www.npmjs.org/package/codeforces-plus) [![Built With TypeScript](https://img.shields.io/badge/Built%20With-TypeScript-007acc?style=flat-square)](https://www.typescriptlang.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&color=green)](https://opensource.org/licenses/MIT) [![install size](https://img.shields.io/badge/dynamic/json?url=https://packagephobia.com/v2/api.json?p=codeforces-plus&query=$.install.pretty&label=install%20size&style=flat-square&color=orange)](https://packagephobia.now.sh/result?p=codeforces-plus) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/codeforces-plus?style=flat-square&color=purple)](https://bundlephobia.com/package/codeforces-plus@latest) [![npm downloads](https://img.shields.io/npm/dm/codeforces-plus.svg?style=flat-square&color=red)](https://npm-stat.com/charts.html?package=codeforces-plus) `codeforces-plus` is a TypeScript library that abstracts the complexity of interacting with the [Codeforces API](https://codeforces.com/apiHelp). It provides a simple and easy-to-use interface to fetch data from Codeforces, eliminating the need for managing API requests or handling API authentication. With **codeforces-plus**, you can quickly access data like contest standings, user submissions, problem details, and much more without worrying about API types and intricate authentication steps. --- ## **Key Features** - ⚑ **Strongly Typed**: Fully typed methods and parameters, providing type safety and code completion in TypeScript. - πŸ“Š **Multi-Contest Leaderboard**: Combine leaderboards from multiple contests using the makeLeaderboard function, which is helpful for cases where each contest is designed for a single question. - πŸ–₯️ **Auto-Completion**: Enjoy IntelliSense features in your IDE, including suggestions for functions, types, and parameters as you type. - πŸ”„ **Seamless API Integration**: Automatically handles API signing for requests requiring authorization. - πŸš€ **Simple to Use**: Concise methods that eliminate manual API request creation. - πŸ”‘ **Easy Authentication**: Supports API key and secret-based authentication. - πŸ›‘οΈ **Error Handling**: Provides consistent error responses for easy debugging. --- ## **Installation** You can install `codeforces-plus` via [npm](https://www.npmjs.com/package/codeforces-plus) or [yarn](https://yarnpkg.com/package?q=codeforces-plus&name=codeforces-plus). ### Using npm: ```bash npm install codeforces-plus ``` ### Using yarn: ```bash yarn add codeforces-plus ``` ## Note 1. **Using Node.js Modules in Client-side Code**: The `node:crypto` module is not available on the client side. To ensure compatibility, you should utilize **Server Components** when working with the `codeforces-plus` package. This allows you to safely access Node.js modules such as `crypto` without running into issues. Refer [**example**](https://github.com/Nikhilesh002/codeforces-plus/tree/main/example) 2. **Importing the Package**: When using `codeforces-plus` in your Next.js application, make sure to import it directly from the `dist` directory as follows: ``` javascript import { fetchCodeforcesData,makeLeaderboard } from "codeforces-plus/dist"; ``` --- # Quick Start Below is an example of how to use the library to interact with the Codeforces API using [`codeforces-plus`](https://www.npmjs.com/package/codeforces-plus) package, such as fetching contest standings. ## Authentication Credentials For authenticated requests, you need to provide your Codeforces API key and secret. To generate these credentials, follow these steps: - Log in to your Codeforces account. - Go to your API settings page. - Generate and store your API key and secret. The codeforces-plus library will handle signing requests by calculating an apiSig based on your API credentials. ## Codeforces API methods - Please refer [**example**](https://github.com/Nikhilesh002/codeforces-plus/tree/main/example) usage. ``` typescript import {fetchCodeforcesData} from "codeforces-plus"; const creds = { // creds object is optional apiKey:"API_KEY", apiSecret:"API_SECRET" } const demoFun= async () => { const res=await fetchCodeforcesData({ creds, method:"contest.standings", props:{ contestId:566, asManager:true, participantTypes:"CONTESTANT" } }) console.log(res) } demoFun() ``` ### Public API methods For public API methods, credentials are not required. You can omit the credentials object: ```typescript import {fetchCodeforcesData} from "codeforces-plus"; const demoFun= async () => { const res=await fetchCodeforcesData({ method:"recentActions", props:{ maxCount:1 } }) console.log(res) } demoFun() ``` ## fetchCodeforcesData Response In case of errors during the API request, fetchCodeforcesData returns a response object that includes the status and error fields. Example of error handling: ``` cpp { status: 'OK', result: [ { timeSeconds: 1727572643, blogEntry: [Object], comment: [Object] }, { timeSeconds: 1727570332, blogEntry: [Object], comment: [Object] }, { timeSeconds: 1727568558, blogEntry: [Object], comment: [Object] }, { timeSeconds: 1727568011, blogEntry: [Object], comment: [Object] }, { timeSeconds: 1727567865, blogEntry: [Object], comment: [Object] } ], queryUrl: 'https://codeforces.com/api/recentActions?maxCount=5' } ``` ## Multi-Contest Leaderboard: makeLeaderboard Codeforces does not natively allow stopping submissions for a particular problem in a multi-problem contest. To address this, you can create separate contests for each problem, allowing greater control over submissions. The makeLeaderboard function allows you to combine the leaderboards from multiple contests into one, making it easier to view cumulative results across multiple single-question contests. Using **multiple mashup contests**, you can now conduct contests on Codeforces with full control, similar to HackerRank, providing a more tailored experience for participants. ``` typescript import { makeLeaderboard } from "codeforces-plus"; const creds = { apiKey:"API_KEY", apiSecret:"API_SECRET" } const demoLeaderboard = async () => { const res = await makeLeaderboard({ creds, contestIds: [542343, 542530], // Contest IDs for multiple single-question contests asManager: true, participantTypes: ["CONTESTANT", "MANAGER"] }); console.log(res); }; demoLeaderboard(); ``` - **Penalities are calculated with ICPC rules. For more details see code base**. ## makeLeaderboard Response ``` cpp { contestants:{ Nikhilesh002: { acceptedCount: 0, totalWrongs: 0, totalSubmissionTime: 0, totalPenality: 0, submissions: { A: { contestId: 542343, accepted: false, wrongs: 0, time: 0 }, B: { contestId: 542530, accepted: false, wrongs: 0, time: 0 } }, profileLink:"https://codeforces.com/profile/Nikhilesh002" }, Bhairava : { acceptedCount: 0, totalWrongs: 0, totalSubmissionTime: 0, totalPenality: 0, submissions: { A: { contestId: 542343, accepted: false, wrongs: 0, time: 0 } }, profileLink:"https://codeforces.com/profile/Bhairava" } }, problemIndexes: [ 'A', 'B' ], problemLinks: [ 'https://codeforces.com/gym/542343/problem/A', 'https://codeforces.com/gym/542530/problem/A' ] } ``` ## Auto Completion ### Method Types ![An image showing auto-completion of methods](./assets//method-recomentations.png) ### Props Types ![An image showing auto-completion of props](./assets//props-recomentations.png) ## Contributing I welcome contributions! Whether it’s fixing a bug, improving the documentation, or adding new features, feel free to open an issue or submit a pull request. ### To contribute: - Fork this repository. - Create a new branch (git checkout -b feature/your-feature-name). - Commit your changes (git commit -m 'Add new feature'). - Push the branch (git push origin feature/your-feature-name). - Open a pull request. ## License This project is licensed under the MIT License. Feel free to use it as you wish. ## Contact For support or questions, please create an issue in the repository. ## Changelog - v1.0.0 - Release with support for all Codeforces API methods. - v2.0.0 - Release with support for multiple single-question contests.