Skip to content

Commit

Permalink
Prettier format
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 22, 2024
1 parent 111c400 commit 39cafcc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
11 changes: 8 additions & 3 deletions eleventy-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ function isFullUrl(url) {
}

function isAwaitable(maybeAwaitable) {
return (typeof maybeAwaitable === "object" && typeof maybeAwaitable.then === "function") || (maybeAwaitable.constructor.name === "AsyncFunction");
return (
(typeof maybeAwaitable === "object" && typeof maybeAwaitable.then === "function") ||
maybeAwaitable.constructor.name === "AsyncFunction"
);
}

async function save(source, options) {
if(!(isFullUrl(source) || isAwaitable(source))) {
if (!(isFullUrl(source) || isAwaitable(source))) {
return Promise.reject(new Error("Caching an already local asset is not yet supported."));
}

if (isAwaitable(source) && !options.formatUrlForDisplay) {
return Promise.reject(new Error("formatUrlForDisplay must be implemented, as a Promise has been provided."));
return Promise.reject(
new Error("formatUrlForDisplay must be implemented, as a Promise has been provided."),
);
}

let asset = new RemoteAssetCache(source, options.directory, options);
Expand Down
4 changes: 2 additions & 2 deletions src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class RemoteAssetCache extends AssetCache {

let body;
let type = optionsOverride.type || this.options.type;
if(typeof this.url === "object" && typeof this.url.then === "function") {
if (typeof this.url === "object" && typeof this.url.then === "function") {
body = await this.url;
} else if (typeof this.url === "function" && this.url.constructor.name === "AsyncFunction") {
body = await this.url();
Expand All @@ -95,7 +95,7 @@ class RemoteAssetCache extends AssetCache {

body = await this.getResponseValue(response, type);
}
if(!isDryRun) {
if (!isDryRun) {
await super.save(body, type);
}
return body;
Expand Down
29 changes: 14 additions & 15 deletions test/AssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,24 @@ test("Cache path should handle slashes without creating directories, issue #14",
let cache = new AssetCache("lksdjflk/jsdf", "/tmp/.cache");
let cachePath = normalizePath(cache.cachePath);


t.is(cachePath, "/tmp/.cache/eleventy-fetch-135797dbf5ab1187e5003c49162602");
});

test("Uses formatUrlForDisplay when caching a promise", async t => {
let promise = Promise.resolve();
let displayUrl = 'mock-display-url'
let asset = new AssetCache(promise, ".customcache", {
formatUrlForDisplay() {
return displayUrl;
}
});
let cachePath = normalizePath(asset.cachePath);
let jsonCachePath = normalizePath(asset.getCachedContentsPath("json"));
test("Uses formatUrlForDisplay when caching a promise", async (t) => {
let promise = Promise.resolve();
let displayUrl = "mock-display-url";
let asset = new AssetCache(promise, ".customcache", {
formatUrlForDisplay() {
return displayUrl;
},
});
let cachePath = normalizePath(asset.cachePath);
let jsonCachePath = normalizePath(asset.getCachedContentsPath("json"));

await asset.save({name: "Sophia Smith" }, "json");
await asset.save({ name: "Sophia Smith" }, "json");

t.truthy(fs.existsSync(jsonCachePath));
t.truthy(fs.existsSync(jsonCachePath));

fs.unlinkSync(cachePath);
fs.unlinkSync(jsonCachePath);
fs.unlinkSync(cachePath);
fs.unlinkSync(jsonCachePath);
});
41 changes: 22 additions & 19 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Util } = require("../");
const AssetCache = require("../src/AssetCache");
const RemoteAssetCache = require("../src/RemoteAssetCache");

test("getDurationMs", t => {
test("getDurationMs", (t) => {
let cache = new RemoteAssetCache("lksdjflkjsdf");
t.is(cache.getDurationMs("1s"), 1000);
t.is(cache.getDurationMs("1m"), 60 * 1000);
Expand Down Expand Up @@ -191,8 +191,11 @@ test("Error with `cause`", async (t) => {

try {
await asset.fetch();
} catch(e) {
t.is(e.message, `Bad response for https://example.com/207115/photos/243-0-1.jpg (404): Not Found`)
} catch (e) {
t.is(
e.message,
`Bad response for https://example.com/207115/photos/243-0-1.jpg (404): Not Found`,
);
t.truthy(e.cause);
}
});
Expand All @@ -201,10 +204,10 @@ test("supports promises that resolve", async (t) => {
let expected = { mockKey: "mockValue" };
let promise = Promise.resolve(expected);
let asset = new RemoteAssetCache(promise, undefined, {
type: "json",
formatUrlForDisplay() {
return "resolve-promise";
},
type: "json",
formatUrlForDisplay() {
return "resolve-promise";
},
});

let actual = await asset.fetch();
Expand All @@ -226,21 +229,21 @@ test("supports promises that reject", async (t) => {
try {
await asset.fetch();
} catch (e) {
t.is(e.message, expected);
t.is(e.message, expected);
t.is(e.cause, cause);
}
});

test("supports async functions that return data", async (t) => {
let expected = { mockKey: "mockValue" };
let asyncFunction = async () => {
return Promise.resolve(expected);
return Promise.resolve(expected);
};
let asset = new RemoteAssetCache(asyncFunction, undefined, {
type: "json",
formatUrlForDisplay() {
return "async-return";
},
type: "json",
formatUrlForDisplay() {
return "async-return";
},
});

let actual = await asset.fetch();
Expand All @@ -253,18 +256,18 @@ test("supports async functions that throw", async (t) => {
let cause = new Error("mock cause");
let asyncFunction = async () => {
throw new Error(expected, { cause });
};
};

let asset = new RemoteAssetCache(asyncFunction, undefined, {
formatUrlForDisplay() {
return "async-throws";
},
formatUrlForDisplay() {
return "async-throws";
},
});

try {
await asset.fetch();
await asset.fetch();
} catch (e) {
t.is(e.message, expected);
t.is(e.message, expected);
t.is(e.cause, cause);
}
});

0 comments on commit 39cafcc

Please sign in to comment.