Skip to content

Commit 71a823e

Browse files
authored
Merge pull request #2921 from Shopify/compliance/export-storage-inerface
Export storage API interfaces Refactor storage API
2 parents f67e803 + e00be66 commit 71a823e

File tree

9 files changed

+35
-13
lines changed

9 files changed

+35
-13
lines changed

.changeset/loud-kings-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/ui-extensions': patch
3+
---
4+
5+
Export storage API interfaces and update Storage.entries return type

packages/ui-extensions/src/surfaces/point-of-sale/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export type {
7676
SessionApi,
7777
} from './render/api/session-api/session-api';
7878

79+
export type {StorageApi} from './render/api/storage-api/storage-api';
80+
7981
export type {
8082
ShowToastOptions,
8183
ToastApiContent,
@@ -122,3 +124,5 @@ export type {
122124
export type {CountryCode} from './types/country-code';
123125

124126
export type {Session} from './types/session';
127+
export type {Storage} from './types/storage';
128+
export {StorageError} from './types/storage';

packages/ui-extensions/src/surfaces/point-of-sale/data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type {BaseData} from './event/data/BaseData';
2+
export type {BaseApi} from './event/data/BaseApi';
23
export type {ReprintReceiptData} from './event/data/ReprintReceiptData';
34
export type {TransactionCompleteData} from './event/data/TransactionCompleteData';
45
export type {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {StorageApi} from '../../render/api/storage-api/storage-api';
2+
3+
export type BaseApi = StorageApi;
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type {ConnectivityState, Device, Session} from '../../../point-of-sale';
2-
import {Storage} from '../../types/storage';
32

43
export interface BaseData {
54
connectivity: ConnectivityState;
65
device: Device;
76
locale: string;
87
session: Session;
9-
storage?: Storage;
108
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {BaseData} from './BaseData';
22
import {Cart} from '../../api';
3+
import {BaseApi} from './BaseApi';
34

4-
export interface CartUpdateEventData extends BaseData {
5+
export interface CartUpdateEventData extends BaseData, BaseApi {
56
cart: Cart;
67
}

packages/ui-extensions/src/surfaces/point-of-sale/event/data/CashTrackingSessionData.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {BaseData} from './BaseData';
2+
import {BaseApi} from './BaseApi';
23

3-
export interface CashTrackingSessionStartData extends BaseData {
4+
export interface CashTrackingSessionStartData extends BaseData, BaseApi {
45
cashTrackingSessionStart: {
56
id: number;
67
openingTime: string;
78
};
89
}
910

10-
export interface CashTrackingSessionCompleteData extends BaseData {
11+
export interface CashTrackingSessionCompleteData extends BaseData, BaseApi {
1112
cashTrackingSessionComplete: {
1213
id: number;
1314
openingTime: string;

packages/ui-extensions/src/surfaces/point-of-sale/event/data/TransactionCompleteData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {BaseData} from './BaseData';
22
import type {SaleTransactionData} from './SaleTransactionData';
33
import type {ExchangeTransactionData} from './ExchangeTransactionData';
44
import type {ReturnTransactionData} from './ReturnTransactionData';
5+
import {BaseApi} from './BaseApi';
56

6-
export interface TransactionCompleteData extends BaseData {
7+
export interface TransactionCompleteData extends BaseData, BaseApi {
78
transaction:
89
| SaleTransactionData
910
| ReturnTransactionData

packages/ui-extensions/src/surfaces/point-of-sale/types/storage.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
export interface StorageExceededError extends Error {
2-
name: 'StorageExceededError';
3-
code: 'RecordsCount' | 'RecordSize';
1+
export class StorageError extends Error {
2+
public name = 'StorageError';
3+
constructor(
4+
public code: 'RecordsCount' | 'RecordSize' | 'KeyType' | 'KeySize',
5+
message: string,
6+
) {
7+
super(message);
8+
}
49
}
510
export interface Storage<
611
BaseStorageTypes extends Record<string, any> = Record<string, unknown>,
@@ -11,8 +16,10 @@ export interface Storage<
1116
* @param key - The key to set the value for.
1217
* @param value - The value to set for the key.
1318
* Can be any primitive type supported by `JSON.stringify`.
14-
* @throws StorageExceededError if the extension exceeds its allotted storage limit.
15-
* @throws StorageExceededError if value exceeds its allotted storage limit.
19+
* @throws StorageError when:
20+
* the extension exceeds its allotted storage limit.
21+
* the value exceeds its allotted storage limit.
22+
* the key is not a string or exceeds its allotted size.
1623
*/
1724
set<
1825
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
@@ -28,6 +35,7 @@ export interface Storage<
2835
* @param key - The key to get the value for.
2936
* @returns The value of the key.
3037
* If no value for the key exists, the resolved value is undefined.
38+
* @throws StorageError when the key is not a string or exceeds its allotted size.
3139
*/
3240
get<
3341
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
@@ -56,10 +64,10 @@ export interface Storage<
5664
/**
5765
* Gets all the keys and values in the storage.
5866
*
59-
* @returns An iterator containing all the keys and values in the storage.
67+
* @returns An array containing all the keys and values in the storage.
6068
*/
6169
entries<
6270
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
6371
Keys extends keyof StorageTypes = keyof StorageTypes,
64-
>(): Promise<IterableIterator<[Keys, StorageTypes[Keys]]>>;
72+
>(): Promise<[Keys, StorageTypes[Keys]][]>;
6573
}

0 commit comments

Comments
 (0)