Skip to content

Commit

Permalink
Export IWithData and IWithForm types
Browse files Browse the repository at this point in the history
Fixes #63

Exports IWithData and IWithForm types
Explicitly makes generic extend IOptionsBase
Changes IWithData's data property to be string, Buffer, or object (allows strings, Buffers, objects, or arrays)

No behavior is affected so no new tests were written
  • Loading branch information
jdforsythe committed Mar 2, 2021
1 parent 545c98d commit 8075b32
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 43 deletions.
8 changes: 7 additions & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ <h5 class="subsection-title">Properties:</h5>


<span class="param-type">string</span>
|

<span class="param-type">Buffer</span>
|

<span class="param-type">object</span>



Expand Down Expand Up @@ -1084,7 +1090,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jun 03 2019 19:40:29 GMT-0700 (Pacific Daylight Time)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:08:53 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
33 changes: 14 additions & 19 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@ <h3> </h3>

<hr>
<blockquote>
<p>The ultra-lightweight Node.js HTTP client</p>
<p>The lightweight Node.js HTTP client</p>
</blockquote>
<p><a href="https://ethanent.github.io/phin/">Full documentation</a> | <a href="https://github.com/ethanent/phin">GitHub</a> | <a href="https://www.npmjs.com/package/phin">NPM</a></p>
<p><a href="https://ethanent.github.io/phin/global.html">Full documentation</a> | <a href="https://github.com/ethanent/phin">GitHub</a> | <a href="https://www.npmjs.com/package/phin">NPM</a></p>
<h2>Simple Usage</h2><pre class="prettyprint source lang-javascript"><code>const p = require('phin')

const res = await p('https://ethanent.me')

