-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7299ae
commit adcfdd6
Showing
6 changed files
with
131 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,69 @@ | ||
/// <reference types="node" /> | ||
import * as http from 'http'; | ||
import * as zlib from 'zlib'; | ||
import { WriteStream } from 'fs'; | ||
import { ServerResponse } from 'http'; | ||
import PokeOption from './interfaces/PokeOption'; | ||
import PokeResult, { PokeSuccess } from './interfaces/PokeResult'; | ||
import { EventManagerClass } from './helpers/Event'; | ||
/** | ||
* Usage: https://github.com/LawsonCheng/poke/blob/main/README.md | ||
* | ||
* @param host:string The destination of your request pointing to. | ||
* @param options Specify your method, body, headers etc to customize your request | ||
* @param callback Callback(result:PokeResult) will be called when the request is completed | ||
* @returns Promise<PokeResult> | .on('response'|'end'|'data'|'error', callback(result:any)) | pipe(stream:WriteStream) | ||
* Class of "Poke" | ||
*/ | ||
export declare class PokeClass extends EventManagerClass { | ||
host: string; | ||
options?: PokeOption<Body>; | ||
callback?: (pr: PokeResult) => void; | ||
requestFired: boolean; | ||
req?: http.ClientRequest; | ||
protected callback?: (pr: PokeResult) => void; | ||
protected requestFired: boolean; | ||
protected req?: http.ClientRequest; | ||
/** | ||
* Constructor | ||
* @param host required. https://foo.com/api | ||
* @param options optional | ||
* @param callback optional | ||
*/ | ||
constructor(host: string, options?: PokeOption<Body>, callback?: (pr: PokeResult) => void); | ||
private prepareStream; | ||
private makeRequest; | ||
/** | ||
* @private Handling stream response | ||
* @param stream:pipe | ||
* @returns http.Incomingmessage|zlib.Gunzip | ||
*/ | ||
protected prepareStream: (stream: http.IncomingMessage | zlib.Gunzip) => http.IncomingMessage | zlib.Gunzip; | ||
/** | ||
* @private Main body to hanlding the request | ||
* @param requestCallback: | ||
* @returns | ||
*/ | ||
protected makeRequest(requestCallback?: (pokeResult: PokeResult) => void): this | void; | ||
/** | ||
* Returns the request in the form of Promise() | ||
* @returns Promise<PokeSuccess> | ||
*/ | ||
promise: () => Promise<PokeSuccess>; | ||
/** | ||
* Terminate request if it's not completed yet | ||
*/ | ||
abort: () => void; | ||
/** | ||
* Listening to various of events of the request | ||
* @param eventName | ||
* @param callback | ||
* @returns | ||
*/ | ||
on: (eventName: string, callback: () => void) => this; | ||
/** | ||
* Pipe response | ||
* @param stream | ||
* @returns | ||
*/ | ||
pipe: (stream: any) => WriteStream | ServerResponse; | ||
} | ||
/** | ||
* Usage: https://github.com/LawsonCheng/poke/blob/main/README.md | ||
* | ||
* @param host:string The destination of your request pointing to. | ||
* @param options Specify your method, body, headers etc to customize your request | ||
* @param callback Callback(result:PokeResult) will be called when the request is completed | ||
* @returns Promise<PokeResult> | .on('response'|'end'|'data'|'error', callback(result:any)) | pipe(stream:WriteStream) | ||
*/ | ||
declare const Poke: (host: string, options?: PokeOption<Body> | undefined, callback?: ((pr: PokeResult) => void) | undefined) => PokeClass; | ||
export default Poke; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,44 @@ | ||
export default interface PokeOption<Body> { | ||
/** | ||
* @description Standard http request methods | ||
*/ | ||
method?: 'POST' | 'GET' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE'; | ||
/** | ||
* @description The path of the requested url | ||
*/ | ||
path?: string; | ||
/** | ||
* @description Port number of the requested url | ||
*/ | ||
port?: number; | ||
/** | ||
* @description Put your customized headers here | ||
*/ | ||
headers?: Headers; | ||
/** | ||
* @description Accepts Query object, the object will be parsed into query string | ||
*/ | ||
query?: { | ||
[key: string]: number | boolean | string | null; | ||
}; | ||
/** | ||
* @description Form Data/Request Body | ||
*/ | ||
body?: Body; | ||
/** | ||
* @description Set true if use gzip, default false | ||
*/ | ||
gzip?: boolean; | ||
/** | ||
* @description Terminate when the timeout value is reacted if the request is not finished yet. e.g. 1000 for 1s | ||
*/ | ||
timeout?: number; | ||
/** | ||
* @description The username of basic auth | ||
*/ | ||
username?: string; | ||
/** | ||
* @description The password of basic auth | ||
*/ | ||
password?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters