open-index/open-npm huggingface dataset:
npm package: @jafps/config
Description: Functions for reading configuration.
Author: Joshua Avalon
Latest version: 1.2.2

# @jafps/config

Functions for reading configuration.

## Usage

Read configuration and validate it with schema.

```ts
import { readConfig } from "@jafps/config";
import { JsonConfigReader } from "@jafps/config/json";
import { Type } from "typebox";

const cfgSchema = Type.Object({
  a: Type.String()
});
const cfg = await readConfig({
  readers: [new JsonConfigReader("/config.json")],
  schema: cfgSchema
});
```

### Environment Variables

```ts
import {
  BooleanEnvProperty,
  EnvConfigReader,
  NumberEnvProperty,
  StringCommaListEnvProperty,
  StringEnvProperty
} from "@jafps/config/env";

/**
 * {
 *   "a": true,
 *   "c": {
 *      "d": 1,
 *      "e": "a"
 *    },
 *   "f": ["a", "b"]
 * }
 */
const reader = new EnvConfigReader({
  a: new BooleanEnvProperty("ENV_BOOLEAN"), // true or false
  c: {
    d: new NumberEnvProperty("ENV_NUMBER"), // number
    e: new StringEnvProperty("ENV_STRING") // any characters
  },
  f: new StringCommaListEnvProperty("ENV_STRING_ARRAY") // Comma separated string
});
```

### JSON

```ts
import { JsonConfigReader } from "@jafps/config/json";

const reader = new JsonConfigReader("/config.json");
```

### YAML

```ts
import { YamlConfigReader } from "@jafps/config/yaml";

const reader = new YamlConfigReader("/config.yaml");
```
npm package: @jafps/password
Description: Functions for hashing and verifying password.
Author: Joshua Avalon
Latest version: 1.0.4

# @jafps/password

Functions for hashing and verifying password.

## Usage

```ts
import { verifyPassword, hashPassword } from "@jafps/password";
import { randomUUID } from "node:crypto";

const password = randomUUID();
const hash = await hashPassword(password);
const verifiedResult = await verifyPassword(hash, password);

// Hash with predefined parameters
const hash2 = await hashPassword(password, { limit: "moderate" });
// Hash with custom parameters
const hash3 = await hashPassword(password, { memLimit, opsLimit });
```
npm package: @jafps/plugin-error
Description: Fastify plugin for handling global errors formatting and error schemas.
Author: Joshua Avalon
Latest version: 1.0.2

# @jafps/plugin-error

Fastify plugin for handling global errors formatting and error schemas.

## Usage

```ts
import errorPlugin from "@jafps/plugin-error";

await app.register(errorPlugin);
```

### Error Formatter

An `ErrorFormatter` is responsible for formatter caught error to JSON.
For unsupported error, the formatter should return `null`.
The first formatter that return a non-null value is used.

```ts
import type { ErrorFormatter } from "@jafps/plugin-error";

const customErrorFormatter: ErrorFormatter = async function (err) {
  if (!(err instanceof HttpError) || err.status < 400) {
    return null;
  }
  const message = statuses[err.status];
  return {
    code: message,
    data: { message },
    status: err.status,
    success: false
  };
};
```

### Error Schema

A typebox schema must be set in route or added to global schema before throwing.

#### Route Specific Error

```ts
import type { TSchema } from "typebox";

const errorSchema: TSchema;
app.get(
  "/throw-error",
  {
    schema: {
      querystring: querySchema,
      response: {
        200: responseSchema,
        400: errorSchema
      }
    }
  },
  async function (req, res) {
    return res.send({ success: true });
  }
);
```

#### Global Error

```ts
import type { TSchema } from "typebox";

const errorSchema: TSchema;
app.errorSchemas.addSchema(400, errorSchema);
```
npm package: @jafps/plugin-openapi
Description: Fastify plugin for generating OpenAPI 3.1 document.
Author: Joshua Avalon
Latest version: 1.2.1

# @jafps/plugin-openapi

Fastify plugin for generating OpenAPI 3.1 document.

## Usage

### Register Plugin

```ts
import openapiPlugin from "@jafps/plugin-openapi";

await app.register(openapiPlugin, {
  openapi: {
    info: {
      title: "API",
      version: "1.0.0"
    }
  }
});
```

`openapi`: Basic options for OpenAPI.  
For example, `externalDocs`, `security`, `servers`, `webhooks` are not populated by this plugin.  
See [OpenAPI Specification v3.1.0][openapi] for details.

`exposeHeadRoutes`: `true` to expose `HEAD` routes. It can also be configured at route level.  
Default is `false`.

`hideUntagged`: Hide routes that have no tags.  
Default is `false`.

