-
-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client option to pass custom RequestInit object into fetch reques…
…ts for supported implementations (#2020) * Add client option to pass custom RequestInit object into fetch requests for supported implementations * Add client option to pass custom RequestInit object into fetch requests for supported implementations - formatting * Add client option to pass custom RequestInit object into fetch requests for supported implementations - formatting * Add client option to pass custom RequestInit object into fetch requests for supported implementations - fix e2e test failures * Add client option to pass custom RequestInit object into fetch requests for supported implementations - refactor test to use own server/endpoint * Add client option to pass custom RequestInit object into fetch requests for supported implementations - refactor test to use own server/endpoint * Add client option to pass custom RequestInit object into fetch requests for supported implementations - refactor test to use own server/endpoint * Add client option to pass custom RequestInit object into fetch requests for supported implementations - refactor test to use own server/endpoint * Add client option to pass custom RequestInit object into fetch requests for supported implementations - fix lockfile * Add client option to pass custom RequestInit object into fetch requests for supported implementations - add changeset * Add client option to pass custom RequestInit object into fetch requests for supported implementations - add changeset
- Loading branch information
1 parent
75399f8
commit 7081842
Showing
6 changed files
with
498 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-fetch": patch | ||
--- | ||
|
||
Add client option to pass custom RequestInit object into fetch requests for supported implementations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/openapi-fetch/test/common/create-client-e2e.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import express from "express"; | ||
import { expect, test } from "vitest"; | ||
import * as https from "node:https"; | ||
import { Agent } from "undici"; | ||
import createClient from "../../src/index.js"; | ||
import * as forge from "node-forge"; | ||
import * as crypto from "node:crypto"; | ||
|
||
const pki = forge.pki; | ||
|
||
const genCACert = async (opts = {}) => { | ||
const options = { | ||
...{ | ||
commonName: "Testing CA - DO NOT TRUST", | ||
bits: 2048, | ||
}, | ||
...opts, | ||
}; | ||
|
||
const keyPair = await new Promise((res, rej) => { | ||
pki.rsa.generateKeyPair({ bits: options.bits }, (error, pair) => { | ||
if (error) { | ||
rej(error); | ||
} else { | ||
res(pair); | ||
} | ||
}); | ||
}); | ||
|
||
const cert = pki.createCertificate(); | ||
cert.publicKey = keyPair.publicKey; | ||
cert.serialNumber = crypto.randomUUID().replace(/-/g, ""); | ||
|
||
cert.validity.notBefore = new Date(); | ||
cert.validity.notBefore.setDate(cert.validity.notBefore.getDate() - 1); | ||
cert.validity.notAfter = new Date(); | ||
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1); | ||
|
||
cert.setSubject([{ name: "commonName", value: options.commonName }]); | ||
cert.setExtensions([{ name: "basicConstraints", cA: true }]); | ||
|
||
cert.setIssuer(cert.subject.attributes); | ||
cert.sign(keyPair.privateKey, forge.md.sha256.create()); | ||
|
||
return { | ||
ca: { | ||
key: pki.privateKeyToPem(keyPair.privateKey), | ||
cert: pki.certificateToPem(cert), | ||
}, | ||
fingerprint: forge.util.encode64( | ||
pki.getPublicKeyFingerprint(keyPair.publicKey, { | ||
type: "SubjectPublicKeyInfo", | ||
md: forge.md.sha256.create(), | ||
encoding: "binary", | ||
}), | ||
), | ||
}; | ||
}; | ||
|
||
const caToBuffer = (ca) => { | ||
return { | ||
key: Buffer.from(ca.key), | ||
cert: Buffer.from(ca.cert), | ||
}; | ||
}; | ||
|
||
const API_PORT = process.env.API_PORT || 4578; | ||
|
||
const app = express(); | ||
app.get("/v1/foo", (req, res) => { | ||
res.send("bar"); | ||
}); | ||
|
||
test("requestInitExt", async () => { | ||
const cert = await genCACert(); | ||
const buffers = caToBuffer(cert.ca); | ||
const options = {}; | ||
options.key = buffers.key; | ||
options.cert = buffers.cert; | ||
const httpsServer = https.createServer(options, app); | ||
httpsServer.listen(4578); | ||
const dispatcher = new Agent({ | ||
connect: { | ||
rejectUnauthorized: false, | ||
}, | ||
}); | ||
const client = createClient({ baseUrl: `https://localhost:${API_PORT}`, requestInitExt: { dispatcher } }); | ||
const fetchResponse = await client.GET("/v1/foo", { parseAs: "text" }); | ||
httpsServer.closeAllConnections(); | ||
httpsServer.close(); | ||
expect(fetchResponse.response.ok).toBe(true); | ||
}); |
Oops, something went wrong.