Source: resources/files.js

/**
 * Files Resource
 * Generated: 2025-04-01T19:44:53.325Z
 */

export class FilesResource {
  /**
   * Create a new Files resource
   * @param {ApiClient} client - API client instance
   */
  constructor(client) {
    this.client = client;
    this.resourcePath = 'files'; // Preserve the exact path name

    // Make QueryChain methods available at the resource level
    this.then = (resolve, reject) => this.find().execute().then(resolve, reject);
  }

  /**
   * Starts a new query chain with the given criteria
   * @param {Object} criteria - Filter criteria
   * @returns {QueryChain} - Query chain for further operations
   */
  find(criteria = {}) {
    return new QueryChain(this.client, this.resourcePath, criteria);
  }

  /**
   * Find a single record by id
   * @param {string|number} id - Record id
   * @param {string|string[]} [populate] - Fields to populate
   * @returns {QueryChain|Promise<Object>} - Query chain for further operations or found record with data and headers
   */
  findOne(id, populate) {
    const query = this.find({ id });
    if (populate) {
      query.populate(populate);
    }

    // Add a custom executeOne method that will be called when awaited
    const originalThen = query.then;
    query.then = (resolve, reject) => {
      return query.executeOne().then(resolve, reject);
    };

    return query;
  }

  /**
   * Create a new record
   * @param {Object} data - Record data
   * @returns {Promise<Object>} - Created record with data and headers
   */
  async create(data) {
    return await this.client.request({
      method: 'post',
      url: `/${this.resourcePath}`,
      data
    });
  }

  /**
   * Update a record by id
   * @param {string|number} id - Record id
   * @param {Object} data - Update data
   * @returns {Promise<Object>} - Updated record with data and headers
   */
  async update(id, data) {
    return await this.client.request({
      method: 'patch',
      url: `/${this.resourcePath}/${id}`,
      data
    });
  }

  /**
   * Delete a record by id
   * @param {string|number} id - Record id
   * @returns {Promise<Object>} - Deletion result with data and headers
   */
  async destroy(id) {
    return await this.client.request({
      method: 'delete',
      url: `/${this.resourcePath}/${id}`
    });
  }

  /**
   * Sum field values
   * Calculate the sum of a specified numeric field across filtered records.
   * @param {Object} [query] - Query parameters
   * @returns {Promise<any>} - Response with data and headers
   */
  async getFilesSum(query = {}) {
    let url = '/files/sum'
;

    return await this.client.request({
      method: 'get',
      url,
      params: query,
    });
  }

  /**
   * Count records
   * Count the number of records that match the specified criteria.
   * @param {Object} [query] - Query parameters
   * @returns {Promise<any>} - Response with data and headers
   */
  async getFilesCount(query = {}) {
    let url = '/files/count'
;

    return await this.client.request({
      method: 'get',
      url,
      params: query,
    });
  }

  /**
   * Upload file
   * Upload a file and store its metadata.
   * @param {Object} [query] - Query parameters
   * @returns {Promise<void>} - Response with data and headers
   */
  async postFilesUpload(query = {}) {
    let url = '/files/upload'
;

    return await this.client.request({
      method: 'post',
      url,
      params: query,
    });
  }

  /**
   * Download file
   * Download a file by ID.
   * @param {string} _id - Path parameter
   * @param {Object} [query] - Query parameters
   * @returns {Promise<any>} - Response with data and headers
   */
  async getFilesDownloadId(_id, query = {}) {
    let url = '/files/download/{_id}'
      .replace('_id', encodeURIComponent(_id))
      ;

    return await this.client.request({
      method: 'get',
      url,
      params: query,
    });
  }

  /**
   * List File (find where)
   * Find a list of **File** records that match the specified criteria.
   * @param {Object} [query] - Query parameters
   * @returns {Promise<file[]>} - Response with data and headers
   */
  async getFiles(query = {}) {
    let url = '/files'
;

    return await this.client.request({
      method: 'get',
      url,
      params: query,
    });
  }

  /**
   * Create File
   * Create a new **File** record.
   * @param {Object} data - Request body
   * @returns {Promise<file>} - Response with data and headers
   */
  async postFiles(data) {
    let url = '/files'
;

    return await this.client.request({
      method: 'post',
      url,
      data,
    });
  }

