Skip to content

Commit

Permalink
Merge pull request #1851 from demergent-labs/composite_queries
Browse files Browse the repository at this point in the history
Composite queries
  • Loading branch information
lastmjs authored Jul 8, 2024
2 parents 1f479e7 + c9b83a9 commit 9905546
Show file tree
Hide file tree
Showing 20 changed files with 10,363 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/composite_queries/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/composite_queries/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "composite_queries_end_to_end_test_functional_syntax",
"scripts": {
"pre_tests": "ts-node --transpile-only --ignore=false test/pretest.ts",
"tests": "npm run pre_tests && jest",
Expand Down
15 changes: 15 additions & 0 deletions src/lib/stable/ic_apis/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Principal } from '..';

/**
* Gets the id of this canister
* @returns the canister id
*/
export function id(): Principal {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

// TODO consider bytes instead of string, just like with caller
const idString = globalThis._azleIc.id();
return Principal.fromText(idString);
}
2 changes: 2 additions & 0 deletions src/lib/stable/ic_apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { call } from './call';
export { id } from './id';
export { notify } from './notify';
export { reply } from './reply';
export { trap } from './trap';
18 changes: 18 additions & 0 deletions src/lib/stable/ic_apis/reply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IDL } from '..';

/**
* Used to manually reply to an ingress message. Intended to be used in
* canister methods with a {@link Manual} return type.
* @param reply the value with which to reply. Must be of type `T` where `T`
* 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 {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

return globalThis._azleIc.replyRaw(
new Uint8Array(IDL.encode([type], [data])).buffer
);
}
11 changes: 7 additions & 4 deletions src/lib/stable/query.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IDL } from '@dfinity/candid';

import { isAsync } from '../canister_methods/is_async';
import { executeWithCandidSerde } from './execute_with_candid_serde';

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

descriptor.value = methodCallback as any;

globalThis._azleCanisterMethods.queries.push({
name: propertyKey as string,
composite: isAsync(originalMethod)
composite: options?.composite ?? false
// TODO implement guard
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"canisters": {
"canister1": {
"type": "azle",
"main": "src/canister1/index.ts",
"candid": "src/canister1/index.did",
"candid_gen": "custom",
"env": [
"CANISTER1_PRINCIPAL",
"CANISTER2_PRINCIPAL",
"CANISTER3_PRINCIPAL",
"AZLE_TEST_FETCH"
],
"assets": [
["src/canister2/index.did", "candid/canister2.did"],
["src/canister3/index.did", "candid/canister3.did"]
],
"declarations": {
"output": "test/dfx_generated/canister1",
"node_compatibility": true
}
},
"canister2": {
"type": "azle",
"main": "src/canister2/index.ts",
"candid": "src/canister2/index.did",
"candid_gen": "custom",
"env": ["CANISTER3_PRINCIPAL", "AZLE_TEST_FETCH"],
"assets": [["src/canister3/index.did", "candid/canister3.did"]],
"declarations": {
"output": "test/dfx_generated/canister2",
"node_compatibility": true
}
},
"canister3": {
"type": "azle",
"main": "src/canister3/index.ts",
"candid": "src/canister3/index.did",
"candid_gen": "custom",
"declarations": {
"output": "test/dfx_generated/canister3",
"node_compatibility": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
Loading

0 comments on commit 9905546

Please sign in to comment.