@osskit/fetch-enhancers
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -9,3 +9,14 @@ "use strict"; | ||
const responseText = await response.text(); | ||
throw new types_1.FetchError({ message: responseText ?? 'fetch error', url: JSON.stringify(url), status: response.status }); | ||
let responseJson; | ||
try { | ||
responseJson = JSON.parse(responseText); | ||
} | ||
catch { | ||
} | ||
throw new types_1.FetchError({ | ||
message: responseText ?? 'fetch error', | ||
url: JSON.stringify(url), | ||
status: response.status, | ||
data: responseJson, | ||
}); | ||
} | ||
@@ -12,0 +23,0 @@ return response; |
import type { RequestInfo, RequestInit, Response, Request } from 'node-fetch'; | ||
export declare type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
export interface FetchErrorProperties { | ||
export interface FetchErrorProperties<T> { | ||
message: string; | ||
url: string; | ||
status?: number; | ||
data?: Record<string, string>; | ||
data?: T; | ||
} | ||
export declare class FetchError extends Error { | ||
export declare class FetchError<T = Record<string, string>> extends Error { | ||
url: string; | ||
status?: number; | ||
data?: Record<string, string>; | ||
constructor({ message, url, status, data }: FetchErrorProperties); | ||
data?: T; | ||
constructor({ message, url, status, data }: FetchErrorProperties<T>); | ||
} | ||
@@ -15,0 +15,0 @@ export declare type EnhancedFetchRequestInit<T> = RequestInit & { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const http_1 = require("http"); | ||
const node_fetch_1 = __importStar(require("node-fetch")); | ||
const node_fetch_1 = __importDefault(require("node-fetch")); | ||
const src_1 = require("../../src"); | ||
const throwingFetch = (0, src_1.withThrow)(node_fetch_1.default); | ||
test('throws error when fetch fails ', async () => { | ||
const throwingFetch = (0, src_1.withThrow)(node_fetch_1.default); | ||
const server = (0, http_1.createServer)((_, res) => { | ||
res.writeHead(500); | ||
res.end(); | ||
const serverError = { type: 'mySpecialError' }; | ||
res.writeHead(400, { | ||
'Content-Type': 'application/json', | ||
}); | ||
res.end(JSON.stringify(serverError)); | ||
}); | ||
@@ -39,3 +26,4 @@ return new Promise((resolve, reject) => { | ||
catch (err) { | ||
expect(err instanceof node_fetch_1.FetchError).toBeTruthy(); | ||
expect(err).toBeInstanceOf(src_1.FetchError); | ||
expect(err.data?.type).toBe('mySpecialError'); | ||
resolve(); | ||
@@ -42,0 +30,0 @@ } |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const http_1 = require("http"); | ||
const node_fetch_1 = __importStar(require("node-fetch")); | ||
const node_fetch_1 = __importDefault(require("node-fetch")); | ||
const src_1 = require("../../src"); | ||
const timeoutFetch = (0, src_1.withTimeout)(node_fetch_1.default, { requestTimeoutMs: 100 }); | ||
test('single request configuration - times out when server does not respond in time ', async () => { | ||
const timeoutFetch = (0, src_1.withTimeout)(node_fetch_1.default, { requestTimeoutMs: 100 }); | ||
const server = (0, http_1.createServer)((_, res) => { | ||
@@ -41,3 +25,3 @@ setTimeout(() => { | ||
catch (err) { | ||
expect(err instanceof node_fetch_1.FetchError).toBeTruthy(); | ||
expect(err).toBeInstanceOf(src_1.FetchError); | ||
resolve(); | ||
@@ -53,3 +37,2 @@ } | ||
test('global configuration - times out when server does not respond in time ', async () => { | ||
const timeoutFetch = (0, src_1.withTimeout)(node_fetch_1.default, { requestTimeoutMs: 100 }); | ||
const server = (0, http_1.createServer)((_, res) => { | ||
@@ -69,3 +52,3 @@ setTimeout(() => { | ||
catch (err) { | ||
expect(err instanceof node_fetch_1.FetchError).toBeTruthy(); | ||
expect(err).toBeInstanceOf(src_1.FetchError); | ||
resolve(); | ||
@@ -72,0 +55,0 @@ } |
{ | ||
"name": "@osskit/fetch-enhancers", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "url": "https://github.com/osskit/fetch-enhancers" |
@@ -12,4 +12,16 @@ import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
const responseText = await response.text(); | ||
let responseJson; | ||
throw new FetchError({ message: responseText ?? 'fetch error', url: JSON.stringify(url), status: response.status}); | ||
try { | ||
responseJson = JSON.parse(responseText); | ||
} catch { | ||
// do nothing | ||
} | ||
throw new FetchError({ | ||
message: responseText ?? 'fetch error', | ||
url: JSON.stringify(url), | ||
status: response.status, | ||
data: responseJson, | ||
}); | ||
} | ||
@@ -16,0 +28,0 @@ |
@@ -5,15 +5,17 @@ import type { RequestInfo, RequestInit, Response, Request } from 'node-fetch'; | ||
export interface FetchErrorProperties { | ||
export interface FetchErrorProperties<T> { | ||
message: string; | ||
url: string; | ||
status?:number; | ||
data?: Record<string, string>; | ||
data?: T; | ||
} | ||
export class FetchError extends Error { | ||
export class FetchError<T = Record<string, string>> extends Error { | ||
url: string; | ||
status?: number; | ||
data?: Record<string, string>; | ||
constructor({message, url, status, data}: FetchErrorProperties) { | ||
data?: T; | ||
constructor({message, url, status, data}: FetchErrorProperties<T>) { | ||
super(message); | ||
@@ -20,0 +22,0 @@ this.url = url; |
import { createServer } from 'http'; | ||
import { AddressInfo } from 'net'; | ||
import fetch, { FetchError } from 'node-fetch'; | ||
import fetch from 'node-fetch'; | ||
import { withThrow } from '../../src'; | ||
import { withThrow, FetchError } from '../../src'; | ||
const throwingFetch = withThrow(fetch); | ||
test('throws error when fetch fails ', async () => { | ||
const throwingFetch = withThrow(fetch); | ||
const server = createServer((_, res) => { | ||
res.writeHead(500); | ||
res.end(); | ||
const serverError = { type: 'mySpecialError' }; | ||
res.writeHead(400, { | ||
'Content-Type': 'application/json', | ||
}); | ||
res.end(JSON.stringify(serverError)); | ||
}); | ||
@@ -20,4 +25,5 @@ | ||
reject(); | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
} catch (err: unknown) { | ||
expect(err).toBeInstanceOf(FetchError); | ||
expect((err as FetchError).data?.type).toBe('mySpecialError'); | ||
resolve(); | ||
@@ -31,1 +37,2 @@ } finally { | ||
}); | ||
import { createServer } from 'http'; | ||
import { AddressInfo } from 'net'; | ||
import fetch, { FetchError } from 'node-fetch'; | ||
import fetch from 'node-fetch'; | ||
import { withTimeout } from '../../src'; | ||
import { withTimeout, FetchError } from '../../src'; | ||
const timeoutFetch = withTimeout(fetch, { requestTimeoutMs: 100 }); | ||
test('single request configuration - times out when server does not respond in time ', async () => { | ||
const timeoutFetch = withTimeout(fetch, { requestTimeoutMs: 100 }); | ||
const server = createServer((_, res) => { | ||
@@ -23,3 +24,3 @@ setTimeout(() => { | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
expect(err).toBeInstanceOf(FetchError); | ||
resolve(); | ||
@@ -35,3 +36,2 @@ } finally { | ||
test('global configuration - times out when server does not respond in time ', async () => { | ||
const timeoutFetch = withTimeout(fetch, { requestTimeoutMs: 100 }); | ||
const server = createServer((_, res) => { | ||
@@ -51,3 +51,3 @@ setTimeout(() => { | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
expect(err).toBeInstanceOf(FetchError); | ||
resolve(); | ||
@@ -54,0 +54,0 @@ } finally { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60
44734
817