Skip to content

Commit

Permalink
Revert "Merge pull request #103 from seasonedcc/parser-agnostic"
Browse files Browse the repository at this point in the history
This reverts commit 5546cd3, reversing
changes made to 2cd2832.
  • Loading branch information
diogob committed Sep 12, 2023
1 parent e898a31 commit 9e690f3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 1,181 deletions.
46 changes: 3 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Keep your business logic clean with Domain Functions

Domain Functions helps you decouple your business logic from your controllers, with first-class type inference from end to end.
It does this by enforcing the parameters' types at runtime and always wrapping results (even exceptions) into a `Promise<Result<Output>>` type.
It does this by enforcing the parameters' types at runtime (through [zod](https://github.com/colinhacks/zod#what-is-zod) schemas) and always wrapping results (even exceptions) into a `Promise<Result<Output>>` type.

![](example.gif)

Expand Down Expand Up @@ -42,7 +42,6 @@ It does this by enforcing the parameters' types at runtime and always wrapping r
- [inputFromFormData](#inputfromformdata)
- [inputFromUrl](#inputfromurl)
- [inputFromSearch](#inputfromsearch)
- [Using other schema validators](#using-other-schema-validators)
- [Resources](#resources)
- [FAQ](#faq)
- [Acknowlegements](#acknowlegements)
Expand All @@ -61,9 +60,6 @@ It does this by enforcing the parameters' types at runtime and always wrapping r
npm i domain-functions zod
```

**NOTE :** All our examples will use [zod](https://zod.dev/) which we recommend if you have no existing schema validation library in your project.
You can, however, [use other schema validators](#using-other-schema-validators).

```tsx
import { makeDomainFunction, inputFromForm } from 'domain-functions'
import * as z from 'zod'
Expand Down Expand Up @@ -92,8 +88,7 @@ failedResult = {
*/
```

To understand how to build the schemas, refer to your schema validation library documentation.
See the [Zod documentation](https://zod.dev) to better understand how our examples are built.
To understand how to build the schemas, refer to [Zod documentation](https://github.com/colinhacks/zod#defining-schemas).

## Using Deno

Expand Down Expand Up @@ -523,7 +518,7 @@ const result = await first(a, b)({ id: 1 })
It will pass the same environment to all given functions, and it will pass the output of a function as the next function's input in left-to-right order.
The resulting data will be the output of the rightmost function.

**NOTE :** that there is no type-level assurance that a function's output will align with and be succesfully parsed by the next function in the pipeline.
Note that there is no type-level assurance that a function's output will align with and be succesfully parsed by the next function in the pipeline.

```ts
const a = makeDomainFunction(z.object({ aNumber: z.number() }))(
Expand Down Expand Up @@ -978,40 +973,6 @@ async (request: Request) => {

To better understand how to structure your data, refer to [this test file](./src/input-resolvers.test.ts)

## Using other schema validators

Although our examples use `zod`, you are able to chose any one of the supported libraries:

* [Valibot](https://valibot.dev/)
* [yup](https://github.com/jquense/yup)
* [Superstruct](https://docs.superstructjs.org/)
* [TypeBox](https://github.com/sinclairzx81/typebox)
* [runtypes](https://github.com/pelotom/runtypes)

See below another version of our initial example using [Valibot](https://valibot.dev/).

```typescript
import { makeDomainFunction, inputFromForm } from 'domain-functions'
import { object, number } from 'valibot';

const schema = object({ number: number() })
const increment = makeDomainFunction(schema)(({ number }) => number + 1)

const result = await increment({ number: 1 })

/*
result = {
success: true,
data: 2,
errors: []
inputErrors: []
environmentErrors: []
}
*/
```

For more examples of how to use other libraries check our [test file](./src/parsers.test.ts).

## Resources

- [The case for domain-functions](https://dev.to/diogob/the-case-for-domain-functions-f4e)
Expand All @@ -1027,7 +988,6 @@ For more examples of how to use other libraries check our [test file](./src/pars
## Acknowlegements

We are grateful for [Zod](https://github.com/colinhacks/zod), as it is a great library and it informed our design.
[TypeSchema](https://typeschema.com/) is another great project that allowed us to support multiple validators.
It's worth mentioning two other projects that inspired domain-functions:

- [Servant](https://github.com/haskell-servant/servant/)
Expand Down
Loading

0 comments on commit 9e690f3

Please sign in to comment.