Skip to content

Commit

Permalink
Update ESLint rules and fix type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Feb 17, 2024
1 parent 42290fa commit 1753a78
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 117 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"prettier"
],
"rules": {
"prettier/prettier": "warn",
"@typescript-eslint/no-unused-vars": [
"error",
{ "argsIgnorePattern": "^_" }
],
"no-console": 1,
"prettier/prettier": 2
"no-console": 1
},
"env": {
"browser": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/store/src/actor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class ActorManager<A = BaseActor> {

public extractService(): ExtractedService<A> {
return this.idlFactory({ IDL })._fields.reduce((acc, service) => {
const functionName = service[0] as keyof A
const functionName = service[0] as FunctionName<A>
const type = service[1]

const visit = ((extractorClass, data) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/store/src/actor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export type ExtractVisitorType<V> = V extends IDL.Visitor<infer D, infer R>
? { data: D; returnType: R }
: never

export type ExtractedService<A = BaseActor, M extends keyof A = keyof A> = {
export type ExtractedService<
A = BaseActor,
M extends FunctionName<A> = FunctionName<A>
> = {
[K in M]: <V extends IDL.Visitor<unknown, unknown>>(
extractorClass: V,
data?: ExtractVisitorType<V>["data"]
Expand Down
4 changes: 3 additions & 1 deletion packages/store/test/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ describe("createReActorStore", () => {
})

test("Uninitialized", () => {
const value = visitFunction.get_app(new VisitRandomResponse<Example>())
const value = visitFunction.get_app(
new VisitRandomResponse<Example, "get_app">()
)
const data = visitFunction.get_app(new VisitTransform<Example>(), {
value,
label: "app",
Expand Down
16 changes: 8 additions & 8 deletions packages/visitor/src/details/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type {
} from "./types"
import { IDL } from "@dfinity/candid"
import { isQuery } from "../helper"
import { ActorSubclass } from "@dfinity/agent"
import { DefaultActorType, FunctionName } from "@ic-reactor/store"
import { BaseActor, FunctionName } from "@ic-reactor/store"
import { FieldType } from "../types"

export * from "./types"
Expand All @@ -21,7 +20,8 @@ export * from "./types"
* @category Main
*/
export class VisitDetails<
A extends ActorSubclass<any> = DefaultActorType
A = BaseActor,
M extends FunctionName<A> = FunctionName<A>
> extends IDL.Visitor<
string,
| ExtractedServiceDetails<A>
Expand All @@ -31,9 +31,9 @@ export class VisitDetails<
> {
public counter = 0

public visitFunc(
public visitFunc<Method extends M>(
t: IDL.FuncClass,
functionName: FunctionName<A>
functionName: Method
): MethodDetails<A> {
const functionType = isQuery(t) ? "query" : "update"

Expand Down Expand Up @@ -100,7 +100,7 @@ export class VisitDetails<
}
}

public visitTuple<T extends any[]>(
public visitTuple<T extends IDL.Type[]>(
t: IDL.TupleClass<T>,
components: IDL.Type[],
__label: string
Expand Down Expand Up @@ -225,9 +225,9 @@ export class VisitDetails<
const functionName = services[0] as FunctionName<A>
const func = services[1]

acc[functionName] = (extractorClass) => {
acc[functionName] = ((extractorClass) => {
return func.accept(extractorClass || this, functionName)
}
}) as ServiceDetails<A>[FunctionName<A>]

return acc
}, {} as ServiceDetails<A>)
Expand Down
4 changes: 3 additions & 1 deletion packages/visitor/src/details/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export interface ExtractedServiceDetails<A = DefaultActorType> {
}

export type ServiceDetails<A = DefaultActorType> = {
[K in FunctionName<A>]: <ExtractorClass extends IDL.Visitor<any, any>>(
[K in FunctionName<A>]: <
ExtractorClass extends IDL.Visitor<unknown, unknown>
>(
extractorClass?: ExtractorClass
) => MethodDetails<A>
}
Expand Down
30 changes: 13 additions & 17 deletions packages/visitor/src/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import type {
} from "./types"
import { isQuery, validateError, validateNumberError } from "../helper"
import { IDL } from "@dfinity/candid"
import type {
ActorSubclass,
DefaultActorType,
FunctionName,
} from "@ic-reactor/store"
import type { BaseActor, FunctionName } from "@ic-reactor/store"

export * from "./types"
export * from "../helper"
Expand All @@ -36,7 +32,7 @@ export * from "../helper"
* @category Main
*/
export class VisitFields<
A extends ActorSubclass<any> = DefaultActorType,
A = BaseActor,
M extends FunctionName<A> = FunctionName<A>
> extends IDL.Visitor<
string,
Expand All @@ -62,7 +58,7 @@ export class VisitFields<
return acc
},
{
fields: [] as DynamicFieldTypeByClass<IDL.Type<any>>[],
fields: [] as DynamicFieldTypeByClass<IDL.Type>[],
defaultValue: {} as MethodDefaultValues<Method>,
}
)
Expand All @@ -84,7 +80,7 @@ export class VisitFields<
t: IDL.RecordClass,
_fields: Array<[string, IDL.Type]>,
label: string
): RecordFields<IDL.Type<any>> {
): RecordFields<IDL.Type> {
const { fields, defaultValues } = _fields.reduce(
(acc, [key, type]) => {
const field = type.accept(this, key) as AllFieldTypes<typeof type>
Expand All @@ -96,7 +92,7 @@ export class VisitFields<
},
{
fields: [] as AllFieldTypes<IDL.Type>[],
defaultValues: {} as Record<string, FieldTypeFromIDLType>,
defaultValues: {} as Record<string, FieldTypeFromIDLType<IDL.Type>>,
}
)

Expand All @@ -113,7 +109,7 @@ export class VisitFields<
t: IDL.VariantClass,
fields_: Array<[string, IDL.Type]>,
label: string
): VariantFields<IDL.Type<any>> {
): VariantFields<IDL.Type> {
const { fields, options } = fields_.reduce(
(acc, [key, type]) => {
const field = type.accept(this, key) as AllFieldTypes<typeof type>
Expand Down Expand Up @@ -146,11 +142,11 @@ export class VisitFields<
}
}

public visitTuple<T extends any[]>(
public visitTuple<T extends IDL.Type[]>(
t: IDL.TupleClass<T>,
components: IDL.Type[],
label: string
): TupleFields<IDL.Type<any>> {
): TupleFields<IDL.Type> {
const { fields, defaultValues } = components.reduce(
(acc, type, index) => {
const field = type.accept(this, `_${index}_`) as AllFieldTypes<
Expand All @@ -163,7 +159,7 @@ export class VisitFields<
},
{
fields: [] as AllFieldTypes<IDL.Type>[],
defaultValues: [] as any[],
defaultValues: [] as FieldTypeFromIDLType<IDL.Type>[],
}
)

Expand All @@ -186,7 +182,7 @@ export class VisitFields<
label,
validate: validateError(t),
name: ty.name,
extract: () => ty.accept(this, label) as VariantFields<IDL.Type<any>>,
extract: () => ty.accept(this, label) as VariantFields<IDL.Type>,
}
}

Expand Down Expand Up @@ -228,7 +224,7 @@ export class VisitFields<
validate: validateError(t),
label,
required: true,
defaultValue: undefined as any,
defaultValue: undefined as InputField<IDL.Type<T>>["defaultValue"],
}
}

Expand Down Expand Up @@ -312,9 +308,9 @@ export class VisitFields<
const functionName = services[0] as FunctionName<A>
const func = services[1]

acc[functionName] = (extractorClass) => {
acc[functionName] = ((extractorClass) => {
return func.accept(extractorClass || this, functionName)
}
}) as ServiceFields<A>[FunctionName<A>]

return acc
}, {} as ServiceFields<A>)
Expand Down
28 changes: 15 additions & 13 deletions packages/visitor/src/fields/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type ServiceFields<
A = DefaultActorType,
M extends FunctionName<A> = FunctionName<A>
> = {
[key in M]: <ExtractorClass extends IDL.Visitor<any, any>>(
[key in M]: <ExtractorClass extends IDL.Visitor<unknown, unknown>>(
extractorClass?: ExtractorClass
) => MethodFields<A, M>
}
Expand All @@ -26,8 +26,8 @@ export interface MethodFields<
> {
functionName: M
functionType: FunctionType
fields: AllFieldTypes<IDL.Type<any>>[] | []
validate: (value: any) => boolean | string
fields: AllFieldTypes<IDL.Type>[] | []
validate: (value: FieldTypeFromIDLType<IDL.Type>) => boolean | string
defaultValues: ServiceDefaultValues<A, M>
}

Expand Down Expand Up @@ -101,7 +101,7 @@ export interface InputField<T extends IDL.Type> extends DefaultField {
defaultValue: FieldTypeFromIDLType<T>
}

export type DynamicFieldType<T extends FieldType = any> = T extends "record"
export type DynamicFieldType<T extends FieldType> = T extends "record"
? RecordFields<IDL.Type>
: T extends "variant"
? VariantFields<IDL.Type>
Expand Down Expand Up @@ -130,15 +130,15 @@ export type DynamicFieldType<T extends FieldType = any> = T extends "record"
export type DynamicFieldTypeByClass<T extends IDL.Type> =
T extends IDL.RecordClass
? RecordFields<T>
: T extends IDL.TupleClass<any>
: T extends IDL.TupleClass<IDL.Type[]>
? TupleFields<T>
: T extends IDL.VariantClass
? VariantFields<T>
: T extends IDL.VecClass<any>
: T extends IDL.VecClass<IDL.Type>
? VectorFields
: T extends IDL.OptClass<any>
: T extends IDL.OptClass<IDL.Type>
? OptionalFields
: T extends IDL.RecClass<any>
: T extends IDL.RecClass<IDL.Type>
? RecursiveFields
: T extends IDL.PrincipalClass
? PrincipalField
Expand All @@ -165,9 +165,9 @@ export type AllFieldTypes<T extends IDL.Type> =
| NumberField
| InputField<T>

export type FieldTypeFromIDLType<T = any> = T extends IDL.Type
export type FieldTypeFromIDLType<T> = T extends IDL.Type
? ReturnType<T["decodeValue"]>
: any
: IDL.Type

export type ExtraInputFormFields = Partial<{
maxLength: number
Expand All @@ -177,7 +177,9 @@ export type ExtraInputFormFields = Partial<{
export interface DefaultField extends ExtraInputFormFields {
type: FieldType
label: string
validate: (value: any) => boolean | string
defaultValue?: any
defaultValues?: any
validate: (value: FieldTypeFromIDLType<IDL.Type>) => boolean | string
defaultValue?: FieldTypeFromIDLType<IDL.Type>
defaultValues?:
| FieldTypeFromIDLType<IDL.Type>[]
| Record<string, FieldTypeFromIDLType<IDL.Type>>
}
6 changes: 3 additions & 3 deletions packages/visitor/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const extractAndSortArgs = <T extends Record<string, unknown>>(
const args: Array<T[keyof T]> = []
let index = 0

while (argsObject.hasOwnProperty(`arg${index}`)) {
while (Object.prototype.hasOwnProperty.call(argsObject, `arg${index}`)) {
args.push(argsObject[`arg${index}`] as T[keyof T])
index++
}
Expand Down Expand Up @@ -52,8 +52,8 @@ export const validateNumberError = (t: IDL.Type) => {
}
}

export const validateError = (t: IDL.Type<any>) => {
return function validate(value: any) {
export const validateError = (t: IDL.Type) => {
return function validate(value: unknown) {
try {
t.covariant(value)
return true
Expand Down
Loading

0 comments on commit 1753a78

Please sign in to comment.