import { HttpResponseModel, ODataHttpClient, ODataHttpMethods, ODataRequestConfig } from "@odata2ts/http-client-api"; import { MainResponseConverter } from "@odata2ts/odata-query-objects"; import { MainRequestConverter, RequestConverter } from "./converter/RequestConverter"; import { ResponseConverter } from "./converter/ResponseConverter"; import { NoInferConfig } from "./NoInferConfig"; import { RequestInfo } from "./RequestInfo"; /** * What a command needs in order to take part in optimistic concurrency control. * * Filled in by the service which creates the command, because only that service knows which resource is * addressed, whether it is under concurrency control, and how to read an ETag out of a response body of * its own OData version. */ export interface ConcurrencyOptions { /** The resource this command addresses; the key its ETag is stored under. */ key: string; /** Whether the service states `Core.OptimisticConcurrency` for this resource. */ controlled: boolean; /** * Every (key, ETag) pair the response body yields: at most one for an entity, one per row for a * collection. Left out where the body cannot carry one. */ harvest?: (data: any) => Array<[string, string]>; } export interface RequestCmdOptions { /** * Set headers for the request. */ headers?: Record; /** * Optimistic concurrency control for this request - see {@link ConcurrencyOptions}. */ concurrency?: ConcurrencyOptions; /** * Sets the main request converter which converts from the user facing model * to the OData facing model. */ mainRequestConverter?: MainRequestConverter; /** * Sets the main response converter which converts from the OData facing model * to the user facing model. */ mainResponseConverter?: MainResponseConverter; } /** * Encapsulates an HTTP request to the OData server. Follows the Command Pattern. */ export declare abstract class RequestCmd { protected client: ODataHttpClient; protected method: ODataHttpMethods; protected data?: DataStructure | undefined; protected options: RequestCmdOptions; private readonly requestConverter; private readonly responseConverter; /** * An ETag the caller stated, or `"*"` where the caller chose to write past whatever is current. Set * through the write command classes; when present it wins over the store. */ protected etagOverride?: string; constructor(client: ODataHttpClient, method: ODataHttpMethods, data?: DataStructure | undefined, options?: RequestCmdOptions); /** * The unchanging URL of this command object. */ abstract getUrl(): string; /** * Get information about the request. * The data (if any) is presented with user facing typings. */ getInfo(): RequestInfo; /** * Get base information about the request. * All request converters get applied. * * With regard to data (if any), it gets converted from the user facing model * to the OData facing model, for which we don't have any typings, so we use `any`. * */ getInfoConverted(): RequestInfo; /** * Add a new request converter at the beginning of the converter chain. * This converter can then handle the user facing data structures. * * @param converter * @returns itself in builder fashion */ prependRequestConverter(converter: RequestConverter): this; /** * Add a new request converter at the end of the converter chain. * This converter can then handle the OData facing data structures. * Since we don't have any typings for this we use `any`. * * @param converter * @returns itself in builder fashion */ appendRequestConverter(converter: RequestConverter): this; /** * Add a response converter to the beginning of the converter chain. * This converter can then handle the data structures as they are returned from OData. * Since we don't have any typings for this we use `any`. * * @param converter * @returns itself in builder fashion */ prependResponseConverter(converter: ResponseConverter): this; /** * Add a response converter to the end of the converter chain. * This converter can then handle the data structures as they are returned for the user, * so with mapped property names and converted types. * * As the appended converter changes the final response structure, it is essential that * you follow the builder pattern to maintain the correct typings. * * @param converter * @returns itself in builder fashion */ appendResponseConverter(converter: ResponseConverter): RequestCmd; /** * The first half of {@link execute}: the request converters and the concurrency rule, returning the * request as it will be sent. * * Split out so a batch can run the same pipeline once per collected command before anything is sent. * Throws {@link ODataConcurrencyError} where a controlled write has no known ETag - for a direct request * that is before the send, and for a batch before the whole thing goes out, so a doomed batch is refused * rather than sent. */ prepareRequest(): RequestInfo; /** * The second half of {@link execute}: the response converters and the concurrency harvesting, run * against one answer and returning it. * * A direct request only ever sees a 2xx answer - the client throws everything else - so the conversion * and harvesting run exactly as before. A batch hands non-2xx answers back as values: those are returned * unconverted and nothing is harvested, because that sub-request did not happen. * * `request` is the prepared request {@link prepareRequest} returned. */ handleResponse(request: RequestInfo, response: HttpResponseModel): HttpResponseModel; /** * Main method of this command object: Executes the request. * * The config type defaults to what every HTTP client understands - headers and URL params. Anything a * specific client adds on top of that is opted into by naming its config type, e.g. * `execute({ credentials: "include" })`. * * @param requestConfig optional configuration */ execute(requestConfig?: NoInferConfig): Promise>; /** * Adds the `If-Match` header a write to a concurrency-controlled resource requires (OData V4.01 Part 1, * ยง8.3.1), or refuses the write where nothing is known: the service would answer `428` and change * nothing, so there is nothing to gain from sending it. */ private applyConcurrency; /** * Forgets an ETag the service has just disproved. * * A `412` is the one answer that says something about the stored ETag itself: the resource has moved on, * so the token is stale for good. Keeping it would send the very same `If-Match` on the next write and * earn the very same `412` - silently, forever, until something happens to re-read the resource. After * eviction that next write instead hits the pre-send {@link ODataConcurrencyError} ("nothing known, read * it first"), which names the resource and says what to do about it. * * No other status evicts: a `404`, a `500` or a broken connection say nothing about whether the token * was still current, and throwing it away would turn a passing write into a needless error. * * The error itself travels on untouched - resolving a conflict is the application's business. */ private evictOnConflict; /** Drop the stored ETag of the resource this command addresses, if one is under concurrency control. */ private evictConcurrency; /** * Keeps the ETag store in step with what the service just said. * * A read stores what it learned. A write takes the new ETag where the service handed one back and * forgets the old one otherwise - keeping it would fail the next write with a `412` although nobody * else had touched the resource. A delete forgets it either way. */ private updateConcurrency; /** * Hands the prepared request over to the HTTP client. * * Overridden by commands whose payload cannot travel the generic JSON path: binary data needs the * client's dedicated blob operations, which pass the body through untouched and read the response as * binary instead of parsing it as JSON. * * @param request the request with all request converters applied * @param requestConfig optional configuration */ protected sendRequest(request: RequestInfo, requestConfig?: ODataRequestConfig): Promise>; private convertResponse; }