Skip to content

Commit c70c30c

Browse files
ajimaemaslowivan
andauthored
Remove redundant dependencies (#1830)
* Optimize auth.js defaultsDeep includes the lodash.defaultsdeep file what weight 51kb we can use just Object.assign here * chore(auth.js): merge remote branch - remove loadah.defaultsdeep library - remove object.assign and use spread instread * chore(sdk-auth): add lodash.defaultsdeep and refactor * chore(sdk-auth): remove lodash.defaultsdeep dependency from sdk-auth package * Create tiny-wombats-arrive.md Add changeset Co-authored-by: Ivan Maslov <cryptox3@gmail.com>
1 parent 43b8c12 commit c70c30c

5 files changed

Lines changed: 44 additions & 15 deletions

File tree

.changeset/tiny-wombats-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@commercetools/sdk-auth": patch
3+
---
4+
5+
Remove redundant dependencies and improve browser integration.

packages/sdk-auth/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
},
3737
"dependencies": {
3838
"@commercetools/sdk-middleware-http": "^7.0.0",
39-
"lodash.defaultsdeep": "^4.6.0",
4039
"qss": "2.0.3"
4140
},
4241
"devDependencies": {

packages/sdk-auth/src/auth.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* @flow */
2-
32
import type {
43
AuthOptions,
54
CustomAuthOptions,
@@ -9,7 +8,6 @@ import type {
98
UserAuthOptions,
109
} from 'types/sdk'
1110
import { decode, encode } from 'qss'
12-
import defaultsDeep from 'lodash.defaultsdeep'
1311
import { getErrorByCode } from '@commercetools/sdk-middleware-http'
1412
import * as constants from './constants'
1513

@@ -88,7 +86,8 @@ export default class SdkAuth {
8886
clientId,
8987
clientSecret,
9088
}: ClientAuthOptions): string {
91-
return Buffer.from(`${clientId}:${clientSecret}`).toString('base64')
89+
const targetStr = `${clientId}:${clientSecret}`
90+
return typeof Buffer === 'undefined' ? btoa(targetStr) : Buffer.from(targetStr).toString('base64')
9291
}
9392

9493
static _getScopes(scopes: ?Array<string>, projectKey: ?string): string {
@@ -221,7 +220,7 @@ export default class SdkAuth {
221220
const { uri, body, basicAuth, authType, headers } = request
222221
const fetchHeaders = headers || {
223222
Authorization: `${authType || constants.DEFAULT_AUTH_TYPE} ${basicAuth}`,
224-
'Content-Length': Buffer.byteLength(body).toString(),
223+
'Content-Length': JSON.stringify(body?.length || 0),
225224
'Content-Type': 'application/x-www-form-urlencoded',
226225
}
227226

@@ -237,10 +236,8 @@ export default class SdkAuth {
237236
}
238237

239238
_getRequestConfig(config: CustomAuthOptions = {}): AuthOptions {
240-
const mergedConfig = defaultsDeep({}, config, this.config)
241-
242-
// handle scopes array - defaultsDeep would merge arrays together
243-
// instead of taking its first occurrence
239+
// eslint-disable-next-line prefer-object-spread
240+
const mergedConfig = Object.assign({}, this.config, config)
244241
if (config.scopes) mergedConfig.scopes = config.scopes
245242

246243
return mergedConfig

packages/sdk-auth/test/token-provider.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,20 @@ describe('Token Provider', () => {
342342
expect(_tokenProvider.tokenInfo).toEqual(null)
343343
})
344344
})
345+
346+
describe('scopes', () => {
347+
const auth = new Auth(config)
348+
test('should include scope in the merged config', () => {
349+
expect(config.scopes).toBeFalsy()
350+
351+
const mergedConfig = auth._getRequestConfig({ scopes: ['new-scope:demo-projectKey'] })
352+
353+
expect(mergedConfig).toBeTruthy()
354+
expect(mergedConfig).toMatchObject(
355+
expect.objectContaining({
356+
scopes: ['new-scope:demo-projectKey']
357+
})
358+
)
359+
})
360+
})
345361
})

