summaryrefslogtreecommitdiff
path: root/src/bootstrap.ts
blob: 14ebbe80de64cb9a6debfd43d079070e2f79a8c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Sets up app specific configurations
import type { Opt } from '@jet/environment';
import type { Intent } from '@jet/environment/dispatching';
import type { ActionModel } from '@jet/environment/types/models';
import { initializeUniqueIdContext } from '@amp/web-app-components/src/utils/uniqueId';
import { setLocale as setSharedLocale } from '@amp/web-app-components/src/utils/locale';

import type {
    NormalizedStorefront,
    NormalizedLanguage,
} from '@jet-app/app-store/api/locale';

import {
    DEFAULT_STOREFRONT_CODE,
    DEFAULT_LANGUAGE_BCP47,
} from '~/constants/storefront';
import { Jet } from '~/jet';
import { setup as setupI18n } from '~/stores/i18n';
import type { PrefetchedIntents } from '@amp/web-apps-common/src/jet/prefetched-intents';
import type { LoggerFactory } from '@amp/web-apps-logger';
import type { Locale as Language } from '@amp/web-apps-localization';
import type I18N from '@amp/web-apps-localization';
import '~/config/components/artwork';
import '~/config/components/shelf';
import type { FeaturesCallbacks } from './jet/dependencies/net';

export type Context = Map<string, unknown>;

export async function bootstrap({
    loggerFactory,
    initialUrl,
    fetch,
    prefetchedIntents,
    featuresCallbacks,
}: {
    loggerFactory: LoggerFactory;
    initialUrl: string;
    fetch: typeof window.fetch;
    prefetchedIntents: PrefetchedIntents;
    featuresCallbacks?: FeaturesCallbacks;
}): Promise<{
    context: Context;
    jet: Jet;
    initialAction: Opt<ActionModel>;
    intent: Opt<Intent<unknown>>;
    storefront: NormalizedStorefront;
    language: NormalizedLanguage;
    i18n: I18N;
}> {
    const log = loggerFactory.loggerFor('bootstrap');

    const context = new Map();

    const jet = Jet.load({
        loggerFactory,
        context,
        fetch,
        prefetchedIntents,
        featuresCallbacks,
    });

    initializeUniqueIdContext(context, loggerFactory);

    const routing = await jet.routeUrl(initialUrl);

    if (routing) {
        log.info('initial URL routed to:', routing);
    } else {
        log.warn('initial URL was unroutable:', initialUrl);
    }

    const {
        intent = null,
        action: initialAction = null,
        storefront = DEFAULT_STOREFRONT_CODE,
        language = DEFAULT_LANGUAGE_BCP47,
    } = routing || {};

    // TODO: rdar://78109398 (i18n Improvements)
    const i18nStore = await setupI18n(
        context,
        loggerFactory,
        language.toLowerCase() as Language,
    );
    jet.setLocale(i18nStore, storefront, language);
    setSharedLocale(context, { storefront, language });

    return {
        context,
        jet,
        initialAction,
        intent,
        storefront,
        language,
        i18n: i18nStore,
    };
}