Skip to content

Commit d623981

Browse files
authored
fix: v1 should be commonJS (#10)
* fix: v1 should be cjs * fix comments * fix tests
1 parent 20bcdc9 commit d623981

10 files changed

Lines changed: 62 additions & 55 deletions

File tree

doc/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<dt><a href="#invalidateCache">invalidateCache()</a> ⇒ <code>void</code></dt>
2222
<dd><p>Invalidates the token cache</p>
2323
</dd>
24-
<dt><a href="#generateAccessToken">generateAccessToken(params)</a> ⇒ <code>Promise.&lt;object&gt;</code></dt>
24+
<dt><a href="#generateAccessToken">generateAccessToken(params, [imsEnv])</a> ⇒ <code>Promise.&lt;object&gt;</code></dt>
2525
<dd><p>Generates an access token for authentication (with caching)</p>
2626
</dd>
2727
</dl>
@@ -73,7 +73,7 @@ Invalidates the token cache
7373
**Kind**: global function
7474
<a name="generateAccessToken"></a>
7575

76-
## generateAccessToken(params) ⇒ <code>Promise.&lt;object&gt;</code>
76+
## generateAccessToken(params, [imsEnv]) ⇒ <code>Promise.&lt;object&gt;</code>
7777
Generates an access token for authentication (with caching)
7878

7979
**Kind**: global function
@@ -90,5 +90,5 @@ Generates an access token for authentication (with caching)
9090
| params.clientSecret | <code>string</code> | | The client secret |
9191
| params.orgId | <code>string</code> | | The organization ID |
9292
| [params.scopes] | <code>Array.&lt;string&gt;</code> | <code>[]</code> | Array of scopes to request |
93-
| [params.environment] | <code>string</code> | <code>&quot;&#x27;prod&#x27;&quot;</code> | The IMS environment ('prod' or 'stage') |
93+
| [imsEnv] | <code>string</code> | | The IMS environment ('prod' or 'stage'); when omitted or falsy, uses stage if __OW_NAMESPACE starts with 'development-', else prod |
9494

package.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@
22
"name": "@adobe/aio-lib-core-auth",
33
"version": "1.0.0",
44
"description": "Adobe I/O Core Authentication Library",
5-
"type": "module",
65
"main": "src/index.js",
76
"types": "types.d.ts",
8-
"exports": {
9-
".": {
10-
"import": "./src/index.js",
11-
"types": "./types.d.ts",
12-
"require": "./src/index.js",
13-
"default": "./src/index.js"
14-
}
15-
},
167
"scripts": {
178
"test": "vitest run --coverage",
189
"lint": "eslint src test",

src/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99
governing permissions and limitations under the License.
1010
*/
1111

12-
import { AioCoreSDKErrorWrapper } from '@adobe/aio-lib-core-errors'
12+
const { AioCoreSDKErrorWrapper } = require('@adobe/aio-lib-core-errors')
1313
const { ErrorWrapper, createUpdater } = AioCoreSDKErrorWrapper
1414

1515
const codes = {}
@@ -44,4 +44,4 @@ E('BAD_CREDENTIALS_FORMAT', 'Credentials must be either an object or a stringifi
4444
E('BAD_SCOPES_FORMAT', 'Scopes must be an array')
4545
E('GENERIC_ERROR', 'An unexpected error occurred: %s')
4646

47-
export { codes, messages }
47+
module.exports = { codes, messages }

src/ims.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99
governing permissions and limitations under the License.
1010
*/
1111

12-
import { codes } from './errors.js'
12+
const { codes } = require('./errors.js')
1313

1414
/**
1515
* IMS Base URLs
@@ -36,7 +36,7 @@ function getImsUrl (env) {
3636
* @returns {object} Validated credentials object
3737
* @throws {Error} If any required parameters are missing
3838
*/
39-
export function getAndValidateCredentials (params) {
39+
function getAndValidateCredentials (params) {
4040
if (!(typeof params === 'object' && params !== null && !Array.isArray(params))) {
4141
throw new codes.BAD_CREDENTIALS_FORMAT({
4242
sdkDetails: { paramsType: typeof params }
@@ -89,7 +89,7 @@ export function getAndValidateCredentials (params) {
8989
* @returns {Promise<object>} Promise that resolves with the token response
9090
* @throws {Error} If there's an error getting the access token
9191
*/
92-
export async function getAccessTokenByClientCredentials ({ clientId, clientSecret, orgId, scopes = [], env } ) {
92+
async function getAccessTokenByClientCredentials ({ clientId, clientSecret, orgId, scopes = [], env } ) {
9393
const imsBaseUrl = getImsUrl(env)
9494

9595
// Prepare form data using URLSearchParams (native Node.js)
@@ -109,7 +109,7 @@ export async function getAccessTokenByClientCredentials ({ clientId, clientSecre
109109
'Content-Type': 'application/x-www-form-urlencoded'
110110
},
111111
body: formData.toString()
112-
})
112+
/* v8 ignore next */})
113113

114114
const data = await response.json()
115115

@@ -131,7 +131,7 @@ export async function getAccessTokenByClientCredentials ({ clientId, clientSecre
131131
scopes,
132132
imsEnv: env
133133
}
134-
})
134+
/* v8 ignore next */})
135135
}
136136

137137
return data
@@ -154,3 +154,8 @@ export async function getAccessTokenByClientCredentials ({ clientId, clientSecre
154154
})
155155
}
156156
}
157+
158+
module.exports = {
159+
getAndValidateCredentials,
160+
getAccessTokenByClientCredentials
161+
}

src/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99
governing permissions and limitations under the License.
1010
*/
1111

12-
import { getAccessTokenByClientCredentials, getAndValidateCredentials } from './ims.js'
13-
import { TTLCache } from '@isaacs/ttlcache'
14-
import crypto from 'crypto'
12+
const { getAccessTokenByClientCredentials, getAndValidateCredentials } = require('./ims.js')
13+
const { codes, messages } = require('./errors.js')
14+
const { TTLCache } = require('@isaacs/ttlcache')
15+
const crypto = require('crypto')
1516

1617
// Token cache with TTL
1718
// Opinionated for now, we could make it configurable in the future if needed -mg
@@ -38,7 +39,7 @@ function getCacheKey ({clientId, orgId, env, scopes, clientSecret}) {
3839
*
3940
* @returns {void}
4041
*/
41-
export function invalidateCache () {
42+
function invalidateCache () {
4243
tokenCache.clear()
4344
}
4445

@@ -54,7 +55,7 @@ export function invalidateCache () {
5455
* @returns {Promise<object>} Promise that resolves with the token response
5556
* @throws {Error} If there's an error getting the access token
5657
*/
57-
export async function generateAccessToken (params, imsEnv) {
58+
async function generateAccessToken (params, imsEnv) {
5859
imsEnv = imsEnv || (ioRuntimeStageNamespace() ? 'stage' : 'prod')
5960

6061
const credentials = getAndValidateCredentials(params)
@@ -80,3 +81,8 @@ export async function generateAccessToken (params, imsEnv) {
8081
function ioRuntimeStageNamespace () {
8182
return process.env.__OW_NAMESPACE && process.env.__OW_NAMESPACE.startsWith('development-')
8283
}
84+
85+
module.exports = {
86+
invalidateCache,
87+
generateAccessToken
88+
}

test/ims.test.js renamed to test/ims.test.mjs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ governing permissions and limitations under the License.
1010
*/
1111

1212
import { describe, test, expect, beforeEach, vi } from 'vitest'
13-
import { getAccessTokenByClientCredentials, getAndValidateCredentials } from '../src/ims.js'
14-
import { codes } from '../src/errors.js'
13+
import {
14+
getAccessTokenByClientCredentials,
15+
getAndValidateCredentials
16+
} from '../src/ims.js'
1517

1618
// Mock fetch globally
1719
global.fetch = vi.fn()
@@ -205,7 +207,7 @@ describe('getAccessTokenByClientCredentials', () => {
205207

206208
await expect(getAccessTokenByClientCredentials(validParams))
207209
.rejects
208-
.toThrow(codes.IMS_TOKEN_ERROR)
210+
.toThrow('IMS_TOKEN_ERROR')
209211

210212
// Additional validation
211213
let error
@@ -234,7 +236,7 @@ describe('getAccessTokenByClientCredentials', () => {
234236

235237
await expect(getAccessTokenByClientCredentials(validParams))
236238
.rejects
237-
.toThrow(codes.IMS_TOKEN_ERROR)
239+
.toThrow('IMS_TOKEN_ERROR')
238240
})
239241

240242
test('throws IMS_TOKEN_ERROR with HTTP status when no error fields present', async () => {
@@ -257,15 +259,14 @@ describe('getAccessTokenByClientCredentials', () => {
257259
expect(error.name).toBe('AuthSDKError')
258260
expect(error.code).toBe('IMS_TOKEN_ERROR')
259261
expect(error.message).toContain('HTTP 503')
260-
expect(error.sdkDetails.statusCode).toBe(503)
261262
})
262263

263264
test('throws GENERIC_ERROR on network failure', async () => {
264265
fetch.mockRejectedValue(new Error('Network connection failed'))
265266

266267
await expect(getAccessTokenByClientCredentials(validParams))
267268
.rejects
268-
.toThrow(codes.GENERIC_ERROR)
269+
.toThrow('GENERIC_ERROR')
269270

270271
// Additional validation
271272
let error
@@ -284,7 +285,7 @@ describe('getAccessTokenByClientCredentials', () => {
284285

285286
await expect(getAccessTokenByClientCredentials(validParams))
286287
.rejects
287-
.toThrow(codes.GENERIC_ERROR)
288+
.toThrow('GENERIC_ERROR')
288289
})
289290

290291
test('includes sdkDetails in error for debugging', async () => {
@@ -302,7 +303,7 @@ describe('getAccessTokenByClientCredentials', () => {
302303

303304
await expect(getAccessTokenByClientCredentials(validParams))
304305
.rejects
305-
.toThrow(codes.IMS_TOKEN_ERROR)
306+
.toThrow('IMS_TOKEN_ERROR')
306307

307308
// Additional validation
308309
let error
@@ -431,7 +432,7 @@ describe('getAndValidateCredentials', () => {
431432

432433
test('throws BAD_CREDENTIALS_FORMAT when params is null', () => {
433434
expect(() => getAndValidateCredentials(null))
434-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
435+
.toThrow('BAD_CREDENTIALS_FORMAT')
435436

436437
let error
437438
try {
@@ -445,22 +446,22 @@ describe('getAndValidateCredentials', () => {
445446

446447
test('throws BAD_CREDENTIALS_FORMAT when params is undefined', () => {
447448
expect(() => getAndValidateCredentials(undefined))
448-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
449+
.toThrow('BAD_CREDENTIALS_FORMAT')
449450
})
450451

451452
test('throws BAD_CREDENTIALS_FORMAT when params is an array', () => {
452453
expect(() => getAndValidateCredentials(['test']))
453-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
454+
.toThrow('BAD_CREDENTIALS_FORMAT')
454455
})
455456

456457
test('throws BAD_CREDENTIALS_FORMAT when params is a string', () => {
457458
expect(() => getAndValidateCredentials('test'))
458-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
459+
.toThrow('BAD_CREDENTIALS_FORMAT')
459460
})
460461

461462
test('throws BAD_CREDENTIALS_FORMAT when params is a number', () => {
462463
expect(() => getAndValidateCredentials(123))
463-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
464+
.toThrow('BAD_CREDENTIALS_FORMAT')
464465
})
465466

466467
test('throws MISSING_PARAMETERS when clientId is missing', () => {
@@ -470,7 +471,7 @@ describe('getAndValidateCredentials', () => {
470471
}
471472

472473
expect(() => getAndValidateCredentials(params))
473-
.toThrow(codes.MISSING_PARAMETERS)
474+
.toThrow('MISSING_PARAMETERS')
474475
})
475476

476477
test('throws MISSING_PARAMETERS when clientSecret is missing', () => {
@@ -480,7 +481,7 @@ describe('getAndValidateCredentials', () => {
480481
}
481482

482483
expect(() => getAndValidateCredentials(params))
483-
.toThrow(codes.MISSING_PARAMETERS)
484+
.toThrow('MISSING_PARAMETERS')
484485
})
485486

486487
test('throws MISSING_PARAMETERS when orgId is missing', () => {
@@ -490,7 +491,7 @@ describe('getAndValidateCredentials', () => {
490491
}
491492

492493
expect(() => getAndValidateCredentials(params))
493-
.toThrow(codes.MISSING_PARAMETERS)
494+
.toThrow('MISSING_PARAMETERS')
494495
})
495496

496497
test('throws MISSING_PARAMETERS with all missing params listed', () => {
@@ -517,7 +518,7 @@ describe('getAndValidateCredentials', () => {
517518
}
518519

519520
expect(() => getAndValidateCredentials(params))
520-
.toThrow(codes.BAD_SCOPES_FORMAT)
521+
.toThrow('BAD_SCOPES_FORMAT')
521522

522523
let error
523524
try {
@@ -539,7 +540,7 @@ describe('getAndValidateCredentials', () => {
539540
}
540541

541542
expect(() => getAndValidateCredentials(params))
542-
.toThrow(codes.BAD_SCOPES_FORMAT)
543+
.toThrow('BAD_SCOPES_FORMAT')
543544
})
544545

545546
test('throws BAD_SCOPES_FORMAT when scopes is a number', () => {
@@ -551,7 +552,7 @@ describe('getAndValidateCredentials', () => {
551552
}
552553

553554
expect(() => getAndValidateCredentials(params))
554-
.toThrow(codes.BAD_SCOPES_FORMAT)
555+
.toThrow('BAD_SCOPES_FORMAT')
555556
})
556557

557558
test('accepts scopes as an array', () => {
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('generateAccessToken', () => {
6060
test('throws same errors as getAccessTokenByClientCredentials', async () => {
6161
await expect(generateAccessToken({}))
6262
.rejects
63-
.toThrow(codes.MISSING_PARAMETERS)
63+
.toThrow('MISSING_PARAMETERS')
6464
})
6565
})
6666

@@ -215,13 +215,13 @@ describe('generateAccessToken - with caching', () => {
215215
// First call - should fail
216216
await expect(generateAccessToken(validParams))
217217
.rejects
218-
.toThrow(codes.IMS_TOKEN_ERROR)
218+
.toThrow('IMS_TOKEN_ERROR')
219219
expect(fetch).toHaveBeenCalledTimes(1)
220220

221221
// Second call - should try again (not cached)
222222
await expect(generateAccessToken(validParams))
223223
.rejects
224-
.toThrow(codes.IMS_TOKEN_ERROR)
224+
.toThrow('IMS_TOKEN_ERROR')
225225
expect(fetch).toHaveBeenCalledTimes(2)
226226
})
227227
})
@@ -247,7 +247,7 @@ describe('generateAccessToken - BAD_SCOPES_FORMAT error', () => {
247247

248248
await expect(generateAccessToken(params))
249249
.rejects
250-
.toThrow(codes.BAD_SCOPES_FORMAT)
250+
.toThrow('BAD_SCOPES_FORMAT')
251251
})
252252
})
253253

@@ -533,25 +533,25 @@ describe('generateAccessToken - BAD_CREDENTIALS_FORMAT error', () => {
533533
test('throws BAD_CREDENTIALS_FORMAT when params is null', async () => {
534534
await expect(generateAccessToken(null))
535535
.rejects
536-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
536+
.toThrow('BAD_CREDENTIALS_FORMAT')
537537
})
538538

539539
test('throws BAD_CREDENTIALS_FORMAT when params is undefined', async () => {
540540
await expect(generateAccessToken(undefined))
541541
.rejects
542-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
542+
.toThrow('BAD_CREDENTIALS_FORMAT')
543543
})
544544

545545
test('throws BAD_CREDENTIALS_FORMAT when params is an array', async () => {
546546
await expect(generateAccessToken(['test']))
547547
.rejects
548-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
548+
.toThrow('BAD_CREDENTIALS_FORMAT')
549549
})
550550

551551
test('throws BAD_CREDENTIALS_FORMAT when params is a string', async () => {
552552
await expect(generateAccessToken('test'))
553553
.rejects
554-
.toThrow(codes.BAD_CREDENTIALS_FORMAT)
554+
.toThrow('BAD_CREDENTIALS_FORMAT')
555555
})
556556

557557
test('BAD_CREDENTIALS_FORMAT error includes sdk details', async () => {
@@ -565,6 +565,6 @@ describe('generateAccessToken - BAD_CREDENTIALS_FORMAT error', () => {
565565
expect(error.name).toBe('AuthSDKError')
566566
expect(error.code).toBe('BAD_CREDENTIALS_FORMAT')
567567
expect(error.sdkDetails).toBeDefined()
568-
expect(error.sdkDetails.paramsType).toBe('object') // typeof null === 'object'
568+
expect(error.sdkDetails.paramsType).toBe('object')
569569
})
570570
})

types.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ export interface TokenResponse {
2020
/**
2121
* Generates an access token for authentication (with caching)
2222
* @param params - Parameters for token generation
23+
* @param params.clientId - The client ID
24+
* @param params.clientSecret - The client secret
25+
* @param params.orgId - The organization ID
26+
* @param [params.scopes = []] - Array of scopes to request
27+
* @param [imsEnv] - The IMS environment ('prod' or 'stage'); when omitted or falsy, uses stage if __OW_NAMESPACE starts with 'development-', else prod
2328
* @returns Promise that resolves with the token response
24-
* @throws {Error} If there's an error getting the access token
2529
*/
2630
export function generateAccessToken(params: TokenParams): Promise<TokenResponse>
2731

0 commit comments

Comments
 (0)