Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to esm #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Note, all these examples also work in the browser with [browserify](http://brows
Doesn't get easier than this:

```js
const get = require('simple-get')
import get from 'simple-get'

get('http://example.com', function (err, res) {
if (err) throw err
Expand All @@ -53,7 +53,7 @@ get('http://example.com', function (err, res) {
If you just want the data, and don't want to deal with streams:

```js
const get = require('simple-get')
import get from 'simple-get'

get.concat('http://example.com', function (err, res, data) {
if (err) throw err
Expand All @@ -67,7 +67,7 @@ get.concat('http://example.com', function (err, res, data) {
For `POST`, call `get.post` or use option `{ method: 'POST' }`.

```js
const get = require('simple-get')
import get from 'simple-get'

const opts = {
url: 'http://example.com',
Expand All @@ -82,7 +82,7 @@ get.post(opts, function (err, res) {
#### A more complex example:

```js
const get = require('simple-get')
import get from 'simple-get'

get({
url: 'http://example.com',
Expand Down Expand Up @@ -117,7 +117,7 @@ get({
You can serialize/deserialize request and response with JSON:

```js
const get = require('simple-get')
import get from 'simple-get'

const opts = {
method: 'POST',
Expand All @@ -140,7 +140,7 @@ If the request takes longer than `timeout` to complete, then the entire request
will fail with an `Error`.

```js
const get = require('simple-get')
import get from 'simple-get'

const opts = {
url: 'http://example.com',
Expand All @@ -156,12 +156,11 @@ It's a good idea to set the `'user-agent'` header so the provider can more easil
see how their resource is used.

```js
const get = require('simple-get')
const pkg = require('./package.json')
import get from 'simple-get'

get('http://example.com', {
headers: {
'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
'user-agent': `my-module/2.1.6 (https://github.com/username/my-module)`
}
})
```
Expand All @@ -172,8 +171,8 @@ You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with t
`agent` option to work with proxies:

```js
const get = require('simple-get')
const tunnel = require('tunnel')
import get from 'simple-get'
import tunnel from 'tunnel'

const opts = {
url: 'http://example.com',
Expand All @@ -193,8 +192,8 @@ You can use the [`cookie`](https://github.com/jshttp/cookie) module to include
cookies in a request:

```js
const get = require('simple-get')
const cookie = require('cookie')
import get from 'simple-get'
import cookie from 'cookie'

const opts = {
url: 'http://example.com',
Expand All @@ -212,9 +211,9 @@ You can use the [`form-data`](https://github.com/form-data/form-data) module to
create POST request with form data:

```js
const fs = require('fs')
const get = require('simple-get')
const FormData = require('form-data')
import fs from 'fs'
import get from 'simple-get'
import FormData from 'form-data'
const form = new FormData()

form.append('my_file', fs.createReadStream('/foo/bar.jpg'))
Expand Down Expand Up @@ -244,7 +243,7 @@ get.post(opts, function (err, res) {})
### Specifically disallowing redirects

```js
const get = require('simple-get')
import get from 'simple-get'

const opts = {
url: 'http://example.com/will-redirect-elsewhere',
Expand Down Expand Up @@ -274,9 +273,9 @@ You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to crea
a signed OAuth request:

```js
const get = require('simple-get')
const crypto = require('crypto')
const OAuth = require('oauth-1.0a')
import get from 'simple-get'
import crypto from 'crypto'
import OAuth from 'oauth-1.0a'

const oauth = OAuth({
consumer: {
Expand Down Expand Up @@ -308,8 +307,8 @@ get(opts, function (err, res) {})
You can use [limiter](https://github.com/jhurliman/node-rate-limiter) to throttle requests. This is useful when calling an API that is rate limited.

```js
const simpleGet = require('simple-get')
const RateLimiter = require('limiter').RateLimiter
import simpleGet from 'simple-get'
import {RateLimiter} from 'limiter'
const limiter = new RateLimiter(1, 'second')

const get = (opts, cb) => limiter.removeTokens(1, () => simpleGet(opts, cb))
Expand Down
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/*! simple-get. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
module.exports = simpleGet

const concat = require('simple-concat')
const decompressResponse = require('decompress-response') // excluded from browser build
const http = require('http')
const https = require('https')
const once = require('once')
const querystring = require('querystring')
const url = require('url')
import http from 'node:http'
import https from 'node:https'
import { stringify } from 'node:querystring'
import url from 'node:url'
import concat from 'simple-concat'
import decompressResponse from 'decompress-response' // excluded from browser build
import once from 'once'

const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'

function simpleGet (opts, cb) {
export default function simpleGet (opts, cb) {
opts = Object.assign({ maxRedirects: 10 }, typeof opts === 'string' ? { url: opts } : opts)
cb = once(cb)

Expand All @@ -30,7 +29,7 @@ function simpleGet (opts, cb) {
if (opts.body) {
body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body
} else if (opts.form) {
body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form)
body = typeof opts.form === 'string' ? opts.form : stringify(opts.form)
opts.headers['content-type'] = 'application/x-www-form-urlencoded'
}

Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
"email": "feross@feross.org",
"url": "https://feross.org"
},
"type": "module",
"browser": {
"decompress-response": false
},
"bugs": {
"url": "https://github.com/feross/simple-get/issues"
},
"dependencies": {
"decompress-response": "^6.0.0",
"once": "^1.3.1",
"simple-concat": "^1.0.0"
"decompress-response": "^7.0.0",
"once": "^1.4.0",
"simple-concat": "^1.0.1"
},
"devDependencies": {
"self-signed-https": "^1.0.5",
"standard": "*",
"string-to-stream": "^3.0.0",
"tape": "^5.0.0"
"string-to-stream": "^3.0.1",
"tape": "^5.3.1"
},
"homepage": "https://github.com/feross/simple-get",
"keywords": [
Expand Down
10 changes: 5 additions & 5 deletions test/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const concat = require('simple-concat')
const get = require('../')
const http = require('http')
const test = require('tape')
import { createServer } from 'node:http'
import concat from 'simple-concat'
import test from 'tape'
import get from '../index.js'

test('basic auth', function (t) {
t.plan(5)

const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
t.equal(req.headers.authorization, 'Basic Zm9vOmJhcg==')
res.statusCode = 200
res.end('response')
Expand Down
24 changes: 13 additions & 11 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const concat = require('simple-concat')
const get = require('../')
const http = require('http')
const selfSignedHttps = require('self-signed-https')
const test = require('tape')
import { createServer } from 'node:http'
import concat from 'simple-concat'
import selfSignedHttps from 'self-signed-https'
import test from 'tape'
import get from '../index.js'

const { head } = get

test('simple get', function (t) {
t.plan(5)

const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
t.equal(req.url, '/path')
res.statusCode = 200
res.end('response')
Expand Down Expand Up @@ -56,7 +58,7 @@ test('https', function (t) {
test('simple get json', function (t) {
t.plan(6)

const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
t.equal(req.url, '/path')
t.equal(req.headers.accept, 'application/json')
res.statusCode = 200
Expand Down Expand Up @@ -84,7 +86,7 @@ test('simple get json', function (t) {
test('HEAD request', function (t) {
t.plan(3)

const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
t.equal(req.method, 'HEAD')
// Taken from real-world response from HEAD request to GitHub.com
res.setHeader('content-type', 'text/html; charset=utf-8')
Expand All @@ -100,7 +102,7 @@ test('HEAD request', function (t) {
method: 'HEAD',
url: 'http://localhost:' + port
}
get.head(opts, function (err, res) {
head(opts, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
server.close()
Expand All @@ -111,7 +113,7 @@ test('HEAD request', function (t) {
test('timeout option', function (t) {
t.plan(2)

const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
t.equal(req.url, '/path')
setTimeout(function () {
// response should not be sent - should timeout before it's sent
Expand All @@ -136,7 +138,7 @@ test('rewrite POST redirects to GET', function (t) {

let redirected = false

const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
if (redirected) {
t.equal(req.url, '/getthis')
t.equal(req.method, 'GET')
Expand Down
26 changes: 14 additions & 12 deletions test/concat.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const get = require('../')
const http = require('http')
const str = require('string-to-stream')
const test = require('tape')
import { createServer } from 'node:http'
import str from 'string-to-stream'
import test from 'tape'
import get from '../index.js'

const { concat } = get

test('get.concat (post, stream body, and json option)', function (t) {
t.plan(4)

const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
res.statusCode = 200
req.pipe(res)
})
Expand All @@ -19,7 +21,7 @@ test('get.concat (post, stream body, and json option)', function (t) {
method: 'POST',
json: true
}
get.concat(opts, function (err, res, data) {
concat(opts, function (err, res, data) {
t.error(err)
t.equal(typeof data, 'object')
t.deepEqual(Object.keys(data), ['a'])
Expand All @@ -31,14 +33,14 @@ test('get.concat (post, stream body, and json option)', function (t) {

test('get.concat', function (t) {
t.plan(4)
const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
res.statusCode = 200
res.end('blah blah blah')
})

server.listen(0, function () {
const port = server.address().port
get.concat('http://localhost:' + port, function (err, res, data) {
concat('http://localhost:' + port, function (err, res, data) {
t.error(err)
t.equal(res.statusCode, 200)
t.ok(Buffer.isBuffer(data), '`data` is type buffer')
Expand All @@ -50,7 +52,7 @@ test('get.concat', function (t) {

test('get.concat json', function (t) {
t.plan(3)
const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
res.statusCode = 200
res.end('{"message":"response"}')
})
Expand All @@ -61,7 +63,7 @@ test('get.concat json', function (t) {
url: 'http://localhost:' + port + '/path',
json: true
}
get.concat(opts, function (err, res, data) {
concat(opts, function (err, res, data) {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(data.message, 'response')
Expand All @@ -72,7 +74,7 @@ test('get.concat json', function (t) {

test('get.concat json error', function (t) {
t.plan(1)
const server = http.createServer(function (req, res) {
const server = createServer(function (req, res) {
res.statusCode = 500
res.end('not json')
})
Expand All @@ -83,7 +85,7 @@ test('get.concat json error', function (t) {
url: 'http://localhost:' + port + '/path',
json: true
}
get.concat(opts, function (err, res, data) {
concat(opts, function (err, res, data) {
t.ok(err instanceof Error)
server.close()
})
Expand Down
Loading