Skip to content

jacobhumston/sxcu.api

Repository files navigation

sxcu.api

sxcu.api

Node.js library to interact with the sxcu.net API. (A cli is also included!)

sxcu.api was created to allow any developer of any skill set to easily create images, links, etc on sxcu.net without the hassle of learning the sxcu.net documentation directly.

This module has 0 dependencies. (excluding dev) Check for yourself!

Links

sxcu.api: Docs / Github / npm

sxcu.net: Website / API Docs / Discord / Donate

Updating to v2.0.0 from v1.x.x

The guide for doing so can be found here.

Installation

You can install sxcu.api using npm.

npm install sxcu.api

If you would like to install the latest developer build, you can install from the dev-build branch.

npm install "https://github.com/jacobhumston/sxcu.api.git#dev-build"

Documentation

The documentation includes api information and guides/tutorials.

Example Usage

Here is an example of uploading a file.

// Import the package.
import * as sxcu from 'sxcu.api';

// Set the request user agent to the default.
// This will be done for you on the first request if you don't do it yourself.
sxcu.UserAgent.useDefault();

// Upload the file and log the response.
sxcu.uploadFile('image.png')
    .then((response) => console.log(response))
    .catch((err) => console.log(err));

In v2.0.0 we made the switch to ESM and TypeScript. However, we still support CommonJS.

// Use CommonJS's require method.
const { uploadFile, UserAgent, categorizeImports } = require('sxcu.api');

UserAgent.useDefault();

uploadFile('your-img')
    .then((response) => console.log(response))
    .catch((err) => console.log(err));

If you preferred categorized imports, then you can use categorizeImports. This works with ESM and CommonJS.

// Import the package.
import { categorizeImports } from 'sxcu.api';

// Categorize imports.
const sxcu = categorizeImports();

// Set the request user agent to the default.
sxcu.userAgent.useDefault();

// Upload the file and log the response.
sxcu.files
    .uploadFile('image.png')
    .then((response) => console.log(response))
    .catch((err) => console.log(err));

Respecting rate limits has been made extremely easy, all you need to do is enable the request queue.

// Import methods.
import { toggleRequestQueue } from 'sxcu.api';

// Enable the request queue.
toggleRequestQueue(true, true);

Need to make a custom request? It's pretty simple as well. The request method allows you to make requests that respect rate limits as well (as long as toggleRequestQueue is used beforehand).

// Import methods.
import { request, toggleRequestQueue, resolveError } from 'sxcu.api';

// Enable the request queue.
toggleRequestQueue(true, true);

// Create your own request.
const response = await request({
    type: 'GET',
    statusErrors: [400, 429],
    baseUrl: 'https://sxcu.net/api/',
    path: `files/abc123`,
}).catch((error) => {
    throw resolveError(error);
});

Contributors

jacobhumston
Jacob Humston