UNPKG

@shopware/api-client

Version:
385 lines (286 loc) 12.9 kB
# shopware/frontends - api-client [![](https://img.shields.io/npm/v/@shopware/api-client?color=blue&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTIiIHZpZXdCb3g9IjAgMCA0ODggNTUzIiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cGF0aCBkPSJNNDM5LjA0MSAxMjkuNTkzTDI1OC43NjkgMzEuMzA3NkMyNDQuOTE1IDIzLjc1NDEgMjI4LjExNiAyNC4wMDkzIDIxNC40OTcgMzEuOTgwMkw0Ny4yNjkgMTI5Ljg1OEMzMy40NzYzIDEzNy45MzEgMjUgMTUyLjcxMyAyNSAxNjguNjk1VjM4OC40NjZDMjUgNDA0LjczMiAzMy43Nzg1IDQxOS43MzIgNDcuOTYwMiA0MjcuNjk5TDIxNS4xNzggNTIxLjYzNkMyMjguNDUxIDUyOS4wOTIgMjQ0LjU5MyA1MjkuMzMyIDI1OC4wODIgNTIyLjI3NEw0MzguMzY0IDQyNy45MzRDNDUzLjIwMSA0MjAuMTcgNDYyLjUgNDA0LjgwOSA0NjIuNSAzODguMDYzVjE2OS4xMDJDNDYyLjUgMTUyLjYzMiA0NTMuNTAyIDEzNy40NzcgNDM5LjA0MSAxMjkuNTkzWiIgc3Ryb2tlPSJ1cmwoI3BhaW50MF9saW5lYXJfMTUzXzY5MjY1KSIgc3Ryb2tlLXdpZHRoPSI1MCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIvPgo8ZGVmcz4KPGxpbmVhckdyYWRpZW50IGlkPSJwYWludDBfbGluZWFyXzE1M182OTI2NSIgeDE9Ii0xNi4yOTg5IiB5MT0iMTY1LjM0OSIgeDI9IjI3Ni40MTIiIHkyPSItODkuMzIzNCIgZ3JhZGllbnRVbml0cz0idXNlclNwYWNlT25Vc2UiPgo8c3RvcCBzdG9wLWNvbG9yPSIjMDA4NUZGIi8+CjxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iI0MwRTJGNSIvPgo8L2xpbmVhckdyYWRpZW50Pgo8L2RlZnM+Cjwvc3ZnPg==)](https://npmjs.com/package/@shopware/api-client) [![](https://img.shields.io/github/package-json/v/shopware/frontends?color=blue&filename=packages%2Fapi-client%2Fpackage.json&label=frontends/api-client&logo=github)](https://github.com/shopware/frontends/tree/main/packages/api-client) [![](https://img.shields.io/github/issues/shopware/frontends/api-client?label=package%20issues&logo=github)](https://github.com/shopware/frontends/issues?q=is%3Aopen+is%3Aissue+label%3Aapi-client) [![](https://img.shields.io/github/license/shopware/frontends?color=blue)](#) 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. To generate your own types use [@shopware/api-gen](https://www.npmjs.com/package/@shopware/api-gen) CLI. ## Setup Install npm package: <!-- automd:pm-install name="@shopware/api-client" --> ```sh # ✨ Auto-detect npx nypm install @shopware/api-client # npm npm install @shopware/api-client # yarn yarn add @shopware/api-client # pnpm pnpm install @shopware/api-client # bun bun install @shopware/api-client # deno deno install @shopware/api-client ``` <!-- /automd --> ## Store API client setup Recommended practice is to create a separate module file. For example `src/apiClient.ts`, and import it whenever you need to use API Client. ```typescript import { createAPIClient } from "@shopware/api-client"; // You can pick types of your current API version, the default one: import type { operations } from "@shopware/api-client/store-api-types"; // or - RECOMMENDED - your types generated by [@shopware/api-gen](https://www.npmjs.com/package/@shopware/api-gen) CLI: import type { operations } from "./api-types/storeApiTypes"; // you can pick cookies library of your choice import Cookies from "js-cookie"; export const apiClient = createAPIClient<operations>({ baseURL: "https://demo-frontends.shopware.store/store-api", accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW", contextToken: Cookies.get("sw-context-token"), }); apiClient.hook("onContextChanged", (newContextToken) => { Cookies.set("sw-context-token", newContextToken, { expires: 365, // days path: "/", sameSite: "lax", secure: shopwareEndpoint.startsWith("https://"), }); }); ``` ## Admin API client setup ```typescript import { createAdminAPIClient } from "@shopware/api-client"; ``` The setup works the same way as `creteAPIClient` function, with few differences ### credentials (optional) - Quick scripting or token-based authentication We provide optional `credentials` parameter to `createAdminAPIClient`. Which allows you to use authentication type of your choice whenever you wish to create connection to any endpoint. Example: ```typescript import type { operations, } from "@shopware/api-client/admin-api-types"; // we take default admin api types from different directory than store-api - use your own types by generating schema with @shopware/api-gen CLI import type { operations } from "./api-types/adminApiTypes"; // or use your own types generated by @shopware/api-gen CLI const adminApiClient = createAdminAPIClient<operations>({ baseURL: `${process.env.SHOP_URL}/api`, credentials: { grant_type: "password", client_id: "administration", scopes: "write", username: process.env.SHOP_ADMIN_USERNAME, password: process.env.SHOP_ADMIN_PASSWORD, }, // credentials: { // or token-based example // grant_type: "client_credentials", // client_id: "administration", // client_secret: process.env.SHOP_ADMIN_TOKEN, // }, }); await adminApiClient.invoke(...); // invoke defined endpoint ``` ### sessionData (optional) - Persistent authentication This parameter is used to store session data in cookies (or other place you want to store it), so you can keep your session persistent. You can combine this option with `credentials` property. ```typescript // example adminApiClient.ts file import { createAdminAPIClient } from "@shopware/api-client"; // we use different function to create admin api client import { createAdminAPIClient } from "@shopware/api-client"; import type { operations, Schemas } 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>({ baseURL: "https://demo-frontends.shopware.store/api", sessionData: JSON.parse(Cookies.get("sw-admin-session-data") || "{}"), }); adminApiClient.hook("onAuthChange", (sessionData) => { Cookies.set("sw-admin-session-data", JSON.stringify(sessionData), { expires: 1, // days path: "/", sameSite: "lax", secure: shopwareEndpoint.startsWith("https://"), }); }); ``` the rest works the same as store-api client. ## Basic usage Take a look at [example project using API Client](https://stackblitz.com/github/shopware/frontends/tree/main/examples/new-api-client). ### Simple invocation ```typescript import { apiClient, RequestReturnType } from "./apiClient"; // could be reactive value, you can use ApiReturnType to type it properly let productsResponse: RequestReturnType<"readProduct">; async function loadProducts() { productsResponse = await apiClient.invoke("readProduct post /product", { limit: 2, }); } ``` ### Fetch features The new API client is leveraging [ofetch](https://github.com/unjs/ofetch) library, which has built in support for AbortController, timeout and other features. Example usage of AbortController to cancell your request: ```typescript const controller = new AbortController(); const request = client.invoke("readContext get /context", { fetchOptions: { signal: controller.signal, }, }); controller.abort(); // At this point client will throw an error with the information, that the request has been cancelled ``` Other example of using `fetchOptions` for setting the timeout: ```typescript const request = client.invoke("readContext get /context", { fetchOptions: { timeout: 5000, // 5 seconds }, }); ``` All exposed options available under `fetchOptions` are: - `cache` - `duplex` - `keepalive` - `priority` - `redirect` - `retry` - `retryDelay` - `retryStatusCodes` - `signal` - `timeout` ### Predefining methods If you prefer to add another layer of abstraction you can use created previously types to define your own concept of methods. ```typescript // add for example into apiClient.ts file const readNavigation = ({ depth, type, }: { depth: number; type: "main-navigation"; }) => apiClient.invoke("readNavigation post /navigation/{activeId}/{rootId}", { headers: { "sw-include-seo-urls": true, }, pathParams: { activeId: type, rootId: type, }, body: { depth, }, }); // 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({ body: { activeId: "main-navigation", rootId: "main-navigation" }, }); } ``` ### Error handling 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. ```typescript 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 } } ``` ### Hooks Api client provides hooks to listen to events like context change, authentication change or default headers change. Example: ```typescript apiClient.hook("onDefaultHeaderChanged", (key, value) => { // here we can detect that the default header has changed, either by the user or by the headers incoming from the API }); ``` Available hooks: - `onContextChanged`: Triggered when context token changes - `onResponseError`: Triggered when API returns an error - `onSuccessResponse`: Triggered when API request succeeds - `onDefaultHeaderChanged`: Triggered when default headers are modified - `onRequest`: Triggered before each request is made, allowing for request inspection and modification calling `apiClient.hook` will autocomplete the list of available hooks. ### Base Configuration Management The API client provides methods to manage its base configuration: ```typescript // Get current configuration const config = apiClient.getBaseConfig(); console.log(config.baseURL); // "https://demo-frontends.shopware.store/store-api" console.log(config.accessToken); // "SWSCBHFSNTVMAWNZDNFKSHLAYW" // Update configuration apiClient.updateBaseConfig({ baseURL: "https://new-url.com/store-api", accessToken: "NEW_TOKEN", }); ``` This allows you to dynamically change the API endpoint or access token during runtime, for example when switching between different environments or when the access token needs to be updated. ## Helper Functions The API client provides helper functions that can be imported separately to keep your main bundle size smaller. ### encodeForQuery The `encodeForQuery` function compresses and encodes objects into base64url format for use in query strings. This is particularly useful for complex criteria objects that need to be passed as URL parameters. Related issue: https://github.com/shopware/shopware/issues/12388 ```typescript import { encodeForQuery } from "@shopware/api-client/helpers"; // Example: Encoding complex search criteria const criteria = { page: 1, limit: 10, filter: [ { type: "equals", field: "active", value: true }, { type: "contains", field: "name", value: "smartphone" } ], associations: { manufacturer: {}, categories: { associations: { media: {} } } } }; // Use in URL apiClient.invoke("getProducts get /product", { query: { _criteria: encodeForQuery(encodedCriteria), }, }); ``` ## Links - [📘 Documentation](https://frontends.shopware.com) - [👥 Community Discord](https://discord.com/channels/1308047705309708348/1405501315160739951) (`#composable-frontend` channel) <!-- AUTO GENERATED CHANGELOG --> ## Changelog Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-client/CHANGELOG.md) ### Latest changes: 1.4.0 ### Minor Changes - [#2012](https://github.com/shopware/frontends/pull/2012) [`70dcf95`](https://github.com/shopware/frontends/commit/70dcf95d4370c63964d877a5cab113a53f93ca19) Thanks [@patzick](https://github.com/patzick)! - Added helper to support encoded `_criteria` field in GET query parameters. Context information: https://github.com/shopware/shopware/issues/12388 This helper is available under the `@shopware/api-client/helpers` import path. ```typescript import { encodeForQuery } from "@shopware/api-client/helpers"; const criteria = { page: 1, limit: 10, ... } const encodedCriteria = encodeForQuery(criteria); const result = await apiClient.invoke("getProducts get /product", { query: { _criteria: encodedCriteria, }, }); ``` - [#1959](https://github.com/shopware/frontends/pull/1959) [`c77daa6`](https://github.com/shopware/frontends/commit/c77daa6a11e96c7f3688b16f7da010b54c7f5e8b) Thanks [@patzick](https://github.com/patzick)! - Updated default types to Shopware 6.7