|
1 | | -# Engine Config |
| 1 | +<div align="center"> |
| 2 | + <br /> |
| 3 | + <p> |
| 4 | + <a href="https://github.com/NanoForge-dev"><img src="https://github.com/NanoForge-dev/blob/main/.github/logo.png" width="546" alt="NanoForge" /></a> |
| 5 | + </p> |
| 6 | + <br /> |
| 7 | + <p> |
| 8 | + <a href="https://www.npmjs.com/package/@nanoforge-dev/config"><img src="https://img.shields.io/npm/v/@nanoforge-dev/config.svg?maxAge=3600" alt="npm version" /></a> |
| 9 | + <a href="https://www.npmjs.com/package/@nanoforge-dev/config"><img src="https://img.shields.io/npm/dt/@nanoforge-dev/config.svg?maxAge=3600" alt="npm downloads" /></a> |
| 10 | + <a href="https://github.com/NanoForge-dev/Engine/actions"><img src="https://github.com/NanoForge-dev/Engine/actions/workflows/tests.yml/badge.svg" alt="Tests status" /></a> |
| 11 | + <a href="https://github.com/NanoForge-dev/Engine/commits/main/packages/config"><img src="https://img.shields.io/github/last-commit/NanoForge-dev/Engine.svg?logo=github&logoColor=ffffff&path=packages%2Fconfig" alt="Last commit." /></a> |
| 12 | + </p> |
| 13 | +</div> |
2 | 14 |
|
3 | | -## Installing dependencies |
| 15 | +## About |
4 | 16 |
|
5 | | -To install dependencies run: |
| 17 | +`@nanoforge-dev/config` is a wrapper of [class-validator][class-validator] and [class-transformer][class-transformer] to imports validation and transformation decorators. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +**Node.js 24.11.0 or newer is required.** |
6 | 22 |
|
7 | 23 | ```sh |
8 | | -pnpm i |
| 24 | +npm install @nanoforge-dev/config |
| 25 | +yarn add @nanoforge-dev/config |
| 26 | +pnpm add @nanoforge-dev/config |
| 27 | +bun add @nanoforge-dev/config |
9 | 28 | ``` |
10 | 29 |
|
11 | | -## Building |
| 30 | +## Warning |
12 | 31 |
|
13 | | -To build the project run: |
| 32 | +This library is of exclusive usage for other libraries. To put variables in the environment to allow libraries to use it through this config library, you must put it in factory options : |
14 | 33 |
|
15 | | -```sh |
16 | | -pnpm build |
| 34 | +```ts |
| 35 | +import { type IRunOptions } from "@nanoforge-dev/common"; |
| 36 | +import { NanoforgeFactory } from "@nanoforge-dev/core"; |
| 37 | +import { NetworkLibrary } from "@nanoforge-dev/network"; |
| 38 | + |
| 39 | +export async function main(options: IRunOptions) { |
| 40 | + const app = NanoforgeFactory.createClient({ |
| 41 | + environment: { |
| 42 | + serverTcpPort: "4445", |
| 43 | + serverUdpPort: "4444", |
| 44 | + serverAddress: "127.0.0.1", |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + const network = new NetworkLibrary(); |
| 49 | + |
| 50 | + app.useNetwork(network); |
| 51 | + |
| 52 | + await app.init(options); |
| 53 | + |
| 54 | + await app.run(); |
| 55 | +} |
17 | 56 | ``` |
18 | 57 |
|
19 | | -## Tests |
| 58 | +## Example usage |
20 | 59 |
|
21 | | -To run tests use: |
| 60 | +Initialize the library in your main file. |
22 | 61 |
|
23 | | -```sh |
24 | | -pnpm test |
| 62 | +```ts |
| 63 | +export class NetworkClientLibrary extends BaseNetworkLibrary { |
| 64 | + get __name(): string { |
| 65 | + return "NetworkClientLibrary"; |
| 66 | + } |
| 67 | + |
| 68 | + public override async __init(context: InitContext): Promise<void> { |
| 69 | + const config = await context.config.registerConfig(ClientConfigNetwork); |
| 70 | + |
| 71 | + // Do something with config |
| 72 | + } |
| 73 | +} |
25 | 74 | ``` |
| 75 | + |
| 76 | +Using this config |
| 77 | + |
| 78 | +```ts |
| 79 | +import { |
| 80 | + Default, |
| 81 | + Expose, |
| 82 | + IsByteLength, |
| 83 | + IsIpOrFQDN, |
| 84 | + IsOptional, |
| 85 | + IsPort, |
| 86 | +} from "@nanoforge-dev/config"; |
| 87 | + |
| 88 | +export class ClientConfigNetwork { |
| 89 | + // This var must be a string port (ex: "4444") but can be undefined |
| 90 | + @Expose() |
| 91 | + @IsOptional() |
| 92 | + @IsPort() |
| 93 | + serverTcpPort?: string; |
| 94 | + |
| 95 | + @Expose() |
| 96 | + @IsOptional() |
| 97 | + @IsPort() |
| 98 | + serverUdpPort?: string; |
| 99 | + |
| 100 | + // This var must be ip address or fqdn (it cannot be undefined) |
| 101 | + @Expose() |
| 102 | + @IsIpOrFQDN() |
| 103 | + serverAddress?: string; |
| 104 | + |
| 105 | + // This var must be a byte length between 2 and 64. It can be undefined as it as a default value. |
| 106 | + @Expose() |
| 107 | + @Default("PACKET_END") |
| 108 | + @IsByteLength(2, 64) |
| 109 | + magicValue!: string; |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Links |
| 114 | + |
| 115 | +- [GitHub][source] |
| 116 | +- [npm][npm] |
| 117 | + |
| 118 | +## Contributing |
| 119 | + |
| 120 | +Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the |
| 121 | +[documentation][documentation]. |
| 122 | +See [the contribution guide][contributing] if you'd like to submit a PR. |
| 123 | + |
| 124 | +## Help |
| 125 | + |
| 126 | +If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to ask questions in [discussions][discussions]. |
| 127 | + |
| 128 | +[documentation]: https://github.com/NanoForge-dev/Engine |
| 129 | +[discussions]: https://github.com/NanoForge-dev/Engine/discussions |
| 130 | +[source]: https://github.com/NanoForge-dev/Engine/tree/main/packages/config |
| 131 | +[npm]: https://www.npmjs.com/package/@nanoforge-dev/config |
| 132 | +[contributing]: https://github.com/NanoForge-dev/Engine/blob/main/.github/CONTRIBUTING.md |
| 133 | +[class-validator]: https://github.com/typestack/class-validator |
| 134 | +[class-transformer]: https://github.com/typestack/class-transformer |
0 commit comments