-
-
Notifications
You must be signed in to change notification settings - Fork 529
Fix 0.10.0 errors #1719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix 0.10.0 errors #1719
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-fetch": patch | ||
--- | ||
|
||
Remove nanoid from dependencies |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-fetch": patch | ||
--- | ||
|
||
Fix "failed to execute fetch on Window" error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-fetch": patch | ||
--- | ||
|
||
Revert customFetch API back to `fetch(input: Request)` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,15 +54,17 @@ | |
"build:cjs": "esbuild --bundle src/index.js --format=cjs --outfile=dist/cjs/index.cjs && cp dist/index.d.ts dist/cjs/index.d.cts", | ||
"format": "biome format . --write", | ||
"lint": "biome check .", | ||
"generate-types": "openapi-typescript test/fixtures/api.yaml -o test/fixtures/api.d.ts", | ||
"generate-types": "openapi-typescript -c test/redocly.yaml", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use fancy new 7.x ability to generate multiple schemas with one config file |
||
"pretest": "pnpm run generate-types", | ||
"test": "pnpm run \"/^test:/\"", | ||
"test:js": "vitest run", | ||
"test:ts": "tsc --noEmit", | ||
"test-e2e": "playwright test", | ||
"e2e-vite-build": "vite build test/fixtures/e2e", | ||
"e2e-vite-start": "vite preview test/fixtures/e2e", | ||
"version": "pnpm run prepare && pnpm run build" | ||
}, | ||
"dependencies": { | ||
"nanoid": "^5.0.7", | ||
"openapi-typescript-helpers": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
|
@@ -76,6 +78,6 @@ | |
"openapi-typescript-fetch": "^2.0.0", | ||
"superagent": "^9.0.2", | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.6.0" | ||
"vite": "^5.3.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { defineConfig, devices } from "@playwright/test"; | ||
|
||
const PORT = Number.parseInt(process.env.PORT || 4173 || "", 10); | ||
|
||
export default defineConfig({ | ||
testMatch: "test/**/*.e2e.ts", | ||
webServer: { | ||
command: "pnpm run e2e-vite-build && pnpm run e2e-vite-start", | ||
port: PORT, | ||
}, | ||
use: { | ||
baseURL: `http://localhost:${PORT}`, | ||
}, | ||
projects: [ | ||
{ | ||
name: "chrome", | ||
use: { ...devices["Desktop Chrome"] }, | ||
}, | ||
{ | ||
name: "firefox", | ||
use: { ...devices["Desktop Firefox"] }, | ||
}, | ||
{ | ||
name: "webkit", | ||
use: { ...devices["Desktop Safari"] }, | ||
}, | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ export interface ClientOptions extends Omit<RequestInit, "headers"> { | |
/** set the common root URL for all API requests */ | ||
baseUrl?: string; | ||
/** custom fetch (defaults to globalThis.fetch) */ | ||
fetch?: (input: string, init?: RequestInit) => Promise<Response>; | ||
fetch?: (input: Request) => Promise<Response>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is technically a breaking change, but since it was a breaking change from |
||
/** global querySerializer */ | ||
querySerializer?: QuerySerializer<unknown> | QuerySerializerOptions; | ||
/** global bodySerializer */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { nanoid } from "nanoid"; | ||
|
||
// settings & const | ||
const DEFAULT_HEADERS = { | ||
"Content-Type": "application/json", | ||
|
@@ -21,6 +19,14 @@ class CustomRequest extends Request { | |
} | ||
} | ||
|
||
/** | ||
* Returns a cheap, non-cryptographically-secure random ID | ||
* Courtesy of @imranbarbhuiya (https://github.com/imranbarbhuiya) | ||
*/ | ||
export function randomID() { | ||
return Math.random().toString(36).slice(2, 11); | ||
} | ||
|
||
/** | ||
* Create an openapi-fetch client. | ||
* @type {import("./index.js").default} | ||
|
@@ -84,56 +90,63 @@ export default function createClient(clientOptions) { | |
requestInit.headers.delete("Content-Type"); | ||
} | ||
|
||
const id = nanoid(); | ||
let id; | ||
let options; | ||
let request = new CustomRequest(createFinalURL(schemaPath, { baseUrl, params, querySerializer }), requestInit); | ||
|
||
// middleware (request) | ||
const options = Object.freeze({ | ||
baseUrl, | ||
fetch, | ||
parseAs, | ||
querySerializer, | ||
bodySerializer, | ||
}); | ||
for (const m of middlewares) { | ||
if (m && typeof m === "object" && typeof m.onRequest === "function") { | ||
const result = await m.onRequest({ | ||
request, | ||
schemaPath, | ||
params, | ||
options, | ||
id, | ||
}); | ||
if (result) { | ||
if (!(result instanceof Request)) { | ||
throw new Error("onRequest: must return new Request() when modifying the request"); | ||
if (middlewares.length) { | ||
id = randomID(); | ||
|
||
// middleware (request) | ||
options = Object.freeze({ | ||
baseUrl, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Micro-optimization: don’t run certain codepaths if no middleware loaded |
||
fetch, | ||
parseAs, | ||
querySerializer, | ||
bodySerializer, | ||
}); | ||
for (const m of middlewares) { | ||
if (m && typeof m === "object" && typeof m.onRequest === "function") { | ||
const result = await m.onRequest({ | ||
request, | ||
schemaPath, | ||
params, | ||
options, | ||
id, | ||
}); | ||
if (result) { | ||
if (!(result instanceof Request)) { | ||
throw new Error("onRequest: must return new Request() when modifying the request"); | ||
} | ||
request = result; | ||
} | ||
request = result; | ||
} | ||
} | ||
} | ||
|
||
// fetch! | ||
let response = await fetch(request.url, request); | ||
let response = await fetch(request); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the central error fix— |
||
// middleware (response) | ||
// execute in reverse-array order (first priority gets last transform) | ||
for (let i = middlewares.length - 1; i >= 0; i--) { | ||
const m = middlewares[i]; | ||
if (m && typeof m === "object" && typeof m.onResponse === "function") { | ||
const result = await m.onResponse({ | ||
request, | ||
response, | ||
schemaPath, | ||
params, | ||
options, | ||
id, | ||
}); | ||
if (result) { | ||
if (!(result instanceof Response)) { | ||
throw new Error("onResponse: must return new Response() when modifying the response"); | ||
if (middlewares.length) { | ||
for (let i = middlewares.length - 1; i >= 0; i--) { | ||
const m = middlewares[i]; | ||
if (m && typeof m === "object" && typeof m.onResponse === "function") { | ||
const result = await m.onResponse({ | ||
request, | ||
response, | ||
schemaPath, | ||
params, | ||
options, | ||
id, | ||
}); | ||
if (result) { | ||
if (!(result instanceof Response)) { | ||
throw new Error("onResponse: must return new Response() when modifying the response"); | ||
} | ||
response = result; | ||
} | ||
response = result; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"status": "passed", | ||
"failedTests": [] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import createClient from "../../../src"; | ||
import type { paths } from "./e2e.d.ts"; | ||
|
||
const client = createClient<paths>({ | ||
baseUrl: "/api/v1", | ||
}); | ||
|
||
/** | ||
* Test 1: GET /api/v1/get | ||
*/ | ||
async function testGet() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New tests that get loaded in a true browser environment. Not comprehensive, but we at least have:
|
||
const { data } = await client.GET("/get"); | ||
if (!data) { | ||
throw new Error("/get: No data"); | ||
} | ||
} | ||
|
||
/** | ||
* Test 2: POST /api/v1/post | ||
*/ | ||
async function testPost() { | ||
const { data } = await client.POST("/post", { body: { message: "POST" } }); | ||
if (!data) { | ||
throw new Error("/post: No data"); | ||
} | ||
} | ||
|
||
/** | ||
* Test 3: PUT /api/v1/multi-form | ||
*/ | ||
async function testMultiForm() { | ||
const { data } = await client.POST("/multi-form", { | ||
body: { | ||
message: "Form", | ||
file: new File(["Hello, World!"], "hello.txt") as unknown as string, | ||
}, | ||
}); | ||
if (!data) { | ||
throw new Error("/multi-form: No data"); | ||
} | ||
} | ||
|
||
// run all tests immediately on load | ||
(async () => { | ||
await Promise.all([testGet(), testPost(), testMultiForm()]); | ||
|
||
// add element Playwright is waiting for | ||
const div = document.createElement("div"); | ||
div.setAttribute("data-status", "success"); | ||
div.innerHTML = "Success"; | ||
document.body.appendChild(div); | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"test"
. We don’t need to run browser tests multiple times in different OSs and different Node versions (Playwright is the environment). We only need to run this ONCE in a single environment (Ubuntu, Node latest because that’s the cheapest/fastest).