Skip to content

Commit

Permalink
chore: use of schema and schemaoperation components
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 24, 2024
1 parent a3a9075 commit a452832
Show file tree
Hide file tree
Showing 37 changed files with 735 additions and 685 deletions.
56 changes: 0 additions & 56 deletions packages/swagger-faker/src/FakerBuilder.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/swagger-faker/src/OperationGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRoot } from '@kubb/react'
import { OperationGenerator as Generator } from '@kubb/swagger'
import { Oas } from '@kubb/swagger/components'

import { Schema } from './components/Schema.tsx'
import { OperationSchema } from './components/OperationSchema.tsx'

import type { KubbFile } from '@kubb/core'
import type { AppContextProps } from '@kubb/react'
Expand All @@ -25,7 +25,7 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions
root.render(
<Oas oas={oas} operations={[operation]} getOperationSchemas={(...props) => this.getSchemas(...props)}>
<Oas.Operation operation={operation}>
<Schema.File mode={mode} />
<OperationSchema.File mode={mode} />
</Oas.Operation>
</Oas>,
{ meta: { pluginManager, plugin: { ...plugin, options } } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import path from 'node:path'
import { mockedPluginManager } from '@kubb/core/mocks'
import { OasManager } from '@kubb/swagger'

import { FakerGenerator } from './FakerGenerator'
import { SchemaGenerator } from './SchemaGenerator.tsx'

import type { Plugin } from '@kubb/core'
import type { SchemaObject } from '@kubb/swagger/oas'
import type { PluginOptions } from './types'

describe('FakeGenerator enums', async () => {
describe('Faker SchemaGenerator enums', async () => {
const schemaPath = path.resolve(__dirname, '../mocks/enums.yaml')
const oas = await new OasManager().parse(schemaPath)
const generator = new FakerGenerator({
const generator = new SchemaGenerator({
dateType: 'string',
mapper: undefined,
seed: 1,
transformers: {},
unknownType: 'any',
enumType: 'asConst',
usedEnumNames: {},
}, {
oas,
pluginManager: mockedPluginManager,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import { SchemaGenerator } from '@kubb/swagger'
import { createRoot } from '@kubb/react'
import { SchemaGenerator as Generator } from '@kubb/swagger'
import { Oas, Schema } from '@kubb/swagger/components'
import { pluginKey as swaggerTypeScriptPluginKey } from '@kubb/swagger-ts'

import { fakerParser } from './fakerParser.tsx'
import { pluginKey } from './plugin.ts'

import type { Schema as SchemaType, SchemaGeneratorBuildOptions, SchemaGeneratorOptions } from '@kubb/swagger'
import type { AppContextProps } from '@kubb/react'
import type { Schema as SchemaType, SchemaGeneratorBuildOptions, SchemaGeneratorOptions, SchemaMethodResult } from '@kubb/swagger'
import type { SchemaObject } from '@kubb/swagger/oas'
import type { FileMeta, PluginOptions } from './types.ts'

type Options = SchemaGeneratorOptions & {
seed?: number | number[]
}

export class FakerGenerator extends SchemaGenerator<Options> {
async schema() {
return null
export class SchemaGenerator extends Generator<PluginOptions['resolvedOptions'], PluginOptions> {
async schema(name: string, object: SchemaObject): SchemaMethodResult<FileMeta> {
const { oas, pluginManager, mode, plugin, output } = this.context

const root = createRoot<AppContextProps<PluginOptions['appMeta']>>({ logger: pluginManager.logger })

root.render(
<Oas oas={oas}>
<Oas.Schema generator={this} name={name} object={object}>
<Schema.File isTypeOnly output={output} mode={mode} />
</Oas.Schema>
</Oas>,
{ meta: { pluginManager, plugin } },
)

return root.files
}

getSource(name: string, schemas: SchemaType[], {
operationName,
operation,
description,
}: SchemaGeneratorBuildOptions = {}): string[] {
const texts: string[] = []
Expand All @@ -32,34 +47,14 @@ export class FakerGenerator extends SchemaGenerator<Options> {
description,
seed: this.options.seed,
})
// hack to add typescript imports
if (typeName) {
const typeFileName = this.context.pluginManager.resolveName({ name, pluginKey: swaggerTypeScriptPluginKey, type: 'file' })

const typePath = this.context.pluginManager.resolvePath({
baseName: operationName || typeFileName,
pluginKey: swaggerTypeScriptPluginKey,
options: { tag: operation?.getTags()[0]?.name },
})

if (typePath) {
this.imports.push({
ref: {
propertyName: typeName,
originalName: name,
path: typePath,
pluginKey: swaggerTypeScriptPluginKey,
},
path: typePath,
isTypeOnly: true,
})
}
}

texts.push(output)

return texts
}
/**
* @deprecated only used for testing
*/
buildSource(name: string, schema: SchemaObject, options: SchemaGeneratorBuildOptions = {}): string[] {
const schemas = this.buildSchemas(schema, name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ exports[`OperationGenerator > [DELETE] should generate with unknownType \`any\`
export function DeletePetPetidMutationResponse(override?: NonNullable<Partial<DeletePetPetidMutationResponse>>): NonNullable<DeletePetPetidMutationResponse> {
return undefined;
}
"
`;
exports[`OperationGenerator > [GET] should generate 1`] = `
"import { faker } from "@faker-js/faker";
import { Pets } from "./Pets";
import { Error } from "./Error";
import type { ListPets200, ListPetsError, ListPetsQueryParams, ListPetsQueryResponse } from "./ListPets";
import type { ListPetsQueryParams, ListPets200, ListPetsError, ListPetsQueryResponse } from "./ListPets";
import type { Pets } from "./Pets";
export function ListPetsQueryParams(override: NonNullable<Partial<ListPetsQueryParams>> = {}): NonNullable<ListPetsQueryParams> {
return {
...{ "limit": faker.string.alpha() },
...override
};
}
/**
Expand All @@ -34,29 +45,31 @@ export function ListPetsError(override?: NonNullable<Partial<ListPetsError>>): N
}
export function ListPetsQueryParams(override: NonNullable<Partial<ListPetsQueryParams>> = {}): NonNullable<ListPetsQueryParams> {
return {
...{ "limit": faker.string.alpha() },
...override
};
}
/**
* @description A paged array of pets
*/
export function ListPetsQueryResponse(override?: NonNullable<Partial<ListPetsQueryResponse>>): NonNullable<ListPetsQueryResponse> {
return Pets(override);
}
"
`;
exports[`OperationGenerator > [GET] should generate 2`] = `
"import { faker } from "@faker-js/faker";
import { Pet } from "./Pet";
import { Error } from "./Error";
import type { ShowPetById200, ShowPetByIdError, ShowPetByIdPathParams, ShowPetByIdQueryResponse } from "./ShowPetById";
import type { ShowPetByIdPathParams, ShowPetById200, ShowPetByIdError, ShowPetByIdQueryResponse } from "./ShowPetById";
import type { Pet } from "./Pet";
export function ShowPetByIdPathParams(override: NonNullable<Partial<ShowPetByIdPathParams>> = {}): NonNullable<ShowPetByIdPathParams> {
return {
...{ "petId": faker.string.alpha(), "testId": faker.string.alpha() },
...override
};
}
/**
Expand All @@ -75,29 +88,32 @@ export function ShowPetByIdError(override?: NonNullable<Partial<ShowPetByIdError
}
export function ShowPetByIdPathParams(override: NonNullable<Partial<ShowPetByIdPathParams>> = {}): NonNullable<ShowPetByIdPathParams> {
return {
...{ "petId": faker.string.alpha(), "testId": faker.string.alpha() },
...override
};
}
/**
* @description Expected response to a valid request
*/
export function ShowPetByIdQueryResponse(override?: NonNullable<Partial<ShowPetByIdQueryResponse>>): NonNullable<ShowPetByIdQueryResponse> {
return Pet(override);
}
"
`;
exports[`OperationGenerator > [GET] should generate with seed \`[222]\` 1`] = `
"import { faker } from "@faker-js/faker";
import { Pets } from "./Pets";
import { Error } from "./Error";
import type { ListPets200, ListPetsError, ListPetsQueryParams, ListPetsQueryResponse } from "./ListPets";
import type { ListPetsQueryParams, ListPets200, ListPetsError, ListPetsQueryResponse } from "./ListPets";
import type { Pets } from "./Pets";
export function ListPetsQueryParams(override: NonNullable<Partial<ListPetsQueryParams>> = {}): NonNullable<ListPetsQueryParams> {
faker.seed([222]);
return {
...{ "limit": faker.string.alpha() },
...override
};
}
/**
Expand All @@ -118,31 +134,33 @@ export function ListPetsError(override?: NonNullable<Partial<ListPetsError>>): N
}
export function ListPetsQueryParams(override: NonNullable<Partial<ListPetsQueryParams>> = {}): NonNullable<ListPetsQueryParams> {
faker.seed([222]);
return {
...{ "limit": faker.string.alpha() },
...override
};
}
/**
* @description A paged array of pets
*/
export function ListPetsQueryResponse(override?: NonNullable<Partial<ListPetsQueryResponse>>): NonNullable<ListPetsQueryResponse> {
faker.seed([222]);
return Pets(override);
}
"
`;
exports[`OperationGenerator > [GET] should generate with seed \`[222]\` 2`] = `
"import { faker } from "@faker-js/faker";
import { Pet } from "./Pet";
import { Error } from "./Error";
import type { ShowPetById200, ShowPetByIdError, ShowPetByIdPathParams, ShowPetByIdQueryResponse } from "./ShowPetById";
import type { ShowPetByIdPathParams, ShowPetById200, ShowPetByIdError, ShowPetByIdQueryResponse } from "./ShowPetById";
import type { Pet } from "./Pet";
export function ShowPetByIdPathParams(override: NonNullable<Partial<ShowPetByIdPathParams>> = {}): NonNullable<ShowPetByIdPathParams> {
faker.seed([222]);
return {
...{ "petId": faker.string.alpha(), "testId": faker.string.alpha() },
...override
};
}
/**
Expand All @@ -163,23 +181,15 @@ export function ShowPetByIdError(override?: NonNullable<Partial<ShowPetByIdError
}
export function ShowPetByIdPathParams(override: NonNullable<Partial<ShowPetByIdPathParams>> = {}): NonNullable<ShowPetByIdPathParams> {
faker.seed([222]);
return {
...{ "petId": faker.string.alpha(), "testId": faker.string.alpha() },
...override
};
}
/**
* @description Expected response to a valid request
*/
export function ShowPetByIdQueryResponse(override?: NonNullable<Partial<ShowPetByIdQueryResponse>>): NonNullable<ShowPetByIdQueryResponse> {
faker.seed([222]);
return Pet(override);
}
"
`;
Expand Down Expand Up @@ -218,5 +228,7 @@ export function CreatePetsError(override?: NonNullable<Partial<CreatePetsError>>
export function CreatePetsMutationResponse(override?: NonNullable<Partial<CreatePetsMutationResponse>>): NonNullable<CreatePetsMutationResponse> {
return undefined;
}
"
`;
Loading

0 comments on commit a452832

Please sign in to comment.