fork of hey-api/openapi-ts because I need some additional things

refactor: Reuse parseExtensions and remove JSDoc from extension fields

- Made parseExtensions generic and exportable from schema parsers
- Reused parseExtensions in parameter and operation parsers
- Removed JSDoc comments from extension field index signatures

Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>

Changed files
+55 -115
packages
-12
packages/openapi-ts/src/ir/types.d.ts
··· 27 } 28 29 export interface IROperationObject { 30 - /** 31 - * OpenAPI extension fields (x-*) from the original operation. 32 - * These are custom fields that can be used by plugins. 33 - */ 34 [extension: `x-${string}`]: unknown; 35 body?: IRBodyObject; 36 deprecated?: boolean; ··· 56 57 interface IRParameterObject 58 extends Pick<JsonSchemaDraft2020_12, 'deprecated' | 'description'> { 59 - /** 60 - * OpenAPI extension fields (x-*) from the original parameter. 61 - * These are custom fields that can be used by plugins. 62 - */ 63 [extension: `x-${string}`]: unknown; 64 /** 65 * Determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986 `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. The default value is `false`. This property SHALL be ignored if the request body media type is not `application/x-www-form-urlencoded` or `multipart/form-data`. If a value is explicitly defined, then the value of `contentType` (implicit or explicit) SHALL be ignored. ··· 153 | 'title' 154 | 'example' 155 > { 156 - /** 157 - * OpenAPI extension fields (x-*) from the original schema. 158 - * These are custom fields that can be used by plugins. 159 - */ 160 [extension: `x-${string}`]: unknown; 161 /** 162 * If the schema is intended to be used as an object property, it can be
··· 27 } 28 29 export interface IROperationObject { 30 [extension: `x-${string}`]: unknown; 31 body?: IRBodyObject; 32 deprecated?: boolean; ··· 52 53 interface IRParameterObject 54 extends Pick<JsonSchemaDraft2020_12, 'deprecated' | 'description'> { 55 [extension: `x-${string}`]: unknown; 56 /** 57 * Determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986 `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. The default value is `false`. This property SHALL be ignored if the request body media type is not `application/x-www-form-urlencoded` or `multipart/form-data`. If a value is explicitly defined, then the value of `contentType` (implicit or explicit) SHALL be ignored. ··· 145 | 'title' 146 | 'example' 147 > { 148 [extension: `x-${string}`]: unknown; 149 /** 150 * If the schema is intended to be used as an object property, it can be
+5 -8
packages/openapi-ts/src/openApi/2.0.x/parser/operation.ts
··· 13 } from '../types/spec'; 14 import { contentToSchema, mediaTypeObjects } from './mediaType'; 15 import { paginationField } from './pagination'; 16 - import { schemaToIrSchema } from './schema'; 17 18 interface Operation 19 extends Omit<OperationObject, 'parameters'>, ··· 77 operation, 78 }); 79 80 - // Copy extension fields 81 - for (const key in operation) { 82 - if (key.startsWith('x-')) { 83 - (irOperation as unknown as Record<string, unknown>)[key] = 84 - operation[key as keyof Operation]; 85 - } 86 - } 87 88 return irOperation; 89 };
··· 13 } from '../types/spec'; 14 import { contentToSchema, mediaTypeObjects } from './mediaType'; 15 import { paginationField } from './pagination'; 16 + import { parseExtensions, schemaToIrSchema } from './schema'; 17 18 interface Operation 19 extends Omit<OperationObject, 'parameters'>, ··· 77 operation, 78 }); 79 80 + parseExtensions({ 81 + source: operation, 82 + target: irOperation, 83 + }); 84 85 return irOperation; 86 };
+5 -8
packages/openapi-ts/src/openApi/2.0.x/parser/parameter.ts
··· 8 SchemaObject, 9 } from '../types/spec'; 10 import { paginationField } from './pagination'; 11 - import { schemaToIrSchema } from './schema'; 12 13 type Parameter = Exclude<ParameterObject, { in: 'body' }>; 14 ··· 165 irParameter.required = parameter.required; 166 } 167 168 - // Copy extension fields 169 - for (const key in parameter) { 170 - if (key.startsWith('x-')) { 171 - (irParameter as unknown as Record<string, unknown>)[key] = 172 - parameter[key as keyof ParameterObject]; 173 - } 174 - } 175 176 return irParameter; 177 };
··· 8 SchemaObject, 9 } from '../types/spec'; 10 import { paginationField } from './pagination'; 11 + import { parseExtensions, schemaToIrSchema } from './schema'; 12 13 type Parameter = Exclude<ParameterObject, { in: 'body' }>; 14 ··· 165 irParameter.required = parameter.required; 166 } 167 168 + parseExtensions({ 169 + source: parameter, 170 + target: irParameter, 171 + }); 172 173 return irParameter; 174 };
+9 -11
packages/openapi-ts/src/openApi/2.0.x/parser/schema.ts
··· 270 return irSchema; 271 }; 272 273 - const parseExtensions = ({ 274 - irSchema, 275 - schema, 276 }: { 277 - irSchema: IR.SchemaObject; 278 - schema: SchemaObject; 279 }) => { 280 - // Copy all x-* extension fields from the source schema to the IR schema 281 - for (const key in schema) { 282 if (key.startsWith('x-')) { 283 - (irSchema as unknown as Record<string, unknown>)[key] = 284 - schema[key as keyof SchemaObject]; 285 } 286 } 287 }; ··· 299 }); 300 301 parseExtensions({ 302 - irSchema, 303 - schema, 304 }); 305 306 return irSchema;
··· 270 return irSchema; 271 }; 272 273 + export const parseExtensions = <T extends Record<string, unknown>>({ 274 + source, 275 + target, 276 }: { 277 + source: T; 278 + target: Record<string, unknown>; 279 }) => { 280 + for (const key in source) { 281 if (key.startsWith('x-')) { 282 + target[key] = source[key]; 283 } 284 } 285 }; ··· 297 }); 298 299 parseExtensions({ 300 + source: schema, 301 + target: irSchema, 302 }); 303 304 return irSchema;
+5 -8
packages/openapi-ts/src/openApi/3.0.x/parser/operation.ts
··· 12 } from '../types/spec'; 13 import { contentToSchema, mediaTypeObjects } from './mediaType'; 14 import { paginationField } from './pagination'; 15 - import { schemaToIrSchema } from './schema'; 16 17 interface Operation 18 extends Omit<OperationObject, 'parameters'>, ··· 74 operation, 75 }); 76 77 - // Copy extension fields 78 - for (const key in operation) { 79 - if (key.startsWith('x-')) { 80 - (irOperation as unknown as Record<string, unknown>)[key] = 81 - operation[key as keyof Operation]; 82 - } 83 - } 84 85 return irOperation; 86 };
··· 12 } from '../types/spec'; 13 import { contentToSchema, mediaTypeObjects } from './mediaType'; 14 import { paginationField } from './pagination'; 15 + import { parseExtensions, schemaToIrSchema } from './schema'; 16 17 interface Operation 18 extends Omit<OperationObject, 'parameters'>, ··· 74 operation, 75 }); 76 77 + parseExtensions({ 78 + source: operation, 79 + target: irOperation, 80 + }); 81 82 return irOperation; 83 };
+5 -8
packages/openapi-ts/src/openApi/3.0.x/parser/parameter.ts
··· 9 } from '../types/spec'; 10 import { mediaTypeObjects } from './mediaType'; 11 import { paginationField } from './pagination'; 12 - import { schemaToIrSchema } from './schema'; 13 14 /** 15 * Returns default parameter `allowReserved` based on value of `in`. ··· 173 irParameter.required = parameter.required; 174 } 175 176 - // Copy extension fields 177 - for (const key in parameter) { 178 - if (key.startsWith('x-')) { 179 - (irParameter as unknown as Record<string, unknown>)[key] = 180 - parameter[key as keyof ParameterObject]; 181 - } 182 - } 183 184 return irParameter; 185 };
··· 9 } from '../types/spec'; 10 import { mediaTypeObjects } from './mediaType'; 11 import { paginationField } from './pagination'; 12 + import { parseExtensions, schemaToIrSchema } from './schema'; 13 14 /** 15 * Returns default parameter `allowReserved` based on value of `in`. ··· 173 irParameter.required = parameter.required; 174 } 175 176 + parseExtensions({ 177 + source: parameter, 178 + target: irParameter, 179 + }); 180 181 return irParameter; 182 };
+9 -11
packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
··· 276 return irSchema; 277 }; 278 279 - const parseExtensions = ({ 280 - irSchema, 281 - schema, 282 }: { 283 - irSchema: IR.SchemaObject; 284 - schema: SchemaObject; 285 }) => { 286 - // Copy all x-* extension fields from the source schema to the IR schema 287 - for (const key in schema) { 288 if (key.startsWith('x-')) { 289 - (irSchema as unknown as Record<string, unknown>)[key] = 290 - schema[key as keyof SchemaObject]; 291 } 292 } 293 }; ··· 305 }); 306 307 parseExtensions({ 308 - irSchema, 309 - schema, 310 }); 311 312 return irSchema;
··· 276 return irSchema; 277 }; 278 279 + export const parseExtensions = <T extends Record<string, unknown>>({ 280 + source, 281 + target, 282 }: { 283 + source: T; 284 + target: Record<string, unknown>; 285 }) => { 286 + for (const key in source) { 287 if (key.startsWith('x-')) { 288 + target[key] = source[key]; 289 } 290 } 291 }; ··· 303 }); 304 305 parseExtensions({ 306 + source: schema, 307 + target: irSchema, 308 }); 309 310 return irSchema;
-12
packages/openapi-ts/src/openApi/3.0.x/types/spec.d.ts
··· 452 * TODO: examples 453 */ 454 export interface OperationObject { 455 - /** 456 - * OpenAPI extension fields (x-*) from the original operation. 457 - * Specification Extensions may be used to add additional metadata. 458 - */ 459 [extension: `x-${string}`]: unknown; 460 /** 461 * A map of possible out-of band callbacks related to the parent operation. The key is a unique identifier for the Callback Object. Each value in the map is a {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#callback-object Callback Object} that describes a request that may be initiated by the API provider and the expected responses. ··· 552 * TODO: examples 553 */ 554 export interface ParameterObject { 555 - /** 556 - * OpenAPI extension fields (x-*) from the original parameter. 557 - * Specification Extensions may be used to add additional metadata. 558 - */ 559 [extension: `x-${string}`]: unknown; 560 /** 561 * If `true`, clients MAY pass a zero-length string value in place of parameters that would otherwise be omitted entirely, which the server SHOULD interpret as the parameter being unused. Default value is `false`. If {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#parameter-style `style`} is used, and if {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#style-examples behavior is _n/a_ (cannot be serialized)}, the value of `allowEmptyValue` SHALL be ignored. Interactions between this field and the parameter's {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#schema-object Schema Object} are implementation-defined. This field is valid only for `query` parameters. Use of this field is NOT RECOMMENDED, and it is likely to be removed in a later revision. ··· 855 * TODO: content, examples 856 */ 857 export interface SchemaObject extends EnumExtensions { 858 - /** 859 - * OpenAPI extension fields (x-*) from the original schema. 860 - * Specification Extensions may be used to add additional metadata. 861 - */ 862 [extension: `x-${string}`]: unknown; 863 /** 864 * The value of "additionalProperties" MUST be a boolean or a schema.
··· 452 * TODO: examples 453 */ 454 export interface OperationObject { 455 [extension: `x-${string}`]: unknown; 456 /** 457 * A map of possible out-of band callbacks related to the parent operation. The key is a unique identifier for the Callback Object. Each value in the map is a {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#callback-object Callback Object} that describes a request that may be initiated by the API provider and the expected responses. ··· 548 * TODO: examples 549 */ 550 export interface ParameterObject { 551 [extension: `x-${string}`]: unknown; 552 /** 553 * If `true`, clients MAY pass a zero-length string value in place of parameters that would otherwise be omitted entirely, which the server SHOULD interpret as the parameter being unused. Default value is `false`. If {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#parameter-style `style`} is used, and if {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#style-examples behavior is _n/a_ (cannot be serialized)}, the value of `allowEmptyValue` SHALL be ignored. Interactions between this field and the parameter's {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#schema-object Schema Object} are implementation-defined. This field is valid only for `query` parameters. Use of this field is NOT RECOMMENDED, and it is likely to be removed in a later revision. ··· 847 * TODO: content, examples 848 */ 849 export interface SchemaObject extends EnumExtensions { 850 [extension: `x-${string}`]: unknown; 851 /** 852 * The value of "additionalProperties" MUST be a boolean or a schema.
+4 -7
packages/openapi-ts/src/openApi/3.1.x/parser/operation.ts
··· 74 operation, 75 }); 76 77 - // Copy extension fields 78 - for (const key in operation) { 79 - if (key.startsWith('x-')) { 80 - (irOperation as unknown as Record<string, unknown>)[key] = 81 - operation[key as keyof Operation]; 82 - } 83 - } 84 85 return irOperation; 86 };
··· 74 operation, 75 }); 76 77 + parseExtensions({ 78 + source: operation, 79 + target: irOperation, 80 + }); 81 82 return irOperation; 83 };
+4 -7
packages/openapi-ts/src/openApi/3.1.x/parser/parameter.ts
··· 166 irParameter.required = parameter.required; 167 } 168 169 - // Copy extension fields 170 - for (const key in parameter) { 171 - if (key.startsWith('x-')) { 172 - (irParameter as unknown as Record<string, unknown>)[key] = 173 - parameter[key as keyof ParameterObject]; 174 - } 175 - } 176 177 return irParameter; 178 };
··· 166 irParameter.required = parameter.required; 167 } 168 169 + parseExtensions({ 170 + source: parameter, 171 + target: irParameter, 172 + }); 173 174 return irParameter; 175 };
+9 -11
packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
··· 357 return irSchema; 358 }; 359 360 - const parseExtensions = ({ 361 - irSchema, 362 - schema, 363 }: { 364 - irSchema: IR.SchemaObject; 365 - schema: SchemaObject; 366 }) => { 367 - // Copy all x-* extension fields from the source schema to the IR schema 368 - for (const key in schema) { 369 if (key.startsWith('x-')) { 370 - (irSchema as unknown as Record<string, unknown>)[key] = 371 - schema[key as keyof SchemaObject]; 372 } 373 } 374 }; ··· 386 }); 387 388 parseExtensions({ 389 - irSchema, 390 - schema, 391 }); 392 393 return irSchema;
··· 357 return irSchema; 358 }; 359 360 + export const parseExtensions = <T extends Record<string, unknown>>({ 361 + source, 362 + target, 363 }: { 364 + source: T; 365 + target: Record<string, unknown>; 366 }) => { 367 + for (const key in source) { 368 if (key.startsWith('x-')) { 369 + target[key] = source[key]; 370 } 371 } 372 }; ··· 384 }); 385 386 parseExtensions({ 387 + source: schema, 388 + target: irSchema, 389 }); 390 391 return irSchema;
-4
packages/openapi-ts/src/openApi/3.1.x/types/json-schema-draft-2020-12.d.ts
··· 11 StringKeywords, 12 EnumExtensions, 13 OpenApiSchemaExtensions { 14 - /** 15 - * OpenAPI extension fields (x-*) from the original schema. 16 - * Specification Extensions may be used to add additional metadata. 17 - */ 18 [extension: `x-${string}`]: unknown; 19 /** 20 * The `$comment` {@link https://json-schema.org/learn/glossary#keyword keyword} is strictly intended for adding comments to a schema. Its value must always be a string. Unlike the annotations `title`, `description`, and `examples`, JSON schema {@link https://json-schema.org/learn/glossary#implementation implementations} aren't allowed to attach any meaning or behavior to it whatsoever, and may even strip them at any time. Therefore, they are useful for leaving notes to future editors of a JSON schema, but should not be used to communicate to users of the schema.
··· 11 StringKeywords, 12 EnumExtensions, 13 OpenApiSchemaExtensions { 14 [extension: `x-${string}`]: unknown; 15 /** 16 * The `$comment` {@link https://json-schema.org/learn/glossary#keyword keyword} is strictly intended for adding comments to a schema. Its value must always be a string. Unlike the annotations `title`, `description`, and `examples`, JSON schema {@link https://json-schema.org/learn/glossary#implementation implementations} aren't allowed to attach any meaning or behavior to it whatsoever, and may even strip them at any time. Therefore, they are useful for leaving notes to future editors of a JSON schema, but should not be used to communicate to users of the schema.
-8
packages/openapi-ts/src/openApi/3.1.x/types/spec.d.ts
··· 1046 * ``` 1047 */ 1048 export interface OperationObject { 1049 - /** 1050 - * OpenAPI extension fields (x-*) from the original operation. 1051 - * Specification Extensions may be used to add additional metadata. 1052 - */ 1053 [extension: `x-${string}`]: unknown; 1054 /** 1055 * A map of possible out-of band callbacks related to the parent operation. The key is a unique identifier for the Callback Object. Each value in the map is a {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#callback-object Callback Object} that describes a request that may be initiated by the API provider and the expected responses. ··· 1199 * ``` 1200 */ 1201 export interface ParameterObject { 1202 - /** 1203 - * OpenAPI extension fields (x-*) from the original parameter. 1204 - * Specification Extensions may be used to add additional metadata. 1205 - */ 1206 [extension: `x-${string}`]: unknown; 1207 /** 1208 * Sets the ability to pass empty-valued parameters. This is valid only for `query` parameters and allows sending a parameter with an empty value. Default value is `false`. If {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterStyle `style`} is used, and if behavior is `n/a` (cannot be serialized), the value of `allowEmptyValue` SHALL be ignored. Use of this property is NOT RECOMMENDED, as it is likely to be removed in a later revision.
··· 1046 * ``` 1047 */ 1048 export interface OperationObject { 1049 [extension: `x-${string}`]: unknown; 1050 /** 1051 * A map of possible out-of band callbacks related to the parent operation. The key is a unique identifier for the Callback Object. Each value in the map is a {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#callback-object Callback Object} that describes a request that may be initiated by the API provider and the expected responses. ··· 1195 * ``` 1196 */ 1197 export interface ParameterObject { 1198 [extension: `x-${string}`]: unknown; 1199 /** 1200 * Sets the ability to pass empty-valued parameters. This is valid only for `query` parameters and allows sending a parameter with an empty value. Default value is `false`. If {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterStyle `style`} is used, and if behavior is `n/a` (cannot be serialized), the value of `allowEmptyValue` SHALL be ignored. Use of this property is NOT RECOMMENDED, as it is likely to be removed in a later revision.