Skip to content

node-fantasyJS

(written by d4nny)

Overview

The fantasyJS module provides functionality for interacting with the fantasy.cat API and the FC2 API. It also encapsulates the logic for working with IndexedDB and handling data storage.

The module can be used in any browser environment. It does work in a vanilla HTML/CSS/JS environament however, it is not intended to do so and it will break some methods. You can use it by removing the exports and the node:fs module. But keep in mind that this will break some methods.

The module can be used with a large variety of frameworks and contexts:


  • Single-page applications (SPAs): Whether you are using a framework like React, Angular, or Vue.js, or building a custom SPA, you can incorporate this code to manage client-side data storage.

  • Progressive Web Apps (PWAs): PWAs are web applications that can provide native-like experiences on mobile devices. This code can be used to enable data storage and offline capabilities in PWAs.

  • Hybrid mobile applications: If you are using frameworks like Cordova or Ionic to build hybrid mobile applications, this code can be used to store data on the device using IndexedDB.

  • Electron applications: Electron allows you to build cross-platform desktop applications using web technologies. You can use this code in Electron applications to implement client-side data storage.

Installation

Download the necessary files from here. The archive will always be named node-fantasyJS - vX.X.X.zip and it contains 2 files: fantasyJS.js (main source file) and fantasyJS-min.js (minified version of fantasyJS.js).

Usage

In order to use the files in your code you will need to import them according to the framework or technology you are using.

Angular/Vue/React: import { fantasyAPI, IndexedDB } from './path/to/fantasyJS'

IndexedDB()

Description

The API

IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs. This API uses indexes to enable high-performance searches of this data. For more information regarding indexedDB, check out the MDN documentation here.

The Function

The IndexedDB() function provides an interface for working with indexedDB, allowing you to store and retrieve data on the client-side.

The function accepts two parameters. IndexedDB(dbName, storeName).

  • dbName(string) it references the database name you want to modify.

  • storeName(string) it references the object stores. More information on object stores can be found here.

A database can contain multiple uniquely named object stores which can contain multiple objects or other data types.

It includes methods for setting, getting, removing, and clearing data in the IndexedDB database.

Methods

Note: The value parameter accepts all JavaScript data types. (More Info) The key parameter has to be a string.

set(key, value): Sets the value of a given key in the IndexedDB database.

get(key): Retrieves the value associated with a given key from the IndexedDB database.

remove(key): Removes the value associated with a given key from the IndexedDB database.

clear(): Clears all data stored in the IndexedDB database.

Usage
JavaScript
// Creating an instance of IndexedDB

const db = IndexedDB('dbName', 'storeName');



// Storing data

db.set(key, value);

// Here, 'key' is the identifier for the data and 'value' is the data to be stored



// Retrieving data

const data = db.get(key);

// Here, 'key' is the identifier for the data to be retrieved



// Removing data

db.remove(key);

// Here, 'key' is the identifier for the data to be removed



// Clearing the database

db.clear();



// IndexedDB function usage explained:

// 1. Create an instance by calling IndexedDB(dbName, storeName)

//    where 'dbName' is the name of the database and 'storeName' is the name of the object store

// 2. Use the 'set' method to store data by providing a key and value

// 3. Use the 'get' method to retrieve data by providing the key

// 4. Use the 'remove' method to remove data by providing the key

// 5. Use the 'clear' method to delete all data in the database


fantasyAPI

Description

fantasyAPI is the exported variable that contains the WebAPI() function. It is being exported in this way in order to prevent confusion.

This function encapsulates the functionality for making HTTP requests to web APIs, specifically the fantasy.cat web API. It provides methods for performing most of the API operations presented in the fantasy.cat SDK.

Methods

All the available methods are named after their counterparts as found in the web API documentation (here).

The methods available in this module follow this format:

JavaScript
1
2
3
4
5
methodName: async (key = isRequired(), scriptContent = isRequired(), software = 'universe') {

[ key, software, scriptContent ].forEach(sanityCheck)

}

Where:

  • isRequired() is a function that ensures the parameter entered is not missing.

  • sanityCheck() is a function that the makes sure the entered parameters are not empty strings.

  • software = 'universe' all methods' software parameter will default to 'universe' if no parameter is provided

The rest of the parameters are named after their web API counterparts. (Such as key = license key, and so on).

Usage
JavaScript
1
2
3
4
5
fantasyAPI.methodName(param1, param2, param3).then((data) => {

    doSomething(data);

});