diff --git a/docs/src/components/Header/Search.tsx b/docs/src/components/Header/Search.tsx index 8e77b3185..5f98246c5 100644 --- a/docs/src/components/Header/Search.tsx +++ b/docs/src/components/Header/Search.tsx @@ -1,10 +1,9 @@ /** @jsxImportSource react */ import { useState, useCallback, useRef } from "react"; import "@docsearch/css"; -import "./Search.css"; - -import { createPortal } from "react-dom"; import * as docSearchReact from "@docsearch/react"; +import { createPortal } from "react-dom"; +import "./Search.css"; /** FIXME: This is still kinda nasty, but DocSearch is not ESM ready. */ const DocSearchModal = docSearchReact.DocSearchModal || (docSearchReact as any).default.DocSearchModal; diff --git a/docs/src/components/LeftSidebar/LeftSidebar.astro b/docs/src/components/LeftSidebar/LeftSidebar.astro index 4ee049086..a8dd5c662 100644 --- a/docs/src/components/LeftSidebar/LeftSidebar.astro +++ b/docs/src/components/LeftSidebar/LeftSidebar.astro @@ -1,6 +1,4 @@ --- -import { SIDEBAR } from "../../consts.js"; - type Props = { currentPage: string; }; diff --git a/docs/src/layouts/MainLayout.astro b/docs/src/layouts/MainLayout.astro index d9407fb06..9347a4198 100644 --- a/docs/src/layouts/MainLayout.astro +++ b/docs/src/layouts/MainLayout.astro @@ -15,6 +15,11 @@ type Props = CollectionEntry<"docs">["data"] & { headings: MarkdownHeading[]; }; +interface Link { + url: string; + text: string; +} + const { headings, ...data } = Astro.props; const canonicalURL = new URL(Astro.url.pathname, Astro.site); const currentPage = Astro.url.pathname; @@ -138,5 +143,14 @@ const nextLink: Record = { + + diff --git a/packages/openapi-fetch/CHANGELOG.md b/packages/openapi-fetch/CHANGELOG.md index f0e08f9ae..e72b9371d 100644 --- a/packages/openapi-fetch/CHANGELOG.md +++ b/packages/openapi-fetch/CHANGELOG.md @@ -1,5 +1,17 @@ # openapi-fetch +## 0.1.4 + +### Patch Changes + +- 63ebe48: Fix request body type when optional (#48) + +## 0.1.3 + +### Patch Changes + +- 8c01480: Fix querySerializer signature + ## 0.1.2 ### Patch Changes diff --git a/packages/openapi-fetch/src/index.test.ts b/packages/openapi-fetch/src/index.test.ts index 08107b456..4f2d19a54 100644 --- a/packages/openapi-fetch/src/index.test.ts +++ b/packages/openapi-fetch/src/index.test.ts @@ -351,6 +351,42 @@ describe("post()", () => { // assert error is empty expect(error).toBe(undefined); }); + + it("request body type when optional", async () => { + fetchMocker.mockResponse(() => ({ status: 201, body: "{}" })); + const client = createClient(); + + // expect error on wrong body type + // @ts-expect-error + await client.post("/post/optional", { body: { error: true } }); + + // (no error) + await client.post("/post/optional", { + body: { + title: "", + publish_date: 3, + body: "", + }, + }); + }); + + it("request body type when optional inline", async () => { + fetchMocker.mockResponse(() => ({ status: 201, body: "{}" })); + const client = createClient(); + + // expect error on wrong body type + // @ts-expect-error + await client.post("/post/optional/inline", { body: { error: true } }); + + // (no error) + await client.post("/post/optional/inline", { + body: { + title: "", + publish_date: 3, + body: "", + }, + }); + }); }); describe("delete()", () => { diff --git a/packages/openapi-fetch/src/index.ts b/packages/openapi-fetch/src/index.ts index e2f4b54b8..1a3bf9218 100644 --- a/packages/openapi-fetch/src/index.ts +++ b/packages/openapi-fetch/src/index.ts @@ -34,16 +34,19 @@ export type FilterKeys = { [K in keyof Obj]: K extends Matchers ? /** handle "application/json", "application/vnd.api+json", "appliacation/json;charset=utf-8" and more */ export type JSONLike = `${string}json${string}`; -// fetch types +// general purpose types export type Params = O extends { parameters: any } ? { params: NonNullable } : BaseParams; -export type RequestBodyObj = O extends { requestBody: any } ? O["requestBody"] : never; +export type RequestBodyObj = O extends { requestBody?: any } ? O["requestBody"] : never; export type RequestBodyContent = undefined extends RequestBodyObj ? FilterKeys>, "content"> | undefined : FilterKeys, "content">; export type RequestBodyJSON = FilterKeys, JSONLike> extends never ? FilterKeys>, JSONLike> | undefined : FilterKeys, JSONLike>; export type RequestBody = undefined extends RequestBodyJSON ? { body?: RequestBodyJSON } : { body: RequestBodyJSON }; export type QuerySerializer = (query: O extends { parameters: { query: any } } ? O["parameters"]["query"] : Record) => string; -export type FetchOptions = Params & RequestBody & Omit & { querySerializer?: QuerySerializer }; +export type RequestOptions = Params & RequestBody & { querySerializer?: QuerySerializer }; export type Success = FilterKeys, "content">; export type Error = FilterKeys, "content">; + +// fetch types +export type FetchOptions = RequestOptions & Omit; export type FetchResponse = | { data: T extends { responses: any } ? NonNullable, JSONLike>> : unknown; error?: never; response: Response } | { data?: never; error: T extends { responses: any } ? NonNullable, JSONLike>> : unknown; response: Response }; diff --git a/packages/openapi-fetch/test/v1.yaml b/packages/openapi-fetch/test/v1.yaml index ec8bd58b1..385ff4761 100644 --- a/packages/openapi-fetch/test/v1.yaml +++ b/packages/openapi-fetch/test/v1.yaml @@ -1,5 +1,7 @@ -openapi: - version: '3.1' +openapi: 3.1 +info: + title: Test Specification + version: '1.0' paths: /comment: put: @@ -19,6 +21,27 @@ paths: $ref: '#/components/responses/CreatePost' 500: $ref: '#/components/responses/Error' + /post/optional: + post: + requestBody: + $ref: '#/components/requestBodies/CreatePostOptional' + responses: + 201: + $ref: '#/components/responses/CreatePost' + 500: + $ref: '#/components/responses/Error' + /post/optional/inline: + post: + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Post' + responses: + 201: + $ref: '#/components/responses/CreatePost' + 500: + $ref: '#/components/responses/Error' /posts: get: responses: @@ -214,6 +237,23 @@ components: - title - body - publish_date + CreatePostOptional: + required: false + content: + application/json: + schema: + type: object + properties: + title: + type: string + body: + type: string + publish_date: + type: number + required: + - title + - body + - publish_date CreateTag: content: application/json: