1.2.2 • Published 5 months ago
@evlmaistrenko/tools-fetch v1.2.2
@evlmaistrenko/tools-fetch
Utilities for FetchAPI .
Usage
npm i @evlmaistrenko/tools-fetchimport * as fetchTools from "@evlmaistrenko/tools-fetch";
// Check for error
try {
await fetch("<some-url>").then((response) =>
fetchTools.ResponseError.check(response),
);
} catch (error) {
if (!(error instanceof fetchTools.ResponseError)) throw error;
console.log(error.response.status, error.parsed?.message);
}Extending ResponseError
Assuming that your response body contains comma-separated list of errors.
import * as fetchTools from "@evlmaistrenko/tools-fetch";
class MyResponseError extends fetchTools.ResponseErrorBase<{
errors: string[];
}> {
async parse() {
const json = await this.response.json();
this.parsedValue = json.errors.split(", ");
}
}