@@ -61,61 +61,70 @@ const browse = async (query, searchFields) => {
61
61
} ;
62
62
63
63
/**
64
- * Register libraries routes .
64
+ * Handle GET / libraries requests .
65
65
*
66
- * @param {import('hono').Hono } app App instance.
66
+ * @param {import('hono').Context } ctx Request context.
67
+ * @return {Promise<Response> }
67
68
*/
68
- export default app => {
69
- app . get ( '/libraries' , async ctx => {
70
- // Get the index results
71
- const searchFields = queryArray ( ctx . req . queries ( 'search_fields' ) ) ;
72
- const results = await browse (
73
- ( ctx . req . query ( 'search' ) || '' ) . toString ( ) . slice ( 0 , maxQueryLength ) ,
74
- searchFields . includes ( '*' ) ? [ ] : searchFields ,
75
- ) ;
69
+ const handleGetLibraries = async ctx => {
70
+ // Get the index results
71
+ const searchFields = queryArray ( ctx . req . queries ( 'search_fields' ) ) ;
72
+ const results = await browse (
73
+ ( ctx . req . query ( 'search' ) || '' ) . toString ( ) . slice ( 0 , maxQueryLength ) ,
74
+ searchFields . includes ( '*' ) ? [ ] : searchFields ,
75
+ ) ;
76
76
77
- // Transform the results into our filtered array
78
- const requestedFields = queryArray ( ctx . req . queries ( 'fields' ) ) ;
79
- const response = results . filter ( hit => {
80
- if ( hit ?. name ) return true ;
81
- console . warn ( 'Found bad entry in Algolia data' ) ;
82
- console . info ( hit ) ;
83
- ctx . sentry ?. withScope ( scope => {
84
- scope . setExtra ( 'hit' , hit ) ;
85
- ctx . sentry . captureException ( new Error ( 'Bad entry in Algolia data' ) ) ;
86
- } ) ;
87
- return false ;
88
- } ) . map ( hit => filter (
89
- {
90
- // Ensure name is first prop
91
- name : hit . name ,
92
- // Custom latest prop
93
- latest : hit . filename && hit . version ? 'https://cdnjs.cloudflare.com/ajax/libs/' + hit . name + '/' + hit . version + '/' + hit . filename : null ,
94
- // All other hit props
95
- ...hit ,
96
- } ,
97
- [
98
- // Always send back name & latest
99
- 'name' ,
100
- 'latest' ,
101
- // Send back whatever else was requested
102
- ...requestedFields ,
103
- ] ,
104
- requestedFields . includes ( '*' ) , // Send all if they have '*'
105
- ) ) ;
77
+ // Transform the results into our filtered array
78
+ const requestedFields = queryArray ( ctx . req . queries ( 'fields' ) ) ;
79
+ const response = results . filter ( hit => {
80
+ if ( hit ?. name ) return true ;
81
+ console . warn ( 'Found bad entry in Algolia data' ) ;
82
+ console . info ( hit ) ;
83
+ ctx . sentry ?. withScope ( scope => {
84
+ scope . setExtra ( 'hit' , hit ) ;
85
+ ctx . sentry . captureException ( new Error ( 'Bad entry in Algolia data' ) ) ;
86
+ } ) ;
87
+ return false ;
88
+ } ) . map ( hit => filter (
89
+ {
90
+ // Ensure name is first prop
91
+ name : hit . name ,
92
+ // Custom latest prop
93
+ latest : hit . filename && hit . version ? 'https://cdnjs.cloudflare.com/ajax/libs/' + hit . name + '/' + hit . version + '/' + hit . filename : null ,
94
+ // All other hit props
95
+ ...hit ,
96
+ } ,
97
+ [
98
+ // Always send back name & latest
99
+ 'name' ,
100
+ 'latest' ,
101
+ // Send back whatever else was requested
102
+ ...requestedFields ,
103
+ ] ,
104
+ requestedFields . includes ( '*' ) , // Send all if they have '*'
105
+ ) ) ;
106
106
107
- // If they want less data, allow that
108
- const limit = ctx . req . query ( 'limit' ) && Number ( ctx . req . query ( 'limit' ) ) ;
109
- const trimmed = limit ? response . slice ( 0 , limit ) : response ;
107
+ // If they want less data, allow that
108
+ const limit = ctx . req . query ( 'limit' ) && Number ( ctx . req . query ( 'limit' ) ) ;
109
+ const trimmed = limit ? response . slice ( 0 , limit ) : response ;
110
110
111
- // Set a 6 hour life on this response
112
- cache ( ctx , 6 * 60 * 60 ) ;
111
+ // Set a 6 hour life on this response
112
+ cache ( ctx , 6 * 60 * 60 ) ;
113
113
114
- // Send the response
115
- return respond ( ctx , {
116
- results : trimmed ,
117
- total : trimmed . length , // Total results we're sending back
118
- available : response . length , // Total number available without trimming
119
- } ) ;
114
+ // Send the response
115
+ return respond ( ctx , {
116
+ results : trimmed ,
117
+ total : trimmed . length , // Total results we're sending back
118
+ available : response . length , // Total number available without trimming
120
119
} ) ;
121
120
} ;
121
+
122
+ /**
123
+ * Register libraries routes.
124
+ *
125
+ * @param {import('hono').Hono } app App instance.
126
+ */
127
+ export default app => {
128
+ app . get ( '/libraries' , handleGetLibraries ) ;
129
+ app . get ( '/libraries/' , handleGetLibraries ) ;
130
+ } ;
0 commit comments