diff options
author | JJ <nicetry@noemail.com> | 2025-03-19 14:56:26 +0000 |
---|---|---|
committer | JJ <nicetry@noemail.com> | 2025-03-19 14:56:26 +0000 |
commit | 16f52b7bef745097f7076dde76715db378b54343 (patch) | |
tree | cfcacda8adced2059dcc120d2bc2446d3c4f960a /content/snippets/11ty-fetch-put-request.md |
first commit
Diffstat (limited to 'content/snippets/11ty-fetch-put-request.md')
-rw-r--r-- | content/snippets/11ty-fetch-put-request.md | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/content/snippets/11ty-fetch-put-request.md b/content/snippets/11ty-fetch-put-request.md new file mode 100644 index 0000000..ac0a9e8 --- /dev/null +++ b/content/snippets/11ty-fetch-put-request.md @@ -0,0 +1,50 @@ +--- +title: Make a PUT request with @11ty/eleventy-fetch +description: Make a PUT request with @11ty/eleventy-fetch +tags: ["11ty"] +--- + +[11ty](https://www.11ty.dev/) fetch lets you cache resources for any remote asset at build time. It scored it in a cache in your project. + +The basic request format supports `GET` only: + +```js +const EleventyFetch = require("@11ty/eleventy-fetch"); + +module.exports = async function () { + let url = "https://api.github.com/repos/11ty/eleventy"; + + /* This returns a promise */ + return EleventyFetch(url, { + duration: "1d", // save for 1 day + type: "json", // we’ll parse JSON for you + }); +}; +``` + +You can update it to change it to a `PUT` request (there were no examples I could find of this anywhere). Here i'm making a PUT request to retrieve my username from [Hardcover](https://hardcover.app/) using a GraphQL query. + +```js +const url = "https://api.hardcover.app/v1/graphql"; + +return EleventyFetch(url, { + duration: "1d", + type: "json", + fetchOptions: { + method: "POST", + headers: { + "Content-Type": "application/json", + authorization: "<BEARER_TOKEN>", + }, + body: JSON.stringify({ + query: `{ + me { + username + } + } `, + }), + }, +}); +``` + +Running this at build time in `_data/username.json` will store the asset in a global `username` object. |