mirror of
https://github.com/Wessel/wumpfetch.git
synced 2026-07-21 23:57:11 +02:00
Some QOL improvements
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
const path = require( 'path' );
|
const path = require( 'path' );
|
||||||
|
|
||||||
module.exports = ( url, method ) => { return new( require( path.join( __dirname, 'model', 'WumpRequest.js' ) ) )( url, method ); }
|
module.exports = ( url, method ) => { return new( require( path.join( __dirname, 'model', 'WumpRequest.js' ) ) )( url, method ); };
|
||||||
@@ -14,107 +14,129 @@ module.exports = class WumpRequest {
|
|||||||
const o = typeof url === 'string' ? m : url;
|
const o = typeof url === 'string' ? m : url;
|
||||||
const obj = typeof o === 'object';
|
const obj = typeof o === 'object';
|
||||||
|
|
||||||
this.m = typeof o === 'string' ? o : obj && o.method ? o.method : 'GET';
|
if ( typeof url !== 'string' && !o.hasOwnProperty( 'url' ) ) throw new Error( 'Missing url parameter' );
|
||||||
this.url = typeof url === 'string' ? new URL( url ) : obj && typeof o.url === 'string' ? new URL( o.url ) : url;
|
|
||||||
this.SDA = obj && typeof o.sendDataAs === 'string' ? o.sendDataAs : null;
|
this.o = {
|
||||||
this.data = obj && o.data ? o.data : null;
|
'm' : typeof o === 'string' ? o : obj && o.method ? o.method : 'GET',
|
||||||
this.streamed = obj && o.streamed ? true : false;
|
'url' : typeof url === 'string' ? new URL( url ) : obj && typeof o.url === 'string' ? new URL( o.url ) : url,
|
||||||
this.compressed = obj && o.compressed ? true : false;
|
'SDA' : obj && typeof o.sendDataAs === 'string' ? o.sendDataAs : null,
|
||||||
this.rHeaders = obj && typeof o.headers === 'object' ? o.headers : {};
|
'data' : obj && o.data ? o.data : obj && o.form ? qs.stringify( o.form ) : null,
|
||||||
this.timeoutTime = obj && typeof o.timeout === 'number' ? o.timeout : null;
|
'parse' : obj && o.parse ? o.parse : null,
|
||||||
this.coreOptions = obj && typeof o.coreOptions === 'object' ? o.coreOptions : {};
|
'follow' : !!( obj && o.followRedirects ),
|
||||||
|
'streamed' : !!( obj && o.streamed ),
|
||||||
|
'compressed' : !!( obj && o.compressed ),
|
||||||
|
'rHeaders' : obj && typeof o.headers === 'object' ? o.headers : {},
|
||||||
|
'timeoutTime' : obj && typeof o.timeout === 'number' ? o.timeout : null,
|
||||||
|
'coreOptions' : obj && typeof o.coreOptions === 'object' ? o.coreOptions : {}
|
||||||
|
};
|
||||||
|
|
||||||
|
if ( typeof o.core === 'object' ) Object.keys( o.core ).forEach( ( v ) => this.option( v, o.core[ v ] ) );
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
query ( a, b ) {
|
query ( a, b ) {
|
||||||
if ( typeof a === 'object' ) Object.keys( a ).forEach( ( v ) => this.url.searchParams.append( v, a[ v ] ) );
|
if ( typeof a === 'object' ) Object.keys( a ).forEach( ( v ) => this.o.url.searchParams.append( v, a[ v ] ) );
|
||||||
else this.url.searchParams.append( a, b );
|
else this.o.url.searchParams.append( a, b );
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
body ( data, SA ) {
|
body ( data, SA ) {
|
||||||
this.SDA = typeof data === 'object' && !SA && !Buffer.isBuffer( data ) ? 'json' : ( SA ? SA.toLowerCase() : 'buffer' );
|
this.o.SDA = typeof data === 'object' && !SA && !Buffer.isBuffer( data ) ? 'json' : ( SA ? SA.toLowerCase() : 'buffer' );
|
||||||
this.data = this.SDA === 'form' ? qs.stringify( data ) : ( this.SDA === 'json' ? JSON.stringify( data ) : data );
|
this.o.data = this.SDA === 'form' ? qs.stringify( data ) : ( this.SDA === 'json' ? JSON.stringify( data ) : data );
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
header ( a, b ) {
|
header ( a, b ) {
|
||||||
if (typeof a === 'object') Object.keys( a ).forEach( ( v ) => this.rHeaders[ v.toLowerCase() ] = a[ v ] );
|
if (typeof a === 'object') Object.keys( a ).forEach( ( v ) => this.o.rHeaders[ v.toLowerCase() ] = a[ v ] );
|
||||||
else this.rHeaders[ a.toLowerCase() ] = b;
|
else this.o.rHeaders[ a.toLowerCase() ] = b;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
compress () {
|
compress () {
|
||||||
this.compressed = true;
|
this.compressed = true;
|
||||||
if ( !this.rHeaders[ 'accept-encoding' ] ) this.rHeaders[ 'accept-encoding' ] = c.join( ', ' );
|
if ( !this.o.rHeaders[ 'accept-encoding' ] ) this.o.rHeaders[ 'accept-encoding' ] = c.join( ', ' );
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
path ( p ) { this.url.pathname = path.join( this.url.pathname, p ); return this; }
|
path ( p ) { this.o.url.pathname = path.join( this.o.url.pathname, p ); return this; }
|
||||||
stream () { this.streamed = true; return this; }
|
stream () { this.o.streamed = true; return this; }
|
||||||
option ( n, v ) { this.coreOptions[ n ] = v; return this; }
|
option ( n, v ) { this.o.coreOptions[ n ] = v; return this; }
|
||||||
timeout ( timeout ) { this.timeoutTime = timeout; return this; }
|
timeout ( timeout ) { this.o.timeoutTime = timeout; return this; }
|
||||||
|
|
||||||
send () {
|
send () {
|
||||||
return new Promise( ( resolve, reject ) => {
|
return new Promise( ( resolve, reject ) => {
|
||||||
if ( this.data ) {
|
if ( this.o.data ) {
|
||||||
if ( !this.rHeaders.hasOwnProperty( 'user-agent' ) ) this.rHeaders[ 'User-Agent' ] = 'wumpfetch/0.0.1 (https://github.com/PassTheWessel/wumpfetch)';
|
if ( !this.o.rHeaders.hasOwnProperty( 'user-agent' ) ) this.o.rHeaders[ 'User-Agent' ] = 'wumpfetch/0.0.1 (https://github.com/PassTheWessel/wumpfetch)';
|
||||||
|
|
||||||
if ( this.SDA === 'json' && !this.rHeaders.hasOwnProperty( 'content-type' ) ) this.rHeaders[ 'Content-Type' ] = 'application/json';
|
if ( this.o.SDA === 'json' && !this.o.rHeaders.hasOwnProperty( 'content-type' ) ) this.o.rHeaders[ 'Content-Type' ] = 'application/json';
|
||||||
if ( this.SDA === 'form') {
|
if ( this.o.SDA === 'form') {
|
||||||
if ( !this.rHeaders.hasOwnProperty( 'content-type' ) ) this.rHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
|
if ( !this.o.rHeaders.hasOwnProperty( 'content-type' ) ) this.o.rHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||||
if ( !this.rHeaders.hasOwnProperty( 'content-length' ) ) this.rHeaders[ 'Content-Length' ] = Buffer.byteLength( this.data );
|
if ( !this.o.rHeaders.hasOwnProperty( 'content-length' ) ) this.o.rHeaders[ 'Content-Length' ] = Buffer.byteLength( this.o.data );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let req;
|
let req;
|
||||||
const options = Object.assign({
|
const options = Object.assign({
|
||||||
'host' : this.url.hostname,
|
'host' : this.o.url.hostname,
|
||||||
'port' : this.url.port,
|
'port' : this.o.url.port,
|
||||||
'path' : this.url.pathname + this.url.search,
|
'path' : this.o.url.pathname + this.o.url.search,
|
||||||
'method' : this.m,
|
'method' : this.o.m,
|
||||||
'headers' : this.rHeaders,
|
'headers' : this.o.rHeaders,
|
||||||
'protocol': this.url.protocol
|
'protocol': this.o.url.protocol
|
||||||
}, this.coreOptions );
|
}, this.o.coreOptions );
|
||||||
|
|
||||||
const resHandler = ( res ) => {
|
const resHandler = async( res ) => {
|
||||||
let stream = res;
|
let stream = res;
|
||||||
|
|
||||||
if ( this.compressed ) {
|
if ( this.o.compressed ) {
|
||||||
if ( res.headers[ 'content-encoding' ] === 'gzip' ) stream = res.pipe( zlib.createGunzip() );
|
if ( res.headers[ 'content-encoding' ] === 'gzip' ) stream = res.pipe( zlib.createGunzip() );
|
||||||
else if ( res.headers[ 'content-encoding' ] === 'deflate' ) stream = res.pipe( zlib.createInflate() );
|
else if ( res.headers[ 'content-encoding' ] === 'deflate' ) stream = res.pipe( zlib.createInflate() );
|
||||||
}
|
}
|
||||||
|
|
||||||
let wumpRes;
|
let wumpRes;
|
||||||
|
|
||||||
if ( this.streamed ) resolve( stream );
|
if ( this.o.streamed ) resolve( stream );
|
||||||
else {
|
else {
|
||||||
wumpRes = new WumpResponse( res );
|
wumpRes = new WumpResponse( res );
|
||||||
|
|
||||||
|
if ( res.headers.hasOwnProperty('location') && this.o.follow ) {
|
||||||
|
this.o.url = ( new URL( res.headers[ 'location' ], this.o.url ) ).toString();
|
||||||
|
return await this( this.o );
|
||||||
|
}
|
||||||
|
|
||||||
stream.on( 'error', ( e ) => reject( e ) );
|
stream.on( 'error', ( e ) => reject( e ) );
|
||||||
stream.on( 'data', ( c ) => wumpRes._addChunk( c ) );
|
stream.on( 'data', ( c ) => wumpRes._addChunk( c ) );
|
||||||
stream.on( 'end', () => resolve( wumpRes ) );
|
stream.on( 'end', () => {
|
||||||
|
if ( this.o.parse ) {
|
||||||
|
if ( this.o.parse === 'json' ) wumpRes.body = JSON.parse( wumpRes.body );
|
||||||
|
else if ( this.o.parse === 'text' ) wumpRes.body = wumpRes.body.toString();
|
||||||
|
else wumpRes.body = wumpRes.body;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve( wumpRes );
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if ( this.url.protocol === 'http:' ) req = http.request( options, resHandler );
|
if ( this.o.url.protocol === 'http:' ) req = http.request( options, resHandler );
|
||||||
else if (this.url.protocol === 'https:') req = https.request( options, resHandler );
|
else if (this.o.url.protocol === 'https:') req = https.request( options, resHandler );
|
||||||
else throw new Error( `Bad URL protocol: ${this.url.protocol}` );
|
else throw new Error( `Bad URL protocol: ${this.o.url.protocol}` );
|
||||||
|
|
||||||
if ( this.timeoutTime ) {
|
|
||||||
req.setTimeout( this.timeoutTime, () => {
|
if ( this.o.timeoutTime ) {
|
||||||
|
req.setTimeout( this.o.timeoutTime, () => {
|
||||||
req.abort();
|
req.abort();
|
||||||
if ( !this.streamed ) reject( new Error( 'Timeout reached' ) );
|
if ( !this.o.streamed ) reject( new Error( 'Timeout reached' ) );
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
req.on( 'error', ( e ) => reject( e ) );
|
req.on( 'error', ( e ) => reject( e ) );
|
||||||
if ( this.data ) req.write( this.data );
|
if ( this.o.data ) req.write( this.o.data );
|
||||||
|
|
||||||
req.end();
|
req.end();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module.exports = class WumpResponse {
|
module.exports = class WumpResponse {
|
||||||
constructor ( res ) {
|
constructor ( res ) {
|
||||||
this.body = Buffer.alloc( 0 );
|
this.body = Buffer.alloc( 0 );
|
||||||
this.coreRes = res
|
this.coreRes = res;
|
||||||
|
|
||||||
this.headers = res.headers;
|
this.headers = res.headers;
|
||||||
this.statusCode = res.statusCode;
|
this.statusCode = res.statusCode;
|
||||||
@@ -11,4 +11,4 @@ module.exports = class WumpResponse {
|
|||||||
|
|
||||||
text () { return this.body.toString(); }
|
text () { return this.body.toString(); }
|
||||||
json () { return JSON.parse( this.body ); }
|
json () { return JSON.parse( this.body ); }
|
||||||
}
|
};
|
||||||
4
test.js
4
test.js
@@ -1,6 +1,6 @@
|
|||||||
const w = require( './createRequest' );
|
const w = require( './createRequest' );
|
||||||
|
|
||||||
;( async() => {
|
;( async() => {
|
||||||
const r = await w( { url: 'https://aws.random.cat/meow' } ).send();
|
const r = await w( { url: 'https://aws.random.cat/meow', parse: 'json' } ).send();
|
||||||
console.log( r.json() )
|
console.log( r.body );
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user