UNPKG

@paritydeals/node-sdk

Version:

Node.js SDK for interacting with the ParityDeals API.

91 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubscriptionOperations = void 0; const constants_1 = require("./constants"); const exceptions_1 = require("./exceptions"); // For potential client-side validation /** * Handles 'subscription' related API operations. * This class is instantiated by the ParityDeals client and accessed * via `client.subscription`. All methods are asynchronous. */ class SubscriptionOperations { constructor(client) { this.client = client; this.client.logger.debug("SubscriptionOperations initialized."); } /** * Cancels an active subscription. * @param params - Parameters for cancelling a subscription, matching CancelSubscriptionPayload. * - subscriptionId: string - The ID of the subscription to cancel (used in URL path). * - cancellationType: CancellationType - When the cancellation should take effect. * - cancellationDate?: string | null - Required if cancellationType is "SPECIFIC_DATE" (YYYY-MM-DD). * @returns A Promise resolving to CancelSubscriptionResponse. */ async cancel(params // Merge with payload type for body parameters ) { this.client.logger.info(`Initiating cancel subscription for subscriptionId: ${params.subscriptionId}, type: ${params.cancellationType}`); // Client-side validation for cancellationDate based on cancellationType if (params.cancellationType === 'SPECIFIC_DATE' && !params.cancellationDate) { const errMsg = "cancellationDate is required when cancellationType is 'SPECIFIC_DATE'."; this.client.logger.warn(errMsg); // Throw an error that the user of the SDK can catch throw new exceptions_1.InvalidRequestError(errMsg, undefined, [{ type: 'value_error', // Mimic Pydantic-like error structure loc: ['cancellationDate'], msg: errMsg, input: params, }]); } if (params.cancellationType !== 'SPECIFIC_DATE' && params.cancellationDate) { this.client.logger.warn("cancellationDate provided but cancellationType is not 'SPECIFIC_DATE'. It might be ignored by the API or cause an error."); // Depending on API behavior, you might choose to clear params.cancellationDate or let API handle it. } // Construct the payload for the request body from the params object // Exclude subscriptionId as it's a path parameter. const payloadForBody = { cancellationType: params.cancellationType, cancellationDate: params.cancellationDate, }; const endpointModulePath = `${constants_1.SUBSCRIPTION_MODULE_PREFIX}/${params.subscriptionId}/cancel/`; this.client.logger.debug(`Cancel subscription payload (camelCase, before client processing): ${JSON.stringify(payloadForBody)}`); // The client._request method will handle converting payloadForBody keys to snake_case return this.client._request("POST", endpointModulePath, payloadForBody // Pass the constructed payload for the body ); } /** * Updates an existing subscription. * @param params - Parameters for updating a subscription, matching UpdateSubscriptionPayload plus subscriptionId. * - subscriptionId: string - The ID of the subscription to update (used in URL path). * - planIdentifier: string - Identifier of the new plan. * - chargePeriod: ChargePeriod - The new charging period. * - probationBehaviour?: ProbationBehaviour | null - Optional probation behaviour. * - offeringId?: string | null - Optional ID of the new offering. * - pricingTableId?: string | null - Optional ID of the new pricingTable. * - ruleId?: string | null - Optional ID of the new pricing rule. * - features?: FeatureListItem[] | null - Optional list of features and their quantities. * - ipAddress?: string | null - Optional IP address of the customer. * @returns A Promise resolving to UpdateSubscriptionResponse. */ async update(params // Merge with payload type for body parameters ) { this.client.logger.info(`Initiating update subscription for subscriptionId: ${params.subscriptionId}`); // Construct the payload for the request body from the params object // Exclude subscriptionId as it's a path parameter. const payloadForBody = { planIdentifier: params.planIdentifier, chargePeriod: params.chargePeriod, probationBehaviour: params.probationBehaviour, offeringId: params.offeringId, pricingTableId: params.pricingTableId, ruleId: params.ruleId, features: params.features, ipAddress: params.ipAddress, }; const endpointModulePath = `${constants_1.SUBSCRIPTION_MODULE_PREFIX}/${params.subscriptionId}/update/`; this.client.logger.debug(`Update subscription payload (camelCase, before client processing): ${JSON.stringify(payloadForBody)}`); return this.client._request("POST", endpointModulePath, payloadForBody // Pass the constructed payload for the body ); } } exports.SubscriptionOperations = SubscriptionOperations; //# sourceMappingURL=subscription.js.map