yarn.lock

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,14 @@
16251625
update-notifier "^2.2.0"
16261626
yargs "^8.0.2"
16271627

1628+
"@iarna/cli@^2.1.0":
1629+
version "2.1.0"
1630+
resolved "https://registry.yarnpkg.com/@iarna/cli/-/cli-2.1.0.tgz#f830356d54c72c804bd7afc43999de31e40fc3d6"
1631+
integrity sha512-rvVVqDa2g860niRbqs3D5RhL4la3dc1vwk+NlpKPZxKaMSHtE2se6C2x8NeveN+rcjp3/686X+u+09CZ+7lmAQ==
1632+
dependencies:
1633+
glob "^7.1.2"
1634+
signal-exit "^3.0.2"
1635+
16281636
"@istanbuljs/load-nyc-config@^1.0.0":
16291637
version "1.1.0"
16301638
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
@@ -9575,7 +9583,7 @@ locate-path@^6.0.0:
95759583
dependencies:
95769584
p-locate "^5.0.0"
95779585

9578-
lock-verify@^2.0.2, lock-verify@^2.1.0:
9586+
lock-verify@^2.0.2:
95799587
version "2.2.1"
95809588
resolved "https://registry.yarnpkg.com/lock-verify/-/lock-verify-2.2.1.tgz#81107948c51ed16f97b96ff8b60675affb243fc1"
95819589
integrity sha512-n0Zw2DVupKfZMazy/HIFVNohJ1z8fIoZ77WBnyyBGG6ixw83uJNyrbiJvvHWe1QKkGiBCjj8RCPlymltliqEww==
@@ -9584,6 +9592,15 @@ lock-verify@^2.0.2, lock-verify@^2.1.0:
95849592
npm-package-arg "^6.1.0"
95859593
semver "^5.4.1"
95869594

9595+
lock-verify@^2.1.0:
9596+
version "2.2.2"
9597+
resolved "https://registry.yarnpkg.com/lock-verify/-/lock-verify-2.2.2.tgz#9e93c0999dc3cbbede4f16f9cfdaa93ead8c76ef"
9598+
integrity sha512-2CUNtr1ZSVKJHcYP8uEzafmmuyauCB5zZimj8TvQd/Lflt9kXVZs+8S+EbAzZLaVUDn8CYGmeC3DFGdYfnCzeQ==
9599+
dependencies:
9600+
"@iarna/cli" "^2.1.0"
9601+
npm-package-arg "^6.1.0"
9602+
semver "^5.4.1"
9603+
95879604
lockfile@^1.0.4:
95889605
version "1.0.4"
95899606
resolved "https://registry.yarnpkg.com/lockfile/-/lockfile-1.0.4.tgz#07f819d25ae48f87e538e6578b6964a4981a5609"
@@ -9703,11 +9720,6 @@ lodash.defaults@^4.0.1, lodash.defaults@^4.2.0:
97039720
resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"
97049721
integrity sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==
97059722

9706-
lodash.defaultsdeep@^4.6.0:
9707-
version "4.6.1"
9708-
resolved "https://registry.yarnpkg.com/lodash.defaultsdeep/-/lodash.defaultsdeep-4.6.1.tgz#512e9bd721d272d94e3d3a63653fa17516741ca6"
9709-
integrity sha512-3j8wdDzYuWO3lM3Reg03MuQR957t287Rpcxp1njpEa8oDrikb+FwGdW3n+FELh/A6qib6yPit0j/pv9G/yeAqA==
9710-
97119723
lodash.difference@^4.5.0:
97129724
version "4.5.0"
97139725
resolved "https://registry.yarnpkg.com/lodash.difference/-/lodash.difference-4.5.0.tgz#9ccb4e505d486b91651345772885a2df27fd017c"

0 commit comments

Comments
 (0)