From 021b7fcc18e315cf6010e840bf59d71acbc51db3 Mon Sep 17 00:00:00 2001 From: Kepler Vital Date: Mon, 28 Oct 2024 10:01:58 +0100 Subject: [PATCH] fix(didc-js): wasm-pack build targets bundler mode (#578) * fix(didc-js): wasm-pack build targets bundler mode * bump to 0.0.2 * fix typo --- tools/didc-js/README.md | 65 ------------------------- tools/didc-js/wasm-package/README.md | 24 ++------- tools/didc-js/wasm-package/build.sh | 2 +- tools/didc-js/wasm-package/package.json | 2 +- 4 files changed, 5 insertions(+), 88 deletions(-) diff --git a/tools/didc-js/README.md b/tools/didc-js/README.md index b7cd542c..020c1f5d 100644 --- a/tools/didc-js/README.md +++ b/tools/didc-js/README.md @@ -1,68 +1,3 @@ # @dfinity/didc A multi-purpose Candid tool for JavaScript projects, including encoding and decoding Candid values. - -## Usage - -```javascript -import { getServiceMethods, encode, decode } from "@dfinity/didc"; - -// The IDL in text format to be used, most canisters expose their IDL through -// the `candid:service` public metadata. -// -// You can fetch the IDL with an agent call or dfx with `dfx canister metadata candid:service` -export const IDL = ` - type StoreNumberInput = record { - number : nat64; - }; - - service : { - store_number : (input : StoreNumberInput) -> (); - get_number : () -> (nat64) query; - }; -`; - -// Gets the service methods from the IDL and returns an array of the methods. -// -// Example returned value: ['store_number', 'get_number'] -const methods = getServiceMethods(IDL); - -// Encodes a candid text representation of a value to a hex representation. -// -// Example returned value: '4449444c016c01c98dea8b0a7801005a00000000000000' -const encoded encode({ - idl: IDL, - input: "(record { number=90; })", - serviceMethod: "store_number", - targetFormat: "hex", -}); - -// Decodes a hex representation of a candid value to a text representation. -// -// Example returned value: '(90 : nat64)' -const decoded = decode({ - idl: IDL, - input: "4449444c0001785a00000000000000", - serviceMethod: "get_number", - inputFormat: "hex", - targetFormat: "candid", -}); -``` - -### Web Usage - -For web usage, you need to initialize the async wasm module before using the functions. - -```javascript -import init, { encode } from "@dfinity/didc"; - -await init(); - -// then you can use the functions because the wasm will be initialized -const encoded = encode({ - idl: IDL, - input: "(record { number=90; })", - serviceMethod: "store_number", - targetFormat: "hex", -}); -``` diff --git a/tools/didc-js/wasm-package/README.md b/tools/didc-js/wasm-package/README.md index b7cd542c..2d5954b9 100644 --- a/tools/didc-js/wasm-package/README.md +++ b/tools/didc-js/wasm-package/README.md @@ -27,17 +27,17 @@ export const IDL = ` // Example returned value: ['store_number', 'get_number'] const methods = getServiceMethods(IDL); -// Encodes a candid text representation of a value to a hex representation. +// Encodes a candid in text format to a hex representation. // // Example returned value: '4449444c016c01c98dea8b0a7801005a00000000000000' -const encoded encode({ +const encoded = encode({ idl: IDL, input: "(record { number=90; })", serviceMethod: "store_number", targetFormat: "hex", }); -// Decodes a hex representation of a candid value to a text representation. +// Decodes a hex representation of a candid value to a text format. // // Example returned value: '(90 : nat64)' const decoded = decode({ @@ -48,21 +48,3 @@ const decoded = decode({ targetFormat: "candid", }); ``` - -### Web Usage - -For web usage, you need to initialize the async wasm module before using the functions. - -```javascript -import init, { encode } from "@dfinity/didc"; - -await init(); - -// then you can use the functions because the wasm will be initialized -const encoded = encode({ - idl: IDL, - input: "(record { number=90; })", - serviceMethod: "store_number", - targetFormat: "hex", -}); -``` diff --git a/tools/didc-js/wasm-package/build.sh b/tools/didc-js/wasm-package/build.sh index 53f5a509..8c6a326c 100755 --- a/tools/didc-js/wasm-package/build.sh +++ b/tools/didc-js/wasm-package/build.sh @@ -120,5 +120,5 @@ if [[ "$BUILD_TARGET" == "nodejs" ]]; then else echo "Building the library for the web..." - ./node_modules/wasm-pack/run.js build --target $BUILD_TARGET --out-name $LIBRARY_NAME --out-dir $OUT_DIR $RUST_BUILD_MODE --no-pack + ./node_modules/wasm-pack/run.js build --target bundler --out-name $LIBRARY_NAME --out-dir $OUT_DIR $RUST_BUILD_MODE --no-pack fi diff --git a/tools/didc-js/wasm-package/package.json b/tools/didc-js/wasm-package/package.json index 84d21c52..904d900e 100644 --- a/tools/didc-js/wasm-package/package.json +++ b/tools/didc-js/wasm-package/package.json @@ -1,7 +1,7 @@ { "name": "@dfinity/didc", "description": "Utility tools for candid.", - "version": "0.0.1", + "version": "0.0.2", "author": "DFINITY Stiftung", "license": "Apache-2.0", "repository": "github:dfinity/candid",