TLS: custom CA bundles
Use case
Connect to servers signed by an internal certificate authority (Node.js only).
Smallest working example
import { createClient } from '@parcely/core'
import { readFileSync } from 'node:fs'
const http = createClient({
baseURL: 'https://internal.corp.example.com',
tls: {
ca: readFileSync('/etc/ssl/internal-ca.pem', 'utf-8'),
},
})
const { data } = await http.get('/api/status')
Multiple CAs
Pass an array of PEM strings:
const http = createClient({
baseURL: 'https://internal.corp.example.com',
tls: {
ca: [
readFileSync('/etc/ssl/ca-1.pem', 'utf-8'),
readFileSync('/etc/ssl/ca-2.pem', 'utf-8'),
],
},
})
Axios equivalent
// axios:
import https from 'https'
import { readFileSync } from 'node:fs'
const http = axios.create({
baseURL: 'https://internal.corp.example.com',
httpsAgent: new https.Agent({
ca: readFileSync('/etc/ssl/internal-ca.pem', 'utf-8'),
}),
})
// parcely:
const http2 = createClient({
baseURL: 'https://internal.corp.example.com',
tls: {
ca: readFileSync('/etc/ssl/internal-ca.pem', 'utf-8'),
},
})
Notes and gotchas
- The
caoption acceptsstring | string[](PEM-encoded certificates). - Node-only. In browsers and other runtimes,
tlsis ignored with a one-shotconsole.warn. - You can combine
cawithrejectUnauthorized: true(the default) to enforce validation against your custom CA while still rejecting unknown certificates. - The
TlsConfigtype:{ rejectUnauthorized?: boolean; ca?: string | string[] }.