Skip to content

Commit f73d7fa

Browse files
author
Crhistian Ramirez
committed
🐛 cookie options not using defaults
1 parent 9cf8f42 commit f73d7fa

5 files changed

Lines changed: 53 additions & 13 deletions

File tree

codegen/templates/Configuration.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SdkConfiguration } from './models'
22

33
class Configuration {
4-
private config: SdkConfiguration = {
4+
private defaultConfig: SdkConfiguration = {
55
baseApiUrl: 'https://api.ordercloud.io',
66
apiVersion: 'v1',
77
timeoutInMilliseconds: 60 * 1000, // 60 seconds by default
@@ -14,6 +14,7 @@ class Configuration {
1414
path: '/', // accessible on any path in the domain
1515
},
1616
}
17+
private config: SdkConfiguration = cloneDeep(this.defaultConfig)
1718

1819
/**
1920
* @ignore
@@ -25,8 +26,9 @@ class Configuration {
2526
}
2627

2728
Set(config: SdkConfiguration): void {
28-
this.config = { ...this.config, ...(config || {}) }
29+
this.config = { ...this.defaultConfig, ...this.config, ...(config || {}) }
2930
this.config.cookieOptions = {
31+
...this.defaultConfig.cookieOptions,
3032
...this.config.cookieOptions,
3133
...(config?.cookieOptions || {}),
3234
}
@@ -37,4 +39,10 @@ class Configuration {
3739
}
3840
}
3941

42+
// takes an object and creates a new one with same properties/values
43+
// useful so we don't mutate original object
44+
function cloneDeep(obj: any): any {
45+
return JSON.parse(JSON.stringify(obj))
46+
}
47+
4048
export default new Configuration()

docs/assets/js/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ <h3>Type aliases</h3>
235235
<section class="tsd-index-section ">
236236
<h3>Functions</h3>
237237
<ul class="tsd-index-list">
238+
<li class="tsd-kind-function"><a href="globals.html#clonedeep" class="tsd-kind-icon">clone<wbr>Deep</a></li>
238239
<li class="tsd-kind-function"><a href="globals.html#geterrorcode" class="tsd-kind-icon">get<wbr>Error<wbr>Code</a></li>
239240
</ul>
240241
</section>
@@ -487,6 +488,26 @@ <h3>Xp<wbr>Thing<wbr>Type</h3>
487488
</section>
488489
<section class="tsd-panel-group tsd-member-group ">
489490
<h2>Functions</h2>
491+
<section class="tsd-panel tsd-member tsd-kind-function">
492+
<a name="clonedeep" class="tsd-anchor"></a>
493+
<h3>clone<wbr>Deep</h3>
494+
<ul class="tsd-signatures tsd-kind-function">
495+
<li class="tsd-signature tsd-kind-icon">clone<wbr>Deep<span class="tsd-signature-symbol">(</span>obj<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">any</span></li>
496+
</ul>
497+
<ul class="tsd-descriptions">
498+
<li class="tsd-description">
499+
<aside class="tsd-sources">
500+
</aside>
501+
<h4 class="tsd-parameters-title">Parameters</h4>
502+
<ul class="tsd-parameters">
503+
<li>
504+
<h5>obj: <span class="tsd-signature-type">any</span></h5>
505+
</li>
506+
</ul>
507+
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">any</span></h4>
508+
</li>
509+
</ul>
510+
</section>
490511
<section class="tsd-panel tsd-member tsd-kind-function">
491512
<a name="geterrorcode" class="tsd-anchor"></a>
492513
<h3>get<wbr>Error<wbr>Code</h3>
@@ -1035,6 +1056,9 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">string</s
10351056
<li class=" tsd-kind-type-alias">
10361057
<a href="index.html#xpthingtype" class="tsd-kind-icon">Xp<wbr>Thing<wbr>Type</a>
10371058
</li>
1059+
<li class=" tsd-kind-function">
1060+
<a href="globals.html#clonedeep" class="tsd-kind-icon">clone<wbr>Deep</a>
1061+
</li>
10381062
<li class=" tsd-kind-function">
10391063
<a href="globals.html#geterrorcode" class="tsd-kind-icon">get<wbr>Error<wbr>Code</a>
10401064
</li>

src/configuration.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SdkConfiguration } from './models'
22

33
class Configuration {
4-
private config: SdkConfiguration = {
4+
private defaultConfig: SdkConfiguration = {
55
baseApiUrl: 'https://api.ordercloud.io',
66
apiVersion: 'v1',
77
timeoutInMilliseconds: 60 * 1000, // 60 seconds by default
@@ -14,6 +14,7 @@ class Configuration {
1414
path: '/', // accessible on any path in the domain
1515
},
1616
}
17+
private config: SdkConfiguration = cloneDeep(this.defaultConfig)
1718

1819
/**
1920
* @ignore
@@ -25,8 +26,9 @@ class Configuration {
2526
}
2627

2728
Set(config: SdkConfiguration): void {
28-
this.config = { ...this.config, ...(config || {}) }
29+
this.config = { ...this.defaultConfig, ...this.config, ...(config || {}) }
2930
this.config.cookieOptions = {
31+
...this.defaultConfig.cookieOptions,
3032
...this.config.cookieOptions,
3133
...(config?.cookieOptions || {}),
3234
}
@@ -37,4 +39,10 @@ class Configuration {
3739
}
3840
}
3941

42+
// takes an object and creates a new one with same properties/values
43+
// useful so we don't mutate original object
44+
function cloneDeep(obj: any): any {
45+
return JSON.parse(JSON.stringify(obj))
46+
}
47+
4048
export default new Configuration()

tests/configuration.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ beforeEach(() => {
77
})
88

99
test('should handle modifying a top level property', () => {
10-
const newApiUrl = 'https://sandboxapi.ordercloud.io'
10+
const baseApiUrl = 'https://sandboxapi.ordercloud.io'
1111

1212
const expected = cloneDeep(defaultConfig) as SdkConfiguration
13-
expected.baseApiUrl = newApiUrl
14-
Configuration.Set({ baseApiUrl: newApiUrl })
13+
expected.baseApiUrl = baseApiUrl
14+
Configuration.Set({ baseApiUrl })
1515

16-
expect(expected).toMatchObject(Configuration.Get())
16+
expect(expected).toStrictEqual(Configuration.Get())
1717
})
1818

1919
test('should handle modifying a cookie option', () => {
20-
const newCookiePrefix = 'myprefix'
20+
const prefix = 'myprefix'
2121

2222
const expected = cloneDeep(defaultConfig) as SdkConfiguration
23-
expected.cookieOptions.prefix = newCookiePrefix
24-
Configuration.Set({ cookieOptions: { prefix: newCookiePrefix } })
23+
expected.cookieOptions.prefix = prefix
24+
Configuration.Set({ cookieOptions: { prefix } })
2525

26-
expect(expected).toMatchObject(Configuration.Get())
26+
expect(expected).toStrictEqual(Configuration.Get())
2727
})
2828

2929
// takes an object and creates a new one with same properties/values

0 commit comments

Comments
 (0)