Skip to content

Commit b3758d1

Browse files
refactor: extract parseAudience type guard to shared location
- Add AthenaAudience type, VALID_AUDIENCES array, and parseAudience() function to authenticationManager.ts - Export from SDK index for consumer use - Update e2e test, samples to use shared parseAudience function - Removes unsafe type assertions in favor of runtime validation
1 parent a858d99 commit b3758d1

5 files changed

Lines changed: 37 additions & 7 deletions

File tree

__tests__/functional/e2e.functional.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, beforeAll } from 'vitest';
2-
import { ClassifierSdk, ImageFormat } from '../../src';
2+
import { ClassifierSdk, ImageFormat, parseAudience } from '../../src';
33
import fs from 'fs';
44
import path from 'path';
55

@@ -64,7 +64,7 @@ describe('E2E Test Cases', () => {
6464
process.env.VITE_OAUTH_ISSUER ?? 'https://crispthinking.auth0.com/',
6565
clientId: process.env.VITE_ATHENA_CLIENT_ID ?? '',
6666
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET ?? '',
67-
audience: (process.env.VITE_ATHENA_AUDIENCE as 'crisp-athena-live' | 'crisp-athena-dev' | 'crisp-athena-qa') ?? 'crisp-athena-live',
67+
audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE),
6868
scope: 'manage:classify',
6969
},
7070
});

samples/e2e-testcases/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* against expected outputs.
99
*/
1010

11-
import { ClassifierSdk, ImageFormat } from '@crispthinking/athena-classifier-sdk';
11+
import { ClassifierSdk, ImageFormat, parseAudience } from '@crispthinking/athena-classifier-sdk';
1212
import fs from 'fs';
1313
import path from 'path';
1414
import { fileURLToPath } from 'url';
@@ -26,7 +26,7 @@ const CONFIG = {
2626
affiliate: process.env.ATHENA_AFFILIATE,
2727
issuerUrl: process.env.ATHENA_ISSUER_URL || 'https://crispthinking.auth0.com/',
2828
grpcAddress: process.env.ATHENA_GRPC_ADDRESS || 'trust-messages-global.crispthinking.com:443',
29-
audience: process.env.ATHENA_AUDIENCE || 'crisp-athena-live'
29+
audience: parseAudience(process.env.ATHENA_AUDIENCE)
3030
};
3131

3232
/**

samples/hash-server/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ClassifierSdk, HashType, ImageFormat, RequestEncoding, type ClassifyImageInput } from '@crispthinking/athena-classifier-sdk';
1+
import { ClassifierSdk, HashType, ImageFormat, RequestEncoding, parseAudience, type ClassifyImageInput } from '@crispthinking/athena-classifier-sdk';
22
import express from 'express';
33
import type { Request, Response, NextFunction } from 'express';
44
import multer from 'multer';
@@ -147,7 +147,7 @@ const sdk = new ClassifierSdk({
147147
clientId: CONFIG.clientId!,
148148
clientSecret: CONFIG.clientSecret!,
149149
issuerUrl: CONFIG.issuerUrl!,
150-
audience: CONFIG.audience! as 'crisp-athena-live' | 'crisp-athena-dev' | 'crisp-athena-qa'
150+
audience: parseAudience(CONFIG.audience)
151151
},
152152
grpcAddress: CONFIG.grpcAddress!,
153153
affiliate: CONFIG.affiliate!,

src/authenticationManager.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ import {
88
import * as grpc from '@grpc/grpc-js';
99
import { jwtDecode, type JwtPayload } from 'jwt-decode';
1010

11+
/**
12+
* Valid audience values for the Athena SDK.
13+
*/
14+
export type AthenaAudience = 'crisp-athena-live' | 'crisp-athena-dev' | 'crisp-athena-qa';
15+
16+
/**
17+
* List of valid audience values.
18+
*/
19+
export const VALID_AUDIENCES: readonly AthenaAudience[] = ['crisp-athena-live', 'crisp-athena-dev', 'crisp-athena-qa'] as const;
20+
21+
/**
22+
* Type guard to validate and parse an audience string.
23+
* Returns the validated audience value or a default if invalid/undefined.
24+
* @param value The string value to validate
25+
* @param defaultValue The default audience if value is invalid (default: 'crisp-athena-live')
26+
* @returns A valid AthenaAudience value
27+
*/
28+
export function parseAudience(value: string | undefined, defaultValue: AthenaAudience = 'crisp-athena-live'): AthenaAudience {
29+
if (value && VALID_AUDIENCES.includes(value as AthenaAudience)) {
30+
return value as AthenaAudience;
31+
}
32+
return defaultValue;
33+
}
34+
1135
/**
1236
* Options for configuring the AuthenticationManager.
1337
*/
@@ -23,7 +47,7 @@ export type AuthenticationOptions = {
2347
/** OAuth scope to request. */
2448
scope?: string;
2549
/** OAuth audience to request. */
26-
audience?: 'crisp-athena-live' | 'crisp-athena-dev' | 'crisp-athena-qa';
50+
audience?: AthenaAudience;
2751
};
2852

2953
/**

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,9 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<
391391
export * from './generated/athena/models.js';
392392
export { ClassifierServiceClient } from './generated/athena/athena.js';
393393
export * from './hashing.js';
394+
export {
395+
type AuthenticationOptions,
396+
type AthenaAudience,
397+
VALID_AUDIENCES,
398+
parseAudience,
399+
} from './authenticationManager.js';

0 commit comments

Comments
 (0)