Skip to content

Commit 0f749a5

Browse files
authored
feat: add GetNodesByIds method to hierarchies endpoint (#64)
BREAKING CHANGE: add GetNodesByIds method to hierarchies endpoint
1 parent 2d17a79 commit 0f749a5

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

src/endpoints/hierarchies.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,42 @@ class HierarchiesEndpoint extends CRUDExtend {
3737
token
3838
)
3939
}
40+
41+
GetNodesByIds(nodeIds, token = null) {
42+
if (!nodeIds || nodeIds.length === 0) {
43+
return Promise.resolve({
44+
data: [],
45+
links: {},
46+
meta: {
47+
page: {
48+
current: 1,
49+
limit: 100,
50+
offset: 0,
51+
total: 0
52+
},
53+
results: {
54+
total: 0
55+
}
56+
}
57+
})
58+
}
59+
60+
const filter = {
61+
or: nodeIds.map(id => ({
62+
eq: { id }
63+
}))
64+
}
65+
66+
return this.request.send(
67+
buildURL('hierarchies/nodes', {
68+
filter,
69+
include_hierarchies: true
70+
}),
71+
'GET',
72+
undefined,
73+
token
74+
)
75+
}
4076
}
4177

4278
export default HierarchiesEndpoint

src/types/hierarchies.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface DuplicateHierarchyBody {
4949
}
5050
}
5151

52-
export type DuplicateHierarchyJob = Identifiable & PcmJobBase
52+
export type DuplicateHierarchyJob = Identifiable & PcmJobBase
5353

5454
export interface HierarchyFilter {
5555
// TODO
@@ -82,6 +82,7 @@ export interface HierarchiesEndpoint
8282
body: DuplicateHierarchyBody,
8383
token?: string
8484
): Promise<Resource<DuplicateHierarchyJob>>
85+
GetNodesByIds(nodeIds: string[], token?: string): Promise<ResourcePage<Node>>
8586
Limit(value: number): HierarchiesEndpoint
8687
Offset(value: number): HierarchiesEndpoint
8788
}

src/utils/helpers.js

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

93-
function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSlugs, total_method }) {
93+
function buildQueryParams(params) {
94+
const { includes, sort, limit, offset, filter, useTemplateSlugs, total_method, ...additionalParams } = params
9495
const query = {}
9596

9697
if (includes) {
@@ -121,6 +122,13 @@ function buildQueryParams({ includes, sort, limit, offset, filter, useTemplateSl
121122
query.total_method = total_method
122123
}
123124

125+
// Add any additional parameters with URI encoding
126+
Object.keys(additionalParams).forEach(key => {
127+
if (additionalParams[key] !== undefined && additionalParams[key] !== null) {
128+
query[key] = additionalParams[key]
129+
}
130+
})
131+
124132
return Object.keys(query)
125133
.map(k => formatQueryString(k, query[k]))
126134
.join('&')
@@ -133,18 +141,17 @@ export function formatQueryParams(query) {
133141
}
134142

135143
export function buildURL(endpoint, params) {
136-
if (
137-
params.includes ||
138-
params.sort ||
139-
(params.limit !== undefined && params.limit !== null) ||
140-
params.offset ||
141-
params.filter ||
142-
params.useTemplateSlugs ||
143-
params.total_method
144-
) {
144+
// Check if any params are provided
145+
const hasParams = Object.keys(params).some(key =>
146+
params[key] !== undefined && params[key] !== null
147+
)
148+
149+
if (hasParams) {
145150
const paramsString = buildQueryParams(params)
146-
147-
return `${endpoint}?${paramsString}`
151+
152+
if (paramsString) {
153+
return `${endpoint}?${paramsString}`
154+
}
148155
}
149156
return endpoint
150157
}

0 commit comments

Comments
 (0)