kql icon indicating copy to clipboard operation
kql copied to clipboard

TypeScript types

Open benwest opened this issue 1 year ago • 5 comments

I've been making some moves on TypeScript typings for KQL queries. It's early days, but I am able to derive response types from queries pretty well already.

I'm testing and developing it in my current project, and I'm keen for anyone else to give it a try or contribute (In particular, the models.ts file is incomplete and needs filling out with the types of all Kirby's built-in stuff).

https://github.com/benwest/kql-ts

import { createClient } from "./kql/client";
import { KQLQueryData } from "./kql/query";

const query = {
  query: "site",
  select: {
    title: true,
    logo: {
      query: "site.logo.toFile",
      select: {
        srcset: true,
        width: true,
        height: true,
        placeholder: "file.resize(5).url",
      },
    },
    pages: {
      query: "site.children",
      select: {
        title: true,
        tags: `page.tags.split(',')`,
      },
    },
  },
} as const; // <- important

type Content = KQLQueryData<typeof query>;
/*   ^^^^^^^
{
  readonly title: string;
  readonly logo: {
      readonly srcset: string;
      readonly width: number;
      readonly height: number;
      readonly placeholder: string;
  } | null;
  readonly pages: {
      readonly title: string;
      readonly tags: string[];
  }[];
} */

const kql = createClient({
  user: KQL_USER,
  password: KQL_PASSWORD,
  url: KQL_URL,
});

const response = await kql(query);
if (response.status === "ok") {
  const data = response.result; // strongly typed!
}

benwest avatar Mar 21 '23 16:03 benwest

Just to add: There's also kirby-fest by @johannschopplich.

tobimori avatar Mar 21 '23 17:03 tobimori

While this is really great, it feels like it's the wrong place as an issue. Would you mind checking out @johannschopplich's plugin? I cannot really compare the two sets of types at the moment, but if you feel like yours still adds enough unique value, we could add it to the plugins directory.

bastianallgeier avatar Apr 21 '23 10:04 bastianallgeier

While this is really great, it feels like it's the wrong place as an issue. Would you mind checking out @johannschopplich's plugin? I cannot really compare the two sets of types at the moment, but if you feel like yours still adds enough unique value, we could add it to the plugins directory.

I know there are some query libraries (e.g. for Nuxt) in the plugin directory already, but I don't feel like they actually fit in because they're not installable as "real" Kirby plugin?

There probably needs to be some kind of separate section or category for third party libraries that were especially designed for Kirby or stuff like the Starterkits from Johann.

Otherwise I'd leave them as mentions in this repositories' readme. (https://github.com/getkirby/kql#plugins)

tobimori avatar Apr 21 '23 12:04 tobimori

You are totally right. They don't fit super well in there. I wonder if we could come up with some sort of category for the plugin directory that would make it clearer. Mentioning them in the readmes always feel a bit like we are underrepresenting those projects.

bastianallgeier avatar Apr 24 '23 09:04 bastianallgeier

👍 For a separate Kirby plugin page “integration” plugins.

johannschopplich avatar Apr 25 '23 13:04 johannschopplich