UNPKG

@planet-a/affinity-node

Version:
148 lines (147 loc) 5.39 kB
import { listEntriesUrl } from './urls.js'; import { defaultTransformers } from './axios_default_transformers.js'; import { transformListEntryReference } from './transform_list_entry_reference.js'; /** * @module * * *Notes*: Although list entries correspond to rows in an Affinity spreadsheet, the values associated with the entity are not stored inside the list entry resource. * If you are trying to update, create, or delete a value in one of the custom columns for this list entry, please refer to the [Field Values](https://api-docs.affinity.co/#field-values) section. * The list entry API is only used for getting, adding, or removing entities from a list. * It does not handle updating individual cells in columns. */ export class ListEntries { /** @hidden */ constructor(axios) { Object.defineProperty(this, "axios", { enumerable: true, configurable: true, writable: true, value: axios }); } /** * Fetches entries in a given list * @returns Either an array of all list entries or a chunk of list entries with the maximum size specified in the query */ async all(query) { const { list_id, ...params } = query; const response = await this.axios.get(listEntriesUrl(list_id), { params, transformResponse: [ ...defaultTransformers(), (json) => { if ('list_entries' in json) { return { ...json, list_entries: json.list_entries.map(transformListEntryReference), }; } else { return json.map(transformListEntryReference); } }, ], }); return response.data; } /** * Fetches a list entry with a specified id. * * @returns The list entry object corresponding to the list_entry_id. * * @example * ```typescript * const listEntry = await affinity.lists.entries.get({ list_id: 450, list_entry_id: 16367 }) * console.log(listEntry) * ``` */ async get(reference) { const { list_id, list_entry_id } = reference; const response = await this.axios.get(listEntriesUrl(list_id, list_entry_id), { transformResponse: [ ...defaultTransformers(), transformListEntryReference, ], }); return response.data; } /** * Returns an async iterator that yields all list entries in the list with the supplied list id. * Each yielded array contains up to the number specified in {@link PagingParameters.page_size} of list entries. * Use this method if you want to process the list entries in a streaming fashion. * * *Please note:* the yielded list entries array may be empty on the last page. * * @example * ```typescript * let page = 0 * for await (const entries of affinity.lists.entries.pagedIterator({ * list_id: 123, * page_size: 10 * })) { * console.log(`Page ${++page} of entries:`, entries) * } * ``` */ async *pagedIterator(query) { let page_token = undefined; while (true) { const response = await this.all(page_token ? { ...query, page_token } : query); yield response.list_entries; if (response.next_page_token === null) { // no more pages to fetch return; } else { page_token = response.next_page_token; } } } /** * Deletes a specific list entry. * * @returns boolean indicating whether the list entry was successfully deleted. * * @example * ```typescript * const success = await affinity.lists.entries.delete({ * list_id: 450, * list_entry_id: 16367 * }) * console.log(success ? 'List entry deleted': 'List entry not deleted') * ``` */ async delete(reference) { const { list_id, list_entry_id } = reference; const response = await this.axios.delete(listEntriesUrl(list_id, list_entry_id)); return response.data.success === true; } /** * Creates a new list entry in the list with the supplied list_id. * * *Notes*: * - Opportunities cannot be created using this endpoint. Instead use the POST /opportunities endpoint. * - Person and company lists can contain the same entity multiple times. Depending on your use case, before you add an entry, you may want to verify whether or not it exists in the list already. * * @returns The created list entry object. * * @example * ```typescript * const newListEntry = await affinity.lists.entries.create({ * list_id: 450, * entity_id: 38706 * }) * console.log(newListEntry) * ``` */ async create(request) { const { list_id, ...rest } = request; const response = await this.axios.post(listEntriesUrl(list_id), rest, { transformResponse: [ ...defaultTransformers(), transformListEntryReference, ], }); return response.data; } }