fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 214 lines 7.0 kB view raw
1import type { Node, Symbol, SymbolIn } from '@hey-api/codegen-core'; 2 3import type { SchemaProcessorContext } from '../ir/schema-processor'; 4import type { IROperationObject } from '../ir/types'; 5import type { PluginInstance } from '../plugins/shared/utils/instance'; 6 7export type Hooks = { 8 /** 9 * Event hooks. 10 */ 11 events?: { 12 /** 13 * Triggered after adding or updating a node. 14 * 15 * You can use this to perform actions after a node is added or updated. 16 * 17 * @param args Arguments object. 18 * @returns void 19 */ 20 'node:set:after'?: (args: { 21 /** The node added or updated. */ 22 node: Node | null; 23 /** Plugin that added or updated the node. */ 24 plugin: PluginInstance; 25 }) => void; 26 /** 27 * Triggered before adding or updating a node. 28 * 29 * You can use this to modify the node before it's added or updated. 30 * 31 * @param args Arguments object. 32 * @returns void 33 */ 34 'node:set:before'?: (args: { 35 /** The node to be added or updated. */ 36 node: Node | null; 37 /** Plugin adding or updating the node. */ 38 plugin: PluginInstance; 39 }) => void; 40 /** 41 * Triggered after executing a plugin handler. 42 * 43 * @param args Arguments object. 44 * @returns void 45 */ 46 'plugin:handler:after'?: (args: { 47 /** Plugin that just executed. */ 48 plugin: PluginInstance; 49 }) => void; 50 /** 51 * Triggered before executing a plugin handler. 52 * 53 * @param args Arguments object. 54 * @returns void 55 */ 56 'plugin:handler:before'?: (args: { 57 /** Plugin about to execute. */ 58 plugin: PluginInstance; 59 }) => void; 60 /** 61 * Triggered after registering a symbol. 62 * 63 * You can use this to perform actions after a symbol is registered. 64 * 65 * @param args Arguments object. 66 * @returns void 67 */ 68 'symbol:register:after'?: (args: { 69 /** Plugin that registered the symbol. */ 70 plugin: PluginInstance; 71 /** The registered symbol. */ 72 symbol: Symbol; 73 }) => void; 74 /** 75 * Triggered before registering a symbol. 76 * 77 * You can use this to modify the symbol before it's registered. 78 * 79 * @param args Arguments object. 80 * @returns void 81 */ 82 'symbol:register:before'?: (args: { 83 /** Plugin registering the symbol. */ 84 plugin: PluginInstance; 85 /** Symbol to register. */ 86 symbol: SymbolIn; 87 }) => void; 88 }; 89 /** 90 * Hooks specifically for overriding operations behavior. 91 * 92 * Use these to classify operations, decide which outputs to generate, 93 * or apply custom behavior to individual operations. 94 */ 95 operations?: { 96 /** 97 * Classify the given operation into one or more kinds. 98 * 99 * Each kind determines how we treat the operation (e.g., generating queries or mutations). 100 * 101 * **Default behavior:** 102 * - GET → 'query' 103 * - DELETE, PATCH, POST, PUT → 'mutation' 104 * 105 * **Resolution order:** 106 * 1. If `isQuery` or `isMutation` returns `true` or `false`, that overrides `getKind`. 107 * 2. If `isQuery` or `isMutation` returns `undefined`, the result of `getKind` is used. 108 * 109 * @param operation - The operation object to classify. 110 * @returns An array containing one or more of 'query' or 'mutation', or undefined to fallback to default behavior. 111 * @example 112 * ```ts 113 * getKind: (operation) => { 114 * if (operation.method === 'get' && operation.path === '/search') { 115 * return ['query', 'mutation']; 116 * } 117 * return; // fallback to default behavior 118 * } 119 * ``` 120 */ 121 getKind?: (operation: IROperationObject) => ReadonlyArray<'mutation' | 'query'> | undefined; 122 /** 123 * Check if the given operation should be treated as a mutation. 124 * 125 * This affects which outputs are generated for the operation. 126 * 127 * **Default behavior:** DELETE, PATCH, POST, and PUT operations are treated as mutations. 128 * 129 * **Resolution order:** If this returns `true` or `false`, it overrides `getKind`. 130 * If it returns `undefined`, `getKind` is used instead. 131 * 132 * @param operation - The operation object to check. 133 * @returns true if the operation is a mutation, false otherwise, or undefined to fallback to `getKind`. 134 * @example 135 * ```ts 136 * isMutation: (operation) => { 137 * if (operation.method === 'post' && operation.path === '/search') { 138 * return true; 139 * } 140 * return; // fallback to default behavior 141 * } 142 * ``` 143 */ 144 isMutation?: (operation: IROperationObject) => boolean | undefined; 145 /** 146 * Check if the given operation should be treated as a query. 147 * 148 * This affects which outputs are generated for the operation. 149 * 150 * **Default behavior:** GET operations are treated as queries. 151 * 152 * **Resolution order:** If this returns `true` or `false`, it overrides `getKind`. 153 * If it returns `undefined`, `getKind` is used instead. 154 * 155 * @param operation - The operation object to check. 156 * @returns true if the operation is a query, false otherwise, or undefined to fallback to `getKind`. 157 * @example 158 * ```ts 159 * isQuery: (operation) => { 160 * if (operation.method === 'post' && operation.path === '/search') { 161 * return true; 162 * } 163 * return; // fallback to default behavior 164 * } 165 * ``` 166 */ 167 isQuery?: (operation: IROperationObject) => boolean | undefined; 168 }; 169 schemas?: { 170 /** 171 * Whether to extract the given schema into a separate symbol. 172 * 173 * This affects how schemas are processed and output. 174 * 175 * **Default behavior:** No schemas are extracted. 176 * 177 * @param ctx - The processing context for the schema. 178 * @returns true to extract the schema, false to keep it inline, or undefined to fallback to default behavior. 179 * @example 180 * ```ts 181 * shouldExtract: (ctx) => { 182 * if (ctx.meta.resource === 'requestBody') { 183 * return true; 184 * } 185 * return; // fallback to default behavior 186 * } 187 * ``` 188 */ 189 shouldExtract?: (ctx: SchemaProcessorContext) => boolean; 190 }; 191 /** 192 * Hooks specifically for overriding symbols behavior. 193 * 194 * Use these to customize file placement. 195 */ 196 symbols?: { 197 /** 198 * Optional output strategy to override default plugin behavior. 199 * 200 * Use this to re-export symbols from specific files. 201 * 202 * @returns The file path(s) that re-export this symbol, or undefined to fallback to default behavior. 203 */ 204 getExportFromFilePath?: (symbol: Symbol) => ReadonlyArray<string> | undefined; 205 /** 206 * Optional output strategy to override default plugin behavior. 207 * 208 * Use this to route symbols to specific files. 209 * 210 * @returns The file path to output the symbol to, or undefined to fallback to default behavior. 211 */ 212 getFilePath?: (symbol: Symbol) => string | undefined; 213 }; 214};