Class RequestManager
publicimport RequestManager from '@ember-data/request';
A RequestManager provides a request/response flow in which configured handlers are successively given the opportunity to handle, modify, or pass-along a request.
interface RequestManager {
request<T>(req: RequestInfo): Future<T>;
}
For example:
import RequestManager from '@ember-data/request';
import Fetch from '@ember-data/request/fetch';
import Auth from 'ember-simple-auth/ember-data-handler';
import Config from './config';
const { apiUrl } = Config;
// ... create manager
const manager = new RequestManager();
manager.use([Auth, Fetch]);
// ... execute a request
const response = await manager.request({
url: `${apiUrl}/users`
});
Futures
The return value of manager.request
is a Future
, which allows
access to limited information about the request while it is still
pending and fulfills with the final state when the request completes.
A Future
is cancellable via abort
.
Handlers may optionally expose a ReadableStream
to the Future
for
streaming data; however, when doing so the future should not resolve
until the response stream is fully read.
interface Future<T> extends Promise<StructuredDocument<T>> {
abort(): void;
async getStream(): ReadableStream | null;
}
StructuredDocuments
A Future resolves with a StructuredDataDocument
or rejects with a StructuredErrorDocument
.
interface StructuredDataDocument<T> {
request: ImmutableRequestInfo;
response: ImmutableResponseInfo;
content: T;
}
interface StructuredErrorDocument extends Error {
request: ImmutableRequestInfo;
response: ImmutableResponseInfo;
error: string | object;
}
type StructuredDocument<T> = StructuredDataDocument<T> | StructuredErrorDocument;
request (request) Future public
Defined in ../packages/request/src/-private/manager.ts:595
- request
- RequestInfo
- returns
- Future
Issue a Request.
Returns a Future that fulfills with a StructuredDocument
use (newHandlers) Void public
Defined in ../packages/request/src/-private/manager.ts:561
- newHandlers
- Handler[]
- returns
- Void
Register handler(s) to use when a request is issued.
Handlers will be invoked in the order they are registered. Each Handler is given the opportunity to handle the request, curry the request, or pass along a modified request.
useCache (cacheHandler) Void public
Defined in ../packages/request/src/-private/manager.ts:533
- cacheHandler
- Handler[]
- returns
- Void
Register a handler to use for primary cache intercept.
Only one such handler may exist. If using the same RequestManager as the Store instance the Store registers itself as a Cache handler.