Skip to content

Commit 9ee0087

Browse files
committed
Handle pagination via get or post
1 parent 65aae95 commit 9ee0087

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/lib/seam/connect/seam-http-request.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export class SeamHttpRequest<
4848

4949
public get url(): URL {
5050
const { client } = this.#parent
51-
const { params } = this.#config
5251

5352
const serializer =
5453
typeof client.defaults.paramsSerializer === 'function'
@@ -58,7 +57,9 @@ export class SeamHttpRequest<
5857
const origin = getUrlPrefix(client.defaults.baseURL ?? '')
5958

6059
const path =
61-
params == null ? this.pathname : `${this.pathname}?${serializer(params)}`
60+
this.params == null
61+
? this.pathname
62+
: `${this.pathname}?${serializer(this.params)}`
6263

6364
return new URL(`${origin}${path}`)
6465
}
@@ -120,10 +121,10 @@ export class SeamHttpRequest<
120121
async fetchResponse(): Promise<TResponse> {
121122
const { client } = this.#parent
122123
const response = await client.request({
123-
url: this.#config.pathname,
124-
method: this.#config.method,
125-
data: this.#config.body,
126-
params: this.#config.params,
124+
url: this.pathname,
125+
method: this.method,
126+
data: this.body,
127+
params: this.params,
127128
})
128129
return response.data as unknown as TResponse
129130
}

src/lib/seam/connect/seam-paginator.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,38 @@ export class SeamPaginator<
6363
pathname: this.#request.pathname,
6464
method: 'get',
6565
responseKey,
66-
params: { ...this.#request.params, next_page_cursor: nextPageCursor },
66+
params:
67+
this.#request.params != null
68+
? { ...this.#request.params, next_page_cursor: nextPageCursor }
69+
: undefined,
70+
body:
71+
this.#request.body != null
72+
? { ...this.#request.body, next_page_cursor: nextPageCursor }
73+
: undefined,
6774
})
75+
6876
const response = await request.fetchResponse()
6977
const data = response[responseKey]
70-
const pagination: Pagination =
78+
79+
const paginationData =
7180
response != null &&
7281
typeof response === 'object' &&
7382
'pagination' in response
74-
? (response.pagination as Pagination)
75-
: {
76-
hasNextPage: false,
77-
nextPageCursor: null,
78-
nextPageUrl: null,
79-
}
83+
? (response.pagination as PaginationData)
84+
: null
85+
86+
const pagination: Pagination = {
87+
hasNextPage: paginationData?.has_next_page ?? false,
88+
nextPageCursor: paginationData?.next_page_cursor ?? null,
89+
nextPageUrl: paginationData?.next_page_url ?? null,
90+
}
91+
8092
if (!Array.isArray(data)) {
81-
throw new Error('Expected an array response')
93+
throw new Error(
94+
`Expected an array response for ${String(responseKey)} but got ${String(typeof data)}`,
95+
)
8296
}
97+
8398
return [
8499
data as EnsureReadonlyArray<TResponse[TResponseKey]>,
85100
pagination,
@@ -126,3 +141,9 @@ export class SeamPaginator<
126141

127142
type EnsureReadonlyArray<T> = T extends readonly any[] ? T : never
128143
type EnsureMutableArray<T> = T extends any[] ? T : never
144+
145+
interface PaginationData {
146+
has_next_page: boolean
147+
next_page_cursor: string | null
148+
next_page_url: string | null
149+
}

0 commit comments

Comments
 (0)