Skip to content

Commit

Permalink
Merge pull request #1600 from hey-api/refactor/client-bundle-index
Browse files Browse the repository at this point in the history
fix: bundle clients from compiled index file
  • Loading branch information
mrlubos authored Jan 21, 2025
2 parents 7eebbef + 0432418 commit 1b73773
Show file tree
Hide file tree
Showing 61 changed files with 2,046 additions and 4,735 deletions.
8 changes: 8 additions & 0 deletions .changeset/eight-squids-crash.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: bundle clients from compiled index file
3 changes: 1 addition & 2 deletions examples/openapi-ts-sample/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ 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',
input: '../../packages/openapi-ts/test/spec/3.1.x/body-nested-array.yaml',
output: {
format: 'prettier',
lint: 'eslint',
Expand Down
4 changes: 3 additions & 1 deletion examples/openapi-ts-sample/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { postFoo } from './client/sdk.gen';
function App() {
const onClick = async () => {
postFoo({
body: 'foo',
body: {
foo: [[1, 2]],
},
});
};

Expand Down
9 changes: 5 additions & 4 deletions examples/openapi-ts-sample/src/client/sdk.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
import {
createClient,
createConfig,
formDataBodySerializer,
type Options,
} from '@hey-api/client-fetch';

import type { PostFooData, PostFooResponse } from './types.gen';
import type { PostFooData } 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,
(options?.client ?? client).post<unknown, unknown, ThrowOnError>({
...formDataBodySerializer,
url: '/foo',
...options,
headers: {
'Content-Type': 'text/plain',
'Content-Type': null,
...options?.headers,
},
});
8 changes: 4 additions & 4 deletions examples/openapi-ts-sample/src/client/types.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This file is auto-generated by @hey-api/openapi-ts

export type PostFooData = {
body: string;
body: {
foo: Array<Array<number>>;
};
path?: never;
query?: never;
url: '/foo';
Expand All @@ -11,7 +13,5 @@ export type PostFooResponses = {
/**
* OK
*/
200: string;
200: unknown;
};

export type PostFooResponse = PostFooResponses[keyof PostFooResponses];
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"husky": "9.1.7",
"lint-staged": "15.3.0",
"prettier": "3.4.2",
"rollup": "4.31.0",
"rollup-plugin-dts": "6.1.1",
"tsup": "8.3.5",
"typescript": "5.5.3",
"typescript-eslint": "8.19.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/client-axios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"src"
],
"scripts": {
"build": "tsup && pnpm check-exports",
"build": "tsup && rollup -c && pnpm check-exports",
"check-exports": "attw --pack .",
"dev": "tsup --watch",
"prepublishOnly": "pnpm build",
Expand All @@ -68,6 +68,7 @@
"axios": ">= 1.0.0 < 2"
},
"devDependencies": {
"@hey-api/client-core": "workspace:*",
"axios": "1.7.9"
}
}
30 changes: 30 additions & 0 deletions packages/client-axios/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import path from 'node:path';

import { defineConfig } from 'rollup';
import dts from 'rollup-plugin-dts';

const files = ['index.d.ts', 'index.d.cts'];

export default files.map((file) =>
defineConfig({
external: (id) => {
const normalizedId = id.split(path.sep).join('/');
if (normalizedId === '@hey-api/client-core') {
return false;
}
return (
!normalizedId.startsWith('/') && !/^[a-zA-Z]:\//.test(normalizedId)
);
},
input: `./dist/${file}`,
output: {
file: `./dist/${file}`,
format: 'es',
},
plugins: [
dts({
respectExternal: true,
}),
],
}),
);
59 changes: 2 additions & 57 deletions packages/client-axios/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,7 @@
import type { Auth } from '@hey-api/client-core';
import { describe, expect, it, vi } from 'vitest';

import type { Auth } from '../types';
import {
axiosHeadersKeywords,
getAuthToken,
mergeHeaders,
setAuthParams,
} from '../utils';

describe('getAuthToken', () => {
it('returns bearer token', async () => {
const auth = vi.fn().mockReturnValue('foo');
const token = await getAuthToken(
{
scheme: 'bearer',
type: 'http',
},
auth,
);
expect(auth).toHaveBeenCalled();
expect(token).toBe('Bearer foo');
});

it('returns basic token', async () => {
const auth = vi.fn().mockReturnValue('foo:bar');
const token = await getAuthToken(
{
scheme: 'basic',
type: 'http',
},
auth,
);
expect(auth).toHaveBeenCalled();
expect(token).toBe(`Basic ${btoa('foo:bar')}`);
});

it('returns raw token', async () => {
const auth = vi.fn().mockReturnValue('foo');
const token = await getAuthToken(
{
type: 'http',
},
auth,
);
expect(auth).toHaveBeenCalled();
expect(token).toBe('foo');
});

it('returns nothing when auth function is undefined', async () => {
const token = await getAuthToken(
{
type: 'http',
},
undefined,
);
expect(token).toBeUndefined();
});
});
import { axiosHeadersKeywords, mergeHeaders, setAuthParams } from '../utils';

describe('mergeHeaders', () => {
it.each(axiosHeadersKeywords)(
Expand Down
6 changes: 3 additions & 3 deletions packages/client-axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ export const createClient = (config: Config): Client => {
};

export type {
Auth,
Client,
Config,
Options,
OptionsLegacyParser,
RequestOptions,
RequestResult,
} from './types';
export { createConfig } from './utils';
export type { Auth, QuerySerializerOptions } from '@hey-api/client-core';
export {
createConfig,
formDataBodySerializer,
jsonBodySerializer,
urlSearchParamsBodySerializer,
} from './utils';
} from '@hey-api/client-core';
19 changes: 6 additions & 13 deletions packages/client-axios/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import type {
Auth,
BodySerializer,
QuerySerializer,
QuerySerializerOptions,
} from '@hey-api/client-core';
import type {
AxiosError,
AxiosInstance,
Expand All @@ -6,12 +12,6 @@ import type {
CreateAxiosDefaults,
} from 'axios';

import type {
BodySerializer,
QuerySerializer,
QuerySerializerOptions,
} from './utils';

type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;

export interface Config<ThrowOnError extends boolean = boolean>
Expand Down Expand Up @@ -96,13 +96,6 @@ export interface Config<ThrowOnError extends boolean = boolean>
throwOnError?: ThrowOnError;
}

export interface Auth {
in?: 'header' | 'query';
name?: string;
scheme?: 'basic' | 'bearer';
type: 'apiKey' | 'http';
}

type AuthToken = string | undefined;

export interface RequestOptions<
Expand Down
Loading

0 comments on commit 1b73773

Please sign in to comment.