[openapi]: https://spec.openapis.org/oas/v3.1.0.html#openapi-object
npm package: @jafps/plugin-redoc
Description: Fastify plugin for OpenAPI 3.1 API reference documentation.
Author: Joshua Avalon
Latest version: 1.0.4

# @jafps/plugin-redoc

Fastify plugin for OpenAPI 3.1 API reference documentation.

It depends on [@fastify/helmet] and [@jafps/plugin-openapi].

## Usage

```ts
import redocPlugin from "@jafps/plugin-redoc";
import helmetPlugin from "@fastify/helmet";
import openapiPlugin from "@jafps/plugin-openapi";

await app.register(helmetPlugin, { global: false });
await app.register(openapiPlugin, {
  openapi: {
    info: {
      title: "API",
      version: "1.0.0"
    }
  }
});
await app.register(redocPlugin);
```

If [@fastify/static] is used, `staticOptions.decorateReply` need to be set to `false`.

[@fastify/static]: https://github.com/fastify/fastify-static
[@fastify/helmet]: https://github.com/fastify/fastify-helmet
[@jafps/plugin-openapi]: https://github.com/jafpsjs/plugin-openapi
npm package: @jafps/plugin-scalar
Description: Fastify plugin for OpenAPI 3.1 API reference documentation.
Author: Joshua Avalon
Latest version: 1.0.7

# @jafps/plugin-scalar

Fastify plugin for OpenAPI 3.1 API reference documentation.

It depends on [@fastify/helmet] and [@jafps/plugin-openapi].

## Usage

```ts
import scalarPlugin from "@jafps/plugin-scalar";
import helmetPlugin from "@fastify/helmet";
import openapiPlugin from "@jafps/plugin-openapi";

await app.register(helmetPlugin, { global: false });
await app.register(openapiPlugin, {
  openapi: {
    info: {
      title: "API",
      version: "1.0.0"
    }
  }
});
await app.register(scalarPlugin);
```

If [@fastify/static] is used, `staticOptions.decorateReply` need to be set to `false`.

[@fastify/static]: https://github.com/fastify/fastify-static
[@fastify/helmet]: https://github.com/fastify/fastify-helmet
[@jafps/plugin-openapi]: https://github.com/jafpsjs/plugin-openapi
npm package: @jafps/plugin-schema
Description: Fastify plugin for using typebox to handle JSON schemas.
Author: Joshua Avalon
Latest version: 1.2.8

# @jafps/plugin-schema

Fastify plugin for using typebox to handle JSON schemas.

It depends on [@jafps/plugin-error].

## Usage

```ts
import schemaPlugin from "@jafps/plugin-schema";

await app.register(schemaPlugin);
```

[@jafps/plugin-error]: https://github.com/jafpsjs/plugin-error
npm package: @jafps/plugin-session
Description: Fastify plugin for session.
Author: Joshua Avalon
Latest version: 1.0.2

# @jafps/plugin-session

Fastify plugin for session. Use [typebox] to ensure the session match the schema.  
Inspired by [@fastify/secure-session].

It depends on [@fastify/cookie].

## Usage

### Register Plugin

```ts
import sessionPlugin from "@jafps/plugin-session";

await app.register(sessionPlugin, {
  session: {
    salt,
    schema,
    secret: "a".repeat(32)
  },
  session2: {
    salt,
    schema: Type.Object({ b: Type.String({ default: "a" }) }),
    secret: "a".repeat(32)
  }
});

declare module "fastify" {
  interface FastifySessions {
    session: { a: number };
    session2: { b: string };
  }
}
```

For all the sessions, it must be defined in `FastifySessions` and a corresponding configuration defined in options.

A `key` that is 32 bytes or 16 bytes `salt` with at least 32 bytes `secret` can be used.

### JSON Schema

```ts
Type.Object({ b: Type.Options(Type.String(), { default: "a" }) });
Type.Object({ b: Type.Optional(Type.String()) });
```

Properties in JSON schema must be defined `default` or optional. Otherwise, the initial session will throw error.

### Serialization

By using `Type.Codec`, the session can be serialized and deserialized automatically.

```ts
declare module "fastify" {
  interface FastifySessions {
    session: { foo: Date };
  }
}

await app.register(plugin, {
  session: {
    salt,
    schema: Type.Object({
      foo: Type.Codec(Type.Options(Type.String(), { format: "date-time" }))
        .Decode((v) => new Date(v))
        .Encode((v) => v.toISOString())
    }),
    secret: "a".repeat(32)
  }
});

req.sessions.session.set(foo, new Date());
```

[typebox]: https://github.com/sinclairzx81/typebox
[@fastify/secure-session]: https://github.com/fastify/fastify-secure-session
[@fastify/cookie]: https://github.com/fastify/fastify-cookie