console.log(res.body)</code></pre><p>Note that the above should be in an async context! phin also provides an unpromisified version of the library.</p>
<h2>Install</h2><pre class="prettyprint source"><code>npm install phin</code></pre><h2>Why phin?</h2><p>phin is <strong>trusted</strong> by some really important projects. The hundreds of contributors at <a href="https://github.com/less/less.js">Less</a>, for example, depend on phin as part of their development process.</p>
<p>Also, phin is super <strong>lightweight</strong>. Like <strong>99.8% smaller than request</strong> lightweight. To compare to other libraries, see <a href="https://github.com/ethanent/phin/blob/master/README.md#phin-vs-the-competition">phin vs. the Competition</a>.</p>
<p><img src="https://pbs.twimg.com/media/DSPF9TaUQAA0tIe.jpg:large" alt="phin became 33% lighter with release 2.7.0!"/></p>
console.log(res.body)</code></pre><p>Note that the above should be in an async context! Phin also provides an unpromisified version of the library.</p>
<h2>Install</h2><pre class="prettyprint source"><code>npm install phin</code></pre><h2>Why Phin?</h2><p>Phin is relied upon by important projects and large companies. The hundreds of contributors at <a href="https://github.com/less/less.js">Less</a>, for example, depend on Phin as part of their development process.</p>
<p>Also, Phin is very lightweight. To compare to other libraries, see <a href="https://github.com/ethanent/phin/blob/master/README.md#phin-vs-the-competition">Phin vs. the Competition</a>.</p>
<h2>Quick Demos</h2><p>Simple POST:</p>
<pre class="prettyprint source lang-js"><code>await p({
url: 'https://ethanent.me',
Expand Down Expand Up @@ -88,15 +87,15 @@ <h2>Quick Demos</h2><p>Simple POST:</p>
const res = await ppostjson('https://ethanent.me/somejson')
// ^ An options object could also be used here to set other options.

// Do things with res.body?</code></pre><h3>Custom Core HTTP Options</h3><p>phin allows you to set <a href="https://nodejs.org/api/http.html#http_http_request_url_options_callback">core HTTP options</a>.</p>
// Do things with res.body?</code></pre><h3>Custom Core HTTP Options</h3><p>Phin allows you to set <a href="https://nodejs.org/api/http.html#http_http_request_url_options_callback">core HTTP options</a>.</p>
<pre class="prettyprint source lang-js"><code>await p({
'url': 'https://ethanent.me/name',
'core': {
'agent': myAgent // Assuming you'd already created myAgent earlier.
}
})</code></pre><h2>Full Documentation</h2><p>There's a lot more which can be done with the phin library.</p>
<p>See <a href="https://ethanent.github.io/phin/">the phin documentation</a>.</p>
<h2>phin vs. the Competition</h2><p>phin is a very lightweight library, yet it contains all of the common HTTP client features included in competing libraries!</p>
})</code></pre><h2>Full Documentation</h2><p>There's a lot more which can be done with the Phin library.</p>
<p>See <a href="https://ethanent.github.io/phin/global.html">the Phin documentation</a>.</p>
<h2>Phin vs. the Competition</h2><p>Phin is a very lightweight library, yet it contains all of the common HTTP client features included in competing libraries!</p>
<p>Here's a size comparison table:</p>
<table>
<thead>
Expand All @@ -115,16 +114,16 @@ <h2>phin vs. the Competition</h2><p>phin is a very lightweight library, yet it c
<td><a href="https://packagephobia.now.sh/result?p=superagent"><img src="https://packagephobia.now.sh/badge?p=superagent" alt="superagent package size"></a></td>
</tr>
<tr>
<td>isomorphic-fetch</td>
<td><a href="https://packagephobia.now.sh/result?p=isomorphic-fetch"><img src="https://packagephobia.now.sh/badge?p=isomorphic-fetch" alt="isomorphic-fetch package size"></a></td>
<td>got</td>
<td><a href="https://packagephobia.now.sh/result?p=got"><img src="https://packagephobia.now.sh/badge?p=got" alt="got package size"></a></td>
</tr>
<tr>
<td>axios</td>
<td><a href="https://packagephobia.now.sh/result?p=axios"><img src="https://packagephobia.now.sh/badge?p=axios" alt="axios package size"></a></td>
</tr>
<tr>
<td>got</td>
<td><a href="https://packagephobia.now.sh/result?p=got"><img src="https://packagephobia.now.sh/badge?p=got" alt="got package size"></a></td>
<td>isomorphic-fetch</td>
<td><a href="https://packagephobia.now.sh/result?p=isomorphic-fetch"><img src="https://packagephobia.now.sh/badge?p=isomorphic-fetch" alt="isomorphic-fetch package size"></a></td>
</tr>
<tr>
<td>r2</td>
Expand All @@ -135,10 +134,6 @@ <h2>phin vs. the Competition</h2><p>phin is a very lightweight library, yet it c
<td><a href="https://packagephobia.now.sh/result?p=node-fetch"><img src="https://packagephobia.now.sh/badge?p=node-fetch" alt="node-fetch package size"></a></td>
</tr>
<tr>
<td>snekfetch</td>
<td><a href="https://packagephobia.now.sh/result?p=snekfetch"><img src="https://packagephobia.now.sh/badge?p=snekfetch" alt="snekfetch package size"></a></td>
</tr>
<tr>
<td>phin</td>
<td><a href="https://packagephobia.now.sh/result?p=phin"><img src="https://packagephobia.now.sh/badge?p=phin" alt="phin package size"></a></td>
</tr>
Expand All @@ -160,7 +155,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jun 03 2019 19:40:29 GMT-0700 (Pacific Daylight Time)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:08:53 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
16 changes: 13 additions & 3 deletions docs/phin.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h1 class="page-title">Source: phin.js</h1>
* @typedef {Object} phinOptions
* @property {string} url - URL to request (autodetect infers from this URL)
* @property {string} [method=GET] - Request method ('GET', 'POST', etc.)
* @property {string} [data] - Data to send as request body (phin may attempt to convert this data to a string if it isn't already)
* @property {string|Buffer|object} [data] - Data to send as request body (phin may attempt to convert this data to a string if it isn't already)
* @property {Object} [form] - Object to send as form data (sets 'Content-Type' and 'Content-Length' headers, as well as request body) (overwrites 'data' option if present)
* @property {Object} [headers={}] - Request headers
* @property {Object} [core={}] - Custom core HTTP options
Expand Down Expand Up @@ -122,7 +122,17 @@ <h1 class="page-title">Source: phin.js</h1>

// Defaults

phin.defaults = (defaultOpts) => async (opts) => await phin(Object.assign(defaultOpts, typeof opts === 'string' ? {'url': opts} : opts))
phin.defaults = (defaultOpts) => async (opts) => {
const nops = typeof opts === 'string' ? {'url': opts} : opts

Object.keys(defaultOpts).forEach((doK) => {
if (!nops.hasOwnProperty(doK) || nops[doK] === null) {
nops[doK] = defaultOpts[doK]
}
})

return await phin(nops)
}

module.exports = phin
</code></pre>
Expand All @@ -141,7 +151,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jun 03 2019 19:40:29 GMT-0700 (Pacific Daylight Time)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Mar 01 2021 21:08:53 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion lib/phin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const centra = require('centra')
* @typedef {Object} phinOptions
* @property {string} url - URL to request (autodetect infers from this URL)
* @property {string} [method=GET] - Request method ('GET', 'POST', etc.)
* @property {string} [data] - Data to send as request body (phin may attempt to convert this data to a string if it isn't already)
* @property {string|Buffer|object} [data] - Data to send as request body (phin may attempt to convert this data to a string if it isn't already)
* @property {Object} [form] - Object to send as form data (sets 'Content-Type' and 'Content-Length' headers, as well as request body) (overwrites 'data' option if present)
* @property {Object} [headers={}] - Request headers
* @property {Object} [core={}] - Custom core HTTP options
Expand Down
36 changes: 17 additions & 19 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,34 @@ interface IOptionsBase {
path?: string
}

// Form and data property has been written this way so they're mutually exclusive.
type IWithData<T extends {}> = T & {
data: {
toString(): string
}
}

type IWithForm<T extends {}> = T & {
form: {
[index: string]: string
}
}

declare function phin<T>(options:
phin.IJSONResponseOptions |
IWithData<phin.IJSONResponseOptions> |
IWithForm<phin.IJSONResponseOptions>): Promise<phin.IJSONResponse<T>>
phin.IWithData<phin.IJSONResponseOptions> |
phin.IWithForm<phin.IJSONResponseOptions>): Promise<phin.IJSONResponse<T>>

declare function phin(options:
phin.IStreamResponseOptions |
IWithData<phin.IStreamResponseOptions> |
IWithForm<phin.IStreamResponseOptions>): Promise<phin.IStreamResponse>
phin.IWithData<phin.IStreamResponseOptions> |
phin.IWithForm<phin.IStreamResponseOptions>): Promise<phin.IStreamResponse>

declare function phin(options:
phin.IOptions |
IWithData<phin.IOptions> |
IWithForm<phin.IOptions> |
phin.IWithData<phin.IOptions> |
phin.IWithForm<phin.IOptions> |
string): Promise<phin.IResponse>

declare namespace phin {
// Form and data property has been written this way so they're mutually exclusive.
export type IWithData<T extends IOptionsBase> = T & {
data: string | Buffer | object;
}

export type IWithForm<T extends IOptionsBase> = T & {
form: {
[index: string]: string
}
}

export interface IJSONResponseOptions extends IOptionsBase {
parse: 'json'
}
Expand Down

0 comments on commit 8075b32

Please sign in to comment.