Skip to content

Commit

Permalink
docs: 📝 add back Constraint fields section to README
Browse files Browse the repository at this point in the history
  • Loading branch information
cat394 committed Jul 10, 2024
1 parent f1e18da commit 5cf4b3a
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![A purple-haired, pink-eyed character named Kokomi says, 'I wish broken links would just disappear from this world!'](https://github.com/cat394/link-generator/blob/master/images/thumbnail.webp)
![A purple-haired, pink-eyed character named Kokomi says, 'I wish broken links would just disappear from this world!'](https://github.com/cat394/link-generator/blob/main/images/thumbnail.webp)

# Link Generator

Expand Down Expand Up @@ -138,6 +138,78 @@ Example:
// => '/users/alice/posts/1?q=page=10'
```

### Constraint Fields

The type of values for path and query parameters is `string|number` by default.
While this is sufficient in most cases, this type can be made more strict by
defining a **constraint field**. This is a special string that can be included
in the path, like `<Constraint>`. Conditions can be defined within open (`<`)
and close (`>`) mountain brackets. In this field, the following three type
constraints can be placed on path and query parameters:

- **String type**

You can narrow down the id to a string type by defining a condition field with
the string as the condition followed by the parameter name, such as
`/:id<string>`.

- **Numumber type**

You can narrow down the id to a number type by defining a condition field with a
parameter name followed by the string number, as in `/:id<number>`.

- **String or Number literal union type**

You can create a literal type for those values by writing `(Literal|Union)` for
the condition followed by the parameter and separated by the `|` sign, as in
`/id<(a|b|10)>`. If the value can be converted to a numeric value, it is
inferred as a numeric literal type. To define the query parameter for a route,
add a `query` property to the route configuration object. The query property
should be an object with keys as query parameter names and values as types.

Example:

1. Define a route configuration object with condition fields defined in the
path.

```ts
const routeConfig = {
users: {
path: "users/:userid<string>",
},
posts: {
path: "posts/:postid<number>",
},
categories: {
path: " categories/:categoryid<(a|b|10)>",
},
} as const satisfies RouteConfig;
```

2. Flattens the routing object

```ts
const flatRouteConfig = flattenRouteConfig(routeConfig);
```

3. Create a link generator.

```ts
const link = createLinkGenerator(flatRouteConfig);
```

4. link is generated.

You will notice that the userid value is of type string, the postid value is
of type number, and the categoryid value is of type `'a'|'b'|10` union. You
will notice that the value of categoryid is a union type of `'a'|'b'|10`.

```ts
const userpage = link("users", { userid: "alice" }); // userid only accept string type!
const postpage = link("posts", { postid: 1 }); // postid only accept number type!
const categorypage = link("categories", { categoryid: "a" }); // categoryid only accept 'a' or 'b' or 10!
```

### Optional Type

When a path parameter or query parameter value is set to `null` or `undefined`,
Expand Down

0 comments on commit 5cf4b3a

Please sign in to comment.