Skip to content

Commit

Permalink
Update syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Sep 11, 2023
1 parent 2ed552f commit dbea089
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
80 changes: 47 additions & 33 deletions examples/rejections/canisters/rejections/index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
import {
CallResult,
ic,
Principal,
RejectionCode,
Service,
serviceUpdate,
$update
text,
update,
Void
} from 'azle';
import { someService } from '../some_service';
import { default as SomeService } from '../some_service';

class Nonexistent extends Service {
@serviceUpdate
method: () => CallResult<void>;
@update([], Void)
method: () => Void;
}

export const nonexistentCanister = new Nonexistent(
Principal.fromText('rkp4c-7iaaa-aaaaa-aaaca-cai')
);
export default class extends Service {
nonexistentCanister = new Nonexistent(
Principal.fromText('rkp4c-7iaaa-aaaaa-aaaca-cai')
);

$update;
export async function getRejectionCodeNoError(): Promise<RejectionCode> {
await someService.accept().call();
return ic.rejectCode();
}
someService = new SomeService(
Principal.fromText(
process.env.SOME_SERVICE_PRINCIPAL ??
ic.trap('process.env.SOME_SERVICE_PRINCIPAL is undefined')
)
);

$update;
export async function getRejectionCodeDestinationInvalid(): Promise<RejectionCode> {
await nonexistentCanister.method().call();
return ic.rejectCode();
}
@update([], RejectionCode)
async getRejectionCodeNoError(): Promise<RejectionCode> {
await ic.call(this.someService.accept);

$update;
export async function getRejectionCodeCanisterReject(): Promise<RejectionCode> {
await someService.reject('reject').call();
return ic.rejectCode();
}
return ic.rejectCode();
}

$update;
export async function getRejectionCodeCanisterError(): Promise<RejectionCode> {
await someService.error().call();
return ic.rejectCode();
}
@update([], RejectionCode)
async getRejectionCodeDestinationInvalid(): Promise<RejectionCode> {
await ic.call(this.nonexistentCanister.method);

return ic.rejectCode();
}

@update([], RejectionCode)
async getRejectionCodeCanisterReject(): Promise<RejectionCode> {
await ic.call(this.someService.reject, { args: ['reject'] });

return ic.rejectCode();
}

@update([], RejectionCode)
async getRejectionCodeCanisterError(): Promise<RejectionCode> {
await ic.call(this.someService.error);

return ic.rejectCode();
}

@update([text], text)
async getRejectionMessage(message: text): Promise<text> {
await ic.call(this.someService.reject, { args: [message] });

$update;
export async function getRejectionMessage(message: string): Promise<string> {
await someService.reject(message).call();
return ic.rejectMessage();
return ic.rejectMessage();
}
}
31 changes: 16 additions & 15 deletions examples/rejections/canisters/some_service/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { CallResult, empty, ic, Principal, Service, serviceQuery } from 'azle';
import { bool, empty, ic, query, Service, text } from 'azle';

export class SomeService extends Service {
@serviceQuery
reject: (message: string) => CallResult<empty>;
type Manual<T> = void;

@serviceQuery
accept: () => CallResult<boolean>;
export default class extends Service {
@query([text], empty, { manual: true })
reject(message: text): Manual<empty> {
ic.reject(message);
}

@serviceQuery
error: () => CallResult<empty>;
}
@query([], bool)
accept(): bool {
return true;
}

export const someService = new SomeService(
Principal.fromText(
process.env.SOME_SERVICE_PRINCIPAL ??
ic.trap('process.env.SOME_SERVICE_PRINCIPAL is undefined')
)
);
@query([], empty, { manual: true })
error(): Manual<empty> {
// This errors because neither ic.reject nor ic.reply were called
}
}

0 comments on commit dbea089

Please sign in to comment.