Skip to content

Commit

Permalink
Update turboapi.js
Browse files Browse the repository at this point in the history
  • Loading branch information
liablelua authored Sep 4, 2023
1 parent 2411619 commit d18b427
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions extensions/turboapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,78 @@

(function (Scratch) {
"use strict";

let APIResponse = null;
let JsonThing = null;

class TurboAPI {
getInfo() {
return {
id: 'TurboAPI',
name: 'TurboAPI',
menuIconURI: 'https://files.catbox.moe/4eb196.png',
color1: '#FF4C4C',
id: "TurboAPI",
name: "TurboAPI",
menuIconURI: "https://files.catbox.moe/4eb196.png",
color1: "#FF4C4C",
blocks: [
{
opcode: 'sendResponse',
opcode: "sendResponse",
blockType: Scratch.BlockType.COMMAND,
text: 'GET Request from [endpoint]',
text: "GET Request from [endpoint]",
arguments: {
endpoint: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Website Endpoint'
}
}
defaultValue: "Website Endpoint",
},
},
},
{
opcode: 'sendPost',
opcode: "sendPost",
blockType: Scratch.BlockType.COMMAND,
text: 'Send a Post Request to [endpoint] with [arg]',
text: "Send a Post Request to [endpoint] with [arg]",
arguments: {
endpoint: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Website Endpoint'
defaultValue: "Website Endpoint",
},
arg: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Arguments'
}
}
defaultValue: "Arguments",
},
},
},
{
opcode: 'responseAPI',
opcode: "responseAPI",
blockType: Scratch.BlockType.REPORTER,
text: 'API Response',
blockIconURI: 'https://freeiconshop.com/wp-content/uploads/edd/download-flat.png'
}
]
}
}

sendResponse(args) {
APIResponse = Scratch.fetch(args.endpoint).then((r) => r.text()).catch(() => "");
}

sendPost(args) {
APIResponse = Scratch.fetch(args.endpoint, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
text: "API Response",
blockIconURI:
"https://freeiconshop.com/wp-content/uploads/edd/download-flat.png",
},
body: args.arg
}).then((r) => r.text()).catch(() => "");
}

responseAPI() {
return APIResponse;
}
],
};
}

sendResponse(args) {
APIResponse = Scratch.fetch(args.endpoint)
.then((r) => r.text())
.catch(() => "");
}

sendPost(args) {
APIResponse = Scratch.fetch(args.endpoint, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: args.arg,
})
.then((r) => r.text())
.catch(() => "");
}

responseAPI() {
return APIResponse;
}
}

Scratch.extensions.register(new TurboAPI());
})(Scratch);

0 comments on commit d18b427

Please sign in to comment.