Skip to content

Commit 60def83

Browse files
authored
Strip file extensions on customBaseQueryNode (#19)
1 parent 08c45cd commit 60def83

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

src/generate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { createQuestionToken, keywordType } from 'oazapfts/lib/codegen/tscodegen
1212
import { OpenAPIV3 } from 'openapi-types';
1313
import { generateReactHooks } from './generators/react-hooks';
1414
import { GenerationOptions, OperationDefinition } from './types';
15-
import { capitalize, getOperationDefinitions, getV3Doc, isQuery, MESSAGES } from './utils';
15+
import { capitalize, getOperationDefinitions, getV3Doc, isQuery, MESSAGES, stripFileExtension } from './utils';
1616

1717
const { factory } = ts;
1818

@@ -116,7 +116,7 @@ export async function generateApi(
116116
throw new Error(MESSAGES.FILE_NOT_FOUND);
117117
}
118118

119-
customBaseQueryNode = generateImportNode(filePath, {
119+
customBaseQueryNode = generateImportNode(stripFileExtension(filePath), {
120120
[baseQueryFn]: baseQueryFn,
121121
});
122122
} else {
@@ -135,7 +135,7 @@ export async function generateApi(
135135

136136
baseQueryFn = 'customBaseQuery';
137137

138-
customBaseQueryNode = generateImportNode(filePath, {
138+
customBaseQueryNode = generateImportNode(stripFileExtension(filePath), {
139139
default: baseQueryFn,
140140
});
141141
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './isQuery';
55
export * from './isValidUrl';
66
export * from './prettier';
77
export * from './messages';
8+
export * from './stripFileExtension';

src/utils/stripFileExtension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const stripFileExtension = (path: string) => path.replace(/\.(ts|js)$/, '');

test/cli.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('CLI options testing', () => {
110110
);
111111

112112
expect(result.stdout).not.toContain('fetchBaseQuery');
113-
expect(result.stdout).toContain(`import { anotherNamedBaseQuery } from \"test/fixtures/customBaseQuery.ts\"`);
113+
expect(result.stdout).toContain(`import { anotherNamedBaseQuery } from \"test/fixtures/customBaseQuery\"`);
114114

115115
const expectedErrors = [MESSAGES.NAMED_EXPORT_MISSING];
116116

@@ -125,7 +125,7 @@ describe('CLI options testing', () => {
125125
);
126126

127127
expect(result.stdout).not.toContain('fetchBaseQuery');
128-
expect(result.stdout).toContain(`import { default as customBaseQuery } from \"test/fixtures/customBaseQuery.ts\"`);
128+
expect(result.stdout).toContain(`import { default as customBaseQuery } from \"test/fixtures/customBaseQuery\"`);
129129

130130
const expectedErrors = [MESSAGES.NAMED_EXPORT_MISSING];
131131

test/utils.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { stripFileExtension } from '../src/utils';
2+
3+
describe('stripFileExtension', () => {
4+
it('should strip js and ts file extensions from possible input paths', async () => {
5+
const testFileInputs = [
6+
'./whatever/output.ts',
7+
'./whatever/output.js',
8+
'whatever/output.ts',
9+
'./whatever.ts/output.ts',
10+
'./no/file/name',
11+
'../hello.ts',
12+
'banana.js',
13+
'banana',
14+
'./banana',
15+
];
16+
17+
for (const path of testFileInputs) {
18+
const stripped = stripFileExtension(path);
19+
const hasExt = stripped.endsWith('.js') || stripped.endsWith('.ts');
20+
21+
expect(hasExt).toBeFalsy();
22+
}
23+
});
24+
});

0 commit comments

Comments
 (0)