@telorun/http-client 0.1.1

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,17 @@
1
+ # SUSTAINABLE USE LICENSE (Fair-code)
2
+
3
+ Copyright (c) 2026 DiglyAI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, modify, and distribute the Software for any purpose—including commercial purposes—subject to the following conditions:
6
+
7
+ 1. ANTI-COMPETITION RESTRICTION: The Software may not be provided to third parties as a managed service, commercial SaaS (Software-as-a-Service), PaaS (Platform-as-a-Service), BaaS (Backend-as-a-Service), or similar offering where the primary value provided to the user is the functionality of the Software itself, without a separate commercial license from the copyright holder.
8
+
9
+ 2. PERMITTED COMMERCIAL USE: You are free to use the Software to build, host, and monetize your own commercial applications, products, and services, provided such use does not violate Clause 1.
10
+
11
+ 3. ATTRIBUTION: This copyright notice and license must be included in all copies or substantial portions of the Software.
12
+
13
+ 4. CONTRIBUTIONS: Contributions to the Software are welcome and encouraged. By contributing, you agree that your contributions may be incorporated into the Software and distributed under this license.
14
+
15
+ 5. DISCLAIMER: The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.
16
+
17
+ For commercial licensing, managed hosting exemptions, or enterprise inquiries, please contact DiglyAI.
@@ -0,0 +1,25 @@
1
+ import type { ResourceContext, ResourceInstance } from "@telorun/sdk";
2
+ interface HttpClientManifest {
3
+ baseUrl?: string;
4
+ headers?: Record<string, string>;
5
+ timeout?: number;
6
+ followRedirects?: boolean;
7
+ }
8
+ declare class HttpClientResource implements ResourceInstance {
9
+ private readonly manifest;
10
+ readonly metadata: {
11
+ name: string;
12
+ module: string;
13
+ [key: string]: any;
14
+ };
15
+ constructor(manifest: any);
16
+ snapshot(): {
17
+ baseUrl: any;
18
+ headers: any;
19
+ timeout: any;
20
+ followRedirects: any;
21
+ };
22
+ }
23
+ export declare function register(): void;
24
+ export declare function create(resource: HttpClientManifest, _ctx: ResourceContext): Promise<HttpClientResource>;
25
+ export {};
@@ -0,0 +1,20 @@
1
+ class HttpClientResource {
2
+ manifest;
3
+ metadata;
4
+ constructor(manifest) {
5
+ this.manifest = manifest;
6
+ this.metadata = manifest.metadata ?? {};
7
+ }
8
+ snapshot() {
9
+ return {
10
+ baseUrl: this.manifest.baseUrl ?? "",
11
+ headers: this.manifest.headers ?? {},
12
+ timeout: this.manifest.timeout ?? 10000,
13
+ followRedirects: this.manifest.followRedirects ?? true,
14
+ };
15
+ }
16
+ }
17
+ export function register() { }
18
+ export async function create(resource, _ctx) {
19
+ return new HttpClientResource(resource);
20
+ }
@@ -0,0 +1,26 @@
1
+ import type { ResourceContext, ResourceInstance } from "@telorun/sdk";
2
+ interface TeloResponse {
3
+ status: number;
4
+ headers: Record<string, string>;
5
+ body: unknown;
6
+ }
7
+ interface HttpRequestManifest {
8
+ client?: string;
9
+ url: string;
10
+ method?: string;
11
+ query?: Record<string, string>;
12
+ headers?: Record<string, string>;
13
+ body?: string | Record<string, unknown>;
14
+ timeout?: number;
15
+ throwOnHttpError?: boolean;
16
+ retries?: number;
17
+ }
18
+ declare class HttpRequestResource implements ResourceInstance {
19
+ private readonly manifest;
20
+ private readonly ctx;
21
+ constructor(manifest: HttpRequestManifest, ctx: ResourceContext);
22
+ invoke(input: any): Promise<TeloResponse>;
23
+ }
24
+ export declare function register(): void;
25
+ export declare function create(resource: HttpRequestManifest, ctx: ResourceContext): Promise<HttpRequestResource>;
26
+ export {};
@@ -0,0 +1,179 @@
1
+ const MAX_REDIRECTS = 5;
2
+ const DEFAULT_TIMEOUT = 10000;
3
+ function createNetworkError(code, message, url) {
4
+ const payload = {
5
+ error: "NetworkError",
6
+ code,
7
+ message,
8
+ details: { url },
9
+ };
10
+ const err = new Error(message);
11
+ err.networkError = payload;
12
+ err.code = code;
13
+ Object.assign(err, payload);
14
+ return err;
15
+ }
16
+ function mapNetworkError(err, url) {
17
+ const e = err;
18
+ if (e.name === "AbortError") {
19
+ throw createNetworkError("TIMEOUT", `Request timed out`, url);
20
+ }
21
+ const msg = e.message?.toLowerCase() ?? "";
22
+ if (msg.includes("econnrefused") || msg.includes("connection refused")) {
23
+ throw createNetworkError("CONNECTION_REFUSED", e.message, url);
24
+ }
25
+ if (msg.includes("enotfound") || msg.includes("getaddrinfo") || msg.includes("dns")) {
26
+ throw createNetworkError("DNS_RESOLUTION_FAILED", e.message, url);
27
+ }
28
+ if (msg.includes("ssl") || msg.includes("cert") || msg.includes("tls")) {
29
+ throw createNetworkError("SSL_ERROR", e.message, url);
30
+ }
31
+ throw createNetworkError("CONNECTION_REFUSED", e.message, url);
32
+ }
33
+ function normalizeHeaders(headers) {
34
+ const result = {};
35
+ for (const [key, value] of Object.entries(headers)) {
36
+ result[key.toLowerCase()] = value;
37
+ }
38
+ return result;
39
+ }
40
+ async function executeRequest(url, method, headers, body, timeout) {
41
+ const controller = new AbortController();
42
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
43
+ let currentUrl = url;
44
+ let redirectsLeft = MAX_REDIRECTS;
45
+ try {
46
+ while (true) {
47
+ const response = await fetch(currentUrl, {
48
+ method,
49
+ headers,
50
+ body,
51
+ redirect: "manual",
52
+ signal: controller.signal,
53
+ });
54
+ // Handle redirects manually (limit to MAX_REDIRECTS)
55
+ if ((response.status === 301 || response.status === 302) && redirectsLeft > 0) {
56
+ const location = response.headers.get("location");
57
+ if (location) {
58
+ currentUrl = location.startsWith("http")
59
+ ? location
60
+ : new URL(location, currentUrl).toString();
61
+ redirectsLeft--;
62
+ // For redirects, switch to GET and drop body per HTTP spec
63
+ method = "GET";
64
+ body = undefined;
65
+ delete headers["content-length"];
66
+ continue;
67
+ }
68
+ }
69
+ // Normalize response headers
70
+ const responseHeaders = {};
71
+ response.headers.forEach((value, key) => {
72
+ responseHeaders[key.toLowerCase()] = value;
73
+ });
74
+ // Deserialize body
75
+ const contentType = responseHeaders["content-type"] ?? "";
76
+ let responseBody;
77
+ if (contentType.includes("application/json")) {
78
+ const text = await response.text();
79
+ responseBody = text.length === 0 ? null : JSON.parse(text);
80
+ }
81
+ else {
82
+ responseBody = await response.text();
83
+ }
84
+ return { status: response.status, headers: responseHeaders, body: responseBody };
85
+ }
86
+ }
87
+ catch (err) {
88
+ mapNetworkError(err, url);
89
+ }
90
+ finally {
91
+ clearTimeout(timeoutId);
92
+ }
93
+ }
94
+ async function executeWithRetry(url, method, headers, body, timeout, retriesLeft) {
95
+ try {
96
+ return await executeRequest(url, method, headers, body, timeout);
97
+ }
98
+ catch (err) {
99
+ if (retriesLeft > 0 && err.error === "NetworkError") {
100
+ return executeWithRetry(url, method, headers, body, timeout, retriesLeft - 1);
101
+ }
102
+ throw err;
103
+ }
104
+ }
105
+ class HttpRequestResource {
106
+ manifest;
107
+ ctx;
108
+ constructor(manifest, ctx) {
109
+ this.manifest = manifest;
110
+ this.ctx = ctx;
111
+ }
112
+ async invoke(input) {
113
+ const ctx = this.ctx;
114
+ const m = this.manifest;
115
+ // Resolve client config
116
+ let clientBaseUrl = "";
117
+ let clientHeaders = {};
118
+ let clientTimeout = DEFAULT_TIMEOUT;
119
+ if (m.client) {
120
+ const clientName = ctx.expandValue(m.client, input ?? {});
121
+ const client = ctx.getResourcesByName("Client", clientName);
122
+ if (!client) {
123
+ throw new Error(`Http.Client "${clientName}" not found`);
124
+ }
125
+ clientBaseUrl = client.baseUrl ?? "";
126
+ clientHeaders = normalizeHeaders(client.headers ?? {});
127
+ clientTimeout = client.timeout ?? DEFAULT_TIMEOUT;
128
+ }
129
+ // Expand template fields
130
+ const rawUrl = ctx.expandValue(m.url, input ?? {});
131
+ const method = (ctx.expandValue(m.method ?? "GET", input ?? {}) || "GET").toUpperCase();
132
+ const requestHeaders = normalizeHeaders(ctx.expandValue(m.headers ?? {}, input ?? {}) ?? {});
133
+ const query = ctx.expandValue(m.query ?? {}, input ?? {}) ?? {};
134
+ const body = m.body !== undefined ? ctx.expandValue(m.body, input ?? {}) : undefined;
135
+ const effectiveTimeout = m.timeout ?? clientTimeout;
136
+ const retries = m.retries ?? 0;
137
+ const throwOnHttpError = m.throwOnHttpError ?? false;
138
+ // Build URL
139
+ let fullUrl = rawUrl.startsWith("http") ? rawUrl : `${clientBaseUrl}${rawUrl}`;
140
+ // Append query params
141
+ const queryEntries = Object.entries(query);
142
+ if (queryEntries.length > 0) {
143
+ const params = new URLSearchParams(queryEntries);
144
+ fullUrl = `${fullUrl}${fullUrl.includes("?") ? "&" : "?"}${params.toString()}`;
145
+ }
146
+ // Merge headers: client defaults < request-specific
147
+ const mergedHeaders = { ...clientHeaders, ...requestHeaders };
148
+ // Serialize body
149
+ let serializedBody;
150
+ if (body !== undefined) {
151
+ if (typeof body === "object" && body !== null) {
152
+ const contentType = mergedHeaders["content-type"] ?? "application/json";
153
+ if (!mergedHeaders["content-type"]) {
154
+ mergedHeaders["content-type"] = "application/json";
155
+ }
156
+ if (contentType.includes("application/x-www-form-urlencoded")) {
157
+ serializedBody = new URLSearchParams(body).toString();
158
+ }
159
+ else {
160
+ // Default to JSON
161
+ mergedHeaders["content-type"] = mergedHeaders["content-type"] ?? "application/json";
162
+ serializedBody = JSON.stringify(body);
163
+ }
164
+ }
165
+ else {
166
+ serializedBody = String(body);
167
+ }
168
+ }
169
+ const response = await executeWithRetry(fullUrl, method, mergedHeaders, serializedBody, effectiveTimeout, retries);
170
+ if (throwOnHttpError && response.status >= 400) {
171
+ throw new Error(`HTTP ${response.status} error from ${fullUrl}`);
172
+ }
173
+ return response;
174
+ }
175
+ }
176
+ export function register() { }
177
+ export async function create(resource, ctx) {
178
+ return new HttpRequestResource(resource, ctx);
179
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@telorun/http-client",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "exports": {
6
+ "./http-client": {
7
+ "bun": "./src/http-client-controller.ts",
8
+ "import": "./dist/http-client-controller.js"
9
+ },
10
+ "./http-request": {
11
+ "bun": "./src/http-request-controller.ts",
12
+ "import": "./dist/http-request-controller.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist/**"
17
+ ],
18
+ "dependencies": {
19
+ "@telorun/sdk": "0.2.5"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^20.0.0",
23
+ "typescript": "^5.0.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.lib.json"
27
+ }
28
+ }