|
| 1 | +import { JSONPathOptions } from "jsonpath-plus" |
| 2 | + |
| 3 | +declare module 'jsonpath-plus' { |
| 4 | + type JSONPathCallback = (payload: any, payloadType: any, fullPayload: any) => any |
| 5 | + |
| 6 | + type JSONPathOtherTypeCallback = (...args: Array<any>) => void |
| 7 | + |
| 8 | + interface JSONPathOptions { |
| 9 | + /** |
| 10 | + * The JSONPath expression as a (normalized or unnormalized) string or array. |
| 11 | + */ |
| 12 | + path: string | Array<any> |
| 13 | + /** |
| 14 | + * The JSON object to evaluate (whether of null, boolean, number, string, object, or array type). |
| 15 | + */ |
| 16 | + json: null | boolean | number | string | object | Array<any> |
| 17 | + /** |
| 18 | + * If this is supplied as false, one may call the evaluate method manually. |
| 19 | + * |
| 20 | + * @default true |
| 21 | + */ |
| 22 | + autostart?: true | boolean |
| 23 | + /** |
| 24 | + * Whether the returned array of results will be flattened to a single dimension array. |
| 25 | + * |
| 26 | + * @default false |
| 27 | + */ |
| 28 | + flatten?: false | boolean |
| 29 | + /** |
| 30 | + * Can be case-insensitive form of "value", "path", "pointer", "parent", or "parentProperty" to determine respectively whether to return results as the values of the found items, |
| 31 | + * as their absolute paths, as JSON Pointers to the absolute paths, as their parent objects, or as their parent's property name. |
| 32 | + * |
| 33 | + * If set to "all", all of these types will be returned on an object with the type as key name. |
| 34 | + * |
| 35 | + * @default 'value' |
| 36 | + */ |
| 37 | + resultType?: 'value' | 'path' | 'pointer' | 'parent' | 'parentProperty' | 'all' |
| 38 | + |
| 39 | + /** |
| 40 | + * Key-value map of variables to be available to code evaluations such as filtering expressions. |
| 41 | + * (Note that the current path and value will also be available to those expressions; see the Syntax section for details.) |
| 42 | + */ |
| 43 | + sandbox?: Map<string, any> |
| 44 | + /** |
| 45 | + * Whether or not to wrap the results in an array. |
| 46 | + * |
| 47 | + * If wrap is set to false, and no results are found, undefined will be returned (as opposed to an empty array when wrap is set to true). |
| 48 | + * |
| 49 | + * If wrap is set to false and a single non-array result is found, that result will be the only item returned (not within an array). |
| 50 | + * |
| 51 | + * An array will still be returned if multiple results are found, however. |
| 52 | + * To avoid ambiguities (in the case where it is necessary to distinguish between a result which is a failure and one which is an empty array), |
| 53 | + * it is recommended to switch the default to false. |
| 54 | + * |
| 55 | + * @default true |
| 56 | + */ |
| 57 | + wrap?: true | boolean |
| 58 | + /** |
| 59 | + * Although JavaScript evaluation expressions are allowed by default, |
| 60 | + * for security reasons (if one is operating on untrusted user input, for example), |
| 61 | + * one may wish to set this option to true to throw exceptions when these expressions are attempted. |
| 62 | + * |
| 63 | + * @default false |
| 64 | + */ |
| 65 | + preventEval?: false | boolean |
| 66 | + /** |
| 67 | + * In the event that a query could be made to return the root node, |
| 68 | + * this allows the parent of that root node to be returned within results. |
| 69 | + * |
| 70 | + * @default null |
| 71 | + */ |
| 72 | + parent?: null | any |
| 73 | + /** |
| 74 | + * In the event that a query could be made to return the root node, |
| 75 | + * this allows the parentProperty of that root node to be returned within results. |
| 76 | + * |
| 77 | + * @default null |
| 78 | + */ |
| 79 | + parentProperty?: null | any |
| 80 | + /** |
| 81 | + * If supplied, a callback will be called immediately upon retrieval of an end point value. |
| 82 | + * |
| 83 | + * The three arguments supplied will be the value of the payload (according to resultType), |
| 84 | + * the type of the payload (whether it is a normal "value" or a "property" name), |
| 85 | + * and a full payload object (with all resultTypes). |
| 86 | + * |
| 87 | + * @default undefined |
| 88 | + */ |
| 89 | + callback?: undefined | JSONPathCallback |
| 90 | + /** |
| 91 | + * In the current absence of JSON Schema support, |
| 92 | + * one can determine types beyond the built-in types by adding the operator @other() at the end of one's query. |
| 93 | + * |
| 94 | + * If such a path is encountered, the otherTypeCallback will be invoked with the value of the item, |
| 95 | + * its path, its parent, and its parent's property name, and |
| 96 | + * it should return a boolean indicating whether the supplied value belongs to the "other" type or not |
| 97 | + * (or it may handle transformations and return false). |
| 98 | + * |
| 99 | + * @default undefined <A function that throws an error when @other() is encountered> |
| 100 | + */ |
| 101 | + otherTypeCallback?: undefined | JSONPathOtherTypeCallback |
| 102 | + } |
| 103 | + |
| 104 | + interface JSONPathCallable { |
| 105 | + (options: JSONPathOptions): JSONPathClass |
| 106 | + |
| 107 | + (path: JSONPathOptions['path'], json: JSONPathOptions['json'], callback: JSONPathOptions['callback'], otherTypeCallback: JSONPathOptions['otherTypeCallback']): JSONPathClass |
| 108 | + } |
| 109 | + |
| 110 | + class JSONPathClass { |
| 111 | + /** |
| 112 | + * Exposes the cache object for those who wish to preserve and reuse it for optimization purposes. |
| 113 | + */ |
| 114 | + static cache: any |
| 115 | + |
| 116 | + /** |
| 117 | + * Accepts a normalized or unnormalized path as string and |
| 118 | + * converts to an array: for example, ['$', 'aProperty', 'anotherProperty']. |
| 119 | + */ |
| 120 | + static toPathArray(path: string): Array<string> |
| 121 | + |
| 122 | + /** |
| 123 | + * Accepts a path array and converts to a normalized path string. |
| 124 | + * The string will be in a form like: $['aProperty']['anotherProperty][0]. |
| 125 | + * The JSONPath terminal constructions ~ and ^ and type operators like @string() are silently stripped. |
| 126 | + */ |
| 127 | + static toPathString(path: Array<string>): string |
| 128 | + |
| 129 | + /** |
| 130 | + * Accepts a path array and converts to a JSON Pointer. |
| 131 | + * |
| 132 | + * The string will be in a form like: '/aProperty/anotherProperty/0 |
| 133 | + * (with any ~ and / internal characters escaped as per the JSON Pointer spec). |
| 134 | + * |
| 135 | + * The JSONPath terminal constructions ~ and ^ and type operators like @string() are silently stripped. |
| 136 | + */ |
| 137 | + static toPointer(path: Array<string>): any |
| 138 | + |
| 139 | + constructor(options: JSONPathOptions) |
| 140 | + constructor(path: JSONPathOptions['path'], json: JSONPathOptions['json'], callback: JSONPathOptions['callback'], otherTypeCallback: JSONPathOptions['otherTypeCallback']) |
| 141 | + |
| 142 | + evaluate(path: JSONPathOptions['path'], json: JSONPathOptions['json'], callback: JSONPathOptions['callback'], otherTypeCallback: JSONPathOptions['otherTypeCallback']) |
| 143 | + evaluate(options: { path: JSONPathOptions['path'], json: JSONPathOptions['json'], callback: JSONPathOptions['callback'], otherTypeCallback: JSONPathOptions['otherTypeCallback'] }) |
| 144 | + } |
| 145 | + |
| 146 | + type JSONPathType = JSONPathCallable & JSONPathClass |
| 147 | + |
| 148 | + export const JSONPath: JSONPathType |
| 149 | +} |
0 commit comments