@11ty/eleventy-fetch
Advanced tools
Comparing version 4.0.0 to 4.0.1
{ | ||
"name": "@11ty/eleventy-fetch", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Fetch and locally cache remote API calls and assets.", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -11,2 +11,3 @@ const fs = require("fs"); | ||
constructor(uniqueKey, cacheDirectory, options = {}) { | ||
this.uniqueKey = uniqueKey; | ||
this.hash = AssetCache.getHash(uniqueKey, options.hashLength); | ||
@@ -18,2 +19,10 @@ this.cacheDirectory = cacheDirectory || ".cache"; | ||
log(message) { | ||
if(this.options.verbose) { | ||
console.log(`[11ty/eleventy-fetch] ${message}`); | ||
} else { | ||
debug(message); | ||
} | ||
} | ||
// Defult hashLength also set in global options, duplicated here for tests | ||
@@ -65,3 +74,3 @@ static getHash(url, hashLength = 30) { | ||
// https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html | ||
// Bad: LAMBDA_TASK_ROOT is /var/task/ on AWS so we must use ELEVENTY_ROOT | ||
@@ -138,4 +147,8 @@ // When using ELEVENTY_ROOT, cacheDirectory must be relative | ||
} | ||
let contentPath = this.getCachedContentsPath(type); | ||
// the contents must exist before the cache metadata are saved below | ||
await fsp.writeFile(this.getCachedContentsPath(type), contents); | ||
await fsp.writeFile(contentPath, contents); | ||
debug(`Writing ${contentPath}`); | ||
@@ -152,2 +165,4 @@ let cache = this.cache; | ||
let contentPath = this.getCachedContentsPath(type); | ||
debug(`Fetching from cache ${contentPath}`); | ||
if(type === "json") { | ||
@@ -219,5 +234,7 @@ return require(contentPath); | ||
// promise | ||
this.log( `Using cached version of: ${this.uniqueKey}` ); | ||
return this.getCachedValue(); | ||
} | ||
this.log( `Saving ${this.uniqueKey} to ${this.cacheFilename}` ); | ||
await this.save(this.source, options.type); | ||
@@ -224,0 +241,0 @@ |
@@ -36,10 +36,2 @@ const fs = require("fs"); | ||
log(message) { | ||
if(this.options.verbose) { | ||
console.log(message); | ||
} else { | ||
debug(message); | ||
} | ||
} | ||
get url() { | ||
@@ -67,2 +59,3 @@ return this._url; | ||
if(super.isCacheValid(duration) ) { | ||
this.log( `Cache hit for ${this.displayUrl}` ); | ||
return super.getCachedValue(); | ||
@@ -73,3 +66,3 @@ } | ||
let isDryRun = optionsOverride.dryRun || this.options.dryRun; | ||
this.log( `[11ty/eleventy-fetch] ${isDryRun? "Fetching" : "Caching"}: ${this.displayUrl}` ); | ||
this.log( `${isDryRun? "Fetching" : "Cache miss for"} ${this.displayUrl}`); | ||
@@ -90,4 +83,4 @@ let fetchOptions = optionsOverride.fetchOptions || this.options.fetchOptions || {}; | ||
if(this.cachedObject) { | ||
this.log( `[11ty/eleventy-fetch] Error fetching ${this.displayUrl}. Message: ${e.message}`); | ||
this.log( `[11ty/eleventy-fetch] Failing gracefully with an expired cache entry.` ); | ||
this.log( `Error fetching ${this.displayUrl}. Message: ${e.message}`); | ||
this.log( `Failing gracefully with an expired cache entry.` ); | ||
return super.getCachedValue(); | ||
@@ -94,0 +87,0 @@ } else { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14847
351