Skip to content

Commit

Permalink
Cleanup types for package
Browse files Browse the repository at this point in the history
Export all types as module
Add Options interface for generic options
Add different options types for every function with options
Add known result type for whois search functions
  • Loading branch information
AKorezin committed Apr 14, 2021
1 parent 9f770b8 commit e2c60cd
Showing 1 changed file with 83 additions and 53 deletions.
136 changes: 83 additions & 53 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,113 @@
type CommonOptions = {
/** WHOIS server to query. Default: WHOIS server from IANA */
host?: string
/** WHOIS server request timeout in ms. Default: `1500` */
timeout?: number
/** Return the raw WHOIS result in response. Added to `__raw` */
raw?: boolean
}
declare module 'whoiser' {
interface Options {
/**
* WHOIS server to query.
*/
host?: string

interface Whoiser {
/**
* Get WHOIS data for any internet address
*
* @param {string|number} query
* @param {Object} options
* @returns {Promise<*>} Parsed WHOIS server response
*/
(query: string | number, options?: CommonOptions & Record<string, string>): Promise<any>
port?: number

/**
* WHOIS server request timeout in ms.
*
* @default: 1500
*/
timeout?: number

/**
* How many WHOIS server to query.
* 1 = registry server (faster),
* 2 = registry + registrar (more domain details).
*
* @default: 2
*/
follow?: number

/**
* Return the raw WHOIS result in response.
* Added to `__raw`
*/
raw?: boolean

query?: string

/**
* Low level end of query suffix.
*
* @default '\r\n'
*/
querySuffix?: string
}

type OptionsIp = Pick<Options, 'host' | 'timeout' | 'raw'>
type OptionsAsn = OptionsIp
type OptionsQuery = Omit<Options, 'raw' | 'follow'>
type OptionsTld = Pick<Options, 'timeout' | 'raw'>
type OptionsDomain = Omit<Options, 'querySuffix' | 'query' | 'port'>

interface WhoisSearchResult {
[key: string]: string | Array<string> | WhoisSearchResult
}

/**
* Get WHOIS data for a TLD
* Returns a list of all TLDs,
* [downloaded from IANA](https://www.iana.org/domains/root/db)
*
* @param {string} tld Ex. `.net`
* @param {Object} options
* @returns {Promise<*>} Parsed WHOIS server response
* @returns {Promise<string[]>}
*/
tld(tld: string, options?: Omit<CommonOptions, 'host'>): Promise<any>;
function allTlds(): Promise<string[]>

/**
* Query a WHOIS server for data
* Get WHOIS data for an AS number
*
* @param {Object} options
* @returns {Promise<string>} Raw WHOIS server response
* @param {string|number} asn
* @param {OptionsAsn} options
* @returns {Promise<WhoisSearchResult>} Parsed WHOIS server response
*/
query(options: CommonOptions & {
query: string;
/** Low level end of query suffix. Default `\r\n` */
querySuffix?: string;
}): Promise<string>
function asn(asn: string | number, options?: OptionsAsn): Promise<WhoisSearchResult>

/**
* Get parsed WHOIS data for a domain
*
* @param domain
* @param options
* @param {string} domain
* @param {OptionsDomain} options
* @returns {Promise<WhoisSearchResult>} Parsed WHOIS server response
*/
domain(
domain: string,
options?: CommonOptions & {
/** How many WHOIS server to query. 1 = registry server (faster), 2 = registry + registrar (more domain details). Default: 2 */
follow?: number
}
): Promise<any>
function domain(domain: string, options?: OptionsDomain): Promise<WhoisSearchResult>

/**
* Get WHOIS data for a IP
*
* @param {string} ip
* @param {Object} options
* @returns {Promise<*>} Parsed WHOIS server response
* @param {OptionsIp} options
* @returns {Promise<WhoisSearchResult>} Parsed WHOIS server response
*/
ip(ip: string, options?: CommonOptions): Promise<any>
function ip(ip: string, options?: OptionsIp): Promise<WhoisSearchResult>

/**
* Get WHOIS data for an AS number
* Query a WHOIS server for data
*
* @param {string|number} asn
* @param {Object} options
* @returns {Promise<*>} Parsed WHOIS server response
* @param {OptionsQuery} options
* @returns {Promise<string>} Raw WHOIS server response
*/
asn(asn: string | number, options?: CommonOptions): Promise<any>;
function query(options: OptionsQuery): Promise<string>

/**
* Returns a list of all TLDs, [downloaded from IANA](https://www.iana.org/domains/root/db)
* Get WHOIS data for a TLD
*
* @returns {Promise<string[]>}
* @param {string} tld Ex. `.net`
* @param {OptionsTld} options
* @returns {Promise<WhoisSearchResult>} Parsed WHOIS server response
*/
allTlds(): Promise<string[]>;
}
function tld(tld: string, options?: OptionsTld): Promise<WhoisSearchResult>

declare const whoiser: Whoiser;
export = whoiser;
/**
*
* @param {string} query
* @param {Options} options
* @returns {Promise<WhoisSearchResult>} Parsed WHOIS server response
*/
function whoiser(query: string, options?: Options): Promise<WhoisSearchResult>

export = whoiser
}

0 comments on commit e2c60cd

Please sign in to comment.