Skip to content

Commit

Permalink
chore: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbcf committed Jun 16, 2023
1 parent 88a7e9b commit 1afdd58
Showing 1 changed file with 91 additions and 6 deletions.
97 changes: 91 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Roadmap to the 1.0 release

- [X] 100% typesafety
- [ ] optional runtime arguments evaluation
- [X] optional runtime arguments evaluation
- [ ] wss backend support
- [X] event names obfuscation
- [ ] events rate limiting
Expand All @@ -30,6 +30,9 @@
- [`.extend`](#contract-extending)
- [`.init`](#contract-init)
- [`.setupRouter`](#contract-router-setup)
- [contract type check level](#contract-type-check-level)
- .getCheckTypeLevel
- .setCheckTypeLevel
- [$typeOnly](#typeonly)
- [alt:V built in types](#altv-built-in-types)
- $client
Expand Down Expand Up @@ -63,10 +66,11 @@ To create a contract you simply have to call the ``contract.create`` method
import { contract } from "@yannbcf/altv-rpc";
import { z } from "zod";

// empty contract
const ct = contract.create({});
// empty contract, with no runtime type checking
const ct = contract.create("no_typecheck", {});

const ct = contract.create({
// contract with runtime type checking
const ct = contract.create("typecheck", {
// an rpc simply emiting an event with a custom internal event name
coolRpc: {
internalEventName: "kekw",
Expand Down Expand Up @@ -154,15 +158,15 @@ async function main() {

## Contract router setup

In order to receive rpc's, and handle them you need to setup a router
In order to receive rpc's, and handle them you need to setup a router. As you may have noticed, the contract has a type checking level, if set to typecheck, every rpc's are going to be type checked at runtime using Zod's parsing evaluation for the args and returns rpc fields.

```ts
import { contract } from "@yannbcf/altv-rpc";
import { z } from "zod";

import * as alt from "alt-client";

const ct = contract.create({
const ct = contract.create("typecheck", {
test: {
args: z.object({
playerId: z.number(),
Expand All @@ -189,6 +193,87 @@ contract.setupRouter("server", ct, {
});
```

## Contract type check level

With the version @0.3.0 you define the contract type check level when creating it.

```ts
import { contract } from "@yannbcf/altv-rpc";

// runtime typechecking for the rpc's args / returns type
const ct1 = contract("typecheck", {});

// no runtime typechecking for the rpc's args / returns type
const ct2 = contract("no_typecheck", {});
```

You can get and set a contract check type level

```ts
import { contract } from "@yannbcf/altv-rpc";
import { ct } from "./contract.ts";

const typeLevel = contract.getCheckTypeLevel(ct);
contract.setCheckTypeLevel(ct, "typecheck");
```

You can override the contract check type level per router rpc handler

```ts
import { contract } from "@yannbcf/altv-rpc";
import { z } from "zod";

import * as alt from "alt-client";

const ct = contract.create("no_typecheck", {
test: {
args: z.object({
playerId: z.number(),
}),
},
});

contract.setupRouter("server", ct, {
on: alt.onClient,
emit: alt.emitClient
}, {
// args: { player: alt.Player, playerId: number }
test: (args) => {
//
},
});

contract.setupRouter("server", ct, {
on: alt.onClient,
emit: alt.emitClient
}, {
// even if the contract is set to no_typecheck, this specific rpc handler is going to be type checked for its arguments types and return value type
test: ["typecheck", (args) => {
//
}],
});

contract.setupRouter("server", ct, {
on: alt.onClient,
emit: alt.emitClient
}, {
// even if the contract is set to no_typecheck, this specific rpc handler is going to be type checked for its arguments types
test: ["typecheck_args", (args) => {
//
}],
});

contract.setupRouter("server", ct, {
on: alt.onClient,
emit: alt.emitClient
}, {
// even if the contract is set to no_typecheck, this specific rpc handler is going to be type checked for its return value type
test: ["typecheck_returns", (args) => {
//
}],
});
```

## $TypeOnly

Do you want to declare a schema without the runtime type checking ? The ``$typeOnly`` method is for here you. This allows you to precisely decide on which fields of your contract you want runtime type checking.
Expand Down

0 comments on commit 1afdd58

Please sign in to comment.