Fetch API
Chooksie provides a custom Fetch API wrapper based on the Undici HTTP client.
Usage
To get started, install the undici
package.
TIP
For bots using NodeJS >= 18.0.0, fetch
is now part of NodeJS itself, which means you don't need to install the package to use the wrapper.
$ npm i undici
$ yarn add undici
$ pnpm add undici
Then, to start using the wrapper, import fetch
from chooksie/fetch
:
import { defineOnLoad } from 'chooksie'
import { fetch } from 'chooksie/fetch'
interface Post {
userId: number
id: number
title: string
body: string
}
export const chooksOnLoad = defineOnLoad(async ctx => {
const res = await fetch('https://jsonplaceholder.typicode.com/posts')
const posts = await res.json() as Post[]
for (const post of posts) {
ctx.logger.info(`Got post ID ${post.id}`)
}
})
import { defineOnLoad } from 'chooksie'
import { fetch } from 'chooksie/fetch'
export const chooksOnLoad = defineOnLoad(async ctx => {
const res = await fetch('https://jsonplaceholder.typicode.com/posts')
const posts = await res.json()
for (const post of posts) {
ctx.logger.info(`Got post ID ${post.id}`)
}
})
import { defineOnLoad } from 'chooksie'
import { fetch } from 'chooksie/fetch'
export const chooksOnLoad = defineOnLoad(async ctx => {
const res = await fetch('https://jsonplaceholder.typicode.com/posts')
const posts = await res.json()
for (const post of posts) {
ctx.logger.info(`Got post ID ${post.id}`)
}
})
Out of the box, fetch
works exactly the same as undici.fetch
, but it also includes a couple of utilities to improve your dev experience:
import { defineOnLoad } from 'chooksie'
import { fetch } from 'chooksie/fetch'
interface Post {
userId: number
id: number
title: string
body: string
}
export const chooksOnLoad = defineOnLoad(async ctx => {
// Send a GET request and convert response to JSON
const posts = await fetch<Post[]>('https://jsonplaceholder.typicode.com/posts').json()
ctx.logger.info(`Got ${posts.length} posts.`)
// Send a POST request and access the Response object
const res = await fetch.post('https://jsonplaceholder.typicode.com/posts', {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
})
ctx.logger.info(`API responded with status ${res.status}.`)
// Send a PATCH request and convert response to JSON
const patched = await fetch
.patch<Post>('https://jsonplaceholder.typicode.com/posts/1', {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'foo' }),
})
.json()
ctx.logger.info(`Updated post ID ${patched.id}`)
})
import { defineOnLoad } from 'chooksie'
import { fetch } from 'chooksie/fetch'
export const chooksOnLoad = defineOnLoad(async ctx => {
// Send a GET request and convert response to JSON
const posts = await fetch('https://jsonplaceholder.typicode.com/posts').json()
ctx.logger.info(`Got ${posts.length} posts.`)
// Send a POST request and access the Response object
const res = await fetch.post('https://jsonplaceholder.typicode.com/posts', {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
})
ctx.logger.info(`API responded with status ${res.status}.`)
// Send a PATCH request and convert response to JSON
const patched = await fetch
.patch('https://jsonplaceholder.typicode.com/posts/1', {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'foo' }),
})
.json()
ctx.logger.info(`Updated post ID ${patched.id}`)
})
import { defineOnLoad } from 'chooksie'
import { fetch } from 'chooksie/fetch'
export const chooksOnLoad = defineOnLoad(async ctx => {
// Send a GET request and convert response to JSON
const posts = await fetch('https://jsonplaceholder.typicode.com/posts').json()
ctx.logger.info(`Got ${posts.length} posts.`)
// Send a POST request and access the Response object
const res = await fetch.post('https://jsonplaceholder.typicode.com/posts', {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
})
ctx.logger.info(`API responded with status ${res.status}.`)
// Send a PATCH request and convert response to JSON
const patched = await fetch
.patch('https://jsonplaceholder.typicode.com/posts/1', {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'foo' }),
})
.json()
ctx.logger.info(`Updated post ID ${patched.id}`)
})
Shorthands for HTTP get
, post
, put
, patch
, delete
methods and parsing body using json
, blob
, text
, arrayBuffer
, and body
are available.