Skip to content

Commit

Permalink
fix: example app
Browse files Browse the repository at this point in the history
  • Loading branch information
kyr0 committed Jan 12, 2024
1 parent 6a0af5f commit 5365534
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 98 deletions.
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.1.0"
"@jsheaven/astro-client-generator": "1.1.1"
}
}
3 changes: 3 additions & 0 deletions example/todo-list/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import { ViewTransitions } from 'astro:transitions'
export interface Props {
title: string
}
Expand All @@ -9,6 +11,7 @@ const { title } = Astro.props
<!DOCTYPE html>
<html lang="en">
<head>
<ViewTransitions />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
Expand Down
40 changes: 19 additions & 21 deletions example/todo-list/src/pages/api-client/create-todo-client.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import { type APIRoute } from 'astro'
import { type Todo } from '../../model/Todo'
import { readFile, writeFile } from 'fs/promises'





export interface QueryMap {
[key: string]: string
[key: string]: string
}

export interface RequestOptions extends RequestInit {
query?: QueryMap
query?: QueryMap
}

export interface ApiRequest extends Partial<Todo> {}
export interface ApiRequest extends Partial<Todo> { }

export interface ApiResponse {
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
}


/** return (await fetch('/api/create-todo', { method: 'POST', ... })).json() */
export const createTodo = async(payload: ApiRequest, options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/create-todo'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'POST'
options.body = JSON.stringify(payload)
return (await fetch(requestUrl, options)).json()
}
export const createTodo = async (payload: ApiRequest, options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/create-todo'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'POST'
options.body = JSON.stringify(payload)
return (await fetch(requestUrl, options)).json()
}
38 changes: 18 additions & 20 deletions example/todo-list/src/pages/api-client/get-todos-client.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import { type APIRoute } from 'astro'
import { type Todo } from '../../model/Todo'
import { readFile } from 'fs/promises'





export interface QueryMap {
[key: string]: string
[key: string]: string
}

export interface RequestOptions extends RequestInit {
query?: QueryMap
query?: QueryMap
}



export interface ApiResponse {
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
}


/** return (await fetch('/api/get-todos', { method: 'GET', ... })).json() */
export const getTodos = async(options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/get-todos'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'GET'
return (await fetch(requestUrl, options)).json()
}
export const getTodos = async (options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/get-todos'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'GET'

return (await fetch(requestUrl, options)).json()
}
40 changes: 19 additions & 21 deletions example/todo-list/src/pages/api-client/remove-todo-client.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
import { type APIContext, type APIRoute } from 'astro'
import { type Todo } from '../../model/Todo'
import { readFile, writeFile } from 'fs/promises'





export interface QueryMap {
[key: string]: string
[key: string]: string
}

export interface RequestOptions extends RequestInit {
query?: QueryMap
query?: QueryMap
}

export interface ApiRequest {
id: number
id: number
}

export interface ApiResponse {
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
}


/** return (await fetch('/api/remove-todo', { method: 'DELETE', ... })).json() */
export const removeTodo = async(payload: ApiRequest, options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/remove-todo'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'DELETE'
options.body = JSON.stringify(payload)
return (await fetch(requestUrl, options)).json()
}
export const removeTodo = async (payload: ApiRequest, options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/remove-todo'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'DELETE'
options.body = JSON.stringify(payload)
return (await fetch(requestUrl, options)).json()
}
40 changes: 19 additions & 21 deletions example/todo-list/src/pages/api-client/update-todo-client.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import { type APIContext, type APIRoute } from 'astro'
import { type Todo } from '../../model/Todo'
import { readFile, writeFile } from 'fs/promises'





export interface QueryMap {
[key: string]: string
[key: string]: string
}

export interface RequestOptions extends RequestInit {
query?: QueryMap
query?: QueryMap
}

export interface ApiRequest extends Partial<Todo> {}
export interface ApiRequest extends Partial<Todo> { }

export interface ApiResponse {
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
status: 'SUCCESS' | 'ERROR'
error?: string
todos: Array<Todo>
}


