From 41e10e49f5c0ba185539167e4c3ed52d03534bb6 Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 5 Oct 2022 00:19:44 +0800 Subject: [PATCH] geoip: fix service register --- packages/geoip/index.ts | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/geoip/index.ts b/packages/geoip/index.ts index 165073318..d581650db 100644 --- a/packages/geoip/index.ts +++ b/packages/geoip/index.ts @@ -13,22 +13,24 @@ export interface Result { display: string } -export function apply() { - Context.service('geoip', class GeoIPService extends Service { - provider = 'MaxMind'; - lookup(ip: string, locale: string): Result { - const res: any = reader.get(ip); - if (!res) return { display: 'Unknown address'.translate(locale) }; - const ret: Result = { display: '' }; - if (res.location) ret.location = res.location; - if (res.continent) ret.continent = res.continent.names[locale] || res.continent.names.en; - if (res.country || res.registered_country) { - ret.country = (res.country || res.registered_country).names[locale] - || (res.country || res.registered_country).names.en; - } - if (res.city) ret.city = res.city.names[locale] || res.city.names.en; - ret.display = `${ret.continent} ${ret.country}${ret.city ? ` ${ret.city}` : ''}`; - return ret; +class GeoIPService extends Service { + provider = 'MaxMind'; + lookup(ip: string, locale: string): Result { + const res: any = reader.get(ip); + if (!res) return { display: 'Unknown address'.translate(locale) }; + const ret: Result = { display: '' }; + if (res.location) ret.location = res.location; + if (res.continent) ret.continent = res.continent.names[locale] || res.continent.names.en; + if (res.country || res.registered_country) { + ret.country = (res.country || res.registered_country).names[locale] + || (res.country || res.registered_country).names.en; } - }); + if (res.city) ret.city = res.city.names[locale] || res.city.names.en; + ret.display = `${ret.continent} ${ret.country}${ret.city ? ` ${ret.city}` : ''}`; + return ret; + } +} +export function apply(ctx: Context) { + Context.service('geoip', GeoIPService); + ctx.geoip = new GeoIPService(ctx, 'geoip', true); }