-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds requestId option (to add your own unique id for cache/reuse/dedupe)
- Loading branch information
Showing
7 changed files
with
224 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class Sources { | ||
static isFullUrl(url) { | ||
try { | ||
if(url instanceof URL) { | ||
return true; | ||
} | ||
|
||
new URL(url); | ||
return true; | ||
} catch (e) { | ||
// invalid url OR already a local path | ||
return false; | ||
} | ||
} | ||
|
||
static isValidSource(source) { | ||
// String (url?) | ||
if(typeof source === "string") { | ||
return true; | ||
} | ||
if(this.isValidComplexSource(source)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
static isValidComplexSource(source) { | ||
// Async/sync Function | ||
if(typeof source === "function") { | ||
return true; | ||
} | ||
if(typeof source === "object") { | ||
// Raw promise | ||
if(typeof source.then === "function") { | ||
return true; | ||
} | ||
// anything string-able | ||
if(typeof source.toString === "function") { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static getInvalidSourceError(source, errorCause) { | ||
return new Error("Invalid source: must be a string, function, or Promise. If a function or Promise, you must provide a `toString()` method or an `options.requestId` unique key. Received: " + source, { cause: errorCause }); | ||
} | ||
} | ||
|
||
module.exports = Sources; |
Oops, something went wrong.