Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/bitable/src/field/field-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import type { HttpClient, TokenManager } from '@lark-kit/core'
import type { BitableField } from '@lark-kit/shared'
import { createField } from './create'
import { deleteField } from './delete'
import { listFields } from './read'
import { listFields, listAllFields } from './read'
import type {
CreateFieldPayload,
DeleteFieldPayload,
ListFieldsPayload,
ListFieldsResult,
ListAllFieldsPayload,
UpdateFieldPayload,
} from './types'
import { updateField } from './update'
Expand All @@ -26,6 +27,17 @@ export class AppTableFieldClient {
return listFields(this.httpClient, this.tokenManager, payload)
}

/**
* List all fields in a table with automatic pagination
* @example
* for await (const field of client.bitable.appTableField.listAll({ path: { app_token, table_id } })) {
* console.log(field)
* }
*/
listAll(payload: ListAllFieldsPayload): AsyncGenerator<BitableField, void, unknown> {
return listAllFields(this.httpClient, this.tokenManager, payload)
}

/**
* Create a field in a table
* @see https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-field/create
Expand Down
1 change: 1 addition & 0 deletions packages/bitable/src/field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
type FieldTypeValue,
type ListFieldsPayload,
type ListFieldsResult,
type ListAllFieldsPayload,
type CreateFieldPayload,
type UpdateFieldPayload,
type DeleteFieldPayload,
Expand Down
25 changes: 24 additions & 1 deletion packages/bitable/src/field/read.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { HttpClient, TokenManager } from '@lark-kit/core'
import { LarkApiError } from '@lark-kit/core'
import { ListFieldsResponseSchema, parseResponse } from '@lark-kit/shared'
import type { ListFieldsPayload, ListFieldsResult } from './types'
import type { BitableField } from '@lark-kit/shared'
import type { ListFieldsPayload, ListFieldsResult, ListAllFieldsPayload } from './types'

export async function listFields(
httpClient: HttpClient,
Expand Down Expand Up @@ -33,3 +34,25 @@ export async function listFields(
total: parsed.data?.total,
}
}

export async function* listAllFields(
httpClient: HttpClient,
tokenManager: TokenManager,
payload: ListAllFieldsPayload
): AsyncGenerator<BitableField, void, unknown> {
let pageToken: string | undefined
const { params, path } = payload

do {
const result = await listFields(httpClient, tokenManager, {
params: { ...params, page_token: pageToken },
path,
})

for (const field of result.items) {
yield field
}

pageToken = result.has_more ? result.page_token : undefined
} while (pageToken)
}
8 changes: 8 additions & 0 deletions packages/bitable/src/field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export interface ListFieldsResult {
total?: number
}

export interface ListAllFieldsPayload {
params?: Omit<ListFieldsPayload['params'], 'page_token'>
path: {
app_token: string
table_id: string
}
}

export interface CreateFieldPayload {
path: {
app_token: string
Expand Down
2 changes: 2 additions & 0 deletions packages/bitable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
type DeleteRecordPayload,
type ListRecordsPayload,
type ListRecordsResult,
type ListAllRecordsPayload,
type BatchCreateRecordsPayload,
type BatchUpdateRecordsPayload,
type BatchDeleteRecordsPayload,
Expand All @@ -17,6 +18,7 @@ export {
type FieldTypeValue,
type ListFieldsPayload,
type ListFieldsResult,
type ListAllFieldsPayload,
type CreateFieldPayload,
type UpdateFieldPayload,
type DeleteFieldPayload,
Expand Down
1 change: 1 addition & 0 deletions packages/bitable/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type {
DeleteRecordPayload,
ListRecordsPayload,
ListRecordsResult,
ListAllRecordsPayload,
BatchCreateRecordsPayload,
BatchUpdateRecordsPayload,
BatchDeleteRecordsPayload,
Expand Down
24 changes: 23 additions & 1 deletion packages/bitable/src/record/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ListRecordsResponseSchema,
parseResponse,
} from '@lark-kit/shared'
import type { GetRecordPayload, ListRecordsPayload, ListRecordsResult } from './types'
import type { GetRecordPayload, ListRecordsPayload, ListRecordsResult, ListAllRecordsPayload } from './types'

export async function getRecord(
httpClient: HttpClient,
Expand Down Expand Up @@ -63,3 +63,25 @@ export async function listRecords(
total: parsed.data?.total,
}
}

export async function* listAllRecords(
httpClient: HttpClient,
tokenManager: TokenManager,
payload: ListAllRecordsPayload
): AsyncGenerator<BitableRecord, void, unknown> {
let pageToken: string | undefined
const { params, path } = payload

do {
const result = await listRecords(httpClient, tokenManager, {
params: { ...params, page_token: pageToken },
path,
})

for (const record of result.items) {
yield record
}

pageToken = result.has_more ? result.page_token : undefined
} while (pageToken)
}
14 changes: 13 additions & 1 deletion packages/bitable/src/record/record-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { HttpClient, TokenManager } from '@lark-kit/core'
import type { BitableRecord } from '@lark-kit/shared'
import { batchCreateRecords, createRecord } from './create'
import { batchDeleteRecords, deleteRecord } from './delete'
import { getRecord, listRecords } from './read'
import { getRecord, listRecords, listAllRecords } from './read'
import type {
BatchCreateRecordsPayload,
BatchDeleteRecordsPayload,
Expand All @@ -12,6 +12,7 @@ import type {
GetRecordPayload,
ListRecordsPayload,
ListRecordsResult,
ListAllRecordsPayload,
UpdateRecordPayload,
} from './types'
import { batchUpdateRecords, updateRecord } from './update'
Expand Down Expand Up @@ -62,6 +63,17 @@ export class AppTableRecordClient {
return listRecords(this.httpClient, this.tokenManager, payload)
}

/**
* List all records in a table with automatic pagination
* @example
* for await (const record of client.bitable.appTableRecord.listAll({ path: { app_token, table_id } })) {
* console.log(record)
* }
*/
listAll(payload: ListAllRecordsPayload): AsyncGenerator<BitableRecord, void, unknown> {
return listAllRecords(this.httpClient, this.tokenManager, payload)
}

/**
* Batch create records in a table
* @see https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/bitable-v1/app-table-record/batch_create
Expand Down
8 changes: 8 additions & 0 deletions packages/bitable/src/record/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export interface ListRecordsResult {
total?: number
}

export interface ListAllRecordsPayload {
params?: Omit<ListRecordsPayload['params'], 'page_token'>
path: {
app_token: string
table_id: string
}
}

export interface BatchCreateRecordsPayload {
data: {
records: Array<{ fields: Record<string, unknown> }>
Expand Down
2 changes: 2 additions & 0 deletions packages/lark-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export type {
DeleteRecordPayload,
ListRecordsPayload,
ListRecordsResult,
ListAllRecordsPayload,
BatchCreateRecordsPayload,
BatchUpdateRecordsPayload,
BatchDeleteRecordsPayload,
ListFieldsPayload,
ListFieldsResult,
ListAllFieldsPayload,
CreateFieldPayload,
UpdateFieldPayload,
DeleteFieldPayload,
Expand Down