Skip to content

Commit

Permalink
add return types
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jul 15, 2024
1 parent 6ce5cda commit cb091cf
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/lib/candid/types/reference/service/canister_function/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO make this function's return type explicit https://github.com/demergent-labs/azle/issues/1860
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { IDL } from '@dfinity/candid';

import { CanisterMethodInfo } from '../../../../../canister_methods/types/canister_method_info';
Expand Down Expand Up @@ -45,7 +43,9 @@ export interface ServiceFunctionInfo {
[key: string]: FunctionInfo;
}

export function createCanisterFunction(canisterOptions: CanisterOptions) {
export function createCanisterFunction(
canisterOptions: CanisterOptions
): _AzleFunctionReturnType {
let canister = createCanisterFunctionBase(canisterOptions);
canister.init = createSystemMethod('init', canisterOptions);
canister.heartbeat = createSystemMethod('heartbeat', canisterOptions);
Expand All @@ -65,7 +65,9 @@ export function createCanisterFunction(canisterOptions: CanisterOptions) {
return canister;
}

function createGetIdlTypeFunction(canisterOptions: CanisterOptions) {
function createGetIdlTypeFunction(
canisterOptions: CanisterOptions
): (parents: Parent[]) => IDL.ServiceClass {
return (parents: Parent[]): IDL.ServiceClass => {
const serviceFunctionInfo = canisterOptions as ServiceFunctionInfo;

Expand Down Expand Up @@ -119,7 +121,9 @@ function createUpdateOrQueryFunctionIdlType(
return IDL.Func(paramIdlTypes, returnIdlTypes, annotations);
}

function createCallbacks(canisterOptions: CanisterOptions) {
function createCallbacks(
canisterOptions: CanisterOptions
): Record<string, ((...args: any) => any) | undefined> {
return Object.entries(canisterOptions).reduce((acc, entry) => {
const canisterMethod = entry[1];

Expand All @@ -146,7 +150,7 @@ function createCanisterFunctionBase(
callFunction: CallRawFunction | NotifyRawFunction,
cycles: bigint,
args: any[]
) => {
): void | Promise<any> => {
return serviceCall(
principal as any,
key,
Expand All @@ -166,12 +170,19 @@ function createCanisterFunctionBase(
};
}

type ServiceCall = (
notify: boolean,
callFunction: CallRawFunction | NotifyRawFunction,
cycles: bigint,
args: any[]
) => void | Promise<any>;

function serviceCall(
canisterId: Principal,
methodName: string,
paramCandidTypes: CandidType[],
returnCandidType: CandidType
) {
): ServiceCall {
return (
notify: boolean,
callFunction: CallRawFunction | NotifyRawFunction,
Expand All @@ -188,7 +199,7 @@ function serviceCall(
cycles
);
} else {
return (async () => {
return (async (): Promise<any> => {
const encodedResult = await (callFunction as CallRawFunction)(
canisterId,
methodName,
Expand Down

0 comments on commit cb091cf

Please sign in to comment.