@@ -5,9 +5,15 @@ const api = new Hono();
55async function GetReqJson ( request ) {
66 let data = { } ;
77 try {
8- data = ( await request . json ( ) ) || { } ;
8+ const contentType = request . headers . get ( 'Content-Type' ) || '' ;
9+ if ( contentType . includes ( 'application/json' ) ) {
10+ data = ( await request . json ( ) ) || { } ;
11+ } else if ( contentType . includes ( 'application/x-www-form-urlencoded' ) ) {
12+ const formData = await request . formData ( ) ;
13+ data = Object . fromEntries ( formData ) ;
14+ }
915 } catch ( e ) {
10- console . log ( e . message ) ;
16+ console . error ( 'Failed to parse request body:' , e . message ) ;
1117 }
1218 return data ;
1319}
@@ -19,12 +25,33 @@ export default class ControllerAPI {
1925 this . utils = utils ;
2026 }
2127
28+ // Response helper functions
29+ createResponse ( code , msg , data = null , status = 200 , headers = { } ) {
30+ return Response . json ( { code, msg, data } , { status, headers } ) ;
31+ }
32+
33+ createErrorResponse ( code , msg , status = 400 ) {
34+ return this . createResponse ( code , msg , null , status ) ;
35+ }
36+
37+ createSuccessResponse ( data = null , msg = 'Success' ) {
38+ return this . createResponse ( 0 , msg , data ) ;
39+ }
40+
41+ createCookieResponse ( code , msg , data , cookieName , cookieValue , expires = null ) {
42+ const cookieString = expires
43+ ? `${ cookieName } =${ cookieValue } ; path=/; expires=${ new Date ( expires ) . toUTCString ( ) } `
44+ : `${ cookieName } =${ cookieValue } ; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT` ;
45+
46+ return this . createResponse ( code , msg , data , 200 , { 'Set-Cookie' : cookieString } ) ;
47+ }
48+
2249 async Gateway ( ) {
2350 const { request } = this . utils ;
2451 const url = new URL ( request . url ) ;
2552 const action = String ( url . searchParams . get ( 'action' ) ) ;
2653 if ( ! this [ action ] ) {
27- return Response . json ( { code : 1000 , msg : 'Invalid action.' , data : null } , { status : 404 } ) ;
54+ return this . createErrorResponse ( 1000 , 'Invalid action.' , 404 ) ;
2855 }
2956 return await this [ action ] ( ) ;
3057 }
@@ -38,48 +65,31 @@ export default class ControllerAPI {
3865 const token = await SHA256 ( JSON . stringify ( [ Math . random ( ) , time ] ) ) ;
3966 const expires = time + 86400000 ;
4067 await STORE . put ( 'token' , JSON . stringify ( { token, expires } ) ) ;
41- return Response . json (
42- { code : 0 , msg : 'Success' , data : token } ,
43- {
44- headers : {
45- 'Set-Cookie' : `token=${ token } ; path=/; expires=${ new Date ( expires ) . toUTCString ( ) } ` ,
46- } ,
47- }
48- ) ;
68+ return this . createCookieResponse ( 0 , 'Success' , token , 'token' , token , expires ) ;
4969 }
50- return Response . json ( { code : 1001 , msg : 'Password error.' , data : null } , { status : 401 } ) ;
70+ return this . createErrorResponse ( 1001 , 'Password error.' , 401 ) ;
5171 }
5272
5373 // logout
5474 async logout ( ) {
5575 const { STORE } = this . utils ;
5676 await STORE . delete ( 'token' ) ;
57- return Response . json (
58- { code : 0 , msg : 'Success' , data : null } ,
59- {
60- headers : {
61- 'Set-Cookie' : `token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT` ,
62- } ,
63- }
64- ) ;
77+ return this . createCookieResponse ( 0 , 'Success' , null , 'token' , '' ) ;
6578 }
6679
6780 // generate a random slug
6881 async randomize ( ) {
69- return Response . json ( { code : 0 , msg : 'Success' , data : await this . utils . Slug ( ) } , { status : 200 } ) ;
82+ return this . createSuccessResponse ( await this . utils . Slug ( ) ) ;
7083 }
7184
7285 async check_slug ( ) {
7386 const { request } = this . utils ;
7487 const { slug } = await GetReqJson ( request ) ;
7588 const obj = await this . utils . ParseFirst ( slug ) ;
76- return Response . json (
77- {
78- code : obj . url ? 1040 : 0 ,
79- msg : obj . url ? 'Slug already exists.' : 'Success' ,
80- data : ! ! obj . url ,
81- } ,
82- { status : 200 }
89+ return this . createResponse (
90+ obj . url ? 1040 : 0 ,
91+ obj . url ? 'Slug already exists.' : 'Success' ,
92+ ! ! obj . url
8393 ) ;
8494 }
8595
@@ -90,8 +100,8 @@ export default class ControllerAPI {
90100 let { url, slug, creation } = body ;
91101
92102 // check if url is valid
93- if ( ! ( await CheckURL ( url ) ) ) {
94- return Response . json ( { code : 1050 , msg : 'Invalid URL.' , data : null } , { status : 400 } ) ;
103+ if ( ! CheckURL ( url ) ) {
104+ return this . createErrorResponse ( 1050 , 'Invalid URL.' ) ;
95105 }
96106
97107 // if slug is not provided, generate one
@@ -101,17 +111,17 @@ export default class ControllerAPI {
101111 // check if slug already exists
102112 const { url : existed } = await this . utils . ParseFirst ( slug ) ;
103113 if ( existed ) {
104- return Response . json ( { code : 1051 , msg : 'Slug already exists.' , data : null } , { status : 400 } ) ;
114+ return this . createErrorResponse ( 1051 , 'Slug already exists.' ) ;
105115 }
106116 }
107117
108118 // save url
109119 const { success, meta : details } = await STORE . put ( slug , JSON . stringify ( body ) ) ;
110- return Response . json ( {
111- code : 0 ,
112- msg : success ? 'Success' : JSON . stringify ( details ) ,
113- data : success ? slug : null ,
114- } ) ;
120+ return this . createResponse (
121+ success ? 0 : 1052 ,
122+ success ? 'Success' : JSON . stringify ( details ) ,
123+ success ? slug : null
124+ ) ;
115125 }
116126
117127 // get all slugs
@@ -150,11 +160,11 @@ export default class ControllerAPI {
150160 }
151161
152162 // 直接返回数据库中的结果,点击数已经通过Counter函数保持最新
153- return Response . json ( {
154- code : success ? 0 : 1052 ,
155- msg : 'Success' ,
156- data : { results : filteredResults , count : filteredCount , rows, page } ,
157- } ) ;
163+ return this . createResponse (
164+ success ? 0 : 1052 ,
165+ 'Success' ,
166+ { results : filteredResults , count : filteredCount , rows, page }
167+ ) ;
158168 }
159169
160170 // delete a slug
@@ -163,11 +173,11 @@ export default class ControllerAPI {
163173 let body = await GetReqJson ( request ) ;
164174 let { slug } = body ;
165175 const { success, meta : details } = await STORE . delete ( slug ) ;
166- return Response . json ( {
167- code : success ? 0 : 1060 ,
168- msg : success ? 'Success' : JSON . stringify ( details ) ,
169- data : null ,
170- } ) ;
176+ return this . createResponse (
177+ success ? 0 : 1060 ,
178+ success ? 'Success' : JSON . stringify ( details ) ,
179+ null
180+ ) ;
171181 }
172182}
173183
0 commit comments