@exceptionless/fetchclient
Advanced tools
Comparing version 0.22.0 to 0.23.0
@@ -140,3 +140,3 @@ import { Counter } from "./Counter.js"; | ||
* @param {string} url - The URL to send the request to. | ||
* @param {object | string} [body] - The JSON payload to send with the request. | ||
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request. | ||
* @param {RequestOptions} [options] - Additional options for the request. | ||
@@ -288,3 +288,3 @@ * @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data. | ||
response?.headers.get("Content-Type")?.startsWith("application/problem+json")) { | ||
ctx.response = await this.getJSONResponse(response); | ||
ctx.response = await this.getJSONResponse(response, ctx.options); | ||
} | ||
@@ -331,6 +331,17 @@ else { | ||
} | ||
async getJSONResponse(response) { | ||
async getJSONResponse(response, options) { | ||
let data = null; | ||
try { | ||
data = await response.json(); | ||
if (options.reviver) { | ||
const body = await response.text(); | ||
data = JSON.parse(body, options.reviver); | ||
} | ||
else if (options.shouldParseDates) { | ||
// TODO: Combine reviver and shouldParseDates into a single function | ||
const body = await response.text(); | ||
data = JSON.parse(body, this.parseDates); | ||
} | ||
else { | ||
data = await response.json(); | ||
} | ||
} | ||
@@ -352,2 +363,14 @@ catch { | ||
} | ||
parseDates(_key, value) { | ||
if (typeof value !== "string") { | ||
return value; | ||
} | ||
if (/^\d{4}-\d{2}-\d{2}/.test(value)) { | ||
const date = new Date(value); | ||
if (!isNaN(date.getTime())) { | ||
return date; | ||
} | ||
} | ||
return value; | ||
} | ||
problemToResponse(problem, url) { | ||
@@ -372,2 +395,4 @@ const headers = new Headers(); | ||
arrayBuffer: () => new Promise((resolve) => resolve(new ArrayBuffer(0))), | ||
// @ts-ignore: New in Deno 1.44 | ||
bytes: () => new Promise((resolve) => resolve(new Uint8Array())), | ||
blob: () => new Promise((resolve) => resolve(new Blob())), | ||
@@ -374,0 +399,0 @@ formData: () => new Promise((resolve) => resolve(new FormData())), |
{ | ||
"name": "@exceptionless/fetchclient", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"description": "A simple fetch client with middleware support for Deno and the browser.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -11,2 +11,3 @@ <!-- deno-fmt-ignore-file --> | ||
* Problem Details support | ||
* Option to parse dates in responses | ||
@@ -13,0 +14,0 @@ ## Install |
@@ -143,3 +143,3 @@ "use strict"; | ||
* @param {string} url - The URL to send the request to. | ||
* @param {object | string} [body] - The JSON payload to send with the request. | ||
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request. | ||
* @param {RequestOptions} [options] - Additional options for the request. | ||
@@ -291,3 +291,3 @@ * @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data. | ||
response?.headers.get("Content-Type")?.startsWith("application/problem+json")) { | ||
ctx.response = await this.getJSONResponse(response); | ||
ctx.response = await this.getJSONResponse(response, ctx.options); | ||
} | ||
@@ -334,6 +334,17 @@ else { | ||
} | ||
async getJSONResponse(response) { | ||
async getJSONResponse(response, options) { | ||
let data = null; | ||
try { | ||
data = await response.json(); | ||
if (options.reviver) { | ||
const body = await response.text(); | ||
data = JSON.parse(body, options.reviver); | ||
} | ||
else if (options.shouldParseDates) { | ||
// TODO: Combine reviver and shouldParseDates into a single function | ||
const body = await response.text(); | ||
data = JSON.parse(body, this.parseDates); | ||
} | ||
else { | ||
data = await response.json(); | ||
} | ||
} | ||
@@ -355,2 +366,14 @@ catch { | ||
} | ||
parseDates(_key, value) { | ||
if (typeof value !== "string") { | ||
return value; | ||
} | ||
if (/^\d{4}-\d{2}-\d{2}/.test(value)) { | ||
const date = new Date(value); | ||
if (!isNaN(date.getTime())) { | ||
return date; | ||
} | ||
} | ||
return value; | ||
} | ||
problemToResponse(problem, url) { | ||
@@ -375,2 +398,4 @@ const headers = new Headers(); | ||
arrayBuffer: () => new Promise((resolve) => resolve(new ArrayBuffer(0))), | ||
// @ts-ignore: New in Deno 1.44 | ||
bytes: () => new Promise((resolve) => resolve(new Uint8Array())), | ||
blob: () => new Promise((resolve) => resolve(new Blob())), | ||
@@ -377,0 +402,0 @@ formData: () => new Promise((resolve) => resolve(new FormData())), |
@@ -124,7 +124,7 @@ import { Counter } from "./Counter.js"; | ||
* @param {string} url - The URL to send the request to. | ||
* @param {object | string} [body] - The JSON payload to send with the request. | ||
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request. | ||
* @param {RequestOptions} [options] - Additional options for the request. | ||
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data. | ||
*/ | ||
postJSON<T>(url: string, body?: object | string, options?: RequestOptions): Promise<FetchClientResponse<T>>; | ||
postJSON<T>(url: string, body?: object | string | FormData, options?: RequestOptions): Promise<FetchClientResponse<T>>; | ||
/** | ||
@@ -178,2 +178,3 @@ * Sends a PUT request to the specified URL with the given body and options. | ||
private getJSONResponse; | ||
private parseDates; | ||
private problemToResponse; | ||
@@ -180,0 +181,0 @@ private buildUrl; |
@@ -31,2 +31,10 @@ import type { CacheKey } from "./FetchClientCache.js"; | ||
/** | ||
* Specifies whether the JSON parsing should convert strings that look like dates into Date instances. | ||
*/ | ||
shouldParseDates?: boolean; | ||
/** | ||
* Specifies a reviver function to use for JSON response parsing. | ||
*/ | ||
reviver?: (this: unknown, key: string, value: unknown) => unknown; | ||
/** | ||
* Any additional metadata to be used during the request and middleware. | ||
@@ -33,0 +41,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
177254
103
2411
140