Add support for cursor and page based pagination inside REST query request body

This commit is contained in:
Andrew Kingston 2022-01-05 18:10:22 +00:00
parent f5649b48d1
commit 4ab06c200d
1 changed files with 39 additions and 13 deletions

View File

@ -217,20 +217,36 @@ module RestModule {
return complete return complete
} }
addBody(bodyType: string, body: string | any, input: any) { addBody(bodyType: string, body: string | any, input: any, pagination: PaginationConfig | null, paginationValues: PaginationValues | null) {
let error, object, string if (bodyType === BodyTypes.NONE) {
try { return input
string = typeof body !== "string" ? JSON.stringify(body) : body
object = typeof body === "object" ? body : JSON.parse(body)
} catch (err) {
error = err
} }
if (!input.headers) { if (!input.headers) {
input.headers = {} input.headers = {}
} }
let error, object = {}, string = ""
try {
if (body) {
string = typeof body !== "string" ? JSON.stringify(body) : body
object = typeof body === "object" ? body : JSON.parse(body)
}
} catch (err) {
error = err
}
// Util to add pagination values to a certain body type
const addPaginationToBody = (insertFn: Function) => {
if (pagination?.location === "body") {
if (pagination?.pageParam && paginationValues?.page != null) {
insertFn(pagination.pageParam, paginationValues.page)
}
if (pagination?.sizeParam && paginationValues?.limit != null) {
insertFn(pagination.sizeParam, paginationValues.limit)
}
}
}
switch (bodyType) { switch (bodyType) {
case BodyTypes.NONE:
break
case BodyTypes.TEXT: case BodyTypes.TEXT:
// content type defaults to plaintext // content type defaults to plaintext
input.body = string input.body = string
@ -240,6 +256,9 @@ module RestModule {
for (let [key, value] of Object.entries(object)) { for (let [key, value] of Object.entries(object)) {
params.append(key, value) params.append(key, value)
} }
addPaginationToBody((key: string, value: any) => {
params.append(key, value)
})
input.body = params input.body = params
break break
case BodyTypes.FORM_DATA: case BodyTypes.FORM_DATA:
@ -247,6 +266,9 @@ module RestModule {
for (let [key, value] of Object.entries(object)) { for (let [key, value] of Object.entries(object)) {
form.append(key, value) form.append(key, value)
} }
addPaginationToBody((key: string, value: any) => {
form.append(key, value)
})
input.body = form input.body = form
break break
case BodyTypes.XML: case BodyTypes.XML:
@ -262,7 +284,13 @@ module RestModule {
if (error) { if (error) {
throw "Invalid JSON for request body" throw "Invalid JSON for request body"
} }
input.body = string if (!body) {
body = {}
}
addPaginationToBody((key: string, value: any) => {
body[key] = value
})
input.body = JSON.stringify(body)
input.headers["Content-Type"] = "application/json" input.headers["Content-Type"] = "application/json"
break break
} }
@ -328,9 +356,7 @@ module RestModule {
} }
let input: any = { method, headers: this.headers } let input: any = { method, headers: this.headers }
if (requestBody) { input = this.addBody(bodyType, requestBody, input, pagination, paginationValues)
input = this.addBody(bodyType, requestBody, input)
}
this.startTimeMs = performance.now() this.startTimeMs = performance.now()
const url = this.getUrl(path, queryString, pagination, paginationValues) const url = this.getUrl(path, queryString, pagination, paginationValues)