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

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1457 from hey-api/fix/plugin-export-explicit

fix: allow plugins to be explicit about being re-exported from index

authored by

Lubos and committed by
GitHub
cfad37ab fca78543

+77 -26
+5
.changeset/thirty-schools-perform.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix: allow plugins to explicitly declare whether they should be re-exported from the index file
+14
packages/openapi-ts/src/generate/files.ts
··· 52 52 } 53 53 54 54 export class TypeScriptFile { 55 + /** 56 + * Should the exports from this file be re-exported in the index barrel file? 57 + */ 58 + private _exportFromIndex: boolean; 55 59 private _headers: Array<string> = []; 56 60 private _identifierCase: StringCase | undefined; 57 61 private _imports = new Map<string, Map<string, ImportExportItemObject>>(); ··· 72 76 73 77 public constructor({ 74 78 dir, 79 + exportFromIndex = false, 75 80 header = true, 76 81 identifierCase, 77 82 name, 78 83 }: { 79 84 dir: string; 85 + /** 86 + * Should the exports from this file be re-exported in the index barrel file? 87 + */ 88 + exportFromIndex?: boolean; 80 89 header?: boolean; 81 90 identifierCase?: StringCase; 82 91 name: string; 83 92 }) { 93 + this._exportFromIndex = exportFromIndex; 84 94 this._identifierCase = identifierCase; 85 95 this._name = this._setName(name); 86 96 this._path = path.resolve(dir, this._name); ··· 120 130 created: false, 121 131 name: refValue.name, 122 132 }; 133 + } 134 + 135 + public get exportFromIndex(): boolean { 136 + return this._exportFromIndex; 123 137 } 124 138 125 139 public identifier({
+22 -19
packages/openapi-ts/src/generate/output.ts
··· 130 130 131 131 await parseIR({ context }); 132 132 133 - const indexFile = context.createFile({ 134 - id: '_index', 135 - path: 'index', 136 - }); 133 + if (!context.config.dryRun) { 134 + const indexFile = context.createFile({ 135 + id: '_index', 136 + path: 'index', 137 + }); 138 + 139 + for (const file of Object.values(context.files)) { 140 + const fileName = file.nameWithoutExtension(); 141 + 142 + if (fileName === indexFile.nameWithoutExtension()) { 143 + continue; 144 + } 137 145 138 - Object.entries(context.files).forEach(([name, file]) => { 139 - if (context.config.dryRun || name === '_index') { 140 - return; 141 - } 146 + if (!file.isEmpty() && file.exportFromIndex) { 147 + // TODO: parser - add export method for more granular control over 148 + // what's exported so we can support named exports 149 + indexFile.add( 150 + compiler.exportAllDeclaration({ 151 + module: `./${fileName}`, 152 + }), 153 + ); 154 + } 142 155 143 - // TODO: parser - refactor once we have typed Plugin Files API 144 - if (!file.isEmpty() && ['sdk', 'types'].includes(name)) { 145 - indexFile.add( 146 - compiler.exportAllDeclaration({ 147 - module: `./${file.nameWithoutExtension()}`, 148 - }), 149 - ); 156 + file.write('\n\n'); 150 157 } 151 158 152 - file.write('\n\n'); 153 - }); 154 - 155 - if (!context.config.dryRun) { 156 159 indexFile.write(); 157 160 } 158 161 };
+5
packages/openapi-ts/src/ir/context.ts
··· 15 15 16 16 interface ContextFile { 17 17 /** 18 + * Should the exports from this file be re-exported in the index barrel file? 19 + */ 20 + exportFromIndex?: boolean; 21 + /** 18 22 * Unique file identifier. 19 23 */ 20 24 id: string; ··· 137 141 ); 138 142 const createdFile = new TypeScriptFile({ 139 143 dir: outputDir, 144 + exportFromIndex: file.exportFromIndex, 140 145 identifierCase: file.identifierCase, 141 146 name: `${outputParts[outputParts.length - 1]}.ts`, 142 147 });
+2
packages/openapi-ts/src/plugins/@hey-api/schemas/__tests__/schemas.test.ts
··· 70 70 files, 71 71 openApi, 72 72 plugin: { 73 + exportFromIndex: false, 73 74 name: '@hey-api/schemas', 74 75 output: 'schemas', 75 76 }, ··· 147 148 files, 148 149 openApi, 149 150 plugin: { 151 + exportFromIndex: false, 150 152 name: '@hey-api/schemas', 151 153 output: 'schemas', 152 154 },
+1
packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts
··· 318 318 319 319 export const handler: Plugin.Handler<Config> = ({ context, plugin }) => { 320 320 context.createFile({ 321 + exportFromIndex: plugin.exportFromIndex, 321 322 id: schemasId, 322 323 path: plugin.output, 323 324 });
+4
packages/openapi-ts/src/plugins/@hey-api/sdk/__tests__/plugin.test.ts
··· 103 103 files, 104 104 openApi, 105 105 plugin: { 106 + exportFromIndex: false, 106 107 output: '', 107 108 }, 108 109 }); ··· 209 210 files, 210 211 openApi, 211 212 plugin: { 213 + exportFromIndex: false, 212 214 output: '', 213 215 }, 214 216 }); ··· 277 279 files, 278 280 openApi, 279 281 plugin: { 282 + exportFromIndex: false, 280 283 output: '', 281 284 }, 282 285 }); ··· 347 350 files, 348 351 openApi, 349 352 plugin: { 353 + exportFromIndex: false, 350 354 output: '', 351 355 }, 352 356 });
+1
packages/openapi-ts/src/plugins/@hey-api/sdk/config.ts
··· 30 30 }, 31 31 asClass: false, 32 32 auth: true, 33 + exportFromIndex: true, 33 34 name: '@hey-api/sdk', 34 35 operationId: true, 35 36 output: 'sdk',
+1
packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
··· 545 545 } 546 546 547 547 const file = context.createFile({ 548 + exportFromIndex: plugin.exportFromIndex, 548 549 id: sdkId, 549 550 path: plugin.output, 550 551 });
+1
packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts
··· 390 390 // handles only response transformers for now 391 391 export const handler: Plugin.Handler<Config> = ({ context, plugin }) => { 392 392 const file = context.createFile({ 393 + exportFromIndex: plugin.exportFromIndex, 393 394 id: transformersId, 394 395 path: plugin.output, 395 396 });
+1
packages/openapi-ts/src/plugins/@hey-api/typescript/__tests__/plugin.test.ts
··· 96 96 files, 97 97 openApi, 98 98 plugin: { 99 + exportFromIndex: false, 99 100 name: '@hey-api/typescript', 100 101 output: '', 101 102 },
+1
packages/openapi-ts/src/plugins/@hey-api/typescript/config.ts
··· 8 8 _handlerLegacy: handlerLegacy, 9 9 enums: false, 10 10 enumsCase: 'SCREAMING_SNAKE_CASE', 11 + exportFromIndex: true, 11 12 exportInlineEnums: false, 12 13 identifierCase: 'PascalCase', 13 14 name: '@hey-api/typescript',
+1
packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts
··· 1008 1008 1009 1009 export const handler: Plugin.Handler<Config> = ({ context, plugin }) => { 1010 1010 context.createFile({ 1011 + exportFromIndex: plugin.exportFromIndex, 1011 1012 id: typesId, 1012 1013 identifierCase: plugin.identifierCase, 1013 1014 path: plugin.output,
+1
packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts
··· 735 735 | VueQueryConfig 736 736 > = ({ context, plugin }) => { 737 737 const file = context.createFile({ 738 + exportFromIndex: plugin.exportFromIndex, 738 739 id: plugin.name, 739 740 path: plugin.output, 740 741 });
+1
packages/openapi-ts/src/plugins/fastify/plugin.ts
··· 197 197 198 198 export const handler: Plugin.Handler<Config> = ({ context, plugin }) => { 199 199 const file = context.createFile({ 200 + exportFromIndex: plugin.exportFromIndex, 200 201 id: fastifyId, 201 202 path: plugin.output, 202 203 });
+9 -1
packages/openapi-ts/src/plugins/types.d.ts
··· 28 28 } 29 29 30 30 interface BaseConfig { 31 + /** 32 + * **This feature works only with the experimental parser** 33 + * 34 + * Should the exports from the plugin's file be re-exported in the index 35 + * barrel file? 36 + */ 37 + exportFromIndex?: boolean; 31 38 // eslint-disable-next-line @typescript-eslint/ban-types 32 39 name: PluginNames | (string & {}); 33 40 output?: string; ··· 71 78 Meta<Config> & { 72 79 _handler: Plugin.Handler<Config>; 73 80 _handlerLegacy: Plugin.LegacyHandler<Config>; 81 + exportFromIndex?: boolean; 74 82 }; 75 83 76 84 export type DefineConfig<Config extends BaseConfig> = ( ··· 86 94 }) => void; 87 95 88 96 export type Instance<Config extends BaseConfig> = OmitUnderscoreKeys<Config> & 89 - Pick<Required<Config>, 'output'>; 97 + Pick<Required<BaseConfig>, 'exportFromIndex' | 'output'>; 90 98 91 99 /** 92 100 * Plugin implementation for legacy parser. Use only if you need to support
+1
packages/openapi-ts/src/plugins/zod/plugin.ts
··· 863 863 864 864 export const handler: Plugin.Handler<Config> = ({ context, plugin }) => { 865 865 const file = context.createFile({ 866 + exportFromIndex: plugin.exportFromIndex, 866 867 id: zodId, 867 868 identifierCase: 'camelCase', 868 869 path: plugin.output,
+6 -6
packages/openapi-ts/test/sample.cjs
··· 6 6 client: { 7 7 // bundle: true, 8 8 // name: '@hey-api/client-axios', 9 - // name: '@hey-api/client-fetch', 10 - name: 'legacy/xhr', 9 + name: '@hey-api/client-fetch', 10 + // name: 'legacy/xhr', 11 11 }, 12 - // experimentalParser: true, 12 + experimentalParser: true, 13 13 input: { 14 14 // exclude: '^#/components/schemas/ModelWithCircularReference$', 15 15 // include: 16 16 // '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$', 17 - // path: './test/spec/3.1.x/full.json', 18 - path: './test/spec/v3-transforms.json', 17 + path: './test/spec/3.1.x/full.json', 18 + // path: './test/spec/v3-transforms.json', 19 19 // path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json', 20 20 // path: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml', 21 21 }, ··· 66 66 // name: '@tanstack/vue-query', 67 67 }, 68 68 { 69 - // name: 'zod', 69 + name: 'zod', 70 70 }, 71 71 ], 72 72 // useOptions: false,