Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
@shopware/api-client
Advanced tools
Readme
Dynamic and fully typed API Client for Shopware 6. Usable in any JavaScript and TypeScript project. You can use types generated from your custom API instance to have autocompletion and type safety.
Install npm package:
# Using pnpm
pnpm add @shopware/api-client
# Using yarn
yarn add @shopware/api-client
# Using npm
npm i @shopware/api-client
Recommended practice is to create a separate module file. For example src/apiClient.ts
, and import it whenever you need to use API Client.
import { createAPIClient } from "@shopware/api-client";
// You can pick types of your current API version, the default one:
import type {
operationPaths,
operations,
components,
} from "@shopware/api-client/api-types";
// or (specific version):
import type {
operationPaths,
operations,
components,
} from "@shopware/api-client/api-types/apiTypes-6.4.20.0";
// or your types generated by @shopware/api-gen CLI:
import { operationPaths, operations, components } from "./apiTypes";
// you can pick cookies library of your choice
import Cookies from "js-cookie";
export const apiClient = createAPIClient<operations, operationPaths>({
baseURL: "https://demo-frontends.shopware.store/store-api",
accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW",
contextToken: Cookies.get("sw-context-token"),
onContextChanged(newContextToken) {
Cookies.set("sw-context-token", newContextToken, {
expires: 365, // days
path: "/",
sameSite: "lax",
});
},
});
// reimport schemas to use it in application
export type ApiSchemas = components["schemas"];
// reimport operations request parameters to use it in application
export type ApiRequestParams<OPERATION_NAME extends keyof operations> =
RequestParameters<OPERATION_NAME, operations>;
// reimport operations return types to use it in application
export type ApiReturnType<OPERATION_NAME extends keyof operations> =
RequestReturnType<OPERATION_NAME, operations>;
The setup works the same way as creteAPIClient
function, with few differences:
// example adminApiClient.ts file
import { createAdminAPIClient } from "@shopware/api-client"; // we use different function to create admin api client
import {
RequestParameters,
RequestReturnType,
createAdminAPIClient,
} from "@shopware/api-client";
import type {
operationPaths,
operations,
components,
} from "@shopware/api-client/admin-api-types"; // we take default admin api types from different directory than store-api
import Cookies from "js-cookie";
export const adminApiClient = createAdminAPIClient<operations, operationPaths>({
baseURL: "https://demo-frontends.shopware.store/api",
sessionData: JSON.parse(Cookies.get("sw-admin-session-data") || "{}"),
onAuthChange(sessionData) {
Cookies.set("sw-admin-session-data", JSON.stringify(sessionData), {
expires: 1, // days
path: "/",
sameSite: "lax",
});
},
});
export type AdminApiSchemas = components["schemas"];
export type AdminApiRequestParams<OPERATION_NAME extends keyof operations> =
RequestParameters<OPERATION_NAME, operations>;
export type AdminApiReturnType<OPERATION_NAME extends keyof operations> =
RequestReturnType<OPERATION_NAME, operations>;
the rest works the same as store-api client.
Take a look at example project using API Client.
import { apiClient, ApiReturnType } from "./apiClient";
// could be reactive value, you can use ApiReturnType to type it properly
let productsResponse: ApiReturnType<"readProduct">;
async function loadProducts() {
productsResponse = await apiClient.invoke("readProduct post /product", {
limit: 2,
});
}
If you prefer to add another layer of abstraction you can use created previously types to define your own concept of methods.
// add for example into apiClient.ts file
const readNavigation = (params: ApiRequestParams<"readNavigation">) =>
apiClient.invoke(
"readNavigation post /navigation/{activeId}/{rootId} sw-include-seo-urls",
{
depth: 2,
...params,
},
);
// in another file you can use it, and depth property will be set to 2 by default
import { readNavigation } from "./apiClient";
async function loadMainNavigation() {
const navigation = await readNavigation({
activeId: "main-navigation",
rootId: "main-navigation",
});
}
Client is throwing ApiClientError
with detailed information returned from the API. It will display clear message in the console or you can access details
property to get raw information from the response.
import { ApiClientError } from "@shopware/api-client";
try {
// ... your request
} catch (error) {
if (error instanceof ApiClientError) {
console.error(error); // This prints message summary
console.error("Details:", error.details); // Raw response from API
} else {
console.error("==>", error); // Another type of error, not recognized by API client
}
}
👥 Community Slack (#shopware-frontends
channel)
Full changelog for stable version is available here
FAQs
Shopware client for API connection.
We found that @shopware/api-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).