fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2832 from hey-api/refactor/graph-declarations-2

refactor: allow plugins to override walk options

authored by

Lubos and committed by
GitHub
b0135542 dfe04188

+25 -19
+1 -1
packages/openapi-ts/src/graph/index.ts
··· 1 1 export type { Graph, NodeInfo } from './types/graph'; 2 2 export type { 3 3 GetPointerPriorityFn, 4 - PointerGroupMatch, 4 + MatchPointerToGroupFn, 5 5 WalkOptions, 6 6 } from './types/walk'; 7 7 export { walk } from './walk';
+9 -4
packages/openapi-ts/src/graph/types/walk.d.ts
··· 4 4 5 5 export type GetPointerPriorityFn = (pointer: string) => number; 6 6 7 - export type PointerGroupMatch<T extends string = string> = 7 + export type MatchPointerToGroupFn<T extends string = string> = ( 8 + pointer: string, 9 + kind?: T, 10 + ) => PointerGroupMatch<T>; 11 + 12 + type PointerGroupMatch<T extends string = string> = 8 13 | { kind: T; matched: true } 9 14 | { kind?: undefined; matched: false }; 10 15 ··· 21 26 * @param pointer The pointer string 22 27 * @returns The group name, or undefined if no match 23 28 */ 24 - matchPointerToGroup?: (pointer: string) => PointerGroupMatch<T>; 29 + matchPointerToGroup?: MatchPointerToGroupFn<T>; 25 30 /** 26 31 * Order of walking schemas. 27 32 * ··· 47 52 preferGroups?: ReadonlyArray<T>; 48 53 }; 49 54 50 - export type WalkFn = ( 55 + export type WalkFn = <T extends string = string>( 51 56 graph: Graph, 52 57 callback: WalkCallbackFn, 53 - options?: WalkOptions, 58 + options?: WalkOptions<T>, 54 59 ) => void;
+5 -5
packages/openapi-ts/src/ir/graph.ts
··· 1 - import type { GetPointerPriorityFn, PointerGroupMatch } from '~/graph'; 1 + import type { GetPointerPriorityFn, MatchPointerToGroupFn } from '~/graph'; 2 2 3 3 export const irTopLevelKinds = [ 4 4 'operation', ··· 18 18 * @param kind - (Optional) The component kind to check 19 19 * @returns { matched: true, kind: IrTopLevelKind } | { matched: false } - Whether it matched, and the matched kind if so 20 20 */ 21 - export const matchIrPointerToGroup = ( 22 - pointer: string, 23 - kind?: IrTopLevelKind, 24 - ): PointerGroupMatch<IrTopLevelKind> => { 21 + export const matchIrPointerToGroup: MatchPointerToGroupFn<IrTopLevelKind> = ( 22 + pointer, 23 + kind, 24 + ) => { 25 25 const patterns: Record<IrTopLevelKind, RegExp> = { 26 26 operation: 27 27 /^#\/paths\/[^/]+\/(get|put|post|delete|options|head|patch|trace)$/,
+10 -9
packages/openapi-ts/src/plugins/shared/utils/instance.ts
··· 8 8 } from '@hey-api/codegen-core'; 9 9 10 10 import { HeyApiError } from '~/error'; 11 - import type { WalkOptions } from '~/graph'; 11 + import type { MatchPointerToGroupFn, WalkOptions } from '~/graph'; 12 12 import { walk } from '~/graph'; 13 13 import type { IrTopLevelKind } from '~/ir/graph'; 14 14 import { ··· 138 138 ...args: [ 139 139 ...events: ReadonlyArray<T>, 140 140 callback: (event: WalkEvent<T>) => void, 141 - options: WalkOptions, 141 + options: WalkOptions<T>, 142 142 ] 143 143 ): void; 144 144 forEach<T extends IrTopLevelKind = IrTopLevelKind>( ··· 154 154 155 155 let callback: (event: WalkEvent<T>) => void; 156 156 let events: ReadonlyArray<T>; 157 - let options: WalkOptions = { 157 + let options: WalkOptions<T> = { 158 + getPointerPriority: getIrPointerPriority, 159 + // default functions operate on the full union of kinds; cast them 160 + // to the WalkOptions generic to keep strict typing for callers. 161 + matchPointerToGroup: 162 + matchIrPointerToGroup as unknown as MatchPointerToGroupFn<T>, 158 163 order: 'topological', 164 + preferGroups: preferGroups as unknown as ReadonlyArray<T>, 159 165 }; 160 166 if (typeof args[args.length - 1] === 'function') { 161 167 events = args.slice(0, -1); ··· 238 244 } 239 245 } 240 246 }, 241 - { 242 - getPointerPriority: getIrPointerPriority, 243 - matchPointerToGroup: matchIrPointerToGroup, 244 - order: options.order, 245 - preferGroups, 246 - }, 247 + options, 247 248 ); 248 249 } 249 250