Skip to content

Commit ca23426

Browse files
authored
Add Storage API for Admin (#3095)
This pull request introduces a new `Storage` API for managing key-value data storage within extensions. It includes the implementation of the `Storage` interface, its integration into the `StandardApi`, and documentation for the new API. ### New `Storage` API Implementation: * Added a `Storage` interface with methods for setting, getting, deleting, and managing key-value pairs in the extension's storage. This includes support for batch operations and error handling for storage limits. (`packages/ui-extensions/src/surfaces/admin/api/storage/storage.ts`) ### Integration with `StandardApi`: * Updated the `StandardApi` interface to include the new `storage` property, allowing extensions to access the `Storage` API. (`packages/ui-extensions/src/surfaces/admin/api/standard/standard.ts`) ### Documentation: * Added a documentation file for the `Storage` API, describing its purpose, usage, and available methods. (`packages/ui-extensions/src/surfaces/admin/api/storage/storage.doc.ts`)
2 parents 00df1fa + f96f234 commit ca23426

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

packages/ui-extensions/src/surfaces/admin/api/standard/standard.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {I18n} from '../../../../api';
22
import {ApiVersion} from '../../../../shared';
3+
import type {Storage} from '../storage/storage';
34
import type {ExtensionTarget as AnyExtensionTarget} from '../../extension-targets';
45

56
export interface Intents {
@@ -54,6 +55,11 @@ export interface StandardApi<ExtensionTarget extends AnyExtensionTarget> {
5455
*/
5556
intents: Intents;
5657

58+
/**
59+
* Provides methods for setting, getting, and clearing browser data from the extension
60+
*/
61+
storage: Storage;
62+
5763
/**
5864
* Used to query the Admin GraphQL API
5965
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs';
2+
3+
const data: ReferenceEntityTemplateSchema = {
4+
name: 'Storage',
5+
description:
6+
"This API is used to store data in the extension. It allows you to set, get, and manage key-value pairs within the app's allocated storage space. This is useful for persisting state or configuration data across user sessions.",
7+
isVisualComponent: false,
8+
type: 'API',
9+
definitions: [
10+
{
11+
title: 'Storage',
12+
description: '',
13+
type: 'Storage',
14+
},
15+
],
16+
category: 'API',
17+
subCategory: 'Target APIs',
18+
related: [],
19+
};
20+
21+
export default data;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
export interface Storage<
2+
BaseStorageTypes extends Record<string, any> = Record<string, unknown>,
3+
> {
4+
/**
5+
* Sets the value of a key in the storage.
6+
*
7+
* @param key - The key to set the value for.
8+
* @param value - The value to set for the key.
9+
* Can be any primitive type supported by `JSON.stringify`.
10+
*
11+
* Rejects with a `StorageExceededError` if the extension exceeds its allotted storage limit.
12+
*/
13+
set<
14+
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
15+
Keys extends keyof StorageTypes = keyof StorageTypes,
16+
>(
17+
key: Keys,
18+
value: StorageTypes[Keys],
19+
): Promise<void>;
20+
21+
/**
22+
* Sets multiple key-value pairs in the storage at once.
23+
*
24+
* If the operation fails, no changes are made to storage.
25+
*
26+
* @param entries - An object containing key-value pairs to store.
27+
* Values can be any primitive type supported by `JSON.stringify`.
28+
*
29+
* Rejects with a `StorageExceededError` if the extension exceeds its allotted storage limit.
30+
*/
31+
setMany<StorageTypes extends BaseStorageTypes = BaseStorageTypes>(
32+
entries: Partial<StorageTypes>,
33+
): Promise<void>;
34+
35+
/**
36+
* Gets the value of a key in the storage.
37+
*
38+
* @param key - The key to get the value for.
39+
* @returns The value of the key.
40+
*
41+
* If no value for the key exists, the resolved value is undefined.
42+
*/
43+
get<
44+
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
45+
Keys extends keyof StorageTypes = keyof StorageTypes,
46+
>(
47+
key: Keys,
48+
): Promise<StorageTypes[Keys] | undefined>;
49+
50+
/**
51+
* Gets the values of multiple keys in the storage at once.
52+
*
53+
* @param keys - An array of keys to get the values for.
54+
* @returns An object containing key-value pairs for the requested keys.
55+
*
56+
* The returned array is in the same order as `keys`, with `undefined` values for keys that do not exist.
57+
*/
58+
getMany<
59+
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
60+
Keys extends keyof StorageTypes = keyof StorageTypes,
61+
>(
62+
keys: Keys[],
63+
): Promise<(StorageTypes[Keys] | undefined)[]>;
64+
65+
/**
66+
* Clears the storage.
67+
*/
68+
clear(): Promise<void>;
69+
70+
/**
71+
* Deletes a key from the storage.
72+
*
73+
* @param key - The key to delete.
74+
* @returns A promise that resolves to `true` if the key was deleted, or `false` if the key did not exist.
75+
*/
76+
delete<
77+
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
78+
Keys extends keyof StorageTypes = keyof StorageTypes,
79+
>(
80+
key: Keys,
81+
): Promise<boolean>;
82+
83+
/**
84+
* Deletes multiple keys from the storage at once.
85+
*
86+
* @param keys - An array of keys to delete.
87+
* @returns A promise that resolves to an object with `keys` keys, and boolean values,
88+
* which are `true` if the key was deleted, or `false` if the key did not exist.
89+
*/
90+
deleteMany<
91+
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
92+
Keys extends keyof StorageTypes = keyof StorageTypes,
93+
>(
94+
keys: Keys[],
95+
): Promise<Record<Keys, boolean>>;
96+
97+
/**
98+
* Gets all the keys and values in the storage.
99+
*
100+
* @returns An iterator containing all the keys and values in the storage.
101+
*/
102+
entries<
103+
StorageTypes extends BaseStorageTypes = BaseStorageTypes,
104+
Keys extends keyof StorageTypes = keyof StorageTypes,
105+
>(): Promise<IterableIterator<[Keys, StorageTypes[Keys]]>>;
106+
}
107+
108+
export interface StorageExceededError extends Error {
109+
name: 'StorageExceededError';
110+
}

0 commit comments

Comments
 (0)