KnHttpJS Changelog ========== v4.0.0 (2026-09-24): ---------------------------- ### Breaking changes * New request methods: **get**, **post**, **put**, **patch** and **delete** with the **json**, **form**, **formData** or **body** option * **getText**, **del**, **postRaw**, **postForm**, **postFormData**, **postJson**, **putRaw**, **putForm**, **putFormData** and **putJson** removed * **request(url, method, opt)** replaced by **request(url, { method, ...opt })** (GET by default), the **requestType** option removed * **KnHttp.DEFAULTS** renamed **KnHttp.defaults** * **DEFAULTS.onError** replaced by **defaults.onUnhandledError**: called for the requests without onError callback and not awaited * **bearerAuthToken** option replaced by **authToken** (with **authHeader** and **authScheme**) * **res.httpCode** and **err.httpCode** renamed **res.status** and **err.status** * **KnHttp.validateStatus()** method removed (**defaults.validateStatus**) * Error codes are now strings: **CANCELED_ERROR** = 'canceled', **NETWORK_ERROR** = 'network', **HTTP_ERROR** = 'http', **UNKNOWN_ERROR** = 'unknown' (compare with the constants) * A timeout is now a **TIMEOUT_ERROR** ('timeout') and an invalid JSON a **PARSE_ERROR** ('parse'), both were an **UNKNOWN_ERROR** * **validateStatus** accepts all the 2xx HTTP status by default (only 200 before) * Empty responses (204...) and the JSON responses **null**, **false**, **0** and **""** are now a success (res.data is **null** for an empty response) * **err.data** is the parsed JSON of the error response when possible (text otherwise) * The **X-Requested-With** default header is no longer sent to the other origins (it forced a CORS preflight request) * **Response**, **Error** and **Deferred** classes renamed **KnHttpResponse**, **KnHttpError** and **KnHttpRequest** * **KnHttpError** now extends the native **Error** (err.message, err.name, err.stack, err.cause) * **req._xhr** replaced by the read-only **req.xhr** property * Falsy bodies other than **null** and **undefined** (**false**, **0**, **""**) are now sent * An option explicitly set to **undefined** now uses the default value * A top-level array given as a form or form data body now throws a **TypeError** * Types moved from **dist/kn-http.d.ts** to **dist/types/** (declared in the **types** and **exports** fields of package.json) * Requires Chrome / Edge 85+, Firefox 79+ or Safari 14.1+ ### New features * Promise support: the request object is awaitable (**await**, **then**, **catch**, **finally**), resolved with the response and rejected with a **KnHttpError** * The callbacks (**onProgress**, **onSuccess**, **onError**, **onEnd**) still work in any function (no async needed), alone or with **await** on the same request * The Promise is only created when used: no unhandled rejection with the callbacks only * **PATCH** requests * Instances: **KnHttp.create(config)** creates an instance with its own default options (merged with the parent instance) * **baseUrl** option for the relative URLs * **params** option: query string parameters (nested objects and arrays as key[subkey]) * Hooks: **beforeRequest** (can be async, updates the URL, method and headers), **afterResponse**, **beforeError** (can be async, returns true to retry the request, e.g. after refreshing an auth token) * Automatic retries: **retry** option (number or options: limit, methods, statusCodes, delay) for the network errors, the timeouts and the 408, 429, 500, 502, 503, 504 status of the idempotent methods * **signal** option: abort the request with an **AbortSignal** * Configurable auth: **authToken** (string or function evaluated at each request), **authHeader** (default Authorization) and **authScheme** (default Bearer, null for the token only) * **csrf** can be set in the defaults and can be a function * **parse** option: optional runtime validation / transformation of the response data (Zod, Valibot or any function, no dependency), its return type is the type of res.data * **KnHttp.TIMEOUT_ERROR**, **KnHttp.PARSE_ERROR**, **KnHttp.isKnHttpError(err)** and **KnHttp.isCanceled(err)** * **err.cause**: original error (JSON SyntaxError, exception of the parse option or of a hook) * **req.ended** property, callbacks can be removed by passing **null** * **Blob** values are supported in form data bodies * **responseType** accepts **arraybuffer** and **document** * The instances and the request objects are never made reactive by Vue: they can be stored in a ref, a reactive object or a Pinia store * Written in TypeScript with strong typing * Generic response type: `KnHttp.get(url)`, **res.data** typed from the **responseType** option (string, Blob, ArrayBuffer, Document) or from the **parse** option * Typed bodies (one body per request, no body for GET), options, hooks, errors and error codes * All the types are exported (**KnHttpRequest**, **KnHttpResponse**, **KnHttpError**, **KnHttpClient**, options, hooks...) ### Bug fixes * HTTP basic auth with non Latin-1 characters (UTF-8 credentials) no longer throws * The options object given to the request methods is no longer modified * The **Content-Type** header given in the **headers** option is no longer duplicated * An exception thrown in a callback no longer blocks the next callbacks (**onEnd** is always called), it is reported in the console * An empty response headers string no longer adds an empty key to **res.headers** * The form keys are URL encoded, the form serialization of objects not created with **{}** uses the right keys ### Build * Migrated from rollup to Vite 8 (library mode) and TypeScript 6 (strict mode) * **build**, **typecheck** and **dev** scripts * Fixed the declarations build (**tsc --outFile** removed in TypeScript 7) * Added the **exports**, **module**, **files** and **sideEffects** fields to package.json v3.0.2 (2025-04-02): ---------------------------- * Added optional global error callback v3.0.1 (2025-03-28): ---------------------------- * File upload by passing a File instance inside a formData req v3.0.0 (2025-03-02): ---------------------------- ### Breaking changes * onSuccess parameters replaced by one Response object * onError parameters replaced by one Error object v2.0.1 (2025-01-22): ---------------------------- * Published as npm package * Added types * Added wasSuccess param to the onEnd callback * Commented out all the console.log * Replaced gulp by rollup v1.0.8 (2024-05-05): ---------------------------- * Added HTTT PUT methods * Added a proper DocBlock for all methods v1.0.7 (2023-12-23): ---------------------------- * Use Object.assign to set default options v1.0.6 (2023-12-16): ---------------------------- * Replace the fake Deferred class by a true javascript class v1.0.5 (2023-09-22): ---------------------------- * Do not abort/cancel an already ended request v1.0.4 (2023-09-18): ---------------------------- * Add URL decoding of file name in download function v1.0.3 (2023-06-11): ---------------------------- * Fixed body serialization when an array contains objects, keys were not correctly set v1.0.2 (2023-06-09): ---------------------------- * Download: If no header attachment, remove the query string and hash at the end of the URL to get the correct file name v1.0.1 (2023-01-07): ---------------------------- * Generates an error if responseType JSON and no JSON in response v1.0.0 (2022-12-08): ---------------------------- * Initial release