Add types for node-ip2region (#47224)

This commit is contained in:
DCsunset 2020-09-03 03:54:28 +08:00 committed by GitHub
parent 33b4f8d690
commit b498ec1033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 132 additions and 0 deletions

80
types/node-ip2region/index.d.ts vendored Normal file
View File

@ -0,0 +1,80 @@
// Type definitions for node-ip2region 1.0
// Project: https://github.com/lionsoul2014/ip2region
// Definitions by: DCsunset <https://github.com/DefinitelyTyped>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.7
/// <reference types="node" />
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;

View File

@ -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();

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",
"node-ip2region-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }