UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

63 lines (62 loc) 2.9 kB
import OAuthClientCredentialsGrant from "../authentication/oauth-client-credentials-grant"; import OAuthPasswordGrant from "../authentication/oauth-password-grant"; import OAuthRefreshTokenGrant from "../authentication/oauth-refreshtoken-grant"; import { MapStringTo } from "../base-types"; import { RequestMessage } from "./request-message"; import { ResponseMessage } from "./response-message"; export type CancelCallback = (cancel: (reason?: string) => void) => void; export interface IInternalClient { /** * Authenticate with the remote server. * * @remarks * Should return true when authentication succeeded. * Should return false or throw an error when authentication failed. * * @returns Promise resolving to a boolean indicating if authentication succeeded. */ authenticateAsync(): Promise<boolean>; /** * Sends a request to the remote server and returns the server's response. * @param requestGenerator - Function which returns a {@link RequestMessage} * @param cancelCallback - A {@link CancelCallback} that will be placed in an axios {@link CancelToken} if provided */ sendAsync<T = unknown, K = unknown>(requestGenerator: () => RequestMessage<T>, cancelCallback?: CancelCallback): Promise<ResponseMessage<K>>; /** * Set headers which will be send with every request. * @param headers - The headers to send with every request */ setRequestHeaders(headers: MapStringTo<string>): void; /** * Sets the error response handler. * @param onError - The callback function to handle the response error. * * @remarks * Only expected to work when authentication to the endpoint has been established without the use of an OAuth grant. */ setResponseErrorHandler(onError: (error: any) => Promise<any>): void; } export declare class InternalClient implements IInternalClient { private _accessToken; private _passwordGrant; private _refreshTokenGrant; private _clientCredentialsGrant; private _isAuthenticated; private _requestHeaders; private readonly _client; /** * Indicates if we should authenticate with OAuth. */ private get shouldAuthenticateWithOAuth(); constructor(baseUri: URI | string, oauthGrant?: OAuthPasswordGrant | OAuthRefreshTokenGrant | OAuthClientCredentialsGrant); authenticateAsync(): Promise<boolean>; setRequestHeaders(headers: MapStringTo<string>): void; /** * Sets the provided error handler as an interceptor for the responses. * @param onError - the callback that should be called on error. */ setResponseErrorHandler(onError: (error: any) => Promise<any>): void; private send_onFulfilled; private send_onRejected; sendAsync<T = any, K = any>(requestGenerator: () => RequestMessage<T>, cancelCallback?: CancelCallback): Promise<ResponseMessage<K>>; }