From fd4f1c805480ccdf6c19318eaf3f498046ff73b9 Mon Sep 17 00:00:00 2001 From: TMoody <137668311+TheMartianMaker@users.noreply.github.com> Date: Sun, 1 Dec 2024 03:39:11 +0000 Subject: [PATCH 1/3] added directory for prototyping --- TemporaryTest/openapi-config.ts | 13 +++++++++++++ TemporaryTest/package.json | 14 ++++++++++++++ TemporaryTest/src/emptyApi.ts | 7 +++++++ TemporaryTest/store.ts | 0 4 files changed, 34 insertions(+) create mode 100644 TemporaryTest/openapi-config.ts create mode 100644 TemporaryTest/package.json create mode 100644 TemporaryTest/src/emptyApi.ts create mode 100644 TemporaryTest/store.ts diff --git a/TemporaryTest/openapi-config.ts b/TemporaryTest/openapi-config.ts new file mode 100644 index 0000000000..e00756bbbd --- /dev/null +++ b/TemporaryTest/openapi-config.ts @@ -0,0 +1,13 @@ +openapi-config.ts +import type { ConfigFile } from '@rtk-query/codegen-openapi' + +const config: ConfigFile = { + schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json', + apiFile: './src/emptyApi.ts', + apiImport: 'emptySplitApi', + outputFile: './src/store/petApi.ts', + exportName: 'petApi', + hooks: true, +} + +export default config \ No newline at end of file diff --git a/TemporaryTest/package.json b/TemporaryTest/package.json new file mode 100644 index 0000000000..9726585a7d --- /dev/null +++ b/TemporaryTest/package.json @@ -0,0 +1,14 @@ +{ + "name": "testproject", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "@reduxjs/toolkit": "^2.3.0" + } +} diff --git a/TemporaryTest/src/emptyApi.ts b/TemporaryTest/src/emptyApi.ts new file mode 100644 index 0000000000..105c7d6de4 --- /dev/null +++ b/TemporaryTest/src/emptyApi.ts @@ -0,0 +1,7 @@ +import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' + +// initialize an empty api service that we'll inject endpoints into later as needed +export const emptySplitApi = createApi({ + baseQuery: fetchBaseQuery({ baseUrl: '/' }), + endpoints: () => ({}), +}) \ No newline at end of file diff --git a/TemporaryTest/store.ts b/TemporaryTest/store.ts new file mode 100644 index 0000000000..e69de29bb2 From b3d176428657de62b043cf6ceece19e2d65d237b Mon Sep 17 00:00:00 2001 From: TMoody <137668311+TheMartianMaker@users.noreply.github.com> Date: Sun, 1 Dec 2024 20:34:17 +0000 Subject: [PATCH 2/3] Added a config property to toggle extensions on generated files based on expected compiled output extension of the file referenced in the apifile property. --- .../rtk-query-codegen-openapi/src/generate.ts | 15 ++++++++++++++- packages/rtk-query-codegen-openapi/src/types.ts | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/rtk-query-codegen-openapi/src/generate.ts b/packages/rtk-query-codegen-openapi/src/generate.ts index d128d100b4..0b147b9a9d 100644 --- a/packages/rtk-query-codegen-openapi/src/generate.ts +++ b/packages/rtk-query-codegen-openapi/src/generate.ts @@ -117,6 +117,7 @@ export async function generateApi( useEnumType = false, mergeReadWriteOnly = false, httpResolverOptions, + ESMExtensions = false }: GenerationOptions ) { const v3Doc = (v3DocCache[spec] ??= await getV3Doc(spec, httpResolverOptions)); @@ -161,7 +162,19 @@ export async function generateApi( if (!apiFile.startsWith('.')) apiFile = `./${apiFile}`; } } - apiFile = apiFile.replace(/\.[jt]sx?$/, ''); + + if(ESMExtensions === true){ + apiFile = apiFile.replace(/\.js$|\.ts$|\.mjs$|\.mts$|\.jsx$|\.tsx$/, (extension) => { + if (extension ==='.js') return '.js'; + if (extension ==='.ts') return '.js'; + if (extension ==='.mjs') return '.mjs'; + if (extension ==='.mts') return '.mjs'; + if (extension ==='.jsx') return '.jsx'; + if (extension ==='.tsx') return '.jsx'; + return apiFile; + }); +}; +if(!ESMExtensions || ESMExtensions === false){apiFile = apiFile.replace(/\.[jt]sx?$/, '');}; return printer.printNode( ts.EmitHint.Unspecified, diff --git a/packages/rtk-query-codegen-openapi/src/types.ts b/packages/rtk-query-codegen-openapi/src/types.ts index 437e058087..c49383edda 100644 --- a/packages/rtk-query-codegen-openapi/src/types.ts +++ b/packages/rtk-query-codegen-openapi/src/types.ts @@ -111,6 +111,11 @@ export interface CommonOptions { * resolution mechanism will be used. */ prettierConfigFile?: string; + /** + * default to false + * Will generate imports with file extension matching the expected compiled output of the api file + */ + ESMExtensions?:boolean; } export type TextMatcher = string | RegExp | (string | RegExp)[]; From 5699edc1ed8eb0e65656e9e9af180d0f3d4c3c9d Mon Sep 17 00:00:00 2001 From: TMoody <137668311+TheMartianMaker@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:51:06 +0000 Subject: [PATCH 3/3] removed user created generation testing folder --- TemporaryTest/openapi-config.ts | 13 ------------- TemporaryTest/package.json | 14 -------------- TemporaryTest/src/emptyApi.ts | 7 ------- 3 files changed, 34 deletions(-) delete mode 100644 TemporaryTest/openapi-config.ts delete mode 100644 TemporaryTest/package.json delete mode 100644 TemporaryTest/src/emptyApi.ts diff --git a/TemporaryTest/openapi-config.ts b/TemporaryTest/openapi-config.ts deleted file mode 100644 index e00756bbbd..0000000000 --- a/TemporaryTest/openapi-config.ts +++ /dev/null @@ -1,13 +0,0 @@ -openapi-config.ts -import type { ConfigFile } from '@rtk-query/codegen-openapi' - -const config: ConfigFile = { - schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json', - apiFile: './src/emptyApi.ts', - apiImport: 'emptySplitApi', - outputFile: './src/store/petApi.ts', - exportName: 'petApi', - hooks: true, -} - -export default config \ No newline at end of file diff --git a/TemporaryTest/package.json b/TemporaryTest/package.json deleted file mode 100644 index 9726585a7d..0000000000 --- a/TemporaryTest/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "testproject", - "version": "1.0.0", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "description": "", - "dependencies": { - "@reduxjs/toolkit": "^2.3.0" - } -} diff --git a/TemporaryTest/src/emptyApi.ts b/TemporaryTest/src/emptyApi.ts deleted file mode 100644 index 105c7d6de4..0000000000 --- a/TemporaryTest/src/emptyApi.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' - -// initialize an empty api service that we'll inject endpoints into later as needed -export const emptySplitApi = createApi({ - baseQuery: fetchBaseQuery({ baseUrl: '/' }), - endpoints: () => ({}), -}) \ No newline at end of file