Skip to content

Add Algolia search script, update openapi-fetch #1108

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

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/src/components/Header/Search.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 0 additions & 2 deletions docs/src/components/LeftSidebar/LeftSidebar.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
import { SIDEBAR } from "../../consts.js";

type Props = {
currentPage: string;
};
Expand Down
14 changes: 14 additions & 0 deletions docs/src/layouts/MainLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,5 +143,14 @@ const nextLink: Record<string, Link | undefined> = {
<RightSidebar headings={headings} githubEditUrl={githubEditUrl} />
</aside>
</main>
<script src="https://cdn.jsdelivr.net/npm/@docsearch/js@3"></script>
<script is:inline type="text/javascript">
docsearch({
appId: import.meta.env.VITE_ALGOLIA_APP_ID,
apiKey: import.meta.env.VITE_ALGOLIA_SEARCH_KEY,
indexName: import.meta.env.VITE_ALGOLIA_INDEX_NAME,
container: "#grid-main",
});
</script>
</body>
</html>
12 changes: 12 additions & 0 deletions packages/openapi-fetch/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 36 additions & 0 deletions packages/openapi-fetch/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<paths>();

// 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<paths>();

// 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()", () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/openapi-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ export type FilterKeys<Obj, Matchers> = { [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> = O extends { parameters: any } ? { params: NonNullable<O["parameters"]> } : BaseParams;
export type RequestBodyObj<O> = O extends { requestBody: any } ? O["requestBody"] : never;
export type RequestBodyObj<O> = O extends { requestBody?: any } ? O["requestBody"] : never;
export type RequestBodyContent<O> = undefined extends RequestBodyObj<O> ? FilterKeys<NonNullable<RequestBodyObj<O>>, "content"> | undefined : FilterKeys<RequestBodyObj<O>, "content">;
export type RequestBodyJSON<O> = FilterKeys<RequestBodyContent<O>, JSONLike> extends never ? FilterKeys<NonNullable<RequestBodyContent<O>>, JSONLike> | undefined : FilterKeys<RequestBodyContent<O>, JSONLike>;
export type RequestBody<O> = undefined extends RequestBodyJSON<O> ? { body?: RequestBodyJSON<O> } : { body: RequestBodyJSON<O> };
export type QuerySerializer<O> = (query: O extends { parameters: { query: any } } ? O["parameters"]["query"] : Record<string, unknown>) => string;
export type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, "body"> & { querySerializer?: QuerySerializer<T> };
export type RequestOptions<T> = Params<T> & RequestBody<T> & { querySerializer?: QuerySerializer<T> };
export type Success<O> = FilterKeys<FilterKeys<O, OkStatus>, "content">;
export type Error<O> = FilterKeys<FilterKeys<O, ErrorStatus>, "content">;

// fetch types
export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, "body">;
export type FetchResponse<T> =
| { data: T extends { responses: any } ? NonNullable<FilterKeys<Success<T["responses"]>, JSONLike>> : unknown; error?: never; response: Response }
| { data?: never; error: T extends { responses: any } ? NonNullable<FilterKeys<Error<T["responses"]>, JSONLike>> : unknown; response: Response };
Expand Down
44 changes: 42 additions & 2 deletions packages/openapi-fetch/test/v1.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
openapi:
version: '3.1'
openapi: 3.1
info:
title: Test Specification
version: '1.0'
paths:
/comment:
put:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down