Skip to content

Commit e2a392d

Browse files
roziscodingMarcin Kwiatkowski
andcommitted
feat!: make loading property readonly in the useConfig composable (#846)
* refactor!: rename UseConfig to UseConfigInterface * refactor: make UseConfigInterface a typescript interface * feat!: make loading `useConfig#loading` readonly * docs: add useConfig composable JSDoc * docs: add storeConfig API JSDoc Co-authored-by: Marcin Kwiatkowski <[email protected]>
1 parent 5fbdf98 commit e2a392d

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

docs/.vuepress/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ module.exports = {
9797
['/api-reference/magento-theme.usecontent', 'useContent()'],
9898
['/api-reference/magento-theme.usecart', 'useCart()'],
9999
['/api-reference/magento-theme.usecategory', 'useCategory()'],
100+
['/api-reference/magento-theme.useconfig', 'useConfig()'],
100101
['/api-reference/magento-theme.usecategorysearch', 'useCategorySearch()'],
101102
['/api-reference/magento-theme.useuserorder', 'useUserOrder()'],
102103
['/api-reference/magento-theme.usewishlist', 'useWishlist()'],
@@ -113,6 +114,7 @@ module.exports = {
113114
['/api-reference/magento-api.deletecustomeraddress', 'deleteCustomerAddress'],
114115
['/api-reference/magento-api.getcustomeraddresses', 'getCustomerAddresses'],
115116
['/api-reference/magento-api.updatecustomeraddress', 'updateCustomerAddress'],
117+
['/api-reference/magento-api.storeconfig', 'storeConfig'],
116118
['/api-reference/magento-api.addproductstocart', 'addProductsToCart'],
117119
['/api-reference/magento-api.addconfigurableproductstocart', 'addConfigurableProductsToCart'],
118120
['/api-reference/magento-api.adddownloadableproductstocart', 'addDownloadableProductsToCart'],
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
import { ApolloQueryResult } from '@apollo/client/core';
22
import { CustomQuery } from '@vue-storefront/core';
33
import { StoreConfigQuery } from '../../types/GraphQL';
4-
import storeConfig from './storeConfig';
4+
import storeConfigMutation from './storeConfig';
55
import { Context } from '../../types/context';
66

7-
export default async (
7+
/**
8+
* Fetches the store configuration from the API
9+
* @param context VSF Context
10+
* @param [customQuery] (optional) - custom GraphQL query that extends the default one
11+
*/
12+
export default async function storeConfig(
813
context: Context,
914
customQuery: CustomQuery = { storeConfig: 'storeConfig' },
10-
): Promise<ApolloQueryResult<StoreConfigQuery>> => {
15+
): Promise<ApolloQueryResult<StoreConfigQuery>> {
1116
const { storeConfig: storeConfigGQL } = context.extendQuery(
1217
customQuery,
1318
{
1419
storeConfig: {
15-
query: storeConfig,
20+
query: storeConfigMutation,
1621
},
1722
},
1823
);
1924

2025
return context.client.query<StoreConfigQuery>({
2126
query: storeConfigGQL.query,
2227
});
23-
};
28+
}

packages/api-client/src/api/storeConfig/storeConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gql from 'graphql-tag';
22

3+
/** GraphQL Query that fetches store configuration from the API */
34
export default gql`
45
query storeConfig {
56
storeConfig {

packages/theme/composables/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export { default as useUiHelpers } from './useUiHelpers';
1010
export { default as useUiNotification } from './useUiNotification';
1111
export { default as useUiState } from './useUiState';
1212
export { default as useImage } from './useImage';
13-
export { default as useConfig } from './useConfig';
13+
export * from './useConfig';
1414
export { default as useStore } from './useStore';
1515
export { default as useCurrency } from './useCurrency';
1616
export { default as useExternalCheckout } from './useExternalCheckout';

packages/theme/composables/useConfig/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import {
2-
computed, ref, useContext,
2+
computed, readonly, ref, useContext,
33
} from '@nuxtjs/composition-api';
44
import { Logger } from '~/helpers/logger';
55
import { useConfigStore } from '~/stores/config';
6-
import { UseConfig, UseConfigErrors } from '~/composables/useConfig/useConfig';
6+
import { UseConfigErrors, UseConfigInterface } from './useConfig';
77

8-
const useConfig = (): UseConfig => {
8+
/**
9+
* The `useConfig` composable is responsible for interactions with the configuration in your eCommerce.
10+
*
11+
* See the {@link UseConfigInterface} page for more information.
12+
*/
13+
export function useConfig(): UseConfigInterface {
914
const { app } = useContext();
1015
const loading = ref(false);
1116
const error = ref<UseConfigErrors>({ load: null });
@@ -33,9 +38,10 @@ const useConfig = (): UseConfig => {
3338

3439
return {
3540
config,
36-
loading,
41+
loading: readonly(loading),
3742
load,
3843
};
39-
};
44+
}
4045

46+
export * from './useConfig';
4147
export default useConfig;
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { ComputedRef, Ref } from '@nuxtjs/composition-api';
1+
import { ComputedRef, DeepReadonly, Ref } from '@nuxtjs/composition-api';
22
import { StoreConfig } from '~/modules/GraphQL/types';
33

4+
/** Errors returned by the {@link useConfig} composable */
45
export interface UseConfigErrors {
6+
/** Error when loading configuration fails, otherwise is `null` */
57
load: Error | null;
68
}
79

8-
export type UseConfig = {
10+
/**
11+
* Represents the data and methods returned by the {@link useConfig} composable
12+
*/
13+
export interface UseConfigInterface {
14+
/** Returns the loaded config as computed property */
915
config: ComputedRef<StoreConfig>,
10-
loading: Ref<boolean>,
16+
/** Return state of loadConfig Function as computed property */
17+
loading: DeepReadonly<Ref<boolean>>,
18+
/** Function to load the config */
1119
load (): Promise<void>
12-
};
20+
}

0 commit comments

Comments
 (0)