Skip to content

Commit

Permalink
fix(core): take output.indexFiles setting into account (#1770)
Browse files Browse the repository at this point in the history
* fix(core): take output.indexFiles setting into account

* fix: move workspace from fetch to default

* fix: refactor

* fix: remove unused imports

* fix: headers
  • Loading branch information
AllieJonsson authored Jan 7, 2025
1 parent 743a71f commit 074936a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 52 deletions.
15 changes: 15 additions & 0 deletions packages/core/src/writers/generate-imports-for-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import uniqBy from 'lodash.uniqby';
import { GeneratorImport, NormalizedOutputOptions } from '../types';
import { upath, camel } from '../utils';

export const generateImportsForBuilder = (
output: NormalizedOutputOptions,
imports: GeneratorImport[],
relativeSchemasPath: string,
) =>
output.schemas && !output.indexFiles
? uniqBy(imports, 'name').map((i) => ({
exports: [i],
dependency: upath.joinSafe(relativeSchemasPath, camel(i.name)),
}))
: [{ exports: imports, dependency: relativeSchemasPath }];
30 changes: 16 additions & 14 deletions packages/core/src/writers/single-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isSyntheticDefaultImportsAllow,
upath,
} from '../utils';
import { generateImportsForBuilder } from './generate-imports-for-builder';
import { generateTarget } from './target';
import { getOrvalGeneratedTypes } from './types';

