diff --git a/src/tools/auth0/handlers/default.ts b/src/tools/auth0/handlers/default.ts index a2e071cfd..8c13ab676 100644 --- a/src/tools/auth0/handlers/default.ts +++ b/src/tools/auth0/handlers/default.ts @@ -16,7 +16,14 @@ import { ConfigFunction } from '../../../configFactory'; export function order(value) { return function decorator(t, n, descriptor) { - descriptor.value.order = value; // eslint-disable-line + const numericValue = Number(value); + if (Number.isNaN(numericValue)) { + throw new Error( + `Invalid @order value '${value}' for method '${n}'. The @order decorator only accepts numeric values. Received: ${typeof value}` + ); + } + + descriptor.value.order = numericValue; return descriptor; }; } diff --git a/src/tools/auth0/handlers/logStreams.ts b/src/tools/auth0/handlers/logStreams.ts index e9f6c9fc6..8f5de4cdc 100644 --- a/src/tools/auth0/handlers/logStreams.ts +++ b/src/tools/auth0/handlers/logStreams.ts @@ -1,4 +1,4 @@ -import DefaultAPIHandler from './default'; +import DefaultAPIHandler, { order } from './default'; import { Asset, Assets } from '../../../types'; export const schema = { @@ -93,6 +93,7 @@ export default class LogStreamsHandler extends DefaultAPIHandler { return logStreams; } + @order('70') async processChanges(assets: Assets): Promise { const { logStreams } = assets; diff --git a/src/tools/auth0/index.ts b/src/tools/auth0/index.ts index ffde3487a..0078cb85b 100644 --- a/src/tools/auth0/index.ts +++ b/src/tools/auth0/index.ts @@ -12,15 +12,26 @@ export type Stage = 'load' | 'validate' | 'processChanges'; type StageFunction = APIHandler['load']; // Using `load` method as a template for what type stage functions resemble +/** + * Sorts handlers by their @order decorator metadata for a given stage. + * Handlers are sorted in ascending order (lower values execute first). + * Default order is 50 for handlers without explicit @order metadata. + * Uses stable sort: preserves insertion order when order values are equal. + * + * @param toSort - Array of API handlers to sort + * @param stage - The stage name (load, validate, processChanges) + * @returns Sorted array of handlers + */ function sortByOrder(toSort: APIHandler[], stage: Stage): APIHandler[] { const defaultOrder = 50; const sorted = [...toSort]; sorted.sort((a, b) => { - //@ts-ignore because this doesn't actually work. TODO: apply stage order - const aOrder = a[stage].order || defaultOrder; - //@ts-ignore because this doesn't actually work. TODO: apply stage order - const bOrder = b[stage].order || defaultOrder; + // @ts-ignore because stage methods may have order property + const aOrder = a[stage]?.order || defaultOrder; + // @ts-ignore because stage methods may have order property + const bOrder = b[stage]?.order || defaultOrder; + return aOrder - bOrder; }); return sorted;