/** return (await fetch('/api/update-todo', { method: 'PATCH', ... })).json() */
export const updateTodo = async(payload: ApiRequest, options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/update-todo'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'PATCH'
options.body = JSON.stringify(payload)
return (await fetch(requestUrl, options)).json()
}
export const updateTodo = async (payload: ApiRequest, options: RequestOptions = {}): Promise<ApiResponse> => {
let requestUrl = 'http://localhost:4321/api/update-todo'
if (options && options.query) {
requestUrl += '?' + Object.keys(options.query)
.map((key) => key + '=' + options.query![key])
.join('&');
}
delete options.query
options.method = 'PATCH'
options.body = JSON.stringify(payload)
return (await fetch(requestUrl, options)).json()
}
18 changes: 9 additions & 9 deletions example/todo-list/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---
import Layout from '../layouts/Layout.astro'
import { readFileSync } from "fs"
// load DB
const currentTodos = (await import('../../todos.json')).default
const currentTodos = JSON.parse(readFileSync('./todos.json', 'utf-8'))
---

<Layout title="Welcome to Astro.">
Expand Down Expand Up @@ -41,23 +43,21 @@ const currentTodos = (await import('../../todos.json')).default
</p>

<script>
console.log('???')
const { createTodo } = await import('./api-client/create-todo-client')
const { removeTodo } = await import('./api-client/remove-todo-client')
const { updateTodo } = await import('./api-client/update-todo-client')
const { createTodo } = await import(/* @vite-ignore */ './api-client/create-todo-client')
const { removeTodo } = await import(/* @vite-ignore */ './api-client/remove-todo-client')
const { updateTodo } = await import(/* @vite-ignore */ './api-client/update-todo-client')

// === add todo
const addBtn = document.querySelector('#add-todo-button') as HTMLButtonElement
const addInput = document.querySelector('#add-todo-input') as HTMLInputElement

console.log(addBtn)
const onAddTodo = async () => {
console.log('asdasd')
await createTodo({
isDone: false,
task: addInput.value,
})
document.location.reload()
//document.location.reload()
}

addBtn.onclick = () => {
Expand All @@ -83,7 +83,7 @@ const currentTodos = (await import('../../todos.json')).default
todo.isDone = checkbox.checked

await updateTodo(todo)
document.location.reload()
//document.location.reload()
}),
)

Expand All @@ -99,7 +99,7 @@ const currentTodos = (await import('../../todos.json')).default
await removeTodo({
id: todo.id,
})
document.location.reload()
//document.location.reload()
}),
)
</script>
Expand Down
2 changes: 1 addition & 1 deletion example/todo-list/todos.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"id":1,"task":"Implement the code generator","isDone":false},{"id":2,"task":"Reach 100% code coverage with Jest unit tests","isDone":false},{"id":3,"task":"Make some devs out there happy","isDone":false},{"id":4,"task":"Practice acceptance for when haters gonna hate :)","isDone":false}]
[{"id":1,"task":"Implement the code generator","isDone":true},{"id":2,"task":"Reach 100% code coverage with Jest unit tests","isDone":true},{"id":3,"task":"Make some devs out there happy","isDone":true},{"id":4,"task":"Practice acceptance for when haters gonna hate :)","isDone":true},{"id":5,"task":"asd","isDone":false}]
8 changes: 4 additions & 4 deletions example/todo-list/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,10 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"

"@jsheaven/[email protected].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==
"@jsheaven/[email protected].1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@jsheaven/astro-client-generator/-/astro-client-generator-1.1.1.tgz#30da9b48c00fbd1cc2a70b51bdaff1be78eecd1c"
integrity sha512-ChCYiOg/m2ZC0QEAxtzbPIEvMOShBiPqij7ERHJ1ED0J4jiHRj1/nLVouAI96zbXbf3fdB0d2WPKr5QH39uMZg==
dependencies:
kleur "^4.1.5"
ts-morph "^21.0.1"
Expand Down

0 comments on commit 5365534

Please sign in to comment.