Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/tools/auth0/handlers/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/tools/auth0/handlers/logStreams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DefaultAPIHandler from './default';
import DefaultAPIHandler, { order } from './default';
import { Asset, Assets } from '../../../types';

export const schema = {
Expand Down Expand Up @@ -93,6 +93,7 @@ export default class LogStreamsHandler extends DefaultAPIHandler {
return logStreams;
}

@order('70')
async processChanges(assets: Assets): Promise<void> {
const { logStreams } = assets;

Expand Down
23 changes: 19 additions & 4 deletions src/tools/auth0/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@ 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 aOrderRaw = a[stage]?.order;
// @ts-ignore because stage methods may have order property
const bOrderRaw = b[stage]?.order;

// Coerce to numbers, default to 50 if undefined/null
const aOrder = aOrderRaw != null ? Number(aOrderRaw) : defaultOrder;
const bOrder = bOrderRaw != null ? Number(bOrderRaw) : defaultOrder;

return aOrder - bOrder;
});
return sorted;
Expand Down