Skip to content

Commit

Permalink
fix(strict): allow for passing in strict mode on apis
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Oct 16, 2024
1 parent 2a95bf7 commit e147220
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
63 changes: 43 additions & 20 deletions src/common/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AntBalancesSchema,
AntControllersSchema,
AntInfoSchema,
AntReadOptions,
AntRecordSchema,
AntRecordsSchema,
AntStateSchema,
Expand Down Expand Up @@ -92,12 +93,14 @@ export class AoANTReadable implements AoANTRead {
}
}

async getState(): Promise<AoANTState> {
async getState(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<AoANTState> {
const tags = [{ name: 'Action', value: 'State' }];
const res = await this.process.read<AoANTState>({
tags,
});
if (this.strict) {
if (strict) {
parseSchemaResult(
AntStateSchema.passthrough().and(
z.object({
Expand All @@ -111,12 +114,14 @@ export class AoANTReadable implements AoANTRead {
return res;
}

async getInfo(): Promise<AoANTInfo> {
async getInfo(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<AoANTInfo> {
const tags = [{ name: 'Action', value: 'Info' }];
const info = await this.process.read<AoANTInfo>({
tags,
});
if (this.strict) {
if (strict) {
parseSchemaResult(AntInfoSchema.passthrough(), info);
}
return info;
Expand All @@ -131,7 +136,10 @@ export class AoANTReadable implements AoANTRead {
* ant.getRecord({ undername: "john" });
* ```
*/
async getRecord({ undername }: { undername: string }): Promise<AoANTRecord> {
async getRecord(
{ undername }: { undername: string },
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<AoANTRecord> {
const tags = [
{ name: 'Sub-Domain', value: undername },
{ name: 'Action', value: 'Record' },
Expand All @@ -140,7 +148,7 @@ export class AoANTReadable implements AoANTRead {
const record = await this.process.read<AoANTRecord>({
tags,
});
if (this.strict) parseSchemaResult(AntRecordSchema.passthrough(), record);
if (strict) parseSchemaResult(AntRecordSchema.passthrough(), record);

return record;
}
Expand All @@ -153,12 +161,14 @@ export class AoANTReadable implements AoANTRead {
* ant.getRecords();
* ````
*/
async getRecords(): Promise<Record<string, AoANTRecord>> {
async getRecords(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<Record<string, AoANTRecord>> {
const tags = [{ name: 'Action', value: 'Records' }];
const records = await this.process.read<Record<string, AoANTRecord>>({
tags,
});
if (this.strict) parseSchemaResult(AntRecordsSchema, records);
if (strict) parseSchemaResult(AntRecordsSchema, records);
return records;
}

Expand All @@ -170,8 +180,10 @@ export class AoANTReadable implements AoANTRead {
* ant.getOwner();
* ```
*/
async getOwner(): Promise<string> {
const info = await this.getInfo();
async getOwner(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<string> {
const info = await this.getInfo({ strict });
return info.Owner;
}

Expand All @@ -183,12 +195,14 @@ export class AoANTReadable implements AoANTRead {
* ant.getControllers();
* ```
*/
async getControllers(): Promise<WalletAddress[]> {
async getControllers(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<WalletAddress[]> {
const tags = [{ name: 'Action', value: 'Controllers' }];
const controllers = await this.process.read<WalletAddress[]>({
tags,
});
if (this.strict) parseSchemaResult(AntControllersSchema, controllers);
if (strict) parseSchemaResult(AntControllersSchema, controllers);
return controllers;
}

Expand All @@ -200,8 +214,10 @@ export class AoANTReadable implements AoANTRead {
* ant.getName();
* ```
*/
async getName(): Promise<string> {
const info = await this.getInfo();
async getName(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<string> {
const info = await this.getInfo({ strict });
return info.Name;
}

Expand All @@ -213,8 +229,10 @@ export class AoANTReadable implements AoANTRead {
* ant.getTicker();
* ```
*/
async getTicker(): Promise<string> {
const info = await this.getInfo();
async getTicker(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<string> {
const info = await this.getInfo({ strict });
return info.Ticker;
}

Expand All @@ -226,12 +244,14 @@ export class AoANTReadable implements AoANTRead {
* ant.getBalances();
* ```
*/
async getBalances(): Promise<Record<string, number>> {
async getBalances(
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<Record<string, number>> {
const tags = [{ name: 'Action', value: 'Balances' }];
const balances = await this.process.read<Record<string, number>>({
tags,
});
if (this.strict) parseSchemaResult(AntBalancesSchema, balances);
if (strict) parseSchemaResult(AntBalancesSchema, balances);
return balances;
}

Expand All @@ -244,15 +264,18 @@ export class AoANTReadable implements AoANTRead {
* ant.getBalance({ address });
* ```
*/
async getBalance({ address }: { address: string }): Promise<number> {
async getBalance(
{ address }: { address: string },
{ strict }: AntReadOptions = { strict: this.strict },
): Promise<number> {
const tags = [
{ name: 'Action', value: 'Balance' },
{ name: 'Recipient', value: address },
];
const balance = await this.process.read<number>({
tags,
});
if (this.strict) parseSchemaResult(z.number(), balance);
if (strict) parseSchemaResult(z.number(), balance);
return balance;
}
}
Expand Down
26 changes: 17 additions & 9 deletions src/types/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,25 @@ export function isAoANTState(state: object): state is AoANTState {
return AntStateSchema.safeParse(state).success;
}

export type AntReadOptions = { strict?: boolean };

export interface AoANTRead {
getState(): Promise<AoANTState>;
getInfo(): Promise<AoANTInfo>;
getRecord({ undername }): Promise<AoANTRecord | undefined>;
getRecords(): Promise<Record<string, AoANTRecord>>;
getOwner(): Promise<WalletAddress>;
getState(opts?: AntReadOptions): Promise<AoANTState>;
getInfo(opts?: AntReadOptions): Promise<AoANTInfo>;
getRecord(
{ undername }: { undername: string },
opts?: AntReadOptions,
): Promise<AoANTRecord | undefined>;
getRecords(opts?: AntReadOptions): Promise<Record<string, AoANTRecord>>;
getOwner(opts?: AntReadOptions): Promise<WalletAddress>;
getControllers(): Promise<WalletAddress[]>;
getTicker(): Promise<string>;
getName(): Promise<string>;
getBalance({ address }: { address: WalletAddress }): Promise<number>;
getBalances(): Promise<Record<WalletAddress, number>>;
getTicker(opts?: AntReadOptions): Promise<string>;
getName(opts?: AntReadOptions): Promise<string>;
getBalance(
{ address }: { address: WalletAddress },
opts?: AntReadOptions,
): Promise<number>;
getBalances(opts?: AntReadOptions): Promise<Record<WalletAddress, number>>;
}

export interface AoANTWrite extends AoANTRead {
Expand Down

0 comments on commit e147220

Please sign in to comment.