Skip to content

Commit

Permalink
docs: add examples for Near host functions
Browse files Browse the repository at this point in the history
  • Loading branch information
denbite committed Nov 10, 2024
1 parent b58ac04 commit 8f00843
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/near-sdk-js/lib/api.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions packages/near-sdk-js/lib/api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions packages/near-sdk-js/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ export function storageReadRaw(key: Uint8Array): Uint8Array | null {
* Reads the utf-8 string value from NEAR storage that is stored under the provided key.
*
* @param key - The utf-8 string key to read from storage.
*
* @example
* ```ts
* import { near } from "near-sdk-js";
*
* near.storageRead("key"); // -> null
* near.storageWrite("key", "value");
* near.storageRead("key"); // -> "value"
* ```
*/
export function storageRead(key: string): string | null {
const ret = storageReadRaw(encode(key));
Expand All @@ -315,6 +324,15 @@ export function storageHasKeyRaw(key: Uint8Array): boolean {
* Checks for the existence of a value under the provided utf-8 string key in NEAR storage.
*
* @param key - The utf-8 string key to check for in storage.
*
* @example
* ```ts
* import { near } from "near-sdk-js";
*
* near.storageHasKey("key"); // -> false
* near.storageWrite("key", "value");
* near.storageHasKey("key"); // -> true
* ```
*/
export function storageHasKey(key: string): boolean {
return storageHasKeyRaw(encode(key));
Expand Down Expand Up @@ -356,6 +374,15 @@ export function storageWriteRaw(key: Uint8Array, value: Uint8Array): boolean {
*
* @param key - The utf-8 string key under which to store the value.
* @param value - The utf-8 string value to store.
*
* @example
* ```ts
* import { near } from "near-sdk-js";
*
* near.storageRead("key"); // -> null
* near.storageWrite("key", "value");
* near.storageRead("key"); // -> "value"
* ```
*/
export function storageWrite(key: string, value: string): boolean {
return storageWriteRaw(encode(key), encode(value));
Expand All @@ -374,6 +401,17 @@ export function storageRemoveRaw(key: Uint8Array): boolean {
* Removes the value of the provided utf-8 string key from NEAR storage.
*
* @param key - The utf-8 string key to be removed.
*
* @returns Removes the value stored under the given key. If key-value existed returns true, otherwise false.
*
* @example
* ```ts
* import { near } from "near-sdk-js";
*
* near.storageRemove("key"); // -> false
* near.storageWrite("key", "value");
* near.storageRemove("key"); // -> true
* ```
*/
export function storageRemove(key: string): boolean {
return storageRemoveRaw(encode(key));
Expand Down

0 comments on commit 8f00843

Please sign in to comment.