Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Cargo.toml

This file was deleted.

50 changes: 27 additions & 23 deletions bun.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "blink-http",
"dependencies": {},
"devDependencies": {
"@types/node": "^22.13.10",
"prettier": "^3.5.3",
"typescript": "^5.8.2"
}
}
},
"packages": {
"@types/node": ["@types/node@22.13.10", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-I6LPUvlRH+O6VRUqYOcMudhaIdUVWfsjnZavnsraHvpBwaEyMN29ry+0UVJhImYL16xsscu0aske3yA+uPOWfw=="],

"prettier": ["prettier@3.5.3", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-QQtaxnoDJeAkDvDKWCLiwIXkTgRhwYDEQCghU9Z6q03iyek/rxRh/2lC3HB7P8sWT2xC/y5JDctPLBIGzHKbhw=="],

"typescript": ["typescript@5.8.2", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ=="],

"undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="]
}
}
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "blink-http",
"dependencies": {
"xhr2": "^0.2.1",
},
"devDependencies": {
"@types/node": "^22.13.10",
"prettier": "^3.5.3",
"typescript": "^5.8.2",
},
},
},
"packages": {
"@types/node": ["@types/node@22.13.10", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-I6LPUvlRH+O6VRUqYOcMudhaIdUVWfsjnZavnsraHvpBwaEyMN29ry+0UVJhImYL16xsscu0aske3yA+uPOWfw=="],

"prettier": ["prettier@3.5.3", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-QQtaxnoDJeAkDvDKWCLiwIXkTgRhwYDEQCghU9Z6q03iyek/rxRh/2lC3HB7P8sWT2xC/y5JDctPLBIGzHKbhw=="],

"typescript": ["typescript@5.8.2", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ=="],

"undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="],

"xhr2": ["xhr2@0.2.1", "", {}, "sha512-sID0rrVCqkVNUn8t6xuv9+6FViXjUVXq8H5rWOH2rz9fDNQEd4g0EA2XlcEdJXRz5BMEn4O1pJFdT+z4YHhoWw=="],
}
}
113 changes: 57 additions & 56 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
{
"name": "blink-http",
"version": "1.1.1",
"description": "A tiny and standalone HTTP client based on XMLHttpRequest",
"main": "./distribution/index.js",
"exports": {
".": {
"import": "./distribution/index.js",
"require": "./distribution/index.js"
}
},
"types": "./distribution/index.d.ts",
"files": [
"distribution",
"LICENSE",
"README.md"
],
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"prepublishOnly": "tsc",
"format": "prettier --write .",
"format:check": "prettier --check .",
"test": "echo \"Error: no test specified\" && exit 1",
"wasm-pack": "wasm-pack build"
},
"keywords": [
"http",
"xmlhttprequest",
"client",
"tiny",
"standalone",
"blink",
"blink-http"
],
"author": {
"name": "Jonas F. Franke <@binary-blazer>",
"email": "me@binaryblazer.me",
"url": "https://binaryblazer.me"
},
"license": "BSD-3-Clause",
"homepage": "https://github.com/binary-blazer/blink-http#readme",
"bugs": {
"url": "https://github.com/binary-blazer/blink-http/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/binary-blazer/blink-http.git"
},
"devDependencies": {
"@types/node": "^22.13.10",
"prettier": "^3.5.3",
"typescript": "^5.8.2"
},
"dependencies": {}
}
{
"name": "blink-http",
"version": "1.1.1",
"description": "A tiny and standalone HTTP client based on XMLHttpRequest",
"main": "./distribution/index.js",
"exports": {
".": {
"import": "./distribution/index.js",
"require": "./distribution/index.js"
}
},
"types": "./distribution/index.d.ts",
"files": [
"distribution",
"LICENSE",
"README.md"
],
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"prepublishOnly": "tsc",
"format": "prettier --write .",
"format:check": "prettier --check .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"http",
"xmlhttprequest",
"client",
"tiny",
"standalone",
"blink",
"blink-http"
],
"author": {
"name": "Jonas F. Franke <@binary-blazer>",
"email": "me@binaryblazer.me",
"url": "https://binaryblazer.me"
},
"license": "BSD-3-Clause",
"homepage": "https://github.com/binary-blazer/blink-http#readme",
"bugs": {
"url": "https://github.com/binary-blazer/blink-http/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/binary-blazer/blink-http.git"
},
"devDependencies": {
"@types/node": "^22.13.10",
"prettier": "^3.5.3",
"typescript": "^5.8.2"
},
"dependencies": {
"xhr2": "^0.2.1"
}
}
55 changes: 39 additions & 16 deletions source/core/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @ts-expect-error: xhr2 has no types
import { XMLHttpRequest } from "xhr2";

import { Interceptor, ResponseInterceptor, BlinkResponse } from "../interfaces";
import {
BLINK_USER_AGENT,
DEFAULT_TIMEOUT,
DEFAULT_OPTIONS,
} from "../constants.js";
import { buildFinalUrl, applyInterceptors, createResponse } from "./utils.js";
import wasm from "../pkg";

class Client {
baseURL: string;
Expand Down Expand Up @@ -53,21 +55,42 @@ class Client {
applyInterceptors(this.interceptors.request, finalUrl, finalOptions);

return new Promise((resolve, reject) => {
wasm
.http_request(finalOptions.method || "GET", finalUrl, finalOptions)
.then((response: BlinkResponse) => {
for (const interceptor of this.interceptors.response) {
const modifiedResponse = interceptor(response);
if (modifiedResponse) return resolve(modifiedResponse);
}

if (!response.ok)
return reject(new Error(`HTTP error! Status: ${response.status}`));
resolve(response);
})
.catch((error: Error) => {
reject(error);
});
const xhr = new XMLHttpRequest();
xhr.open(finalOptions.method || "GET", finalUrl, true);
xhr.timeout = this.timeout;

if (finalOptions.headers) {
for (const [key, value] of Object.entries(finalOptions.headers)) {
xhr.setRequestHeader(key, value as string);
}
}

xhr.setRequestHeader("User-Agent", this.userAgent);

if (onProgress) {
xhr.onprogress = onProgress;
}

xhr.onload = async () => {
let response: BlinkResponse = createResponse(
xhr,
finalUrl,
this.userAgent,
);

for (const interceptor of this.interceptors.response) {
const modifiedResponse = await interceptor(response);
if (modifiedResponse) return resolve(modifiedResponse);
}

if (!response.ok)
return reject(new Error(`HTTP error! Status: ${response.status}`));
resolve(response);
};

xhr.onerror = () => reject(new Error("Network error"));
xhr.ontimeout = () => reject(new Error("Request timed out"));
xhr.send(finalOptions.body as Document | XMLHttpRequestBodyInit | null);
});
}

Expand Down
32 changes: 0 additions & 32 deletions src/lib.rs

This file was deleted.

11 changes: 0 additions & 11 deletions wasm-pack-plugin.config.js

This file was deleted.

29 changes: 0 additions & 29 deletions webpack.config.js

This file was deleted.