Skip to content

Global Objects with data fetching #638

Closed Answered by jg-rp
AsadCSE asked this question in Q&A
Aug 12, 2023 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

Hi,

Both Shopify/Liquid and LiquidJS support drops (as in "drop of liquid"). Drops are objects that expose their methods as liquid properties. The special liquidMethodMissing() method is called if a method is not implemented for a given key.

import { Liquid, Drop } from "liquidjs";

class CollectionDrop extends Drop {
  #fetchCache = {};

  async liquidMethodMissing(key) {
    if (!(key in this.#fetchCache)) {
      this.#fetchCache[key] = await fetch(
        `https://haveibeenpwned.com/api/v3/breach/${key}`
      ).then((r) => r.json());
    }
    return this.#fetchCache[key];
  }
}

const engine = new Liquid();

const template = `\
{{ collection['Linkedin'].Name }}
{{ collection['Linke…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@AsadCSE
Comment options

Comment options

You must be logged in to vote
1 reply
@AsadCSE
Comment options

Answer selected by harttle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants