import { z } from 'zod'; import { Environment } from '../../http/environment.js'; import { ThrowableError } from '../../http/errors/throwable-error.js'; import { SerializationStyle } from '../../http/serialization/base-serializer.js'; import { RequestBuilder } from '../../http/transport/request-builder.js'; import { ContentType, HttpResponse, RequestConfig } from '../../http/types.js'; import { BaseService } from '../base-service.js'; import { User, userRequest, userResponse } from '../common/user.js'; import { UserCollection, userCollectionResponse } from './models/user-collection.js'; import { ListUsersParams } from './request-params.js'; /** * Service class for UsersService operations. * Provides methods to interact with UsersService-related API endpoints. * All methods return promises and handle request/response serialization automatically. */ export class UsersService extends BaseService { /** * Lists all users in the project. * @param {number} [params.limit] - defines the maximum number of items to return per page (default: 50) * @param {string} [params.startingAfter] - a cursor for use in pagination, points to the last ID in previous page * @param {string} [params.endingBefore] - a cursor for use in pagination, points to the first ID in next page * @param {string} [params.query] - filter users by their email address or external ID * @param {RequestConfig} [requestConfig] - The request configuration for retry and validation. * @returns {Promise>} - OK */ async listUsers(params?: ListUsersParams, requestConfig?: RequestConfig): Promise> { const request = new RequestBuilder() .setBaseUrl(requestConfig?.baseUrl || this.config.baseUrl || this.config.environment || Environment.DEFAULT) .setConfig(this.config) .setMethod('GET') .setPath('/users') .setRequestSchema(z.any()) .addAccessTokenAuth(this.config.token, 'Bearer') .setRequestContentType(ContentType.Json) .addResponse({ schema: userCollectionResponse, contentType: ContentType.Json, status: 200, }) .setRetryAttempts(this.config, requestConfig) .setRetryDelayMs(this.config, requestConfig) .setResponseValidation(this.config, requestConfig) .addQueryParam({ key: 'limit', value: params?.limit, }) .addQueryParam({ key: 'starting_after', value: params?.startingAfter, }) .addQueryParam({ key: 'ending_before', value: params?.endingBefore, }) .addQueryParam({ key: 'query', value: params?.query, }) .build(); return this.client.call(request); } /** * Creates or updates a user with the provided details. The user will be associated with the project specified in the request context. * @param {RequestConfig} [requestConfig] - The request configuration for retry and validation. * @returns {Promise>} - OK */ async saveUser(body: User, requestConfig?: RequestConfig): Promise> { const request = new RequestBuilder() .setBaseUrl(requestConfig?.baseUrl || this.config.baseUrl || this.config.environment || Environment.DEFAULT) .setConfig(this.config) .setMethod('PUT') .setPath('/users') .setRequestSchema(userRequest) .addAccessTokenAuth(this.config.token, 'Bearer') .setRequestContentType(ContentType.Json) .addResponse({ schema: userResponse, contentType: ContentType.Json, status: 200, }) .setRetryAttempts(this.config, requestConfig) .setRetryDelayMs(this.config, requestConfig) .setResponseValidation(this.config, requestConfig) .addHeaderParam({ key: 'Content-Type', value: 'application/json' }) .addBody(body) .build(); return this.client.call(request); } /** * Removes a user and all associated data from the project. * @param {string} userId - * @param {RequestConfig} [requestConfig] - The request configuration for retry and validation. * @returns {Promise>} - No Content */ async deleteUser(userId: string, requestConfig?: RequestConfig): Promise> { const request = new RequestBuilder() .setBaseUrl(requestConfig?.baseUrl || this.config.baseUrl || this.config.environment || Environment.DEFAULT) .setConfig(this.config) .setMethod('DELETE') .setPath('/users/{user_id}') .setRequestSchema(z.any()) .addAccessTokenAuth(this.config.token, 'Bearer') .setRequestContentType(ContentType.Json) .addResponse({ schema: z.undefined(), contentType: ContentType.NoContent, status: 204, }) .setRetryAttempts(this.config, requestConfig) .setRetryDelayMs(this.config, requestConfig) .setResponseValidation(this.config, requestConfig) .addPathParam({ key: 'user_id', value: userId, }) .build(); return this.client.call(request); } }