@letsparky/api-v2-client
Version:
TypeScript client for the LetsParky API V2
134 lines (133 loc) • 5.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Memberships = void 0;
const validators_1 = require("./validators");
/**
* Client for managing memberships
* Handles membership creation, subscription management, and device associations
*/
class Memberships {
constructor(client) {
this.client = client;
}
/**
* Get all public memberships with optional filtering
* @param filters Optional filters for memberships
* @returns Promise with list of public memberships
*/
async getPublic(filters) {
const searchParams = new URLSearchParams();
if (filters === null || filters === void 0 ? void 0 : filters.name)
searchParams.append('name', filters.name);
if (filters === null || filters === void 0 ? void 0 : filters.page)
searchParams.append('page', filters.page.toString());
if (filters === null || filters === void 0 ? void 0 : filters.limit)
searchParams.append('limit', filters.limit.toString());
const queryString = searchParams.toString();
const url = `/memberships/public${queryString ? `?${queryString}` : ''}`;
return this.client.get(url);
}
/**
* Get all memberships with optional filtering (authenticated)
* @param filters Optional filter parameters
* @returns Promise with list of memberships
*/
async list(filters) {
return this.client.get('/memberships', filters);
}
/**
* Get a paginated list of memberships with pagination controls (authenticated)
* @param paginationParams Pagination parameters (page, limit, sort, order)
* @param filters Optional filter parameters
* @returns Promise with paginated memberships
*/
async listPaginated(paginationParams, filters) {
return this.client.getPaginated('/memberships', paginationParams, filters);
}
/**
* Create a new membership
* @param params The membership creation parameters
* @returns Promise with the created membership
*/
async create(params) {
validators_1.Validators.validateMembershipCreate(params);
return this.client.post('/memberships', params);
}
/**
* Get user's membership subscriptions
* @returns Promise with list of user's subscriptions
*/
async getMySubscriptions() {
return this.client.get('/memberships/my-subscriptions');
}
/**
* Get a membership by ID with full details
* @param membershipId The ID of the membership to retrieve
* @returns Promise with the membership details
*/
async getById(membershipId) {
validators_1.Validators.validateId(membershipId, 'Membership');
return this.client.get(`/memberships/${membershipId}`);
}
/**
* Update a membership
* @param membershipId The ID of the membership to update
* @param params The membership update parameters
* @returns Promise with the updated membership
*/
async update(membershipId, params) {
validators_1.Validators.validateId(membershipId, 'Membership');
validators_1.Validators.validateMembershipUpdate(params);
return this.client.put(`/memberships/${membershipId}`, params);
}
/**
* Delete a membership
* @param membershipId The ID of the membership to delete
* @returns Promise with success confirmation
*/
async delete(membershipId) {
validators_1.Validators.validateId(membershipId, 'Membership');
return this.client.delete(`/memberships/${membershipId}`);
}
/**
* Subscribe to a membership
* @param membershipId The ID of the membership to subscribe to
* @returns Promise with subscription confirmation
*/
async subscribe(membershipId) {
validators_1.Validators.validateId(membershipId, 'Membership');
return this.client.post(`/memberships/${membershipId}/subscribe`);
}
/**
* Unsubscribe from a membership
* @param membershipId The ID of the membership to unsubscribe from
* @returns Promise with unsubscribe confirmation
*/
async unsubscribe(membershipId) {
validators_1.Validators.validateId(membershipId, 'Membership');
return this.client.delete(`/memberships/${membershipId}/unsubscribe`);
}
/**
* Add device to membership
* @param membershipId The ID of the membership
* @param deviceId The ID of the device to add
* @returns Promise with success confirmation
*/
async addDevice(membershipId, deviceId) {
validators_1.Validators.validateId(membershipId, 'Membership');
validators_1.Validators.validateId(deviceId, 'Device');
return this.client.post(`/memberships/${membershipId}/devices`, { device_id: deviceId });
}
/**
* Remove device from membership
* @param membershipId The ID of the membership
* @param deviceId The ID of the device to remove
* @returns Promise with success confirmation
*/
async removeDevice(membershipId, deviceId) {
validators_1.Validators.validateId(membershipId, 'Membership');
validators_1.Validators.validateId(deviceId, 'Device');
return this.client.delete(`/memberships/${membershipId}/devices/${deviceId}`);
}
}
exports.Memberships = Memberships;