UNPKG

sfcc-cip-analytics-client

Version:

SFCC Commerce Intelligence Platform Analytics Client

110 lines (109 loc) 5.06 kB
import { IConnectionProperties, IPrepareResponse, IExecuteResponse, IStatementHandle } from "./protocol"; import { NormalizedExecuteResponse, NormalizedFetchResponse } from "./normalized-types"; import { Logger } from "./logger"; export interface CIPClientOptions { logger?: Logger; } export declare class CIPClient { private readonly clientId; private readonly clientSecret; private readonly instance; private readonly serverUrl; private readonly logger; private root; private connectionId; private tokenInfo; private sessionId; constructor(clientId: string, clientSecret: string, instance: string, options?: CIPClientOptions); /** * Initializes the protobuf schemas. Called automatically on first request. */ private ensureInitialized; /** * Ensures we have a valid access token, refreshing if necessary. */ private ensureValidToken; /** * Opens a new connection to the Avatica server. * @param info Connection properties (e.g., user, password, schema). */ openConnection(info?: IConnectionProperties): Promise<void>; /** * Closes the existing connection. */ closeConnection(): Promise<void>; /** * Creates a new statement for the current connection. * @returns The ID of the newly created statement. */ createStatement(): Promise<number>; /** * Closes an existing statement. * @param statementId The ID of the statement to close. */ closeStatement(statementId: number): Promise<void>; /** * Prepares and executes a SQL query in a single step. * @param statementId The ID of the statement. * @param sql The SQL query to execute. * @param maxRowCount The maximum number of rows to return in the first frame (-1 for all). * @returns The full execution response, including the first frame of results with normalized offset values. */ execute(statementId: number, sql: string, maxRowCount?: number): Promise<NormalizedExecuteResponse>; /** * Fetches the next frame of results for a query. * @param statementId The statement ID. * @param offset The starting row offset for the new frame. * @param fetchMaxRowCount The maximum number of rows for this frame. * @returns The fetch response containing the next frame with normalized offset values. */ fetch(statementId: number, offset: number, fetchMaxRowCount: number): Promise<NormalizedFetchResponse>; /** * Prepares a SQL statement and returns metadata including parameter information. * @param sql The SQL query to prepare (with ? placeholders for parameters). * @param maxRowsTotal The maximum number of rows that will be allowed for this query (-1 for no limit). * @returns The prepare response containing statement metadata. */ prepare(sql: string, maxRowsTotal?: number): Promise<IPrepareResponse>; /** * Executes a prepared statement with parameters. * @param statementHandle The statement handle returned from prepare(). * @param parameters Array of parameter values to bind to the prepared statement. * @param firstFrameMaxSize The maximum number of rows to return in the first frame (-1 for no limit). * @returns The execution response containing results with normalized offset values. */ executeWithParameters(statementHandle: IStatementHandle, parameters?: any[], firstFrameMaxSize?: number): Promise<IExecuteResponse>; /** * Convenience method to prepare and execute a statement with parameters in one call. * @param sql The SQL query with ? placeholders. * @param parameters Array of parameter values. * @param firstFrameMaxSize The maximum number of rows to return in the first frame. * @returns The execution response containing results. */ prepareAndExecuteWithParameters(sql: string, parameters?: any[], firstFrameMaxSize?: number): Promise<IExecuteResponse>; /** * A generic method to serialize, send, and deserialize Avatica messages. * @param requestTypeName The short name of the request message type (e.g., "OpenConnectionRequest"). * @param payload The JavaScript object for the request payload. * @returns A promise that resolves to the deserialized response payload. */ private sendRequest; /** * Normalizes protobuf Long values to JavaScript numbers. * @param value The value that might be a protobuf Long * @returns A JavaScript number */ private normalizeLongValue; /** * Normalizes frame data to ensure offset and other numeric fields are plain numbers. * @param frame The frame object to normalize * @returns The frame with normalized numeric values */ private normalizeFrame; /** * Converts JavaScript values to Avatica TypedValue format for prepared statement parameters. * @param value The JavaScript value to convert * @returns A TypedValue object suitable for Avatica protocol */ private createTypedValue; }