Axios instances
This reference explains how to create `axios` instances to interact with Magento's API.
Overview
The Front-Commerce's Magento2 extension exposes preconfigured HTTP client
instances for interacting with Magento2's API. These instances are exposed via
the Dependency Injection
system, under the magento2
HTTP namespace.
You can access them via services.DI.get("magento2").http.*
in your code.
publicRestAPI
The publicRestAPI
instance is an axios
instance that can be used to interact
with Magento2's public REST API. It will handle authentication and other
configuration details automatically.
Example:
import { createGraphQLRuntime } from "@front-commerce/core/graphql";
export default createGraphQLRuntime({
contextEnhancer: ({ services, makeDataLoader }) => {
const magento2Client = services.DI.get("magento2").http.publicRestAPI;
return {
MyFeature: new MyFeatureLoader(magento2Client, makeDataLoader),
};
},
});
adminRestAPI
The adminRestAPI
instance is an axios
instance that can be used to interact
with Magento2's admin REST API. It will handle authentication and other
configuration details automatically.
Example:
import { createGraphQLRuntime } from "@front-commerce/core/graphql";
export default createGraphQLRuntime({
contextEnhancer: ({ services, makeDataLoader }) => {
const magento2AdminClient = services.DI.get("magento2").http.adminRestAPI;
return {
MyAdminFeature: new MyAdminFeatureLoader(
magento2AdminClient,
makeDataLoader
),
};
},
});
makeAdminClientFromRequest
The makeAdminClientFromRequest
function creates an axios
instance that can
be used to interact with Magento's API as an Admin.
makeAdminClientFromRequest(request);
Arguments:
Name | Type | Description |
---|---|---|
request | Request | v2 compatible request object |
Example:
import { createGraphQLRuntime } from "@front-commerce/core/graphql";
import { makeAdminClientFromRequest } from "@front-commerce/magento1/axios";
export default createGraphQLRuntime({
contextEnhancer: ({ req }) => {
const adminAxiosInstance = makeAdminClientFromRequest(req);
// rest of logic
},
});
makeUserClientFromRequest
The makeUserClientFromRequest
function creates an axios
instance that can be
used to interact with Magento's API as a customer.
makeUserClientFromRequest(request);
Arguments:
Name | Type | Description |
---|---|---|
request | Request | v2 compatible request object |
Example:
import { createGraphQLRuntime } from "@front-commerce/core/graphql";
import { makeUserClientFromRequest } from "@front-commerce/magento2/axios";
export default createGraphQLRuntime({
contextEnhancer: ({ req }) => {
const axiosInstance = makeUserClientFromRequest(req);
// rest of logic
},
});
makeCartUrlBuilderFromRequest
The makeCartUrlBuilderFromRequest
function creates a CartUrlBuilder
instance.
makeCartUrlBuilderFromRequest(request);
Arguments:
Name | Type | Description |
---|---|---|
request | Request | v2 compatible request object |
Example:
import { createGraphQLRuntime } from "@front-commerce/core/graphql";
import { makeCartUrlBuilderFromRequest } from "@front-commerce/magento2/axios";
export default createGraphQLRuntime({
contextEnhancer: ({ req }) => {
const cartUrlBuilder = makeCartUrlBuilderFromRequest(req);
// rest of logic
},
});