@fhss-web-team/frontend-utils 1.6.1 → 1.6.3

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.
@@ -0,0 +1,460 @@
1
+ import { Injector, Signal, ValueEqualityFn } from "@angular/core";
2
+ type JsonValue = string | number | boolean | null | JsonArray | JsonObject;
3
+ export type JsonObject = {
4
+ [key: string]: JsonValue;
5
+ };
6
+ type JsonArray = JsonValue[];
7
+ export type Json = JsonObject | JsonArray;
8
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
9
+ type DefaultError = {
10
+ code?: string;
11
+ message?: string;
12
+ details?: Json;
13
+ };
14
+ type KnownHeaderKeys = 'Accept' | 'Accept-Encoding' | 'Accept-Language' | 'Access-Control-Request-Headers' | 'Access-Control-Request-Method' | 'Authorization' | 'Cache-Control' | 'Connection' | 'Content-Encoding' | 'Content-Length' | 'Content-Type' | 'Cookie' | 'Date' | 'Expect' | 'Forwarded' | 'From' | 'Host' | 'If-Match' | 'If-Modified-Since' | 'If-None-Match' | 'If-Range' | 'If-Unmodified-Since' | 'Keep-Alive' | 'Max-Forwards' | 'Origin' | 'Prefer' | 'Priority' | 'Proxy-Authorization' | 'Range' | 'Referer' | 'TE' | 'Transfer-Encoding' | 'Upgrade' | 'User-Agent' | 'Via';
15
+ /**
16
+ * A value that may be a plain value of type `T` or a reactive `Signal<T>`.
17
+ *
18
+ * This is useful in APIs that accept either static or reactive values,
19
+ * allowing flexibility while maintaining type safety.
20
+ *
21
+ * Use `unwrapSignal` to extract the underlying value in a uniform way.
22
+ *
23
+ * @template T The underlying value type.
24
+ */
25
+ export type MaybeSignal<T> = T | Signal<T>;
26
+ /**
27
+ * Reads the underlying value from a MaybeSignal.
28
+ *
29
+ * This function is designed for use with reactive APIs (like fetchSignal) where it's important
30
+ * that Angular's computed methods register the dependency. If the input is a Signal, invoking it
31
+ * not only returns its current value but also tracks the signal for reactivity. If the input is
32
+ * a plain value, it simply returns that value.
33
+ *
34
+ * @template T The type of the value.
35
+ * @param value A plain value or a Signal that yields the value.
36
+ * @returns The current value, with dependency tracking enabled if the input is a Signal.
37
+ */
38
+ export declare function readMaybeSignal<T>(value: MaybeSignal<T>): T;
39
+ /**
40
+ * The structure of an `fetchSignal` request which will be sent to the backend via the Fetch API.
41
+ */
42
+ export type FetchSignalRequest = {
43
+ /**
44
+ * URL of the request.
45
+ *
46
+ * This URL should not include query parameters. Instead, specify query parameters through the
47
+ * `params` field.
48
+ */
49
+ url: string;
50
+ /**
51
+ * Body to send with the request, if there is one.
52
+ *
53
+ * If no Content-Type header is specified by the user, the Fetch API will attempt to set one based on
54
+ * the type of `body`.
55
+ */
56
+ body?: BodyInit;
57
+ /**
58
+ * Dictionary of query parameters which will be appeneded to the request URL.
59
+ */
60
+ params?: Record<string, string | number | boolean | undefined> | undefined;
61
+ /**
62
+ * Dictionary of headers to include with the outgoing request.
63
+ */
64
+ headers?: (Partial<Record<KnownHeaderKeys, string | undefined>> & Record<string, string | undefined>) | undefined;
65
+ };
66
+ /**
67
+ * String value capturing the status of a `FetchSignal`.
68
+ *
69
+ * Possible statuses are:
70
+ *
71
+ * `idle` - The fetch signal has no valid request and will not perform any loading. `value()` will be
72
+ * `undefined`.
73
+ *
74
+ * `loading` - The fetch signal is currently loading a new value as a result of a change in its reactive
75
+ * dependencies. `value()` will be `undefined`.
76
+ *
77
+ * `error` - Loading failed with an error. `value()` will be `undefined`.
78
+ *
79
+ * `resolved` - Loading has completed and the fetch signal has the value returned from the loader.
80
+ *
81
+ */
82
+ export type FetchSignalStatus = 'idle' | 'error' | 'loading' | 'resolved' | 'destroyed';
83
+ export type FetchSignalOptions<TResult, TRaw> = {
84
+ /**
85
+ * Whether or not the request should be fetched reactively when the request updates.
86
+ */
87
+ autoRefresh?: boolean;
88
+ /**
89
+ * NEEDS TO BE IMPLEMENTED. WILL NOT WORK
90
+ *
91
+ * Transform the response of the HTTP request before it's delivered to the fetch signal.
92
+ *
93
+ * `parse` receives the value from the HTTP layer as its raw type (e.g. as `unknown` for JSON data).
94
+ * It can be used to validate or transform the type of the fetch signal, and return a more specific
95
+ * type. This is also useful for validating backend responses using a runtime schema validation
96
+ * library such as Zod.
97
+ */
98
+ parse?: ((value: TRaw) => TResult) | undefined;
99
+ /**
100
+ * Value that the fetch signal will take when in Idle or Loading states.
101
+ *
102
+ * If not set, the fetch signal will use `undefined` as its default value.
103
+ */
104
+ defaultValue?: NoInfer<TResult> | undefined;
105
+ /**
106
+ * The `Injector` in which to create the `FetchSignal`.
107
+ *
108
+ * If this is not provided, the current [injection context](https://angular.dev/guide/di/dependency-injection-context)
109
+ * will be used instead (via `inject`).
110
+ */
111
+ injector?: Injector | undefined;
112
+ /**
113
+ * A comparison function which defines equality for the response value.
114
+ */
115
+ equal?: ValueEqualityFn<NoInfer<TResult | undefined>> | undefined;
116
+ };
117
+ export type ResponseTransformer<T> = (response: Response) => Promise<T>;
118
+ /**
119
+ * Represents the reactive result of an HTTP fetch request.
120
+ */
121
+ export type FetchSignal<Response, ErrorResponse = DefaultError> = {
122
+ /**
123
+ * Signal of the HTTP response body when the response is `ok`, when available.
124
+ * This value will persist while the fetchSignal is loading.
125
+ */
126
+ value: Signal<Response | undefined>;
127
+ /**
128
+ * Signal of the HTTP response body when the response is not `ok`, when available.
129
+ * This response must be in the shape of JSON.
130
+ */
131
+ errorResponse: Signal<ErrorResponse | undefined>;
132
+ /**
133
+ * Whether this fetch signal is loading a new value (or reloading the existing one).
134
+ */
135
+ isLoading: Signal<boolean>;
136
+ /**
137
+ * Signal of the response status code, when available.
138
+ */
139
+ statusCode: Signal<number | undefined>;
140
+ /**
141
+ * Signal of the response headers, when available.
142
+ */
143
+ headers: Signal<Record<string, string> | undefined>;
144
+ /**
145
+ * The current status of the `FetchSignal`, which describes what the fetch signal is currently doing and
146
+ * what can be expected of its `value`.
147
+ */
148
+ status: Signal<FetchSignalStatus>;
149
+ /**
150
+ * When in the `error` state, this returns the last known error from the `FetchSignal`.
151
+ */
152
+ error: Signal<Error | undefined>;
153
+ /**
154
+ * Instructs the fetchSignal to refetch the request with the current reactive dependencies
155
+ * @param abortSignal Optional abort signal to abort the fetch request
156
+ * @returns void
157
+ */
158
+ refresh: (abortSignal?: AbortSignal) => void;
159
+ /**
160
+ * Will clean up any effects and disable the fetchSignal.
161
+ * All values will be undefined and the fetchSiganl will have no operations.
162
+ * @returns void
163
+ */
164
+ destroy: () => void;
165
+ };
166
+ export type FetchSignalFactory = {
167
+ /**
168
+ * Initiates a reactive HTTP GET request using the provided request configuration.
169
+ *
170
+ * @template Response - The expected response type.
171
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
172
+ * @param request - The configuration object for the HTTP GET request.
173
+ * @param options - Additional options to provide to the fetchSignal.
174
+ * @returns A `FetchSignal` object containing reactive signals for the response data, loading state, status code, error, headers, and a refresh method.
175
+ */
176
+ <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>): FetchSignal<Response, ErrorResponse>;
177
+ /**
178
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as JSON.
179
+ *
180
+ * @template Response - The expected response type.
181
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
182
+ * @param request - The configuration object for the HTTP GET request.
183
+ * @param options - Additional options to provide to the fetchSignal.
184
+ * @returns A `FetchSignal` object with the JSON-parsed response.
185
+ */
186
+ json: <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>) => FetchSignal<Response, ErrorResponse>;
187
+ /**
188
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as plain text.
189
+ *
190
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
191
+ * @param request - The configuration object for the HTTP GET request.
192
+ * @param options - Additional options to provide to the fetchSignal.
193
+ * @returns A `FetchSignal` object with the text response.
194
+ */
195
+ text: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<string, string>) => FetchSignal<string, ErrorResponse>;
196
+ /**
197
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as a Blob.
198
+ *
199
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
200
+ * @param request - The configuration object for the HTTP GET request.
201
+ * @param options - Additional options to provide to the fetchSignal.
202
+ * @returns A `FetchSignal` object with the Blob response.
203
+ */
204
+ blob: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Blob, Blob>) => FetchSignal<Blob, ErrorResponse>;
205
+ /**
206
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as an ArrayBuffer.
207
+ *
208
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
209
+ * @param request - The configuration object for the HTTP GET request.
210
+ * @param options - Additional options to provide to the fetchSignal.
211
+ * @returns A `FetchSignal` object with the ArrayBuffer response.
212
+ */
213
+ arrayBuffer: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<ArrayBuffer, ArrayBuffer>) => FetchSignal<ArrayBuffer, ErrorResponse>;
214
+ get: {
215
+ /**
216
+ * Initiates a reactive HTTP GET request using the provided request configuration.
217
+ *
218
+ * @template Response - The expected response type.
219
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
220
+ * @param request - The configuration object for the HTTP GET request.
221
+ * @param options - Additional options to provide to the fetchSignal.
222
+ * @returns A `FetchSignal` object containing reactive signals for the response data, loading state, status code, error, headers, and a refresh method.
223
+ */
224
+ <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>): FetchSignal<Response, ErrorResponse>;
225
+ /**
226
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as JSON.
227
+ *
228
+ * @template Response - The expected response type.
229
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
230
+ * @param request - The configuration object for the HTTP GET request.
231
+ * @param options - Additional options to provide to the fetchSignal.
232
+ * @returns A `FetchSignal` object with the JSON-parsed response.
233
+ */
234
+ json: <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>) => FetchSignal<Response, ErrorResponse>;
235
+ /**
236
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as plain text.
237
+ *
238
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
239
+ * @param request - The configuration object for the HTTP GET request.
240
+ * @param options - Additional options to provide to the fetchSignal.
241
+ * @returns A `FetchSignal` object with the text response.
242
+ */
243
+ text: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<string, string>) => FetchSignal<string, ErrorResponse>;
244
+ /**
245
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as a Blob.
246
+ *
247
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
248
+ * @param request - The configuration object for the HTTP GET request.
249
+ * @param options - Additional options to provide to the fetchSignal.
250
+ * @returns A `FetchSignal` object with the Blob response.
251
+ */
252
+ blob: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Blob, Blob>) => FetchSignal<Blob, ErrorResponse>;
253
+ /**
254
+ * Initiates a reactive HTTP GET request using the provided request configuration and parses the response as an ArrayBuffer.
255
+ *
256
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
257
+ * @param request - The configuration object for the HTTP GET request.
258
+ * @param options - Additional options to provide to the fetchSignal.
259
+ * @returns A `FetchSignal` object with the ArrayBuffer response.
260
+ */
261
+ arrayBuffer: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<ArrayBuffer, ArrayBuffer>) => FetchSignal<ArrayBuffer, ErrorResponse>;
262
+ };
263
+ post: {
264
+ /**
265
+ * Initiates a reactive HTTP POST request using the provided request configuration.
266
+ *
267
+ * @template Response - The expected response type.
268
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
269
+ * @param request - The configuration object for the HTTP POST request, which must include a body.
270
+ * @param options - Additional options to provide to the fetchSignal.
271
+ * @returns A `FetchSignal` object containing reactive signals for the response data, loading state, status code, error, headers, and a refresh method.
272
+ */
273
+ <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>): FetchSignal<Response, ErrorResponse>;
274
+ /**
275
+ * Initiates a reactive HTTP POST request using the provided request configuration and parses the response as JSON.
276
+ *
277
+ * @template Response - The expected response type.
278
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
279
+ * @param request - The configuration object for the HTTP POST request, which must include a body.
280
+ * @param options - Additional options to provide to the fetchSignal.
281
+ * @returns A `FetchSignal` object with the JSON-parsed response.
282
+ */
283
+ json: <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>) => FetchSignal<Response, ErrorResponse>;
284
+ /**
285
+ * Initiates a reactive HTTP POST request using the provided request configuration and parses the response as plain text.
286
+ *
287
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
288
+ * @param request - The configuration object for the HTTP POST request, which must include a body.
289
+ * @param options - Additional options to provide to the fetchSignal.
290
+ * @returns A `FetchSignal` object with the text response.
291
+ */
292
+ text: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<string, string>) => FetchSignal<string, ErrorResponse>;
293
+ /**
294
+ * Initiates a reactive HTTP POST request using the provided request configuration and parses the response as a Blob.
295
+ *
296
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
297
+ * @param request - The configuration object for the HTTP POST request, which must include a body.
298
+ * @param options - Additional options to provide to the fetchSignal.
299
+ * @returns A `FetchSignal` object with the Blob response.
300
+ */
301
+ blob: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Blob, Blob>) => FetchSignal<Blob, ErrorResponse>;
302
+ /**
303
+ * Initiates a reactive HTTP POST request using the provided request configuration and parses the response as an ArrayBuffer.
304
+ *
305
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
306
+ * @param request - The configuration object for the HTTP POST request, which must include a body.
307
+ * @param options - Additional options to provide to the fetchSignal.
308
+ * @returns A `FetchSignal` object with the ArrayBuffer response.
309
+ */
310
+ arrayBuffer: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<ArrayBuffer, ArrayBuffer>) => FetchSignal<ArrayBuffer, ErrorResponse>;
311
+ };
312
+ put: {
313
+ /**
314
+ * Initiates a reactive HTTP PUT request using the provided request configuration.
315
+ *
316
+ * @template Response - The expected response type.
317
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
318
+ * @param request - The configuration object for the HTTP PUT request, which must include a body.
319
+ * @param options - Additional options to provide to the fetchSignal.
320
+ * @returns A `FetchSignal` object containing reactive signals for the response data, loading state, status code, error, headers, and a refresh method.
321
+ */
322
+ <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>): FetchSignal<Response, ErrorResponse>;
323
+ /**
324
+ * Initiates a reactive HTTP PUT request using the provided request configuration and parses the response as JSON.
325
+ *
326
+ * @template Response - The expected response type.
327
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
328
+ * @param request - The configuration object for the HTTP PUT request, which must include a body.
329
+ * @param options - Additional options to provide to the fetchSignal.
330
+ * @returns A `FetchSignal` object with the JSON-parsed response.
331
+ */
332
+ json: <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>) => FetchSignal<Response, ErrorResponse>;
333
+ /**
334
+ * Initiates a reactive HTTP PUT request using the provided request configuration and parses the response as plain text.
335
+ *
336
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
337
+ * @param request - The configuration object for the HTTP PUT request, which must include a body.
338
+ * @param options - Additional options to provide to the fetchSignal.
339
+ * @returns A `FetchSignal` object with the text response.
340
+ */
341
+ text: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<string, string>) => FetchSignal<string, ErrorResponse>;
342
+ /**
343
+ * Initiates a reactive HTTP PUT request using the provided request configuration and parses the response as a Blob.
344
+ *
345
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
346
+ * @param request - The configuration object for the HTTP PUT request, which must include a body.
347
+ * @param options - Additional options to provide to the fetchSignal.
348
+ * @returns A `FetchSignal` object with the Blob response.
349
+ */
350
+ blob: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Blob, Blob>) => FetchSignal<Blob, ErrorResponse>;
351
+ /**
352
+ * Initiates a reactive HTTP PUT request using the provided request configuration and parses the response as an ArrayBuffer.
353
+ *
354
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
355
+ * @param request - The configuration object for the HTTP PUT request, which must include a body.
356
+ * @param options - Additional options to provide to the fetchSignal.
357
+ * @returns A `FetchSignal` object with the ArrayBuffer response.
358
+ */
359
+ arrayBuffer: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<ArrayBuffer, ArrayBuffer>) => FetchSignal<ArrayBuffer, ErrorResponse>;
360
+ };
361
+ patch: {
362
+ /**
363
+ * Initiates a reactive HTTP PATCH request using the provided request configuration.
364
+ *
365
+ * @template Response - The expected response type.
366
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
367
+ * @param request - The configuration object for the HTTP PATCH request, which must include a body.
368
+ * @param options - Additional options to provide to the fetchSignal.
369
+ * @returns A `FetchSignal` object containing reactive signals for the response data, loading state, status code, error, headers, and a refresh method.
370
+ */
371
+ <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>): FetchSignal<Response, ErrorResponse>;
372
+ /**
373
+ * Initiates a reactive HTTP PATCH request using the provided request configuration and parses the response as JSON.
374
+ *
375
+ * @template Response - The expected response type.
376
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
377
+ * @param request - The configuration object for the HTTP PATCH request, which must include a body.
378
+ * @param options - Additional options to provide to the fetchSignal.
379
+ * @returns A `FetchSignal` object with the JSON-parsed response.
380
+ */
381
+ json: <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>) => FetchSignal<Response, ErrorResponse>;
382
+ /**
383
+ * Initiates a reactive HTTP PATCH request using the provided request configuration and parses the response as plain text.
384
+ *
385
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
386
+ * @param request - The configuration object for the HTTP PATCH request, which must include a body.
387
+ * @param options - Additional options to provide to the fetchSignal.
388
+ * @returns A `FetchSignal` object with the text response.
389
+ */
390
+ text: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<string, string>) => FetchSignal<string, ErrorResponse>;
391
+ /**
392
+ * Initiates a reactive HTTP PATCH request using the provided request configuration and parses the response as a Blob.
393
+ *
394
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
395
+ * @param request - The configuration object for the HTTP PATCH request, which must include a body.
396
+ * @param options - Additional options to provide to the fetchSignal.
397
+ * @returns A `FetchSignal` object with the Blob response.
398
+ */
399
+ blob: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Blob, Blob>) => FetchSignal<Blob, ErrorResponse>;
400
+ /**
401
+ * Initiates a reactive HTTP PATCH request using the provided request configuration and parses the response as an ArrayBuffer.
402
+ *
403
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
404
+ * @param request - The configuration object for the HTTP PATCH request, which must include a body.
405
+ * @param options - Additional options to provide to the fetchSignal.
406
+ * @returns A `FetchSignal` object with the ArrayBuffer response.
407
+ */
408
+ arrayBuffer: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<ArrayBuffer, ArrayBuffer>) => FetchSignal<ArrayBuffer, ErrorResponse>;
409
+ };
410
+ delete: {
411
+ /**
412
+ * Initiates a reactive HTTP DELETE request using the provided request configuration.
413
+ *
414
+ * @template Response - The expected response type.
415
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
416
+ * @param request - The configuration object for the HTTP DELETE request.
417
+ * @param options - Additional options to provide to the fetchSignal.
418
+ * @returns A `FetchSignal` object containing reactive signals for the response data, loading state, status code, error, headers, and a refresh method.
419
+ */
420
+ <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>): FetchSignal<Response, ErrorResponse>;
421
+ /**
422
+ * Initiates a reactive HTTP DELETE request using the provided request configuration and parses the response as JSON.
423
+ *
424
+ * @template Response - The expected response type.
425
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
426
+ * @param request - The configuration object for the HTTP DELETE request.
427
+ * @param options - Additional options to provide to the fetchSignal.
428
+ * @returns A `FetchSignal` object with the JSON-parsed response.
429
+ */
430
+ json: <Response extends Json, ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Response, any>) => FetchSignal<Response, ErrorResponse>;
431
+ /**
432
+ * Initiates a reactive HTTP DELETE request using the provided request configuration and parses the response as plain text.
433
+ *
434
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
435
+ * @param request - The configuration object for the HTTP DELETE request.
436
+ * @param options - Additional options to provide to the fetchSignal.
437
+ * @returns A `FetchSignal` object with the text response.
438
+ */
439
+ text: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<string, string>) => FetchSignal<string, ErrorResponse>;
440
+ /**
441
+ * Initiates a reactive HTTP DELETE request using the provided request configuration and parses the response as a Blob.
442
+ *
443
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
444
+ * @param request - The configuration object for the HTTP DELETE request.
445
+ * @param options - Additional options to provide to the fetchSignal.
446
+ * @returns A `FetchSignal` object with the Blob response.
447
+ */
448
+ blob: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<Blob, Blob>) => FetchSignal<Blob, ErrorResponse>;
449
+ /**
450
+ * Initiates a reactive HTTP DELETE request using the provided request configuration and parses the response as an ArrayBuffer.
451
+ *
452
+ * @template ErrorResponse - The expected error shape (defaults to `DefaultError`).
453
+ * @param request - The configuration object for the HTTP DELETE request.
454
+ * @param options - Additional options to provide to the fetchSignal.
455
+ * @returns A `FetchSignal` object with the ArrayBuffer response.
456
+ */
457
+ arrayBuffer: <ErrorResponse extends Json = DefaultError>(request: () => FetchSignalRequest, options?: FetchSignalOptions<ArrayBuffer, ArrayBuffer>) => FetchSignal<ArrayBuffer, ErrorResponse>;
458
+ };
459
+ };
460
+ export {};
@@ -0,0 +1,10 @@
1
+ import { Resolver } from "@trpc/client";
2
+ import { ResolverDef, TrpcResource, TrpcResourceOptions } from "./trpcResource.types";
3
+ declare function trpcResource<TDef extends ResolverDef>(procedure: Resolver<TDef>, input: () => TDef['input'], options?: TrpcResourceOptions<TDef['output']>): TrpcResource<TDef>;
4
+ declare function debugTrpcResource<TDef extends ResolverDef>(_trpcResource: TrpcResource<TDef>): {
5
+ value: TDef["output"] | undefined;
6
+ error: TDef["errorShape"] | undefined;
7
+ isLoading: boolean;
8
+ resourceError: Error | undefined;
9
+ };
10
+ export { debugTrpcResource, trpcResource };
@@ -0,0 +1,65 @@
1
+ import { Injector, Signal, ValueEqualityFn } from "@angular/core";
2
+ export type ResolverDef = {
3
+ input: any;
4
+ output: any;
5
+ transformer: boolean;
6
+ errorShape: any;
7
+ };
8
+ export type TrpcResourceOptions<TOutput> = {
9
+ /**
10
+ * Whether or not the request should be fetched reactively when the request updates.
11
+ */
12
+ autoRefresh?: boolean;
13
+ /**
14
+ * Value that the fetch signal will take when in Idle or Loading states.
15
+ *
16
+ * If not set, the fetch signal will use `undefined` as its default value.
17
+ */
18
+ defaultValue?: NoInfer<TOutput> | undefined;
19
+ /**
20
+ * The `Injector` in which to create the `FetchSignal`.
21
+ *
22
+ * If this is not provided, the current [injection context](https://angular.dev/guide/di/dependency-injection-context)
23
+ * will be used instead (via `inject`).
24
+ */
25
+ injector?: Injector | undefined;
26
+ /**
27
+ * A comparison function which defines equality for the response value.
28
+ */
29
+ equal?: ValueEqualityFn<NoInfer<TOutput | undefined>> | undefined;
30
+ };
31
+ type Expand<T> = T extends infer O ? {
32
+ [K in keyof O]: O[K];
33
+ } : never;
34
+ type _TrpcResource<TDef extends ResolverDef> = {
35
+ /**
36
+ * Signal of the tRPC procedure output, when available.
37
+ * This value will persist while the tRPCResource is loading.
38
+ */
39
+ value: Signal<TDef["output"] | undefined>;
40
+ /**
41
+ * Signal of the tRPC procedure error, when available.
42
+ */
43
+ error: Signal<TDef['errorShape'] | undefined>;
44
+ /**
45
+ * Whether this trpcResource is loading a new value (or reloading the existing one).
46
+ */
47
+ isLoading: Signal<boolean>;
48
+ /**
49
+ * Signal of any non trpc error cuaght by the trpcResource.
50
+ */
51
+ resourceError: Signal<Error | undefined>;
52
+ /**
53
+ * Instructs the trpcResource to call the procedure with the current reactive dependencies.
54
+ * @param abortSignal Optional abort signal to abort the procedure call.
55
+ * @param keepLoadingThroughAbort Whether the trpcResource should maintain a loading state after an abort.
56
+ * This is useful when another refresh is the cause of an abort so the resource appears to keep loading.
57
+ * @returns void
58
+ */
59
+ refresh: (abortSignal?: AbortSignal, keepLoadingThroughAbort?: boolean) => Promise<void>;
60
+ };
61
+ /**
62
+ * Represents the reactive result of an tRPC request.
63
+ */
64
+ export type TrpcResource<TDef extends ResolverDef> = Expand<_TrpcResource<TDef>>;
65
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fhss-web-team/frontend-utils",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
4
4
  "peerDependencies": {
5
5
  "@angular/common": "^19.2.0",
6
6
  "@angular/core": "^19.2.0"
package/public-api.d.ts CHANGED
@@ -28,5 +28,6 @@ export * from './lib/services/auth/auth.service';
28
28
  /**
29
29
  * Signals
30
30
  */
31
+ export * from './lib/signals/trpcResource/trpcResource';
31
32
  export * from './lib/signals/fetch-signal/fetch-signal';
32
33
  export * from './lib/signals/debounced/debounced';