  /**
   * Get File (find one)
   * Look up the **File** record with the specified ID.
   * @param {Object} [query] - Query parameters
   * @returns {Promise<file>} - Response with data and headers
   */
  async getFilesId(query = {}) {
    let url = '/files/{_id}'
;

    return await this.client.request({
      method: 'get',
      url,
      params: query,
    });
  }

  /**
   * Update File
   * Update an existing **File** record.
   * @param {Object} data - Request body
   * @returns {Promise<file>} - Response with data and headers
   */
  async patchFilesId(data) {
    let url = '/files/{_id}'
;

    return await this.client.request({
      method: 'patch',
      url,
      data,
    });
  }

  /**
   * Delete File (destroy)
   * Delete the **File** record with the specified ID.
   * @returns {Promise<file>} - Response with data and headers
   */
  async deleteFilesId() {
    let url = '/files/{_id}'
;

    return await this.client.request({
      method: 'delete',
      url,
    });
  }

  /**
   * Populate association for File
   * Populate and return foreign record(s) for the given association of this **File** record.
   * @param {string} association - Path parameter
   * @param {Object} [query] - Query parameters
   * @returns {Promise<any[]>} - Response with data and headers
   */
  async getFilesIdAssociation(association, query = {}) {
    let url = '/files/{_id}/{association}'
      .replace('association', encodeURIComponent(association))
      ;

    return await this.client.request({
      method: 'get',
      url,
      params: query,
    });
  }

}

/**
 * Query chain for waterline-like syntax
 */
class QueryChain {
  constructor(client, resourceName, criteria = {}) {
    this.client = client;
    this.resourceName = resourceName;
    this.criteria = { ...criteria };
    this.limitValue = null;
    this.skipValue = 0;
    this.sortValue = null;
    this.selectFields = null;
    this.populateFields = null;

    // Make the QueryChain thenable
    this.then = (resolve, reject) => this.execute().then(resolve, reject);
  }

  /**
   * Set the maximum number of records to return
   * @param {number} limit - Maximum number of records
   * @returns {QueryChain} - This query chain
   */
  limit(limit) {
    this.limitValue = limit;
    return this;
  }

  /**
   * Set the number of records to skip
   * @param {number} skip - Number of records to skip
   * @returns {QueryChain} - This query chain
   */
  skip(skip) {
    this.skipValue = skip;
    return this;
  }

  /**
   * Set the fields to select
   * @param {string[]} fields - Fields to select
   * @returns {QueryChain} - This query chain
   */
  select(fields) {
    this.selectFields = Array.isArray(fields) ? fields : [fields];
    return this;
  }

  /**
   * Set the sort order
   * @param {Object|string} sort - Sort criteria
   * @returns {QueryChain} - This query chain
   */
  sort(sort) {
    this.sortValue = sort;
    return this;
  }

  /**
   * Specify fields to populate
   * @param {string[]} fields - Fields to populate
   * @returns {QueryChain} - This query chain
   */
  populate(fields) {
    this.populateFields = Array.isArray(fields) ? fields : [fields];
    return this;
  }

  /**
   * Execute the query
   * @returns {Promise<Object>} - Found records with data and headers
   */
  async execute() {
    const queryParams = { ...this.criteria };

    if (this.limitValue !== null) {
      queryParams.limit = this.limitValue;
    }

    if (this.skipValue > 0) {
      queryParams.skip = this.skipValue;
    }

    if (this.sortValue) {
      queryParams.sort = this.sortValue;
    }

    if (this.selectFields) {
      queryParams.select = this.selectFields.join(',');
    }

    if (this.populateFields) {
      queryParams.populate = this.populateFields.join(',');
    }

    return await this.client.request({
      method: 'get',
      url: `/${this.resourceName}`,
      params: queryParams
    });
  }

  /**
   * Execute the query and return a single record
   * @returns {Promise<Object>} - Found record with data and headers
   */
  async executeOne() {
    const response = await this.limit(1).execute();
    if (response.data && Array.isArray(response.data)) {
      return {
        data: response.data.length > 0 ? response.data[0] : null,
        headers: response.headers
      };
    }
    return response;
  }

  /**
   * Count records matching the criteria
   * @returns {Promise<Object>} - Count of matching records with data and headers
   */
  async count() {
    const response = await this.client.request({
      method: 'get',
      url: `/${this.resourceName}/count`,
      params: this.criteria
    });

    if (response.data) {
      return {
        data: { count: response.data.count || 0 },
        headers: response.headers
      };
    }

    return response;
  }
}

export default FilesResource