fork of hey-api/openapi-ts because I need some additional things
at feat/use-query-options-param 112 lines 3.9 kB view raw
1import { binaryParser } from './parsers/binary'; 2import { jsonParser } from './parsers/json'; 3import { textParser } from './parsers/text'; 4import { yamlParser } from './parsers/yaml'; 5import type { JSONSchemaObject, Plugin } from './types'; 6 7export interface DereferenceOptions { 8 /** 9 * Determines whether circular `$ref` pointers are handled. 10 * 11 * If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references. 12 * 13 * If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the `$Refs.circular` property will still be set to `true`. 14 */ 15 circular?: boolean | 'ignore'; 16 /** 17 * A function, called for each path, which can return true to stop this path and all 18 * subpaths from being dereferenced further. This is useful in schemas where some 19 * subpaths contain literal $ref keys that should not be dereferenced. 20 */ 21 excludedPathMatcher?(path: string): boolean; 22 /** 23 * Callback invoked during dereferencing. 24 * 25 * @argument {string} path - The path being dereferenced (ie. the `$ref` string) 26 * @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to 27 * @argument {JSONSchemaObject} parent - The parent of the dereferenced object 28 * @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced 29 */ 30 onDereference?( 31 path: string, 32 value: JSONSchemaObject, 33 parent?: JSONSchemaObject, 34 parentPropName?: string, 35 ): void; 36} 37 38/** 39 * Options that determine how JSON schemas are parsed, resolved, and dereferenced. 40 * 41 * @param [options] - Overridden options 42 * @class 43 */ 44export interface $RefParserOptions { 45 /** 46 * The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema. 47 */ 48 dereference: DereferenceOptions; 49 /** 50 * The `parse` options determine how different types of files will be parsed. 51 * 52 * JSON Schema `$Ref` Parser comes with built-in JSON, YAML, plain-text, and binary parsers, any of which you can configure or disable. You can also add your own custom parsers if you want. 53 */ 54 parse: { 55 binary: Plugin; 56 json: Plugin; 57 text: Plugin; 58 yaml: Plugin; 59 }; 60 /** 61 * The maximum amount of time (in milliseconds) that JSON Schema $Ref Parser will spend dereferencing a single schema. 62 * It will throw a timeout error if the operation takes longer than this. 63 */ 64 timeoutMs?: number; 65} 66 67export const getJsonSchemaRefParserDefaultOptions = (): $RefParserOptions => ({ 68 /** 69 * Determines the types of JSON references that are allowed. 70 */ 71 dereference: { 72 /** 73 * Dereference circular (recursive) JSON references? 74 * If false, then a {@link ReferenceError} will be thrown if a circular reference is found. 75 * If "ignore", then circular references will not be dereferenced. 76 * 77 * @type {boolean|string} 78 */ 79 circular: true, 80 /** 81 * A function, called for each path, which can return true to stop this path and all 82 * subpaths from being dereferenced further. This is useful in schemas where some 83 * subpaths contain literal $ref keys that should not be dereferenced. 84 * 85 * @type {function} 86 */ 87 excludedPathMatcher: () => false, 88 // @ts-expect-error 89 referenceResolution: 'relative', 90 }, 91 /** 92 * Determines how different types of files will be parsed. 93 * 94 * You can add additional parsers of your own, replace an existing one with 95 * your own implementation, or disable any parser by setting it to false. 96 */ 97 parse: { 98 binary: { ...binaryParser }, 99 json: { ...jsonParser }, 100 text: { ...textParser }, 101 yaml: { ...yamlParser }, 102 }, 103}); 104 105export type Options = $RefParserOptions; 106 107type DeepPartial<T> = T extends object 108 ? { 109 [P in keyof T]?: DeepPartial<T[P]>; 110 } 111 : T; 112export type ParserOptions = DeepPartial<$RefParserOptions>;