|
| 1 | +import { world } from '@minecraft/server'; |
| 2 | +import { b64_to_utf8, utf8_to_b64 } from './encoding'; |
| 3 | +; |
| 4 | +export class RESTError extends Error { |
| 5 | +} |
| 6 | +; |
| 7 | +; |
| 8 | +globalThis.KEY = 'jayly-restful'; |
| 9 | +export class Table { |
| 10 | + /** |
| 11 | + * @internal |
| 12 | + */ |
| 13 | + toRawtext() { |
| 14 | + const rawData = JSON.stringify({ route: this.route, data: this.data }); |
| 15 | + return utf8_to_b64(rawData); |
| 16 | + } |
| 17 | + ; |
| 18 | + constructor(rawtext) { |
| 19 | + try { |
| 20 | + const { route, data } = JSON.parse(b64_to_utf8(rawtext)); |
| 21 | + this.route = route; |
| 22 | + this.data = data; |
| 23 | + } |
| 24 | + catch (error) { |
| 25 | + const { route, data } = JSON.parse(rawtext); |
| 26 | + this.route = route; |
| 27 | + this.data = data; |
| 28 | + } |
| 29 | + } |
| 30 | + ; |
| 31 | +} |
| 32 | +; |
| 33 | +export class Member { |
| 34 | + constructor(key, value) { |
| 35 | + this.key = key; |
| 36 | + this.value = value; |
| 37 | + } |
| 38 | + ; |
| 39 | +} |
| 40 | +; |
| 41 | +/** |
| 42 | + * Request methods available with Rest API |
| 43 | + */ |
| 44 | +export var RequestMethod; |
| 45 | +(function (RequestMethod) { |
| 46 | + /** |
| 47 | + * POST requests are commonly used to create a new resource that is a |
| 48 | + * subordinate of the specified route. |
| 49 | + */ |
| 50 | + RequestMethod["POST"] = "POST"; |
| 51 | + /** |
| 52 | + * GET requests are commonly used to retrieve information about a resource |
| 53 | + * at the specified route. |
| 54 | + */ |
| 55 | + RequestMethod["GET"] = "GET"; |
| 56 | + /** |
| 57 | + * PUT requests are commonly used to update a single resource that already |
| 58 | + * exists in a resource collection. |
| 59 | + */ |
| 60 | + RequestMethod["PUT"] = "PUT"; |
| 61 | + /** |
| 62 | + * PATCH requests are commonly used to update partial of an already |
| 63 | + * existed resource collection. |
| 64 | + */ |
| 65 | + RequestMethod["PATCH"] = "PATCH"; |
| 66 | + /** |
| 67 | + * POST requests are commonly used to remove an existing resource that is a |
| 68 | + * subordinate of the specified route. |
| 69 | + */ |
| 70 | + RequestMethod["DELETE"] = "DELETE"; |
| 71 | +})(RequestMethod || (RequestMethod = {})); |
| 72 | +; |
| 73 | +export class REST { |
| 74 | + constructor(id) { |
| 75 | + const regex = /^[a-z]+$/; |
| 76 | + if (!regex.test(id)) |
| 77 | + throw new RESTError('Invalid id.'); |
| 78 | + const objectiveId = id; |
| 79 | + this.scoreboard = world.scoreboard.getObjective(objectiveId) ?? world.scoreboard.addObjective(objectiveId, id); |
| 80 | + } |
| 81 | + ; |
| 82 | + request(route, options) { |
| 83 | + const participant = this.scoreboard.getParticipants().find((value) => new Table(value.displayName).route === route); |
| 84 | + // delete route |
| 85 | + if (options.method === RequestMethod.DELETE) { |
| 86 | + if (!participant) |
| 87 | + throw new RESTError('Route not found.'); |
| 88 | + const parsedOption = options; |
| 89 | + if (typeof parsedOption.key !== 'string') |
| 90 | + this.scoreboard.removeParticipant(participant); |
| 91 | + else { |
| 92 | + const table = new Table(participant.displayName); |
| 93 | + const memberIndex = table.data.findIndex((value) => value.key === parsedOption.key); |
| 94 | + if (memberIndex === -1) |
| 95 | + throw new RESTError('Member not found in route ' + route); |
| 96 | + // set value |
| 97 | + table.data.splice(memberIndex, 1); |
| 98 | + const encrypted = table.toRawtext(); |
| 99 | + // version increment |
| 100 | + const version = participant.getScore(this.scoreboard); |
| 101 | + (async () => { |
| 102 | + await world.getDimension('overworld') |
| 103 | + .runCommandAsync(`scoreboard players set ${JSON.stringify(encrypted)} ${JSON.stringify(this.scoreboard.id)} ${version + 1}`); |
| 104 | + }); |
| 105 | + } |
| 106 | + ; |
| 107 | + } |
| 108 | + // get member value from route |
| 109 | + else if (options.method === RequestMethod.GET) { |
| 110 | + const parsedOption = options; |
| 111 | + if (!participant) |
| 112 | + throw new RESTError('Route not found.'); |
| 113 | + const { data: members } = new Table(participant.displayName); |
| 114 | + const data = members.find((v) => v.key === parsedOption.key); |
| 115 | + if (!data) |
| 116 | + throw new RESTError('Member not found in route ' + route); |
| 117 | + return data.value; |
| 118 | + } |
| 119 | + // modify member value from route |
| 120 | + else if (options.method === RequestMethod.PATCH) { |
| 121 | + const parsedOption = options; |
| 122 | + if (!participant) |
| 123 | + throw new RESTError('Route not found.'); |
| 124 | + const table = new Table(participant.displayName); |
| 125 | + const member = table.data.find((value) => value.key === parsedOption.key); |
| 126 | + if (!member) |
| 127 | + throw new RESTError('Member not found in route ' + route); |
| 128 | + // set value |
| 129 | + member.value = parsedOption.value; |
| 130 | + const encrypted = table.toRawtext(); |
| 131 | + // version increment |
| 132 | + const version = participant.getScore(this.scoreboard); |
| 133 | + return new Promise((resolve, reject) => { |
| 134 | + world.getDimension('overworld') |
| 135 | + .runCommandAsync(`scoreboard players set ${JSON.stringify(encrypted)} ${JSON.stringify(this.scoreboard.id)} ${version + 1}`) |
| 136 | + .catch(reject); |
| 137 | + resolve(version); |
| 138 | + }); |
| 139 | + } |
| 140 | + // create a new route |
| 141 | + else if (options.method === RequestMethod.POST) { |
| 142 | + if (!!participant) |
| 143 | + throw new RESTError('Cannot create new route. Route ' + JSON.stringify(route) + ' already exists.'); |
| 144 | + const rawtext = JSON.stringify({ route, data: [] }); |
| 145 | + const table = new Table(rawtext.toString()); |
| 146 | + return new Promise((resolve, reject) => { |
| 147 | + world.getDimension('overworld') |
| 148 | + .runCommandAsync(`scoreboard players set ${JSON.stringify(table.toRawtext())} ${JSON.stringify(this.scoreboard.id)} 1`) |
| 149 | + .then(() => resolve()) |
| 150 | + .catch(reject); |
| 151 | + }); |
| 152 | + } |
| 153 | + // create a new member from route |
| 154 | + else if (options.method === RequestMethod.PUT) { |
| 155 | + const parsedOption = options; |
| 156 | + if (!participant) |
| 157 | + throw new RESTError('Route not found.'); |
| 158 | + const table = new Table(participant.displayName); |
| 159 | + if (table.data.findIndex((value) => value.key === parsedOption.key) > -1) |
| 160 | + throw new RESTError('Member exist in route ' + route); |
| 161 | + // set value |
| 162 | + const member = new Member(parsedOption.key, parsedOption.value); |
| 163 | + table.data.push(member); |
| 164 | + const encrypted = table.toRawtext(); |
| 165 | + // version increment |
| 166 | + return new Promise((resolve, reject) => { |
| 167 | + world.getDimension('overworld') |
| 168 | + .runCommandAsync(`scoreboard players set ${JSON.stringify(encrypted)} ${JSON.stringify(this.scoreboard.id)} 1`) |
| 169 | + .then(() => resolve()) |
| 170 | + .catch(reject); |
| 171 | + }); |
| 172 | + } |
| 173 | + else |
| 174 | + throw new RESTError('Request method ' + options.method + ' not acceptable.'); |
| 175 | + } |
| 176 | + ; |
| 177 | +} |
| 178 | +; |
0 commit comments