/** * Copyright (c) Microsoft Corporation. All rights reserved. */ import { type ServiceScope } from '@microsoft/sp-core-library'; import SPHttpClientResponse from './SPHttpClientResponse'; import type { IHttpClientOptions } from '../httpClient/HttpClient'; import type { ISPHttpClientBatchConfigurations } from './SPHttpClientBatchConfiguration'; import type SPHttpClientBatchConfiguration from './SPHttpClientBatchConfiguration'; /** * The SPHttpClientBatch class accumulates a number of REST service calls and * transmits them as a single ODATA batch. This protocol is documented here: * http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part1-protocol.html * * The usage is to call SPHttpClientBatch.fetch() to queue each individual request, * and then call SPHttpClientBatch.execute() to execute the batch operation. * The execute() method returns a promise that resolves when the real REST * call has completed. Each call to fetch() also returns a promise that will * resolve with an SPHttpClientResponse object for that particular request. * * @privateRemarks * The type signature of SPHttpClientBatch class suggests that it should inherit from * the HttpClient base class. However, the operational semantics are different * (e.g. nothing happens until execute() is called; further operations are * prohibited afterwards; fetch() calls cannot depend on each other). In the * future we might introduce a base class for batches, but it would be separate * from the HttpClient hierarchy. By contrast, the ISPHttpClientBatchOptions * does naturally inherit from IHttpClientOptions. * * @beta */ export default class SPHttpClientBatch { /** * The standard predefined SPHttpClientBatchConfigurations objects for use with * the SPHttpClientBatch class. */ static readonly configurations: ISPHttpClientBatchConfigurations; private _fetchProvider; private _randomNumberGenerator; private _digestCache; private _batchedRequests; private _correlationId; private _batchResponseBody; private _webUrl; /** * Generally third parties should not use this constructor. * The recommended way to start a batch is by calling SPHttpClient.beginBatch(). * @internal */ constructor(serviceScope: ServiceScope, batchCreationOptions: ISPHttpClientBatchCreationOptions); /** * Get size of curent batched requests. * @returns count of batched requests. * @internal */ batchedSize(): number; /** * Queues a new request, and returns a promise that can be used to access * the server response (after execute() has completed). * * @remarks * The parameters for this function are basically the same as the WHATWG API standard * documented here: * * {@link https://fetch.spec.whatwg.org/ } * * However, be aware that certain REST headers are ignored or not allowed inside * a batch. See the ODATA documentation for details. * * When execute() is called, it will POST to a URL such as * "http://example.com/sites/sample/_api/$batch". Typically SPHttpClientBatch can successfully * guess the appropriate SPWeb URL by looking for a reserved URL segment such as "_api" * in the first URL passed to fetch(). If not, use ISPHttpClientBatchCreationOptions.webUrl to specify it * explicitly. * * @param url - the URL to fetch * @param configuration - determines the default behavior of this request; normally this should * be the latest version number from SPHttpClientBatchConfigurations * @param options - additional options that affect the request * * @returns A promise with behavior similar to WHATWG fetch(). This promise will resolve normally * (with {@link HttpClientResponse.ok} being false) for error status codes such as HTTP 404 * or 500. The promise will only reject for network failures or other errors that prevent communication * with the server. */ fetch(url: string, configuration: SPHttpClientBatchConfiguration, options?: ISPHttpClientBatchOptions): Promise; /** * Calls fetch(), but sets the method to 'GET'. * @param url - the URL to fetch * @param configuration - determines the default behavior of this request; normally this should * be the latest version number from SPHttpClientBatchConfigurations * @param options - additional options that affect the request * @returns A promise with behavior similar to WHATWG fetch(). This promise will resolve normally * (with {@link HttpClientResponse.ok} being false) for error status codes such as HTTP 404 * or 500. The promise will only reject for network failures or other errors that prevent communication * with the server. */ get(url: string, configuration: SPHttpClientBatchConfiguration, options?: ISPHttpClientBatchOptions): Promise; /** * Calls fetch(), but sets the method to 'POST'. * @param url - the URL to fetch * @param configuration - determines the default behavior of this request; normally this should * be the latest version number from SPHttpClientBatchConfigurations * @param options - additional options that affect the request * @returns A promise with behavior similar to WHATWG fetch(). This promise will resolve normally * (with {@link HttpClientResponse.ok} being false) for error status codes such as HTTP 404 * or 500. The promise will only reject for network failures or other errors that prevent communication * with the server. */ post(url: string, configuration: SPHttpClientBatchConfiguration, options: ISPHttpClientBatchOptions): Promise; /** * Executes the batched queries that were queued using SPHttpClientBatch.fetch(). */ execute(): Promise; /** * Returns the correlation id of the most recent batch request if execute() was able to get a response * from SharePoint. * @internal */ correlationId(): string | undefined; /** * Returns the original batch response body if execute() was able to get a response and the respones * status is success. * @internal */ batchResponseBody(): string | undefined; private _parseResponsesFromBody; } /** * This interface is passed to the SPHttpClientBatch constructor. It specifies options * that affect the entire batch. * * @beta */ export interface ISPHttpClientBatchCreationOptions { /** * SPHttpClientBatch will need to perform its POST to an endpoint such as * "http://example.com/sites/sample/_api/$batch". Typically the SPWeb URL * ("https://example.com/sites/sample" in this example) can be guessed by * looking for a reserved URL segment such as "_api" in the first URL * passed to fetch(), but if not, the webUrl can be explicitly specified * using this option. */ webUrl?: string; } /** * This interface defines the options for an individual REST request that * is part of an SPHttpClientBatch. It is based on the WHATWG API standard * parameters that are documented here: * https://fetch.spec.whatwg.org/ * * @beta */ export interface ISPHttpClientBatchOptions extends IHttpClientOptions { } //# sourceMappingURL=SPHttpClientBatch.d.ts.map