Skip to content

Commit 359ad69

Browse files
committed
feat: allow total_method param in customer endpoint
1 parent 5ce719f commit 359ad69

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

src/endpoints/customers.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CRUDExtend from '../extends/crud'
2+
import { buildURL } from '../utils/helpers'
23

34
class CustomersEndpoint extends CRUDExtend {
45
constructor(endpoint) {
@@ -44,5 +45,33 @@ class CustomersEndpoint extends CRUDExtend {
4445
Token(email, password) {
4546
return this.TokenViaPassword(email, password)
4647
}
48+
49+
TotalMethod(totalMethod) {
50+
this.total_method = totalMethod
51+
return this
52+
}
53+
54+
All(token = null) {
55+
const { includes, sort, limit, offset, filter, total_method } = this
56+
57+
this.call = this.request.send(
58+
buildURL(this.endpoint, {
59+
includes,
60+
sort,
61+
limit,
62+
offset,
63+
filter,
64+
total_method: total_method ?? 'exact'
65+
}),
66+
'GET',
67+
undefined,
68+
token,
69+
this
70+
)
71+
72+
this.total_method = undefined
73+
74+
return this.call
75+
}
4776
}
4877
export default CustomersEndpoint

src/types/core.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface ResourcePage<R, I = never> extends ResourceList<R> {
6161
}
6262
results: {
6363
total: number
64+
total_method?: 'observed' | 'exact'
6465
}
6566
}
6667
included?: I

src/types/customer.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,12 @@ export interface CustomersEndpoint
135135
codeVerifier: string,
136136
headers?: object
137137
): Promise<Resource<CustomerToken>>
138+
139+
/**
140+
* Total Method
141+
* Description: The total method to use for the customer endpoint.
142+
* DOCS: https://documentation.elasticpath.com/commerce-cloud/docs/api/orders-and-customers/customers/index.html
143+
* @param totalMethod [string] the total method to use for the customer endpoint
144+
*/
145+
TotalMethod(totalMethod: string): this
138146
}

src/utils/formatQueryString.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export function formatQueryString(key, value) {
2323
return `page${value}`
2424
}
2525

26+
if (key === 'total_method') {
27+
return `page[total_method]=${value}`
28+
}
29+
2630
if (key === 'filter') {
2731
const filterValues = []
2832

src/utils/helpers.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function parseJSON(response) {
9090
})
9191
}
9292

93-
function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSlugs}) {
93+
function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSlugs, total_method }) {
9494
const query = {}
9595

9696
if (includes) {
@@ -117,6 +117,10 @@ function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSl
117117
query.useTemplateSlugs = useTemplateSlugs
118118
}
119119

120+
if(total_method) {
121+
query.total_method = total_method
122+
}
123+
120124
return Object.keys(query)
121125
.map(k => formatQueryString(k, query[k]))
122126
.join('&')
@@ -135,7 +139,8 @@ export function buildURL(endpoint, params) {
135139
params.limit ||
136140
params.offset ||
137141
params.filter ||
138-
params.useTemplateSlugs
142+
params.useTemplateSlugs ||
143+
params.total_method
139144
) {
140145
const paramsString = buildQueryParams(params)
141146

0 commit comments

Comments
 (0)