Skip to content

Instantly share code, notes, and snippets.

@rayhanadev
Last active July 4, 2021 15:11
Show Gist options
  • Save rayhanadev/eabc51a5601ea5842a9da81f51e5e3b8 to your computer and use it in GitHub Desktop.
Save rayhanadev/eabc51a5601ea5842a9da81f51e5e3b8 to your computer and use it in GitHub Desktop.
ReplAPI.it GraphQL Function
import { lightfetch } from 'lightfetch-node';
// You will need your connect.sid cookie for authenticated requests
const { CONNECT_SID } = process.env;
const headers = {
'X-Requested-With': 'Your-Username',
Referrer: 'https://replit.com/',
};
/*
The body parameter should contain the query and stringified
variables. lightfetch handles stringifying the whole body so
your code is cleaner.
Example Body:
{
query: "query UserCycles($username: String!) {\n userByUsername(username: $username) {\n karma\n }\n}",
variables: JSON.stringify({ username: "RayhanADev" })
}
*/
export default async function runGraphQL(body, options = {}) {
if (options.authRequired === true) {
if (!CONNECT_SID) throw new Error('You are not logged in!');
// This is for security, you can uncomment this block if
// you're using a Repl that should be restricted to you.
/*--------------------------------------------------------------
if (!['RayhanADev'].includes(process.env.REPL_OWNER))
throw new Error('You are not allowed to use this Repl!');
--------------------------------------------------------------*/
headers.Cookie = 'connect.sid=' + CONNECT_SID;
body.clientVersion = '7561851';
body.hCaptchaSiteKey = '473079ba-e99f-4e25-a635-e9b661c7dd3e';
}
const info = await lightfetch('https://replit.com/graphql', {
body,
headers,
method: 'POST',
});
try {
const data = info.toJSON();
return data;
} catch (error) {
throw new Error('Could not parse response data.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment