feat(candid): add Zod-inspired Candid codec API (Slice 1)#184
Merged
Conversation
Add the foundational codec layer to @ic-reactor/candid under src/codec/: - Base CandidCodec<T> class with immutable metadata chaining - 19 primitive codec factories (text, bool, nat, int, principal, etc.) - 5 composite codecs (record, variant, opt, vec, tuple) - Service/method codecs (query, update, oneway, service) - idlFactory getter compatible with @icp-sdk/core actor creation - manifest() for structured method/argument metadata - c namespace with c.infer<T> and c.ServiceOf<T> type utilities - CandidMetadata model for docs, labels, examples, form hints 63 new tests covering IDL equivalence, metadata immutability, encode/decode roundtrips, type inference, and edge cases.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add the foundational Candid codec layer to
@ic-reactor/candid— Slice 1 of the Candid Codec plan.This introduces a Zod-inspired
cnamespace for building typed, metadata-rich Candid contracts in TypeScript:What's Included
New:
packages/candid/src/codec/types.tsCandidMetadata, manifest typescodec.tsCandidCodec<T>base with immutable metadata chainingprimitives.tstext,bool,nat,principal, etc.)composites.tsopt,vec,record,variant,tuplewith full type inferenceservice.tsquery,update,oneway,servicecodecs withidlFactory&manifest()index.tscnamespace (const + namespace merge forc.infer<T>)Modified
src/index.ts— addedexport * from "./codec"tests/unit/index.test.ts— added new exports to the allowlistKey Design Decisions
.describe(),.label(),.example(),.meta()return new instancestoIDL()called at consumption time (enables futurec.recursive())cis both a const object and a TS namespace forc.infer<T>/c.ServiceOf<T>Tests
63 new tests covering:
idlFactoryequivalence with hand-written factoriesmanifest()structured outputVerification
vitest run tests/unit/codec.test.ts— 63/63 passvitest run tests/unit/index.test.ts— 11/11 passtsc --noEmit— clean type checkNext Steps
This is Slice 1 of 5 per the Candid Codec plan:
Codec primitives & service layer✅ (this PR)parseDid())