@ember-data-mirror/request 5.6.0-beta.0 → 5.6.0-beta.2

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.
@@ -1,246 +0,0 @@
1
- declare module '@ember-data-mirror/request/-private/types' {
2
- import type { StableDocumentIdentifier } from '@warp-drive-mirror/core-types/identifier';
3
- import type { IS_FUTURE, RequestContext, RequestInfo, ResponseInfo, StructuredDataDocument } from '@warp-drive-mirror/core-types/request';
4
- /**
5
- * @module @ember-data-mirror/request
6
- */
7
- export interface GodContext {
8
- controller: AbortController;
9
- response: ResponseInfo | null;
10
- stream: ReadableStream | Promise<ReadableStream | null> | null;
11
- hasRequestedStream: boolean;
12
- id: number;
13
- identifier: StableDocumentIdentifier | null;
14
- }
15
- export type Deferred<T> = {
16
- resolve(v: T): void;
17
- reject(v: unknown): void;
18
- promise: Promise<T>;
19
- };
20
- export type ManagedRequestPriority = {
21
- blocking: boolean;
22
- };
23
- export type DeferredStream = {
24
- resolve(v: ReadableStream | null): void;
25
- reject(v: unknown): void;
26
- promise: Promise<ReadableStream | null> & {
27
- sizeHint?: number;
28
- };
29
- };
30
- /**
31
- * A Future is a Promise which resolves to a StructuredDocument
32
- * while providing the ability to `abort` the underlying request,
33
- * `getStream` the response before the outer promise resolves;
34
- *
35
- * @class Future
36
- * @extends Promise
37
- * @public
38
- */
39
- export type Future<T> = Promise<StructuredDataDocument<T>> & {
40
- [IS_FUTURE]: true;
41
- /**
42
- * Cancel this request by firing the AbortController's signal.
43
- *
44
- * @method abort
45
- * @param {String} [reason] optional reason for aborting the request
46
- * @public
47
- * @return {void}
48
- */
49
- abort(reason?: string): void;
50
- /**
51
- * Get the response stream, if any, once made available.
52
- *
53
- * @method getStream
54
- * @public
55
- * @return {Promise<ReadableStream | null>}
56
- */
57
- getStream(): Promise<ReadableStream | null>;
58
- /**
59
- * Run a callback when this request completes. Use sparingly,
60
- * mostly useful for instrumentation and infrastructure.
61
- *
62
- * @method onFinalize
63
- * @param cb the callback to run
64
- * @public
65
- * @return void
66
- */
67
- onFinalize(cb: () => void): void;
68
- /**
69
- * The identifier of the associated request, if any, as
70
- * assigned by the CacheHandler.
71
- *
72
- * @property lid
73
- * @type {StableDocumentIdentifier | null}
74
- * @public
75
- */
76
- lid: StableDocumentIdentifier | null;
77
- /**
78
- * The id of the associated request, if any, as assigned
79
- * by the RequestManager
80
- *
81
- * @property id
82
- * @type {Number}
83
- * @public
84
- */
85
- id: number;
86
- };
87
- export type DeferredFuture<T> = {
88
- resolve(v: StructuredDataDocument<T>): void;
89
- reject(v: unknown): void;
90
- promise: Future<T>;
91
- };
92
- export type NextFn<P = unknown> = (req: RequestInfo) => Future<P>;
93
- /**
94
- * Requests are fulfilled by handlers. A handler receives the request context
95
- as well as a `next` function with which to pass along a request to the next
96
- handler if it so chooses.
97
-
98
- A handler may be any object with a `request` method. This allows both stateful and non-stateful
99
- handlers to be utilized.
100
-
101
- If a handler calls `next`, it receives a `Future` which resolves to a `StructuredDocument`
102
- that it can then compose how it sees fit with its own response.
103
-
104
- ```ts
105
- type NextFn<P> = (req: RequestInfo) => Future<P>;
106
-
107
- interface Handler {
108
- async request<T>(context: RequestContext, next: NextFn<P>): T;
109
- }
110
- ```
111
-
112
- `RequestContext` contains a readonly version of the RequestInfo as well as a few methods for building up the `StructuredDocument` and `Future` that will be part of the response.
113
-
114
- ```ts
115
- interface RequestContext<T> {
116
- readonly request: RequestInfo;
117
-
118
- setStream(stream: ReadableStream | Promise<ReadableStream>): void;
119
- setResponse(response: Response | ResponseInfo): void;
120
- }
121
- ```
122
-
123
- A basic `fetch` handler with support for streaming content updates while
124
- the download is still underway might look like the following, where we use
125
- [`response.clone()`](https://developer.mozilla.org/en-US/docs/Web/API/Response/clone) to `tee` the `ReadableStream` into two streams.
126
-
127
- A more efficient handler might read from the response stream, building up the
128
- response content before passing along the chunk downstream.
129
-
130
- ```ts
131
- const FetchHandler = {
132
- async request(context) {
133
- const response = await fetch(context.request);
134
- context.setResponse(reponse);
135
- context.setStream(response.clone().body);
136
-
137
- return response.json();
138
- }
139
- }
140
- ```
141
-
142
- ### Stream Currying
143
-
144
- `RequestManager.request` and `next` differ from `fetch` in one **crucial detail** in that the outer Promise resolves only once the response stream has been processed.
145
-
146
- For context, it helps to understand a few of the use-cases that RequestManager
147
- is intended to allow.
148
-
149
- - to manage and return streaming content (such as video files)
150
- - to fulfill a request from multiple sources or by splitting one request into multiple requests
151
- - for instance one API call for a user and another for the user's friends
152
- - or e.g. fulfilling part of the request from one source (one API, in-memory, localStorage, IndexedDB etc.) and the rest from another source (a different API, a WebWorker, etc.)
153
- - to coalesce multiple requests
154
- - to decorate a request with additional info
155
- - e.g. an Auth handler that ensures the correct tokens or headers or cookies are attached.
156
-
157
- ----
158
-
159
- `await fetch(<req>)` resolves at the moment headers are received. This allows for the body of the request to be processed as a stream by application
160
- code *while chunks are still being received by the browser*.
161
-
162
- When an app chooses to `await response.json()` what occurs is the browser reads the stream to completion and then returns the result. Additionally, this stream may only be read **once**.
163
-
164
- The `RequestManager` preserves this ability to subscribe to and utilize the stream by either the application or the handler – thereby delivering the full power and flexibility of native APIs – without restricting developers in ways that lead to complicated workarounds.
165
-
166
- Each handler may call `setStream` only once, but may do so *at any time* until the promise that the handler returns has resolved. The associated promise returned by calling `future.getStream` will resolve with the stream set by `setStream` if that method is called, or `null` if that method
167
- has not been called by the time that the handler's request method has resolved.
168
-
169
- Handlers that do not create a stream of their own, but which call `next`, should defensively pipe the stream forward. While this is not required (see automatic currying below) it is better to do so in most cases as otherwise the stream may not become available to downstream handlers or the application until the upstream handler has fully read it.
170
-
171
- ```ts
172
- context.setStream(future.getStream());
173
- ```
174
-
175
- Handlers that either call `next` multiple times or otherwise have reason to create multiple fetch requests should either choose to return no stream, meaningfully combine the streams, or select a single prioritized stream.
176
-
177
- Of course, any handler may choose to read and handle the stream, and return either no stream or a different stream in the process.
178
-
179
- ### Automatic Currying of Stream and Response
180
-
181
- In order to simplify the common case for handlers which decorate a request, if `next` is called only a single time and `setResponse` was never called by the handler, the response set by the next handler in the chain will be applied to that handler's outcome. For instance, this makes the following pattern possible `return (await next(<req>)).content;`.
182
-
183
- Similarly, if `next` is called only a single time and neither `setStream` nor `getStream` was called, we automatically curry the stream from the future returned by `next` onto the future returned by the handler.
184
-
185
- Finally, if the return value of a handler is a `Future`, we curry `content` and `errors` as well, thus enabling the simplest form `return next(<req>)`.
186
-
187
- In the case of the `Future` being returned, `Stream` proxying is automatic and immediate and does not wait for the `Future` to resolve.
188
-
189
- ### Handler Order
190
-
191
- Request handlers are registered by configuring the manager via `use`
192
-
193
- ```ts
194
- const manager = new RequestManager()
195
- .use([Handler1, Handler2]);
196
- ```
197
-
198
- Handlers will be invoked in the order they are registered ("fifo", first-in first-out), and may only be registered up until the first request is made. It is recommended but not required to register all handlers at one time in order to ensure explicitly visible handler ordering.
199
-
200
-
201
- @class <Interface> Handler
202
- @public
203
- */
204
- export interface Handler {
205
- /**
206
- * Method to implement to handle requests. Receives the request
207
- * context and a nextFn to call to pass-along the request to
208
- * other handlers.
209
- *
210
- * @method request
211
- * @public
212
- * @param context
213
- * @param next
214
- */
215
- request<T = unknown>(context: RequestContext, next: NextFn<T>): Promise<T | StructuredDataDocument<T>> | Future<T>;
216
- }
217
- /**
218
- * The CacheHandler is identical to other handlers ecxept that it
219
- * is allowed to return a value synchronously. This is useful for
220
- * features like reducing microtask queueing when de-duping.
221
- *
222
- * A RequestManager may only have one CacheHandler, registered via
223
- * `manager.useCache(CacheHandler)`.
224
- *
225
- * @class <Interface> CacheHandler
226
- * @public
227
- */
228
- export interface CacheHandler {
229
- /**
230
- * Method to implement to handle requests. Receives the request
231
- * context and a nextFn to call to pass-along the request to
232
- * other handlers.
233
- *
234
- * @method request
235
- * @public
236
- * @param context
237
- * @param next
238
- */
239
- request<T = unknown>(context: RequestContext, next: NextFn<T>): Promise<T | StructuredDataDocument<T>> | Future<T> | T;
240
- }
241
- export interface RequestResponse<T> {
242
- result: T;
243
- }
244
- export type GenericCreateArgs = Record<string | symbol, unknown>;
245
- }
246
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/-private/types.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,mCAAmC,CAAC;AAClF,OAAO,KAAK,EACV,SAAS,EACT,cAAc,EACd,WAAW,EACX,YAAY,EACZ,sBAAsB,EACvB,MAAM,gCAAgC,CAAC;AAExC;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,eAAe,CAAC;IAC5B,QAAQ,EAAE,YAAY,GAAG,IAAI,CAAC;IAC9B,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC/D,kBAAkB,EAAE,OAAO,CAAC;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,wBAAwB,GAAG,IAAI,CAAC;CAC7C;AAED,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;IACxB,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC;IACpB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IACzB,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IAAE,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAC;AAE3D,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,CAAC,CAAC,EAAE,cAAc,GAAG,IAAI,GAAG,IAAI,CAAC;IACxC,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IACzB,OAAO,EAAE,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,GAAG;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACjE,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,M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;;GAUG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;;;;;;OASG;IACH,OAAO,CAAC,CAAC,GAAG,OAAO,EACjB,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,GACd,OAAO,CAAC,CAAC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;CAC3D;AAED,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,MAAM,EAAE,CAAC,CAAC;CACX;AAED,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC"}
@@ -1,18 +0,0 @@
1
- declare module '@ember-data-mirror/request/-private/utils' {
2
- import type { RequestInfo, StructuredDataDocument } from '@warp-drive-mirror/core-types/request';
3
- import { ContextOwner } from '@ember-data-mirror/request/-private/context';
4
- import type { DeferredFuture, Future, GodContext, Handler } from '@ember-data-mirror/request/-private/types';
5
- export const IS_CACHE_HANDLER: "___(unique) Symbol(IS_CACHE_HANDLER)";
6
- export function curryFuture<T>(owner: ContextOwner, inbound: Future<T>, outbound: DeferredFuture<T>): Future<T>;
7
- export interface HttpErrorProps extends DOMException {
8
- code: number;
9
- name: string;
10
- status: number;
11
- statusText: string;
12
- isRequestError: boolean;
13
- }
14
- export function enhanceReason(reason?: string): DOMException;
15
- export function handleOutcome<T>(owner: ContextOwner, inbound: Promise<T | StructuredDataDocument<T>>, outbound: DeferredFuture<T>): Future<T>;
16
- export function executeNextHandler<T>(wares: Readonly<Handler[]>, request: RequestInfo, i: number, god: GodContext): Future<T>;
17
- }
18
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/-private/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,WAAW,EACX,sBAAsB,EAGvB,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAA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AAI,EAAE,MAAM,CAAC;CACd;AACD,UAAU,QAAQ;IAChB,OAAO,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC;IACrC,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,EAAE,eAAe,CAAC;CAC1B;AACD,OAAO,CAAC,MAAM,CAAC;IACb,MAAM,QAAQ,EAAE,SAAS,GAAG,QAAQ,CAAC;CACtC;AA6ED;;;;;;;;;;;;GAYG;AACH,QAAA,MAAM,KAAK;YACK,CAAC,WAAW,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;CAgJ/C,CAAC;AAMF,eAAe,KAAK,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,IAAI,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC9E,YAAY,EACV,cAAc,EACd,oBAAoB,EACpB,WAAW,EACX,YAAY,EACZ,kBAAkB,EAClB,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC9E,YAAY,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC"}