Skip to content

Commit b9f2ef0

Browse files
committed
chore: refactor types
1 parent 955c91c commit b9f2ef0

5 files changed

Lines changed: 16 additions & 15 deletions

File tree

packages/core/src/types/index.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type ScalarSchema = {
1414
export type NormalizedScalarSchema = {
1515
name?: never;
1616
type?: 'string' | 'boolean' | 'number' | 'integer' | 'object' | 'array';
17-
items?: ScalarSchema;
17+
items?: NormalizedScalarSchema;
1818
enum?: string[];
1919
directResolveAs?: NormalizedNodeType;
2020
resolvable: boolean;
@@ -46,7 +46,7 @@ export type NormalizedNodeType = {
4646
requiredOneOf?: string[];
4747
allowed?: (value: any) => string[] | undefined;
4848
extensionsPrefix?: string;
49-
directResolveAs?: NormalizedPropType;
49+
directResolveAs?: NormalizedNodeType;
5050
description?: string;
5151
documentationLink?: string;
5252
};
@@ -122,7 +122,7 @@ export function normalizeTypes(
122122

123123
if (options.doNotResolveExamples && prop && (prop as ScalarSchema).isExample) {
124124
mappedProps[propName] = {
125-
...(prop as object),
125+
...prop,
126126
resolvable: false,
127127
};
128128
}
@@ -132,24 +132,25 @@ export function normalizeTypes(
132132
}
133133

134134
// typings are painful here...
135-
function resolveType(type?: any): any {
135+
function resolveType(
136+
type: NormalizedPropType | NormalizedResolveTypeFn | string
137+
): NormalizedPropType | NormalizedResolveTypeFn {
136138
if (typeof type === 'string') {
137139
if (!normalizedTypes[type]) {
138140
throw new Error(`Unknown type name found: ${type}`);
139141
}
140142
return normalizedTypes[type];
141143
} else if (typeof type === 'function') {
142-
return (value: any, key: string) => {
143-
return resolveType(type(value, key));
144-
};
145-
} else if (type && type.name) {
144+
return (value: unknown, key: string) =>
145+
resolveType((type as NormalizedResolveTypeFn)(value, key)) as NormalizedPropType;
146+
} else if (isNamedType(type)) {
146147
type = { ...type };
147148
normalizeType(type);
148149
return type;
149-
} else if (type && type.directResolveAs) {
150+
} else if (type?.directResolveAs !== undefined) {
150151
return {
151152
...type,
152-
directResolveAs: resolveType(type.directResolveAs),
153+
directResolveAs: (resolveType(type.directResolveAs) ?? undefined) as NormalizedNodeType,
153154
};
154155
} else {
155156
return type;

packages/core/src/types/redocly-yaml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ const configGovernanceProperties: Record<
231231
return {
232232
...ConfigGovernance,
233233
directResolveAs: { name: 'ConfigGovernance', ...ConfigGovernance },
234-
} as PropType;
234+
};
235235
},
236236
description: 'Use extends to inherit rules and their configurations from other rulesets.',
237237
documentationLink: 'https://redocly.com/docs/cli/configuration/reference/extends#extends',

packages/core/src/typings/openapi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ export interface Oas3Discriminator {
223223
export interface Oas3MediaType<T extends Oas3Schema | Oas3_1Schema = Oas3Schema | Oas3_1Schema> {
224224
schema?: Referenced<T>;
225225
example?: unknown;
226-
examples?: { [name: string]: Referenced<Oas3Example> };
227-
encoding?: { [field: string]: Oas3Encoding<T> };
226+
examples?: Record<string, Referenced<Oas3Example>>;
227+
encoding?: Record<string, Oas3Encoding<T>>;
228228
itemSchema?: Referenced<T>; // added in OAS 3.2
229229
prefixEncoding?: Oas3Encoding<T>[]; // added in OAS 3.2
230230
itemEncoding?: Referenced<Oas3Encoding<T>>; // added in OAS 3.2

packages/core/src/typings/swagger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export type Oas2Header = Oas2Items & { description?: 'string' };
194194
export interface Oas2Response {
195195
description?: string;
196196
schema: Referenced<Schema>;
197-
examples?: Record<string, any>;
197+
examples?: Record<string, unknown>;
198198
headers?: Record<string, Oas2Header>;
199199
}
200200

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function isNotEmptyArray<T>(args?: T[]): args is [T, ...T[]] {
2-
return Array.isArray(args) && !!args.length;
2+
return Array.isArray(args) && args.length > 0;
33
}

0 commit comments

Comments
 (0)