Add types for json8-patch (#41449)

This commit is contained in:
Peter Kooijmans
2020-01-07 15:35:08 -08:00
committed by Armando Aguirre
parent 1c451788fc
commit e576ea16e4
4 changed files with 115 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
// Type definitions for json8-patch 1.0
// Project: https://github.com/sonnyp/JSON8/tree/master/packages/patch
// Definitions by: Peter Kooijmans <https://github.com/peterkooijmans>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Typescript Version: 2.9
export interface BaseOperation {
path: string;
}
export interface AddOperation extends BaseOperation {
op: "add";
value: any;
}
export interface RemoveOperation extends BaseOperation {
op: "remove";
}
/**
* The JSON patch replace operation
*/
export interface ReplaceOperation extends BaseOperation {
op: "replace";
value: any;
}
/**
* The JSON patch move operation
*/
export interface MoveOperation extends BaseOperation {
op: "move";
from: any;
}
/**
* The JSON patch copy operation.
*/
export interface CopyOperation extends BaseOperation {
op: "copy";
from: any;
}
/**
* The JSON patch test operation
*/
export interface TestOperation extends BaseOperation {
op: "test";
value: any;
}
/**
* All possible JSON patch operations
*/
export type JsonPatchOperation = AddOperation | RemoveOperation | ReplaceOperation | MoveOperation | CopyOperation | TestOperation;
/**
* A JSON patch as specified in RFC 6902
*/
export type JsonPatch = JsonPatchOperation[];
export interface ApplyResult {
doc: any;
}
export interface ApplyResultWithRevert extends ApplyResult {
revert: JsonPatch;
}
export type PatchResult = ApplyResult;
export type PatchResultWithRevert = ApplyResultWithRevert;
export function apply(doc: any, patch: JsonPatch, options?: { reversible: false }): ApplyResult;
export function apply(doc: any, patch: JsonPatch, options?: { reversible: true }): ApplyResultWithRevert;
export function patch(doc: any, patch: JsonPatch, options?: { reversible: false }): PatchResult;
export function patch(doc: any, patch: JsonPatch, options?: { reversible: true }): PatchResultWithRevert;
export function diff(doc1: any, doc2: any): JsonPatch;
export function revert(doc: any, revertPatch: JsonPatch): ApplyResult;
+10
View File
@@ -0,0 +1,10 @@
import * as jsonPatch from "json8-patch";
const doc1 = { foo: "bar" };
const doc2 = { bar: 42 };
const patch = jsonPatch.diff(doc1, doc2);
const { doc, revert } = jsonPatch.apply(doc1, patch, { reversible: true });
// $ExpectType ApplyResult
const reverted = jsonPatch.revert(doc, revert);
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"json8-patch-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }