@ng-desktop/http 0.6.0 → 0.7.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.
@@ -175,12 +175,23 @@ export class NativeHttpBackend {
175
175
  if (cancelled) {
176
176
  return;
177
177
  }
178
+ const headers = new HttpHeaders(rawRes.headers ?? {});
178
179
  let parsedBody = rawRes.body;
179
180
  if (req.responseType === 'json' && rawRes.body.length > 0) {
180
181
  try {
181
182
  parsedBody = JSON.parse(rawRes.body);
182
183
  }
183
- catch {
184
+ catch (parseErr) {
185
+ if (rawRes.status >= 200 && rawRes.status < 300) {
186
+ observer.error(new HttpErrorResponse({
187
+ error: { error: parseErr, text: rawRes.body },
188
+ headers,
189
+ status: rawRes.status,
190
+ statusText: rawRes.statusText || 'OK',
191
+ url: req.urlWithParams,
192
+ }));
193
+ return;
194
+ }
184
195
  parsedBody = rawRes.body;
185
196
  }
186
197
  }
@@ -190,7 +201,6 @@ export class NativeHttpBackend {
190
201
  else if (req.responseType === 'arraybuffer' && typeof TextEncoder === 'function') {
191
202
  parsedBody = new TextEncoder().encode(rawRes.body).buffer;
192
203
  }
193
- const headers = new HttpHeaders(rawRes.headers ?? {});
194
204
  if (rawRes.status >= 200 && rawRes.status < 300) {
195
205
  observer.next(new HttpResponse({
196
206
  body: parsedBody,
@@ -218,7 +228,9 @@ export class NativeHttpBackend {
218
228
  ? undefined
219
229
  : typeof serializedBody === 'string'
220
230
  ? serializedBody
221
- : JSON.stringify(serializedBody);
231
+ : typeof URLSearchParams !== 'undefined' && serializedBody instanceof URLSearchParams
232
+ ? serializedBody.toString()
233
+ : JSON.stringify(serializedBody);
222
234
  const callKind = detectNativeCallKind(req);
223
235
  const options = {
224
236
  headers: extractRequestHeaders(req),
@@ -1,4 +1,4 @@
1
- import { computed, signal } from '@angular/core';
1
+ import { computed, effect, inject, Injector, signal, untracked, } from '@angular/core';
2
2
  import { ensureNgNativeBridge, } from '@ng-desktop/platform';
3
3
  /**
4
4
  * Signal-based reactive HTTP resource backed by the OS-native Rust HTTP bridge (`ensureNgNativeBridge()`).
@@ -23,6 +23,7 @@ export function createNativeHttpResource(urlOrFn, options) {
23
23
  const hasValue = computed(() => value() !== undefined, /* @ts-ignore */
24
24
  ...(ngDevMode ? [{ debugName: "hasValue" }] : /* istanbul ignore next */ []));
25
25
  let requestSeq = 0;
26
+ let lastLoadedUrl;
26
27
  const applyResponse = (targetUrl, res) => {
27
28
  if (res.status >= 200 && res.status < 300) {
28
29
  const responseType = options?.request?.responseType ?? 'json';
@@ -52,6 +53,7 @@ export function createNativeHttpResource(urlOrFn, options) {
52
53
  const reload = () => {
53
54
  const seq = ++requestSeq;
54
55
  const targetUrl = resolveUrl();
56
+ lastLoadedUrl = targetUrl;
55
57
  if (!targetUrl) {
56
58
  return resetToIdle();
57
59
  }
@@ -77,6 +79,7 @@ export function createNativeHttpResource(urlOrFn, options) {
77
79
  const reloadAsync = async () => {
78
80
  const seq = ++requestSeq;
79
81
  const targetUrl = resolveUrl();
82
+ lastLoadedUrl = targetUrl;
80
83
  if (!targetUrl) {
81
84
  return resetToIdle();
82
85
  }
@@ -102,6 +105,26 @@ export function createNativeHttpResource(urlOrFn, options) {
102
105
  }
103
106
  };
104
107
  reload();
108
+ if (typeof urlOrFn === 'function') {
109
+ let injector = null;
110
+ try {
111
+ injector = inject(Injector);
112
+ }
113
+ catch {
114
+ injector = null;
115
+ }
116
+ if (injector) {
117
+ effect(() => {
118
+ const nextUrl = url();
119
+ if (nextUrl === lastLoadedUrl) {
120
+ return;
121
+ }
122
+ untracked(() => {
123
+ reload();
124
+ });
125
+ }, { injector });
126
+ }
127
+ }
105
128
  return {
106
129
  url,
107
130
  value,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ng-desktop/http",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Native Angular HttpBackend bridging HttpClient, interceptors, and httpResource() to the Rust host",
5
5
  "license": "MIT",
6
6
  "keywords": [