Skip to content

Commit

Permalink
Merge pull request #1540 from hey-api/chore/nuxt-utils
Browse files Browse the repository at this point in the history
chore: import identifier only when it's accessed
  • Loading branch information
mrlubos authored Jan 9, 2025
2 parents 0270ac8 + 20782ee commit caa46ec
Show file tree
Hide file tree
Showing 63 changed files with 2,767 additions and 3,489 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
node-version: [18.x, 20.x, 22.x]
# node-version: [18.x, 20.x, 22.x]
# tests decided to fail for Node 18 out of nowhere, disable for now
node-version: [20.x, 22.x]
steps:
- uses: actions/checkout@v4

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ yarn-error.log*
dist
coverage
.env
.nuxt
.output
.svelte-kit

# test files
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
**/node_modules
**/templates
**/dist
**/.nuxt
**/.output
**/.svelte-kit
**/.vitepress/cache
**/.vitepress/dist
Expand Down
8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export default tseslint.config(
'@typescript-eslint/consistent-type-imports': 'off',
},
},
{
files: ['**/*.cjs'],
rules: {
'@typescript-eslint/no-require-imports': 'off',
},
},
configPrettier,
{
ignores: [
Expand All @@ -65,6 +71,8 @@ export default tseslint.config(
'**/test/e2e/generated/',
'**/test/generated/',
'**/__snapshots__/',
'**/.nuxt/',
'**/.output/',
'**/.svelte-kit/',
'**/.vitepress/cache',
'**/.vitepress/dist',
Expand Down
10 changes: 5 additions & 5 deletions packages/client-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
},
"scripts": {
"prepublishOnly": "pnpm build",
"test:coverage": "vitest run --coverage",
"test:update": "vitest watch --update",
"test:watch": "vitest watch",
"test": "vitest run",
"typecheck": "tsc --noEmit"
"test:coverage:disabled": "vitest run --coverage",
"test:update:disabled": "vitest watch --update",
"test:watch:disabled": "vitest watch",
"test:disabled": "vitest run",
"typecheck:disabled": "tsc --noEmit"
},
"engines": {
"node": "^18.0.0 || >=20.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/bin/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async function start() {
if (!context[0] || !context[0].config.watch) {
process.exit(0);
}
} catch (error) {
} catch {
process.exit(1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/generate/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { StringCase } from '../types/config';
import { stringCase } from '../utils/stringCase';
import { ensureDirSync } from './utils';

interface Identifier {
export interface Identifier {
/**
* Did this function add a new property to the file's `identifiers` map?
*/
Expand Down
32 changes: 16 additions & 16 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,25 @@ interface Auth {
}

export const operationOptionsType = ({
identifierData,
context,
file,
operation,
throwOnError,
}: {
context: IR.Context;
identifierData?: ReturnType<TypeScriptFile['identifier']>;
// TODO: refactor this so we don't need to import error type unless it's used here
identifierError?: ReturnType<TypeScriptFile['identifier']>;
file: TypeScriptFile;
operation: IR.OperationObject;
throwOnError?: string;
}) => {
const identifierData = importIdentifierData({ context, file, operation });

const optionsName = clientApi.Options.name;

// if (context.config.client.name === '@hey-api/client-nuxt') {
// const identifierError = importIdentifierError({ context, file, operation });
// return `${optionsName}<${identifierData?.name || 'unknown'}, ${identifierError?.name || 'unknown'}, TComposable>`;
// }

// TODO: refactor this to be more generic, works for now
if (throwOnError) {
return `${optionsName}<${identifierData?.name || 'unknown'}, ${throwOnError}>`;
Expand Down Expand Up @@ -444,10 +452,6 @@ const generateClassSdk = ({
const sdks = new Map<string, Array<ts.MethodDeclaration>>();

context.subscribe('operation', ({ operation }) => {
const identifierData = importIdentifierData({ context, file, operation });
// TODO: import error type only if we are sure we are going to use it
// const identifierError = importIdentifierError({ context, file, operation });

const node = compiler.methodDeclaration({
accessLevel: 'public',
comment: [
Expand All @@ -468,8 +472,8 @@ const generateClassSdk = ({
name: 'options',
type: operationOptionsType({
context,
identifierData,
// identifierError,
file,
operation,
throwOnError: 'ThrowOnError',
}),
},
Expand Down Expand Up @@ -527,10 +531,6 @@ const generateFlatSdk = ({
const file = context.file({ id: sdkId })!;

context.subscribe('operation', ({ operation }) => {
const identifierData = importIdentifierData({ context, file, operation });
// TODO: import error type only if we are sure we are going to use it
// const identifierError = importIdentifierError({ context, file, operation });

const node = compiler.constVariable({
comment: [
operation.deprecated && '@deprecated',
Expand All @@ -545,8 +545,8 @@ const generateFlatSdk = ({
name: 'options',
type: operationOptionsType({
context,
identifierData,
// identifierError,
file,
operation,
throwOnError: 'ThrowOnError',
}),
},
Expand Down
72 changes: 44 additions & 28 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/ref.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import type { TypeScriptFile } from '../../../generate/files';
import type { Identifier, TypeScriptFile } from '../../../generate/files';
import type { IR } from '../../../ir/types';
import { operationIrRef } from '../../shared/utils/ref';

export const typesId = 'types';

function refIdentifier<T extends Identifier>(
identifier: T,
onGet?: (identifier: T) => void,
): T {
return {
...identifier,
get name() {
onGet?.(identifier);
return identifier.name;
},
};
}

export const importIdentifierData = ({
context,
file,
Expand All @@ -12,19 +25,20 @@ export const importIdentifierData = ({
context: IR.Context;
file: TypeScriptFile;
operation: IR.OperationObject;
}): ReturnType<TypeScriptFile['identifier']> => {
}): Identifier => {
const identifierData = context.file({ id: 'types' })!.identifier({
$ref: operationIrRef({ id: operation.id, type: 'data' }),
namespace: 'type',
});
if (identifierData.name) {
file.import({
asType: true,
module: file.relativePathToFile({ context, id: 'types' }),
name: identifierData.name,
});
}
return identifierData;
return refIdentifier(identifierData, (identifier) => {
if (identifier.name) {
file.import({
asType: true,
module: file.relativePathToFile({ context, id: 'types' }),
name: identifier.name,
});
}
});
};

export const importIdentifierError = ({
Expand All @@ -35,19 +49,20 @@ export const importIdentifierError = ({
context: IR.Context;
file: TypeScriptFile;
operation: IR.OperationObject;
}): ReturnType<TypeScriptFile['identifier']> => {
}): Identifier => {
const identifierError = context.file({ id: 'types' })!.identifier({
$ref: operationIrRef({ id: operation.id, type: 'error' }),
namespace: 'type',
});
if (identifierError.name) {
file.import({
asType: true,
module: file.relativePathToFile({ context, id: 'types' }),
name: identifierError.name,
});
}
return identifierError;
return refIdentifier(identifierError, (identifier) => {
if (identifier.name) {
file.import({
asType: true,
module: file.relativePathToFile({ context, id: 'types' }),
name: identifier.name,
});
}
});
};

export const importIdentifierResponse = ({
Expand All @@ -58,17 +73,18 @@ export const importIdentifierResponse = ({
context: IR.Context;
file: TypeScriptFile;
operation: IR.OperationObject;
}): ReturnType<TypeScriptFile['identifier']> => {
}): Identifier => {
const identifierResponse = context.file({ id: 'types' })!.identifier({
$ref: operationIrRef({ id: operation.id, type: 'response' }),
namespace: 'type',
});
if (identifierResponse.name) {
file.import({
asType: true,
module: file.relativePathToFile({ context, id: 'types' }),
name: identifierResponse.name,
});
}
return identifierResponse;
return refIdentifier(identifierResponse, (identifier) => {
if (identifier.name) {
file.import({
asType: true,
module: file.relativePathToFile({ context, id: 'types' }),
name: identifier.name,
});
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { ImportExportItemObject } from '../../../compiler/utils';
import type { IR } from '../../../ir/types';
import { operationOptionsType } from '../../@hey-api/sdk/plugin';
import {
importIdentifierData,
importIdentifierError,
importIdentifierResponse,
} from '../../@hey-api/typescript/ref';
Expand All @@ -19,14 +18,10 @@ export const useTypeData = ({
}) => {
const file = context.file({ id: plugin.name })!;

const identifierData = importIdentifierData({ context, file, operation });
// TODO: import error type only if we are sure we are going to use it
// const identifierError = importIdentifierError({ context, file, operation });

const typeData = operationOptionsType({
context,
identifierData,
// identifierError,
file,
operation,
});
return typeData;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig, type Options } from '@hey-api/client-fetch';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesError, CallWithDuplicateResponsesResponse, CallWithResponsesData, CallWithResponsesError, CallWithResponsesResponse, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyError, PostApiVbyApiVersionBodyResponse } from './types.gen';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesResponse, CallWithDuplicateResponsesError, CallWithResponsesData, CallWithResponsesResponse, CallWithResponsesError, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyResponse, PostApiVbyApiVersionBodyError } from './types.gen';

export const client = createClient(createConfig());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig, type Options } from '@hey-api/client-fetch';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesError, CallWithDuplicateResponsesResponse, CallWithResponsesData, CallWithResponsesError, CallWithResponsesResponse, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyError, PostApiVbyApiVersionBodyResponse } from './types.gen';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesResponse, CallWithDuplicateResponsesError, CallWithResponsesData, CallWithResponsesResponse, CallWithResponsesError, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyResponse, PostApiVbyApiVersionBodyError } from './types.gen';

export const client = createClient(createConfig({
throwOnError: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig, type Options } from '@hey-api/client-fetch';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesError, CallWithDuplicateResponsesResponse, CallWithResponsesData, CallWithResponsesError, CallWithResponsesResponse, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyError, PostApiVbyApiVersionBodyResponse } from './types.gen';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesResponse, CallWithDuplicateResponsesError, CallWithResponsesData, CallWithResponsesResponse, CallWithResponsesError, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyResponse, PostApiVbyApiVersionBodyError } from './types.gen';

export const client = createClient(createConfig());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig, type Options } from '@hey-api/client-axios';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesError, CallWithDuplicateResponsesResponse, CallWithResponsesData, CallWithResponsesError, CallWithResponsesResponse, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyError, PostApiVbyApiVersionBodyResponse } from './types.gen';
import type { ServiceWithEmptyTagData, DeleteCallWithoutParametersAndResponseData, GetCallWithoutParametersAndResponseData, HeadCallWithoutParametersAndResponseData, OptionsCallWithoutParametersAndResponseData, PatchCallWithoutParametersAndResponseData, PostCallWithoutParametersAndResponseData, PutCallWithoutParametersAndResponseData, CallWithDescriptionsData, CallWithParametersData, CallWithWeirdParameterNamesData, CallWithDefaultParametersData, CallWithDefaultOptionalParametersData, CallToTestOrderOfParamsData, DuplicateNameData, DuplicateName2Data, DuplicateName3Data, DuplicateName4Data, CallWithNoContentResponseData, CallWithResponseAndNoContentResponseData, CallWithResponseAndNoContentResponseResponse, DummyAData, DummyBData, CallWithResponseData, CallWithResponseResponse, CallWithDuplicateResponsesData, CallWithDuplicateResponsesResponse, CallWithDuplicateResponsesError, CallWithResponsesData, CallWithResponsesResponse, CallWithResponsesError, CollectionFormatData, TypesData, TypesResponse, ComplexTypesData, ComplexTypesResponse, CallWithResultFromHeaderData, TestErrorCodeData, NonAsciiæøåÆøÅöôêÊ字符串Data, NonAsciiæøåÆøÅöôêÊ字符串Response, PostApiVbyApiVersionBodyData, PostApiVbyApiVersionBodyResponse, PostApiVbyApiVersionBodyError } from './types.gen';

export const client = createClient(createConfig());

Expand Down
Loading

0 comments on commit caa46ec

Please sign in to comment.