UNPKG

@yeepay/yop-typescript-sdk

Version:

TypeScript SDK for interacting with YOP (YeePay Open Platform)

54 lines (53 loc) 2.97 kB
import type { YopConfig, YopRequestOptions, ContentType, YopResponse } from "./types.js"; /** * YopClient provides methods for interacting with the repay Open Platform (YOP) API. * It handles request signing, response verification, and configuration management. * * The client can be configured either through an explicit configuration object * passed to the constructor or via environment variables. */ export declare class YopClient { private readonly config; private readonly timeout; /** * Creates an instance of YopClient. * * The constructor accepts an optional configuration object (`YopConfig`). * - If `config` is provided, it takes precedence over environment variables for configuration settings. * - If `config` is *not* provided, the client attempts to load configuration from the following * environment variables: * - `YOP_APP_KEY`: Your application key. * - `YOP_SECRET_KEY`: Your application's private key (used for signing). * - `YOP_PUBLIC_KEY`: The YeePay platform's public key (used for verifying responses). * - `YOP_API_BASE_URL`: (Optional) The base URL for the YOP API. Defaults to 'https://openapi.yeepay.com/yop-center'. * * An error will be thrown if required configuration fields (`appKey`, `secretKey`, `yopPublicKey`) * are missing, whether attempting to load from the `config` object or environment variables. * * @param config - Optional configuration object for the YOP client. * @throws {Error} If required configuration is missing. */ constructor(config?: YopConfig); /** * Loads and validates the configuration, merging provided config, environment variables, and defaults. * Implements prioritized loading for the YOP public key. * @param config Optional configuration object provided during instantiation. * @returns The validated and merged YopConfig. * @throws Error if required configuration fields (appKey, secretKey) are missing or if public key loading fails definitively. */ private _loadConfig; /** * 从证书缓冲区中提取公钥 * @param certBuffer - 证书的二进制缓冲区 * @returns 提取的 PEM 格式公钥 * @throws Error 如果无法从证书中提取公钥 */ private _extractPublicKeyFromCertBuffer; request<T extends YopResponse = YopResponse>(options: YopRequestOptions): Promise<T>; get<T extends YopResponse = YopResponse>(apiUrl: string, params: Record<string, unknown>, // Align with YopRequestOptions timeout?: number): Promise<T>; post<T extends YopResponse = YopResponse>(apiUrl: string, body: Record<string, unknown>, // Align with YopRequestOptions contentType?: ContentType, timeout?: number): Promise<T>; postJson<T extends YopResponse = YopResponse>(apiUrl: string, body: Record<string, unknown>, // Align with YopRequestOptions timeout?: number): Promise<T>; }