core.d.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { PatchError, _deepClone } from './helpers.js';
  2. export declare const JsonPatchError: typeof PatchError;
  3. export declare const deepClone: typeof _deepClone;
  4. export declare type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> | MoveOperation | CopyOperation | TestOperation<any> | GetOperation<any>;
  5. export interface Validator<T> {
  6. (operation: Operation, index: number, document: T, existingPathFragment: string): void;
  7. }
  8. export interface OperationResult<T> {
  9. removed?: any;
  10. test?: boolean;
  11. newDocument: T;
  12. }
  13. export interface BaseOperation {
  14. path: string;
  15. }
  16. export interface AddOperation<T> extends BaseOperation {
  17. op: 'add';
  18. value: T;
  19. }
  20. export interface RemoveOperation extends BaseOperation {
  21. op: 'remove';
  22. }
  23. export interface ReplaceOperation<T> extends BaseOperation {
  24. op: 'replace';
  25. value: T;
  26. }
  27. export interface MoveOperation extends BaseOperation {
  28. op: 'move';
  29. from: string;
  30. }
  31. export interface CopyOperation extends BaseOperation {
  32. op: 'copy';
  33. from: string;
  34. }
  35. export interface TestOperation<T> extends BaseOperation {
  36. op: 'test';
  37. value: T;
  38. }
  39. export interface GetOperation<T> extends BaseOperation {
  40. op: '_get';
  41. value: T;
  42. }
  43. export interface PatchResult<T> extends Array<OperationResult<T>> {
  44. newDocument: T;
  45. }
  46. /**
  47. * Retrieves a value from a JSON document by a JSON pointer.
  48. * Returns the value.
  49. *
  50. * @param document The document to get the value from
  51. * @param pointer an escaped JSON pointer
  52. * @return The retrieved value
  53. */
  54. export declare function getValueByPointer(document: any, pointer: string): any;
  55. /**
  56. * Apply a single JSON Patch Operation on a JSON document.
  57. * Returns the {newDocument, result} of the operation.
  58. * It modifies the `document` and `operation` objects - it gets the values by reference.
  59. * If you would like to avoid touching your values, clone them:
  60. * `jsonpatch.applyOperation(document, jsonpatch._deepClone(operation))`.
  61. *
  62. * @param document The document to patch
  63. * @param operation The operation to apply
  64. * @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
  65. * @param mutateDocument Whether to mutate the original document or clone it before applying
  66. * @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
  67. * @return `{newDocument, result}` after the operation
  68. */
  69. export declare function applyOperation<T>(document: T, operation: Operation, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean, index?: number): OperationResult<T>;
  70. /**
  71. * Apply a full JSON Patch array on a JSON document.
  72. * Returns the {newDocument, result} of the patch.
  73. * It modifies the `document` object and `patch` - it gets the values by reference.
  74. * If you would like to avoid touching your values, clone them:
  75. * `jsonpatch.applyPatch(document, jsonpatch._deepClone(patch))`.
  76. *
  77. * @param document The document to patch
  78. * @param patch The patch to apply
  79. * @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
  80. * @param mutateDocument Whether to mutate the original document or clone it before applying
  81. * @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
  82. * @return An array of `{newDocument, result}` after the patch
  83. */
  84. export declare function applyPatch<T>(document: T, patch: ReadonlyArray<Operation>, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
  85. /**
  86. * Apply a single JSON Patch Operation on a JSON document.
  87. * Returns the updated document.
  88. * Suitable as a reducer.
  89. *
  90. * @param document The document to patch
  91. * @param operation The operation to apply
  92. * @return The updated document
  93. */
  94. export declare function applyReducer<T>(document: T, operation: Operation, index: number): T;
  95. /**
  96. * Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
  97. * @param {object} operation - operation object (patch)
  98. * @param {number} index - index of operation in the sequence
  99. * @param {object} [document] - object where the operation is supposed to be applied
  100. * @param {string} [existingPathFragment] - comes along with `document`
  101. */
  102. export declare function validator(operation: Operation, index: number, document?: any, existingPathFragment?: string): void;
  103. /**
  104. * Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document.
  105. * If error is encountered, returns a JsonPatchError object
  106. * @param sequence
  107. * @param document
  108. * @returns {JsonPatchError|undefined}
  109. */
  110. export declare function validate<T>(sequence: ReadonlyArray<Operation>, document?: T, externalValidator?: Validator<T>): PatchError;
  111. export declare function _areEquals(a: any, b: any): boolean;