Skip to content

Commit

Permalink
fix(generics): use named generic
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Feb 15, 2024
1 parent 37f27d3 commit 4b647f0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ export class ArIO implements ContractCache {
* Fetches the state of a contract.
* @param {string} contractTxId - The Arweave transaction id of the contract.
*/
async getContractState<T>({
async getContractState<ContractState>({
contractTxId,
}: {
contractTxId: string;
}): Promise<T> {
return this.contractStateProvider.getContractState({ contractTxId });
}): Promise<ContractState> {
return this.contractStateProvider.getContractState<ContractState>({
contractTxId,
});
}
}
8 changes: 5 additions & 3 deletions src/common/caches/arns-remote-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ export class ArNSRemoteCache implements ContractCache {
});
}

async getContractState<T>({
async getContractState<ContractState>({
contractTxId,
}: {
contractTxId: string;
}): Promise<T> {
}): Promise<ContractState> {
this.logger.debug(`Fetching contract state`);

const { state } = await this.http.get<EvaluatedContractState<T>>({
const { state } = await this.http.get<
EvaluatedContractState<ContractState>
>({
endpoint: `/contract/${contractTxId.toString()}`,
});

Expand Down
16 changes: 16 additions & 0 deletions src/common/error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BadRequest, NotFound } from './error.js';

describe('Error', () => {
it.each([
['BadRequest', BadRequest],
['NotFound', NotFound],
])(
'Errors should inherit Base error and names should be applied appropriately',
(name, errorClass) => {
const message = 'This is a test error';
const error = new errorClass(message);
expect(error.name).toEqual(name);
expect(error.message).toEqual(message);
},
);
});
17 changes: 11 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@
*/
import { Readable } from 'stream';
import { ReadableStream } from 'stream/web';
import { EvalStateResult, EvaluationOptions } from 'warp-contracts/web';
import { EvalStateResult, EvaluationOptions } from 'warp-contracts';

export interface ContractCache {
/**
* The ContractStateProvider interface is used to define a contract state provider.
*/
getContractState<T>({ contractTxId }: { contractTxId: string }): Promise<T>;
getContractState<ContractState>({
contractTxId,
}: {
contractTxId: string;
}): Promise<ContractState>;
}

export type EvaluatedContractState<T> = EvalStateResult<T> & {
sortKey: string;
evaluationOptions: EvaluationOptions;
};
export type EvaluatedContractState<ContractState> =
EvalStateResult<ContractState> & {
sortKey: string;
evaluationOptions: EvaluationOptions;
};

/* eslint-disable @typescript-eslint/no-explicit-any */
export interface Logger {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/arweave.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ARNS_REGISTRY_TX } from '../constants.js';
import { validateArweaveId } from './arweave.js';

describe('Arweave ID Validation', () => {
it('should validate a valid Arweave ID', () => {
const validId = ARNS_REGISTRY_TX;
expect(validateArweaveId(validId)).toBe(true);
});

it('should not validate an invalid Arweave ID', () => {
const invalidId = 'invalid-id';
expect(validateArweaveId(invalidId)).toBe(false);
});
});

0 comments on commit 4b647f0

Please sign in to comment.