-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(store, world): separate shorthand config from full config re…
…solvers and cleanup (#2464)
- Loading branch information
Showing
40 changed files
with
1,994 additions
and
1,940 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Config conventions | ||
|
||
These are the types and functions that should be placed in a file called `x.ts` | ||
|
||
```ts | ||
/** | ||
* validateX returns input if the input is valid and expected type otherwise. | ||
* This makes it such that there are fine grained (and custom) type errors on the input of defineX. | ||
*/ | ||
type validateX<x> = { [key in keyof x]: x[key] extends Expected ? x[key] : Expected }; | ||
|
||
/** | ||
* validateX function throws a runtime error if x is not X and has a type assertion | ||
*/ | ||
function validateX(x: unknonw): asserts x is X { | ||
// | ||
} | ||
|
||
/** | ||
* resolveX expects a valid input type and maps it to the resolved output type | ||
*/ | ||
type resolveX<x> = x extends X ? { [key in keyof x]: Resolved } : never; | ||
|
||
/** | ||
* defineX function validates the input types and calls resolveX to resolve it. | ||
* Note: the runtime validation happens in `resolveX`. | ||
* This is to take advantage of the type assertion in the function body. | ||
*/ | ||
function defineX<const x>(x: validateX<x>): resolveX<x> { | ||
return resolveX(x); | ||
} | ||
|
||
/** | ||
* resolveX function does not validate the input type, but validates the runtime types. | ||
* (This is to take advantage of the type assertion in the function body). | ||
* This function is used by defineX and other higher level resolution functions. | ||
*/ | ||
function resolveX<const x extends X>(x: x): resolveX<x> { | ||
validateX(x); | ||
// | ||
} | ||
``` | ||
|
||
There are two files that fall out of this patten: `input.ts` and `output.ts`: | ||
|
||
- `input.ts` includes the flattened input types. They are supposed to be broad and not include all constraints. The stronger constraints are implemented in the `validateX` helpers. | ||
- `output.ts` includes the flattened output types. They are supposed to be broad so downstream consumers can use them as input types for working with the config, and strongly typed config outputs will be assignable to them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { CODEGEN_DEFAULTS } from "./defaults"; | ||
import { isObject, mergeIfUndefined } from "./generics"; | ||
|
||
export type resolveCodegen<codegen> = codegen extends {} | ||
? mergeIfUndefined<codegen, typeof CODEGEN_DEFAULTS> | ||
: typeof CODEGEN_DEFAULTS; | ||
|
||
export function resolveCodegen<codegen>(codegen: codegen): resolveCodegen<codegen> { | ||
return ( | ||
isObject(codegen) ? mergeIfUndefined(codegen, CODEGEN_DEFAULTS) : CODEGEN_DEFAULTS | ||
) as resolveCodegen<codegen>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Enums } from "./output"; | ||
import { AbiTypeScope, extendScope } from "./scope"; | ||
|
||
function isEnums(enums: unknown): enums is Enums { | ||
return ( | ||
typeof enums === "object" && | ||
enums != null && | ||
Object.values(enums).every((item) => Array.isArray(item) && item.every((element) => typeof element === "string")) | ||
); | ||
} | ||
|
||
export type scopeWithEnums<enums, scope extends AbiTypeScope = AbiTypeScope> = Enums extends enums | ||
? scope | ||
: enums extends Enums | ||
? extendScope<scope, { [key in keyof enums]: "uint8" }> | ||
: scope; | ||
|
||
export function scopeWithEnums<enums, scope extends AbiTypeScope = AbiTypeScope>( | ||
enums: enums, | ||
scope: scope = AbiTypeScope as scope, | ||
): scopeWithEnums<enums, scope> { | ||
if (isEnums(enums)) { | ||
const enumScope = Object.fromEntries(Object.keys(enums).map((key) => [key, "uint8" as const])); | ||
return extendScope(scope, enumScope) as scopeWithEnums<enums, scope>; | ||
} | ||
return scope as scopeWithEnums<enums, scope>; | ||
} | ||
|
||
export type resolveEnums<enums> = { readonly [key in keyof enums]: Readonly<enums[key]> }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Hex } from "viem"; | ||
import { Codegen, Enums, TableCodegen, UserTypes } from "./output"; | ||
import { Scope } from "./scope"; | ||
|
||
export type SchemaInput = { | ||
readonly [key: string]: string; | ||
}; | ||
|
||
export type ScopedSchemaInput<scope extends Scope> = { | ||
readonly [key: string]: keyof scope["types"]; | ||
}; | ||
|
||
export type TableInput = { | ||
readonly schema: SchemaInput; | ||
readonly key: readonly string[]; | ||
readonly tableId?: Hex; | ||
readonly name: string; | ||
readonly namespace?: string; | ||
readonly type?: "table" | "offchainTable"; | ||
readonly codegen?: Partial<TableCodegen>; | ||
}; | ||
|
||
export type TablesInput = { | ||
readonly [key: string]: TableInput; | ||
}; | ||
|
||
export type StoreInput = { | ||
readonly namespace?: string; | ||
readonly tables: TablesInput; | ||
readonly userTypes?: UserTypes; | ||
readonly enums?: Enums; | ||
readonly codegen?: Partial<Codegen>; | ||
}; | ||
|
||
/******** Variations with shorthands ********/ | ||
|
||
export type TableShorthandInput = SchemaInput | string; | ||
|
||
export type TablesWithShorthandsInput = { | ||
[key: string]: TableInput | TableShorthandInput; | ||
}; | ||
|
||
export type StoreWithShorthandsInput = Omit<StoreInput, "tables"> & { tables: TablesWithShorthandsInput }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.