diff --git a/types/node-ip2region/index.d.ts b/types/node-ip2region/index.d.ts new file mode 100644 index 0000000000..fbde956a11 --- /dev/null +++ b/types/node-ip2region/index.d.ts @@ -0,0 +1,80 @@ +// Type definitions for node-ip2region 1.0 +// Project: https://github.com/lionsoul2014/ip2region +// Definitions by: DCsunset +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Minimum TypeScript Version: 3.7 + +/// + +type SearchResult = { + city: number; + region: string; +} | null; + +type SearchCallback = (err: NodeJS.ErrnoException, result: SearchResult) => void; + +declare class IP2Region { + //#region Static Functions + + // Single Instance + static create(dbPath?: string): IP2Region; + + /** + * For backward compatibility + */ + static destroy(): void; + + //#endregion + + constructor(options?: { dbPath: string }); + + //#region Public Functions + /** + * Destroy the current file by closing it. + */ + destroy(): void; + + /** + * Sync of binarySearch. + * @param ip The IP address to search for. + * @return A result something like `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }` + */ + binarySearchSync(ip: string): SearchResult; + + /** + * Async of binarySearch. + * @param ip The IP address to search for. + * @param callBack The callBack function with two parameters, if successful, + * err is null and result is `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }` + */ + binarySearch(ip: string, callBack: SearchCallback): void; + + /** + * Sync of btreeSearch. + * @param ip The IP address to search for. + * @return A result something like `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }` + */ + btreeSearchSync(ip: string): SearchResult; + + /** + * Async of btreeSearch. + * @param ip The IP address to search for. + * @param callBack The callBack function with two parameters, if successful, + * err is null and result is `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }` + */ + btreeSearch(ip: string, callBack: SearchCallback): void; + + /** + * Sync of MemorySearch. + * @param ip + */ + memorySearchSync(ip: string): SearchResult; + + /** + * Async of MemorySearch. + * @param ip + */ + memorySearch(ip: string, callBack: SearchCallback): void; +} + +export = IP2Region; diff --git a/types/node-ip2region/node-ip2region-tests.ts b/types/node-ip2region/node-ip2region-tests.ts new file mode 100644 index 0000000000..185b5a6573 --- /dev/null +++ b/types/node-ip2region/node-ip2region-tests.ts @@ -0,0 +1,28 @@ +import IP2Region = require('node-ip2region'); + +const searcher = new IP2Region({ + dbPath: './db', +}); +searcher.binarySearch('1.1.1.1', (_err, result) => { + result?.city; + result?.region; +}); +let result = searcher.binarySearchSync('1.1.1.1'); +result?.city; +result?.region; +searcher.btreeSearch('1.1.1.1', (_err, result) => { + result?.city; + result?.region; +}); +result = searcher.btreeSearchSync('1.1.1.1'); +result?.city; +result?.region; +searcher.memorySearch('1.1.1.1', (_err, result) => { + result?.city; + result?.region; +}); +result = searcher.memorySearchSync('1.1.1.1'); +result?.city; +result?.region; + +searcher.destroy(); diff --git a/types/node-ip2region/tsconfig.json b/types/node-ip2region/tsconfig.json new file mode 100644 index 0000000000..ebffc69a6b --- /dev/null +++ b/types/node-ip2region/tsconfig.json @@ -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", + "node-ip2region-tests.ts" + ] +} diff --git a/types/node-ip2region/tslint.json b/types/node-ip2region/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/node-ip2region/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }