From 95d2b0150f6cca602fbc26dced77b0e425ace516 Mon Sep 17 00:00:00 2001 From: Robbie Van Gorkom Date: Fri, 21 Jul 2017 07:04:46 -0700 Subject: [PATCH 1/3] Adding typescript defs. --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 4e2b093..9c446eb 100644 --- a/package.json +++ b/package.json @@ -46,5 +46,6 @@ "karma-mocha": "^0.1.10", "mocha": "^2.2.5", "uglify-js": "^2.4.23" - } + }, + "types": "./index.d.ts"" } From cbf8d2f3ffe1de551a89d551c8f9fc8b44714203 Mon Sep 17 00:00:00 2001 From: Robbie Van Gorkom Date: Fri, 21 Jul 2017 07:11:37 -0700 Subject: [PATCH 2/3] Forgot a file. --- index.d.ts | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e05bba3 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,83 @@ +declare module 'vers' { + type VersionType = number | string; + + export interface VersOptions { + /** + * A function that accepts an object as its only argument, and returns either the current version identifier of the object as a number or string, or a Promise that resolves to the current version identifier. By default, Vers will use the object's version property if it exists, or 1 if it doesn't. + * @param obj + */ + getVersion?: (obj) => Promise | VersionType; + + /** + * The latest version identifier available for this model. If not specified, Vers will detect the latest version by calling Math.max on each version specified in addConverter(). For string-based versions, this option should be specified. + */ + latest?: VersionType; + } + + declare interface ConvertFunc { + (obj: T): Promise | T | undefined; + } + + export class Vers { + /** + * The current maximum version available. + */ + public readonly _maxVersion: VersionType; + + constructor(options?: VersOptions); + + /** + * Adds a converter to this instance that knows how to change an object from one version to another, and optionally, how to go back again. If you're using Vers to power a versioned REST API, then telling it how to go back again is essential. If your versioning scheme uses numbers, Vers will use Math.max to determine what your latest version is so you don't have to specify that in the constructor. + * @param fromVer The version to convert from + * @param toVer The version to convert to + * @param forward A function that accepts an object to be converted, and moves it from fromVer to toVer. + * @param back An optional function that accepts an object and moves it from toVer to fromVer. + */ + addConverter( + fromVer: VersionType, + toVer: VersionType, + forward: ConvertFunc, + back?: ConvertFunc, + ): void; + + /** + * Converts an object from one version to another, using the provided fromVer as the current version instead of trying to detect it. The result is passed on in the form of a Promise that resolves with the object in its target version. + * @param fromVer The starting version + * @param toVer The target version + * @param obj The object to be converted + */ + fromTo( + fromVer: VersionType, + toVer: VersionType, + obj: T, + ): Promise; + + /** + * Converts an object from its current version to the latest version available, using the provided fromVer as the current version instead of trying to detect it. The result is passed on in the form of a Promise that resolves with the object in its target version. + * @param fromVer The starting version + * @param obj The object to be converted + */ + fromToLatest( + fromVer: VersionType, + obj: T, + ): Promise; + + /** + * Converts an object from its auto-detected current version to the toVer version. The result is passed on in the form of a Promise that resolves with the object in its target version. + * @param toVer The target version + * @param obj The object to be converted + */ + to( + toVer: VersionType, + obj: T, + ): Promise; + + /** + * Converts an object from its auto-detected current version to the latest version available. The result is passed on in the form of a Promise that resolves with the object in its target version. + * @param obj + */ + toLatest(obj: T): Promise; + } + + export = Vers; +} From 11c0419cc4fe878547ce4881d75e1dd887b79bc9 Mon Sep 17 00:00:00 2001 From: Robbie Van Gorkom Date: Fri, 21 Jul 2017 07:16:04 -0700 Subject: [PATCH 3/3] Fixing quote in package file. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9c446eb..80fcf59 100644 --- a/package.json +++ b/package.json @@ -47,5 +47,5 @@ "mocha": "^2.2.5", "uglify-js": "^2.4.23" }, - "types": "./index.d.ts"" + "types": "./index.d.ts" }