Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less "meta" demo of hooks #37

Open
floer32 opened this issue Dec 22, 2020 · 2 comments
Open

Less "meta" demo of hooks #37

floer32 opened this issue Dec 22, 2020 · 2 comments

Comments

@floer32
Copy link

floer32 commented Dec 22, 2020

I like how there's a demonstration of hooks, and some documentation. It'd be cool if that was the "advanced" section/post about hooks. It'd be great if there was a very simple hooks example -- some async data fetches. The docs (or the post, don't remember which off-top) -- show an example of an async data fetch, in a code-block. So we could put that, or a dummy of that, in the template.

I'd just do a PR, but I'm actually suggesting this because I'm blocked. I think I'm missing something[s] obvious, as I'm rusty on full-stack JS, and bundlers.

Thoughts? Suggestions?

@swyxio
Copy link
Contributor

swyxio commented Dec 22, 2020

haha, this is a good request indeed. for what its worth my site is in elderjs so perhaps a little less meta for you. https://github.com/sw-yx/swyxdotio/

@nickreese
Copy link
Contributor

@hangtwenty @sw-yx

I've added another bare bones data fetching example to the docs (below) and a link to the repo where I'm collecting some common ones (https://github.com/Elderjs/hooks/tree/main/hooks).

// ./src/hooks.js
const fetch = require('node-fetch');
module.exports = [
  {
    hook: 'bootstrap',
    name: 'addExternalData',
    description: 'Adds arbitrary external data to the data object available in all hooks and routes.',
    run: async ({ settings, data }) => {
      const externalData = await fetch('https://yourapi.here').then((res) => res.json());
      return {
        data: {
          ...data,
          externalData, // this data is now available in the `all` and `data` functions of your `/routes/routeName/route.js`.
        },
      };
    },
  },
};

Any other examples that would be useful?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants