Skip to content

Commit

Permalink
fix: do not use a body serializer on text/plain sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Jan 21, 2025
1 parent 548e5d8 commit 4784727
Show file tree
Hide file tree
Showing 37 changed files with 486 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-donkeys-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: do not use a body serializer on text/plain sdks
8 changes: 8 additions & 0 deletions .changeset/six-horses-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@hey-api/client-axios': patch
'@hey-api/client-fetch': patch
'@hey-api/client-nuxt': patch
'@hey-api/openapi-ts': patch
---

fix: add null to valid bodySerializer types
24 changes: 24 additions & 0 deletions examples/openapi-ts-sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions examples/openapi-ts-sample/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<!-- <link rel="icon" type="image/svg+xml" href="/vite.svg" /> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hey API + Fetch API Demo</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/openapi-ts-sample/openapi-ts.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from '@hey-api/openapi-ts';

export default defineConfig({
client: '@hey-api/client-fetch',
input:
'../../packages/openapi-ts/test/spec/2.0.x/body-response-text-plain.yaml',
output: {
format: 'prettier',
lint: 'eslint',
path: './src/client',
},
plugins: [
'@hey-api/schemas',
'@hey-api/sdk',
{
enums: 'javascript',
name: '@hey-api/typescript',
},
],
});
39 changes: 39 additions & 0 deletions examples/openapi-ts-sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@example/openapi-ts-sample",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"build": "tsc && vite build",
"dev": "vite",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"openapi-ts": "openapi-ts",
"preview": "vite preview",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@hey-api/client-fetch": "workspace:*",
"@radix-ui/react-form": "0.1.1",
"@radix-ui/react-icons": "1.3.2",
"@radix-ui/themes": "3.1.6",
"react": "19.0.0",
"react-dom": "19.0.0"
},
"devDependencies": {
"@hey-api/openapi-ts": "workspace:*",
"@types/react": "19.0.1",
"@types/react-dom": "19.0.1",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.15.0",
"@vitejs/plugin-react": "4.3.1",
"autoprefixer": "10.4.19",
"eslint": "9.17.0",
"eslint-plugin-react-hooks": "4.6.2",
"eslint-plugin-react-refresh": "0.4.7",
"postcss": "8.4.39",
"prettier": "3.4.2",
"tailwindcss": "3.4.4",
"typescript": "5.5.3",
"vite": "6.0.7"
}
}
6 changes: 6 additions & 0 deletions examples/openapi-ts-sample/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
autoprefixer: {},
tailwindcss: {},
},
};
3 changes: 3 additions & 0 deletions examples/openapi-ts-sample/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
40 changes: 40 additions & 0 deletions examples/openapi-ts-sample/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import './App.css';

import {
Box,
Button,
Container,
Flex,
Heading,
Section,
} from '@radix-ui/themes';

import { postFoo } from './client/sdk.gen';

function App() {
const onClick = async () => {
postFoo({
body: 'foo',
});
};

return (
<Box
style={{ background: 'var(--gray-a2)', borderRadius: 'var(--radius-3)' }}
>
<Container size="1">
<Section size="1" />
<Flex align="center">
<Heading>sample for internal testing</Heading>
</Flex>
<Section size="1" />
<Flex direction="column" gapY="2">
<Button onClick={onClick}>Click me</Button>
</Flex>
<Section size="1" />
</Container>
</Box>
);
}

export default App;
3 changes: 3 additions & 0 deletions examples/openapi-ts-sample/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './sdk.gen';
export * from './types.gen';
24 changes: 24 additions & 0 deletions examples/openapi-ts-sample/src/client/sdk.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is auto-generated by @hey-api/openapi-ts

import {
createClient,
createConfig,
type Options,
} from '@hey-api/client-fetch';

import type { PostFooData, PostFooResponse } from './types.gen';

export const client = createClient(createConfig());

export const postFoo = <ThrowOnError extends boolean = false>(
options: Options<PostFooData, ThrowOnError>,
) =>
(options?.client ?? client).post<PostFooResponse, unknown, ThrowOnError>({
bodySerializer: null,
url: '/foo',
...options,
headers: {
'Content-Type': 'text/plain',
...options?.headers,
},
});
17 changes: 17 additions & 0 deletions examples/openapi-ts-sample/src/client/types.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file is auto-generated by @hey-api/openapi-ts

export type PostFooData = {
body: string;
path?: never;
query?: never;
url: '/foo';
};

export type PostFooResponses = {
/**
* OK
*/
200: string;
};

export type PostFooResponse = PostFooResponses[keyof PostFooResponses];
26 changes: 26 additions & 0 deletions examples/openapi-ts-sample/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import '@radix-ui/themes/styles.css';

import { Theme } from '@radix-ui/themes';
import React from 'react';
import ReactDOM from 'react-dom/client';

import App from './App.tsx';
import { client } from './client/sdk.gen';

// configure internal service client
client.setConfig({
// set default base url for requests
baseUrl: 'https://petstore3.swagger.io/api/v3',
// set default headers for requests
headers: {
Authorization: 'Bearer <token_from_service_client>',
},
});

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<Theme appearance="dark">
<App />
</Theme>
</React.StrictMode>,
);
1 change: 1 addition & 0 deletions examples/openapi-ts-sample/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
8 changes: 8 additions & 0 deletions examples/openapi-ts-sample/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{html,js,ts,jsx,tsx}'],
plugins: [],
theme: {
extend: {},
},
};
25 changes: 25 additions & 0 deletions examples/openapi-ts-sample/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
11 changes: 11 additions & 0 deletions examples/openapi-ts-sample/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["vite.config.ts"]
}
7 changes: 7 additions & 0 deletions examples/openapi-ts-sample/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
});
2 changes: 1 addition & 1 deletion packages/client-axios/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface Config<ThrowOnError extends boolean = boolean>
* A function for serializing request body parameter. By default,
* {@link JSON.stringify()} will be used.
*/
bodySerializer?: BodySerializer;
bodySerializer?: BodySerializer | null;
/**
* An object containing any HTTP headers that you want to pre-populate your
* `Headers` object with.
Expand Down
2 changes: 1 addition & 1 deletion packages/client-fetch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface Config<ThrowOnError extends boolean = boolean>
* A function for serializing request body parameter. By default,
* {@link JSON.stringify()} will be used.
*/
bodySerializer?: BodySerializer;
bodySerializer?: BodySerializer | null;
/**
* Fetch API implementation. You can use this option to provide a custom
* fetch instance.
Expand Down
2 changes: 1 addition & 1 deletion packages/client-nuxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface Config
* A function for serializing request body parameter. By default,
* {@link JSON.stringify()} will be used.
*/
bodySerializer?: BodySerializer;
bodySerializer?: BodySerializer | null;
/**
* An object containing any HTTP headers that you want to pre-populate your
* `Headers` object with.
Expand Down
Loading

0 comments on commit 4784727

Please sign in to comment.