Skip to content

Commit

Permalink
docs: updating docs examples to esm modules (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopedrocampos authored Jul 5, 2021
1 parent eacc59e commit d31de8c
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 489 deletions.
123 changes: 63 additions & 60 deletions docs/api/Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ This will instantiate the undici Client, but it will not connect to the origin u

```js
'use strict'
const { Client } = require('undici')
import { Client } from 'undici'

const client = new Client('http://localhost:3000')
```
Expand Down Expand Up @@ -120,35 +120,36 @@ Emitted when a socket has been created and connected. The client will connect on
#### Example - Client connect event

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'

const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()

server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')

client.on('connect', (origin) => {
console.log(`Connected to ${origin}`) // should print before the request body statement
})
const client = new Client(`http://localhost:${server.address().port}`)

client.request({
client.on('connect', (origin) => {
console.log(`Connected to ${origin}`) // should print before the request body statement
})

try {
const { body } = await client.request({
path: '/',
method: 'GET',
}).then(({ body }) => {
body.setEncoding('utf8')
body.on('data', console.log)
client.close()
server.close()
}).catch(error => {
console.error(error)
client.close()
server.close()
method: 'GET'
})
})
body.setEncoding('utf-8')
body.on('data', console.log)
client.close()
server.close()
} catch (error) {
console.error(error)
client.close()
server.close()
}
```

### Event: `'disconnect'`
Expand All @@ -166,30 +167,32 @@ Emitted when socket has disconnected. The error argument of the event is the err
#### Example - Client disconnect event

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'

const server = createServer((request, response) => {
response.destroy()
})
}).listen()

server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')

client.on('disconnect', (origin) => {
console.log(`Disconnected from ${origin}`) // should print before the SocketError
})
const client = new Client(`http://localhost:${server.address().port}`)

client.on('disconnect', (origin) => {
console.log(`Disconnected from ${origin}`)
})

client.request({
try {
await client.request({
path: '/',
method: 'GET',
}).catch(error => {
console.error(error.message)
client.close()
server.close()
method: 'GET'
})
})
} catch (error) {
console.error(error.message)
client.close()
server.close()
}
```

### Event: `'drain'`
Expand All @@ -201,36 +204,36 @@ See [Dispatcher Event: `'drain'`](docs/api/Dispatcher.md#event-drain).
#### Example - Client drain event

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'

const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()

server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')

client.on('drain', () => {
console.log('drain event')
console.log(`Is Client busy: ${client.busy}`)
client.close()
server.close()
})

const requests = [
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' })
]
const client = new Client(`http://localhost:${server.address().port}`)

client.on('drain', () => {
console.log('drain event')
console.log(`Is Client busy: ${client.busy}`)

Promise.all(requests).then(() => {
console.log('requests completed')
})
client.close()
server.close()
})

const requests = [
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' })
]

console.log(`Is Client busy: ${client.busy}`)

await Promise.all(requests)

console.log('requests completed')
```

### Event: `'error'`
Expand Down
Loading

0 comments on commit d31de8c

Please sign in to comment.