Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/wormhole/src/plugins/presets/payloadModifier/hepler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ import type {
// Convert Schema (custom spec) -> OpenAPI SchemaObject
function toSchemaObject(base: SchemaObject, s: Schema): SchemaObject {
const result: SchemaObject = { ...base }

const cleanType = (schema: SchemaObject) => {
delete schema.type
delete schema.enum
delete schema.oneOf
delete schema.anyOf
delete schema.allOf
delete (schema as any).items
delete schema.properties
delete schema.required
return schema
}
// Legacy union as array (treated as oneOf)
if (Array.isArray(s)) {
const baseOneOf = base.oneOf || []
cleanType(result)
result.oneOf = s.map((item, idx) => toSchemaObject(baseOneOf[idx] || {}, item))
return result
}
Expand All @@ -37,16 +48,19 @@ function toSchemaObject(base: SchemaObject, s: Schema): SchemaObject {
if ((s as SchemaOneOf).oneOf) {
const spec = s as SchemaOneOf
const baseOneOf = base.oneOf || []
cleanType(result)
result.oneOf = spec.oneOf.map((item, idx) => toSchemaObject(baseOneOf[idx] || {}, item))
}
if ((s as SchemaAnyOf).anyOf) {
const spec = s as SchemaAnyOf
const baseAnyOf = base.anyOf || []
cleanType(result)
result.anyOf = spec.anyOf.map((item, idx) => toSchemaObject(baseAnyOf[idx] || {}, item))
}
if ((s as SchemaAllOf).allOf) {
const spec = s as SchemaAllOf
const baseAllOf = base.allOf || []
cleanType(result)
result.allOf = spec.allOf.map((item, idx) => toSchemaObject(baseAllOf[idx] || {}, item))
}

Expand All @@ -55,7 +69,7 @@ function toSchemaObject(base: SchemaObject, s: Schema): SchemaObject {
const spec = s as SchemaEnum
result.enum = spec.enum
if (spec.type) {
result.type = spec.type
result.type = spec.type as any
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/wormhole/src/plugins/presets/payloadModifier/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ModifierScope = 'params' | 'pathParams' | 'data' | 'response'
export type SchemaPrimitive = 'number' | 'string' | 'boolean' | 'undefined' | 'null' | 'unknown' | 'any' | 'never'
export type SchemaPrimitive = 'number' | 'string' | 'boolean' | 'undefined' | 'null' | 'unknown' | 'any' | 'never' | ({} & string)

/**
* 表示数组类型
Expand All @@ -23,7 +23,7 @@ export interface SchemaReference {
*/
export interface SchemaEnum {
enum: Array<string | number | boolean | null>
type?: 'string' | 'number' | 'integer' | 'boolean' | 'null'
type?: SchemaPrimitive
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/wormhole/src/plugins/presets/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface RenameConfig {
* Custom transformation function
* Will be applied before style transformation
*/
transform?: (apiDescriptor: ApiDescriptor) => string
transform?: (apiDescriptor: ApiDescriptor, value: string) => string
}

function toCamelCase(str: string): string {
Expand Down Expand Up @@ -69,7 +69,7 @@ function applyRenameRule(value: string, config: RenameConfig, apiDescriptor: Api
}

if (config.transform) {
value = config.transform(apiDescriptor)
value = config.transform(apiDescriptor, value)
}

if (!config.style) {
Expand Down
9 changes: 6 additions & 3 deletions packages/wormhole/test/plugins/payloadModifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('payloadModifier plugin tests', () => {

it('modifies query/path parameters and removes matched ones', () => {
const handleApi = getHandleApi([
{ scope: 'params', match: 'age', handler: () => ({ required: true, value: 'string' }) },
{ scope: 'params', match: 'age', handler: () => ({ required: true, value: ['string', 'number', 'boolean'] }) },
{ scope: 'params', match: 'debug', handler: () => null },
{ scope: 'pathParams', match: 'id', handler: () => ({ required: false, value: 'string' }) },
])
Expand All @@ -21,7 +21,7 @@ describe('payloadModifier plugin tests', () => {
method: 'get',
parameters: [
{ name: 'id', in: 'path', required: true, schema: { type: 'integer' } },
{ name: 'age', in: 'query', required: false, schema: { type: 'integer' } },
{ name: 'age', in: 'query', required: false, schema: { type: 'integer', description: 'hello age' } },
{ name: 'debug', in: 'query', required: false, schema: { type: 'boolean' } },
{ name: 'q', in: 'query', required: false, schema: { type: 'string' } },
],
Expand All @@ -32,7 +32,10 @@ describe('payloadModifier plugin tests', () => {
const result = handleApi(api)!
// age converted to string and required true
const ageParam = result.parameters!.find(p => p.in === 'query' && p.name === 'age')!
expect((ageParam.schema as SchemaObject)?.type).toBe('string')
expect(ageParam.schema).toEqual({
description: 'hello age',
oneOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }],
})
expect(ageParam.required).toBe(true)

// debug removed
Expand Down
6 changes: 3 additions & 3 deletions packages/wormhole/typings/plugins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export declare function filterApiDescriptor(apiDescriptor: ApiDescriptor, config
export declare function apiFilter(config: FilterApiConfig | FilterApiConfig[]): ApiPlugin;
export declare function importType(config: Record<string, string[]>): ApiPlugin;
export type ModifierScope = "params" | "pathParams" | "data" | "response";
export type SchemaPrimitive = "number" | "string" | "boolean" | "undefined" | "null" | "unknown" | "any" | "never";
export type SchemaPrimitive = "number" | "string" | "boolean" | "undefined" | "null" | "unknown" | "any" | "never" | ({} & string);
/**
* 表示数组类型
*/
Expand All @@ -329,7 +329,7 @@ export interface SchemaReference {
*/
export interface SchemaEnum {
enum: Array<string | number | boolean | null>;
type?: "string" | "number" | "integer" | "boolean" | "null";
type?: SchemaPrimitive;
}
/**
* 组合类型表示(与/或/交叉)
Expand Down Expand Up @@ -398,7 +398,7 @@ export interface RenameConfig {
* Custom transformation function
* Will be applied before style transformation
*/
transform?: (apiDescriptor: ApiDescriptor) => string;
transform?: (apiDescriptor: ApiDescriptor, value: string) => string;
}
/**
* Creates a rename plugin that transforms API descriptors
Expand Down