Skip to content

Commit

Permalink
Merge pull request #1854 from demergent-labs/manual
Browse files Browse the repository at this point in the history
Manual
  • Loading branch information
lastmjs authored Jul 8, 2024
2 parents 9905546 + c3bf0e4 commit a8e81fb
Show file tree
Hide file tree
Showing 18 changed files with 4,286 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/manual_reply/package-lock.json

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

1 change: 1 addition & 0 deletions examples/manual_reply/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "manual_reply_end_to_end_test_functional_syntax",
"scripts": {
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "ts-node --transpile-only --ignore=false test/test.ts"
Expand Down
2 changes: 2 additions & 0 deletions src/lib/stable/execute_with_candid_serde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function executeWithCandidSerde(
new Uint8Array(IDL.encode([returnIdl], [result]))
);
} else {
// TODO is void best represented as IDL.encode([], [])?
ic.replyRaw(new Uint8Array(IDL.encode([], [])));
}
}
Expand All @@ -53,6 +54,7 @@ export function executeWithCandidSerde(
if (returnIdl !== undefined) {
ic.replyRaw(new Uint8Array(IDL.encode([returnIdl], [result])));
} else {
// TODO is void best represented as IDL.encode([], [])?
ic.replyRaw(new Uint8Array(IDL.encode([], [])));
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/lib/stable/ic_apis/candid_encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Converts a Candid string into bytes
* @param candidString a valid Candid string
* @returns the candid value as bytes
*/
export function candidEncode(candidString: string): Uint8Array {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

return new Uint8Array(globalThis._azleIc.candidEncode(candidString));
}
3 changes: 3 additions & 0 deletions src/lib/stable/ic_apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export { call } from './call';
export { candidEncode } from './candid_encode';
export { id } from './id';
export { notify } from './notify';
export { reject } from './reject';
export { reply } from './reply';
export { replyRaw } from './reply_raw';
export { trap } from './trap';
7 changes: 7 additions & 0 deletions src/lib/stable/ic_apis/reject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Rejects the current call with the provided message
* @param message the rejection message
*/
export function reject(message: string): void {
return globalThis._azleIc ? globalThis._azleIc.reject(message) : undefined;
}
12 changes: 8 additions & 4 deletions src/lib/stable/ic_apis/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import { IDL } from '..';
* is the generic type supplied to `Manual<T>`. Otherwise will result in an
* uncaught `TypeError`.
*/
export function reply<T>(data: T, type: IDL.Type<T>): void {
export function reply<T>(data: T, type?: IDL.Type): void {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

return globalThis._azleIc.replyRaw(
new Uint8Array(IDL.encode([type], [data])).buffer
);
// TODO is void best represented as IDL.encode([], [])?
const encoded =
type === undefined
? new Uint8Array(IDL.encode([], [])).buffer
: new Uint8Array(IDL.encode([type], [data])).buffer;

return globalThis._azleIc.replyRaw(encoded);
}
22 changes: 22 additions & 0 deletions src/lib/stable/ic_apis/reply_raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Used to manually reply to an ingress message. Intended to be used in
* canister methods with a {@link Manual} return type.
* @param buf the value with which to reply. Intended to be used in conjunction with
* {@link ic.candidEncode}.
* @example
* ```ts
* $update;
* export function replyRaw(): Manual<RawReply> {
* ic.replyRaw(
* ic.candidEncode(
* '(record { "int" = 42; "text" = "text"; "bool" = true; "blob" = blob "raw bytes"; "variant" = variant { Medium } })'
* )
* );
* }
* ```
*/
export function replyRaw(replyBuffer: Uint8Array): void {
return globalThis._azleIc
? globalThis._azleIc.replyRaw(replyBuffer.buffer)
: undefined;
}
7 changes: 5 additions & 2 deletions src/lib/stable/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { executeWithCandidSerde } from './execute_with_candid_serde';

export function update(
paramIdls: IDL.Type[],
returnIdl?: IDL.Type
returnIdl?: IDL.Type,
options?: {
manual?: boolean;
}
): MethodDecorator {
return <T>(
target: object,
Expand All @@ -20,7 +23,7 @@ export function update(
originalMethod,
paramIdls,
returnIdl,
false // TODO implement manual check
options?.manual ?? false
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
14 changes: 14 additions & 0 deletions tests/end_to_end/candid_rpc/class_syntax/manual_reply/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"canisters": {
"manual_reply": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"declarations": {
"output": "test/dfx_generated/manual_reply",
"node_compatibility": true
}
}
}
}
Loading

0 comments on commit a8e81fb

Please sign in to comment.