scratch-cloud
A TypeScript library that allows communication with the scratch cloud servers.
A short example:
import { ScratchCloud } from "@errorgamer2000/scratch-cloud";
async function main() {
const cloud = new ScratchCloud();
await cloud.login("<username>", "<password>");
const session = cloud.createSession(
"<project_id>",
false /* do not use TurboWarp servers */
);
session.on("set", (name: string, value: string) => {
console.log(`${name} was set to ${value}.`);
});
}
This is a rewrite of the cloudsession api from my original scratch3-api
module.
This module provides only methods to communicate with the Scratch/TurboWarp
cloud servers. To use methods that are not centered around the cloud servers,
use scratch-connect
(not yet published). For string to number conversions, see
stringstonumbers
.
Installation
Install with npm or other package manager:
npm install @errorgamer2000/scratch-cloud
Or by cloning this repository:
git clone https://github.com/ErrorGamer2000/scratch-cloud.git
Exports:
Classes
- ScratchCloud
Class used to manage login and clound interfacing.
- Session
ScratchCloud
Class used to manage login and clound interfacing.
Kind: global class
new ScratchCloud()
Create a new ScratchCloud api wrapper.
scratchCloud.login(username, password)
Log into scratch with the given username and password. This is required to use the cloud api, and failing to call this method will result in an error being thrown. Please note that this action will also log the user out of scratch on all devices.
Kind: instance method of ScratchCloud
Throws:
-
TypeError
If the username or password are incorrect.
-
Error
If the scratch server gives an invalid response.
Param | Description |
---|---|
username | The username of the account to log in with. |
password | The password of the account to log in with. |
scratchCloud.createSession(project, turbowarp)
Create a new Cloud Session for the given project.
Kind: instance method of ScratchCloud
Param | Default | Description |
---|---|---|
project | The ID of the project to connect to. |
|
turbowarp | false |
Use the TurboWarp cloud servers rather than the Scratch cloud servers. |
Session
Kind: global class
new Session(userData, projectId, turbowarp)
Create a new connection to the Scratch cloud servers. Should not be
called manually but rather through ScratchCloud.createSession
.
Param | Default | Description |
---|---|---|
userData | Data used to validate the user's login and connect using the correct account. |
|
projectId | The ID of the project to connect to. |
|
turbowarp | false |
Use the TurboWarp cloud servers rather than the Scratch cloud servers. |
session.set(name, value)
Set a cloud variable to the given value.
Kind: instance method of Session
Param | Description |
---|---|
name | The name of the cloud variable. |
value | The value to set it to. Can be a string or a number, but can only include numerical digits. |
session.get(name)
Get the value of the variable with the given name.
Kind: instance method of Session
Param | Description |
---|---|
name | The name of the cloud variable |
session.enableAutoPrefix()
Enable autoprefixing.
Kind: instance method of Session
See: Session.autoPrefix
session.disableAutoPrefix()
Disable autoprefixing.
Kind: instance method of Session
See: Session.autoPrefix
session.on(eventName, listener)
Adds the listener
function to the end of the listeners array for the
event named eventNam
e. No checks are made to see if the listener
has
already been added. Multiple calls passing the same combination of
eventName
and listener
will result in the listener
being added, and
called, multiple times.
Kind: instance method of Session
Param | Description |
---|---|
eventName | The name of the event. |
listener | The callback function. |
session.once(eventName, listener)
Adds a one-time listener
function for the event named eventName
.
The next time eventName
is triggered, this listener is removed and then
invoked.
Kind: instance method of Session
Param | Description |
---|---|
eventName | the name of the event. |
listener | the callback listener. |
© 2022 ErrorGamer2000