Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript types #47

Open
benwest opened this issue Mar 21, 2023 · 5 comments
Open

TypeScript types #47

benwest opened this issue Mar 21, 2023 · 5 comments

Comments

@benwest
Copy link
Contributor

benwest commented Mar 21, 2023

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!
}
@tobimori
Copy link

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

@bastianallgeier
Copy link
Member

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.

@tobimori
Copy link

tobimori commented Apr 21, 2023

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)

@bastianallgeier
Copy link
Member

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.

@johannschopplich
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants