Skip to content

Commit a03c21d

Browse files
author
Crhistian
committed
✨ add new optional param anonuserid to Auth.Anonymous
This is part of the new tracking events featured and was missed because the auth endpoints are one of few endpoints that are not part of the openapi spec and thus not generated automatically
1 parent 5d14fd8 commit a03c21d

5 files changed

Lines changed: 91 additions & 28 deletions

File tree

codegen/templates/api/Auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ class Auth {
259259
* @param clientID of the application the user is logging into
260260
* @param scope roles being requested - space delimited string or array
261261
* @param customRoles optional custom roles being requested - string array
262+
* @param requestOptions.anonuserid Provide an externally generated id to track this user session, used specifically for the tracking events feature for integrating with Send and Discover
262263
* @param requestOptions.cancelToken Provide an [axios cancelToken](https://github.com/axios/axios#cancellation) that can be used to cancel the request.
263264
* @param requestOptions.requestType Provide a value that can be used to identify the type of request. Useful for error logs.
264265
*/
@@ -267,6 +268,7 @@ class Auth {
267268
scope: ApiRole[],
268269
customRoles?: string[],
269270
requestOptions: {
271+
anonuserid?: string
270272
cancelToken?: CancelToken
271273
requestType?: string
272274
} = {}
@@ -285,6 +287,10 @@ class Auth {
285287
client_id: clientID,
286288
scope: _scope,
287289
}
290+
if (requestOptions.anonuserid) {
291+
body['anonuserid'] = requestOptions.anonuserid
292+
delete requestOptions['anonuserid']
293+
}
288294
const configuration = Configuration.Get()
289295
const response = await axios
290296
.post(

docs/classes/Resources.Auth.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ <h2>Methods</h2>
5454
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class"><a id="Anonymous" class="tsd-anchor"></a>
5555
<h3 class="tsd-anchor-link"><span>Anonymous</span><a href="#Anonymous" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
5656
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class">
57-
<li class="tsd-signature tsd-anchor-link" id="Anonymous.Anonymous-1">Anonymous<span class="tsd-signature-symbol">(</span>clientID<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span>, scope<span class="tsd-signature-symbol">: </span><a href="../types/ApiRole.html" class="tsd-signature-type" data-tsd-kind="Type alias">ApiRole</a><span class="tsd-signature-symbol">[]</span>, customRoles<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">[]</span>, requestOptions<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-symbol">{ </span><br/><span>    </span>cancelToken<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">CancelToken</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>requestType<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">; </span><br/><span class="tsd-signature-symbol">}</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><a href="../types/Resources.RequiredObjectDeep.html" class="tsd-signature-type" data-tsd-kind="Type alias">RequiredObjectDeep</a><span class="tsd-signature-symbol">&lt;</span><a href="../interfaces/AccessToken.html" class="tsd-signature-type" data-tsd-kind="Interface">AccessToken</a><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">&gt;</span><a href="#Anonymous.Anonymous-1" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></li>
57+
<li class="tsd-signature tsd-anchor-link" id="Anonymous.Anonymous-1">Anonymous<span class="tsd-signature-symbol">(</span>clientID<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span>, scope<span class="tsd-signature-symbol">: </span><a href="../types/ApiRole.html" class="tsd-signature-type" data-tsd-kind="Type alias">ApiRole</a><span class="tsd-signature-symbol">[]</span>, customRoles<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">[]</span>, requestOptions<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-symbol">{ </span><br/><span>    </span>anonuserid<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>cancelToken<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">CancelToken</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>requestType<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">; </span><br/><span class="tsd-signature-symbol">}</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><a href="../types/Resources.RequiredObjectDeep.html" class="tsd-signature-type" data-tsd-kind="Type alias">RequiredObjectDeep</a><span class="tsd-signature-symbol">&lt;</span><a href="../interfaces/AccessToken.html" class="tsd-signature-type" data-tsd-kind="Interface">AccessToken</a><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">&gt;</span><a href="#Anonymous.Anonymous-1" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></li>
5858
<li class="tsd-description">
5959
<div class="tsd-comment tsd-typography">
6060
<h3>Description</h3><p>allow users to browse your catalog without signing in - must have anonymous template user set in dashboard</p>
@@ -75,9 +75,13 @@ <h5><code class="tsd-tag ts-flagOptional">Optional</code> customRoles: <span cla
7575
<div class="tsd-comment tsd-typography"><p>optional custom roles being requested - string array</p>
7676
</div></li>
7777
<li>
78-
<h5>requestOptions: <span class="tsd-signature-symbol">{ </span><br/><span>    </span>cancelToken<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">CancelToken</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>requestType<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">; </span><br/><span class="tsd-signature-symbol">}</span><span class="tsd-signature-symbol"> = {}</span></h5>
78+
<h5>requestOptions: <span class="tsd-signature-symbol">{ </span><br/><span>    </span>anonuserid<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>cancelToken<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">CancelToken</span><span class="tsd-signature-symbol">; </span><br/><span>    </span>requestType<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">; </span><br/><span class="tsd-signature-symbol">}</span><span class="tsd-signature-symbol"> = {}</span></h5>
7979
<ul class="tsd-parameters">
8080
<li class="tsd-parameter">
81+
<h5><code class="tsd-tag ts-flagOptional">Optional</code> anonuserid<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span></h5>
82+
<div class="tsd-comment tsd-typography"><p>Provide an externally generated id to track this user session, used specifically for the tracking events feature for integrating with Send and Discover</p>
83+
</div></li>
84+
<li class="tsd-parameter">
8185
<h5><code class="tsd-tag ts-flagOptional">Optional</code> cancel<wbr/>Token<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">CancelToken</span></h5>
8286
<div class="tsd-comment tsd-typography"><p>Provide an <a href="https://github.com/axios/axios#cancellation">axios cancelToken</a> that can be used to cancel the request.</p>
8387
</div></li>
@@ -87,7 +91,7 @@ <h5><code class="tsd-tag ts-flagOptional">Optional</code> request<wbr/>Type<span
8791
</div></li></ul></li></ul></div>
8892
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><a href="../types/Resources.RequiredObjectDeep.html" class="tsd-signature-type" data-tsd-kind="Type alias">RequiredObjectDeep</a><span class="tsd-signature-symbol">&lt;</span><a href="../interfaces/AccessToken.html" class="tsd-signature-type" data-tsd-kind="Interface">AccessToken</a><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">&gt;</span></h4><aside class="tsd-sources">
8993
<ul>
90-
<li>Defined in <a href="https://github.com/ordercloud-api/ordercloud-javascript-sdk/blob/master/src/api/Auth.ts#L265">api/Auth.ts:265</a></li></ul></aside></li></ul></section>
94+
<li>Defined in <a href="https://github.com/ordercloud-api/ordercloud-javascript-sdk/blob/master/src/api/Auth.ts#L266">api/Auth.ts:266</a></li></ul></aside></li></ul></section>
9195
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class"><a id="ClientCredentials" class="tsd-anchor"></a>
9296
<h3 class="tsd-anchor-link"><span>Client<wbr/>Credentials</span><a href="#ClientCredentials" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
9397
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class">

docs/variables/Auth.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<h1>Variable Auth</h1></div>
1818
<div class="tsd-signature">Auth<span class="tsd-signature-symbol">:</span> <a href="../classes/Resources.Auth.html" class="tsd-signature-type" data-tsd-kind="Class">Auth</a></div><aside class="tsd-sources">
1919
<ul>
20-
<li>Defined in <a href="https://github.com/ordercloud-api/ordercloud-javascript-sdk/blob/master/src/api/Auth.ts#L311">api/Auth.ts:311</a></li></ul></aside></div>
20+
<li>Defined in <a href="https://github.com/ordercloud-api/ordercloud-javascript-sdk/blob/master/src/api/Auth.ts#L317">api/Auth.ts:317</a></li></ul></aside></div>
2121
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
2222
<div class="tsd-navigation settings">
2323
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">

src/api/Auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ class Auth {
259259
* @param clientID of the application the user is logging into
260260
* @param scope roles being requested - space delimited string or array
261261
* @param customRoles optional custom roles being requested - string array
262+
* @param requestOptions.anonuserid Provide an externally generated id to track this user session, used specifically for the tracking events feature for integrating with Send and Discover
262263
* @param requestOptions.cancelToken Provide an [axios cancelToken](https://github.com/axios/axios#cancellation) that can be used to cancel the request.
263264
* @param requestOptions.requestType Provide a value that can be used to identify the type of request. Useful for error logs.
264265
*/
@@ -267,6 +268,7 @@ class Auth {
267268
scope: ApiRole[],
268269
customRoles?: string[],
269270
requestOptions: {
271+
anonuserid?: string
270272
cancelToken?: CancelToken
271273
requestType?: string
272274
} = {}
@@ -285,6 +287,10 @@ class Auth {
285287
client_id: clientID,
286288
scope: _scope,
287289
}
290+
if (requestOptions.anonuserid) {
291+
body['anonuserid'] = requestOptions.anonuserid
292+
delete requestOptions['anonuserid']
293+
}
288294
const configuration = Configuration.Get()
289295
const response = await axios
290296
.post(

tests/auth.test.ts

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const testdata: TestData = {
3131
}
3232
const testdatawithcustomroles: TestDataWithCustomRoles = {
3333
...testdata,
34-
customRoles: ['InventoryAdmin']
35-
}
34+
customRoles: ['InventoryAdmin'],
35+
}
3636

3737
afterEach(() => {
3838
// cleans up any tracked calls before the next test
@@ -71,10 +71,18 @@ test('can auth with login with custom roles', async () => {
7171
testdatawithcustomroles.username
7272
)}&password=${urlencode(testdatawithcustomroles.password)}&client_id=${
7373
testdatawithcustomroles.clientID
74-
}&scope=${urlencode(`${testdatawithcustomroles.scope.join(' ')} ${testdatawithcustomroles.customRoles.join(' ')}`)}`
75-
expect(mockAxios.post).toHaveBeenCalledWith(testdatawithcustomroles.authUrl, body, {
76-
headers: testdatawithcustomroles.authHeaders,
77-
})
74+
}&scope=${urlencode(
75+
`${testdatawithcustomroles.scope.join(
76+
' '
77+
)} ${testdatawithcustomroles.customRoles?.join?.(' ')}`
78+
)}`
79+
expect(mockAxios.post).toHaveBeenCalledWith(
80+
testdatawithcustomroles.authUrl,
81+
body,
82+
{
83+
headers: testdatawithcustomroles.authHeaders,
84+
}
85+
)
7886
})
7987

8088
test('can auth with elevated login, no custom roles', async () => {
@@ -83,7 +91,7 @@ test('can auth with elevated login, no custom roles', async () => {
8391
testdata.username,
8492
testdata.password,
8593
testdata.clientID,
86-
testdata.scope,
94+
testdata.scope
8795
)
8896
expect(mockAxios.post).toHaveBeenCalledTimes(1)
8997
const body = `grant_type=password&scope=${urlencode(
@@ -109,15 +117,21 @@ test('can auth with elevated login with custom roles', async () => {
109117
)
110118
expect(mockAxios.post).toHaveBeenCalledTimes(1)
111119
const body = `grant_type=password&scope=${urlencode(
112-
`${testdatawithcustomroles.scope.join(' ')} ${testdatawithcustomroles.customRoles.join(' ')}`
120+
`${testdatawithcustomroles.scope.join(
121+
' '
122+
)} ${testdatawithcustomroles.customRoles?.join?.(' ')}`
113123
)}&client_id=${testdatawithcustomroles.clientID}&username=${urlencode(
114124
testdatawithcustomroles.username
115-
)}&password=${urlencode(testdatawithcustomroles.password)}&client_secret=${urlencode(
116-
testdatawithcustomroles.clientSecret
117-
)}`
118-
expect(mockAxios.post).toHaveBeenCalledWith(testdatawithcustomroles.authUrl, body, {
119-
headers: testdatawithcustomroles.authHeaders,
120-
})
125+
)}&password=${urlencode(
126+
testdatawithcustomroles.password
127+
)}&client_secret=${urlencode(testdatawithcustomroles.clientSecret)}`
128+
expect(mockAxios.post).toHaveBeenCalledWith(
129+
testdatawithcustomroles.authUrl,
130+
body,
131+
{
132+
headers: testdatawithcustomroles.authHeaders,
133+
}
134+
)
121135
})
122136

123137
test('can auth with client credentials, no custom roles', async () => {
@@ -144,11 +158,19 @@ test('can auth with client credentials with custom roles', async () => {
144158
)
145159
expect(mockAxios.post).toHaveBeenCalledTimes(1)
146160
const body = `grant_type=client_credentials&scope=${urlencode(
147-
`${testdatawithcustomroles.scope.join(' ')} ${testdatawithcustomroles.customRoles.join(' ')}`
148-
)}&client_id=${testdatawithcustomroles.clientID}&client_secret=${testdatawithcustomroles.clientSecret}`
149-
expect(mockAxios.post).toHaveBeenCalledWith(testdatawithcustomroles.authUrl, body, {
150-
headers: testdatawithcustomroles.authHeaders,
151-
})
161+
`${testdatawithcustomroles.scope.join(
162+
' '
163+
)} ${testdatawithcustomroles.customRoles?.join?.(' ')}`
164+
)}&client_id=${testdatawithcustomroles.clientID}&client_secret=${
165+
testdatawithcustomroles.clientSecret
166+
}`
167+
expect(mockAxios.post).toHaveBeenCalledWith(
168+
testdatawithcustomroles.authUrl,
169+
body,
170+
{
171+
headers: testdatawithcustomroles.authHeaders,
172+
}
173+
)
152174
})
153175

154176
test('can auth with refresh token', async () => {
@@ -173,14 +195,39 @@ test('can auth anonymous, no custom roles', async () => {
173195
})
174196
})
175197

198+
test('handles auth anonymous with anonuserid', async () => {
199+
await Auth.Anonymous(testdata.clientID, testdata.scope, undefined, {
200+
anonuserid: 'myanonuserid',
201+
})
202+
203+
const body = `grant_type=client_credentials&client_id=${
204+
testdata.clientID
205+
}&scope=${urlencode(testdata.scope.join(' '))}&anonuserid=myanonuserid`
206+
expect(mockAxios.post).toHaveBeenCalledWith(testdata.authUrl, body, {
207+
headers: testdata.authHeaders,
208+
})
209+
})
210+
176211
test('can auth anonymous with custom roles', async () => {
177-
await Auth.Anonymous(testdatawithcustomroles.clientID, testdatawithcustomroles.scope, testdatawithcustomroles.customRoles)
212+
await Auth.Anonymous(
213+
testdatawithcustomroles.clientID,
214+
testdatawithcustomroles.scope,
215+
testdatawithcustomroles.customRoles
216+
)
178217
expect(mockAxios.post).toHaveBeenCalledTimes(1)
179218

180219
const body = `grant_type=client_credentials&client_id=${
181220
testdatawithcustomroles.clientID
182-
}&scope=${urlencode(`${testdatawithcustomroles.scope.join(' ')} ${testdatawithcustomroles.customRoles.join(' ')}`)}`
183-
expect(mockAxios.post).toHaveBeenCalledWith(testdatawithcustomroles.authUrl, body, {
184-
headers: testdatawithcustomroles.authHeaders,
185-
})
221+
}&scope=${urlencode(
222+
`${testdatawithcustomroles.scope.join(
223+
' '
224+
)} ${testdatawithcustomroles.customRoles?.join?.(' ')}`
225+
)}`
226+
expect(mockAxios.post).toHaveBeenCalledWith(
227+
testdatawithcustomroles.authUrl,
228+
body,
229+
{
230+
headers: testdatawithcustomroles.authHeaders,
231+
}
232+
)
186233
})

0 commit comments

Comments
 (0)