/ src / bootstrap.ts
bootstrap.ts
 1  // Sets up app specific configurations
 2  import type { Opt } from '@jet/environment';
 3  import type { Intent } from '@jet/environment/dispatching';
 4  import type { ActionModel } from '@jet/environment/types/models';
 5  import { initializeUniqueIdContext } from '@amp/web-app-components/src/utils/uniqueId';
 6  import { setLocale as setSharedLocale } from '@amp/web-app-components/src/utils/locale';
 7  
 8  import type {
 9      NormalizedStorefront,
10      NormalizedLanguage,
11  } from '@jet-app/app-store/api/locale';
12  
13  import {
14      DEFAULT_STOREFRONT_CODE,
15      DEFAULT_LANGUAGE_BCP47,
16  } from '~/constants/storefront';
17  import { Jet } from '~/jet';
18  import { setup as setupI18n } from '~/stores/i18n';
19  import type { PrefetchedIntents } from '@amp/web-apps-common/src/jet/prefetched-intents';
20  import type { LoggerFactory } from '@amp/web-apps-logger';
21  import type { Locale as Language } from '@amp/web-apps-localization';
22  import type I18N from '@amp/web-apps-localization';
23  import '~/config/components/artwork';
24  import '~/config/components/shelf';
25  import type { FeaturesCallbacks } from './jet/dependencies/net';
26  
27  export type Context = Map<string, unknown>;
28  
29  export async function bootstrap({
30      loggerFactory,
31      initialUrl,
32      fetch,
33      prefetchedIntents,
34      featuresCallbacks,
35  }: {
36      loggerFactory: LoggerFactory;
37      initialUrl: string;
38      fetch: typeof window.fetch;
39      prefetchedIntents: PrefetchedIntents;
40      featuresCallbacks?: FeaturesCallbacks;
41  }): Promise<{
42      context: Context;
43      jet: Jet;
44      initialAction: Opt<ActionModel>;
45      intent: Opt<Intent<unknown>>;
46      storefront: NormalizedStorefront;
47      language: NormalizedLanguage;
48      i18n: I18N;
49  }> {
50      const log = loggerFactory.loggerFor('bootstrap');
51  
52      const context = new Map();
53  
54      const jet = Jet.load({
55          loggerFactory,
56          context,
57          fetch,
58          prefetchedIntents,
59          featuresCallbacks,
60      });
61  
62      initializeUniqueIdContext(context, loggerFactory);
63  
64      const routing = await jet.routeUrl(initialUrl);
65  
66      if (routing) {
67          log.info('initial URL routed to:', routing);
68      } else {
69          log.warn('initial URL was unroutable:', initialUrl);
70      }
71  
72      const {
73          intent = null,
74          action: initialAction = null,
75          storefront = DEFAULT_STOREFRONT_CODE,
76          language = DEFAULT_LANGUAGE_BCP47,
77      } = routing || {};
78  
79      // TODO: rdar://78109398 (i18n Improvements)
80      const i18nStore = await setupI18n(
81          context,
82          loggerFactory,
83          language.toLowerCase() as Language,
84      );
85      jet.setLocale(i18nStore, storefront, language);
86      setSharedLocale(context, { storefront, language });
87  
88      return {
89          context,
90          jet,
91          initialAction,
92          intent,
93          storefront,
94          language,
95          i18n: i18nStore,
96      };
97  }