Skip to content

Commit

Permalink
refactor: standarize API Client (#1507)
Browse files Browse the repository at this point in the history
* IN-4072 Use proxy-sdk

* BREAKING remove MagentoApiMethods from Config

* Align test state with current storeconfig state

Somebody changed the magento store config and didnt tell anybody

* fix wrongly renamed methods

* refactor: use function syntax

* remove test comment

* revert prop removal

* fix build issues

* update eslint dependencies

* fix test issues

---------

Co-authored-by: Wojciech Sikora <[email protected]>
  • Loading branch information
sethidden and WojtekTheWebDev authored Apr 11, 2024
1 parent 633b422 commit 8b8fdff
Show file tree
Hide file tree
Showing 101 changed files with 1,072 additions and 1,381 deletions.
24 changes: 24 additions & 0 deletions .changeset/thirty-socks-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@vue-storefront/magento-api": major
---

- **[CHANGED]** `Endpoints` interface. Previously, each endpoint contained `context` param, which is internal and shouldn't be exposed in the final interface. Now, `Endpoints` interface properties don't contain `context` param. If you need to use `context` param, you should use `ApiMethods` type.

```diff
- import { Endpoints } from '@vue-storefront/magento-api';
+ import { ApiMethods } from '@vue-storefront/magento-api';
```

- **[CHANGED]** Removed `ContextualizedEndpoints` type. Use `Endpoints` instead.

```diff
- import { ContextualizedEndpoints } from '@vue-storefront/magento-api';
+ import { Endpoints } from '@vue-storefront/magento-api';
```

- **[REMOVED]** `MagentoApiMethods` interface. Use `Endpoints` instead.

```diff
- import { MagentoApiMethods } from '@vue-storefront/magento-api';
+ import { Endpoints } from '@vue-storefront/magento-api';
```
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
},
"devDependencies": {
"@changesets/cli": "^2.26.1",
"@vue-storefront/api-extractor-config": "^0.0.3",
"@vue-storefront/eslint-config-integrations": "^0.0.14",
"@vue-storefront/integrations-tsconfig": "^0.0.2",
"@vue-storefront/jest-config": "^0.0.2",
"@vue-storefront/rollup-config": "^0.0.3",
"@vue-storefront/api-extractor-config": "^0.0.5",
"@vue-storefront/eslint-config-integrations": "^0.0.21",
"@vue-storefront/integrations-tsconfig": "^0.0.4",
"@vue-storefront/jest-config": "^0.0.4",
"@vue-storefront/rollup-config": "^0.0.7",
"all-contributors-cli": "^6.20.0",
"esbuild": "^0.14.23",
"lerna": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"@apollo/client": "3.8.7",
"@nuxtjs/composition-api": "0.31.0",
"@vue-storefront/middleware": "^3.5.0",
"@vue-storefront/middleware": "^3.9.0",
"@vue-storefront/magento-types": "1.2.0",
"agentkeepalive": "^4.2.1",
"consola": "^3.1.0",
Expand Down
5 changes: 0 additions & 5 deletions packages/api-client/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import json from "@rollup/plugin-json";

import { generateBaseConfig, generateServerConfig } from "@vue-storefront/rollup-config";
import package_ from "./package.json";

const baseConfig = generateBaseConfig(package_);
const serverConfig = generateServerConfig(package_);

baseConfig.plugins.push(json());
serverConfig.plugins.push(json());

export default [baseConfig, serverConfig];
3 changes: 2 additions & 1 deletion packages/api-client/src/api-extractor-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Api Client for Magento 2 integration for Vue Storefront 2.
* `api-client` of Magento 2 integration.
*
* @remarks
* The `@vue-storefront/magento-api` library includes Apollo Client,
Expand All @@ -10,3 +10,4 @@
*/

export * from "./api";
export * from "./types";
10 changes: 5 additions & 5 deletions packages/api-client/src/api/addBundleProductsToCart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import type {
CustomHeaders,
} from "@vue-storefront/magento-types";
import gql from "graphql-tag";
import addBundleProductsToCart from "./addBundleProductsToCart";
import addBundleProductsToCartQuery from "./addBundleProductsToCart";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

export default async (
export async function addBundleProductsToCart(
context: Context,
input: AddBundleProductsToCartInput,
customQuery: CustomQuery = { addBundleProductsToCart: "addBundleProductsToCart" },
customHeaders: CustomHeaders = {}
): Promise<FetchResult<AddBundleProductsToCartMutation>> => {
): Promise<FetchResult<AddBundleProductsToCartMutation>> {
const { addBundleProductsToCart: addBundleProductsToCartGQL } = context.extendQuery(customQuery, {
addBundleProductsToCart: {
query: addBundleProductsToCart,
query: addBundleProductsToCartQuery,
variables: { input },
},
});
Expand All @@ -33,4 +33,4 @@ export default async (
headers: getHeaders(context, customHeaders),
},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import getHeaders from "../getHeaders";
* @param [customQuery] (optional) - custom GraphQL query that extends the default one
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function addConfigurableProductsToCart(
export async function addConfigurableProductsToCart(
context: Context,
input: AddConfigurableProductsToCartInput,
customQuery: CustomQuery = { addConfigurableProductsToCart: "addConfigurableProductsToCart" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import getHeaders from "../getHeaders";
* @param customQuery custom GraphQL query that extends the default one
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function addDownloadableProductsToCart(
export async function addDownloadableProductsToCart(
context: Context,
input: AddDownloadableProductsToCartInput,
customQuery: CustomQuery = { addDownloadableProductsToCart: "addDownloadableProductsToCart" },
Expand Down
10 changes: 5 additions & 5 deletions packages/api-client/src/api/addProductToWishList/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { FetchResult, gql } from "@apollo/client/core";
import { CustomQuery, AddProductsToWishlistMutation, AddProductsToWishlistMutationVariables } from "@vue-storefront/magento-types";
import type { CustomHeaders } from "@vue-storefront/magento-types";
import addProductsToWishlist from "./addProductsToWishlist";
import addProductsToWishListQuery from "./addProductsToWishlist";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

export default async (
export async function addProductToWishList(
context: Context,
input: AddProductsToWishlistMutationVariables,
customQuery: CustomQuery = { addProductsToWishlist: "addProductsToWishlist" },
customHeaders: CustomHeaders = {}
): Promise<FetchResult<AddProductsToWishlistMutation>> => {
): Promise<FetchResult<AddProductsToWishlistMutation>> {
const { addProductsToWishlist: addProductsToWishlistGQL } = context.extendQuery(customQuery, {
addProductsToWishlist: {
query: addProductsToWishlist,
query: addProductsToWishListQuery,
variables: { ...input },
},
});
Expand All @@ -26,4 +26,4 @@ export default async (
headers: getHeaders(context, customHeaders),
},
});
};
}
2 changes: 1 addition & 1 deletion packages/api-client/src/api/addProductsToCart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import getHeaders from "../getHeaders";
* @param [customQuery] (optional) - custom GraphQL query that extends the default one
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function addProductsToCart(
export async function addProductsToCart(
context: Context,
input: AddProductsToCartInput,
customQuery: CustomQuery = { addProductsToCart: "addProductsToCart" },
Expand Down
10 changes: 5 additions & 5 deletions packages/api-client/src/api/addSimpleProductsToCart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import {
AddSimpleProductsToCartMutationVariables,
} from "@vue-storefront/magento-types";
import type { CustomHeaders } from "@vue-storefront/magento-types";
import addSimpleProductsToCart from "./addSimpleProductsToCart";
import addSimpleProductsToCartQuery from "./addSimpleProductsToCart";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

export default async (
export async function addSimpleProductsToCart(
context: Context,
input: AddSimpleProductsToCartInput,
customQuery: CustomQuery = { addSimpleProductsToCart: "addSimpleProductsToCart" },
customHeaders: CustomHeaders = {}
): Promise<FetchResult<AddSimpleProductsToCartMutation>> => {
): Promise<FetchResult<AddSimpleProductsToCartMutation>> {
const { addSimpleProductsToCart: addSimpleProductsToCartGQL } = context.extendQuery(customQuery, {
addSimpleProductsToCart: {
query: addSimpleProductsToCart,
query: addSimpleProductsToCartQuery,
variables: { input },
},
});
Expand All @@ -29,4 +29,4 @@ export default async (
headers: getHeaders(context, customHeaders),
},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import getHeaders from "../getHeaders";
* @param customQuery custom GraphQL query that extends the default one
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function addVirtualProductsToCart(
export async function addVirtualProductsToCart(
context: Context,
input: AddVirtualProductsToCartInput,
customQuery: CustomQuery = { addVirtualProductsToCart: "addVirtualProductsToCart" },
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/api/applyCouponToCart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import getHeaders from "../getHeaders";
* @param customQuery custom GraphQL query that extends the default one
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function applyCouponToCart(
export async function applyCouponToCart(
context: Context,
input: ApplyCouponToCartInput,
customQuery: CustomQuery = { applyCouponToCart: "applyCouponToCart" },
Expand Down
10 changes: 5 additions & 5 deletions packages/api-client/src/api/availableStores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { ApolloQueryResult } from "@apollo/client/core";
import type { CustomHeaders } from "@vue-storefront/magento-types";
import { AvailableStoresQuery, CustomQuery } from "@vue-storefront/magento-types";
import gql from "graphql-tag";
import availableStores from "./availableStores";
import availableStoresQuery from "./availableStores";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

/**
* Returns list of available stores
*/
export default async (
export async function availableStores(
context: Context,
customQuery: CustomQuery = { availableStores: "availableStores" },
customHeaders: CustomHeaders = {}
): Promise<ApolloQueryResult<AvailableStoresQuery>> => {
): Promise<ApolloQueryResult<AvailableStoresQuery>> {
const { availableStores: availableStoresGQL } = context.extendQuery(customQuery, {
availableStores: {
query: availableStores,
query: availableStoresQuery,
},
});

Expand All @@ -28,4 +28,4 @@ export default async (
headers: getHeaders(context, customHeaders),
},
});
};
}
2 changes: 1 addition & 1 deletion packages/api-client/src/api/cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import getHeaders from "../getHeaders";
* @param customQuery custom GraphQL query that extends the default one
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function cart(
export async function cart(
context: Context,
cartId: string,
customQuery: CustomQuery = { cart: "cart" },
Expand Down
5 changes: 3 additions & 2 deletions packages/api-client/src/api/cartTotalQty/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import cartTotalQtyQuery from "./cartTotalQty";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

export default async (context: Context, cartId: string, customHeaders: CustomHeaders = {}): Promise<ApolloQueryResult<CartQuery>> =>
context.client.query<CartQuery, CartQueryVariables>({
export async function cartTotalQty(context: Context, cartId: string, customHeaders: CustomHeaders = {}): Promise<ApolloQueryResult<CartQuery>> {
return context.client.query<CartQuery, CartQueryVariables>({
query: gql`
${cartTotalQtyQuery}
`,
Expand All @@ -16,3 +16,4 @@ export default async (context: Context, cartId: string, customHeaders: CustomHea
headers: getHeaders(context, customHeaders),
},
});
}
2 changes: 1 addition & 1 deletion packages/api-client/src/api/categories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import getHeaders from "../getHeaders";
* @param [customQuery] (optional) - custom GraphQL query that extends the default query
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function categories(
export async function categories(
context: Context,
params: QueryCategoriesArgs,
customQuery: CustomQuery = { categories: "categories" },
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/api/categoryList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import getHeaders from "../getHeaders";
* @param [customQuery] (optional) - custom GraphQL query that extends the default query
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function categoryList(
export async function categoryList(
context: Context,
params: QueryCategoryListArgs,
customQuery: CustomQuery = { categoryList: "categoryList" },
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/api/categorySearch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import getHeaders from "../getHeaders";
* @param [customQuery] (optional) - custom GraphQL query that extends the default query
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function categorySearch(
export async function categorySearch(
context: Context,
filters: CategorySearchQueryVariables,
customQuery: CustomQuery = { categorySearch: "categorySearch" },
Expand Down
10 changes: 5 additions & 5 deletions packages/api-client/src/api/changeCustomerPassword/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { FetchResult } from "@apollo/client/core";
import type { CustomHeaders } from "@vue-storefront/magento-types";
import { ChangeCustomerPasswordMutation, ChangeCustomerPasswordMutationVariables, CustomQuery } from "@vue-storefront/magento-types";
import gql from "graphql-tag";
import changeCustomerPassword from "./changeCustomerPassword";
import changeCustomerPasswordQuery from "./changeCustomerPassword";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

/**
* Changes password of the current customer. To override the default query, use the `changeCustomerPassword` query key.
*/
export default async (
export async function changeCustomerPassword(
context: Context,
params: { currentPassword: string; newPassword: string },
customQuery: CustomQuery = { changeCustomerPassword: "changeCustomerPassword" },
customHeaders: CustomHeaders = {}
): Promise<FetchResult<ChangeCustomerPasswordMutation>> => {
): Promise<FetchResult<ChangeCustomerPasswordMutation>> {
try {
const { changeCustomerPassword: changeCustomerPasswordGQL } = context.extendQuery(customQuery, {
changeCustomerPassword: {
query: changeCustomerPassword,
query: changeCustomerPasswordQuery,
variables: { ...params },
},
});
Expand All @@ -41,4 +41,4 @@ export default async (
}
throw error.networkError?.result || error;
}
};
}
6 changes: 3 additions & 3 deletions packages/api-client/src/api/cmsBlocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApolloQueryResult } from "@apollo/client/core";
import type { CustomHeaders } from "@vue-storefront/magento-types";
import { CmsBlockQuery, CmsBlockQueryVariables, CustomQuery } from "@vue-storefront/magento-types";
import gql from "graphql-tag";
import cmsBlocks from "./cmsBlocks";
import cmsBlocksQuery from "./cmsBlocks";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

Expand All @@ -14,15 +14,15 @@ import getHeaders from "../getHeaders";
* @param [customQuery] - (optional) - custom GraphQL query that extends the default cmsBlocks query
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function getCmsBlocks(
export async function cmsBlocks(
context: Context,
identifiers: string,
customQuery: CustomQuery = { cmsBlocks: "cmsBlocks" },
customHeaders: CustomHeaders = {}
): Promise<ApolloQueryResult<CmsBlockQuery>> {
const { cmsBlocks: cmsBlocksGQL } = context.extendQuery(customQuery, {
cmsBlocks: {
query: cmsBlocks,
query: cmsBlocksQuery,
variables: { identifiers },
},
});
Expand Down
6 changes: 3 additions & 3 deletions packages/api-client/src/api/cmsPage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import consola from "consola";
import type { CustomHeaders } from "@vue-storefront/magento-types";
import { CmsPageQuery, CmsPageQueryVariables, CustomQuery } from "@vue-storefront/magento-types";
import gql from "graphql-tag";
import cmsPage from "./cmsPage";
import cmsPageQuery from "./cmsPage";
import { Context } from "../../types/context";
import getHeaders from "../getHeaders";

Expand All @@ -14,7 +14,7 @@ import getHeaders from "../getHeaders";
* @param customQuery - (optional) - custom query that extends default cmsPage GraphQL query
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function getCmsPage(
export async function cmsPage(
context: Context,
identifier: string,
customQuery: CustomQuery = { cmsPage: "cmsPage" },
Expand All @@ -23,7 +23,7 @@ export default async function getCmsPage(
try {
const { cmsPage: cmsPageGQL } = context.extendQuery(customQuery, {
cmsPage: {
query: cmsPage,
query: cmsPageQuery,
variables: { identifier },
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/api/countries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import getHeaders from "../getHeaders";
* @param [customQuery] (optional) - custom GraphQL query that extends the default one
* @param customHeaders (optional) - custom headers that extends the default headers
*/
export default async function countries(
export async function countries(
context: Context,
customQuery: CustomQuery = { countries: "countries" },
customHeaders: CustomHeaders = {}
Expand Down
Loading

0 comments on commit 8b8fdff

Please sign in to comment.