Skip to content

Commit a9fa5df

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/js-sdk-audit-6x115l-h6-action-attempt-types
2 parents 94dfa30 + a6b5769 commit a9fa5df

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

‎src/lib/seam-paginator.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,21 @@ export class SeamPaginator<
8282
throw new Error('Cannot paginate a response without a responseKey')
8383
}
8484

85+
const method = this.#request.method
86+
87+
const requestData = {
88+
...(usesParams(method)
89+
? (this.#request.params ?? {})
90+
: ((this.#request.body as Record<string, unknown> | null) ?? {})),
91+
page_cursor: nextPageCursor,
92+
}
93+
8594
const request = new SeamHttpRequest<TResponse, TResponseKey>(this.#parent, {
8695
pathname: this.#request.pathname,
87-
method: this.#request.method,
96+
method,
8897
responseKey,
89-
params:
90-
this.#request.params != null
91-
? { ...this.#request.params, page_cursor: nextPageCursor }
92-
: undefined,
93-
body:
94-
this.#request.body != null
95-
? { ...this.#request.body, page_cursor: nextPageCursor }
96-
: undefined,
98+
params: usesParams(method) ? requestData : undefined,
99+
body: usesParams(method) ? undefined : requestData,
97100
})
98101

99102
const response = await request.fetchResponse()
@@ -186,6 +189,9 @@ export class SeamPaginator<
186189
}
187190
}
188191

192+
const usesParams = (method: string): boolean =>
193+
['GET', 'DELETE'].includes(method.toUpperCase())
194+
189195
type EnsureReadonlyArray<T> = T extends readonly any[] ? T : never
190196
type EnsureMutableArray<T> = T extends any[] ? T : never
191197
type ElementOfArray<T> =

‎test/seam/connect/seam-paginator.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,40 @@ test('SeamPaginator: flatten allows iteration over all devices', async (t) => {
9696

9797
const expectType = <Expected>(_value: Expected): void => {}
9898

99+
test('SeamPaginator: sends the page cursor when the request has no parameters', async (t) => {
100+
const { seed, endpoint } = await getTestServer(t)
101+
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
102+
103+
nock(endpoint)
104+
.get('/devices/list')
105+
.reply(200, {
106+
devices: [{ device_id: 'device-1' }],
107+
pagination: {
108+
has_next_page: true,
109+
next_page_cursor: 'page-cursor-1',
110+
next_page_url: `${endpoint}/devices/list?page_cursor=page-cursor-1`,
111+
},
112+
})
113+
.get('/devices/list')
114+
.query({ page_cursor: 'page-cursor-1', _strict: 'true' })
115+
.reply(200, {
116+
devices: [{ device_id: 'device-2' }],
117+
pagination: {
118+
has_next_page: false,
119+
next_page_cursor: null,
120+
next_page_url: null,
121+
},
122+
})
123+
124+
const pages = seam.createPaginator(seam.devices.list())
125+
const devices = await pages.flattenToArray()
126+
127+
t.deepEqual(
128+
devices.map(({ device_id: deviceId }) => deviceId),
129+
['device-1', 'device-2'],
130+
)
131+
})
132+
99133
test('SeamPaginator: stops iterating when the page cursor repeats', async (t) => {
100134
const { seed, endpoint } = await getTestServer(t)
101135
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })

0 commit comments

Comments
 (0)