@myparcel-dev/pdk-admin 2.2.6 → 2.3.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/CHANGELOG.md +26 -0
- package/dist/actions/print/resolvePrintParameters.d.ts +1 -0
- package/dist/services/print/createPdfObjectUrl.d.ts +2 -0
- package/dist/services/print/index.d.ts +3 -0
- package/dist/services/print/labelsNotification.d.ts +5 -0
- package/dist/services/print/pollLabelPdf.d.ts +9 -0
- package/dist/stores/useNotificationStore.d.ts +15 -0
- package/dist/types/actions/endpoints.types.d.ts +1 -1
- package/dist/types/actions/parameters.types.d.ts +3 -0
- package/dist/types/common.types.d.ts +7 -0
- package/dist/types/sdk.types.d.ts +14 -1
- package/dist/utils/encodeArrayParameter.d.ts +1 -0
- package/package.json +2 -2
- package/src/actions/definitions/orders.spec.ts +59 -0
- package/src/actions/definitions/orders.ts +10 -5
- package/src/actions/print/openOrPrintPdf.spec.ts +119 -0
- package/src/actions/print/openOrPrintPdf.ts +48 -3
- package/src/actions/print/resolvePrintParameters.ts +13 -3
- package/src/services/print/createPdfObjectUrl.ts +6 -0
- package/src/services/print/index.ts +3 -0
- package/src/services/print/labelsNotification.ts +52 -0
- package/src/services/print/pollLabelPdf.spec.ts +58 -0
- package/src/services/print/pollLabelPdf.ts +47 -0
- package/src/stores/useNotificationStore.spec.ts +30 -0
- package/src/stores/useNotificationStore.ts +1 -1
- package/src/types/actions/endpoints.types.ts +1 -0
- package/src/types/actions/parameters.types.ts +2 -0
- package/src/types/common.types.ts +7 -0
- package/src/types/sdk.types.ts +16 -0
- package/src/utils/encodeArrayParameter.spec.ts +26 -0
- package/src/utils/encodeArrayParameter.ts +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
<!-- MONOWEAVE:BELOW -->
|
|
4
4
|
|
|
5
|
+
## [2.3.0](https://github.com/myparcelnl/js-pdk/compare/@myparcel-dev/pdk-admin@2.2.7...@myparcel-dev/pdk-admin@2.3.0) "@myparcel-dev/pdk-admin" (2026-10-15)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **admin:** wait for bulk label pdfs and show progress while printing ([#490](https://github.com/myparcelnl/js-pdk/issues/490)) ([c093d2d](https://github.com/myparcelnl/js-pdk/commit/c093d2d8fcaef29b5b2b6fbbce20570f76b631b9))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* **admin:** encode array parameters so proxies keep all ids ([#488](https://github.com/myparcelnl/js-pdk/issues/488)) ([acd1a2a](https://github.com/myparcelnl/js-pdk/commit/acd1a2ad5f8133f187516259bcecb85f5fb6f106))
|
|
16
|
+
* **admin:** stop removing all notifications when a mutation starts ([#489](https://github.com/myparcelnl/js-pdk/issues/489)) ([0a49a1d](https://github.com/myparcelnl/js-pdk/commit/0a49a1d4ee1c7de0793eac17c19417e102c71eee))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## [2.2.7](https://github.com/myparcelnl/js-pdk/compare/@myparcel-dev/pdk-admin@2.2.6...@myparcel-dev/pdk-admin@2.2.7) "@myparcel-dev/pdk-admin" (2026-09-25)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Bug Fixes
|
|
25
|
+
|
|
26
|
+
* **admin:** load the full order when the shipment options modal opens ([#445](https://github.com/myparcelnl/js-pdk/issues/445)) ([5ac4d1c](https://github.com/myparcelnl/js-pdk/commit/5ac4d1c87d469829cb25a9518239977d8e38e923))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
5
31
|
## [2.2.6](https://github.com/myparcelnl/js-pdk/compare/@myparcel-dev/pdk-admin@2.2.5...@myparcel-dev/pdk-admin@2.2.6) "@myparcel-dev/pdk-admin" (2026-09-24)
|
|
6
32
|
|
|
7
33
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { type ActionContext } from '../executors';
|
|
2
2
|
import { type ActionParameters, type PrintAction } from '../../types';
|
|
3
|
+
/** Resolves the label options, then shows the loading notification that openOrPrintPdf closes. */
|
|
3
4
|
export declare const resolvePrintParameters: <A extends PrintAction>(context: ActionContext<A>) => Promise<ActionParameters<A>>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const LABELS_NOTIFICATION_ID = "print-labels";
|
|
2
|
+
export declare const showLabelsLoading: () => void;
|
|
3
|
+
export declare const showLabelsReady: (open: () => void) => void;
|
|
4
|
+
export declare const showLabelsFailed: () => void;
|
|
5
|
+
export declare const hideLabelsNotification: () => void;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type PollOptions = {
|
|
2
|
+
interval?: number;
|
|
3
|
+
timeout?: number;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Ask the backend for a labels pdf until the API has generated it. Resolves with the base64 pdf.
|
|
7
|
+
*/
|
|
8
|
+
export declare const pollLabelPdf: (labelId: string, { interval, timeout }?: PollOptions) => Promise<string>;
|
|
9
|
+
export {};
|
|
@@ -10,6 +10,11 @@ export declare const useNotificationStore: import("pinia").StoreDefinition<"noti
|
|
|
10
10
|
timeout: boolean | number;
|
|
11
11
|
title?: string | undefined;
|
|
12
12
|
variant: import("@myparcel-dev/pdk-common").Variant;
|
|
13
|
+
loading?: boolean | undefined;
|
|
14
|
+
action?: {
|
|
15
|
+
label: string;
|
|
16
|
+
onClick: () => void;
|
|
17
|
+
} | undefined;
|
|
13
18
|
}[]>;
|
|
14
19
|
/**
|
|
15
20
|
* Add a notification to the store.
|
|
@@ -29,6 +34,11 @@ export declare const useNotificationStore: import("pinia").StoreDefinition<"noti
|
|
|
29
34
|
timeout: boolean | number;
|
|
30
35
|
title?: string | undefined;
|
|
31
36
|
variant: import("@myparcel-dev/pdk-common").Variant;
|
|
37
|
+
loading?: boolean | undefined;
|
|
38
|
+
action?: {
|
|
39
|
+
label: string;
|
|
40
|
+
onClick: () => void;
|
|
41
|
+
} | undefined;
|
|
32
42
|
}[]>;
|
|
33
43
|
/**
|
|
34
44
|
* Add a notification to the store.
|
|
@@ -48,6 +58,11 @@ export declare const useNotificationStore: import("pinia").StoreDefinition<"noti
|
|
|
48
58
|
timeout: boolean | number;
|
|
49
59
|
title?: string | undefined;
|
|
50
60
|
variant: import("@myparcel-dev/pdk-common").Variant;
|
|
61
|
+
loading?: boolean | undefined;
|
|
62
|
+
action?: {
|
|
63
|
+
label: string;
|
|
64
|
+
onClick: () => void;
|
|
65
|
+
} | undefined;
|
|
51
66
|
}[]>;
|
|
52
67
|
/**
|
|
53
68
|
* Add a notification to the store.
|
|
@@ -6,4 +6,4 @@ export type BackendMutationEndpoints = BackendEndpoint.CreateWebhooks | BackendE
|
|
|
6
6
|
/**
|
|
7
7
|
* All backend endpoints that are queries.
|
|
8
8
|
*/
|
|
9
|
-
export type BackendQueryEndpoints = BackendEndpoint.FetchContext | BackendEndpoint.FetchOrders | BackendEndpoint.FetchProducts | BackendEndpoint.FetchShipments | BackendEndpoint.FetchWebhooks | BackendEndpoint.ProxyCapabilities;
|
|
9
|
+
export type BackendQueryEndpoints = BackendEndpoint.FetchContext | BackendEndpoint.FetchLabelPdf | BackendEndpoint.FetchOrders | BackendEndpoint.FetchProducts | BackendEndpoint.FetchShipments | BackendEndpoint.FetchWebhooks | BackendEndpoint.ProxyCapabilities;
|
|
@@ -42,6 +42,9 @@ export interface EndpointMutationInputMap extends Record<BackendEndpoint, object
|
|
|
42
42
|
[BackendEndpoint.FetchShipments]: OrderIdParameters & ShipmentIdParameters;
|
|
43
43
|
[BackendEndpoint.UpdateShipments]: OrderIdParameters & ShipmentIdParameters;
|
|
44
44
|
[BackendEndpoint.PrintShipments]: OrderIdParameters & ShipmentIdParameters & OptionalFormParameters & LabelParameters;
|
|
45
|
+
[BackendEndpoint.FetchLabelPdf]: {
|
|
46
|
+
labelId: string;
|
|
47
|
+
};
|
|
45
48
|
[BackendEndpoint.FetchProducts]: ProductIdParameters;
|
|
46
49
|
[BackendEndpoint.UpdatePluginSettings]: FormParameters;
|
|
47
50
|
[BackendEndpoint.UpdateProductSettings]: ProductIdParameters & FormParameters;
|
|
@@ -22,6 +22,13 @@ export interface PdkNotification {
|
|
|
22
22
|
timeout?: boolean | number;
|
|
23
23
|
title?: string;
|
|
24
24
|
variant: Variant;
|
|
25
|
+
/** Shows a spinner next to the content. */
|
|
26
|
+
loading?: boolean;
|
|
27
|
+
/** Shows a button that runs onClick. */
|
|
28
|
+
action?: {
|
|
29
|
+
label: string;
|
|
30
|
+
onClick(): void;
|
|
31
|
+
};
|
|
25
32
|
}
|
|
26
33
|
export type ResolvedNotification = PdkNotification & {
|
|
27
34
|
id: NotificationId;
|
|
@@ -4,10 +4,15 @@ import { type AdminContextObject } from './context.types';
|
|
|
4
4
|
import { type WebhookDefinition } from './common.types';
|
|
5
5
|
export type PdfUrlResponse = {
|
|
6
6
|
url: string;
|
|
7
|
+
/** Hash of the `/pdfs/{hash}` link for the whole batch, not a label id. Poll it with fetchLabelPdf. */
|
|
8
|
+
labelId?: string;
|
|
7
9
|
};
|
|
8
10
|
export type PdfDataResponse = {
|
|
9
11
|
data: string;
|
|
10
12
|
};
|
|
13
|
+
export type PdfPendingResponse = {
|
|
14
|
+
pending: true;
|
|
15
|
+
};
|
|
11
16
|
interface BasePrintDefinition extends PdkEndpointDefinition {
|
|
12
17
|
formattedResponse: PdfUrlResponse | PdfDataResponse;
|
|
13
18
|
parameters: {
|
|
@@ -119,6 +124,14 @@ interface PrintShipmentsDefinition extends BasePrintDefinition {
|
|
|
119
124
|
interface PrintOrdersDefinition extends BasePrintDefinition {
|
|
120
125
|
name: BackendEndpoint.PrintOrders;
|
|
121
126
|
}
|
|
127
|
+
interface FetchLabelPdfDefinition extends PdkEndpointDefinition {
|
|
128
|
+
formattedResponse: PdfDataResponse | PdfPendingResponse;
|
|
129
|
+
name: BackendEndpoint.FetchLabelPdf;
|
|
130
|
+
parameters: {
|
|
131
|
+
labelId: string;
|
|
132
|
+
};
|
|
133
|
+
response: PdfDataResponse | PdfPendingResponse;
|
|
134
|
+
}
|
|
122
135
|
interface FetchWebhooksDefinition extends PdkEndpointDefinition {
|
|
123
136
|
name: BackendEndpoint.FetchWebhooks;
|
|
124
137
|
parameters: never;
|
|
@@ -207,6 +220,6 @@ export type ProxyCapabilitiesCall = (options: {
|
|
|
207
220
|
body: ProxyCapabilitiesBody;
|
|
208
221
|
parameters?: ProxyCapabilitiesParameters;
|
|
209
222
|
}) => Promise<ProxyCapabilitiesDefinition['response']>;
|
|
210
|
-
export type BackendEndpointDefinition = CreateWebhooksDefinition | DebugDownloadLogsDefinition | DebugSwitchToAcceptanceApiDefinition | DebugSwitchToProductionApiDefinition | DeleteAccountDefinition | DeleteShipmentsDefinition | DeleteWebhooksDefinition | ExportOrdersDefinition | FetchContextDefinition | FetchOrdersDefinition | FetchProductsDefinition | FetchShipmentsDefinition | FetchWebhooksDefinition | PrintOrdersDefinition | PrintShipmentsDefinition | ProxyCapabilitiesDefinition | UpdateAccountDefinition | UpdateOrdersDefinition | UpdatePluginSettingsDefinition | UpdateProductSettingsDefinition | UpdateShipmentsDefinition;
|
|
223
|
+
export type BackendEndpointDefinition = CreateWebhooksDefinition | DebugDownloadLogsDefinition | DebugSwitchToAcceptanceApiDefinition | DebugSwitchToProductionApiDefinition | DeleteAccountDefinition | DeleteShipmentsDefinition | DeleteWebhooksDefinition | ExportOrdersDefinition | FetchContextDefinition | FetchLabelPdfDefinition | FetchOrdersDefinition | FetchProductsDefinition | FetchShipmentsDefinition | FetchWebhooksDefinition | PrintOrdersDefinition | PrintShipmentsDefinition | ProxyCapabilitiesDefinition | UpdateAccountDefinition | UpdateOrdersDefinition | UpdatePluginSettingsDefinition | UpdateProductSettingsDefinition | UpdateShipmentsDefinition;
|
|
211
224
|
export type BackendEndpointOptions<N extends BackendEndpoint> = Omit<ExtractEndpointDefinition<N, BackendEndpointDefinition>, 'name' | 'response'>;
|
|
212
225
|
export {};
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { type OneOrMore } from '@myparcel-dev/ts-utils';
|
|
2
|
+
/** Joins values with `;` and URL-encodes the result, because the SDK appends parameters to the URL raw. */
|
|
2
3
|
export declare const encodeArrayParameter: (parameter?: null | OneOrMore<string | number | undefined>) => string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myparcel-dev/pdk-admin",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/myparcelnl/js-pdk.git",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@myparcel-dev/constants": "^2.9.0",
|
|
40
|
-
"@myparcel-dev/pdk-common": "^2.
|
|
40
|
+
"@myparcel-dev/pdk-common": "^2.3.0",
|
|
41
41
|
"@myparcel-dev/sdk": "^5.1.0",
|
|
42
42
|
"@myparcel-dev/vue-form-builder": "1.0.0-beta.42.6",
|
|
43
43
|
"@tanstack/vue-query": "~4.43.0",
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
4
|
+
import {type ActionContext} from '../executors/types';
|
|
5
|
+
import {useActionStore, useModalStore} from '../../stores';
|
|
6
|
+
import {AdminAction, AdminModalKey} from '../../data';
|
|
7
|
+
import {doComponentTestSetup, doComponentTestTeardown} from '../../__tests__';
|
|
8
|
+
import {ordersEditAction} from './orders';
|
|
9
|
+
|
|
10
|
+
const edit = (orderIds: string | string[]): Promise<unknown> =>
|
|
11
|
+
Promise.resolve(ordersEditAction.handler({parameters: {orderIds}} as unknown as ActionContext));
|
|
12
|
+
|
|
13
|
+
describe('ordersEditAction', () => {
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
doComponentTestSetup();
|
|
16
|
+
vi.mocked(useActionStore().dispatch).mockResolvedValue(undefined);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
doComponentTestTeardown();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('loads the order before it opens the modal', async () => {
|
|
24
|
+
let resolveFetch: () => void = () => undefined;
|
|
25
|
+
const dispatch = vi.mocked(useActionStore().dispatch).mockImplementation(
|
|
26
|
+
() =>
|
|
27
|
+
new Promise<void>((resolve) => {
|
|
28
|
+
resolveFetch = resolve;
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
const open = vi.mocked(useModalStore().open);
|
|
32
|
+
|
|
33
|
+
const handled = edit('TEST-1');
|
|
34
|
+
|
|
35
|
+
expect(dispatch).toHaveBeenCalledWith(AdminAction.OrdersFetch, {orderIds: 'TEST-1'});
|
|
36
|
+
expect(open).not.toHaveBeenCalled();
|
|
37
|
+
|
|
38
|
+
resolveFetch();
|
|
39
|
+
await handled;
|
|
40
|
+
|
|
41
|
+
expect(open).toHaveBeenCalledWith(AdminModalKey.ShipmentOptions, {orderIds: 'TEST-1'});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('does not load anything for a bulk selection', async () => {
|
|
45
|
+
await edit(['TEST-1', 'TEST-2']);
|
|
46
|
+
|
|
47
|
+
expect(useActionStore().dispatch).not.toHaveBeenCalled();
|
|
48
|
+
expect(useModalStore().open).toHaveBeenCalledWith(AdminModalKey.ShipmentOptions, {
|
|
49
|
+
orderIds: ['TEST-1', 'TEST-2'],
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('accepts a single id given as an array', async () => {
|
|
54
|
+
await edit(['TEST-1']);
|
|
55
|
+
|
|
56
|
+
expect(useActionStore().dispatch).toHaveBeenCalledWith(AdminAction.OrdersFetch, {orderIds: ['TEST-1']});
|
|
57
|
+
expect(useModalStore().opened).toBe(AdminModalKey.ShipmentOptions);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -5,7 +5,7 @@ import {createOrdersMutationHandler, createQueryHandler, executeNextAction, reso
|
|
|
5
5
|
import {defineAction} from '../defineAction';
|
|
6
6
|
import {getOrderShipmentIds} from '../../utils';
|
|
7
7
|
import {type OrderIds} from '../../types';
|
|
8
|
-
import {useModalStore} from '../../stores';
|
|
8
|
+
import {useActionStore, useModalStore} from '../../stores';
|
|
9
9
|
import {AdminAction, AdminIcon, AdminModalKey} from '../../data';
|
|
10
10
|
import {shipmentsUpdateAction} from './shipments';
|
|
11
11
|
import {useGlobalContext} from '../../composables/context/useGlobalContext';
|
|
@@ -17,11 +17,16 @@ export const ordersEditAction = defineAction({
|
|
|
17
17
|
name: AdminAction.OrdersEdit,
|
|
18
18
|
icon: AdminIcon.Edit,
|
|
19
19
|
label: 'action_edit',
|
|
20
|
-
handler(context) {
|
|
21
|
-
const
|
|
22
|
-
const {parameters} = context;
|
|
20
|
+
async handler(context) {
|
|
21
|
+
const {orderIds} = context.parameters;
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
// The order list seeds the cache with an order without its lines, so load the full one first.
|
|
24
|
+
// Bulk skips this: that form reads no per-order data.
|
|
25
|
+
if (toArray(orderIds ?? []).length === 1) {
|
|
26
|
+
await useActionStore().dispatch(AdminAction.OrdersFetch, {orderIds});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
useModalStore().open(AdminModalKey.ShipmentOptions, {orderIds});
|
|
25
30
|
},
|
|
26
31
|
});
|
|
27
32
|
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
4
|
+
import {createPinia, setActivePinia} from 'pinia';
|
|
5
|
+
import {Variant} from '@myparcel-dev/pdk-common';
|
|
6
|
+
import {useNotificationStore} from '../../stores';
|
|
7
|
+
import {LABELS_NOTIFICATION_ID, showLabelsLoading} from '../../services/print/labelsNotification';
|
|
8
|
+
import {openOrPrintPdf} from './openOrPrintPdf';
|
|
9
|
+
|
|
10
|
+
const {fetchLabelPdf, openUrlInNewTab, downloadFileFromUrl} = vi.hoisted(() => ({
|
|
11
|
+
fetchLabelPdf: vi.fn(),
|
|
12
|
+
openUrlInNewTab: vi.fn(),
|
|
13
|
+
downloadFileFromUrl: vi.fn(),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
vi.mock('../../sdk', () => ({usePdkAdminApi: () => ({fetchLabelPdf})}));
|
|
17
|
+
|
|
18
|
+
vi.mock('../../utils', async (importOriginal) => {
|
|
19
|
+
const actual = await importOriginal<typeof import('../../utils')>();
|
|
20
|
+
|
|
21
|
+
return {...actual, openUrlInNewTab, downloadFileFromUrl};
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
vi.mock('../../composables', async (importOriginal) => {
|
|
25
|
+
const actual = await importOriginal<typeof import('../../composables')>();
|
|
26
|
+
|
|
27
|
+
return {...actual, useLanguage: () => ({translate: (key: string) => key, has: () => true})};
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const labelsNotification = () => {
|
|
31
|
+
return useNotificationStore().notifications.find((notification) => notification.id === LABELS_NOTIFICATION_ID);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const print = (output: 'open' | 'download') => {
|
|
35
|
+
return openOrPrintPdf({
|
|
36
|
+
// @ts-expect-error only the fields openOrPrintPdf reads
|
|
37
|
+
response: {url: 'https://api/pdfs/label_hash', labelId: 'label_hash'},
|
|
38
|
+
// @ts-expect-error only the fields openOrPrintPdf reads
|
|
39
|
+
parameters: {orderIds: ['1', '2'], output},
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
describe('openOrPrintPdf with a label link', () => {
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
setActivePinia(createPinia());
|
|
46
|
+
vi.useFakeTimers();
|
|
47
|
+
fetchLabelPdf.mockReset();
|
|
48
|
+
openUrlInNewTab.mockReset();
|
|
49
|
+
downloadFileFromUrl.mockReset();
|
|
50
|
+
URL.createObjectURL = vi.fn(() => 'blob:labels');
|
|
51
|
+
URL.revokeObjectURL = vi.fn();
|
|
52
|
+
showLabelsLoading();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
vi.useRealTimers();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('shows a button to open the labels once the pdf is ready', async () => {
|
|
60
|
+
fetchLabelPdf.mockResolvedValueOnce({pending: true}).mockResolvedValueOnce({data: 'cGRm'});
|
|
61
|
+
|
|
62
|
+
const promise = print('open');
|
|
63
|
+
|
|
64
|
+
expect(labelsNotification()?.loading).toBe(true);
|
|
65
|
+
|
|
66
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
67
|
+
await promise;
|
|
68
|
+
|
|
69
|
+
const notification = labelsNotification();
|
|
70
|
+
|
|
71
|
+
expect(notification?.loading).toBeFalsy();
|
|
72
|
+
expect(notification?.variant).toBe(Variant.Success);
|
|
73
|
+
expect(openUrlInNewTab).not.toHaveBeenCalled();
|
|
74
|
+
|
|
75
|
+
notification?.action?.onClick();
|
|
76
|
+
|
|
77
|
+
expect(openUrlInNewTab).toHaveBeenCalledWith('blob:labels');
|
|
78
|
+
expect(labelsNotification()).toBeUndefined();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('downloads the pdf once it is ready when the output is download', async () => {
|
|
82
|
+
fetchLabelPdf.mockResolvedValueOnce({data: 'cGRm'});
|
|
83
|
+
|
|
84
|
+
await print('download');
|
|
85
|
+
|
|
86
|
+
expect(downloadFileFromUrl).toHaveBeenCalledWith('blob:labels', 'myparcel-labels.pdf');
|
|
87
|
+
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:labels');
|
|
88
|
+
expect(labelsNotification()).toBeUndefined();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('shows an error when the pdf is not ready in time', async () => {
|
|
92
|
+
fetchLabelPdf.mockResolvedValue({pending: true});
|
|
93
|
+
|
|
94
|
+
const promise = print('open');
|
|
95
|
+
|
|
96
|
+
await vi.advanceTimersByTimeAsync(11 * 60 * 1000);
|
|
97
|
+
await promise;
|
|
98
|
+
|
|
99
|
+
const notification = labelsNotification();
|
|
100
|
+
|
|
101
|
+
expect(notification?.variant).toBe(Variant.Error);
|
|
102
|
+
expect(notification?.loading).toBeFalsy();
|
|
103
|
+
expect(notification?.action).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('openOrPrintPdf without a response', () => {
|
|
108
|
+
beforeEach(() => {
|
|
109
|
+
setActivePinia(createPinia());
|
|
110
|
+
showLabelsLoading();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('removes the loading notification when the print request failed', async () => {
|
|
114
|
+
// @ts-expect-error the handler returns nothing when the request failed
|
|
115
|
+
await openOrPrintPdf({response: undefined, parameters: {orderIds: ['1']}});
|
|
116
|
+
|
|
117
|
+
expect(labelsNotification()).toBeUndefined();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -1,13 +1,58 @@
|
|
|
1
1
|
import {isOfType} from '@myparcel-dev/ts-utils';
|
|
2
2
|
import {type ActionContextWithResponse} from '../executors';
|
|
3
|
-
import {generateLabelFilename} from '../../utils';
|
|
4
|
-
import {type PdfDataResponse, type PrintAction} from '../../types';
|
|
5
|
-
import {
|
|
3
|
+
import {downloadFileFromUrl, generateLabelFilename, openUrlInNewTab} from '../../utils';
|
|
4
|
+
import {type PdfDataResponse, type PdfUrlResponse, type PrintAction} from '../../types';
|
|
5
|
+
import {
|
|
6
|
+
createPdfObjectUrl,
|
|
7
|
+
downloadFile,
|
|
8
|
+
hideLabelsNotification,
|
|
9
|
+
openPdfInNewWindow,
|
|
10
|
+
pollLabelPdf,
|
|
11
|
+
showLabelsFailed,
|
|
12
|
+
showLabelsReady,
|
|
13
|
+
} from '../../services';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A link means the API prepares the pdf in the background. Wait for it, then offer a button to
|
|
17
|
+
* open it, because browsers block a new tab that does not come directly from a click.
|
|
18
|
+
*/
|
|
19
|
+
const openPreparedPdf = async <A extends PrintAction>(
|
|
20
|
+
labelId: string,
|
|
21
|
+
parameters: ActionContextWithResponse<A>['parameters'],
|
|
22
|
+
): Promise<void> => {
|
|
23
|
+
let url: string;
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
url = createPdfObjectUrl(await pollLabelPdf(labelId));
|
|
27
|
+
} catch (error) {
|
|
28
|
+
showLabelsFailed();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (parameters?.output === 'download') {
|
|
33
|
+
hideLabelsNotification();
|
|
34
|
+
downloadFileFromUrl(url, generateLabelFilename(parameters));
|
|
35
|
+
URL.revokeObjectURL(url);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
showLabelsReady(() => openUrlInNewTab(url));
|
|
40
|
+
};
|
|
6
41
|
|
|
7
42
|
export const openOrPrintPdf = async <A extends PrintAction>({
|
|
8
43
|
response,
|
|
9
44
|
parameters,
|
|
10
45
|
}: ActionContextWithResponse<A>): Promise<void> => {
|
|
46
|
+
if (isOfType<PdfUrlResponse>(response, 'labelId') && response.labelId) {
|
|
47
|
+
return openPreparedPdf(response.labelId, parameters);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
hideLabelsNotification();
|
|
51
|
+
|
|
52
|
+
if (!response) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
11
56
|
if (isOfType<PdfDataResponse>(response, 'data')) {
|
|
12
57
|
return openPdfInNewWindow(response.data);
|
|
13
58
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import {toArray} from '@myparcel-dev/ts-utils';
|
|
2
2
|
import {type ActionContext} from '../executors';
|
|
3
3
|
import {type ActionParameters, type PrintAction} from '../../types';
|
|
4
|
+
import {showLabelsLoading} from '../../services';
|
|
4
5
|
import {usePluginSettings} from '../../composables';
|
|
5
6
|
import {waitForLabelPrompt} from './waitForLabelPrompt';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
context: ActionContext<A>,
|
|
9
|
-
): Promise<ActionParameters<A>> => {
|
|
8
|
+
const resolveParameters = <A extends PrintAction>(context: ActionContext<A>): Promise<ActionParameters<A>> => {
|
|
10
9
|
const pluginSettings = usePluginSettings();
|
|
11
10
|
|
|
12
11
|
if (!pluginSettings.label.prompt) {
|
|
@@ -22,3 +21,14 @@ export const resolvePrintParameters = <A extends PrintAction>(
|
|
|
22
21
|
|
|
23
22
|
return waitForLabelPrompt(context);
|
|
24
23
|
};
|
|
24
|
+
|
|
25
|
+
/** Resolves the label options, then shows the loading notification that openOrPrintPdf closes. */
|
|
26
|
+
export const resolvePrintParameters = async <A extends PrintAction>(
|
|
27
|
+
context: ActionContext<A>,
|
|
28
|
+
): Promise<ActionParameters<A>> => {
|
|
29
|
+
const parameters = await resolveParameters(context);
|
|
30
|
+
|
|
31
|
+
showLabelsLoading();
|
|
32
|
+
|
|
33
|
+
return parameters;
|
|
34
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Turns a base64 pdf into an object url that a link can open or download. */
|
|
2
|
+
export const createPdfObjectUrl = (pdf: string): string => {
|
|
3
|
+
const bytes = Uint8Array.from(atob(pdf), (char) => char.charCodeAt(0));
|
|
4
|
+
|
|
5
|
+
return URL.createObjectURL(new Blob([bytes], {type: 'application/pdf'}));
|
|
6
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {Variant} from '@myparcel-dev/pdk-common';
|
|
2
|
+
import {type PdkNotification} from '../../types';
|
|
3
|
+
import {useNotificationStore} from '../../stores';
|
|
4
|
+
import {NotificationCategory} from '../../data';
|
|
5
|
+
import {useLanguage} from '../../composables';
|
|
6
|
+
|
|
7
|
+
export const LABELS_NOTIFICATION_ID = 'print-labels';
|
|
8
|
+
|
|
9
|
+
const replace = (notification: Omit<PdkNotification, 'id' | 'category'>): void => {
|
|
10
|
+
const store = useNotificationStore();
|
|
11
|
+
|
|
12
|
+
store.remove(LABELS_NOTIFICATION_ID);
|
|
13
|
+
store.add({...notification, id: LABELS_NOTIFICATION_ID, category: NotificationCategory.General});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const showLabelsLoading = (): void => {
|
|
17
|
+
replace({
|
|
18
|
+
variant: Variant.Info,
|
|
19
|
+
content: useLanguage().translate('notification_labels_loading'),
|
|
20
|
+
loading: true,
|
|
21
|
+
timeout: false,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const showLabelsReady = (open: () => void): void => {
|
|
26
|
+
const {translate} = useLanguage();
|
|
27
|
+
|
|
28
|
+
replace({
|
|
29
|
+
variant: Variant.Success,
|
|
30
|
+
content: translate('notification_labels_ready'),
|
|
31
|
+
timeout: false,
|
|
32
|
+
action: {
|
|
33
|
+
label: translate('action_open_labels'),
|
|
34
|
+
onClick() {
|
|
35
|
+
open();
|
|
36
|
+
hideLabelsNotification();
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const showLabelsFailed = (): void => {
|
|
43
|
+
replace({
|
|
44
|
+
variant: Variant.Error,
|
|
45
|
+
content: useLanguage().translate('notification_labels_failed'),
|
|
46
|
+
timeout: false,
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const hideLabelsNotification = (): void => {
|
|
51
|
+
useNotificationStore().remove(LABELS_NOTIFICATION_ID);
|
|
52
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
2
|
+
import {pollLabelPdf} from './pollLabelPdf';
|
|
3
|
+
|
|
4
|
+
const fetchLabelPdf = vi.fn();
|
|
5
|
+
|
|
6
|
+
vi.mock('../../sdk', () => ({usePdkAdminApi: () => ({fetchLabelPdf})}));
|
|
7
|
+
|
|
8
|
+
describe('pollLabelPdf', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
vi.useFakeTimers();
|
|
11
|
+
fetchLabelPdf.mockReset();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
vi.useRealTimers();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('asks the backend again until the pdf is ready', async () => {
|
|
19
|
+
fetchLabelPdf
|
|
20
|
+
.mockResolvedValueOnce({pending: true})
|
|
21
|
+
.mockResolvedValueOnce({pending: true})
|
|
22
|
+
.mockResolvedValueOnce({data: 'cGRm'});
|
|
23
|
+
|
|
24
|
+
const promise = pollLabelPdf('label_hash', {interval: 1000, timeout: 10_000});
|
|
25
|
+
|
|
26
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
27
|
+
|
|
28
|
+
await expect(promise).resolves.toBe('cGRm');
|
|
29
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(3);
|
|
30
|
+
expect(fetchLabelPdf).toHaveBeenCalledWith({parameters: {labelId: 'label_hash'}});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('gives up when the pdf is not ready before the timeout', async () => {
|
|
34
|
+
fetchLabelPdf.mockResolvedValue({pending: true});
|
|
35
|
+
|
|
36
|
+
const promise = pollLabelPdf('label_hash', {interval: 1000, timeout: 3000});
|
|
37
|
+
const assertion = expect(promise).rejects.toThrow('timed out');
|
|
38
|
+
|
|
39
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
40
|
+
|
|
41
|
+
await assertion;
|
|
42
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(4);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('stops when the backend reports an error', async () => {
|
|
46
|
+
fetchLabelPdf.mockRejectedValueOnce(new Error('api error'));
|
|
47
|
+
|
|
48
|
+
await expect(pollLabelPdf('label_hash', {interval: 1000, timeout: 3000})).rejects.toThrow('api error');
|
|
49
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(1);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('stops when the backend returns an unknown response', async () => {
|
|
53
|
+
fetchLabelPdf.mockResolvedValueOnce('<html>Fatal error</html>');
|
|
54
|
+
|
|
55
|
+
await expect(pollLabelPdf('label_hash', {interval: 1000, timeout: 3000})).rejects.toThrow('Unexpected response');
|
|
56
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(1);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {isOfType} from '@myparcel-dev/ts-utils';
|
|
2
|
+
import {type PdfDataResponse, type PdfPendingResponse} from '../../types';
|
|
3
|
+
import {usePdkAdminApi} from '../../sdk';
|
|
4
|
+
|
|
5
|
+
type PollOptions = {
|
|
6
|
+
interval?: number;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const DEFAULT_INTERVAL = 3000;
|
|
11
|
+
|
|
12
|
+
/** Ten minutes. */
|
|
13
|
+
const DEFAULT_TIMEOUT = 600_000;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Ask the backend for a labels pdf until the API has generated it. Resolves with the base64 pdf.
|
|
17
|
+
*/
|
|
18
|
+
export const pollLabelPdf = async (
|
|
19
|
+
labelId: string,
|
|
20
|
+
{interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT}: PollOptions = {},
|
|
21
|
+
): Promise<string> => {
|
|
22
|
+
const pdk = usePdkAdminApi();
|
|
23
|
+
const start = Date.now();
|
|
24
|
+
|
|
25
|
+
for (;;) {
|
|
26
|
+
const response = (await pdk.fetchLabelPdf({
|
|
27
|
+
// @ts-expect-error custom endpoints are not typed correctly
|
|
28
|
+
parameters: {labelId},
|
|
29
|
+
})) as PdfDataResponse | PdfPendingResponse;
|
|
30
|
+
|
|
31
|
+
if (isOfType<PdfDataResponse>(response, 'data')) {
|
|
32
|
+
return response.data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!isOfType<PdfPendingResponse>(response, 'pending')) {
|
|
36
|
+
throw new Error(`Unexpected response while polling for label pdf ${labelId}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (Date.now() - start >= timeout) {
|
|
40
|
+
throw new Error(`Polling for label pdf ${labelId} timed out`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await new Promise((resolve) => {
|
|
44
|
+
setTimeout(resolve, interval);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it} from 'vitest';
|
|
2
|
+
import {createPinia, setActivePinia} from 'pinia';
|
|
3
|
+
import {Variant} from '@myparcel-dev/pdk-common';
|
|
4
|
+
import {NotificationCategory, NotificationSource} from '../data';
|
|
5
|
+
import {useNotificationStore} from './useNotificationStore';
|
|
6
|
+
|
|
7
|
+
const ids = () => useNotificationStore().notifications.map((notification) => notification.id);
|
|
8
|
+
|
|
9
|
+
describe('useNotificationStore', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
setActivePinia(createPinia());
|
|
12
|
+
|
|
13
|
+
const store = useNotificationStore();
|
|
14
|
+
|
|
15
|
+
store.add({id: 'from-api', variant: Variant.Error, timeout: false}, {source: NotificationSource.Api});
|
|
16
|
+
store.add({id: 'labels', variant: Variant.Info, category: NotificationCategory.General, timeout: false});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('removes only the notifications from the given source', () => {
|
|
20
|
+
useNotificationStore().remove(NotificationSource.Api);
|
|
21
|
+
|
|
22
|
+
expect(ids()).toEqual(['labels']);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('removes a notification by id', () => {
|
|
26
|
+
useNotificationStore().remove('labels');
|
|
27
|
+
|
|
28
|
+
expect(ids()).toEqual(['from-api']);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -54,7 +54,7 @@ export const useNotificationStore = defineStore('notifications', () => {
|
|
|
54
54
|
let filter: (notification: ResolvedNotification) => boolean;
|
|
55
55
|
|
|
56
56
|
if (isEnumValue(input, NotificationSource)) {
|
|
57
|
-
filter = (notification) => notification.tags?.source
|
|
57
|
+
filter = (notification) => notification.tags?.source !== input;
|
|
58
58
|
} else {
|
|
59
59
|
filter = (notification) => notification.id !== input;
|
|
60
60
|
}
|
|
@@ -61,6 +61,8 @@ export interface EndpointMutationInputMap extends Record<BackendEndpoint, object
|
|
|
61
61
|
[BackendEndpoint.UpdateShipments]: OrderIdParameters & ShipmentIdParameters;
|
|
62
62
|
[BackendEndpoint.PrintShipments]: OrderIdParameters & ShipmentIdParameters & OptionalFormParameters & LabelParameters;
|
|
63
63
|
|
|
64
|
+
[BackendEndpoint.FetchLabelPdf]: {labelId: string};
|
|
65
|
+
|
|
64
66
|
[BackendEndpoint.FetchProducts]: ProductIdParameters;
|
|
65
67
|
|
|
66
68
|
[BackendEndpoint.UpdatePluginSettings]: FormParameters;
|
|
@@ -24,6 +24,13 @@ export interface PdkNotification {
|
|
|
24
24
|
timeout?: boolean | number;
|
|
25
25
|
title?: string;
|
|
26
26
|
variant: Variant;
|
|
27
|
+
/** Shows a spinner next to the content. */
|
|
28
|
+
loading?: boolean;
|
|
29
|
+
/** Shows a button that runs onClick. */
|
|
30
|
+
action?: {
|
|
31
|
+
label: string;
|
|
32
|
+
onClick(): void;
|
|
33
|
+
};
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
export type ResolvedNotification = PdkNotification & {
|
package/src/types/sdk.types.ts
CHANGED
|
@@ -17,12 +17,18 @@ import {type WebhookDefinition} from './common.types';
|
|
|
17
17
|
|
|
18
18
|
export type PdfUrlResponse = {
|
|
19
19
|
url: string;
|
|
20
|
+
/** Hash of the `/pdfs/{hash}` link for the whole batch, not a label id. Poll it with fetchLabelPdf. */
|
|
21
|
+
labelId?: string;
|
|
20
22
|
};
|
|
21
23
|
|
|
22
24
|
export type PdfDataResponse = {
|
|
23
25
|
data: string;
|
|
24
26
|
};
|
|
25
27
|
|
|
28
|
+
export type PdfPendingResponse = {
|
|
29
|
+
pending: true;
|
|
30
|
+
};
|
|
31
|
+
|
|
26
32
|
interface BasePrintDefinition extends PdkEndpointDefinition {
|
|
27
33
|
formattedResponse: PdfUrlResponse | PdfDataResponse;
|
|
28
34
|
parameters: {
|
|
@@ -149,6 +155,15 @@ interface PrintOrdersDefinition extends BasePrintDefinition {
|
|
|
149
155
|
name: BackendEndpoint.PrintOrders;
|
|
150
156
|
}
|
|
151
157
|
|
|
158
|
+
interface FetchLabelPdfDefinition extends PdkEndpointDefinition {
|
|
159
|
+
formattedResponse: PdfDataResponse | PdfPendingResponse;
|
|
160
|
+
name: BackendEndpoint.FetchLabelPdf;
|
|
161
|
+
parameters: {
|
|
162
|
+
labelId: string;
|
|
163
|
+
};
|
|
164
|
+
response: PdfDataResponse | PdfPendingResponse;
|
|
165
|
+
}
|
|
166
|
+
|
|
152
167
|
interface FetchWebhooksDefinition extends PdkEndpointDefinition {
|
|
153
168
|
name: BackendEndpoint.FetchWebhooks;
|
|
154
169
|
parameters: never;
|
|
@@ -247,6 +262,7 @@ export type BackendEndpointDefinition =
|
|
|
247
262
|
| DeleteWebhooksDefinition
|
|
248
263
|
| ExportOrdersDefinition
|
|
249
264
|
| FetchContextDefinition
|
|
265
|
+
| FetchLabelPdfDefinition
|
|
250
266
|
| FetchOrdersDefinition
|
|
251
267
|
| FetchProductsDefinition
|
|
252
268
|
| FetchShipmentsDefinition
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {describe, expect, it} from 'vitest';
|
|
2
|
+
import {encodeArrayParameter} from './encodeArrayParameter';
|
|
3
|
+
|
|
4
|
+
describe('encodeArrayParameter', () => {
|
|
5
|
+
it('returns a single value unchanged', () => {
|
|
6
|
+
expect(encodeArrayParameter('290')).toBe('290');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// A raw `;` is rewritten to `&` by some proxies (e.g. Traefik), which drops every id after the first.
|
|
10
|
+
it('joins multiple values with an encoded semicolon', () => {
|
|
11
|
+
expect(encodeArrayParameter(['290', 289, '288'])).toBe('290%3B289%3B288');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('skips undefined and null values', () => {
|
|
15
|
+
expect(encodeArrayParameter(['290', undefined, '288'])).toBe('290%3B288');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('returns an empty string for no value', () => {
|
|
19
|
+
expect(encodeArrayParameter(undefined)).toBe('');
|
|
20
|
+
expect(encodeArrayParameter(null)).toBe('');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('encodes characters that would break the query string', () => {
|
|
24
|
+
expect(encodeArrayParameter(['a&b', 'c#d'])).toBe('a%26b%3Bc%23d');
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {isDef} from '@vueuse/core';
|
|
2
2
|
import {type OneOrMore, toArray} from '@myparcel-dev/ts-utils';
|
|
3
3
|
|
|
4
|
+
/** Joins values with `;` and URL-encodes the result, because the SDK appends parameters to the URL raw. */
|
|
4
5
|
export const encodeArrayParameter = (parameter?: null | OneOrMore<string | number | undefined>): string => {
|
|
5
|
-
return toArray(parameter).filter(isDef).join(';');
|
|
6
|
+
return encodeURIComponent(toArray(parameter).filter(isDef).join(';'));
|
|
6
7
|
};
|