Skip to content

Commit 76f49f7

Browse files
committed
initial impl
1 parent 549a42e commit 76f49f7

13 files changed

Lines changed: 171 additions & 16 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@test/openapi-ts-faker-v1",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"typecheck": "tsgo --noEmit"
8+
},
9+
"devDependencies": {
10+
"@hey-api/openapi-ts": "workspace:*",
11+
"typescript": "5.9.3"
12+
},
13+
"engines": {
14+
"node": ">=20.19.0"
15+
}
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
import { createClient } from '@hey-api/openapi-ts';
5+
6+
import { getFilePaths } from '../../../utils';
7+
import { snapshotsDir, tmpDir } from './constants';
8+
import { createConfigFactory } from './utils';
9+
10+
const version = '3.1.x';
11+
12+
const outputDir = path.join(tmpDir, version);
13+
14+
describe(`OpenAPI ${version}`, () => {
15+
const createConfig = createConfigFactory({ openApiVersion: version, outputDir });
16+
17+
const scenarios = [
18+
{
19+
config: createConfig({
20+
input: 'faker-basic.yaml',
21+
output: 'faker-basic',
22+
}),
23+
description: 'generates faker factories for basic schemas',
24+
},
25+
];
26+
27+
it.each(scenarios)('$description', async ({ config }) => {
28+
await createClient(config);
29+
30+
const outputString = config.output as string;
31+
const filePaths = getFilePaths(outputString);
32+
33+
await Promise.all(
34+
filePaths.map(async (filePath) => {
35+
const fileContent = fs.readFileSync(filePath, 'utf-8');
36+
await expect(fileContent).toMatchFileSnapshot(
37+
path.join(snapshotsDir, version, filePath.slice(outputDir.length + 1)),
38+
);
39+
}),
40+
);
41+
});
42+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import path from 'node:path';
2+
3+
export const snapshotsDir = path.join(__dirname, '..', '__snapshots__');
4+
export const tmpDir = path.join(__dirname, '..', '.tmp');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fs from 'node:fs';
2+
3+
import { tmpDir } from './constants';
4+
5+
export function teardown() {
6+
fs.rmSync(tmpDir, { force: true, recursive: true });
7+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import path from 'node:path';
2+
3+
import type { UserConfig } from '@hey-api/openapi-ts';
4+
5+
import { getSpecsPath } from '../../../utils';
6+
7+
export function createConfigFactory({
8+
openApiVersion,
9+
outputDir,
10+
}: {
11+
openApiVersion: string;
12+
outputDir: string;
13+
}) {
14+
return (userConfig: UserConfig) => {
15+
const input = userConfig.input instanceof Array ? userConfig.input[0]! : userConfig.input;
16+
const inputPath = path.join(
17+
getSpecsPath(),
18+
openApiVersion,
19+
typeof input === 'string' ? input : (input.path as string),
20+
);
21+
const output = userConfig.output instanceof Array ? userConfig.output[0]! : userConfig.output;
22+
const outputPath = typeof output === 'string' ? output : (output?.path ?? '');
23+
return {
24+
plugins: ['@faker-js/faker'],
25+
...userConfig,
26+
input:
27+
typeof userConfig.input === 'string'
28+
? inputPath
29+
: {
30+
...userConfig.input,
31+
path: inputPath,
32+
},
33+
logs: { level: 'silent', path: './logs' },
34+
output: path.join(outputDir, outputPath),
35+
} as UserConfig;
36+
};
37+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"include": ["test/**/*", "__snapshots__/**/*"],
4+
"references": [{ "path": "../../../openapi-ts" }]
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "../../../../node_modules/turbo/schema.json",
3+
"extends": ["//"],
4+
"tasks": {
5+
"build": {
6+
"dependsOn": [],
7+
"outputs": ["dist/**"]
8+
}
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { fileURLToPath } from 'node:url';
2+
3+
import { beforeAll } from 'vitest';
4+
5+
beforeAll(() => {
6+
process.chdir(fileURLToPath(new URL('.', import.meta.url)));
7+
});

packages/openapi-ts/src/plugins/@faker-js/faker/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const defaultConfig: FakerJsFakerPlugin['Config'] = {
1818
defaultValue: {
1919
case: plugin.config.case ?? 'camelCase',
2020
enabled: true,
21-
name: 'v{{name}}',
21+
name: 'fake{{name}}',
2222
},
2323
mappers,
2424
value: plugin.config.definitions,

packages/openapi-ts/src/plugins/@faker-js/faker/types.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@ export type UserConfig = Plugin.Name<'@faker-js/faker'> &
4444
*/
4545
name?: NameTransformer;
4646
};
47-
/**
48-
* Faker locale for generated data.
49-
*
50-
* @default 'en'
51-
*/
52-
locale?: string;
53-
/**
54-
* Seed for deterministic output. When set, Faker will produce
55-
* the same values across runs.
56-
*/
57-
seed?: number;
5847
};
5948

6049
export type Config = Plugin.Name<'@faker-js/faker'> &
@@ -65,10 +54,6 @@ export type Config = Plugin.Name<'@faker-js/faker'> &
6554
case: Casing;
6655
/** Configuration for reusable schema definitions. */
6756
definitions: NamingOptions & FeatureToggle;
68-
/** Faker locale for generated data. */
69-
locale: string;
70-
/** Seed for deterministic output. */
71-
seed?: number;
7257
};
7358

7459
export type FakerJsFakerPlugin = DefinePlugin<UserConfig, Config, IApi>;

0 commit comments

Comments
 (0)