diff --git a/examples/audio_recorder/src/index.ts b/examples/audio_recorder/src/index.ts index f92ff358ee..0bf1aea4c3 100644 --- a/examples/audio_recorder/src/index.ts +++ b/examples/audio_recorder/src/index.ts @@ -85,7 +85,7 @@ export default Canister({ createRecording: update( [blob, text, principal], Result(Recording, AudioRecorderError), - (audio: blob, name: text, userId: Principal) => { + (audio, name, userId) => { const userOpt = users.get(userId); if (userOpt.length === 0) { diff --git a/src/lib_functional/candid/index.ts b/src/lib_functional/candid/index.ts index 868d6e968e..f5595f5723 100644 --- a/src/lib_functional/candid/index.ts +++ b/src/lib_functional/candid/index.ts @@ -42,12 +42,12 @@ import { AzleResult, Result, AzleTuple, - Tuple + AzleText } from '../../lib_new'; export type TypeMapping = T extends () => any ? ReturnType - : T extends IDL.TextClass + : T extends AzleText ? string : T extends AzleBool ? bool @@ -96,3 +96,7 @@ export type TypeMapping = T extends () => any : T extends AzleEmpty ? empty : T; + +export type CandidType = { + _azleCandidType?: '_azleCandidType'; +}; diff --git a/src/lib_functional/candid/reference/record.ts b/src/lib_functional/candid/reference/record.ts index d7e2b360cc..b6414687c3 100644 --- a/src/lib_functional/candid/reference/record.ts +++ b/src/lib_functional/candid/reference/record.ts @@ -1,11 +1,17 @@ -import { TypeMapping } from '..'; +import { CandidType, TypeMapping } from '..'; import { IDL } from '@dfinity/candid'; import { Parent, processMap } from '../../../lib_new/utils'; import { v4 } from 'uuid'; -export function Record(obj: T): { +export function Record< + T extends { + [K in keyof T]: CandidType; + } +>( + obj: T +): { [K in keyof T]: TypeMapping; -} { +} & { _azleCandidType?: '_azleCandidType' } { const name = v4(); return { diff --git a/src/lib_functional/candid/reference/variant.ts b/src/lib_functional/candid/reference/variant.ts index 2e99a9b700..bde604c04f 100644 --- a/src/lib_functional/candid/reference/variant.ts +++ b/src/lib_functional/candid/reference/variant.ts @@ -6,7 +6,7 @@ import { Null } from '../../../lib_new/primitives'; export function Variant(obj: T): RequireExactlyOne<{ [K in keyof T]: TypeMapping; -}> { +}> & { _azleCandidType?: '_azleCandidType' } { const name = v4(); return { diff --git a/src/lib_functional/canister_methods/init.ts b/src/lib_functional/canister_methods/init.ts index ee40c279bf..767caaa314 100644 --- a/src/lib_functional/canister_methods/init.ts +++ b/src/lib_functional/canister_methods/init.ts @@ -4,11 +4,11 @@ import { newTypesToStingArr } from '../../lib_new/method_decorators'; import { Callback, CanisterMethodInfo, executeMethod } from '.'; -import { TypeMapping } from '../candid'; +import { CandidType, TypeMapping } from '../candid'; import { Void } from '../../lib_new'; export function init< - const Params extends ReadonlyArray, + const Params extends ReadonlyArray, GenericCallback extends Callback >( paramsIdls: Params, diff --git a/src/lib_functional/canister_methods/post_upgrade.ts b/src/lib_functional/canister_methods/post_upgrade.ts index 34a3ebe2c0..dd54c7a2a6 100644 --- a/src/lib_functional/canister_methods/post_upgrade.ts +++ b/src/lib_functional/canister_methods/post_upgrade.ts @@ -4,11 +4,11 @@ import { newTypesToStingArr } from '../../lib_new/method_decorators'; import { Callback, CanisterMethodInfo, executeMethod } from '.'; -import { TypeMapping } from '../candid'; +import { CandidType, TypeMapping } from '../candid'; import { Void } from '../../lib_new'; export function postUpgrade< - const Params extends ReadonlyArray, + const Params extends ReadonlyArray, GenericCallback extends Callback >( paramsIdls: Params, diff --git a/src/lib_functional/canister_methods/query.ts b/src/lib_functional/canister_methods/query.ts index 99ca0338d5..b0802bbb9d 100644 --- a/src/lib_functional/canister_methods/query.ts +++ b/src/lib_functional/canister_methods/query.ts @@ -6,11 +6,11 @@ import { newTypesToStingArr } from '../../lib_new/method_decorators'; import { Callback, CanisterMethodInfo, executeMethod } from '.'; -import { TypeMapping } from '../candid'; +import { CandidType, TypeMapping } from '../candid'; export function query< - const Params extends ReadonlyArray, - Return, + const Params extends ReadonlyArray, + Return extends CandidType, GenericCallback extends Callback >( paramsIdls: Params, diff --git a/src/lib_functional/canister_methods/update.ts b/src/lib_functional/canister_methods/update.ts index 3f6f9e3c90..9e2046ec8c 100644 --- a/src/lib_functional/canister_methods/update.ts +++ b/src/lib_functional/canister_methods/update.ts @@ -6,11 +6,11 @@ import { newTypesToStingArr } from '../../lib_new/method_decorators'; import { Callback, CanisterMethodInfo, executeMethod } from '.'; -import { TypeMapping } from '../candid'; +import { CandidType, TypeMapping } from '../candid'; export function update< - const Params extends ReadonlyArray, - Return, + const Params extends ReadonlyArray, + Return extends CandidType, GenericCallback extends Callback >( paramsIdls: Params, diff --git a/src/lib_new/primitives.ts b/src/lib_new/primitives.ts index f9dcf8bcb0..ef93275ca7 100644 --- a/src/lib_new/primitives.ts +++ b/src/lib_new/primitives.ts @@ -3,6 +3,8 @@ import { CandidClass, Parent, toIDLType } from './utils'; export class AzleNat { _kind: 'AzleNat' = 'AzleNat'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Nat; } @@ -10,6 +12,8 @@ export class AzleNat { export class AzleNat64 { _kind: 'AzleNat64' = 'AzleNat64'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Nat64; } @@ -17,6 +21,8 @@ export class AzleNat64 { export class AzleNat32 { _kind: 'AzleNat32' = 'AzleNat32'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Nat32; } @@ -24,6 +30,8 @@ export class AzleNat32 { export class AzleNat16 { _kind: 'AzleNat16' = 'AzleNat16'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Nat16; } @@ -31,6 +39,8 @@ export class AzleNat16 { export class AzleNat8 { _kind: 'AzleNat8' = 'AzleNat8'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Nat8; } @@ -38,6 +48,8 @@ export class AzleNat8 { export class AzleInt { _kind: 'AzleInt' = 'AzleInt'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Int; } @@ -45,6 +57,8 @@ export class AzleInt { export class AzleInt64 { _kind: 'AzleInt64' = 'AzleInt64'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Int64; } @@ -52,6 +66,8 @@ export class AzleInt64 { export class AzleInt32 { _kind: 'AzleInt32' = 'AzleInt32'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Int32; } @@ -59,6 +75,8 @@ export class AzleInt32 { export class AzleInt16 { _kind: 'AzleInt16' = 'AzleInt16'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Int16; } @@ -66,6 +84,8 @@ export class AzleInt16 { export class AzleInt8 { _kind: 'AzleInt8' = 'AzleInt8'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Int8; } @@ -73,6 +93,8 @@ export class AzleInt8 { export class AzleFloat64 { _kind: 'AzleFloat64' = 'AzleFloat64'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Float64; } @@ -80,6 +102,8 @@ export class AzleFloat64 { export class AzleFloat32 { _kind: 'AzleFloat32' = 'AzleFloat32'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Float32; } @@ -87,6 +111,8 @@ export class AzleFloat32 { export class AzleBlob { _kind: 'AzleBlob' = 'AzleBlob'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Vec(IDL.Nat8); } @@ -94,6 +120,8 @@ export class AzleBlob { export class AzleNull { _kind: 'AzleNull' = 'AzleNull'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Null; } @@ -101,6 +129,8 @@ export class AzleNull { export class AzleReserved { _kind: 'AzleReserved' = 'AzleReserved'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Reserved; } @@ -108,6 +138,8 @@ export class AzleReserved { export class AzleEmpty { _kind: 'AzleEmpty' = 'AzleEmpty'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Empty; } @@ -120,8 +152,19 @@ export class AzleBool { } } +export class AzleText { + _kind: 'AzleText' = 'AzleText'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Text; + } +} + export class AzlePrincipal { _kind: 'AzlePrincipal' = 'AzlePrincipal'; + _azleCandidType?: '_azleCandidType'; + static getIDL() { return IDL.Principal; } @@ -157,7 +200,7 @@ export const Null: AzleNull = AzleNull as any; export type Null = null; export const reserved: AzleReserved = AzleReserved as any; export type reserved = any; -export const text = IDL.Text; +export const text: AzleText = AzleText as any; export type text = string; export const float32: AzleFloat32 = AzleFloat32 as any; export type float32 = number; @@ -198,7 +241,10 @@ export class AzleOpt { constructor(t: any) { this._azleType = t; } + _azleType: CandidClass; + _azleCandidType?: '_azleCandidType'; + getIDL(parents: Parent[]) { return IDL.Opt(toIDLType(this._azleType, [])); } @@ -208,7 +254,10 @@ export class AzleVec { constructor(t: any) { this._azleType = t; } + _azleType: CandidClass; + _azleCandidType?: '_azleCandidType'; + getIDL(parents: Parent[]) { return IDL.Vec(toIDLType(this._azleType, [])); } @@ -218,7 +267,10 @@ export class AzleTuple { constructor(t: CandidClass[]) { this._azleTypes = t; } + _azleTypes: CandidClass[]; + _azleCandidType?: '_azleCandidType'; + getIDL(parents: Parent[]) { const candidTypes = this._azleTypes.map((value) => { return toIDLType(value, parents); diff --git a/src/lib_new/result.ts b/src/lib_new/result.ts index ecefbd1846..ac43d23bc4 100644 --- a/src/lib_new/result.ts +++ b/src/lib_new/result.ts @@ -1,6 +1,7 @@ import { RequireExactlyOne } from '../lib/candid_types/variant'; +import { CandidType } from '../lib_functional'; import { IDL } from './index'; -import { CandidClass, Parent, toIDLType } from './utils'; +import { Parent, toIDLType } from './utils'; export class AzleResult { constructor(ok: any, err: any) { @@ -11,6 +12,8 @@ export class AzleResult { _azleOk: any; _azleErr: any; + _azleCandidType?: '_azleCandidType'; + getIDL(parents: Parent[]) { return IDL.Variant({ Ok: toIDLType(this._azleOk, parents), @@ -19,7 +22,7 @@ export class AzleResult { } } -export function Result( +export function Result( ok: Ok, err: Err ) { diff --git a/src/lib_new/stable_b_tree_map.ts b/src/lib_new/stable_b_tree_map.ts index eab2ef0b62..63489f2674 100644 --- a/src/lib_new/stable_b_tree_map.ts +++ b/src/lib_new/stable_b_tree_map.ts @@ -1,12 +1,11 @@ -import { TypeMapping } from '../lib_functional'; +import { CandidType, TypeMapping } from '../lib_functional'; import { IDL, nat8, nat64, Opt } from './index'; import { toIDLType } from './utils'; -export function StableBTreeMap( - key: Key, - value: Value, - memoryId: nat8 -) { +export function StableBTreeMap< + Key extends CandidType, + Value extends CandidType +>(key: Key, value: Value, memoryId: nat8) { const keyIdl = toIDLType(key, []); const valueIdl = toIDLType(value, []);