Edits history of script submission #22237 for ' Get dataset items (apify)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { ApifyClient } from 'apify-client@^2.19.0';
    
    type ApifyApiKey = {
      api_key: string;
    };
    
    type Apify = {
      token: string;
    };
    
    const createClient = (api_key?: ApifyApiKey, oauth_token?: Apify): ApifyClient => {
      const token = oauth_token?.token ?? api_key?.api_key;
      if (!token) {
        throw new Error('Missing Apify API key or OAuth token');
      }
    
      return new ApifyClient({
        token: token,
        requestInterceptors: [
          (request) => {
            if (!request.headers) {
              request.headers = {};
            }
            request.headers['x-apify-integration-platform'] = 'windmill';
            return request;
          },
        ],
      });
    };
    
    type DynSelect_dataset = string;
    export async function dataset(api_key?: ApifyApiKey, oauth_token?: Apify) {
      if (!api_key?.api_key && !oauth_token?.token) {
        return [{ value: '', label: 'Missing Apify API key or OAuth token' }];
      }
    
      const apifyClient = createClient(api_key, oauth_token);
    
      try {
        const dataset = await apifyClient.datasets().list({
          desc: true,
        });
    
        return dataset.items.map(item => ({
          value: item.id,
          label: `${item.title || item.name} (${item.itemCount} ${pluralize(item.itemCount)} - ${item.id})`
        }));
      } catch (e: any) {
        return {
          error: `Failed to fetch dataset list. Reason: ${e.message}`
        };
      }
    }
    
    function pluralize(n: number) {
      return n === 1 ? "item" : "items";
    }
    
    export async function main(
      api_key?: ApifyApiKey,
      oauth_token?: Apify,
      datasetId?: string,
      dataset?: DynSelect_dataset,
      offset?: number,
      limit?: number,
      fields?: string[],
      omit?: string[],
    ) {
    
      const id = datasetId || dataset;
    
      if (!id) {
        return {
          error: "A Dataset ID is required. Please select one from the list or provide it manually"
        };
      }
    
      const apifyClient = createClient(api_key, oauth_token);
    
      try {
        const result = await apifyClient.dataset(id).listItems({
          // in a Flow null is passed instead of undefined
          offset: offset ?? undefined,
          limit: limit ?? undefined,
          fields: fields ?? undefined,
          omit: omit ?? undefined,
        });
    
        if (!result) {
          return { error: `Dataset with ID "${id}" not found` };
        }
    
        return result.items;
      } catch (e: any) {
        return { error: `Failed to fetch items from dataset "${id}". Reason: ${e.message}` };
      }
    }

    Submitted by jakub.drobnik222 172 days ago