···11-# typed-lexicon
11+# prototypey
2233-> [!WARNING]
44-> this project is in the middle of active initial development and not ready for
55-> use. there will be updates posted [here](https://bsky.app/profile/tylur.dev)
66-> if you'd like to follow along!
33+A (soon-to-be) fully-featured sdk for developing lexicons with typescript.
7488-
55+## Installation
961010-this will be a toolkit for writing lexicon json schema's in typescript and
1111-providing types for lexicon data shape. it will:
77+```bash
88+npm install prototypey
99+```
1010+1111+## Usage
12121313-- remove boilerplate and improve ergonomics
1414-- type hint for
1515- [atproto type parameters](https://atproto.com/specs/lexicon#overview-of-types)
1616-- infer the typescript type definitions for the data shape to avoid duplication
1717- and skew
1818-- methods and a cli for generating json
1313+Prototypey provides both a TypeScript library for authoring lexicons and a CLI for code generation.
19142020-With each of the above finished, i'll plan to write a `validate` method that
2121-will be published alongside this that takes any lexicon json definition and
2222-validates payloads off that.
1515+### Authoring Lexicons
23162424-My working hypothesis: it will be easier to write lexicons in typescript with a
2525-single api, then validate based off the json definition, than it would be to
2626-start with validation library types (standard-schema style) and attempt to use
2727-those as the authoring and validation tools.
1717+Use the library to author type-safe lexicon schemas in TypeScript:
28182929-**what you'd write:**
1919+**what you'll write:**
30203131-```typescript
3232-const profileNamespace = lx.lexicon("app.bsky.actor.profile", {
2121+```ts
2222+const lex = lx.lexicon("app.bsky.actor.profile", {
3323 main: lx.record({
3424 key: "self",
3525 record: lx.object({
···7060}
7161```
72627373----
6363+### CLI Commands
74647575-<p align="center">
7676- <a href="https://github.com/tylersayshi/prototypey/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"><img alt="๐ค Code of Conduct: Kept" src="https://img.shields.io/badge/%F0%9F%A4%9D_code_of_conduct-kept-21bb42" /></a>
7777- <a href="https://github.com/tylersayshi/prototypey/blob/main/LICENSE.md" target="_blank"><img alt="๐ License: MIT" src="https://img.shields.io/badge/%F0%9F%93%9D_license-MIT-21bb42.svg" /></a>
7878- <img alt="๐ช TypeScript: Strict" src="https://img.shields.io/badge/%F0%9F%92%AA_typescript-strict-21bb42.svg" />
7979-</p>
6565+The `prototypey` package includes a CLI with two main commands:
80668181-## Usage
6767+#### `gen-inferred` - Generate TypeScript from JSON schemas
82688383-tbd
6969+```bash
7070+prototypey gen-inferred <outdir> <schemas...>
7171+```
84728585-## Development
7373+Reads ATProto lexicon JSON schemas and generates TypeScript types.
86748787-See [`.github/CONTRIBUTING.md`](./.github/CONTRIBUTING.md), then
8888-[`.github/DEVELOPMENT.md`](./.github/DEVELOPMENT.md). Thanks! ๐
7575+**Example:**
89769090-<!-- You can remove this notice if you don't want it ๐ no worries! -->
7777+```bash
7878+prototypey gen-inferred ./generated/inferred ./lexicons/**/*.json
7979+```
91808181+#### `gen-emit` - Emit JSON schemas from TypeScript
8282+8383+```bash
8484+prototypey gen-emit <outdir> <sources...>
8585+```
8686+8787+Extracts JSON schemas from TypeScript lexicon definitions.
8888+8989+**Example:**
9090+9191+```bash
9292+prototypey gen-emit ./lexicons ./src/lexicons/**/*.ts
9393+```
9494+9595+### Typical Workflow
9696+9797+1. Author lexicons in TypeScript using the library
9898+2. Emit JSON schemas with `gen-emit` for runtime validation
9999+100100+**Recommended:** Add as a script to your `package.json`:
101101+102102+```json
103103+{
104104+ "scripts": {
105105+ "lexicon:emit": "prototypey gen-emit ./schemas ./src/lexicons/**/*.ts"
106106+ }
107107+}
108108+```
109109+110110+Then run:
111111+112112+```bash
113113+npm run lexicon:emit
114114+```
115115+116116+## State of the Project
117117+118118+**Done:**
119119+120120+- Full atproto spec lexicon authoring with in IDE docs & hints for each attribute (ts => json)
121121+- CLI generates json from ts definitions
122122+- CLI generates ts from json definitions
123123+- Inferrance of valid type from full lexicon definition
124124+ - the really cool part of this is that it fills in the refs from the defs all at the type level
125125+126126+**TODO/In Progress:**
127127+128128+- Library art! Please reach out if you'd be willing to contribute some drawings or anything!
129129+- Runtime validation using [@atproto/lexicon](https://www.npmjs.com/package/@atproto/lexicon)
130130+ - this will be hard to get correct, I'm weary of loading all of the json in a project's lexicons into js memory and would like to run benchmarks and find the best way to get this right.
131131+- The CLI needs more real world use and mileage. I expect bugs and weird behavior in this initial release (sorry).
132132+133133+## Disclaimer:
134134+135135+I'm considering how to use the json for validation (there will likely be some lazy-loading). For the cli,
136136+files may need to adopt a convention so it's easy to determine what is an `lx.lexicon` and then generate out it's json and export it as a validator that lazy loads json to validate. (these are just ideas right now, but I want to share where we are now :)
137137+138138+Please give any and all feedback. I've not really written many lexicons much myself yet, so this project is at a point of "well I think this makes sense" ๐. Both the [issues page](https://github.com/tylersayshi/prototypey/issues) and [discussions](https://github.com/tylersayshi/prototypey/discussions) are open and ready for y'all ๐.
139139+140140+<small>
92141> ๐ This package was templated with
93142> [`create-typescript-app`](https://github.com/JoshuaKGoldberg/create-typescript-app)
94143> using the [Bingo framework](https://create.bingo).
144144+</small>