aly revised this gist . Go to revision
1 file changed, 28 insertions
xivicon.js(file created)
| @@ -0,0 +1,28 @@ | |||
| 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 | + | }; | |
Newer
Older