@ng-desktop/http 0.0.0-stage → 0.4.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 +21 -0
- package/README.md +13 -2
- package/fesm2022/http-backend.d.ts +51 -0
- package/fesm2022/http-backend.js +285 -0
- package/fesm2022/http-resource.d.ts +23 -0
- package/fesm2022/http-resource.js +115 -0
- package/fesm2022/index.d.ts +2 -0
- package/fesm2022/index.js +2 -0
- package/package.json +52 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gaspar1992 and NgDesktop Engine Contributors
|
|
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
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ng-desktop/http
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Native Angular HttpBackend bridging HttpClient, interceptors, and httpResource() to the Rust host
|
|
4
|
+
|
|
5
|
+
Part of **[NgDesktop Engine](https://github.com/Gaspar1992/ng-desktop-engine)** — Native desktop rendering engine for Angular 22+ (`GTK4`, `WinUI 3`, `AppKit`).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @ng-desktop/http
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
- Repository path: `packages/http-native`
|
|
14
|
+
- Documentation & Ecosystem Guide: https://github.com/Gaspar1992/ng-desktop-engine/blob/main/docs/ECOSYSTEM_COMPATIBILITY.md
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { HttpBackend, HttpEvent, HttpFeature, HttpFeatureKind, HttpRequest, withFetch, withInterceptors, withInterceptorsFromDi, withJsonpSupport, withNoXsrfProtection, withRequestsMadeViaParent, withXhr, withXsrfConfiguration } from '@angular/common/http';
|
|
2
|
+
import { EnvironmentProviders, InjectionToken } from '@angular/core';
|
|
3
|
+
import { NativeHttpCallKind, NativeHttpConfig } from '@ng-desktop/platform';
|
|
4
|
+
import { Observable } from 'rxjs';
|
|
5
|
+
import * as i0 from "@angular/core";
|
|
6
|
+
export { HttpFeatureKind, withFetch, withInterceptors, withInterceptorsFromDi, withJsonpSupport, withNoXsrfProtection, withRequestsMadeViaParent, withXhr, withXsrfConfiguration, };
|
|
7
|
+
/**
|
|
8
|
+
* Injection token holding the resolved `NativeHttpConfig` translated from `provideHttpClient(...)` features.
|
|
9
|
+
*/
|
|
10
|
+
export declare const NATIVE_HTTP_CONFIG: InjectionToken<NativeHttpConfig>;
|
|
11
|
+
/**
|
|
12
|
+
* Translates Angular `HttpFeature` configurations (`withFetch`, `withXsrfConfiguration`,
|
|
13
|
+
* `withNoXsrfProtection`, `withInterceptors`, `withJsonpSupport`, and `@ng-desktop/http` features)
|
|
14
|
+
* into a Rust-compatible `NativeHttpConfig`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function resolveNativeHttpConfig(features: readonly HttpFeature<HttpFeatureKind>[]): NativeHttpConfig;
|
|
17
|
+
/**
|
|
18
|
+
* Configures native engine HTTP options alongside standard Angular `provideHttpClient(...)` features.
|
|
19
|
+
*/
|
|
20
|
+
export declare function withNativeHttpConfig(config: Partial<NativeHttpConfig>): HttpFeature<HttpFeatureKind>;
|
|
21
|
+
/**
|
|
22
|
+
* Enables or disables response caching in the Rust OS-native HTTP engine.
|
|
23
|
+
*/
|
|
24
|
+
export declare function withHttpCaching(cacheEnabled: boolean): HttpFeature<HttpFeatureKind>;
|
|
25
|
+
/**
|
|
26
|
+
* Tunes parallel multiplexed HTTP/2 connections for batch and asset transfers in Rust.
|
|
27
|
+
*/
|
|
28
|
+
export declare function withParallelBatching(maxParallelConnections: number): HttpFeature<HttpFeatureKind>;
|
|
29
|
+
export declare function detectNativeCallKind(req: HttpRequest<unknown>): NativeHttpCallKind;
|
|
30
|
+
/**
|
|
31
|
+
* Custom Angular `HttpBackend` that bypasses browser `XMLHttpRequest` / `fetch`
|
|
32
|
+
* and executes requests through `globalThis.__NG_NATIVE__.httpRequest` in the Rust OS-native HTTP engine
|
|
33
|
+
* (`libsoup-3.0` on Linux, `NSURLSession` on macOS, `WinHTTP` on Windows).
|
|
34
|
+
* Compatible with `HttpClient`, functional `HttpInterceptorFn`, and `httpResource()`.
|
|
35
|
+
*/
|
|
36
|
+
export declare class NativeHttpBackend implements HttpBackend {
|
|
37
|
+
handle(req: HttpRequest<unknown>): Observable<HttpEvent<unknown>>;
|
|
38
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<NativeHttpBackend, never>;
|
|
39
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Drop-in Angular `provideHttpClient(...)` provider for native desktop apps.
|
|
43
|
+
* Translates Angular features (`withFetch`, `withInterceptors`, `withXsrfConfiguration`,
|
|
44
|
+
* `withNoXsrfProtection`, `withJsonpSupport`, `withRequestsMadeViaParent`, and native transport
|
|
45
|
+
* helpers) into the Rust OS-native HTTP engine and routes `HttpClient` through `NativeHttpBackend`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function provideHttpClient(...features: HttpFeature<HttpFeatureKind>[]): EnvironmentProviders;
|
|
48
|
+
/**
|
|
49
|
+
* Backward-compatible alias for `provideHttpClient(...)`.
|
|
50
|
+
*/
|
|
51
|
+
export declare const provideNativeHttpClient: typeof provideHttpClient;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { HttpBackend, HttpErrorResponse, HttpFeatureKind, HttpHeaders, HttpResponse, provideHttpClient as angularProvideHttpClient, withFetch, withInterceptors, withInterceptorsFromDi, withJsonpSupport, withNoXsrfProtection, withRequestsMadeViaParent, withXhr, withXsrfConfiguration, } from '@angular/common/http';
|
|
2
|
+
import { Injectable, InjectionToken, makeEnvironmentProviders, } from '@angular/core';
|
|
3
|
+
import { ensureNgNativeBridge, } from '@ng-desktop/platform';
|
|
4
|
+
import { Observable } from 'rxjs';
|
|
5
|
+
import * as i0 from "@angular/core";
|
|
6
|
+
export { HttpFeatureKind, withFetch, withInterceptors, withInterceptorsFromDi, withJsonpSupport, withNoXsrfProtection, withRequestsMadeViaParent, withXhr, withXsrfConfiguration, };
|
|
7
|
+
/**
|
|
8
|
+
* Injection token holding the resolved `NativeHttpConfig` translated from `provideHttpClient(...)` features.
|
|
9
|
+
*/
|
|
10
|
+
export const NATIVE_HTTP_CONFIG = new InjectionToken('NATIVE_HTTP_CONFIG');
|
|
11
|
+
const NATIVE_CONFIG_OVERRIDE_TOKEN = new InjectionToken('NATIVE_HTTP_CONFIG_OVERRIDE');
|
|
12
|
+
function isNativeConfigProvider(item) {
|
|
13
|
+
return (typeof item === 'object' &&
|
|
14
|
+
item !== null &&
|
|
15
|
+
item.provide === NATIVE_CONFIG_OVERRIDE_TOKEN &&
|
|
16
|
+
typeof item.useValue === 'object' &&
|
|
17
|
+
item.useValue !== null);
|
|
18
|
+
}
|
|
19
|
+
function applyXsrfProviders(providers, config) {
|
|
20
|
+
if (!providers) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const stringValues = [];
|
|
24
|
+
for (const item of providers) {
|
|
25
|
+
if (typeof item !== 'object' || item === null) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const candidate = item;
|
|
29
|
+
if (typeof candidate.useValue !== 'string') {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const desc = String(candidate.provide ?? '');
|
|
33
|
+
if (desc.includes('XSRF_COOKIE_NAME')) {
|
|
34
|
+
config.xsrfCookieName = candidate.useValue;
|
|
35
|
+
}
|
|
36
|
+
else if (desc.includes('XSRF_HEADER_NAME')) {
|
|
37
|
+
config.xsrfHeaderName = candidate.useValue;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
stringValues.push(candidate.useValue);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (stringValues.length >= 1 && config.xsrfCookieName === 'XSRF-TOKEN') {
|
|
44
|
+
config.xsrfCookieName = stringValues[0];
|
|
45
|
+
}
|
|
46
|
+
if (stringValues.length >= 2 && config.xsrfHeaderName === 'X-XSRF-TOKEN') {
|
|
47
|
+
config.xsrfHeaderName = stringValues[1];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Translates Angular `HttpFeature` configurations (`withFetch`, `withXsrfConfiguration`,
|
|
52
|
+
* `withNoXsrfProtection`, `withInterceptors`, `withJsonpSupport`, and `@ng-desktop/http` features)
|
|
53
|
+
* into a Rust-compatible `NativeHttpConfig`.
|
|
54
|
+
*/
|
|
55
|
+
export function resolveNativeHttpConfig(features) {
|
|
56
|
+
const config = {
|
|
57
|
+
useFetchTransport: true,
|
|
58
|
+
interceptorsEnabled: true,
|
|
59
|
+
xsrfEnabled: true,
|
|
60
|
+
xsrfCookieName: 'XSRF-TOKEN',
|
|
61
|
+
xsrfHeaderName: 'X-XSRF-TOKEN',
|
|
62
|
+
xsrfTokenValue: '',
|
|
63
|
+
jsonpEnabled: false,
|
|
64
|
+
requestsViaParent: false,
|
|
65
|
+
maxParallelConnections: 24,
|
|
66
|
+
cacheEnabled: true,
|
|
67
|
+
timeoutMs: 15_000,
|
|
68
|
+
};
|
|
69
|
+
for (const feature of features) {
|
|
70
|
+
const internal = feature;
|
|
71
|
+
if (internal.ɵproviders) {
|
|
72
|
+
for (const item of internal.ɵproviders) {
|
|
73
|
+
if (isNativeConfigProvider(item)) {
|
|
74
|
+
Object.assign(config, item.useValue);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
switch (internal.ɵkind) {
|
|
79
|
+
case HttpFeatureKind.Fetch:
|
|
80
|
+
config.useFetchTransport = true;
|
|
81
|
+
break;
|
|
82
|
+
case HttpFeatureKind.Xhr:
|
|
83
|
+
config.useFetchTransport = false;
|
|
84
|
+
break;
|
|
85
|
+
case HttpFeatureKind.Interceptors:
|
|
86
|
+
case HttpFeatureKind.LegacyInterceptors:
|
|
87
|
+
config.interceptorsEnabled = true;
|
|
88
|
+
break;
|
|
89
|
+
case HttpFeatureKind.NoXsrfProtection:
|
|
90
|
+
config.xsrfEnabled = false;
|
|
91
|
+
break;
|
|
92
|
+
case HttpFeatureKind.CustomXsrfConfiguration:
|
|
93
|
+
config.xsrfEnabled = true;
|
|
94
|
+
applyXsrfProviders(internal.ɵproviders, config);
|
|
95
|
+
break;
|
|
96
|
+
case HttpFeatureKind.JsonpSupport:
|
|
97
|
+
config.jsonpEnabled = true;
|
|
98
|
+
break;
|
|
99
|
+
case HttpFeatureKind.RequestsMadeViaParent:
|
|
100
|
+
config.requestsViaParent = true;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return config;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Configures native engine HTTP options alongside standard Angular `provideHttpClient(...)` features.
|
|
108
|
+
*/
|
|
109
|
+
export function withNativeHttpConfig(config) {
|
|
110
|
+
return {
|
|
111
|
+
ɵkind: HttpFeatureKind.Interceptors,
|
|
112
|
+
ɵproviders: [{ provide: NATIVE_CONFIG_OVERRIDE_TOKEN, useValue: config }],
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Enables or disables response caching in the Rust OS-native HTTP engine.
|
|
117
|
+
*/
|
|
118
|
+
export function withHttpCaching(cacheEnabled) {
|
|
119
|
+
return withNativeHttpConfig({ cacheEnabled });
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Tunes parallel multiplexed HTTP/2 connections for batch and asset transfers in Rust.
|
|
123
|
+
*/
|
|
124
|
+
export function withParallelBatching(maxParallelConnections) {
|
|
125
|
+
return withNativeHttpConfig({ maxParallelConnections: Math.max(1, maxParallelConnections) });
|
|
126
|
+
}
|
|
127
|
+
export function detectNativeCallKind(req) {
|
|
128
|
+
const overrideKind = req.headers.get('X-Ng-Native-Call-Kind')?.trim().toLowerCase();
|
|
129
|
+
if (overrideKind === 'graphql' ||
|
|
130
|
+
overrideKind === 'asset' ||
|
|
131
|
+
overrideKind === 'batch' ||
|
|
132
|
+
overrideKind === 'text' ||
|
|
133
|
+
overrideKind === 'json') {
|
|
134
|
+
return overrideKind;
|
|
135
|
+
}
|
|
136
|
+
if (req.responseType === 'arraybuffer' || req.responseType === 'blob') {
|
|
137
|
+
return 'asset';
|
|
138
|
+
}
|
|
139
|
+
if (req.responseType === 'text') {
|
|
140
|
+
return 'text';
|
|
141
|
+
}
|
|
142
|
+
const cleanUrl = req.urlWithParams.split('?')[0].toLowerCase();
|
|
143
|
+
if (req.method.toUpperCase() === 'POST' && cleanUrl.includes('graphql')) {
|
|
144
|
+
return 'graphql';
|
|
145
|
+
}
|
|
146
|
+
if (/\.(png|jpe?g|webp|svg|ico|bin)$/i.test(cleanUrl)) {
|
|
147
|
+
return 'asset';
|
|
148
|
+
}
|
|
149
|
+
if (/\.(txt|html|css|xml|csv)$/i.test(cleanUrl)) {
|
|
150
|
+
return 'text';
|
|
151
|
+
}
|
|
152
|
+
return 'json';
|
|
153
|
+
}
|
|
154
|
+
function extractRequestHeaders(req) {
|
|
155
|
+
const headers = {};
|
|
156
|
+
for (const key of req.headers.keys()) {
|
|
157
|
+
const values = req.headers.getAll(key);
|
|
158
|
+
if (values && values.length > 0) {
|
|
159
|
+
headers[key] = values.join(', ');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return headers;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Custom Angular `HttpBackend` that bypasses browser `XMLHttpRequest` / `fetch`
|
|
166
|
+
* and executes requests through `globalThis.__NG_NATIVE__.httpRequest` in the Rust OS-native HTTP engine
|
|
167
|
+
* (`libsoup-3.0` on Linux, `NSURLSession` on macOS, `WinHTTP` on Windows).
|
|
168
|
+
* Compatible with `HttpClient`, functional `HttpInterceptorFn`, and `httpResource()`.
|
|
169
|
+
*/
|
|
170
|
+
export class NativeHttpBackend {
|
|
171
|
+
handle(req) {
|
|
172
|
+
return new Observable((observer) => {
|
|
173
|
+
let cancelled = false;
|
|
174
|
+
const emitResponse = (rawRes) => {
|
|
175
|
+
if (cancelled) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
let parsedBody = rawRes.body;
|
|
179
|
+
if (req.responseType === 'json' && rawRes.body.length > 0) {
|
|
180
|
+
try {
|
|
181
|
+
parsedBody = JSON.parse(rawRes.body);
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
parsedBody = rawRes.body;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
else if (req.responseType === 'blob' && typeof Blob === 'function') {
|
|
188
|
+
parsedBody = new Blob([rawRes.body]);
|
|
189
|
+
}
|
|
190
|
+
else if (req.responseType === 'arraybuffer' && typeof TextEncoder === 'function') {
|
|
191
|
+
parsedBody = new TextEncoder().encode(rawRes.body).buffer;
|
|
192
|
+
}
|
|
193
|
+
const headers = new HttpHeaders(rawRes.headers ?? {});
|
|
194
|
+
if (rawRes.status >= 200 && rawRes.status < 300) {
|
|
195
|
+
observer.next(new HttpResponse({
|
|
196
|
+
body: parsedBody,
|
|
197
|
+
headers,
|
|
198
|
+
status: rawRes.status,
|
|
199
|
+
statusText: rawRes.statusText,
|
|
200
|
+
url: req.urlWithParams,
|
|
201
|
+
}));
|
|
202
|
+
observer.complete();
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
observer.error(new HttpErrorResponse({
|
|
206
|
+
error: parsedBody,
|
|
207
|
+
headers,
|
|
208
|
+
status: rawRes.status,
|
|
209
|
+
statusText: rawRes.statusText,
|
|
210
|
+
url: req.urlWithParams,
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
try {
|
|
215
|
+
const bridge = ensureNgNativeBridge();
|
|
216
|
+
const serializedBody = req.serializeBody();
|
|
217
|
+
const bodyStr = serializedBody === null || serializedBody === undefined
|
|
218
|
+
? undefined
|
|
219
|
+
: typeof serializedBody === 'string'
|
|
220
|
+
? serializedBody
|
|
221
|
+
: JSON.stringify(serializedBody);
|
|
222
|
+
const callKind = detectNativeCallKind(req);
|
|
223
|
+
const options = {
|
|
224
|
+
headers: extractRequestHeaders(req),
|
|
225
|
+
responseType: req.responseType,
|
|
226
|
+
callKind,
|
|
227
|
+
withCredentials: req.withCredentials,
|
|
228
|
+
};
|
|
229
|
+
const hasHostService = typeof globalThis
|
|
230
|
+
.__NG_NATIVE_HOST_SERVICE__ === 'function';
|
|
231
|
+
if (hasHostService && typeof bridge.httpRequestAsync === 'function') {
|
|
232
|
+
bridge
|
|
233
|
+
.httpRequestAsync(req.method, req.urlWithParams, bodyStr, options)
|
|
234
|
+
.then(emitResponse)
|
|
235
|
+
.catch((err) => {
|
|
236
|
+
if (!cancelled) {
|
|
237
|
+
observer.error(err);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
return () => {
|
|
241
|
+
cancelled = true;
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const rawRes = bridge.httpRequest(req.method, req.urlWithParams, bodyStr, options);
|
|
245
|
+
emitResponse(rawRes);
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
observer.error(err);
|
|
249
|
+
}
|
|
250
|
+
return undefined;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.6", ngImport: i0, type: NativeHttpBackend, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
254
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.6", ngImport: i0, type: NativeHttpBackend, providedIn: 'root' });
|
|
255
|
+
}
|
|
256
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.6", ngImport: i0, type: NativeHttpBackend, decorators: [{
|
|
257
|
+
type: Injectable,
|
|
258
|
+
args: [{ providedIn: 'root' }]
|
|
259
|
+
}] });
|
|
260
|
+
/**
|
|
261
|
+
* Drop-in Angular `provideHttpClient(...)` provider for native desktop apps.
|
|
262
|
+
* Translates Angular features (`withFetch`, `withInterceptors`, `withXsrfConfiguration`,
|
|
263
|
+
* `withNoXsrfProtection`, `withJsonpSupport`, `withRequestsMadeViaParent`, and native transport
|
|
264
|
+
* helpers) into the Rust OS-native HTTP engine and routes `HttpClient` through `NativeHttpBackend`.
|
|
265
|
+
*/
|
|
266
|
+
export function provideHttpClient(...features) {
|
|
267
|
+
const resolvedConfig = resolveNativeHttpConfig(features);
|
|
268
|
+
const appliedConfig = ensureNgNativeBridge().configureHttp(resolvedConfig);
|
|
269
|
+
const sanitizedFeatures = features.filter((feature) => {
|
|
270
|
+
const kind = feature.ɵkind;
|
|
271
|
+
return kind !== HttpFeatureKind.Fetch && kind !== HttpFeatureKind.Xhr;
|
|
272
|
+
});
|
|
273
|
+
const providers = [
|
|
274
|
+
angularProvideHttpClient(...sanitizedFeatures),
|
|
275
|
+
{ provide: NATIVE_HTTP_CONFIG, useValue: appliedConfig },
|
|
276
|
+
];
|
|
277
|
+
if (!resolvedConfig.requestsViaParent) {
|
|
278
|
+
providers.push({ provide: HttpBackend, useExisting: NativeHttpBackend });
|
|
279
|
+
}
|
|
280
|
+
return makeEnvironmentProviders(providers);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Backward-compatible alias for `provideHttpClient(...)`.
|
|
284
|
+
*/
|
|
285
|
+
export const provideNativeHttpClient = provideHttpClient;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Signal, WritableSignal } from '@angular/core';
|
|
2
|
+
import { NativeHttpRequestOptions } from '@ng-desktop/platform';
|
|
3
|
+
export type NativeResourceStatus = 'idle' | 'loading' | 'resolved' | 'error';
|
|
4
|
+
export interface NativeHttpResourceOptions<T> {
|
|
5
|
+
request?: NativeHttpRequestOptions;
|
|
6
|
+
defaultValue?: T;
|
|
7
|
+
map?: (raw: unknown) => T;
|
|
8
|
+
}
|
|
9
|
+
export interface NativeHttpResourceRef<T> {
|
|
10
|
+
readonly url: Signal<string | undefined>;
|
|
11
|
+
readonly value: WritableSignal<T | undefined>;
|
|
12
|
+
readonly status: WritableSignal<NativeResourceStatus>;
|
|
13
|
+
readonly error: WritableSignal<unknown>;
|
|
14
|
+
readonly isLoading: Signal<boolean>;
|
|
15
|
+
readonly hasValue: Signal<boolean>;
|
|
16
|
+
reload(): boolean;
|
|
17
|
+
reloadAsync(): Promise<boolean>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Signal-based reactive HTTP resource backed by the OS-native Rust HTTP bridge (`ensureNgNativeBridge()`).
|
|
21
|
+
* Mirrors Angular's `httpResource()` ergonomics (`value`, `status`, `error`, `isLoading`, `hasValue`, `reload`, `reloadAsync`).
|
|
22
|
+
*/
|
|
23
|
+
export declare function createNativeHttpResource<T = unknown>(urlOrFn: string | (() => string | undefined), options?: NativeHttpResourceOptions<T>): NativeHttpResourceRef<T>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { computed, signal } from '@angular/core';
|
|
2
|
+
import { ensureNgNativeBridge, } from '@ng-desktop/platform';
|
|
3
|
+
/**
|
|
4
|
+
* Signal-based reactive HTTP resource backed by the OS-native Rust HTTP bridge (`ensureNgNativeBridge()`).
|
|
5
|
+
* Mirrors Angular's `httpResource()` ergonomics (`value`, `status`, `error`, `isLoading`, `hasValue`, `reload`, `reloadAsync`).
|
|
6
|
+
*/
|
|
7
|
+
export function createNativeHttpResource(urlOrFn, options) {
|
|
8
|
+
const resolveUrl = () => {
|
|
9
|
+
const raw = typeof urlOrFn === 'function' ? urlOrFn() : urlOrFn;
|
|
10
|
+
const clean = raw?.trim();
|
|
11
|
+
return clean && clean.length > 0 ? clean : undefined;
|
|
12
|
+
};
|
|
13
|
+
const url = computed(() => resolveUrl(), /* @ts-ignore */
|
|
14
|
+
...(ngDevMode ? [{ debugName: "url" }] : /* istanbul ignore next */ []));
|
|
15
|
+
const value = signal(options?.defaultValue, /* @ts-ignore */
|
|
16
|
+
...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
|
|
17
|
+
const status = signal('idle', /* @ts-ignore */
|
|
18
|
+
...(ngDevMode ? [{ debugName: "status" }] : /* istanbul ignore next */ []));
|
|
19
|
+
const error = signal(undefined, /* @ts-ignore */
|
|
20
|
+
...(ngDevMode ? [{ debugName: "error" }] : /* istanbul ignore next */ []));
|
|
21
|
+
const isLoading = computed(() => status() === 'loading', /* @ts-ignore */
|
|
22
|
+
...(ngDevMode ? [{ debugName: "isLoading" }] : /* istanbul ignore next */ []));
|
|
23
|
+
const hasValue = computed(() => value() !== undefined, /* @ts-ignore */
|
|
24
|
+
...(ngDevMode ? [{ debugName: "hasValue" }] : /* istanbul ignore next */ []));
|
|
25
|
+
let requestSeq = 0;
|
|
26
|
+
const applyResponse = (targetUrl, res) => {
|
|
27
|
+
if (res.status >= 200 && res.status < 300) {
|
|
28
|
+
const responseType = options?.request?.responseType ?? 'json';
|
|
29
|
+
let parsed = res.body;
|
|
30
|
+
if (responseType === 'json' && typeof res.body === 'string' && res.body.length > 0) {
|
|
31
|
+
parsed = JSON.parse(res.body);
|
|
32
|
+
}
|
|
33
|
+
const finalValue = options?.map ? options.map(parsed) : parsed;
|
|
34
|
+
value.set(finalValue);
|
|
35
|
+
status.set('resolved');
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
error.set({
|
|
39
|
+
status: res.status,
|
|
40
|
+
statusText: res.statusText,
|
|
41
|
+
body: res.body,
|
|
42
|
+
url: targetUrl,
|
|
43
|
+
});
|
|
44
|
+
status.set('error');
|
|
45
|
+
return false;
|
|
46
|
+
};
|
|
47
|
+
const resetToIdle = () => {
|
|
48
|
+
status.set('idle');
|
|
49
|
+
error.set(undefined);
|
|
50
|
+
return false;
|
|
51
|
+
};
|
|
52
|
+
const reload = () => {
|
|
53
|
+
const seq = ++requestSeq;
|
|
54
|
+
const targetUrl = resolveUrl();
|
|
55
|
+
if (!targetUrl) {
|
|
56
|
+
return resetToIdle();
|
|
57
|
+
}
|
|
58
|
+
status.set('loading');
|
|
59
|
+
error.set(undefined);
|
|
60
|
+
try {
|
|
61
|
+
const bridge = ensureNgNativeBridge();
|
|
62
|
+
const res = bridge.httpRequest('GET', targetUrl, undefined, options?.request);
|
|
63
|
+
if (seq !== requestSeq) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return applyResponse(targetUrl, res);
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
if (seq !== requestSeq) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
error.set(err);
|
|
73
|
+
status.set('error');
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const reloadAsync = async () => {
|
|
78
|
+
const seq = ++requestSeq;
|
|
79
|
+
const targetUrl = resolveUrl();
|
|
80
|
+
if (!targetUrl) {
|
|
81
|
+
return resetToIdle();
|
|
82
|
+
}
|
|
83
|
+
status.set('loading');
|
|
84
|
+
error.set(undefined);
|
|
85
|
+
try {
|
|
86
|
+
const bridge = ensureNgNativeBridge();
|
|
87
|
+
const res = typeof bridge.httpRequestAsync === 'function'
|
|
88
|
+
? await bridge.httpRequestAsync('GET', targetUrl, undefined, options?.request)
|
|
89
|
+
: bridge.httpRequest('GET', targetUrl, undefined, options?.request);
|
|
90
|
+
if (seq !== requestSeq) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return applyResponse(targetUrl, res);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
if (seq !== requestSeq) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
error.set(err);
|
|
100
|
+
status.set('error');
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
reload();
|
|
105
|
+
return {
|
|
106
|
+
url,
|
|
107
|
+
value,
|
|
108
|
+
status,
|
|
109
|
+
error,
|
|
110
|
+
isLoading,
|
|
111
|
+
hasValue,
|
|
112
|
+
reload,
|
|
113
|
+
reloadAsync,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { detectNativeCallKind, HttpFeatureKind, NATIVE_HTTP_CONFIG, NativeHttpBackend, provideHttpClient, provideNativeHttpClient, resolveNativeHttpConfig, withFetch, withHttpCaching, withInterceptors, withInterceptorsFromDi, withJsonpSupport, withNativeHttpConfig, withNoXsrfProtection, withParallelBatching, withRequestsMadeViaParent, withXhr, withXsrfConfiguration, } from './http-backend';
|
|
2
|
+
export { createNativeHttpResource, type NativeHttpResourceOptions, type NativeHttpResourceRef, type NativeResourceStatus, } from './http-resource';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { detectNativeCallKind, HttpFeatureKind, NATIVE_HTTP_CONFIG, NativeHttpBackend, provideHttpClient, provideNativeHttpClient, resolveNativeHttpConfig, withFetch, withHttpCaching, withInterceptors, withInterceptorsFromDi, withJsonpSupport, withNativeHttpConfig, withNoXsrfProtection, withParallelBatching, withRequestsMadeViaParent, withXhr, withXsrfConfiguration, } from './http-backend.js';
|
|
2
|
+
export { createNativeHttpResource, } from './http-resource.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ng-desktop/http",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Native Angular HttpBackend bridging HttpClient, interceptors, and httpResource() to the Rust host",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"angular",
|
|
8
|
+
"angular22",
|
|
9
|
+
"desktop",
|
|
10
|
+
"native",
|
|
11
|
+
"gtk4",
|
|
12
|
+
"winui3",
|
|
13
|
+
"appkit",
|
|
14
|
+
"rust"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/Gaspar1992/ng-desktop-engine",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/Gaspar1992/ng-desktop-engine/issues"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/Gaspar1992/ng-desktop-engine.git",
|
|
23
|
+
"directory": "packages/http-native"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"files": [
|
|
31
|
+
"fesm2022",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"!**/*.map"
|
|
35
|
+
],
|
|
36
|
+
"main": "./fesm2022/index.js",
|
|
37
|
+
"module": "./fesm2022/index.js",
|
|
38
|
+
"types": "./fesm2022/index.d.ts",
|
|
39
|
+
"exports": {
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./fesm2022/index.d.ts",
|
|
42
|
+
"default": "./fesm2022/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./package.json": {
|
|
45
|
+
"default": "./package.json"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@angular/common": ">=22.0.0",
|
|
50
|
+
"@angular/core": ">=22.0.0",
|
|
51
|
+
"@ng-desktop/platform": ">=0.4.0",
|
|
52
|
+
"rxjs": ">=7.8.0"
|
|
53
|
+
}
|
|
54
|
+
}
|