Custom fetch
Test
node test-server.js
node test.js
result:
const fetch = require('./index.js').default
async function get () {
fetch.debug = false
const res = await fetch.get('http://localhost:8000')
console.log('get ', res);
}
async function cookie () {
fetch.debug = false
fetch.credentials = "same-origin"
const headers = {
'content-type': 'application/json',
cookie: 'accessToken=1234abc;userId=1234'
}
fetch.headers = headers
const res = await fetch.get('http://localhost:8000/cookie')
console.log('cookie ', res);
}
async function post () {
const res = await fetch.post('http://localhost:8000')
console.log('post ', res);
}
async function postWithAuth () {
const headers = {
'content-type': 'application/json',
Authorization: 'Basic YTpi'
}
fetch.headers = headers
const res = await fetch.post('http://localhost:8000/auth')
console.log('postWithAuth ', res);
}
async function main () {
get()
post()
postWithAuth()
cookie()
}
main()
Compile
npx tsc -d --outDir ./
Publish
npm publish --access public