Skip to content
7 changes: 7 additions & 0 deletions runtime/analytics/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface AnalyticsService {
sendTrackingLogEvent(eventName: string, properties: Record<string, unknown>): Promise<unknown>;
identifyAuthenticatedUser(userId: string | number, traits?: Record<string, unknown>): void;
identifyAnonymousUser(traits?: Record<string, unknown>): void;
sendTrackEvent(eventName: string, properties?: Record<string, unknown>): void;
sendPageEvent(category?: string, name?: string, properties?: Record<string, unknown>): void;
}
12 changes: 3 additions & 9 deletions runtime/auth/AxiosJwtAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ const optionsPropTypes = {
class AxiosJwtAuthService {
/**
* @param {Object} options
* @param {Object} options.config
* @param {string} options.config.baseUrl
* @param {string} options.config.lmsBaseUrl
* @param {string} options.config.loginUrl
* @param {string} options.config.logoutUrl
* @param {string} options.config.refreshAccessTokenApiPath
* @param {string} options.config.accessTokenCookieName
* @param {string} options.config.csrfTokenApiPath
* @param {import('../../types').SiteConfig} options.config
* @param {Object} options.loggingService requires logError and logInfo methods
* @param {Array} [options.middleware] Optional array of middleware functions to apply to the HTTP clients.
*/
constructor(options) {
this.authenticatedHttpClient = null;
Expand Down Expand Up @@ -288,7 +282,7 @@ class AxiosJwtAuthService {
* console.log(authenticatedUser); // Will contain additional user information
* ```
*
* @returns {Promise<null>}
* @returns {Promise<void>}
*/
async hydrateAuthenticatedUser() {
const user = this.getAuthenticatedUser();
Expand Down
13 changes: 3 additions & 10 deletions runtime/auth/MockAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,7 @@ const optionsPropTypes = {
class MockAuthService {
/**
* @param {Object} options
* @param {Object} options.config
* @param {string} options.config.baseUrl
* @param {string} options.config.lmsBaseUrl
* @param {string} options.config.loginUrl
* @param {string} options.config.logoutUrl
* @param {string} options.config.refreshAccessTokenApiPath
* @param {string} options.config.accessTokenCookieName
* @param {string} options.config.csrfTokenApiPath
* @param {import('../../types').SiteConfig} options.config
* @param {Object} options.loggingService requires logError and logInfo methods
*/
constructor(options) {
Expand Down Expand Up @@ -265,9 +258,9 @@ class MockAuthService {
* console.log(authenticatedUser); // Will contain additional user information
* ```
*
* @returns {Promise<null>}
* @returns {Promise<void>}
*/
hydrateAuthenticatedUser = jest.fn(() => {
hydrateAuthenticatedUser = jest.fn(async () => {
const user = this.getAuthenticatedUser();
if (user !== null) {
this.setAuthenticatedUser({ ...user, ...this.hydratedAuthenticatedUser });
Expand Down
15 changes: 15 additions & 0 deletions runtime/auth/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { User } from '../../types';

export interface AuthService {
getAuthenticatedHttpClient(options?: Record<string, unknown>): unknown;
getHttpClient(options?: Record<string, unknown>): unknown;
getLoginRedirectUrl(redirectUrl?: string): string;
redirectToLogin(redirectUrl?: string): void;
getLogoutRedirectUrl(redirectUrl?: string): string;
redirectToLogout(redirectUrl?: string): void;
getAuthenticatedUser(): User | null;
setAuthenticatedUser(authUser: User): void;
fetchAuthenticatedUser(options?: Record<string, unknown>): Promise<User | null>;
ensureAuthenticatedUser(redirectUrl?: string): Promise<User>;
hydrateAuthenticatedUser(): Promise<void>;
}
6 changes: 6 additions & 0 deletions runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export {
sendTrackingLogEvent
} from './analytics';

export type { AnalyticsService } from './analytics/types';

export {
AUTHENTICATED_USER_CHANGED,
AUTHENTICATED_USER_TOPIC,
Expand All @@ -31,6 +33,8 @@ export {
setAuthenticatedUser
} from './auth';

export type { AuthService } from './auth/types';

export {
getSiteConfig,
setSiteConfig,
Expand Down Expand Up @@ -102,6 +106,8 @@ export {
resetLoggingService
} from './logging';

export type { LoggingService } from './logging/types';

export {
CurrentAppContext,
CurrentAppProvider,
Expand Down
26 changes: 25 additions & 1 deletion types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { FC, ReactElement, ReactNode } from 'react';
import { MessageDescriptor } from 'react-intl';
import { RouteObject } from 'react-router';
import { SlotOperation } from './runtime/slots/types';
import { LoggingService } from './runtime/logging/types';
import { AnalyticsService } from './runtime/analytics/types';
import { AuthService } from './runtime/auth/types';

// Apps

Expand Down Expand Up @@ -60,6 +63,22 @@ export interface RequiredSiteConfig {
export type LocalizedMessages = Record<string, Record<string, string>>;
export type SiteMessages = LocalizedMessages[];

export type LoggingServiceClass = new (options: {
config: SiteConfig;
}) => LoggingService;

export type AnalyticsServiceClass = new (options: {
config: SiteConfig;
loggingService: LoggingService;
httpClient: unknown;
}) => AnalyticsService;

export type AuthServiceClass = new (options: {
config: SiteConfig;
loggingService: LoggingService;
middleware: unknown[];
}) => AuthService;
Comment thread
arbrandes marked this conversation as resolved.
Comment thread
arbrandes marked this conversation as resolved.

export interface OptionalSiteConfig {
// Site environment
environment: EnvironmentTypes;
Expand Down Expand Up @@ -97,6 +116,11 @@ export interface OptionalSiteConfig {

// Analytics
segmentKey: string | null;

// Services
loggingService: LoggingServiceClass;
analyticsService: AnalyticsServiceClass;
authService: AuthServiceClass;
}

export type SiteConfig = RequiredSiteConfig & Partial<OptionalSiteConfig>;
Expand Down Expand Up @@ -125,7 +149,7 @@ export interface User {
roles: string[];
userId: number;
username: string;
avatar: string;
avatar?: string;
}

export enum EnvironmentTypes {
Expand Down