Skip to content

Commit e75e45d

Browse files
committed
feat(storefront): New typed fetchModule function exported on @@sf/state/modules-info
1 parent cb66b57 commit e75e45d

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

packages/storefront/src/lib/state/modules-info.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type {
2+
AppModuleName,
3+
ListPaymentsParams,
24
ListPaymentsResponse,
5+
CalculateShippingParams,
36
CalculateShippingResponse,
7+
ApplyDiscountParams,
48
ApplyDiscountResponse,
59
} from '@cloudcommerce/types';
610
import { reactive, computed } from 'vue';
@@ -32,6 +36,36 @@ loadingGlobalInfoPreset.then((modulesInfoPreset) => {
3236
Object.assign(modulesInfo, modulesInfoPreset);
3337
});
3438

39+
export type ModuleName = Exclude<AppModuleName, 'create_transaction'>;
40+
41+
export type ModulePayload<M extends ModuleName> =
42+
M extends 'list_payments' ? ListPaymentsParams :
43+
M extends 'calculate_shipping' ? CalculateShippingParams :
44+
M extends 'apply_discount' ? ApplyDiscountParams :
45+
Record<string, any>;
46+
47+
type FetchModule = <M extends ModuleName>(
48+
modName: M,
49+
reqOptions?: {
50+
method: 'get',
51+
params?: Record<string, string>,
52+
} | {
53+
method: 'post',
54+
body: ModulePayload<M>,
55+
},
56+
) => Promise<Response & {
57+
json<P = ModulePayload<M>>(): Promise<P>,
58+
}>;
59+
60+
export const fetchModule: FetchModule = (modName, reqOptions) => {
61+
const { hostname } = window.location;
62+
const { domain } = globalThis.$storefront.settings;
63+
const modulesBaseUri = hostname !== 'localhost' && hostname !== '127.0.0.1'
64+
? `https://${domain}/_api/modules/`
65+
: '/_api/modules/';
66+
return afetch(`${modulesBaseUri}${modName}`, reqOptions);
67+
};
68+
3569
if (!import.meta.env.SSR) {
3670
const storageKey = 'MODULES_INFO';
3771
const sessionJson = sessionStorage.getItem(storageKey);
@@ -50,8 +84,8 @@ if (!import.meta.env.SSR) {
5084
}
5185

5286
const fetchInfo = () => {
53-
const modulesToFetch: { modName: string, reqOptions?: Record<string, any> }[] = [];
54-
['list_payments', 'calculate_shipping'].forEach((modName) => {
87+
const modulesToFetch: { modName: ModuleName, reqOptions?: any }[] = [];
88+
(['list_payments', 'calculate_shipping'] as const).forEach((modName) => {
5589
if (!Object.keys(modulesInfo[modName]).length) {
5690
modulesToFetch.push({ modName });
5791
}
@@ -67,12 +101,7 @@ if (!import.meta.env.SSR) {
67101
}
68102

69103
modulesToFetch.forEach(({ modName, reqOptions }) => {
70-
const { hostname } = window.location;
71-
const { domain } = globalThis.$storefront.settings;
72-
const modulesBaseUri = hostname !== 'localhost' && hostname !== '127.0.0.1'
73-
? `https://${domain}/_api/modules/`
74-
: '/_api/modules/';
75-
afetch(`${modulesBaseUri}${modName}`, reqOptions)
104+
fetchModule(modName, reqOptions)
76105
.then(async (response) => {
77106
if (response.ok) {
78107
const modInfo = {};

0 commit comments

Comments
 (0)