Expand Down Expand Up @@ -50,20 +51,20 @@ export const writeSingleMode = async ({
output.tsconfig,
);

const importsForBuilder = schemasPath
? generateImportsForBuilder(
output,
imports.filter(
(imp) => !importsMock.some((impMock) => imp.name === impMock.name),
),
schemasPath,
)
: [];

data += builder.imports({
client: output.client,
implementation,
imports: schemasPath
? [
{
exports: imports.filter(
(imp) =>
!importsMock.some((impMock) => imp.name === impMock.name),
),
dependency: schemasPath,
},
]
: [],
imports: importsForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand All @@ -77,11 +78,12 @@ export const writeSingleMode = async ({
});

if (output.mock) {
const importsMockForBuilder = schemasPath
? generateImportsForBuilder(output, importsMock, schemasPath)
: [];
data += builder.importsMock({
implementation: implementationMock,
imports: schemasPath
? [{ exports: importsMock, dependency: schemasPath }]
: [],
imports: importsMockForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/writers/split-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { generateTarget } from './target';
import { getOrvalGeneratedTypes } from './types';
import { getMockFileExtensionByTypeName } from '../utils/fileExtensions';
import { generateImportsForBuilder } from './generate-imports-for-builder';

export const writeSplitMode = async ({
builder,
Expand Down Expand Up @@ -52,10 +53,16 @@ export const writeSplitMode = async ({
output.tsconfig,
);

const importsForBuilder = generateImportsForBuilder(
output,
imports,
relativeSchemasPath,
);

implementationData += builder.imports({
client: output.client,
implementation,
imports: [{ exports: imports, dependency: relativeSchemasPath }],
imports: importsForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand All @@ -68,9 +75,15 @@ export const writeSplitMode = async ({
output,
});

const importsMockForBuilder = generateImportsForBuilder(
output,
importsMock,
relativeSchemasPath,
);

mockData += builder.importsMock({
implementation: implementationMock,
imports: [{ exports: importsMock, dependency: relativeSchemasPath }],
imports: importsMockForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
25 changes: 4 additions & 21 deletions packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import fs from 'fs-extra';
import { generateModelsInline, generateMutatorImports } from '../generators';
import {
GeneratorImport,
NormalizedOutputOptions,
OutputClient,
WriteModeProps,
} from '../types';
import { OutputClient, WriteModeProps } from '../types';
import {
camel,
getFileInfo,
Expand All @@ -16,7 +11,7 @@ import {
import { generateTargetForTags } from './target-tags';
import { getOrvalGeneratedTypes } from './types';
import { getMockFileExtensionByTypeName } from '../utils/fileExtensions';
import uniqBy from 'lodash.uniqby';
import { generateImportsForBuilder } from './generate-imports-for-builder';

export const writeSplitTagsMode = async ({
builder,
Expand Down Expand Up @@ -63,7 +58,7 @@ export const writeSplitTagsMode = async ({
)
: '../' + filename + '.schemas';

const importsForBuilder = generateImports(
const importsForBuilder = generateImportsForBuilder(
output,
imports,
relativeSchemasPath,
Expand All @@ -85,7 +80,7 @@ export const writeSplitTagsMode = async ({
output,
});

const importsMockForBuilder = generateImports(
const importsMockForBuilder = generateImportsForBuilder(
output,
importsMock,
relativeSchemasPath,
Expand Down Expand Up @@ -192,15 +187,3 @@ export const writeSplitTagsMode = async ({

return generatedFilePathsArray.flatMap((it) => it);
};

const generateImports = (
output: NormalizedOutputOptions,
imports: GeneratorImport[],
relativeSchemasPath: string,
) =>
output.schemas && !output.indexFiles
? uniqBy(imports, 'name').map((i) => ({
exports: [i],
dependency: upath.join(relativeSchemasPath, camel(i.name)),
}))
: [{ exports: imports, dependency: relativeSchemasPath }];
27 changes: 15 additions & 12 deletions packages/core/src/writers/tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
kebab,
upath,
} from '../utils';
import { generateImportsForBuilder } from './generate-imports-for-builder';
import { generateTargetForTags } from './target-tags';
import { getOrvalGeneratedTypes } from './types';

Expand Down Expand Up @@ -55,15 +56,13 @@ export const writeTagsMode = async ({
)
: './' + filename + '.schemas';

const importsForBuilder = [
{
exports: imports.filter(
(imp) =>
!importsMock.some((impMock) => imp.name === impMock.name),
),
dependency: schemasPathRelative,
},
];
const importsForBuilder = generateImportsForBuilder(
output,
imports.filter(
(imp) => !importsMock.some((impMock) => imp.name === impMock.name),
),
schemasPathRelative,
);

data += builder.imports({
client: output.client,
Expand All @@ -82,11 +81,15 @@ export const writeTagsMode = async ({
});

if (output.mock) {
const importsMockForBuilder = generateImportsForBuilder(
output,
importsMock,
schemasPathRelative,
);

data += builder.importsMock({
implementation: implementationMock,
imports: [
{ exports: importsMock, dependency: schemasPathRelative },
],
imports: importsMockForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
7 changes: 4 additions & 3 deletions packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ ${
const ignoreContentTypes = ['multipart/form-data'];
const fetchHeadersOption =
body.contentType && !ignoreContentTypes.includes(body.contentType)
? `headers: { 'Content-Type': '${body.contentType}', ...options?.headers }`
: '';
? `headers: { 'Content-Type': '${body.contentType}',${headers ? '...headers,' : ''} ...options?.headers }`
: headers
? 'headers: {...headers, ...options?.headers}'
: '';
const requestBodyParams = generateBodyOptions(
body,
isFormData,
Expand All @@ -170,7 +172,6 @@ ${
const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}),
{${globalFetchOptions ? '\n' : ''} ${globalFetchOptions}
${isRequestOptions ? '...options,' : ''}
${headers ? 'headers: { ...headers, ...options?.headers },' : ''}
${fetchMethodOption}${fetchHeadersOption ? ',' : ''}
${fetchHeadersOption}${fetchBodyOption ? ',' : ''}
${fetchBodyOption}
Expand Down
11 changes: 11 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,15 @@ export default defineConfig({
mock: true,
},
},
noIndexFiles: {
output: {
target: '../generated/default/no-index-files/endpoints.ts',
schemas: '../generated/default/no-index-files/model',
client: 'fetch',
indexFiles: false,
},
input: {
target: '../specifications/petstore.yaml',
},
},
});

0 comments on commit 074936a

Please sign in to comment.