import { ServiceKey, type ServiceScope, SPEvent } from '@microsoft/sp-core-library'; import type { IHttpClientOptions } from '../httpClient/HttpClient'; import type { ISPHttpClientConfigurations } from './SPHttpClientConfiguration'; import type SPHttpClientConfiguration from './SPHttpClientConfiguration'; import type { IRequestCacheOptions, IHttpRequestCacheOptions } from '../caching/IRequestCacheOptions'; import type SPHttpClientResponse from './SPHttpClientResponse'; import SPHttpClientBatch, { type ISPHttpClientBatchCreationOptions } from './SPHttpClientBatch'; import type { IClientCachableResponse } from '../caching/IClientCachableResponse'; import type { BeforeFullPageRedirectEventArgs, PopupRequestEventArgs } from './ISPCookieRefresher'; /** * SPHttpClient is used to perform REST calls against SharePoint. It adds default * headers, manages the digest needed for writes, and collects telemetry that * helps the service to monitor the performance of an application. * * @remarks * For communicating with other internet services, use the {@link HttpClient} class. * * @public * @sealed */ export default class SPHttpClient { /** * The standard predefined SPHttpClientConfiguration objects for use with * the SPHttpClient class. */ static readonly configurations: ISPHttpClientConfigurations; /** * The service key for SPHttpClient. */ static readonly serviceKey: ServiceKey; private static _logSource; private static _onBeforeRedirectEventId; private static _onPopupRequestedEventId; readonly onBeforeRedirectEvent: SPEvent; readonly onPopupRequestedEvent: SPEvent; private _digestCache; private _parentSource; private _serviceScope; private _fetchProvider; private _cacheProvider; private _isNavigate; private _prefetchProvider; private _cookieRefresher; /** * Use a heuristic to infer the base URL for authentication. * * @remarks * Attempts to infer the SPWeb URL associated with the provided REST URL, by looking * for common SharePoint path components such as "_api", "_layouts", or "_vit_bin". * This is necessary for operations such as the X-RequestDigest * and ODATA batching, which require POSTing to a separate REST endpoint * in order to complete a request. * * For example, if the requestUrl is "/sites/site/web/_api/service", * the returned URL would be "/sites/site/web". Or if the requestUrl * is "http://example.com/_layouts/service", the returned URL would be * "http://example.com". * * If the URL cannot be determined, an exception is thrown. * * @param requestUrl - The URL for a SharePoint REST service * @returns the inferred SPWeb URL */ static getWebUrlFromRequestUrl(requestUrl: string): string; constructor(serviceScope: ServiceScope); get isNavigate(): boolean; set isNavigate(isNavigate: boolean); /** * Perform a REST service call. * * @remarks * Generally, the parameters and semantics for SPHttpClient.fetch() are essentially * the same as the WHATWG API standard that is documented here: * https://fetch.spec.whatwg.org/ * * The SPHttpClient subclass adds some additional behaviors that are convenient when * working with SharePoint ODATA API's (which can be avoided by using * HttpClient instead): * * - Default "Accept" and "Content-Type" headers are added if not explicitly specified. * * - For write operations, an "X-RequestDigest" header is automatically added * * - The request digest token is automatically fetched and stored in a cache, with * support for preloading * * For a write operation, SPHttpClient will automatically add the "X-RequestDigest" * header, which may need to be obtained by issuing a separate request such as * "https://example.com/sites/sample/_api/contextinfo". Typically the appropriate * SPWeb URL can be guessed by looking for a reserved URL segment such as "_api" * in the original URL passed to fetch(); if not, use ISPHttpClientOptions.webUrl * to specify it explicitly. * * @param url - the URL to fetch * @param configuration - determines the default behavior of SPHttpClient; normally this should * be the latest version number from SPHttpClientConfigurations * @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. * * @public */ fetch(url: string, configuration: SPHttpClientConfiguration, options: ISPHttpClientOptions): Promise; /** * Perform a REST service call. * * @remarks * Generally, the parameters and semantics for SPHttpClient.fetch() are essentially * the same as the WHATWG API standard that is documented here: * https://fetch.spec.whatwg.org/ * * The SPHttpClient subclass adds some additional behaviors that are convenient when * working with SharePoint ODATA API's (which can be avoided by using * HttpClient instead): * * - Default "Accept" and "Content-Type" headers are added if not explicitly specified. * * - For write operations, an "X-RequestDigest" header is automatically added * * - The request digest token is automatically fetched and stored in a cache, with * support for preloading * * For a write operation, SPHttpClient will automatically add the "X-RequestDigest" * header, which may need to be obtained by issuing a separate request such as * "https://example.com/sites/sample/_api/contextinfo". Typically the appropriate * SPWeb URL can be guessed by looking for a reserved URL segment such as "_api" * in the original URL passed to fetch(); if not, use ISPHttpClientOptions.webUrl * to specify it explicitly. * * @param url - the URL to fetch * @param configuration - determines the default behavior of SPHttpClient; normally this should * be the latest version number from SPHttpClientConfigurations * @param options - additional options that affect the request * @param cacheConfiguration - determines the configuration for cache management of this request * @returns A promise containing a IClientCachableResponse, which contains a * Promise to the cached data response and a Promise to the server response * * @internal */ fetch(url: string, configuration: SPHttpClientConfiguration, options: ISPHttpClientOptions, cacheConfiguration?: IRequestCacheOptions): Promise>; /** * Perform a REST service call. * * @remarks * Generally, the parameters and semantics for SPHttpClient.fetch() are essentially * the same as the WHATWG API standard that is documented here: * https://fetch.spec.whatwg.org/ * * The SPHttpClient subclass adds some additional behaviors that are convenient when * working with SharePoint ODATA API's (which can be avoided by using * HttpClient instead): * * - Default "Accept" and "Content-Type" headers are added if not explicitly specified. * * - For write operations, an "X-RequestDigest" header is automatically added * * - The request digest token is automatically fetched and stored in a cache, with * support for preloading * * For a write operation, SPHttpClient will automatically add the "X-RequestDigest" * header, which may need to be obtained by issuing a separate request such as * "https://example.com/sites/sample/_api/contextinfo". Typically the appropriate * SPWeb URL can be guessed by looking for a reserved URL segment such as "_api" * in the original URL passed to fetch(); if not, use ISPHttpClientOptions.webUrl * to specify it explicitly. * * @param url - the URL to fetch * @param configuration - determines the default behavior of SPHttpClient; normally this should * be the latest version number from SPHttpClientConfigurations * @param options - additional options that affect the request * @param cacheConfiguration - determines the configuration for cache management of this request * @returns A Promise to the cached data response or a Promise to the server response * * @internal */ fetch(url: string, configuration: SPHttpClientConfiguration, options: ISPHttpClientOptions, cacheConfiguration?: IHttpRequestCacheOptions): Promise; /** * Calls fetch(), but sets the method to "GET". * * @param url - the URL to fetch * @param configuration - determines the default behavior of SPHttpClient; normally this should * be the latest version number from SPHttpClientConfigurations * @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. * * @public */ get(url: string, configuration: SPHttpClientConfiguration, options?: ISPHttpClientOptions): Promise; /** * Calls fetch(), but sets the method to "GET". * * @param url - the URL to fetch * @param configuration - determines the default behavior of SPHttpClient; normally this should * be the latest version number from SPHttpClientConfigurations * @param options - additional options that affect the request * @param cacheConfiguration - determines the configuration for cache management of this request * @returns A promise containing a IClientCachableResponse, which contains a * Promise to the cached data response and a Promise to the server response * * @internal */ get(url: string, configuration: SPHttpClientConfiguration, options: ISPHttpClientOptions | undefined, cacheConfiguration?: IRequestCacheOptions): Promise>; /** * Calls fetch(), but sets the method to "GET". * * @param url - the URL to fetch * @param configuration - determines the default behavior of SPHttpClient; normally this should * be the latest version number from SPHttpClientConfigurations * @param options - additional options that affect the request * @param cacheConfiguration - determines the configuration for cache management of this request * @returns A Promise to the cached data response or a Promise to the server response * * @internal */ get(url: string, configuration: SPHttpClientConfiguration, options: ISPHttpClientOptions | undefined, cacheConfiguration?: IHttpRequestCacheOptions): Promise; /** * Calls fetch(), but sets the method to "POST". * * @param url - the URL to fetch * @param configuration - determines the default behavior of SPHttpClient; normally this should * be the latest version number from SPHttpClientConfigurations * @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: SPHttpClientConfiguration, options: ISPHttpClientOptions): Promise; /** * Begins an ODATA batch, which allows multiple REST queries to be bundled into * a single web request. * * @returns An {@link SPHttpClientBatch} object used to manage the batch operation. * * @beta */ beginBatch(batchCreationOptions?: ISPHttpClientBatchCreationOptions): SPHttpClientBatch; protected _fetch(url: string, configuration: SPHttpClientConfiguration, options: ISPHttpClientOptions): Promise; /** * Gets the cache data provider */ private get _cacheDataProvider(); private get _logSourceId(); /** * Gets the prefetch data provider */ private get _prefetchDataProvider(); } /** * This interface defines the options for the SPHttpClient operations such as * get(), post(), fetch(), etc. It is based on the WHATWG API standard * parameters that are documented here: * https://fetch.spec.whatwg.org/ * * @public */ export interface ISPHttpClientOptions extends IHttpClientOptions { /** * Configure the SPWeb URL for authentication. * * @remarks * For a write operation, SPHttpClient will automatically add the * "X-RequestDigest" header, which may need to be fetched using a seperate * request such as "https://example.com/sites/sample/_api/contextinfo". * 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 original REST query, however certain REST endpoints * do not contain a reserved URL segment; in this case, the webUrl can * be explicitly specified using this option. */ webUrl?: string; } //# sourceMappingURL=SPHttpClient.d.ts.map