Last active
September 22, 2022 12:01
-
-
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
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
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