File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <template >
2+ <ServiceBase v-bind =" props " >
3+ <template v-if =" props .options ?.flagIcon !== false " #icon =" { service } " >
4+ <ServiceBaseIcon :name =" ` flag:${service ?.data ?.country }-1x1 ` " />
5+ </template >
6+ <template #title =" { service } " >
7+ {{ service?.data?.ip || '' }}
8+ </template >
9+ <template #description =" { service } " >
10+ {{ service?.data?.place || '' }}
11+ </template >
12+ </ServiceBase >
13+ </template >
14+
15+ <script setup lang="ts">
16+ import type { IpApiService , ServiceClient } from ' ~/types'
17+
18+ const props = defineProps <ServiceClient <IpApiService >>()
19+ </script >
Original file line number Diff line number Diff line change 1+ import type { IpApiService } from '~/types'
2+ import { getServiceWithDefaultData , returnServiceWithData } from '~/server/utils/services'
3+
4+ interface IpApiFetchResponse {
5+ status : string
6+ country : string
7+ countryCode : string
8+ region : string
9+ regionName : string
10+ city : string
11+ zip : string
12+ lat : number
13+ lon : number
14+ timezone : string
15+ isp : string
16+ org : string
17+ as : string
18+ query : string
19+ }
20+
21+ const cachedIpApiData = defineCachedFunction ( async ( lang : string = 'en' ) => {
22+ const response = await $fetch < IpApiFetchResponse > ( `http://ip-api.com/json/?lang=${ lang } ` )
23+
24+ return {
25+ ip : response . query ,
26+ place : `${ response . city } , ${ response . regionName } ` ,
27+ country : response . countryCode . toLowerCase ( ) ,
28+ }
29+ } , { maxAge : 60 * 24 } )
30+
31+ export default defineEventHandler ( async ( event ) => {
32+ const service = await getServiceWithDefaultData < IpApiService > ( event )
33+ const config = await getLocalConfig ( )
34+ const ip = await cachedIpApiData ( config ?. lang )
35+
36+ return returnServiceWithData ( service , ip )
37+ } )
Original file line number Diff line number Diff line change @@ -42,3 +42,13 @@ export interface ServiceWithDefaultData<S> {
4242export type ReturnServiceWithData < D , S extends ServiceWithDefaultData < Service > [ 'defaultData' ] > = S & { data : D }
4343
4444export interface BaseService extends Service { }
45+
46+ export interface IpApiService extends Service {
47+ options ?: {
48+ flagIcon ?: boolean
49+ }
50+ server : {
51+ ip : string
52+ place : string
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments