json-patch-query
Version:
Implementation of JSON Patch Query as proposed by the TM Forum
85 lines (83 loc) • 3.29 kB
text/typescript
interface BaseOperation {
path: string;
}
interface AddOperation<T> extends BaseOperation {
op: 'add';
value: T;
}
interface RemoveOperation extends BaseOperation {
op: 'remove';
}
interface ReplaceOperation<T> extends BaseOperation {
op: 'replace';
value: T;
}
interface MoveOperation extends BaseOperation {
op: 'move';
from: string;
}
interface CopyOperation extends BaseOperation {
op: 'copy';
from: string;
}
interface TestOperation<T> extends BaseOperation {
op: 'test';
value: T;
}
interface GetOperation extends BaseOperation {
op: '_get';
}
type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> | MoveOperation | CopyOperation | TestOperation<any> | GetOperation;
/**
* Takes a TMF format path and expands it into lodash format, for all matching paths.
* Path must use dot notation for separators.
* @param path A dot notation path, that ignores array indexes
* @param obj The document to run the path against
* @param options.startingPath Used for recursion, if starting at top level provide ''
* @param options.includeIndex Whether or not to include array indexes
* useful for adding non-existent properties or removing values from an array
* @returns An array of matching paths in lodash format (with index)
*/
declare function expandPaths(path: string, document: any, options: {
startingPath: string;
includeIndex?: boolean;
}): string[];
/**
* Find paths that match the map of key value pairs against a document, expanded in lodash format
* @param matchers A map of key value pairs where the key is the path
* in dot notation, and the value is the value to match against
* @param document The document to match paths against
* @returns An array of matching paths in lodash format (with index)
*/
declare function findPaths(query: {
[matcher: string]: string;
}, document: any): string[];
/**
* Resolves a path against a document and returns an array of path strings in dot notation
* @param path A TMF path that uses TMF Path Query syntax
* @param document The document to run the path against
* @returns
*/
declare function resolveTMFPath(path: string, document: any, includeIndex?: boolean): string[][];
declare class JSONPatchQuery {
/**
* Legacy Content-Type: application/json-patch-query+json
*
* Ideally this should not be used any longer,
* instead application/json-patch+query should be used instead.
*
* @param document The document to apply the patch operation to
* @param patch An array of patch operations to perform
* @returns The modified document
*/
static applyLegacy(document: any, patch: Operation[]): any;
/**
* Content-Type: application/json-patch+query
* @param document The document to apply the patch operation to
* @param patch An array of patch operations to perform
* @returns The modified document
*/
static apply(document: any, patch: Operation[]): any;
private static applyOperation;
}
export { type AddOperation, type BaseOperation, type CopyOperation, type GetOperation, type MoveOperation, type Operation, type RemoveOperation, type ReplaceOperation, type TestOperation, JSONPatchQuery as default, expandPaths, findPaths, resolveTMFPath };