Skip to content

Commit

Permalink
feat: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kyr0 committed Jan 12, 2024
1 parent e229bb2 commit 6a0af5f
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 66 deletions.
4 changes: 2 additions & 2 deletions example/todo-list/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
],
})
2 changes: 1 addition & 1 deletion example/todo-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
6 changes: 3 additions & 3 deletions example/todo-list/src/pages/api-client/create-todo-client.ts
Original file line number Diff line number Diff line change
@@ -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'


Expand All @@ -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<ApiResponse> => {
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])
Expand Down
6 changes: 3 additions & 3 deletions example/todo-list/src/pages/api-client/get-todos-client.ts
Original file line number Diff line number Diff line change
@@ -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'


Expand All @@ -25,7 +25,7 @@ export interface ApiResponse {

/** return (await fetch('/api/get-todos', { method: 'GET', ... })).json() */
export const getTodos = async(options: RequestOptions = {}): Promise<ApiResponse> => {
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])
Expand Down
6 changes: 3 additions & 3 deletions example/todo-list/src/pages/api-client/remove-todo-client.ts
Original file line number Diff line number Diff line change
@@ -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'


Expand Down Expand Up @@ -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<ApiResponse> => {
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])
Expand Down
6 changes: 3 additions & 3 deletions example/todo-list/src/pages/api-client/update-todo-client.ts
Original file line number Diff line number Diff line change
@@ -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'


Expand All @@ -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<ApiResponse> => {
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])
Expand Down
4 changes: 2 additions & 2 deletions example/todo-list/src/pages/api/create-todo.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions example/todo-list/src/pages/api/get-todos.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions example/todo-list/src/pages/api/remove-todo.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions example/todo-list/src/pages/api/update-todo.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
71 changes: 30 additions & 41 deletions example/todo-list/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand All @@ -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==

[email protected]:
version "2.0.0"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsheaven/astro-client-generator",
"version": "1.1.0",
"version": "1.1.1",
"type": "module",
"publishConfig": {
"access": "public"
Expand Down
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
})
}

Expand Down

0 comments on commit 6a0af5f

Please sign in to comment.