0.0.20 • Published 7 months ago

nuxt-ltrl v0.0.20

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

nuxt-ltrl

🍱 Compose system literals from JSON in Nuxt.

 Release Notes

Getting started

  1. Install the module
pnpm add nuxt-ltrl
  1. Create a config
// ~/ltrl.config.ts
import { defineLtrlConfig } from "nuxt-ltrl/config";

export default defineLtrlConfig({
  foo: "an example string",
  bar: ["primary", "secondary", "tertiary"],
  baz: {
    a: "A",
    b: "B",
    c: "C",
  },
  qux: [
    { id: 1, label: "One" },
    { id: 2, label: "Two" },
  ],
});
  1. Activate the module
// ~/nuxt.config.ts
import ltrl from "./ltrl.config";

export default defineNuxtConfig({
  modules: ["nuxt-ltrl"],
  ltrl,
});
  1. You are done, literally!

Features

Define system-level JSON configurations in Nuxt for:

  • constants Literal strings, numbers, or booleans
  • tuples Literal arrays of strings or numbers
  • enums Literal key/value object w/ string keys & string or number values
  • congruents Literal arrays of congruent key/value objects containing at least an id property

Read more

NOTE: system literals are not available within the ~/server directory

Utils

nuxt-ltrl exposes system literals to your Nuxt application to interact w/ your ltrl config:

FunctionDescription
useNuxtConstant(key)Access a ltrl constant extracted from your Nuxt config.
useNuxtTuple(key)Access a ltrl tuple extracted from your Nuxt config.
useNuxtEnum(key)Access a ltrl enum extracted from your Nuxt config.
useNuxtCongruent(key)Access a ltrl congruent extracted from your Nuxt config.
useNuxtLtrlConfig()Access the entire ltrl config defined in ~/nuxt.config.ts.
useNuxtLtrl(key)Access a specific ltrl object w/ a given key.

Usage

export function useLtrlFoo() {
  const { foo } = useNuxtLtrlConfig();
  return foo;
}

export function useLtrlBar() {
  return useNuxtLtrl("bar");
}

Types

nuxt-ltrl will generate types based on your configuration that are globally available within Nuxt. A type name is derived from the ltrl key in PascalCase w/ a Ltrl prefix, the type itself will depend on the literal that was defined.

Constants

Our example defines a constant literal w/ the foo key, which generates a literal constant:

export type LtrlFoo = "example";

Tuples

Our example defines a tuple literal w/ the bar key, which generates a literal tuple:

export type LtrlBar = ["primary", "secondary", "plain"];

Enums

Our example defines an enum literal w/ the baz key, which generates a literal enum:

export enum LtrlBaz {
  A = "A",
  B = "",
}

Congruents

Our example defines a congruent literal w/ the qux key, which generates a namespace containing a Template type & literal types for every supplied option:

export namespace LtrlQuz {
  export type Template = { id: number; label: string };
  export type One = { id: 1; label: "One" };
  export type Two = { id: 2; label: "Two" };
}

Option type names are derived from the label property.

Helpers

In addition to the generated types, nuxt-ltrl also exposes some type helpers:

TypeDescription
LtrlConstantConfigAn interface that maps ltrl constant keys to the literal type of the value resolved by the given key.
LtrlTupleConfigAn interface that maps ltrl tuple keys to the literal type of the value resolved by the given key.
LtrlEnumConfigAn interface that maps ltrl enum keys to the literal type of the value resolved by the given key.
LtrlCongruentConfigAn interface that maps ltrl congruent keys to the literal type of the value resolved by the given key.
LtrlConfigAn interface that maps the ltrl key to the literal type of value resolved by the given key.
LtrlConstantKeyA string-literal union type containing all available ltrl constant keys.
LtrlTupleKeyA string-literal union type containing all available ltrl tuple keys.
LtrlEnumKeyA string-literal union type containing all available ltrl enum keys.
LtrlCongruentKeyA string-literal union type containing all available ltrl congruent keys.
LtrlKeyA string-literal union type containing all available keys from the ltrl config.
LtrlConstant<K extends LtrlConstantKey>Access the literal type of a given ltrl constant key.
LtrlTuple<K extends LtrlTupleKey>Access the literal type of a given ltrl tuple key.
LtrlEnum<K extends LtrlEnumKey>Access the literal type of a given ltrl enum key.
LtrlCongruent<K extends LtrlCongruentKey>Access the literal type of a given ltrl congruent key.
LtrlValue<K extends LtrlKey>Access the literal type of a given ltrl key.
LtrlTupleItem<K extends LtrlTupleKey>Access a union-type representing the available options in a ltrl tuple.
LtrlEnumItem<K extends LtrlEnumKey>Access a union-type representing the available options in a ltrl enum.
LtrlCongruentItem<K extends LtrlCongruentKey>Access a union-type representing the available options in a ltrl congruent.

Usage

In this example, we are using the bar literal from our config to compose a string-literal union type & assigns the type to a prop in a button component:

<script setup lang="ts">
defineProps<{
  variant: LtrlTupleItem<"bar">; // or `LtrlBar[number]`
}>();
</script>

<template>
  <button :data-variant="variant">
    <slot />
  </button>
</template>

The component will have a string-literal type-helper when implementing the variant prop, complete w/ errors in the IDE if an invalid option is provided!

License

MIT License © 2024-PRESENT Alexander Thorwaldson

0.0.20

7 months ago

0.0.18

8 months ago

0.0.19

8 months ago

0.0.17

8 months ago

0.0.14

8 months ago

0.0.13

8 months ago

0.0.12

8 months ago

0.0.10

8 months ago

0.0.9

8 months ago