From e14722032c79baefb9729fac2f5a328168797005 Mon Sep 17 00:00:00 2001 From: atticusofsparta Date: Wed, 16 Oct 2024 14:24:17 -0600 Subject: [PATCH] fix(strict): allow for passing in strict mode on apis --- src/common/ant.ts | 63 ++++++++++++++++++++++++++++++++--------------- src/types/ant.ts | 26 ++++++++++++------- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/common/ant.ts b/src/common/ant.ts index 60212971..ad865272 100644 --- a/src/common/ant.ts +++ b/src/common/ant.ts @@ -19,6 +19,7 @@ import { AntBalancesSchema, AntControllersSchema, AntInfoSchema, + AntReadOptions, AntRecordSchema, AntRecordsSchema, AntStateSchema, @@ -92,12 +93,14 @@ export class AoANTReadable implements AoANTRead { } } - async getState(): Promise { + async getState( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { const tags = [{ name: 'Action', value: 'State' }]; const res = await this.process.read({ tags, }); - if (this.strict) { + if (strict) { parseSchemaResult( AntStateSchema.passthrough().and( z.object({ @@ -111,12 +114,14 @@ export class AoANTReadable implements AoANTRead { return res; } - async getInfo(): Promise { + async getInfo( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { const tags = [{ name: 'Action', value: 'Info' }]; const info = await this.process.read({ tags, }); - if (this.strict) { + if (strict) { parseSchemaResult(AntInfoSchema.passthrough(), info); } return info; @@ -131,7 +136,10 @@ export class AoANTReadable implements AoANTRead { * ant.getRecord({ undername: "john" }); * ``` */ - async getRecord({ undername }: { undername: string }): Promise { + async getRecord( + { undername }: { undername: string }, + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { const tags = [ { name: 'Sub-Domain', value: undername }, { name: 'Action', value: 'Record' }, @@ -140,7 +148,7 @@ export class AoANTReadable implements AoANTRead { const record = await this.process.read({ tags, }); - if (this.strict) parseSchemaResult(AntRecordSchema.passthrough(), record); + if (strict) parseSchemaResult(AntRecordSchema.passthrough(), record); return record; } @@ -153,12 +161,14 @@ export class AoANTReadable implements AoANTRead { * ant.getRecords(); * ```` */ - async getRecords(): Promise> { + async getRecords( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise> { const tags = [{ name: 'Action', value: 'Records' }]; const records = await this.process.read>({ tags, }); - if (this.strict) parseSchemaResult(AntRecordsSchema, records); + if (strict) parseSchemaResult(AntRecordsSchema, records); return records; } @@ -170,8 +180,10 @@ export class AoANTReadable implements AoANTRead { * ant.getOwner(); * ``` */ - async getOwner(): Promise { - const info = await this.getInfo(); + async getOwner( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { + const info = await this.getInfo({ strict }); return info.Owner; } @@ -183,12 +195,14 @@ export class AoANTReadable implements AoANTRead { * ant.getControllers(); * ``` */ - async getControllers(): Promise { + async getControllers( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { const tags = [{ name: 'Action', value: 'Controllers' }]; const controllers = await this.process.read({ tags, }); - if (this.strict) parseSchemaResult(AntControllersSchema, controllers); + if (strict) parseSchemaResult(AntControllersSchema, controllers); return controllers; } @@ -200,8 +214,10 @@ export class AoANTReadable implements AoANTRead { * ant.getName(); * ``` */ - async getName(): Promise { - const info = await this.getInfo(); + async getName( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { + const info = await this.getInfo({ strict }); return info.Name; } @@ -213,8 +229,10 @@ export class AoANTReadable implements AoANTRead { * ant.getTicker(); * ``` */ - async getTicker(): Promise { - const info = await this.getInfo(); + async getTicker( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { + const info = await this.getInfo({ strict }); return info.Ticker; } @@ -226,12 +244,14 @@ export class AoANTReadable implements AoANTRead { * ant.getBalances(); * ``` */ - async getBalances(): Promise> { + async getBalances( + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise> { const tags = [{ name: 'Action', value: 'Balances' }]; const balances = await this.process.read>({ tags, }); - if (this.strict) parseSchemaResult(AntBalancesSchema, balances); + if (strict) parseSchemaResult(AntBalancesSchema, balances); return balances; } @@ -244,7 +264,10 @@ export class AoANTReadable implements AoANTRead { * ant.getBalance({ address }); * ``` */ - async getBalance({ address }: { address: string }): Promise { + async getBalance( + { address }: { address: string }, + { strict }: AntReadOptions = { strict: this.strict }, + ): Promise { const tags = [ { name: 'Action', value: 'Balance' }, { name: 'Recipient', value: address }, @@ -252,7 +275,7 @@ export class AoANTReadable implements AoANTRead { const balance = await this.process.read({ tags, }); - if (this.strict) parseSchemaResult(z.number(), balance); + if (strict) parseSchemaResult(z.number(), balance); return balance; } } diff --git a/src/types/ant.ts b/src/types/ant.ts index 79b9c390..27f82534 100644 --- a/src/types/ant.ts +++ b/src/types/ant.ts @@ -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; - getInfo(): Promise; - getRecord({ undername }): Promise; - getRecords(): Promise>; - getOwner(): Promise; + getState(opts?: AntReadOptions): Promise; + getInfo(opts?: AntReadOptions): Promise; + getRecord( + { undername }: { undername: string }, + opts?: AntReadOptions, + ): Promise; + getRecords(opts?: AntReadOptions): Promise>; + getOwner(opts?: AntReadOptions): Promise; getControllers(): Promise; - getTicker(): Promise; - getName(): Promise; - getBalance({ address }: { address: WalletAddress }): Promise; - getBalances(): Promise>; + getTicker(opts?: AntReadOptions): Promise; + getName(opts?: AntReadOptions): Promise; + getBalance( + { address }: { address: WalletAddress }, + opts?: AntReadOptions, + ): Promise; + getBalances(opts?: AntReadOptions): Promise>; } export interface AoANTWrite extends AoANTRead {