Skip to content

Instantly share code, notes, and snippets.

@danielberndt
Last active September 22, 2022 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielberndt/8b0e1e11a8900f5acdd4d4ca533b70fd to your computer and use it in GitHub Desktop.
Save danielberndt/8b0e1e11a8900f5acdd4d4ca533b70fd to your computer and use it in GitHub Desktop.
Codeck.io-Api: get info about card by it's three letter id
let subdomain = YOUR_SUBDOMAIN;
let CARD_ID='123'
// turns the string representation of a card id into a number.
// `111` will become 1, `15z` will become 140
let convertIdToNumber = (seq) => {
let startVal = 28 * 29 - 1;
let implicitZero= true;
let letters = "123456789acefghijkoqrsuvwxyz";
let length = letters.length;
let letterToIndex = Object.fromEntries(letters.split("").map((letter, index) => ([letter, index])));
let intVal = letterToIndex[seq[0]] || 0;
for (let i = 1; i < seq.length; i += 1) {
if (implicitZero) intVal += 1;
intVal *= length;
intVal += letterToIndex[seq[i]];
}
return intVal - startVal;
}
// define condition by which to query the cards. We want to load the cards which have the corresponding `accountSeq`.
let cardQuery = {accountSeq: convertIdToNumber(CARD_ID),};
// build up the nested query and define which models and fields we want to receive
let query = {
_root: [
{
account: [
{
[`cards(${JSON.stringify(cardQuery)})`]: [
"title",
"content",
{attachments: [
"title",
{file: ["url"]}
]}
],
},
],
},
],
};
// pass the query to the api
let data = await fetch(`https://api.codecks.io?query=${JSON.stringify(query)}&x-account=${subdomain}`, {credentials: "include"}).then(r => r.json())
console.log(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment