Skip to content

Commit

Permalink
Merge pull request #14 from mahabubx7/13-documentation
Browse files Browse the repository at this point in the history
[Patch & Docs] Added a validator fix + documentations for guides
  • Loading branch information
mahabubx7 authored Oct 16, 2024
2 parents 55e2628 + 797926f commit d9f44d8
Show file tree
Hide file tree
Showing 20 changed files with 556 additions and 141 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "always",
"source.organizeImports": "always"
}
},
"deno.enable": true
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ Made with 💚 by @mahabubx7
Thanks,
~/[Mahabub](https://github.com/mahabubx7)
~/[Mahabub](https://mahabubx7.netlify.app)
4 changes: 2 additions & 2 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export default defineConfig({
base: "/guide",
items: [
{ text: "Get Started", link: "/get-started" },
{ text: "Define Schema", link: "/schema" },
{ text: "Data Validation", link: "/validation" }
{ text: "Schema & Validation", link: "/schema-validation" },
{ text: "Infer Types", link: "/infer-types" }
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/api/array.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Array

::: info
We are wotking on it.
Work in progress ⏳
:::
2 changes: 1 addition & 1 deletion docs/api/boolean.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Boolean

::: info
We are wotking on it.
Work in progress ⏳
:::
2 changes: 1 addition & 1 deletion docs/api/enum.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Enum

::: info
We are wotking on it.
Work in progress ⏳
:::
4 changes: 2 additions & 2 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

Welcome to the `Akar.js`. This page will walk you through the available API references of this library.

:::info
We are wotking on it.
::: info
Work in progress ⏳
:::
2 changes: 1 addition & 1 deletion docs/api/number.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Number

::: info
We are wotking on it.
Work in progress ⏳
:::
2 changes: 1 addition & 1 deletion docs/api/object.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Object

::: info
We are wotking on it.
Work in progress ⏳
:::
2 changes: 1 addition & 1 deletion docs/api/string.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# String

::: info
We are wotking on it.
Work in progress ⏳
:::
4 changes: 2 additions & 2 deletions docs/greetings.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Welcome!

::: info
This package is almost ready for its first release `v1.0.0-beta`.
It is still in `development` and very soon will be available in npm.
This package is available with its latest release `v1.0.1` or later.
It is still in `development` and very soon will have its first stable/lts release.
:::

If you consider this project useful or worth the attentions of developers, then please give its GitHub repository a star. Also, you can contribute to this open-source project. Follow the guidelines to contribute.
28 changes: 18 additions & 10 deletions docs/guide/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,31 @@ To install the npm package, follow these steps:
1. Open your terminal.
2. Run the following command to install the package:

<Tabs>
<TabItem value="npm" label="npm">
::: tip

```sh
npm install akarjs
We are assuming that `Deno ^2.0` runtime can use our package with its built-in node.js runtime supports.

:::

::: code-group

```sh [npm]
$ npm add akarjs
```

</TabItem>
```sh [pnpm]
$ pnpm add akarjs
```

<TabItem value="yarn" label="yarn">
```sh [yarn]
$ yarn add akarjs
```

```sh
yarn add akarjs
```sh [bun]
$ bun add akarjs
```

</TabItem>
</Tabs>
:::

3. Verify the installation by checking the package version:

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ We recommend **node.js** `>=20.x` as the runtime to use our library without any

## Conclusion

This pkg was developed in Node.js runtime. Still it can be used in other `node.js` like runtimes such as `bun` or `deno 2.0` _(coming soon)_
This pkg was developed in Node.js runtime. Still it can be used in other `node.js` like runtimes such as `bun` or `deno ^2.0`
96 changes: 96 additions & 0 deletions docs/guide/infer-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Infer Types

## Summary

While working in TypeScript, we often need to use types for various usages. Our library gives the opportunity to generate types from your defined schemas.

## Table of Contents

- [Infer Schema](#infer-schema)
- [Infer Schema With Conditions](#infer-schema-with-conditions)
- [Why Conditional Types?](#why-conditional-types)
- [Conclusion](#conclusion)

## Infer Schema

```TypeScript
import { a, InferSchema } from 'akarjs'

// Sample | for example, an object-schema
const createTodo = a.object({
title: a.string().min(3),
completed: a.boolean().optional(),
})

type CreateTodo = InferSchema<typeof createTodo>

/*
/// Infers as:
type CreateTodo {
title: string;
completed: boolean | undefined;
}
*/

```

## Infer Schema With Conditions

```TypeScript
import { a, InferSchemaWithConditions } from 'akarjs'

// conditional for types
const updateUser = a.object({
name: a.string().min(3),
age: a.number().optional(),
role: a.enum(['customer', 'vendor'] as const).optional(),
})

type UpdateUser = InferSchemaWithConditions<typeof updateUser>

/*
/// Infers as:
type UpdateUser {
name: string;
} & {
age?: number;
} & {
role?: 'customer' | 'vendor';
}
*/

```

## Why Conditional Types?

We have noticed that when you infer a type and try to use it, you might face a problem. For example,

```TypeScript
const updateUser = a.object({
name: a.string().min(3),
age: a.number().optional(),
role: a.enum(['customer', 'vendor'] as const).optional(),
})

type UpdateUserT1 = InferSchema<typeof updateUser>
type UpdateUserT2 = InferSchemaWithConditions<typeof updateUser>

const user1: UpdateUserT1 = {
name: "Mahabub",
age: undefined, // property must be declared, otherwise it might make problems
role: undefined, // property must be declared, otherwise it might make problems
};

const user2: UpdateUserT2 = {
name: "Mahabub",
// no need to add optional properties
// Enjoy 👍
};

```

## Conclusion

In conclusion, leveraging the power of TypeScript with our library allows for robust type inference directly from your schemas. This not only ensures type safety but also enhances code maintainability and readability. By using conditional types, you can further refine your type definitions, making your code more flexible and easier to work with. We hope this guide helps you understand how to effectively use inferred types in your projects.
Loading

0 comments on commit d9f44d8

Please sign in to comment.