evikit 2.8.1 → 2.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -232,12 +232,9 @@ import { ErrorPage } from "./routes/error.js";
232
232
 
233
233
  acceptLanguages();
234
234
 
235
- /** @param {{ lang?: string, url: string }} data */
236
- export async function prerender(data) {
237
- return await ssr(App, data);
238
- }
235
+ export const prerender = ssr(App);
239
236
 
240
- /** @param {{ dehydratedState?: ReturnType<import("evikit").dehydrate> | undefined, i18n: ReturnType<import("evikit").useI18n> }} props */
237
+ /** @type {import("evikit").SsrApp} */
241
238
  function App({ dehydratedState, i18n }) {
242
239
  return h(
243
240
  Router,
@@ -484,12 +481,12 @@ import { QuestionIdGet } from "./types.js";
484
481
  export const QuestionIdServer = {
485
482
  get: endpoint(QuestionIdGet, async ({ input }) => {
486
483
  const i18n = await getI18n(input.lang);
487
- const Question = db.Question.select({
484
+ const [question] = db.Question.select({
488
485
  with: { Choice: true },
486
+ where: { id: input.id },
489
487
  });
490
488
  // It maps join results to objects, for example you can get:
491
- // Question[0]?.Choice[0]?.text
492
- const question = Question.find(({ id }) => id == input.id);
489
+ // question?.Choice[0]?.text
493
490
  if (!question) {
494
491
  return text(i18n.gettext("Question does not exist"), 404);
495
492
  }
@@ -567,8 +564,7 @@ export const ChoiceServer = {
567
564
  post: endpoint(ChoicePost, async ({ input }) => {
568
565
  const i18n = await getI18n(input.lang);
569
566
 
570
- const Question = db.Question.select();
571
- const question = Question.find(({ id }) => id == input.questionId);
567
+ const [question] = db.Question.select({ where: { id: input.questionId } });
572
568
  if (!question) {
573
569
  return text(i18n.gettext("Question does not exist"), 400);
574
570
  }
@@ -649,8 +645,7 @@ export const VoteServer = {
649
645
  post: endpoint(VotePost, async ({ input }) => {
650
646
  const i18n = await getI18n(input.lang);
651
647
 
652
- const Choice = db.Choice.select();
653
- const choice = Choice.find(({ id }) => id == input.choiceId);
648
+ const [choice] = db.Choice.select({ where: { id: input.choiceId } });
654
649
  if (!choice) {
655
650
  return text(i18n.gettext("You didn't select a choice"), 400);
656
651
  }
package/db.d.ts CHANGED
@@ -14,31 +14,17 @@ export type Db<
14
14
  > = {
15
15
  [Table in keyof R]: Table extends keyof F & keyof Schema & string
16
16
  ? {
17
- delete(where: {
18
- [K in keyof Schema[Table]["entries"]]?:
19
- | InferOutput<Schema[Table]["entries"][K]>
20
- | InferOutput<Schema[Table]["entries"][K]>[];
21
- }): void;
17
+ delete(where: Where<Schema, Table>): void;
22
18
 
23
19
  select<
24
- W extends Partial<Join<R, Table>> & {
25
- where?: {
26
- [K in keyof Schema[Table]["entries"]]?:
27
- | InferOutput<Schema[Table]["entries"][K]>
28
- | InferOutput<Schema[Table]["entries"][K]>[];
29
- };
30
- },
20
+ W extends Partial<Join<R, Table>> & { where?: Where<Schema, Table> },
31
21
  >(
32
22
  props?: W,
33
23
  ): Select<Schema, R, F, Table, W>[];
34
24
 
35
25
  update(query: {
36
26
  set: Partial<InferOutput<Schema[Table]>>;
37
- where: {
38
- [K in keyof Schema[Table]["entries"]]?:
39
- | InferOutput<Schema[Table]["entries"][K]>
40
- | InferOutput<Schema[Table]["entries"][K]>[];
41
- };
27
+ where: Where<Schema, Table>;
42
28
  }): void;
43
29
 
44
30
  upsert(
@@ -164,6 +150,22 @@ export type Relations<Schema extends Record<string, ObjectSchema<any, any>>> = {
164
150
  : undefined;
165
151
  };
166
152
 
153
+ export type Where<
154
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
155
+ Schema extends Record<string, ObjectSchema<any, any>>,
156
+ Table extends keyof Schema,
157
+ > = {
158
+ [K in keyof Schema[Table]["entries"]]?:
159
+ | InferOutput<Schema[Table]["entries"][K]>
160
+ | InferOutput<Schema[Table]["entries"][K]>[]
161
+ | Partial<
162
+ Record<
163
+ "gt" | "gte" | "lt" | "lte" | "not",
164
+ InferOutput<Schema[Table]["entries"][K]>
165
+ >
166
+ >;
167
+ };
168
+
167
169
  type OmitExtends<T, U> = {
168
170
  [K in keyof T as T[K] extends U ? never : K]: T[K];
169
171
  };
package/index.d.ts CHANGED
@@ -1,158 +1,167 @@
1
- import type { ClassAttributes, ComponentChildren, JSX, VNode } from "preact";
2
-
3
- import i18n from "gettext.js";
4
-
5
- import { flatten, primary, references, relations } from "./db.js";
6
- import { useApi } from "./use-api.js";
7
- import { useEnhance } from "./use-enhance.js";
8
-
9
- export interface DehydratedState {
10
- queries: { queryHash: string; state: QueryState }[];
11
- }
12
-
13
- export interface QueryState {
14
- data: any; // eslint-disable-line @typescript-eslint/no-explicit-any
15
- dataUpdatedAt: number;
16
- error: null | unknown;
17
- isInvalidated: boolean;
18
- status: "error" | "loading" | "success";
19
- }
20
-
21
- type H<T extends keyof JSX.IntrinsicElements> = (
22
- props: (ClassAttributes<HTMLElement> & JSX.IntrinsicElements[T]) | null,
23
- ...children: ComponentChildren[]
24
- ) => VNode<ClassAttributes<HTMLElement> & JSX.IntrinsicElements[T]>;
25
-
26
- export function dehydrate(
27
- queryClient: ReturnType<typeof useQueryClient>,
28
- options: {
29
- shouldRedactErrors: (error: unknown) => boolean;
30
- },
31
- ): DehydratedState;
32
-
33
- export function getI18n(...langs: string[]): Promise<i18n.Gettext>;
34
-
35
- export function hydrate(
36
- App: (props: {
37
- dehydratedState?: DehydratedState | undefined;
38
- i18n: i18n.Gettext;
39
- }) => VNode<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
40
- renderTarget: string,
41
- ): Promise<void>;
42
-
43
- export function Router({
44
- children,
45
- dehydratedState,
46
- i18n,
47
- }: {
48
- children?: ComponentChildren;
49
- dehydratedState?: DehydratedState | undefined;
50
- i18n: i18n.Gettext;
51
- }): VNode<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
52
-
53
- export function ssr(
54
- App: (props: {
55
- dehydratedState?: DehydratedState | undefined;
56
- i18n: i18n.Gettext;
57
- }) => VNode<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
58
- data: { cookie?: string | undefined; lang?: string; url: string },
59
- ): Promise<{
60
- data: {
61
- dehydratedState: DehydratedState;
62
- lang?: string;
63
- url: string;
64
- };
65
- head: {
66
- elements: Set<{
67
- props: Record<string, string>;
68
- type: string;
69
- }>;
70
- lang: string;
71
- title: string;
72
- };
73
- html: string;
74
- links: Set<string> | undefined;
75
- }>;
76
-
77
- export function useI18n(): i18n.Gettext;
78
-
79
- export function useQueryClient(): {
80
- invalidateQueries(): void;
81
- };
82
-
83
- export function useStatus(error: unknown): string;
84
-
85
- export const a: H<"a">;
86
- export const address: H<"address">;
87
- export const article: H<"article">;
88
- export const aside: H<"aside">;
89
- export const audio: H<"audio">;
90
- export const b: H<"b">;
91
- export const blockquote: H<"blockquote">;
92
- export const br: H<"br">;
93
- export const button: H<"button">;
94
- export const canvas: H<"canvas">;
95
- export const circle: H<"circle">;
96
- export const code: H<"code">;
97
- export const del: H<"del">;
98
- export const details: H<"details">;
99
- export const dialog: H<"dialog">;
100
- export const div: H<"div">;
101
- export const ellipse: H<"ellipse">;
102
- export const em: H<"em">;
103
- export const fieldset: H<"fieldset">;
104
- export const figcaption: H<"figcaption">;
105
- export const figure: H<"figure">;
106
- export const footer: H<"footer">;
107
- export const form: H<"form">;
108
- export const h1: H<"h1">;
109
- export const h2: H<"h2">;
110
- export const h3: H<"h3">;
111
- export const h4: H<"h4">;
112
- export const h5: H<"h5">;
113
- export const h6: H<"h6">;
114
- export const header: H<"header">;
115
- export const hgroup: H<"hgroup">;
116
- export const hr: H<"hr">;
117
- export const i: H<"i">;
118
- export const iframe: H<"iframe">;
119
- export const img: H<"img">;
120
- export const input: H<"input">;
121
- export const ins: H<"ins">;
122
- export const label: H<"label">;
123
- export const legend: H<"legend">;
124
- export const li: H<"li">;
125
- export const main: H<"main">;
126
- export const nav: H<"nav">;
127
- export const ol: H<"ol">;
128
- export const option: H<"option">;
129
- export const p: H<"p">;
130
- export const path: H<"path">;
131
- export const picture: H<"picture">;
132
- export const polyline: H<"polyline">;
133
- export const pre: H<"pre">;
134
- export const rect: H<"rect">;
135
- export const section: H<"section">;
136
- export const select: H<"select">;
137
- export const small: H<"small">;
138
- export const source: H<"source">;
139
- export const span: H<"span">;
140
- export const strong: H<"strong">;
141
- export const sub: H<"sub">;
142
- export const summary: H<"summary">;
143
- export const sup: H<"sup">;
144
- export const svg: H<"svg">;
145
- export const table: H<"table">;
146
- export const tbody: H<"tbody">;
147
- export const td: H<"td">;
148
- export const text: H<"text">;
149
- export const textarea: H<"textarea">;
150
- export const th: H<"th">;
151
- export const thead: H<"thead">;
152
- export const title: H<"title">;
153
- export const tr: H<"tr">;
154
- export const ul: H<"ul">;
155
- export const video: H<"video">;
156
-
157
- export { flatten, primary, references, relations };
158
- export { useApi, useEnhance };
1
+ import type { ClassAttributes, ComponentChildren, JSX, VNode } from "preact";
2
+
3
+ import i18n from "gettext.js";
4
+
5
+ import { flatten, primary, references, relations } from "./db.js";
6
+ import { useApi } from "./use-api.js";
7
+ import { useEnhance } from "./use-enhance.js";
8
+
9
+ export interface DehydratedState {
10
+ queries: { queryHash: string; state: QueryState }[];
11
+ }
12
+
13
+ export interface QueryState {
14
+ data: any; // eslint-disable-line @typescript-eslint/no-explicit-any
15
+ dataUpdatedAt: number;
16
+ error: null | unknown;
17
+ isInvalidated: boolean;
18
+ status: "error" | "loading" | "success";
19
+ }
20
+
21
+ export type SsrApp = (props: {
22
+ dehydratedState?: DehydratedState | undefined;
23
+ i18n: i18n.Gettext;
24
+ }) => VNode<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
25
+
26
+ export interface SsrProps {
27
+ cookie?: string | undefined;
28
+ lang?: string;
29
+ url: string;
30
+ }
31
+
32
+ export interface SsrResponse {
33
+ data: {
34
+ dehydratedState: DehydratedState;
35
+ lang?: string;
36
+ url: string;
37
+ };
38
+ head: {
39
+ elements: Set<{
40
+ props: Record<string, string>;
41
+ type: string;
42
+ }>;
43
+ lang: string;
44
+ title: string;
45
+ };
46
+ html: string;
47
+ links: Set<string> | undefined;
48
+ }
49
+
50
+ type H<T extends keyof JSX.IntrinsicElements> = (
51
+ props: (ClassAttributes<HTMLElement> & JSX.IntrinsicElements[T]) | null,
52
+ ...children: ComponentChildren[]
53
+ ) => VNode<ClassAttributes<HTMLElement> & JSX.IntrinsicElements[T]>;
54
+
55
+ export function dehydrate(
56
+ queryClient: ReturnType<typeof useQueryClient>,
57
+ options: {
58
+ shouldRedactErrors: (error: unknown) => boolean;
59
+ },
60
+ ): DehydratedState;
61
+
62
+ export function getI18n(...langs: string[]): Promise<i18n.Gettext>;
63
+
64
+ export function hydrate(
65
+ App: (props: {
66
+ dehydratedState?: DehydratedState | undefined;
67
+ i18n: i18n.Gettext;
68
+ }) => VNode<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
69
+ renderTarget: string,
70
+ ): Promise<void>;
71
+
72
+ export function Router({
73
+ children,
74
+ dehydratedState,
75
+ i18n,
76
+ }: {
77
+ children?: ComponentChildren;
78
+ dehydratedState?: DehydratedState | undefined;
79
+ i18n: i18n.Gettext;
80
+ }): VNode<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
81
+
82
+ /** @deprecated */
83
+ export function ssr(App: SsrApp, props: SsrProps): Promise<SsrResponse>;
84
+ export function ssr(App: SsrApp): (props: SsrProps) => Promise<SsrResponse>;
85
+
86
+ export function useI18n(): i18n.Gettext;
87
+
88
+ export function useQueryClient(): {
89
+ invalidateQueries(): void;
90
+ };
91
+
92
+ export function useStatus(error: unknown): string;
93
+
94
+ export const a: H<"a">;
95
+ export const address: H<"address">;
96
+ export const article: H<"article">;
97
+ export const aside: H<"aside">;
98
+ export const audio: H<"audio">;
99
+ export const b: H<"b">;
100
+ export const blockquote: H<"blockquote">;
101
+ export const br: H<"br">;
102
+ export const button: H<"button">;
103
+ export const canvas: H<"canvas">;
104
+ export const circle: H<"circle">;
105
+ export const code: H<"code">;
106
+ export const del: H<"del">;
107
+ export const details: H<"details">;
108
+ export const dialog: H<"dialog">;
109
+ export const div: H<"div">;
110
+ export const ellipse: H<"ellipse">;
111
+ export const em: H<"em">;
112
+ export const fieldset: H<"fieldset">;
113
+ export const figcaption: H<"figcaption">;
114
+ export const figure: H<"figure">;
115
+ export const footer: H<"footer">;
116
+ export const form: H<"form">;
117
+ export const h1: H<"h1">;
118
+ export const h2: H<"h2">;
119
+ export const h3: H<"h3">;
120
+ export const h4: H<"h4">;
121
+ export const h5: H<"h5">;
122
+ export const h6: H<"h6">;
123
+ export const header: H<"header">;
124
+ export const hgroup: H<"hgroup">;
125
+ export const hr: H<"hr">;
126
+ export const i: H<"i">;
127
+ export const iframe: H<"iframe">;
128
+ export const img: H<"img">;
129
+ export const input: H<"input">;
130
+ export const ins: H<"ins">;
131
+ export const label: H<"label">;
132
+ export const legend: H<"legend">;
133
+ export const li: H<"li">;
134
+ export const main: H<"main">;
135
+ export const nav: H<"nav">;
136
+ export const ol: H<"ol">;
137
+ export const option: H<"option">;
138
+ export const p: H<"p">;
139
+ export const path: H<"path">;
140
+ export const picture: H<"picture">;
141
+ export const polyline: H<"polyline">;
142
+ export const pre: H<"pre">;
143
+ export const rect: H<"rect">;
144
+ export const section: H<"section">;
145
+ export const select: H<"select">;
146
+ export const small: H<"small">;
147
+ export const source: H<"source">;
148
+ export const span: H<"span">;
149
+ export const strong: H<"strong">;
150
+ export const sub: H<"sub">;
151
+ export const summary: H<"summary">;
152
+ export const sup: H<"sup">;
153
+ export const svg: H<"svg">;
154
+ export const table: H<"table">;
155
+ export const tbody: H<"tbody">;
156
+ export const td: H<"td">;
157
+ export const text: H<"text">;
158
+ export const textarea: H<"textarea">;
159
+ export const th: H<"th">;
160
+ export const thead: H<"thead">;
161
+ export const title: H<"title">;
162
+ export const tr: H<"tr">;
163
+ export const ul: H<"ul">;
164
+ export const video: H<"video">;
165
+
166
+ export { flatten, primary, references, relations };
167
+ export { useApi, useEnhance };