@winxs/wind 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Winxs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # ๐ŸŒฌ๏ธ Wind โ€” @winxs/wind
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@winxs/wind)](https://www.npmjs.com/package/@winxs/wind)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@winxs/wind)](https://www.npmjs.com/package/@winxs/wind)
5
+ [![license](https://img.shields.io/npm/l/@winxs/wind)](LICENSE)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-ready-blue)](https://www.typescriptlang.org/)
7
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/@winxs/wind)](https://bundlephobia.com/package/@winxs/wind)
8
+
9
+ **Modern HTTP orchestration client for JavaScript & TypeScript**
10
+
11
+ > Axios helps you make requests.
12
+ > **Wind helps you manage flows.**
13
+
14
+ **Wind** is a modern HTTP orchestration client for JavaScript and TypeScript.
15
+
16
+ > Axios helps you make requests.
17
+ > **Wind helps you manage flows.**
18
+
19
+ Wind is built for **real-world APIs** โ€” pagination, retries, batching, circuit breakers, and failure-safe third-party integrations.
20
+
21
+ ---
22
+
23
+ ## โœจ Why Wind?
24
+
25
+ Most HTTP clients stop at `request โ†’ response`.
26
+
27
+ In real systems you also need:
28
+
29
+ - Pagination without writing loops
30
+ - Safe retries
31
+ - Partial-failure batch calls
32
+ - Protection against unstable third-party APIs
33
+ - Worker & SSR-friendly (no global state, isolated clients)
34
+ > โš ๏ธ For SSR or workers, always create a new client using `wind()` or `windClient`.
35
+
36
+
37
+ **Wind provides these as first-class primitives.**
38
+
39
+ ---
40
+
41
+ ## ๐Ÿš€ Features
42
+
43
+ - โšก **Simple API** (Axios-style defaults)
44
+ - ๐Ÿ” **Built-in retry support**
45
+ - ๐Ÿ”Œ **Circuit breaker** for failing APIs
46
+ - ๐Ÿ“„ **Pagination as async iterators**
47
+ - ๐Ÿ“ฆ **Batch requests with partial failures**
48
+ - ๐Ÿงต **Worker & SSR safe** (no global mutation)
49
+ - ๐Ÿชถ **Lightweight & dependency-minimal**
50
+
51
+ ### ๐ŸŒ Runtime Environments
52
+
53
+ Wind is designed to run in:
54
+
55
+ - Browsers
56
+ - Node.js (18+)
57
+ - Workers / Edge runtimes
58
+
59
+ > Wind does not rely on global mutable state,
60
+ making it safe for concurrent and isolated environments.
61
+
62
+ ---
63
+
64
+ ## ๐Ÿ“ฆ Installation
65
+
66
+ ```bash
67
+ npm install @winxs/wind
68
+ ```
69
+ ## ๐Ÿงฉ Usage
70
+ ### 1๏ธโƒฃ Quick (Axios-style)
71
+ ```ts
72
+
73
+ import wind from "@winxs/wind";
74
+ const users = await wind.get("/users");
75
+ The default wind client is shared.
76
+ For production, workers, or multiple APIs โ€” prefer the factory or class.
77
+ ```
78
+
79
+ ### 2๏ธโƒฃ Recommended: Factory API
80
+ ```ts
81
+ import { wind } from "@winxs/wind";
82
+
83
+ const api = wind({
84
+ baseURL: "https://api.example.com",
85
+ });
86
+
87
+ const users = await api.get("/users");
88
+ ```
89
+ ### 3๏ธโƒฃ Advanced: Isolated Client
90
+ ```ts
91
+ import { windClient } from "@winxs/wind";
92
+
93
+ const github = new windClient("https://api.github.com");
94
+
95
+ const repos = await github.get("/users/octocat/repos");
96
+ ```
97
+ ### ๐Ÿ” Pagination (No Loops)
98
+ #### โŒ Traditional approach
99
+ ```ts
100
+ let page = 1;
101
+ while (true) {
102
+ const res = await fetch(`/users?page=${page}`);
103
+ if (!res.length) break;
104
+ page++;
105
+ }
106
+ ```
107
+ ### โœ… Wind way
108
+ ```ts
109
+ for await (const page of api.paginate("/users")) {
110
+ console.log(page);
111
+ }
112
+ ```
113
+ * Lazy
114
+ * Memory-safe
115
+ * Failure-aware
116
+
117
+ ### ๐Ÿ“ฆ Batch Requests (Promise.all++)
118
+ #### โŒ Traditional
119
+ ```ts
120
+ await Promise.all([
121
+ fetch("/a"),
122
+ fetch("/b"),
123
+ ]);
124
+ ```
125
+ #### โœ… Wind
126
+ ```ts
127
+ const { results, errors } = await api.batch(
128
+ [
129
+ () => api.get("/a"),
130
+ () => api.get("/b"),
131
+ ],
132
+ { concurrency: 2 }
133
+ );
134
+ ```
135
+ * Controlled concurrency
136
+ * Partial success support
137
+ * No global failures
138
+
139
+ ### ๐Ÿ”Œ Circuit Breaker
140
+ 1. Wind protects your system from unstable APIs.
141
+ 2. Trips on network failures
142
+ 3. Trips on 5xx responses
143
+ 4. Trips on rate-limits (429)
144
+ 5. Ignores 4xx & validation errors
145
+
146
+ ```ts
147
+ await api.get("/third-party"); // auto-protected
148
+ ```
149
+ * When the circuit is open, requests fail fast instead of cascading failures.
150
+
151
+ ### ๐Ÿ” Retry Support
152
+ ```ts
153
+ await api.get("/unstable", {
154
+ retry: {
155
+ attempts: 3,
156
+ },
157
+ });
158
+ ```
159
+ * Retry happens before circuit breaker evaluation.
160
+
161
+ ### ๐Ÿ”„ Axios โ†’ Wind Migration
162
+ #### Axios
163
+ ```ts
164
+ import axios from "axios";
165
+ axios.get("/users");
166
+ ```
167
+ #### Wind
168
+ ```ts
169
+ import wind from "@winxs/wind";
170
+ wind.get("/users");
171
+ ```
172
+ #### Axios Instance
173
+ ```ts
174
+ const api = axios.create({ baseURL });
175
+ ```
176
+ #### Wind Factory
177
+ ```ts
178
+ const api = wind({ baseURL });
179
+ ```
180
+ #### Axios Pagination
181
+ ```ts
182
+ // manual looping
183
+ ```
184
+ #### Wind Pagination
185
+ ```ts
186
+ for await (const page of api.paginate("/users")) {}
187
+ ```
@@ -0,0 +1,55 @@
1
+ type ErrorType = "NETWORK" | "TIMEOUT" | "RATE_LIMIT" | "SERVER" | "CLIENT" | "SCHEMA" | "UNKNOWN";
2
+
3
+ declare class HttpError extends Error {
4
+ type: ErrorType;
5
+ status?: number;
6
+ response?: any;
7
+ constructor(message: string, type: ErrorType, status?: number, response?: any);
8
+ }
9
+
10
+ interface RetryOptions {
11
+ attemps: number;
12
+ backOffMs?: number;
13
+ retryOn?: ErrorType[];
14
+ }
15
+
16
+ interface RequestOptions {
17
+ method?: string;
18
+ headers?: Record<string, string>;
19
+ body?: any;
20
+ timeoutMS?: number;
21
+ retry?: RetryOptions;
22
+ }
23
+
24
+ interface PaginationConfig {
25
+ pageParam?: string;
26
+ startPage?: number;
27
+ stopOnEmpty?: boolean;
28
+ options?: RequestOptions;
29
+ }
30
+
31
+ interface BatchOptions {
32
+ concurrency?: number;
33
+ failFast?: boolean;
34
+ }
35
+
36
+ declare class windClient {
37
+ private baseURL;
38
+ private breaker;
39
+ constructor(baseURL?: string);
40
+ request<T>(path: string, options?: RequestOptions): Promise<T>;
41
+ get<T>(path: string, options?: RequestOptions): Promise<T>;
42
+ post<T>(path: string, body: any, options?: RequestOptions): Promise<T>;
43
+ paginate<T>(path: string, config?: PaginationConfig): AsyncGenerator<T, any, any>;
44
+ batch<T>(tasks: (() => Promise<T>)[], options?: BatchOptions): Promise<{
45
+ results: T[];
46
+ errors: HttpError[];
47
+ }>;
48
+ private shouldTripBreaker;
49
+ }
50
+ declare function wind(config?: {
51
+ baseURL?: string;
52
+ }): windClient;
53
+ declare const defaultWind: windClient;
54
+
55
+ export { defaultWind as default, wind, windClient };
package/dist/index.js ADDED
@@ -0,0 +1,186 @@
1
+ // src/Utility/HttpError.ts
2
+ var HttpError = class extends Error {
3
+ constructor(message, type, status, response) {
4
+ super(message);
5
+ this.type = type;
6
+ this.status = status;
7
+ this.response = response;
8
+ }
9
+ };
10
+ function classifyError(err) {
11
+ if (err instanceof HttpError) return err;
12
+ if (err.name === "AbortError") return new HttpError("Request Timed Out", "TIMEOUT");
13
+ if (err.status) {
14
+ if (err.status === 429) return new HttpError("Rate limited", "RATE_LIMIT", 429);
15
+ if (err.status >= 500) return new HttpError("Server error", "SERVER", err.status);
16
+ if (err.status >= 400) return new HttpError("Client error", "CLIENT", err.status);
17
+ }
18
+ return new HttpError(err.message || "Unknown error", "UNKNOWN");
19
+ }
20
+
21
+ // src/Utility/coreRequest.ts
22
+ async function coreRequest(url, options) {
23
+ const controller = new AbortController();
24
+ if (options.timeoutMS) {
25
+ setTimeout(() => controller.abort(), options.timeoutMS);
26
+ }
27
+ const res = await fetch(url, {
28
+ method: options.method || "GET",
29
+ headers: {
30
+ ...options.headers
31
+ },
32
+ body: options.body ? JSON.stringify(options.body) : void 0,
33
+ signal: controller.signal
34
+ });
35
+ let data = null;
36
+ let text = null;
37
+ try {
38
+ text = await res.text();
39
+ data = text ? JSON.parse(text) : null;
40
+ } catch {
41
+ throw new HttpError("Invalid JSON Response", "SCHEMA", res.status, text);
42
+ }
43
+ if (!res.ok) {
44
+ throw new HttpError("HTTP Error", "CLIENT", res.status, data);
45
+ }
46
+ return data;
47
+ }
48
+
49
+ // src/Utility/Retry.ts
50
+ async function withRetry(fn, options) {
51
+ const {
52
+ attemps,
53
+ backOffMs = 300,
54
+ //5 mins
55
+ retryOn = ["NETWORK", "TIMEOUT", "RATE_LIMIT", "SERVER"]
56
+ } = options;
57
+ let lastError;
58
+ for (let i = 0; i < attemps; i++) {
59
+ try {
60
+ return await fn();
61
+ } catch (err) {
62
+ let classified = classifyError(err);
63
+ lastError = classified;
64
+ if (!retryOn.includes(classified.type)) throw classified;
65
+ await new Promise((r) => setTimeout(r, backOffMs * (i + 1)));
66
+ }
67
+ }
68
+ throw lastError;
69
+ }
70
+
71
+ // src/Utility/CircuitBreaker.ts
72
+ var CircuitBreaker = class {
73
+ constructor(threshold = 5, timeoutMs = 1e4) {
74
+ this.threshold = threshold;
75
+ this.timeoutMs = timeoutMs;
76
+ this.failures = 0;
77
+ this.lastFailure = 0;
78
+ }
79
+ canRequest() {
80
+ if (this.failures < this.threshold) return true;
81
+ return Date.now() - this.lastFailure > this.timeoutMs;
82
+ }
83
+ success() {
84
+ this.failures = 0;
85
+ }
86
+ failure() {
87
+ this.failures++;
88
+ this.lastFailure = Date.now();
89
+ }
90
+ };
91
+
92
+ // src/Helper/Pagination.ts
93
+ async function* paginate(fetcher, path, config = {}) {
94
+ const pageParam = config.pageParam ?? "page";
95
+ let page = config.startPage ?? 1;
96
+ while (true) {
97
+ const data = await fetcher(
98
+ `${path}?${pageParam}=${page}`,
99
+ config.options
100
+ );
101
+ if (config.stopOnEmpty !== false && (!data || Array.isArray(data) && data.length === 0)) {
102
+ break;
103
+ }
104
+ yield data;
105
+ page++;
106
+ }
107
+ }
108
+
109
+ // src/Helper/Batch.ts
110
+ async function batch(tasks, options = {}) {
111
+ const concurrency = options.concurrency ?? tasks.length;
112
+ const failFast = options.failFast ?? false;
113
+ const results = [];
114
+ const errors = [];
115
+ let index = 0;
116
+ async function worker() {
117
+ while (index < tasks.length) {
118
+ const i = index++;
119
+ try {
120
+ results[i] = await tasks[i]();
121
+ } catch (err) {
122
+ errors[i] = err;
123
+ if (failFast) throw err;
124
+ }
125
+ }
126
+ }
127
+ await Promise.all(
128
+ Array.from({ length: concurrency }, worker)
129
+ );
130
+ return { results, errors };
131
+ }
132
+
133
+ // src/index.ts
134
+ var windClient = class {
135
+ constructor(baseURL = "") {
136
+ this.baseURL = baseURL;
137
+ this.breaker = new CircuitBreaker();
138
+ }
139
+ async request(path, options = {}) {
140
+ if (!this.breaker.canRequest()) {
141
+ throw new Error("Circuit breaker is open");
142
+ }
143
+ const exec = async () => {
144
+ return coreRequest(this.baseURL + path, options);
145
+ };
146
+ try {
147
+ const result = options.retry ? await withRetry(exec, options.retry) : await exec();
148
+ this.breaker.success();
149
+ return result;
150
+ } catch (err) {
151
+ if (this.shouldTripBreaker(err)) {
152
+ this.breaker.failure();
153
+ }
154
+ throw err;
155
+ }
156
+ }
157
+ get(path, options) {
158
+ return this.request(path, { ...options, method: "GET" });
159
+ }
160
+ post(path, body, options) {
161
+ return this.request(path, { ...options, method: "POST", body });
162
+ }
163
+ paginate(path, config) {
164
+ return paginate(this.get.bind(this), path, config);
165
+ }
166
+ batch(tasks, options) {
167
+ return batch(tasks, options);
168
+ }
169
+ shouldTripBreaker(err) {
170
+ const status = err?.status;
171
+ if (!status) return true;
172
+ if (status >= 500) return true;
173
+ if (status === 429) return true;
174
+ return false;
175
+ }
176
+ };
177
+ function wind(config = {}) {
178
+ return new windClient(config.baseURL ?? "");
179
+ }
180
+ var defaultWind = new windClient();
181
+ var index_default = defaultWind;
182
+ export {
183
+ index_default as default,
184
+ wind,
185
+ windClient
186
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@winxs/wind",
3
+ "version": "0.1.0",
4
+ "description": "Modern HTTP orchestration client for pagination, retries, batching, and failure-safe APIs",
5
+ "license": "MIT",
6
+ "author": "Winxs",
7
+ "type": "module",
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsup src/index.ts --format esm --dts",
23
+ "dev": "tsup src/index.ts --watch",
24
+ "prepublishOnly": "npm run build"
25
+ },
26
+ "keywords": [
27
+ "http",
28
+ "fetch",
29
+ "axios",
30
+ "client",
31
+ "pagination",
32
+ "retry",
33
+ "batch",
34
+ "workers"
35
+ ],
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://github.com/winxs/wind"
39
+ },
40
+ "devDependencies": {
41
+ "tsup": "^8.5.1",
42
+ "typescript": "^5.9.3"
43
+ }
44
+ }