diff --git a/example/todo-list/astro.config.mjs b/example/todo-list/astro.config.mjs index 00da8cf..3fb1a4a 100644 --- a/example/todo-list/astro.config.mjs +++ b/example/todo-list/astro.config.mjs @@ -14,8 +14,8 @@ export default defineConfig({ apiClientGenerator({ // example for CodeSandbox site: process.env.IS_SANDBOX - ? `https://${hostname()}--3000.local-credentialless.webcontainer.io` - : 'http://localhost:3000', + ? `https://${hostname()}--4321.local-credentialless.webcontainer.io` + : 'http://localhost:4321', }), ], }) diff --git a/example/todo-list/package.json b/example/todo-list/package.json index bb4edc0..58e3536 100644 --- a/example/todo-list/package.json +++ b/example/todo-list/package.json @@ -12,6 +12,6 @@ "dependencies": { "astro": "^4.1.2", "@astrojs/node": "^7.0.4", - "@jsheaven/astro-client-generator": "1.0.10" + "@jsheaven/astro-client-generator": "1.1.0" } } diff --git a/example/todo-list/src/pages/api-client/create-todo-client.ts b/example/todo-list/src/pages/api-client/create-todo-client.ts index c99eb6e..e44eb7c 100644 --- a/example/todo-list/src/pages/api-client/create-todo-client.ts +++ b/example/todo-list/src/pages/api-client/create-todo-client.ts @@ -1,5 +1,5 @@ -import { APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile, writeFile } from 'fs/promises' @@ -25,7 +25,7 @@ export interface ApiResponse { /** return (await fetch('/api/create-todo', { method: 'POST', ... })).json() */ export const createTodo = async(payload: ApiRequest, options: RequestOptions = {}): Promise => { - let requestUrl = 'http://localhost:3000/api/create-todo' + let requestUrl = 'http://localhost:4321/api/create-todo' if (options && options.query) { requestUrl += '?' + Object.keys(options.query) .map((key) => key + '=' + options.query![key]) diff --git a/example/todo-list/src/pages/api-client/get-todos-client.ts b/example/todo-list/src/pages/api-client/get-todos-client.ts index 6b68244..cf3a39d 100644 --- a/example/todo-list/src/pages/api-client/get-todos-client.ts +++ b/example/todo-list/src/pages/api-client/get-todos-client.ts @@ -1,5 +1,5 @@ -import { APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile } from 'fs/promises' @@ -25,7 +25,7 @@ export interface ApiResponse { /** return (await fetch('/api/get-todos', { method: 'GET', ... })).json() */ export const getTodos = async(options: RequestOptions = {}): Promise => { - let requestUrl = 'http://localhost:3000/api/get-todos' + let requestUrl = 'http://localhost:4321/api/get-todos' if (options && options.query) { requestUrl += '?' + Object.keys(options.query) .map((key) => key + '=' + options.query![key]) diff --git a/example/todo-list/src/pages/api-client/remove-todo-client.ts b/example/todo-list/src/pages/api-client/remove-todo-client.ts index 745508a..302e98d 100644 --- a/example/todo-list/src/pages/api-client/remove-todo-client.ts +++ b/example/todo-list/src/pages/api-client/remove-todo-client.ts @@ -1,5 +1,5 @@ -import { APIContext, APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIContext, type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile, writeFile } from 'fs/promises' @@ -27,7 +27,7 @@ export interface ApiResponse { /** return (await fetch('/api/remove-todo', { method: 'DELETE', ... })).json() */ export const removeTodo = async(payload: ApiRequest, options: RequestOptions = {}): Promise => { - let requestUrl = 'http://localhost:3000/api/remove-todo' + let requestUrl = 'http://localhost:4321/api/remove-todo' if (options && options.query) { requestUrl += '?' + Object.keys(options.query) .map((key) => key + '=' + options.query![key]) diff --git a/example/todo-list/src/pages/api-client/update-todo-client.ts b/example/todo-list/src/pages/api-client/update-todo-client.ts index d4c541f..02c716d 100644 --- a/example/todo-list/src/pages/api-client/update-todo-client.ts +++ b/example/todo-list/src/pages/api-client/update-todo-client.ts @@ -1,5 +1,5 @@ -import { APIContext, APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIContext, type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile, writeFile } from 'fs/promises' @@ -25,7 +25,7 @@ export interface ApiResponse { /** return (await fetch('/api/update-todo', { method: 'PATCH', ... })).json() */ export const updateTodo = async(payload: ApiRequest, options: RequestOptions = {}): Promise => { - let requestUrl = 'http://localhost:3000/api/update-todo' + let requestUrl = 'http://localhost:4321/api/update-todo' if (options && options.query) { requestUrl += '?' + Object.keys(options.query) .map((key) => key + '=' + options.query![key]) diff --git a/example/todo-list/src/pages/api/create-todo.ts b/example/todo-list/src/pages/api/create-todo.ts index 01ae055..5e433c9 100644 --- a/example/todo-list/src/pages/api/create-todo.ts +++ b/example/todo-list/src/pages/api/create-todo.ts @@ -1,5 +1,5 @@ -import { APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile, writeFile } from 'fs/promises' export interface ApiResponse { diff --git a/example/todo-list/src/pages/api/get-todos.ts b/example/todo-list/src/pages/api/get-todos.ts index 5ec9ecb..812f488 100644 --- a/example/todo-list/src/pages/api/get-todos.ts +++ b/example/todo-list/src/pages/api/get-todos.ts @@ -1,5 +1,5 @@ -import { APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile } from 'fs/promises' export interface ApiResponse { diff --git a/example/todo-list/src/pages/api/remove-todo.ts b/example/todo-list/src/pages/api/remove-todo.ts index 1c8b9c3..8dae1b3 100644 --- a/example/todo-list/src/pages/api/remove-todo.ts +++ b/example/todo-list/src/pages/api/remove-todo.ts @@ -1,5 +1,5 @@ -import { APIContext, APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIContext, type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile, writeFile } from 'fs/promises' export interface ApiResponse { diff --git a/example/todo-list/src/pages/api/update-todo.ts b/example/todo-list/src/pages/api/update-todo.ts index bba127f..63863af 100644 --- a/example/todo-list/src/pages/api/update-todo.ts +++ b/example/todo-list/src/pages/api/update-todo.ts @@ -1,5 +1,5 @@ -import { APIContext, APIRoute } from 'astro' -import { Todo } from '../../model/Todo' +import { type APIContext, type APIRoute } from 'astro' +import { type Todo } from '../../model/Todo' import { readFile, writeFile } from 'fs/promises' export interface ApiResponse { diff --git a/example/todo-list/yarn.lock b/example/todo-list/yarn.lock index f8ef9f9..20d667a 100644 --- a/example/todo-list/yarn.lock +++ b/example/todo-list/yarn.lock @@ -475,13 +475,13 @@ "@jridgewell/resolve-uri" "3.1.0" "@jridgewell/sourcemap-codec" "1.4.14" -"@jsheaven/astro-client-generator@1.0.10": - version "1.0.10" - resolved "https://registry.yarnpkg.com/@jsheaven/astro-client-generator/-/astro-client-generator-1.0.10.tgz#f77ce8bcdb1456adf5196837acc715a8540872cd" - integrity sha512-Cu45oRnmATj0Dx3gfzlfBACpLXT0yfypl7/0t1R2ARGBaC6bOqBZlsLWdWctQxageeb19P/eqk93GZ6vYpDw2Q== +"@jsheaven/astro-client-generator@1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@jsheaven/astro-client-generator/-/astro-client-generator-1.1.0.tgz#d27be4b1f6bb7672aa682a83f904f8f4f64b6409" + integrity sha512-1ts+ZibGWz6Jme4R8Rq3hgJypUhwWotZY4DxFKr4T6tkJ8w99RShrhBnJK21mWKAyMJBuOKMf8Xfvk3LllZFhg== dependencies: kleur "^4.1.5" - ts-morph "^17.0.1" + ts-morph "^21.0.1" yargs-parser "^21.1.1" "@nodelib/fs.scandir@2.1.5": @@ -570,14 +570,14 @@ resolved "https://registry.yarnpkg.com/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.9.4.tgz#16295ccae354707c9bc6842906bdeaad4f3ba7a5" integrity sha512-LfdGXCV9rdEify1oxlN9eamvDSjv9md9ZVMAbNHA87xqIfFCxImxan9qZ8+Un54iK2nnqPlbnSi4R54ONtbWBw== -"@ts-morph/common@~0.18.0": - version "0.18.1" - resolved "https://registry.yarnpkg.com/@ts-morph/common/-/common-0.18.1.tgz#ca40c3a62c3f9e17142e0af42633ad63efbae0ec" - integrity sha512-RVE+zSRICWRsfrkAw5qCAK+4ZH9kwEFv5h0+/YeHTLieWP7F4wWq4JsKFuNWG+fYh/KF+8rAtgdj5zb2mm+DVA== +"@ts-morph/common@~0.22.0": + version "0.22.0" + resolved "https://registry.yarnpkg.com/@ts-morph/common/-/common-0.22.0.tgz#8951d451622a26472fbc3a227d6c3a90e687a683" + integrity sha512-HqNBuV/oIlMKdkLshXd1zKBqNQCsuPEsgQOkfFQ/eUKjRlwndXW1AjN9LVkBEIukm00gGXSRmfkl0Wv5VXLnlw== dependencies: - fast-glob "^3.2.12" - minimatch "^5.1.0" - mkdirp "^1.0.4" + fast-glob "^3.3.2" + minimatch "^9.0.3" + mkdirp "^3.0.1" path-browserify "^1.0.1" "@types/babel__core@^7.20.4": @@ -1018,10 +1018,10 @@ clsx@^2.0.0: resolved "https://registry.yarnpkg.com/clsx/-/clsx-2.1.0.tgz#e851283bcb5c80ee7608db18487433f7b23f77cb" integrity sha512-m3iNNWpd9rl3jvvcBnu70ylMdrXt8Vlq4HYadnU5fwcOtvkSQWPmj7amUcDT2qYI7risszBjI5AUIUox9D16pg== -code-block-writer@^11.0.3: - version "11.0.3" - resolved "https://registry.yarnpkg.com/code-block-writer/-/code-block-writer-11.0.3.tgz#9eec2993edfb79bfae845fbc093758c0a0b73b76" - integrity sha512-NiujjUFB4SwScJq2bwbYUtXbZhBSlY6vYzm++3Q6oC+U+injTqfPYFK8wS9COOmb2lueqp0ZRB4nK1VYeHgNyw== +code-block-writer@^12.0.0: + version "12.0.0" + resolved "https://registry.yarnpkg.com/code-block-writer/-/code-block-writer-12.0.0.tgz#4dd58946eb4234105aff7f0035977b2afdc2a770" + integrity sha512-q4dMFMlXtKR3XNBHyMHt/3pwYNA69EDk00lloMOaaUMKPUXBw6lpXtbu3MMVG6/uOihGnRDOlkyqsONEUj60+w== color-convert@^1.9.0: version "1.9.3" @@ -1351,17 +1351,6 @@ fast-fifo@^1.1.0, fast-fifo@^1.2.0: resolved "https://registry.yarnpkg.com/fast-fifo/-/fast-fifo-1.3.2.tgz#286e31de96eb96d38a97899815740ba2a4f3640c" integrity sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ== -fast-glob@^3.2.12: - version "3.2.12" - resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.12.tgz#7f39ec99c2e6ab030337142da9e0c18f37afae80" - integrity sha512-DVj4CQIYYow0BlaelwK1pHl5n5cRSJfM60UA0zK891sVInoPri2Ekj7+e1CT3/3qxXenpI+nBBmQAcJPJgaj4w== - dependencies: - "@nodelib/fs.stat" "^2.0.2" - "@nodelib/fs.walk" "^1.2.3" - glob-parent "^5.1.2" - merge2 "^1.3.0" - micromatch "^4.0.4" - fast-glob@^3.3.2: version "3.3.2" resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129" @@ -2348,10 +2337,10 @@ mimic-response@^3.1.0: resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-3.1.0.tgz#2d1d59af9c1b129815accc2c46a022a5ce1fa3c9" integrity sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ== -minimatch@^5.1.0: - version "5.1.6" - resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.6.tgz#1cfcb8cf5522ea69952cd2af95ae09477f122a96" - integrity sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g== +minimatch@^9.0.3: + version "9.0.3" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825" + integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg== dependencies: brace-expansion "^2.0.1" @@ -2365,10 +2354,10 @@ mkdirp-classic@^0.5.2, mkdirp-classic@^0.5.3: resolved "https://registry.yarnpkg.com/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz#fa10c9115cc6d8865be221ba47ee9bed78601113" integrity sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A== -mkdirp@^1.0.4: - version "1.0.4" - resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" - integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== +mkdirp@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-3.0.1.tgz#e44e4c5607fb279c168241713cc6e0fea9adcb50" + integrity sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg== ms@2.0.0: version "2.0.0" @@ -3249,13 +3238,13 @@ trough@^2.0.0: resolved "https://registry.yarnpkg.com/trough/-/trough-2.1.0.tgz#0f7b511a4fde65a46f18477ab38849b22c554876" integrity sha512-AqTiAOLcj85xS7vQ8QkAV41hPDIJ71XJB4RCUrzo/1GM2CQwhkJGaf9Hgr7BOugMRpgGUrqRg/DrBDl4H40+8g== -ts-morph@^17.0.1: - version "17.0.1" - resolved "https://registry.yarnpkg.com/ts-morph/-/ts-morph-17.0.1.tgz#d85df4fcf9a1fcda1b331d52c00655f381c932d1" - integrity sha512-10PkHyXmrtsTvZSL+cqtJLTgFXkU43Gd0JCc0Rw6GchWbqKe0Rwgt1v3ouobTZwQzF1mGhDeAlWYBMGRV7y+3g== +ts-morph@^21.0.1: + version "21.0.1" + resolved "https://registry.yarnpkg.com/ts-morph/-/ts-morph-21.0.1.tgz#712302a0f6e9dbf1aa8d9cf33a4386c4b18c2006" + integrity sha512-dbDtVdEAncKctzrVZ+Nr7kHpHkv+0JDJb2MjjpBaj8bFeCkePU9rHfMklmhuLFnpeq/EJZk2IhStY6NzqgjOkg== dependencies: - "@ts-morph/common" "~0.18.0" - code-block-writer "^11.0.3" + "@ts-morph/common" "~0.22.0" + code-block-writer "^12.0.0" tsconfck@^3.0.0: version "3.0.1" diff --git a/package.json b/package.json index 343ccca..7c277b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jsheaven/astro-client-generator", - "version": "1.1.0", + "version": "1.1.1", "type": "module", "publishConfig": { "access": "public" diff --git a/src/index.ts b/src/index.ts index 06c0b5f..ae409ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -340,8 +340,29 @@ export const generateClientApis = (apiGeneratorOptions: ApiClientGeneratorOption const clientFileDir = parsed.dir const finalFilePath = `${clientFileDir}${sep}${parsed.name}-client.ts` + const project = new Project({ + tsConfigFilePath: apiGeneratorOptions.tsConfigPath, + }) + mkdirSync(clientFileDir, { recursive: true }) - writeFileSync(finalFilePath.toLowerCase(), clientCode, { encoding: 'utf-8' }) + + + // refactor + const sourceFile = project.createSourceFile(finalFilePath.toLowerCase(), clientCode, { + overwrite: true, + }) + + let lastWidth: number; + do { + lastWidth = sourceFile.getFullWidth(); + sourceFile.fixUnusedIdentifiers(); + } while (lastWidth !== sourceFile.getFullWidth()); + + sourceFile.organizeImports(); + sourceFile.formatText(); + sourceFile.saveSync(); + + //writeFileSync(finalFilePath.toLowerCase(), clientCode, { encoding: 'utf-8' }) }) }