Skip to content

feat: allow total_method param in customer endpoint #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2025
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
29 changes: 29 additions & 0 deletions src/endpoints/customers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CRUDExtend from '../extends/crud'
import { buildURL } from '../utils/helpers'

class CustomersEndpoint extends CRUDExtend {
constructor(endpoint) {
Expand Down Expand Up @@ -44,5 +45,33 @@ class CustomersEndpoint extends CRUDExtend {
Token(email, password) {
return this.TokenViaPassword(email, password)
}

TotalMethod(totalMethod) {
this.total_method = totalMethod
return this
}

All(token = null) {
const { includes, sort, limit, offset, filter, total_method } = this

this.call = this.request.send(
buildURL(this.endpoint, {
includes,
sort,
limit,
offset,
filter,
...(total_method && { total_method })
}),
'GET',
undefined,
token,
this
)

this.total_method = undefined

return this.call
}
}
export default CustomersEndpoint
1 change: 1 addition & 0 deletions src/types/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface ResourcePage<R, I = never> extends ResourceList<R> {
}
results: {
total: number
total_method?: 'observed' | 'exact'
}
}
included?: I
Expand Down
8 changes: 8 additions & 0 deletions src/types/customer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,12 @@ export interface CustomersEndpoint
codeVerifier: string,
headers?: object
): Promise<Resource<CustomerToken>>

/**
* Total Method
* Description: The total method to use for the customer endpoint.
* DOCS: https://documentation.elasticpath.com/commerce-cloud/docs/api/orders-and-customers/customers/index.html
* @param totalMethod [string] the total method to use for the customer endpoint
*/
TotalMethod(totalMethod: string): this
}
4 changes: 4 additions & 0 deletions src/utils/formatQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export function formatQueryString(key, value) {
return `page${value}`
}

if (key === 'total_method') {
return `page[total_method]=${value}`
}

if (key === 'filter') {
const filterValues = []

Expand Down
13 changes: 9 additions & 4 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function parseJSON(response) {
})
}

function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSlugs}) {
function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSlugs, total_method }) {
const query = {}

if (includes) {
Expand All @@ -101,7 +101,7 @@ function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSl
query.sort = `${sort}`
}

if (limit) {
if (limit !== undefined && limit !== null) {
query.limit = `[limit]=${limit}`
}

Expand All @@ -117,6 +117,10 @@ function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSl
query.useTemplateSlugs = useTemplateSlugs
}

if(total_method) {
query.total_method = total_method
}

return Object.keys(query)
.map(k => formatQueryString(k, query[k]))
.join('&')
Expand All @@ -132,10 +136,11 @@ export function buildURL(endpoint, params) {
if (
params.includes ||
params.sort ||
params.limit ||
(params.limit !== undefined && params.limit !== null) ||
params.offset ||
params.filter ||
params.useTemplateSlugs
params.useTemplateSlugs ||
params.total_method
) {
const paramsString = buildQueryParams(params)

Expand Down