[proxy-lists] Add types for proxy-lists (#25267)

* [proxy-lists] Add types for proxy-lists

* Change minimum TS version

These definitions depends on request, which has a minimum TS version
 of 2.3.

* Refactor static class and namespace to plain ES6 exports
This commit is contained in:
BehindTheMath
2018-04-25 15:08:37 -04:00
committed by Wesley Wigham
parent e178d12a21
commit 39d211b878
4 changed files with 149 additions and 0 deletions

70
types/proxy-lists/index.d.ts vendored Normal file
View File

@@ -0,0 +1,70 @@
// Type definitions for proxy-lists 1.14
// Project: https://github.com/chill117/proxy-lists#readme
// Definitions by: BehindTheMath <https://github.com/BehindTheMath>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { CoreOptions as RequestOptions } from "request";
import { EventEmitter } from "events";
export function getProxies(options?: Partial<Options>): GetProxiesEventEmitter;
export function getProxiesFromSource(name: string, options?: Options): GetProxiesEventEmitter;
export function addSource(name: string, source: AddSource): void;
export function listSources(options?: ListSourcesOptions): Source[];
export class GetProxiesEventEmitter extends EventEmitter {
on(event: "data", listener: (proxies: Proxy[]) => void): this;
on(event: "error", listener: (error: any) => void): this;
on(event: "end", listener: () => void): this;
}
export interface Options {
filterMode?: "strict" | "loose";
countries?: string[];
countriesBlackList?: string[];
protocols?: Protocol[];
anonymityLevels?: AnonymityLevel[];
sourcesWhiteList?: string[];
sourcesBlackList?: string[];
series?: boolean;
ipTypes?: IPType[];
defaultRequestOptions?: RequestOptions;
}
export type Protocol = "http" | "https" | "socks5" | "socks4";
export type AnonymityLevel = "transparent" | "anonymous" | "elite";
export type IPType = "ipv4" | "ipv6";
export interface Proxy {
ipAddress: string;
port: number;
country: string;
anonymityLevel?: AnonymityLevel;
protocols?: Protocol[];
source: string;
tunnel?: boolean;
}
export interface InternalOptions extends Options {
sample?: boolean;
}
export interface AddSource {
homeUrl: string;
getProxies(options: InternalOptions): GetProxiesEventEmitter;
}
export interface ListSourcesOptions {
sourcesWhiteList?: string[];
sourcesBlackList?: string[];
}
export interface Source {
name: string;
homeUrl: string;
}

View File

@@ -0,0 +1,55 @@
import * as ProxyLists from "proxy-lists";
import { EventEmitter } from "events";
const options: ProxyLists.Options = {
filterMode: 'strict',
countries: ['us', 'ca'],
countriesBlackList: ['de', 'gb'],
protocols: ['http', 'https'],
anonymityLevels: ['anonymous', 'elite'],
sourcesWhiteList: ['freeproxylists'],
sourcesBlackList: ['freeproxylists'],
series: false,
ipTypes: ['ipv4'],
defaultRequestOptions: {
method: "GET"
}
};
// `gettingProxies` is an event emitter object.
let gettingProxies: ProxyLists.GetProxiesEventEmitter = ProxyLists.getProxies(options);
gettingProxies.on('data', proxies => {
// Received some proxies.
});
gettingProxies.on('error', error => {
// Some error has occurred.
console.error(error);
});
gettingProxies.once('end', () => {
// Done getting proxies.
});
gettingProxies = ProxyLists.getProxiesFromSource('freeproxylists', options);
gettingProxies.on('data', proxies => {
// Received some proxies.
});
const source: ProxyLists.AddSource = {
homeUrl: 'www.example.com',
getProxies: (options: ProxyLists.InternalOptions) => {
return new EventEmitter();
}
};
ProxyLists.addSource('testSource', source);
const listSourcesOptions: ProxyLists.ListSourcesOptions = {
sourcesWhiteList: ['freeproxylists'],
sourcesBlackList: ['freeproxylists']
};
ProxyLists.listSources(listSourcesOptions).forEach(source => console.log(source.name, source.homeUrl));

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strictFunctionTypes": true
},
"files": [
"index.d.ts",
"proxy-lists-tests.ts"
]
}

View File

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