@shopware/api-client 1.2.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -8
- package/api-types/adminApiSchema.json +76278 -51743
- package/api-types/adminApiTypes.d.ts +17039 -25893
- package/api-types/storeApiSchema.6.6.10.json +25865 -0
- package/api-types/storeApiSchema.6.6.10.overrides.json +1011 -0
- package/api-types/storeApiSchema.b2b.overrides.json +5 -0
- package/api-types/storeApiSchema.json +8850 -3696
- package/api-types/storeApiSchema.overrides.json +43 -871
- package/api-types/storeApiTypes.d.ts +2612 -651
- package/dist/helpers.cjs +13 -0
- package/dist/helpers.d.cts +9 -0
- package/dist/helpers.d.mts +9 -0
- package/dist/helpers.d.ts +9 -0
- package/dist/helpers.mjs +11 -0
- package/dist/index.cjs +57 -32
- package/dist/index.d.cts +23055 -29933
- package/dist/index.d.mts +23055 -29933
- package/dist/index.d.ts +23055 -29933
- package/dist/index.mjs +57 -32
- package/package.json +16 -6
- package/api-types/storeApiTypes.overrides.ts +0 -48
package/README.md
CHANGED
|
@@ -10,7 +10,6 @@ You can use types generated from your custom API instance to have autocompletion
|
|
|
10
10
|
|
|
11
11
|
To generate your own types use [@shopware/api-gen](https://www.npmjs.com/package/@shopware/api-gen) CLI.
|
|
12
12
|
|
|
13
|
-
To take a deep dive into the topic visit the [🧑🎓 API Client Tutorial](https://api-client-tutorial-composable-frontends.pages.dev) first.
|
|
14
13
|
|
|
15
14
|
## Setup
|
|
16
15
|
|
|
@@ -33,6 +32,9 @@ pnpm install @shopware/api-client
|
|
|
33
32
|
|
|
34
33
|
# bun
|
|
35
34
|
bun install @shopware/api-client
|
|
35
|
+
|
|
36
|
+
# deno
|
|
37
|
+
deno install @shopware/api-client
|
|
36
38
|
```
|
|
37
39
|
|
|
38
40
|
<!-- /automd -->
|
|
@@ -126,7 +128,7 @@ export const adminApiClient = createAdminAPIClient<operations>({
|
|
|
126
128
|
sessionData: JSON.parse(Cookies.get("sw-admin-session-data") || "{}"),
|
|
127
129
|
});
|
|
128
130
|
|
|
129
|
-
adminApiClient.
|
|
131
|
+
adminApiClient.hook("onAuthChange", (sessionData) => {
|
|
130
132
|
Cookies.set("sw-admin-session-data", JSON.stringify(sessionData), {
|
|
131
133
|
expires: 1, // days
|
|
132
134
|
path: "/",
|
|
@@ -263,15 +265,88 @@ apiClient.hook("onDefaultHeaderChanged", (key, value) => {
|
|
|
263
265
|
});
|
|
264
266
|
```
|
|
265
267
|
|
|
268
|
+
Available hooks:
|
|
269
|
+
|
|
270
|
+
- `onContextChanged`: Triggered when context token changes
|
|
271
|
+
- `onResponseError`: Triggered when API returns an error
|
|
272
|
+
- `onSuccessResponse`: Triggered when API request succeeds
|
|
273
|
+
- `onDefaultHeaderChanged`: Triggered when default headers are modified
|
|
274
|
+
- `onRequest`: Triggered before each request is made, allowing for request inspection and modification
|
|
275
|
+
|
|
266
276
|
calling `apiClient.hook` will autocomplete the list of available hooks.
|
|
267
277
|
|
|
268
|
-
|
|
278
|
+
### Base Configuration Management
|
|
279
|
+
|
|
280
|
+
The API client provides methods to manage its base configuration:
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
// Get current configuration
|
|
284
|
+
const config = apiClient.getBaseConfig();
|
|
285
|
+
console.log(config.baseURL); // "https://demo-frontends.shopware.store/store-api"
|
|
286
|
+
console.log(config.accessToken); // "SWSCBHFSNTVMAWNZDNFKSHLAYW"
|
|
287
|
+
|
|
288
|
+
// Update configuration
|
|
289
|
+
apiClient.updateBaseConfig({
|
|
290
|
+
baseURL: "https://new-url.com/store-api",
|
|
291
|
+
accessToken: "NEW_TOKEN",
|
|
292
|
+
});
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
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.
|
|
296
|
+
|
|
297
|
+
## Helper Functions
|
|
298
|
+
|
|
299
|
+
The API client provides helper functions that can be imported separately to keep your main bundle size smaller.
|
|
300
|
+
|
|
301
|
+
### encodeForQuery
|
|
302
|
+
|
|
303
|
+
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.
|
|
304
|
+
|
|
305
|
+
Related issue: https://github.com/shopware/shopware/issues/12388
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { encodeForQuery } from "@shopware/api-client/helpers";
|
|
309
|
+
|
|
310
|
+
// Example: Encoding complex search criteria
|
|
311
|
+
const criteria = {
|
|
312
|
+
page: 1,
|
|
313
|
+
limit: 10,
|
|
314
|
+
filter: [
|
|
315
|
+
{
|
|
316
|
+
type: "equals",
|
|
317
|
+
field: "active",
|
|
318
|
+
value: true
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
type: "contains",
|
|
322
|
+
field: "name",
|
|
323
|
+
value: "smartphone"
|
|
324
|
+
}
|
|
325
|
+
],
|
|
326
|
+
associations: {
|
|
327
|
+
manufacturer: {},
|
|
328
|
+
categories: {
|
|
329
|
+
associations: {
|
|
330
|
+
media: {}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
269
335
|
|
|
270
|
-
|
|
336
|
+
// Use in URL
|
|
337
|
+
apiClient.invoke("getProducts get /product", {
|
|
338
|
+
query: {
|
|
339
|
+
_criteria: encodeForQuery(encodedCriteria),
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
## Links
|
|
271
346
|
|
|
272
347
|
- [📘 Documentation](https://frontends.shopware.com)
|
|
273
348
|
|
|
274
|
-
- [👥 Community
|
|
349
|
+
- [👥 Community Discord](https://discord.com/channels/1308047705309708348/1405501315160739951) (`#composable-frontend` channel)
|
|
275
350
|
|
|
276
351
|
<!-- AUTO GENERATED CHANGELOG -->
|
|
277
352
|
|
|
@@ -279,8 +354,31 @@ calling `apiClient.hook` will autocomplete the list of available hooks.
|
|
|
279
354
|
|
|
280
355
|
Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-client/CHANGELOG.md)
|
|
281
356
|
|
|
282
|
-
### Latest changes: 1.
|
|
357
|
+
### Latest changes: 1.4.0
|
|
358
|
+
|
|
359
|
+
### Minor Changes
|
|
283
360
|
|
|
284
|
-
|
|
361
|
+
- [#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.
|
|
362
|
+
Context information: https://github.com/shopware/shopware/issues/12388
|
|
363
|
+
|
|
364
|
+
This helper is available under the `@shopware/api-client/helpers` import path.
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
import { encodeForQuery } from "@shopware/api-client/helpers";
|
|
368
|
+
|
|
369
|
+
const criteria = {
|
|
370
|
+
page: 1,
|
|
371
|
+
limit: 10,
|
|
372
|
+
...
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const encodedCriteria = encodeForQuery(criteria);
|
|
376
|
+
|
|
377
|
+
const result = await apiClient.invoke("getProducts get /product", {
|
|
378
|
+
query: {
|
|
379
|
+
_criteria: encodedCriteria,
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
```
|
|
285
383
|
|
|
286
|
-
- [#
|
|
384
|
+
- [#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
|