Skip to content

Commit

Permalink
Update openapi-fetch, add docs sitemap (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed May 11, 2023
1 parent 8c1a4a8 commit c94653a
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 41 deletions.
5 changes: 3 additions & 2 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { defineConfig } from "astro/config";
import preact from "@astrojs/preact";
import react from "@astrojs/react";
import sitemap from "@astrojs/sitemap";

// https://astro.build/config
export default defineConfig({
integrations: [preact(), react()],
site: `https://openapi-typescript.pages.dev`,
integrations: [preact(), react(), sitemap()],
site: `https://openapi-ts.pages.dev`,
vite: {
define: {
"import.meta.env.VITE_ALGOLIA_APP_ID": JSON.stringify(process.env.ALGOLIA_APP_ID ?? ""),
Expand Down
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"sass": "^1.62.1"
},
"devDependencies": {
"@astrojs/sitemap": "^1.3.1",
"@types/node": "^20.1.1",
"html-escaper": "^3.0.3",
"typescript": "^5.0.4"
Expand Down
16 changes: 1 addition & 15 deletions docs/src/components/HeadCommon.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
import "../styles/app.scss";
---

<!-- Global Metadata -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="sitemap" href="/sitemap.xml" />

<!-- Scrollable a11y code helper -->
<link rel="sitemap" href="/sitemap-index.xml" />
<script src="/make-scrollable-code-focusable.js" is:inline></script>

<!-- This is intentionally inlined to avoid FOUC -->
<script is:inline>
const root = document.documentElement;
const theme = localStorage.getItem("theme");
Expand All @@ -22,12 +17,3 @@ import "../styles/app.scss";
root.classList.remove("theme-dark");
}
</script>

<!-- Global site tag (gtag.js) - Google Analytics -->
<!-- <script async src="https://www.googletagmanager.com/gtag/js?id=G-TEL60V1WM9" is:inline></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-TEL60V1WM9');
</script> -->
12 changes: 0 additions & 12 deletions docs/src/components/HeadSEO.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ const canonicalImageSrc = new URL(imageSrc, Astro.site);
const imageAlt = image?.alt ?? OPEN_GRAPH.image.alt;
---

<!-- Page Metadata -->
<link rel="canonical" href={canonicalUrl} />

<!-- OpenGraph Tags -->
<meta property="og:title" content={formattedContentTitle} />
<meta property="og:type" content="article" />
<meta property="og:url" content={canonicalUrl} />
Expand All @@ -23,18 +20,9 @@ const imageAlt = image?.alt ?? OPEN_GRAPH.image.alt;
<meta property="og:image:alt" content={imageAlt} />
<meta name="description" property="og:description" content={description ?? SITE.description} />
<meta property="og:site_name" content={SITE.title} />

<!-- Twitter Tags -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content={OPEN_GRAPH.twitter} />
<meta name="twitter:title" content={formattedContentTitle} />
<meta name="twitter:description" content={description ?? SITE.description} />
<meta name="twitter:image" content={canonicalImageSrc} />
<meta name="twitter:image:alt" content={imageAlt} />

<!--
TODO: Add json+ld data, maybe https://schema.org/APIReference makes sense?
Docs: https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data
https://www.npmjs.com/package/schema-dts seems like a great resource for implementing this.
Even better, there's a React component that integrates with `schema-dts`: https://github.com/google/react-schemaorg
-->
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
7 changes: 6 additions & 1 deletion 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 @@ -131,7 +136,7 @@ const nextLink: Record<string, Link | undefined> = {
<slot />
{currentPage === "/about" && <Contributors usernames={OPENAPI_TS_CONTRIBUTORS} />}
{currentPage === "/openapi-fetch/about" && <Contributors usernames={OPENAPI_FETCH_CONTRIBUTORS} />}
{nextLink[currentPage] && <NextLink href={nextLink[currentPage].url}>{nextLink[currentPage].text}</NextLink>}
{nextLink[currentPage] && <NextLink href={nextLink[currentPage]!.url}>{nextLink[currentPage]!.text}</NextLink>}
</PageContent>
</div>
<aside id="grid-right" class="grid-sidebar" title="Table of Contents">
Expand Down
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
31 changes: 31 additions & 0 deletions packages/openapi-fetch/test/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ export interface paths {
};
};
};
"/post/optional": {
post: {
requestBody: components["requestBodies"]["CreatePostOptional"];
responses: {
201: components["responses"]["CreatePost"];
500: components["responses"]["Error"];
};
};
};
"/post/optional/inline": {
post: {
requestBody?: {
content: {
"application/json": components["schemas"]["Post"];
};
};
responses: {
201: components["responses"]["CreatePost"];
500: components["responses"]["Error"];
};
};
};
"/posts": {
get: {
responses: {
Expand Down Expand Up @@ -271,6 +293,15 @@ export interface components {
};
};
};
CreatePostOptional?: {
content: {
"application/json": {
title: string;
body: string;
publish_date: number;
};
};
};
CreateTag?: {
content: {
"application/json": {
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
Loading

0 comments on commit c94653a

Please sign in to comment.