diff --git a/maxmind/index.d.ts b/maxmind/index.d.ts new file mode 100644 index 0000000000..585191c762 --- /dev/null +++ b/maxmind/index.d.ts @@ -0,0 +1,73 @@ +// Type definitions for maxmind +// Project: https://github.com/runk/node-maxmind +// Definitions by: Joshua DeVinney +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export declare function open(database: string, opts?: IOptions): IReader; +export declare function validate(ip: string): boolean; + +export declare interface IReader { + get(ip: string): IResponse | null; + readonly metadata: IMetadata; +} + +export declare interface IOptions { + cache?: { + max?: number; + maxAge?: number; + } +} + +export declare interface IMetadata { + readonly binaryFormatMajorVersion: number; + readonly binaryFormatMinorVersion: number; + readonly buildEpoch: Date; + readonly databaseType: string; + readonly languages: string[]; + readonly description: ITranslations; + readonly ipVersion: number; + readonly nodeCount: number; + readonly recordSize: number; + readonly nodeByteSize: number; + readonly searchTreeSize: number; + readonly treeDepth: number; +} + +export declare interface IResponse { + readonly continent?: { + readonly geoname_id: number; + readonly names: ITranslations; + readonly code: string; + }; + readonly country?: { + readonly geoname_id: number; + readonly names: ITranslations; + readonly iso_code: string; + }; + readonly registered_country?: { + readonly geoname_id: number; + readonly names: ITranslations; + readonly iso_code: string; + }; + readonly location?: { + readonly accuracy_radius: number; + readonly latitude: number; + readonly longitude: number; + readonly metro_code?: number; + readonly time_zone?: string; + }; + readonly subdivisions?: { + readonly geoname_id: number; + readonly names: ITranslations; + readonly iso_code: string; + }[]; + readonly city?: { + readonly geoname_id: number; + readonly names: ITranslations; + }; + readonly postal?: { code: string }; +} + +export declare interface ITranslations { + readonly [index: string]: string; +} diff --git a/maxmind/maxmind-tests.ts b/maxmind/maxmind-tests.ts new file mode 100644 index 0000000000..79fe2e47b6 --- /dev/null +++ b/maxmind/maxmind-tests.ts @@ -0,0 +1,30 @@ +import * as maxmind from 'maxmind'; + +// Examples from https://github.com/runk/node-maxmind/blob/master/README.md + +// Example 1 +var cityLookup = maxmind.open('/path/to/GeoLite2-City.mmdb'); +var city = cityLookup.get('66.6.44.4'); + +var orgLookup = maxmind.open('/path/to/GeoOrg.mmdb'); +var organization = orgLookup.get('66.6.44.4'); + +// Example 2 +var lookup = maxmind.open('/path/to/GeoLite2.mmdb'); +var location = lookup.get('2001:4860:0:1001::3004:ef68'); + +// Example 3 +var lookup = maxmind.open('/path/to/GeoLite2.mmdb', { + cache: { + max: 1000, // max items in cache + maxAge: 1000 * 60 * 60 // life time in milliseconds + } +}) +lookup.get('1.1.1.1'); + +// Example 4 +maxmind.validate('66.6.44.4'); // returns true +maxmind.validate('66.6.44.boom!'); // returns false + +maxmind.validate('2001:4860:0:1001::3004:ef68'); // returns true +maxmind.validate('2001:4860:0:1001::3004:boom!'); // returns false diff --git a/maxmind/tsconfig.json b/maxmind/tsconfig.json new file mode 100644 index 0000000000..31b488d849 --- /dev/null +++ b/maxmind/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "maxmind-tests.ts" + ] +} \ No newline at end of file