diff --git a/README.md b/README.md index 6ee29f7..1f01f1b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ import {fetch, CookieJar} from "node-fetch-cookies"; })(); ``` + ## Documentation ### async fetch(cookieJar, url[, options]) @@ -78,8 +79,8 @@ Alternatively this can also be a string, for example a serialized cookie receive In this case `fromURL` must be specified. - `fromURL` The url a cookie has been received from. -Returns `true` if the cookie has been added successfully. Returns `false` otherwise. -Will log a warning to console if a cookie fails to be parsed. +Returns `true` if the cookie has been added successfully. Returns `false` otherwise. +If the parser throws a [CookieParseError](#class-cookieparseerror) it will be caught and a warning will be printed to console. #### domains() Returns an iterator over all domains currently stored cookies for. @@ -141,6 +142,9 @@ Returns whether the cookie has expired or not. #### isValidForRequest(url) Returns whether the cookie is valid for a request to `url`. +### Class: CookieParseError +The Error that is thrown when the cookie parser located in the constructor of the [Cookie](#class-cookie) class is unable to parse the input. + ## 1.3.0 Breaking API Changes - `new CookieJar(flags, file, cookies)` has been changed to `new CookieJar(file, flags = "rw", cookies)`. diff --git a/src/errors.mjs b/src/errors.mjs index 549970e..fd36d17 100644 --- a/src/errors.mjs +++ b/src/errors.mjs @@ -3,10 +3,10 @@ export class CookieParseError extends Error { super(...args); this.name = "CookieParseError"; } -} +}; export function paramError(position, paramName, functionName, validTypes) { validTypes = [validTypes].flatMap(t => "\"" + t + "\""); validTypes = validTypes.slice(0, -1).join(", ") + (validTypes.length > 1 ? " or " : "") + validTypes.slice(-1); return new TypeError(`${position} parameter "${paramName}" passed to "${functionName}" is not of type ${validTypes}!`); -} +}; diff --git a/src/index.mjs b/src/index.mjs index 2bc2b98..40db751 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,7 +1,7 @@ import _fetch from "node-fetch"; import CookieJar from "./cookie-jar.mjs"; import Cookie from "./cookie.mjs"; -import { paramError } from "./errors.mjs" +import { paramError, CookieParseError } from "./errors.mjs" async function fetch(cookieJars, url, options) { let cookies = ""; @@ -46,4 +46,4 @@ async function fetch(cookieJars, url, options) { return result; } -export {fetch, CookieJar, Cookie}; +export {fetch, CookieJar, Cookie, CookieParseError};