Last active
July 27, 2023 17:49
-
-
Save jamiew/96adcc968210a78bf7ca9e0c2d299dbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ~*~*~*~ glif.xyz ~*~*~*~ | |
// make sure to edit in your API token instead of "TOP_SECRET_GLIF_API_TOKEN" | |
// ~*~*~*~~*~*~*~~*~*~*~~*~ | |
const apiUrl = 'https://alpha.glif.xyz/api/graph-run'; | |
const apiToken = 'TOP_SECRET_GLIF_API_TOKEN'; | |
export default { | |
async fetch(request, env) { | |
if (request.method === "OPTIONS") { | |
return handleOptions(request) | |
} else if (request.method === "POST") { | |
return handlePost(request) | |
} else { | |
return new Response("nothing to see here", { | |
status: 405, | |
statusText: "Method Not Allowed", | |
}) | |
} | |
} | |
} | |
const corsHeaders = { | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Methods": "POST, OPTIONS", | |
"Access-Control-Allow-Headers": "Content-Type", | |
} | |
function handleOptions(request) { | |
if (request.headers.get("Origin") !== null && | |
request.headers.get("Access-Control-Request-Method") !== null && | |
request.headers.get("Access-Control-Request-Headers") !== null) { | |
// Handle CORS pre-flight request. | |
return new Response(null, { | |
headers: corsHeaders | |
}) | |
} else { | |
// Handle standard OPTIONS request. | |
return new Response(null, { | |
headers: { | |
"Allow": "POST, OPTIONS", | |
} | |
}) | |
} | |
} | |
async function handlePost(request) { | |
const json = await request.json(); | |
const id = json.id; | |
const input = json.input; | |
if(!id) throw new Error("missing id"); | |
console.log("params", { id, input }); | |
const result = await runGlif(id, input); | |
console.log("result", result); | |
const responseHeaders = { | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Headers': '*', | |
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', | |
'Access-Control-Allow-Origin': '*', | |
}; | |
return new Response( | |
JSON.stringify({ input, ...result }), { | |
headers: { | |
"Content-Type": "application/json", | |
...corsHeaders, | |
} | |
} | |
) | |
} | |
function runGlif(id, input) { | |
const data = { | |
id, | |
input | |
}; | |
console.log("runGlif", { id, input }); | |
return fetch(apiUrl, { | |
method: 'POST', | |
headers: { | |
'Authorization': `Bearer ${apiToken}`, | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(data) | |
}) | |
.then(response => response.text()) | |
.then(data => { | |
// console.log("data", data) | |
const entries = data.split("\n").filter((output) => { | |
return output !== ""; | |
}); | |
const lastEntry = entries[entries.length - 1]; | |
console.log("lastEntry", lastEntry); | |
return { output: JSON.parse(lastEntry)?.output }; | |
}) | |
.catch(error => { | |
console.error(error); | |
return { output: null, error } | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment