xivicon.js
· 958 B · JavaScript
Raw
export default {
async fetch(request, env, ctx) {
async function newResponse(code, msg, headers) {
return new Response(msg, {
status: code,
headers: headers,
});
}
// Only GET requests work with this proxy.
if (request.method !== "GET") return newResponse(405, `Method ${request.method} not allowed.`, {
Allow: "GET",
});
const { searchParams } = new URL(request.url);
let name = searchParams.get("name"), indexes = searchParams.get("indexes") || "Item";
if (!name) {
return newResponse(400, "Invalid request", {});
}
const url = `https://xivapi.com/search?indexes=${encodeURIComponent(indexes)}&string=${encodeURIComponent(name)}`;
const response = await fetch(url);
const data = await response.json();
if (data.Results.length < 1) {
return newResponse(404, "No results", {});
}
return fetch(`https://xivapi.com${data.Results[0].Icon}`);
},
};
| 1 | export default { |
| 2 | async fetch(request, env, ctx) { |
| 3 | async function newResponse(code, msg, headers) { |
| 4 | return new Response(msg, { |
| 5 | status: code, |
| 6 | headers: headers, |
| 7 | }); |
| 8 | } |
| 9 | // Only GET requests work with this proxy. |
| 10 | if (request.method !== "GET") return newResponse(405, `Method ${request.method} not allowed.`, { |
| 11 | Allow: "GET", |
| 12 | }); |
| 13 | |
| 14 | const { searchParams } = new URL(request.url); |
| 15 | let name = searchParams.get("name"), indexes = searchParams.get("indexes") || "Item"; |
| 16 | if (!name) { |
| 17 | return newResponse(400, "Invalid request", {}); |
| 18 | } |
| 19 | |
| 20 | const url = `https://xivapi.com/search?indexes=${encodeURIComponent(indexes)}&string=${encodeURIComponent(name)}`; |
| 21 | const response = await fetch(url); |
| 22 | const data = await response.json(); |
| 23 | if (data.Results.length < 1) { |
| 24 | return newResponse(404, "No results", {}); |
| 25 | } |
| 26 | return fetch(`https://xivapi.com${data.Results[0].Icon}`); |
| 27 | }, |
| 28 | }; |