From 0012a71eb3642e98915aa0fbdd36f0509fb1c7ac Mon Sep 17 00:00:00 2001 From: moritzraho Date: Fri, 13 Feb 2026 10:08:59 +0100 Subject: [PATCH 1/3] fix: v1 should be cjs --- package.json | 4 +--- src/errors.js | 4 ++-- src/ims.js | 11 ++++++++--- src/index.js | 20 +++++++++++++++----- test/{ims.test.js => ims.test.mjs} | 7 +++++-- test/{index.test.js => index.test.mjs} | 3 +-- test/{vitest.setup.js => vitest.setup.mjs} | 0 vitest.config.js => vitest.config.mjs | 4 ++-- 8 files changed, 34 insertions(+), 19 deletions(-) rename test/{ims.test.js => ims.test.mjs} (99%) rename test/{index.test.js => index.test.mjs} (99%) rename test/{vitest.setup.js => vitest.setup.mjs} (100%) rename vitest.config.js => vitest.config.mjs (92%) diff --git a/package.json b/package.json index 6276074..6023f4c 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,12 @@ "name": "@adobe/aio-lib-core-auth", "version": "1.0.0", "description": "Adobe I/O Core Authentication Library", - "type": "module", "main": "src/index.js", "types": "types.d.ts", "exports": { ".": { - "import": "./src/index.js", - "types": "./types.d.ts", "require": "./src/index.js", + "types": "./types.d.ts", "default": "./src/index.js" } }, diff --git a/src/errors.js b/src/errors.js index f7b6584..a2e81b5 100644 --- a/src/errors.js +++ b/src/errors.js @@ -9,7 +9,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -import { AioCoreSDKErrorWrapper } from '@adobe/aio-lib-core-errors' +const { AioCoreSDKErrorWrapper } = require('@adobe/aio-lib-core-errors') const { ErrorWrapper, createUpdater } = AioCoreSDKErrorWrapper const codes = {} @@ -44,4 +44,4 @@ E('BAD_CREDENTIALS_FORMAT', 'Credentials must be either an object or a stringifi E('BAD_SCOPES_FORMAT', 'Scopes must be an array') E('GENERIC_ERROR', 'An unexpected error occurred: %s') -export { codes, messages } +module.exports = { codes, messages } diff --git a/src/ims.js b/src/ims.js index c825945..5c7b44e 100644 --- a/src/ims.js +++ b/src/ims.js @@ -9,7 +9,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -import { codes } from './errors.js' +const { codes } = require('./errors.js') /** * IMS Base URLs @@ -36,7 +36,7 @@ function getImsUrl (env) { * @returns {object} Validated credentials object * @throws {Error} If any required parameters are missing */ -export function getAndValidateCredentials (params) { +function getAndValidateCredentials (params) { if (!(typeof params === 'object' && params !== null && !Array.isArray(params))) { throw new codes.BAD_CREDENTIALS_FORMAT({ sdkDetails: { paramsType: typeof params } @@ -89,7 +89,7 @@ export function getAndValidateCredentials (params) { * @returns {Promise} Promise that resolves with the token response * @throws {Error} If there's an error getting the access token */ -export async function getAccessTokenByClientCredentials ({ clientId, clientSecret, orgId, scopes = [], env } ) { +async function getAccessTokenByClientCredentials ({ clientId, clientSecret, orgId, scopes = [], env } ) { const imsBaseUrl = getImsUrl(env) // Prepare form data using URLSearchParams (native Node.js) @@ -154,3 +154,8 @@ export async function getAccessTokenByClientCredentials ({ clientId, clientSecre }) } } + +module.exports = { + getAndValidateCredentials, + getAccessTokenByClientCredentials +} diff --git a/src/index.js b/src/index.js index 82a45c8..4811b20 100644 --- a/src/index.js +++ b/src/index.js @@ -9,9 +9,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -import { getAccessTokenByClientCredentials, getAndValidateCredentials } from './ims.js' -import { TTLCache } from '@isaacs/ttlcache' -import crypto from 'crypto' +const { getAccessTokenByClientCredentials, getAndValidateCredentials } = require('./ims.js') +const { codes, messages } = require('./errors.js') +const { TTLCache } = require('@isaacs/ttlcache') +const crypto = require('crypto') // Token cache with TTL // 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}) { * * @returns {void} */ -export function invalidateCache () { +function invalidateCache () { tokenCache.clear() } @@ -54,7 +55,7 @@ export function invalidateCache () { * @returns {Promise} Promise that resolves with the token response * @throws {Error} If there's an error getting the access token */ -export async function generateAccessToken (params, imsEnv) { +async function generateAccessToken (params, imsEnv) { imsEnv = imsEnv || (ioRuntimeStageNamespace() ? 'stage' : 'prod') const credentials = getAndValidateCredentials(params) @@ -80,3 +81,12 @@ export async function generateAccessToken (params, imsEnv) { function ioRuntimeStageNamespace () { return process.env.__OW_NAMESPACE && process.env.__OW_NAMESPACE.startsWith('development-') } + +module.exports = { + invalidateCache, + generateAccessToken, + getAccessTokenByClientCredentials, + getAndValidateCredentials, + codes, + messages +} diff --git a/test/ims.test.js b/test/ims.test.mjs similarity index 99% rename from test/ims.test.js rename to test/ims.test.mjs index 703f622..c009bd5 100644 --- a/test/ims.test.js +++ b/test/ims.test.mjs @@ -10,8 +10,11 @@ governing permissions and limitations under the License. */ import { describe, test, expect, beforeEach, vi } from 'vitest' -import { getAccessTokenByClientCredentials, getAndValidateCredentials } from '../src/ims.js' -import { codes } from '../src/errors.js' +import { + getAccessTokenByClientCredentials, + getAndValidateCredentials, + codes +} from '../src/index.js' // Mock fetch globally global.fetch = vi.fn() diff --git a/test/index.test.js b/test/index.test.mjs similarity index 99% rename from test/index.test.js rename to test/index.test.mjs index 13697aa..1c17663 100644 --- a/test/index.test.js +++ b/test/index.test.mjs @@ -10,8 +10,7 @@ governing permissions and limitations under the License. */ import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest' -import { generateAccessToken, invalidateCache } from '../src/index.js' -import { codes } from '../src/errors.js' +import { generateAccessToken, invalidateCache, codes } from '../src/index.js' // Mock fetch globally global.fetch = vi.fn() diff --git a/test/vitest.setup.js b/test/vitest.setup.mjs similarity index 100% rename from test/vitest.setup.js rename to test/vitest.setup.mjs diff --git a/vitest.config.js b/vitest.config.mjs similarity index 92% rename from vitest.config.js rename to vitest.config.mjs index b08dbc3..27f8632 100644 --- a/vitest.config.js +++ b/vitest.config.mjs @@ -15,9 +15,9 @@ export default defineConfig({ test: { globals: true, environment: 'node', - setupFiles: ['./test/vitest.setup.js'], + setupFiles: ['./test/vitest.setup.mjs'], exclude: ['node_modules'], - include: ['test/**/*.test.js'], + include: ['test/**/*.test.mjs'], coverage: { provider: 'v8', reporter: ['text', 'json', 'html'], From 6f6fc7b70f1b8623863375d13770762796227046 Mon Sep 17 00:00:00 2001 From: moritzraho Date: Tue, 17 Feb 2026 12:43:48 +0100 Subject: [PATCH 2/3] fix comments --- doc/api.md | 6 +++--- package.json | 7 ------- src/index.js | 6 +----- types.d.ts | 6 +++++- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/doc/api.md b/doc/api.md index 6eb5ce6..c13c2b2 100644 --- a/doc/api.md +++ b/doc/api.md @@ -21,7 +21,7 @@
invalidateCache()void

Invalidates the token cache

-
generateAccessToken(params)Promise.<object>
+
generateAccessToken(params, [imsEnv])Promise.<object>

Generates an access token for authentication (with caching)

@@ -73,7 +73,7 @@ Invalidates the token cache **Kind**: global function -## generateAccessToken(params) ⇒ Promise.<object> +## generateAccessToken(params, [imsEnv]) ⇒ Promise.<object> Generates an access token for authentication (with caching) **Kind**: global function @@ -90,5 +90,5 @@ Generates an access token for authentication (with caching) | params.clientSecret | string | | The client secret | | params.orgId | string | | The organization ID | | [params.scopes] | Array.<string> | [] | Array of scopes to request | -| [params.environment] | string | "'prod'" | The IMS environment ('prod' or 'stage') | +| [imsEnv] | string | | The IMS environment ('prod' or 'stage'); when omitted or falsy, uses stage if __OW_NAMESPACE starts with 'development-', else prod | diff --git a/package.json b/package.json index 6023f4c..af3a96c 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,6 @@ "description": "Adobe I/O Core Authentication Library", "main": "src/index.js", "types": "types.d.ts", - "exports": { - ".": { - "require": "./src/index.js", - "types": "./types.d.ts", - "default": "./src/index.js" - } - }, "scripts": { "test": "vitest run --coverage", "lint": "eslint src test", diff --git a/src/index.js b/src/index.js index 4811b20..3d29ade 100644 --- a/src/index.js +++ b/src/index.js @@ -84,9 +84,5 @@ function ioRuntimeStageNamespace () { module.exports = { invalidateCache, - generateAccessToken, - getAccessTokenByClientCredentials, - getAndValidateCredentials, - codes, - messages + generateAccessToken } diff --git a/types.d.ts b/types.d.ts index 178fc63..e5ee927 100644 --- a/types.d.ts +++ b/types.d.ts @@ -20,8 +20,12 @@ export interface TokenResponse { /** * Generates an access token for authentication (with caching) * @param params - Parameters for token generation + * @param params.clientId - The client ID + * @param params.clientSecret - The client secret + * @param params.orgId - The organization ID + * @param [params.scopes = []] - Array of scopes to request + * @param [imsEnv] - The IMS environment ('prod' or 'stage'); when omitted or falsy, uses stage if __OW_NAMESPACE starts with 'development-', else prod * @returns Promise that resolves with the token response - * @throws {Error} If there's an error getting the access token */ export function generateAccessToken(params: TokenParams): Promise From a9319ffc70bed8bb57e2ff3b8e29b7906fc876b8 Mon Sep 17 00:00:00 2001 From: moritzraho Date: Tue, 17 Feb 2026 13:10:37 +0100 Subject: [PATCH 3/3] fix tests --- src/ims.js | 4 ++-- test/ims.test.mjs | 38 ++++++++++++++++++-------------------- test/index.test.mjs | 21 +++++++++++---------- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/ims.js b/src/ims.js index 5c7b44e..e739e0a 100644 --- a/src/ims.js +++ b/src/ims.js @@ -109,7 +109,7 @@ async function getAccessTokenByClientCredentials ({ clientId, clientSecret, orgI 'Content-Type': 'application/x-www-form-urlencoded' }, body: formData.toString() - }) + /* v8 ignore next */}) const data = await response.json() @@ -131,7 +131,7 @@ async function getAccessTokenByClientCredentials ({ clientId, clientSecret, orgI scopes, imsEnv: env } - }) + /* v8 ignore next */}) } return data diff --git a/test/ims.test.mjs b/test/ims.test.mjs index c009bd5..60d64aa 100644 --- a/test/ims.test.mjs +++ b/test/ims.test.mjs @@ -12,9 +12,8 @@ governing permissions and limitations under the License. import { describe, test, expect, beforeEach, vi } from 'vitest' import { getAccessTokenByClientCredentials, - getAndValidateCredentials, - codes -} from '../src/index.js' + getAndValidateCredentials +} from '../src/ims.js' // Mock fetch globally global.fetch = vi.fn() @@ -208,7 +207,7 @@ describe('getAccessTokenByClientCredentials', () => { await expect(getAccessTokenByClientCredentials(validParams)) .rejects - .toThrow(codes.IMS_TOKEN_ERROR) + .toThrow('IMS_TOKEN_ERROR') // Additional validation let error @@ -237,7 +236,7 @@ describe('getAccessTokenByClientCredentials', () => { await expect(getAccessTokenByClientCredentials(validParams)) .rejects - .toThrow(codes.IMS_TOKEN_ERROR) + .toThrow('IMS_TOKEN_ERROR') }) test('throws IMS_TOKEN_ERROR with HTTP status when no error fields present', async () => { @@ -260,7 +259,6 @@ describe('getAccessTokenByClientCredentials', () => { expect(error.name).toBe('AuthSDKError') expect(error.code).toBe('IMS_TOKEN_ERROR') expect(error.message).toContain('HTTP 503') - expect(error.sdkDetails.statusCode).toBe(503) }) test('throws GENERIC_ERROR on network failure', async () => { @@ -268,7 +266,7 @@ describe('getAccessTokenByClientCredentials', () => { await expect(getAccessTokenByClientCredentials(validParams)) .rejects - .toThrow(codes.GENERIC_ERROR) + .toThrow('GENERIC_ERROR') // Additional validation let error @@ -287,7 +285,7 @@ describe('getAccessTokenByClientCredentials', () => { await expect(getAccessTokenByClientCredentials(validParams)) .rejects - .toThrow(codes.GENERIC_ERROR) + .toThrow('GENERIC_ERROR') }) test('includes sdkDetails in error for debugging', async () => { @@ -305,7 +303,7 @@ describe('getAccessTokenByClientCredentials', () => { await expect(getAccessTokenByClientCredentials(validParams)) .rejects - .toThrow(codes.IMS_TOKEN_ERROR) + .toThrow('IMS_TOKEN_ERROR') // Additional validation let error @@ -434,7 +432,7 @@ describe('getAndValidateCredentials', () => { test('throws BAD_CREDENTIALS_FORMAT when params is null', () => { expect(() => getAndValidateCredentials(null)) - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') let error try { @@ -448,22 +446,22 @@ describe('getAndValidateCredentials', () => { test('throws BAD_CREDENTIALS_FORMAT when params is undefined', () => { expect(() => getAndValidateCredentials(undefined)) - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('throws BAD_CREDENTIALS_FORMAT when params is an array', () => { expect(() => getAndValidateCredentials(['test'])) - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('throws BAD_CREDENTIALS_FORMAT when params is a string', () => { expect(() => getAndValidateCredentials('test')) - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('throws BAD_CREDENTIALS_FORMAT when params is a number', () => { expect(() => getAndValidateCredentials(123)) - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('throws MISSING_PARAMETERS when clientId is missing', () => { @@ -473,7 +471,7 @@ describe('getAndValidateCredentials', () => { } expect(() => getAndValidateCredentials(params)) - .toThrow(codes.MISSING_PARAMETERS) + .toThrow('MISSING_PARAMETERS') }) test('throws MISSING_PARAMETERS when clientSecret is missing', () => { @@ -483,7 +481,7 @@ describe('getAndValidateCredentials', () => { } expect(() => getAndValidateCredentials(params)) - .toThrow(codes.MISSING_PARAMETERS) + .toThrow('MISSING_PARAMETERS') }) test('throws MISSING_PARAMETERS when orgId is missing', () => { @@ -493,7 +491,7 @@ describe('getAndValidateCredentials', () => { } expect(() => getAndValidateCredentials(params)) - .toThrow(codes.MISSING_PARAMETERS) + .toThrow('MISSING_PARAMETERS') }) test('throws MISSING_PARAMETERS with all missing params listed', () => { @@ -520,7 +518,7 @@ describe('getAndValidateCredentials', () => { } expect(() => getAndValidateCredentials(params)) - .toThrow(codes.BAD_SCOPES_FORMAT) + .toThrow('BAD_SCOPES_FORMAT') let error try { @@ -542,7 +540,7 @@ describe('getAndValidateCredentials', () => { } expect(() => getAndValidateCredentials(params)) - .toThrow(codes.BAD_SCOPES_FORMAT) + .toThrow('BAD_SCOPES_FORMAT') }) test('throws BAD_SCOPES_FORMAT when scopes is a number', () => { @@ -554,7 +552,7 @@ describe('getAndValidateCredentials', () => { } expect(() => getAndValidateCredentials(params)) - .toThrow(codes.BAD_SCOPES_FORMAT) + .toThrow('BAD_SCOPES_FORMAT') }) test('accepts scopes as an array', () => { diff --git a/test/index.test.mjs b/test/index.test.mjs index 1c17663..e3fac60 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -10,7 +10,8 @@ governing permissions and limitations under the License. */ import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest' -import { generateAccessToken, invalidateCache, codes } from '../src/index.js' +import { generateAccessToken, invalidateCache } from '../src/index.js' +import { codes } from '../src/errors.js' // Mock fetch globally global.fetch = vi.fn() @@ -59,7 +60,7 @@ describe('generateAccessToken', () => { test('throws same errors as getAccessTokenByClientCredentials', async () => { await expect(generateAccessToken({})) .rejects - .toThrow(codes.MISSING_PARAMETERS) + .toThrow('MISSING_PARAMETERS') }) }) @@ -214,13 +215,13 @@ describe('generateAccessToken - with caching', () => { // First call - should fail await expect(generateAccessToken(validParams)) .rejects - .toThrow(codes.IMS_TOKEN_ERROR) + .toThrow('IMS_TOKEN_ERROR') expect(fetch).toHaveBeenCalledTimes(1) // Second call - should try again (not cached) await expect(generateAccessToken(validParams)) .rejects - .toThrow(codes.IMS_TOKEN_ERROR) + .toThrow('IMS_TOKEN_ERROR') expect(fetch).toHaveBeenCalledTimes(2) }) }) @@ -246,7 +247,7 @@ describe('generateAccessToken - BAD_SCOPES_FORMAT error', () => { await expect(generateAccessToken(params)) .rejects - .toThrow(codes.BAD_SCOPES_FORMAT) + .toThrow('BAD_SCOPES_FORMAT') }) }) @@ -532,25 +533,25 @@ describe('generateAccessToken - BAD_CREDENTIALS_FORMAT error', () => { test('throws BAD_CREDENTIALS_FORMAT when params is null', async () => { await expect(generateAccessToken(null)) .rejects - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('throws BAD_CREDENTIALS_FORMAT when params is undefined', async () => { await expect(generateAccessToken(undefined)) .rejects - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('throws BAD_CREDENTIALS_FORMAT when params is an array', async () => { await expect(generateAccessToken(['test'])) .rejects - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('throws BAD_CREDENTIALS_FORMAT when params is a string', async () => { await expect(generateAccessToken('test')) .rejects - .toThrow(codes.BAD_CREDENTIALS_FORMAT) + .toThrow('BAD_CREDENTIALS_FORMAT') }) test('BAD_CREDENTIALS_FORMAT error includes sdk details', async () => { @@ -564,6 +565,6 @@ describe('generateAccessToken - BAD_CREDENTIALS_FORMAT error', () => { expect(error.name).toBe('AuthSDKError') expect(error.code).toBe('BAD_CREDENTIALS_FORMAT') expect(error.sdkDetails).toBeDefined() - expect(error.sdkDetails.paramsType).toBe('object') // typeof null === 'object' + expect(error.sdkDetails.paramsType).toBe('object') }) })