├── .gitignore ├── LICENSE.md ├── README.md ├── example.png ├── index.js ├── package.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | *.log.* 4 | .DS_Store 5 | package-lock.json 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Gabriel Escabora 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Version](https://img.shields.io/npm/v/gatsby-plugin-cache-api.svg)](https://www.npmjs.com/package/gatsby-plugin-cache-api) 2 | [![Downloads Total](https://img.shields.io/npm/dt/gatsby-plugin-cache-api.svg)](https://www.npmjs.com/package/gatsby-plugin-cache-api) 3 | [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/escabora/gatsby-plugin-cache-api/blob/main/LICENSE.md) 4 | [![CodeFactor](https://www.codefactor.io/repository/github/escabora/gatsby-plugin-cache-api/badge)](https://www.codefactor.io/repository/github/escabora/gatsby-plugin-cache-api) 5 | 6 | # gatsby-plugin-cache-api 7 | 8 | This plugin is used to cache any API. 9 | 10 | ## Install 11 | 12 | `$ npm i gatsby-plugin-cache-api` 13 | 14 | or 15 | 16 | `$ yarn add gatsby-plugin-cache-api` 17 | 18 | ## How to use 19 | 20 | import the function `cacheApi` in your `gatsby-node.js`. 21 | 22 | ```javascript 23 | const cacheApi = require('gatsby-plugin-cache-api'); 24 | 25 | exports.onPreInit = async () => { 26 | const api = await fetch('https://dog.ceo/api/breeds/list/all'); 27 | const json = await api.json(); 28 | 29 | const defineCache = { 30 | pathOutput: 'js/json', 31 | nameOutPath: 'breeds.json', 32 | file: json.message, 33 | }; 34 | cacheApi(defineCache); 35 | }; 36 | ``` 37 | 38 | In your component do a fetch directly in your application 39 | 40 | ```javascript 41 | const MyComponent = () => { 42 | const [breeds, setBreeds] = useState([]); 43 | 44 | useEffect(()=> { 45 | const api = await fetch('./js/json/breeds.json'); //- cache 46 | const json = await api.json(); 47 | const parseBreeds = Object.keys(json.message) 48 | setBreeds(parseBreeds); 49 | 50 | }, []) 51 | 52 | return
{breeds.map((breed)=>

{breed}

)}
; 53 | }; 54 | 55 | export default MyComponent; 56 | ``` 57 | 58 | ## On build Time 59 | 60 | In the terminal you will see a success log with the path and name of the generated file 61 | 62 | ![Shell Example](/example.png) 63 | 64 | ## License 65 | 66 | The code is available under the [MIT License](LICENSE.md). 67 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/escabora/gatsby-plugin-cache-api/c07aac46d7d6a39a11e427bffcda22da99f81f4d/example.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fse = require('fs-extra'); 2 | 3 | /** 4 | * @param {string} pathOutput - String referring to the file creation path 5 | * @param {string} nameOutPath - String referring to the name and type of creation of the file 6 | * @param {Object} file - API object 7 | * @returns {Function} Returns asynchronous function to create the files 8 | **/ 9 | const cacheApi = async ({ pathOutput, nameOutPath, file }) => { 10 | try { 11 | const pathName = `./public/${pathOutput}/${nameOutPath}`; 12 | fse.outputFileSync(pathName, JSON.stringify(file)); 13 | console.log('\x1b[32msuccess\x1b[0m', `create file in ${pathName}`); 14 | } catch (err) { 15 | throw new Error(err); 16 | } 17 | }; 18 | 19 | module.exports = cacheApi; 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gatsby-plugin-cache-api", 3 | "description": "Plugin to cache any API with gatsby", 4 | "author": "Gabriel Escabora ", 5 | "version": "1.0.1", 6 | "repository": "git@github.com:escabora/gatsby-plugin-cache-api.git", 7 | "license": "MIT", 8 | "keywords": [ 9 | "cache-api", 10 | "api-gatsby", 11 | "gatsby", 12 | "plugin", 13 | "gatsby-plugin" 14 | ], 15 | "dependencies": { 16 | "fs-extra": "^11.1.1" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | fs-extra@^11.1.1: 6 | version "11.1.1" 7 | resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.1.1.tgz#da69f7c39f3b002378b0954bb6ae7efdc0876e2d" 8 | integrity sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ== 9 | dependencies: 10 | graceful-fs "^4.2.0" 11 | jsonfile "^6.0.1" 12 | universalify "^2.0.0" 13 | 14 | graceful-fs@^4.1.6, graceful-fs@^4.2.0: 15 | version "4.2.11" 16 | resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3" 17 | integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ== 18 | 19 | jsonfile@^6.0.1: 20 | version "6.1.0" 21 | resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae" 22 | integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ== 23 | dependencies: 24 | universalify "^2.0.0" 25 | optionalDependencies: 26 | graceful-fs "^4.1.6" 27 | 28 | universalify@^2.0.0: 29 | version "2.0.0" 30 | resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" 31 | integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ== 32 | --------------------------------------------------------------------------------