From 46e10eac3d52195da11b3c6a1061cab7acdede06 Mon Sep 17 00:00:00 2001 From: Luc van Kampen Date: Sat, 26 Jul 2025 10:24:14 +0000 Subject: [PATCH] Use openapi-hooks in basic example --- .gitignore | 1 + examples/basic/package.json | 3 +- examples/basic/src/index.ts | 4 ++- examples/basic/src/schema.gen.ts | 53 +++++++++++++++++++++++++++++ examples/basic/tsconfig.json | 13 +++++++ packages/openapi-hooks/src/index.ts | 32 +++++++++-------- 6 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 examples/basic/src/schema.gen.ts create mode 100644 examples/basic/tsconfig.json diff --git a/.gitignore b/.gitignore index c2658d7..ccacd4d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +examples/**/src/*.js diff --git a/examples/basic/package.json b/examples/basic/package.json index 74d9cf1..0fc8ce8 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -3,9 +3,10 @@ "version": "0.0.1", "description": "", "scripts": { - "dev": "tsc ./src/index.ts", + "dev": "tsc -p tsconfig.json", "api-schema": "openapi-typescript http://localhost:3000/openapi.json -t -o ./src/schema.gen.ts" }, + "type": "module", "keywords": [], "author": "V3X Labs", "license": "GPL-3.0-only", diff --git a/examples/basic/src/index.ts b/examples/basic/src/index.ts index a2946c6..79dc9e9 100644 --- a/examples/basic/src/index.ts +++ b/examples/basic/src/index.ts @@ -1,7 +1,9 @@ import { createFetch } from "openapi-hooks"; import { paths } from "./schema.gen"; -const fetching = createFetch(); +// Configure the fetcher with an explicit baseUrl so it works in Node.js as well +// as the browser. +const fetching = createFetch({ baseUrl: "http://localhost:3000" }); const response = await fetching('/site/{site_id}', 'get', { path: { diff --git a/examples/basic/src/schema.gen.ts b/examples/basic/src/schema.gen.ts new file mode 100644 index 0000000..83b3dc8 --- /dev/null +++ b/examples/basic/src/schema.gen.ts @@ -0,0 +1,53 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + +export interface paths { + "/site/{site_id}": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + get: { + parameters: { + query?: never; + header?: never; + path: { + site_id: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + }; + }; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; +} +export type webhooks = Record; +export interface components { + schemas: never; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} +export type $defs = Record; +export type operations = Record; diff --git a/examples/basic/tsconfig.json b/examples/basic/tsconfig.json new file mode 100644 index 0000000..a4476ea --- /dev/null +++ b/examples/basic/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es2022", + "module": "es2022", + "moduleResolution": "node", + "lib": ["es2022", "dom"], + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + }, + "include": ["src"] +} diff --git a/packages/openapi-hooks/src/index.ts b/packages/openapi-hooks/src/index.ts index 4f394ba..ad08d4c 100644 --- a/packages/openapi-hooks/src/index.ts +++ b/packages/openapi-hooks/src/index.ts @@ -1,9 +1,3 @@ -type Paths = { - [key: string]: { - [key: string]: any; - }; -} - type HTTPMethod = | 'get' | 'put' @@ -14,10 +8,14 @@ type HTTPMethod = | 'patch' | 'trace'; -export type PathMethods = { - [TMethod in HTTPMethod]: paths[TPath][TMethod] extends undefined - ? never - : TMethod; +export type PathMethods = { + [TMethod in HTTPMethod]: TPaths[TPath] extends Record + ? TMethod extends keyof TPaths[TPath] + ? TPaths[TPath][TMethod] extends undefined + ? never + : TMethod + : never + : never; }[HTTPMethod]; type AnyRequestBody = { @@ -162,7 +160,7 @@ const decodeResponse = async (response, responseContentType) => { } }; -export const createFetch = (options?: OpenApiHookOptions) => { +export const createFetch = (options?: OpenApiHookOptions) => { const { baseUrl = window.location.toString(), headers: defaultHeaders, onError } = options ?? {}; /** @@ -249,13 +247,17 @@ export const createFetch = (options?: OpenApiHookOptions) = * @throws {Error} When an unsupported content type is encountered */ return async < - TPath extends keyof paths, - TMethod extends PathMethods, + TPath extends keyof TPaths, + TMethod extends PathMethods, TOptions extends TRoute['parameters'] & ApiRequestBody & { fetchOptions?: RequestInit; }, - TRoute extends AnyRoute = paths[TPath][TMethod] extends AnyRoute - ? paths[TPath][TMethod] + TRoute extends AnyRoute = TPaths[TPath] extends Record + ? TMethod extends keyof TPaths[TPath] + ? TPaths[TPath][TMethod] extends AnyRoute + ? TPaths[TPath][TMethod] + : never + : never : never, >( path: TPath,