-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from castore-dev/create-zod-command
feature: Create zod command
- Loading branch information
Showing
24 changed files
with
811 additions
and
13 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
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
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
export { JSONSchemaCommand } from './command'; | ||
export type { OnEventAlreadyExistsCallback } from './command'; |
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 @@ | ||
dist |
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,2 @@ | ||
const baseConfig = require('../../.lintstagedrc'); | ||
module.exports = baseConfig; |
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,108 @@ | ||
# Zod Command | ||
|
||
DRY Castore [`Command`](https://github.com/castore-dev/castore/#--command) definition using [`zod`](https://github.com/colinhacks/zod). | ||
|
||
## 📥 Installation | ||
|
||
```bash | ||
# npm | ||
npm install @castore/command-zod | ||
|
||
# yarn | ||
yarn add @castore/command-zod | ||
``` | ||
|
||
This package has `@castore/core` and `zod` (above v3) as peer dependencies, so you will have to install them as well: | ||
|
||
```bash | ||
# npm | ||
npm install @castore/core zod | ||
|
||
# yarn | ||
yarn add @castore/core zod | ||
``` | ||
|
||
## 👩💻 Usage | ||
|
||
```ts | ||
import z from 'zod'; | ||
|
||
import { ZodCommand } from '@castore/command-zod'; | ||
import { tuple } from '@castore/core'; | ||
|
||
const pokemonAppearedInputSchema = z.object({ | ||
name: z.string(), | ||
level: z.number(), | ||
}); | ||
|
||
const pokemonAppearedOutputSchema = z.object({ | ||
pokemonId: z.string().uuid(), | ||
}); | ||
|
||
// 👇 generics are correctly inferred | ||
const pokemonAppearCommand = new ZodCommand({ | ||
commandId: 'POKEMON_APPEAR', | ||
requiredEventStores: tuple(pokemonsEventStore), | ||
inputSchema: pokemonAppearedInputSchema, | ||
outputSchema: pokemonAppearedOutputSchema, | ||
// 👇 handler input/output types are correctly inferred | ||
handler: async ( | ||
commandInput, | ||
[pokemonsEventStore], | ||
{ generateUuid }: { generateUuid: () => string }, | ||
) => { | ||
const { name, level } = commandInput; | ||
const pokemonId = generateUuid(); | ||
|
||
await pokemonsEventStore.pushEvent({ | ||
aggregateId: pokemonId, | ||
version: 1, | ||
type: 'POKEMON_APPEARED', | ||
payload: { name, level }, | ||
}); | ||
|
||
return { pokemonId }; | ||
}, | ||
}); | ||
``` | ||
|
||
👇 Equivalent to: | ||
|
||
```ts | ||
import { Command } from '@castore/core'; | ||
|
||
type RequiredEventStores = [typeof pokemonsEventStore]; | ||
type CommandInput = { name: string; level: number }; | ||
type CommandOutput = { pokemonId: string }; | ||
|
||
const pokemonAppearCommand = new Command< | ||
RequiredEventStores, | ||
RequiredEventStores, | ||
CommandInput, | ||
CommandOutput | ||
>({ | ||
commandId: 'POKEMON_APPEAR', | ||
requiredEventStores: [pokemonsEventStore], | ||
handler: async (commandInput, [pokemonsEventStore]) => { | ||
// ...same code | ||
}, | ||
}); | ||
``` | ||
|
||
## ⚙️ Properties & Methods | ||
|
||
`ZodCommand` implements the [`Command`](https://github.com/castore-dev/castore/#--command) class and adds the following properties to it: | ||
|
||
- <code>inputSchema <i>(?object)</i></code>: The command input zod schema | ||
|
||
```ts | ||
const inputSchema = pokemonAppearCommand.inputSchema; | ||
// => pokemonAppearedInputSchema | ||
``` | ||
|
||
- <code>outputSchema <i>(?object)</i></code>: The command output zod schema | ||
|
||
```ts | ||
const outputSchema = pokemonAppearCommand.outputSchema; | ||
// => pokemonAppearedOutputSchema | ||
``` |
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,3 @@ | ||
const commonBabelConfig = require('../../commonConfiguration/babel.config'); | ||
|
||
module.exports = commonBabelConfig(); |
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,3 @@ | ||
/** @type {import('dependency-cruiser').IConfiguration} */ | ||
const baseConfig = require('../../dependency-cruiser'); | ||
module.exports = baseConfig; |
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,80 @@ | ||
{ | ||
"name": "@castore/command-zod", | ||
"description": "DRY Castore Command definition using Zod", | ||
"license": "MIT", | ||
"homepage": "https://github.com/theodo/castore#readme", | ||
"bugs": "https://github.com/theodo/castore/issues", | ||
"repository": "theodo/castore.git", | ||
"keywords": [ | ||
"event", | ||
"source", | ||
"store", | ||
"typescript" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/types/index.d.ts", | ||
"scripts": { | ||
"lint-fix": "yarn linter-base-config --fix", | ||
"lint-fix-all": "yarn lint-fix .", | ||
"linter-base-config": "eslint --ext=js,ts", | ||
"package": "rm -rf dist && yarn package-cjs && yarn package-esm && yarn package-types", | ||
"package-cjs": "NODE_ENV=cjs yarn transpile --out-dir dist/cjs --source-maps", | ||
"package-esm": "NODE_ENV=esm yarn transpile --out-dir dist/esm --source-maps", | ||
"package-types": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json", | ||
"test": "yarn test-type && yarn test-unit && yarn test-circular && yarn test-linter", | ||
"test-circular": "yarn depcruise --validate dependency-cruiser.js .", | ||
"test-linter": "yarn linter-base-config .", | ||
"test-type": "tsc --noEmit --emitDeclarationOnly false", | ||
"test-unit": "yarn vitest run --passWithNoTests", | ||
"transpile": "babel src --extensions .ts --quiet", | ||
"watch": "rm -rf dist && concurrently 'yarn:package-* --watch'" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.17.6", | ||
"@babel/core": "^7.17.9", | ||
"@babel/plugin-transform-runtime": "^7.17.0", | ||
"@babel/preset-env": "^7.16.11", | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@castore/core": "workspace:", | ||
"@types/node": "^17.0.29", | ||
"babel-plugin-module-resolver": "^4.1.0", | ||
"concurrently": "^7.1.0", | ||
"dependency-cruiser": "^11.7.0", | ||
"eslint": "^8.14.0", | ||
"prettier": "^2.6.2", | ||
"ts-node": "^10.7.0", | ||
"ts-toolbelt": "^9.6.0", | ||
"tsc-alias": "^1.8.7", | ||
"typescript": "^4.6.3", | ||
"vitest": "^0.26.2", | ||
"zod": "^3.15.1" | ||
}, | ||
"maintainers": [ | ||
"Thomas Aribart", | ||
"Charles Géry", | ||
"Juliette Fournier", | ||
"Valentin Beggi", | ||
"Stanislas Hannebelle" | ||
], | ||
"nx": { | ||
"targets": { | ||
"package": { | ||
"outputs": [ | ||
"packages/command-zod/dist" | ||
] | ||
} | ||
} | ||
}, | ||
"peerDependencies": { | ||
"@castore/core": "*", | ||
"zod": "^3.0.0" | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"root": "packages/command-zod", | ||
"projectType": "library", | ||
"tags": [], | ||
"implicitDependencies": ["core"] | ||
} |
Oops, something went wrong.