Skip to content

Commit

Permalink
Release 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LawsonCheng committed Nov 9, 2021
1 parent c7299ae commit adcfdd6
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 32 deletions.
60 changes: 49 additions & 11 deletions lib/index.d.ts
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;
67 changes: 49 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PokeClass = void 0;
/* eslint-disable indent */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable semi */
const https = require("https");
const http = require("http");
const zlib = require("zlib");
Expand All @@ -13,33 +9,59 @@ const Query_1 = require("./helpers/Query");
const JSON_1 = require("./helpers/JSON");
const Event_1 = require("./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"
*/
class PokeClass extends Event_1.EventManagerClass {
/**
* Constructor
* @param host required. https://foo.com/api
* @param options optional
* @param callback optional
*/
constructor(host, options, callback) {
super();
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
/**
* @private Handling stream response
* @param stream:pipe
* @returns http.Incomingmessage|zlib.Gunzip
*/
this.prepareStream = (stream) => stream;
/**
* Returns the request in the form of Promise()
* @returns Promise<PokeSuccess>
*/
this.promise = () => new Promise((resolve, reject) => {
this.makeRequest(result => {
!PokeResult_1.isPokeError(result) ? resolve(result) : reject(result);
});
});
/**
* Terminate request if it's not completed yet
*/
this.abort = () => {
var _a, _b;
if (((_a = this.req) === null || _a === void 0 ? void 0 : _a.destroy) !== undefined) {
if (this.requestFired === true && ((_a = this.req) === null || _a === void 0 ? void 0 : _a.destroy) !== undefined) {
(_b = this.req) === null || _b === void 0 ? void 0 : _b.destroy;
}
};
/**
* Listening to various of events of the request
* @param eventName
* @param callback
* @returns
*/
this.on = (eventName, callback) => {
// save handler for specific event
this.set(eventName, callback);
// fire request
this.makeRequest();
return this;
};
/**
* Pipe response
* @param stream
* @returns
*/
this.pipe = (stream) => {
// listen to stream event
this.stream(stream);
Expand All @@ -56,7 +78,11 @@ class PokeClass extends Event_1.EventManagerClass {
this.makeRequest(callback);
}
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
/**
* @private Main body to hanlding the request
* @param requestCallback:
* @returns
*/
makeRequest(requestCallback) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
// terminate function if request is fired already
Expand Down Expand Up @@ -180,20 +206,25 @@ class PokeClass extends Event_1.EventManagerClass {
(_s = this.req) === null || _s === void 0 ? void 0 : _s.write(this.options.body || {}); // append body
}
// end of the request
(_t = this.req) === null || _t === void 0 ? void 0 : _t.end(); // req end
(_t = this.req) === null || _t === void 0 ? void 0 : _t.end();
// return PokeResult in callback
if (this.callback !== undefined) {
console.log('1');
// fire request
// fire request directly
this.makeRequest(this.callback);
}
else {
console.log('2');
return this;
}
}
}
exports.PokeClass = PokeClass;
// method to create new Poke instance
/**
* 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)
*/
const Poke = (host, options, callback) => new PokeClass(host, options, callback);
exports.default = Poke;
30 changes: 30 additions & 0 deletions lib/interfaces/PokeOption.d.ts
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;
}
2 changes: 1 addition & 1 deletion lib/interfaces/PokeResult.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export declare type PokeResult = PokeSuccess | PokeError;
* @param input:PokeError|PokeSuccess
* @returns Boolean
*/
export declare function isPokeError(input: any): input is PokeError;
export declare function isPokeError(input: PokeError | PokeSuccess): input is PokeError;
export default PokeResult;
2 changes: 1 addition & 1 deletion lib/interfaces/PokeResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ exports.isPokeError = void 0;
* @returns Boolean
*/
function isPokeError(input) {
return input.error != undefined;
return input['error'] !== undefined;
}
exports.isPokeError = isPokeError;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js.poke",
"version": "1.2.5",
"version": "1.3.0",
"description": "A lightweight and handy JS HTTP client",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit adcfdd6

Please sign in to comment.