Skip to content

Commit 08aa5d3

Browse files
committed
feat: Astro 4 support
1 parent 9b196e2 commit 08aa5d3

File tree

15 files changed

+2054
-1325
lines changed

15 files changed

+2054
-1325
lines changed

example/todo-list/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"astro": "astro"
1111
},
1212
"dependencies": {
13-
"astro": "^2.0.10",
14-
"@astrojs/node": "^5.0.3",
13+
"astro": "^4.1.2",
14+
"@astrojs/node": "^7.0.4",
1515
"@jsheaven/astro-client-generator": "1.0.10"
1616
}
1717
}

example/todo-list/src/pages/api/create-todo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface ApiResponse {
1010

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

13-
export const post: APIRoute = async ({ request }) => {
13+
export const POST: APIRoute = async ({ request }) => {
1414
let todos: Array<Todo> = []
1515

1616
const body: ApiRequest = await request.json()

example/todo-list/src/pages/api/get-todos.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ApiResponse {
88
todos: Array<Todo>
99
}
1010

11-
export const get: APIRoute = async ({ params, request, url }) => {
11+
export const GET: APIRoute = async ({ params, request, url }) => {
1212
let todos: Array<Todo> = []
1313

1414
// read from "DB"

example/todo-list/src/pages/api/remove-todo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface ApiRequest {
1212
id: number
1313
}
1414

15-
export const del: APIRoute = async ({ request }) => {
15+
export const DELETE: APIRoute = async ({ request }) => {
1616
let todos: Array<Todo> = []
1717
const body: ApiRequest = await request.json()
1818
try {

example/todo-list/src/pages/api/update-todo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface ApiResponse {
1010

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

13-
export const patch: APIRoute = async ({ request }) => {
13+
export const PATCH: APIRoute = async ({ request }) => {
1414
let todos: Array<Todo> = []
1515
const body: ApiRequest = await request.json()
1616
try {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jsheaven/astro-client-generator",
3-
"version": "1.0.10",
3+
"version": "1.1.0",
44
"type": "module",
55
"publishConfig": {
66
"access": "public"
@@ -55,7 +55,7 @@
5555
"devDependencies": {
5656
"@jsheaven/easybuild": "^1.1.4",
5757
"@types/jest": "^29.4.0",
58-
"astro": "*",
58+
"astro": "^4.1.2",
5959
"jest": "^29.4.2",
6060
"ts-jest": "^29.0.5",
6161
"typescript": "^4.9.5"

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const apiGeneratorOptionsDefaults: ApiClientGeneratorOptions = {
2929

3030
export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'PATCH' | 'HEAD' | 'PUT' | 'OPTIONS'
3131

32-
export const AstroHttpEndpointMethodNames = ['GET', 'POST', 'DEL', 'PATCH', 'HEAD', 'PUT', 'OPTIONS', 'ALL']
32+
export const AstroHttpEndpointMethodNames = ['GET', 'POST', 'DELETE', 'PATCH', 'HEAD', 'PUT', 'OPTIONS', 'ALL']
3333
export const HttpMethods: Array<HttpMethod> = ['POST', 'DELETE', 'GET', 'PATCH', 'HEAD', 'PUT', 'OPTIONS']
3434

3535
export type InterfacePosition = [number, number]
@@ -42,28 +42,28 @@ export const cleanupInterfce = (codeLines: Array<string>) => codeLines.join('\n'
4242
export const analyzeHttpMethodsImplemented = (code: string): Array<HttpMethod> => {
4343
const methods: Array<HttpMethod> = []
4444

45-
if (code.indexOf('function get') > -1 || code.indexOf('export const get') > -1) {
45+
if (code.indexOf('function GET') > -1 || code.indexOf('export const GET') > -1) {
4646
methods.push('GET')
4747
}
48-
if (code.indexOf('function post') > -1 || code.indexOf('export const post') > -1) {
48+
if (code.indexOf('function POST') > -1 || code.indexOf('export const POST') > -1) {
4949
methods.push('POST')
5050
}
51-
if (code.indexOf('function del') > -1 || code.indexOf('export const del') > -1) {
51+
if (code.indexOf('function DELETE') > -1 || code.indexOf('export const DELETE') > -1) {
5252
methods.push('DELETE')
5353
}
54-
if (code.indexOf('function patch') > -1 || code.indexOf('export const patch') > -1) {
54+
if (code.indexOf('function PATCH') > -1 || code.indexOf('export const PATCH') > -1) {
5555
methods.push('PATCH')
5656
}
57-
if (code.indexOf('function head') > -1 || code.indexOf('export const head') > -1) {
57+
if (code.indexOf('function HEAD') > -1 || code.indexOf('export const HEAD') > -1) {
5858
methods.push('HEAD')
5959
}
60-
if (code.indexOf('function put') > -1 || code.indexOf('export const put') > -1) {
60+
if (code.indexOf('function PUT') > -1 || code.indexOf('export const PUT') > -1) {
6161
methods.push('PUT')
6262
}
63-
if (code.indexOf('function options') > -1 || code.indexOf('export const options') > -1) {
63+
if (code.indexOf('function OPTIONS') > -1 || code.indexOf('export const OPTIONS') > -1) {
6464
methods.push('OPTIONS')
6565
}
66-
if (code.indexOf('function all') > -1 || code.indexOf('export const all') > -1) {
66+
if (code.indexOf('function ALL') > -1 || code.indexOf('export const ALL') > -1) {
6767
return HttpMethods
6868
}
6969
return methods
@@ -137,7 +137,7 @@ export const parseApiRoutesBaseline = (
137137
const methodIndex = AstroHttpEndpointMethodNames.indexOf(d.getName().toUpperCase())
138138
if (methodIndex > -1) {
139139
const astroMethod = AstroHttpEndpointMethodNames[methodIndex]
140-
if (astroMethod === 'DEL') {
140+
if (astroMethod === 'DELETE') {
141141
methods.push('DELETE')
142142
} else if (astroMethod === 'ALL') {
143143
methods = HttpMethods

test/astroClientGenerator.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,77 +112,77 @@ describe('astroClientGenerator', () => {
112112
jest.restoreAllMocks()
113113
})
114114
it('correctly analyzes the HTTP method implemented', () => {
115-
let code = 'function get(req, res) {}'
115+
let code = 'function GET(req, res) {}'
116116
let methods = analyzeHttpMethodsImplemented(code)
117117
expect(methods).toEqual(['GET'])
118118

119-
code = 'function post(req, res) {}'
119+
code = 'function POST(req, res) {}'
120120
methods = analyzeHttpMethodsImplemented(code)
121121
expect(methods).toEqual(['POST'])
122122

123-
code = 'function del(req, res) {}'
123+
code = 'function DELETE(req, res) {}'
124124
methods = analyzeHttpMethodsImplemented(code)
125125
expect(methods).toEqual(['DELETE'])
126126

127-
code = 'function patch(req, res) {}'
127+
code = 'function PATCH(req, res) {}'
128128
methods = analyzeHttpMethodsImplemented(code)
129129
expect(methods).toEqual(['PATCH'])
130130

131-
code = 'function head(req, res) {}'
131+
code = 'function HEAD(req, res) {}'
132132
methods = analyzeHttpMethodsImplemented(code)
133133
expect(methods).toEqual(['HEAD'])
134134

135-
code = 'function put(req, res) {}'
135+
code = 'function PUT(req, res) {}'
136136
methods = analyzeHttpMethodsImplemented(code)
137137
expect(methods).toEqual(['PUT'])
138138

139-
code = `function put(req, res) {}\nfunction del(req, res) {}`
139+
code = `function PUT(req, res) {}\nfunction DELETE(req, res) {}`
140140
methods = analyzeHttpMethodsImplemented(code)
141141
expect(methods).toEqual(['DELETE', 'PUT'])
142142

143-
code = 'function options(req, res) {}'
143+
code = 'function OPTIONS(req, res) {}'
144144
methods = analyzeHttpMethodsImplemented(code)
145145
expect(methods).toEqual(['OPTIONS'])
146146

147-
code = 'function all(req, res) {}'
147+
code = 'function ALL(req, res) {}'
148148
methods = analyzeHttpMethodsImplemented(code)
149149
expect(methods).toEqual(['POST', 'DELETE', 'GET', 'PATCH', 'HEAD', 'PUT', 'OPTIONS'])
150150
})
151151

152152
it('correctly analyzes the HTTP method implemented - ', () => {
153-
let code = 'function get(req, res) {}'
153+
let code = 'function GET(req, res) {}'
154154
let methods = analyzeHttpMethodsImplemented(code)
155155
expect(methods).toEqual(['GET'])
156156

157-
code = 'function post(req, res) {}'
157+
code = 'function POST(req, res) {}'
158158
methods = analyzeHttpMethodsImplemented(code)
159159
expect(methods).toEqual(['POST'])
160160

161-
code = 'function del(req, res) {}'
161+
code = 'function DELETE(req, res) {}'
162162
methods = analyzeHttpMethodsImplemented(code)
163163
expect(methods).toEqual(['DELETE'])
164164

165-
code = 'function patch(req, res) {}'
165+
code = 'function PATCH(req, res) {}'
166166
methods = analyzeHttpMethodsImplemented(code)
167167
expect(methods).toEqual(['PATCH'])
168168

169-
code = 'function head(req, res) {}'
169+
code = 'function HEAD(req, res) {}'
170170
methods = analyzeHttpMethodsImplemented(code)
171171
expect(methods).toEqual(['HEAD'])
172172

173-
code = 'function put(req, res) {}'
173+
code = 'function PUT(req, res) {}'
174174
methods = analyzeHttpMethodsImplemented(code)
175175
expect(methods).toEqual(['PUT'])
176176

177-
code = `function put(req, res) {}\nfunction del(req, res) {}`
177+
code = `function PUT(req, res) {}\nfunction DELETE(req, res) {}`
178178
methods = analyzeHttpMethodsImplemented(code)
179179
expect(methods).toEqual(['DELETE', 'PUT'])
180180

181-
code = 'function options(req, res) {}'
181+
code = 'function OPTIONS(req, res) {}'
182182
methods = analyzeHttpMethodsImplemented(code)
183183
expect(methods).toEqual(['OPTIONS'])
184184

185-
code = 'function all(req, res) {}'
185+
code = 'function ALL(req, res) {}'
186186
methods = analyzeHttpMethodsImplemented(code)
187187
expect(methods).toEqual(['POST', 'DELETE', 'GET', 'PATCH', 'HEAD', 'PUT', 'OPTIONS'])
188188
})

test/fixtures/pages/api/allRequest.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,24 @@ export interface ApiRequest {
2626
}
2727

2828
// the APIContext<ApiRequest> makes sure that the typing of props is precisely defined as to what is in POST body
29-
export const all: APIRoute = async ({ params, request, url, props }: APIContext<ApiRequest>) => {
29+
export const ALL: APIRoute = async ({ params, request, url, props }: APIContext<ApiRequest>) => {
3030
const { isLoggedIn } = await checkAuth(request)
3131

3232
// from ApiRequest
3333
console.log('pageId to delete', props.pageId)
3434

35-
return {
35+
return new Response(JSON.stringify({
36+
status: isLoggedIn ? 'SUCCESS' : 'FORBIDDEN',
37+
pages: [
38+
{
39+
title: 'FooBar',
40+
keywords: ['foo', 'bar'],
41+
},
42+
] as Array<Page>,
43+
}), {
3644
status: isLoggedIn ? 200 : 403,
37-
body: JSON.stringify({
38-
status: isLoggedIn ? 'SUCCESS' : 'FORBIDDEN',
39-
pages: [
40-
{
41-
title: 'FooBar',
42-
keywords: ['foo', 'bar'],
43-
},
44-
] as Array<Page>,
45-
}),
4645
headers: {
4746
'Content-Type': 'application/json',
4847
},
49-
}
48+
})
5049
}

test/fixtures/pages/api/alsoRequest.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,26 @@ export interface ApiRequest {
2626
}
2727

2828
// the APIContext<ApiRequest> makes sure that the typing of props is precisely defined as to what is in POST body
29-
export const post: APIRoute = async ({ params, request, url, props }: APIContext<ApiRequest>) => {
29+
export const POST: APIRoute = async ({ params, request, url, props }: APIContext<ApiRequest>) => {
3030
const { isLoggedIn } = await checkAuth(request)
3131

3232
// from ApiRequest
3333
console.log('props', props.pages)
3434

35-
return {
35+
return new Response(JSON.stringify({
36+
status: isLoggedIn ? 'SUCCESS' : 'FORBIDDEN',
37+
pages: [
38+
// echo back the pages we've received
39+
...props.pages,
40+
{
41+
title: 'FooBar',
42+
keywords: ['foo', 'bar'],
43+
},
44+
] as Array<Page>,
45+
}), {
3646
status: isLoggedIn ? 200 : 403,
37-
body: JSON.stringify({
38-
status: isLoggedIn ? 'SUCCESS' : 'FORBIDDEN',
39-
pages: [
40-
// echo back the pages we've received
41-
...props.pages,
42-
{
43-
title: 'FooBar',
44-
keywords: ['foo', 'bar'],
45-
},
46-
] as Array<Page>,
47-
}),
4847
headers: {
4948
'Content-Type': 'application/json',
5049
},
51-
}
50+
})
5251
}

0 commit comments

Comments
 (0)