diff --git a/packages/api/router/geo/index.ts b/packages/api/router/geo/index.ts index 96494c135d..af1ece1517 100644 --- a/packages/api/router/geo/index.ts +++ b/packages/api/router/geo/index.ts @@ -1,25 +1,24 @@ -import { defineRouter, publicProcedure } from '~api/lib/trpc' +import { defineRouter, importHandler, publicProcedure } from '~api/lib/trpc' import * as schema from './schemas' -const HandlerCache: Partial = {} +const NAMESPACE = 'geo' -type GeoHandlerCache = { - autocomplete: typeof import('./query.autocomplete.handler').autocomplete - geoByPlaceId: typeof import('./query.geoByPlaceId.handler').geoByPlaceId -} +const namespaced = (s: string) => `${NAMESPACE}.${s}` export const geoRouter = defineRouter({ - autocomplete: publicProcedure.input(schema.ZAutocompleteSchema).query(async ({ ctx, input }) => { - if (!HandlerCache.autocomplete) - HandlerCache.autocomplete = await import('./query.autocomplete.handler').then((mod) => mod.autocomplete) - if (!HandlerCache.autocomplete) throw new Error('Failed to load handler') - return HandlerCache.autocomplete({ ctx, input }) + autocomplete: publicProcedure.input(schema.ZAutocompleteSchema).query(async (opts) => { + const handler = await importHandler( + namespaced('autocomplete'), + () => import('./query.autocomplete.handler') + ) + return handler(opts) }), - geoByPlaceId: publicProcedure.input(schema.ZGeoByPlaceIdSchema).query(async ({ ctx, input }) => { - if (!HandlerCache.geoByPlaceId) - HandlerCache.geoByPlaceId = await import('./query.geoByPlaceId.handler').then((mod) => mod.geoByPlaceId) - if (!HandlerCache.geoByPlaceId) throw new Error('Failed to load handler') - return HandlerCache.geoByPlaceId({ ctx, input }) + geoByPlaceId: publicProcedure.input(schema.ZGeoByPlaceIdSchema).query(async (opts) => { + const handler = await importHandler( + namespaced('geoByPlaceId'), + () => import('./query.geoByPlaceId.handler') + ) + return handler(opts) }), }) diff --git a/packages/api/router/geo/query.autocomplete.handler.ts b/packages/api/router/geo/query.autocomplete.handler.ts index 67cdf155c0..226489dc5c 100644 --- a/packages/api/router/geo/query.autocomplete.handler.ts +++ b/packages/api/router/geo/query.autocomplete.handler.ts @@ -36,3 +36,4 @@ export const autocomplete = async ({ input }: TRPCHandlerParams = {} - -type MiscHandlerCache = { - hasContactInfo: typeof import('./query.hasContactInfo.handler').hasContactInfo - getCountryTranslation: typeof import('./query.getCountryTranslation.handler').getCountryTranslation - forEditNavbar: typeof import('./query.forEditNavbar.handler').forEditNavbar -} +const NAMESPACE = 'misc' +const namespaced = (s: string) => `${NAMESPACE}.${s}` export const miscRouter = defineRouter({ - hasContactInfo: publicProcedure.input(schema.ZHasContactInfoSchema).query(async ({ ctx, input }) => { - if (!HandlerCache.hasContactInfo) - HandlerCache.hasContactInfo = await import('./query.hasContactInfo.handler').then( - (mod) => mod.hasContactInfo - ) - - if (!HandlerCache.hasContactInfo) throw new Error('Failed to load handler') - return HandlerCache.hasContactInfo({ ctx, input }) + hasContactInfo: publicProcedure.input(schema.ZHasContactInfoSchema).query(async (opts) => { + const handler = await importHandler( + namespaced('hasContactInfo'), + () => import('./query.hasContactInfo.handler') + ) + return handler(opts) + }), + getCountryTranslation: publicProcedure.input(schema.ZGetCountryTranslationSchema).query(async (opts) => { + const handler = await importHandler( + namespaced('getCountryTranslation'), + () => import('./query.getCountryTranslation.handler') + ) + return handler(opts) }), - getCountryTranslation: publicProcedure - .input(schema.ZGetCountryTranslationSchema) - .query(async ({ ctx, input }) => { - if (!HandlerCache.getCountryTranslation) - HandlerCache.getCountryTranslation = await import('./query.getCountryTranslation.handler').then( - (mod) => mod.getCountryTranslation - ) - - if (!HandlerCache.getCountryTranslation) throw new Error('Failed to load handler') - return HandlerCache.getCountryTranslation({ ctx, input }) - }), forEditNavbar: permissionedProcedure('updateLocation') .input(schema.ZForEditNavbarSchema) - .query(async ({ ctx, input }) => { - if (!HandlerCache.forEditNavbar) - HandlerCache.forEditNavbar = await import('./query.forEditNavbar.handler').then( - (mod) => mod.forEditNavbar - ) - - if (!HandlerCache.forEditNavbar) throw new Error('Failed to load handler') - return HandlerCache.forEditNavbar({ ctx, input }) + .query(async (opts) => { + const handler = await importHandler( + namespaced('forEditNavbar'), + () => import('./query.forEditNavbar.handler') + ) + return handler(opts) }), }) diff --git a/packages/api/router/misc/query.forEditNavbar.handler.ts b/packages/api/router/misc/query.forEditNavbar.handler.ts index a52bae3b14..09d77cf512 100644 --- a/packages/api/router/misc/query.forEditNavbar.handler.ts +++ b/packages/api/router/misc/query.forEditNavbar.handler.ts @@ -22,3 +22,4 @@ export const forEditNavbar = async ({ input }: TRPCHandlerParams