noofpi 1.1.1 → 1.2.0

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.
@@ -3,7 +3,7 @@ import {
3
3
  createViteConfig,
4
4
  finishPresetBuild,
5
5
  resolveFonts
6
- } from "./chunk-M7TMNIVH.js";
6
+ } from "./chunk-UF6YTIXD.js";
7
7
  import "./chunk-NX6BO7UB.js";
8
8
  import "./chunk-MTXQR32N.js";
9
9
  import "./chunk-ELXCFUA4.js";
@@ -38,10 +38,11 @@ import {
38
38
  ref
39
39
  } from "vue";
40
40
  var PAYLOAD_KEY = /* @__PURE__ */ Symbol("noofpi:payload");
41
+ var isDoofpiClientError = (error) => error instanceof Error && error.name === "DoofpiClientError";
41
42
  function toError(error, server = false) {
42
43
  const { message, status } = error ?? {};
43
44
  return {
44
- message: server && !isNoofpiError(error) ? "Internal Server Error" : typeof message === "string" ? message : String(error),
45
+ message: server && !isNoofpiError(error) && !isDoofpiClientError(error) ? "Internal Server Error" : typeof message === "string" ? message : String(error),
45
46
  ...typeof status === "number" ? { status } : {}
46
47
  };
47
48
  }
@@ -568,14 +568,15 @@ function scanPageMiddleware(root) {
568
568
  // src/vite/types.ts
569
569
  import { existsSync as existsSync5, mkdirSync, readFileSync as readFileSync3, writeFileSync } from "fs";
570
570
  import path9 from "path";
571
- function doofpiTypes() {
571
+ function doofpiTypes(fetch2 = "lfetch") {
572
572
  return [
573
573
  "// Generated by noofpi, do not edit",
574
574
  'import type * as server from "../server/doofpi";',
575
575
  "",
576
576
  'declare module "noofpi/runtime" {',
577
577
  " interface Register {",
578
- " routes: typeof server extends { routes: infer R } ? R : any;",
578
+ " localRoutes: typeof server extends { routes: infer R } ? R : any;",
579
+ ` defaultFetch: "${fetch2}";`,
579
580
  " }",
580
581
  "}",
581
582
  ""
@@ -637,10 +638,10 @@ function writeI18nTypes(root, i18n) {
637
638
  mkdirSync(path9.dirname(file), { recursive: true });
638
639
  writeFileSync(file, content);
639
640
  }
640
- function writeDoofpiTypes(root) {
641
+ function writeDoofpiTypes(root, fetch2) {
641
642
  if (!existsSync5(path9.join(root, "server/doofpi.ts"))) return;
642
643
  const file = path9.join(root, ".noofpi/doofpi.d.ts");
643
- const content = doofpiTypes();
644
+ const content = doofpiTypes(fetch2);
644
645
  if (existsSync5(file) && readFileSync3(file, "utf8") === content) return;
645
646
  mkdirSync(path9.dirname(file), { recursive: true });
646
647
  writeFileSync(file, content);
@@ -707,7 +708,7 @@ function noofpiPlugin(options) {
707
708
  `[noofpi] i18n locale "${locale}" has no file app/locales/${locale}.ts, it falls back to "${config.i18n.defaultLocale}"`
708
709
  );
709
710
  }
710
- writeDoofpiTypes(root);
711
+ writeDoofpiTypes(root, config.doofpi.fetch);
711
712
  writeRouteTypes(root);
712
713
  writeI18nTypes(root, config.i18n);
713
714
  },
package/dist/cli.js CHANGED
@@ -72,10 +72,10 @@ async function main() {
72
72
  await loadEnvFiles(root, command === "dev" ? "development" : "production");
73
73
  const config = await loadConfig(root);
74
74
  if (command === "dev") {
75
- const { startDev } = await import("./dev-Y5IW7464.js");
75
+ const { startDev } = await import("./dev-CC3RXV3G.js");
76
76
  await startDev(root, config, overrides);
77
77
  } else if (command === "build") {
78
- const { buildApp } = await import("./build-GGRF4JTM.js");
78
+ const { buildApp } = await import("./build-25H52PWT.js");
79
79
  const preset = option(args, "--preset");
80
80
  await buildApp(
81
81
  root,
@@ -83,7 +83,7 @@ async function main() {
83
83
  preset ? assertBuildPreset(preset, "--preset") : void 0
84
84
  );
85
85
  } else {
86
- const { startProd } = await import("./start-IK3X6V3N.js");
86
+ const { startProd } = await import("./start-PARBW5KN.js");
87
87
  await startProd(root, config, overrides);
88
88
  }
89
89
  }
@@ -8,7 +8,7 @@ import {
8
8
  htmlTemplate,
9
9
  resolveCss,
10
10
  resolveFonts
11
- } from "./chunk-M7TMNIVH.js";
11
+ } from "./chunk-UF6YTIXD.js";
12
12
  import {
13
13
  isCloudflarePreset
14
14
  } from "./chunk-NX6BO7UB.js";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createNoofpiApp
3
- } from "../chunk-X34Y6ESE.js";
3
+ } from "../chunk-4YOOGEOF.js";
4
4
  import "../chunk-6OWKWAIW.js";
5
5
  import "../chunk-ELXCFUA4.js";
6
6
  import "../chunk-EZPJF4D4.js";
@@ -4,7 +4,7 @@ import {
4
4
  notFoundError,
5
5
  setServerStateSource,
6
6
  snapshotEntries
7
- } from "../chunk-X34Y6ESE.js";
7
+ } from "../chunk-4YOOGEOF.js";
8
8
  import "../chunk-6OWKWAIW.js";
9
9
  import "../chunk-ELXCFUA4.js";
10
10
  import {
@@ -99,15 +99,46 @@ interface RequestLog {
99
99
  /** doofpi's `Routes` type is not exported, but it is the constraint of `routes()` */
100
100
  type Routes = Parameters<Doofpi["routes"]>[0];
101
101
  /**
102
- * Filled in by the `.noofpi/doofpi.d.ts` that noofpi generates from `server/doofpi.ts` (its `routes` export), so
103
- * clients are typed without a generic. Augment it yourself to register routes from elsewhere.
102
+ * Registers the routes types of the doofpi clients, so they are typed without a generic. Augment it yourself:
103
+ * - `remoteRoutes`: the routes of the remote API, used by `dfetch`, `pfetch` and `sfetch`
104
+ * - `localRoutes`: the routes of `server/doofpi.ts`, used by `lfetch`. Filled in by the `.noofpi/doofpi.d.ts` that
105
+ * noofpi generates (its `routes` export), so you rarely set it
106
+ * - `routes`: used by every mode that has no `localRoutes` / `remoteRoutes`
107
+ * - `defaultFetch`: the `doofpi.fetch` mode of the config (also filled in by `.noofpi/doofpi.d.ts`). A call without
108
+ * `{ mode }` is typed for it, so it cannot see `routeRules[...].fetch`: pass `{ mode }` where a page differs
104
109
  */
105
110
  interface Register {
106
111
  }
107
- /** The routes registered through {@link Register}, `any` when there are none */
112
+ /** The `routes` registered through {@link Register}, `any` when there are none */
108
113
  type RegisteredRoutes = Register extends {
109
114
  routes: infer R extends Routes;
110
115
  } ? R : any;
116
+ type LocalOnly = Register extends {
117
+ localRoutes: infer R extends Routes;
118
+ } ? R : never;
119
+ type RemoteOnly = Register extends {
120
+ remoteRoutes: infer R extends Routes;
121
+ } ? R : never;
122
+ type OrRoutes<R> = [R] extends [never] ? RegisteredRoutes : R;
123
+ /** The `localRoutes` registered through {@link Register}, else {@link RegisteredRoutes} */
124
+ type RegisteredLocalRoutes = OrRoutes<LocalOnly>;
125
+ /** The `remoteRoutes` registered through {@link Register}, else {@link RegisteredRoutes} */
126
+ type RegisteredRemoteRoutes = OrRoutes<RemoteOnly>;
127
+ type DefaultFetch = Register extends {
128
+ defaultFetch: infer F extends FetchMode;
129
+ } ? F : undefined;
130
+ type REMOTE_MODES = "dfetch" | "pfetch" | "sfetch";
131
+ /** Without a default mode: `routes`, else the remote routes, else the local ones, else `any` */
132
+ type UnmodedRoutes = Register extends {
133
+ routes: infer R extends Routes;
134
+ } ? R : [RemoteOnly] extends [never] ? [LocalOnly] extends [never] ? any : LocalOnly : RemoteOnly;
135
+ /**
136
+ * The routes of a fetch mode: `lfetch` -> local, the other modes -> remote. `undefined` (or a mode that is not a single
137
+ * literal) is the `defaultFetch` mode of the config.
138
+ */
139
+ type RoutesForMode<M extends FetchMode | undefined = undefined> = [
140
+ M
141
+ ] extends ["lfetch"] ? RegisteredLocalRoutes : [M] extends [REMOTE_MODES] ? RegisteredRemoteRoutes : [DefaultFetch] extends ["lfetch"] ? RegisteredLocalRoutes : [DefaultFetch] extends [REMOTE_MODES] ? RegisteredRemoteRoutes : UnmodedRoutes;
111
142
  type DoofpiClient<R extends Routes = RegisteredRoutes> = ReturnType<typeof createClient<R>>;
112
143
  interface UseDoofpiOptions {
113
144
  /** Use this fetch mode instead of the one of the current page */
@@ -117,10 +148,13 @@ interface UseDoofpiOptions {
117
148
  * The doofpi client for the `doofpi.fetch` mode that applies to the current page (`routeRules[...].fetch` or the global
118
149
  * one), or for an explicit `{ mode }` (e.g. `useDoofpi({ mode: "sfetch" })`, which must be enabled in the config).
119
150
  * Call it inside `setup()` (or `app.runWithContext`); layouts and `app.vue` are set up once, so they keep the
120
- * client of the first page. Typed by the `routes` export of `server/doofpi.ts`; pass a routes type to override it:
121
- * `useDoofpi<OtherRoutes>()`.
151
+ * client of the first page. Typed by the `Register` routes of the mode (`localRoutes` for `lfetch`, `remoteRoutes` for
152
+ * the others, see {@link Register}); pass a routes type to override it: `useDoofpi<OtherRoutes>()`.
122
153
  */
123
- declare function useDoofpi<R extends Routes = RegisteredRoutes>(options?: UseDoofpiOptions): DoofpiClient<R>;
154
+ declare function useDoofpi<M extends FetchMode | undefined = undefined>(options?: UseDoofpiOptions & {
155
+ mode?: M;
156
+ }): DoofpiClient<RoutesForMode<M>>;
157
+ declare function useDoofpi<R extends Routes>(options?: UseDoofpiOptions): DoofpiClient<R>;
124
158
  interface DoofpiClientConfig {
125
159
  fetch: FetchMode;
126
160
  url: string | undefined;
@@ -294,12 +328,16 @@ declare function useCookie<T>(cookie: CookieDefinition<T>, options?: UseCookieOp
294
328
 
295
329
  /**
296
330
  * `useAsyncData` with the doofpi client handed to the handler: fetched on the server, transferred to the browser,
297
- * not fetched again while hydrating. The client is typed by the `routes` export of `server/doofpi.ts`:
298
- * `useDoofpiFetch("hello", (client) => client.hello.read())`.
331
+ * not fetched again while hydrating. The client is typed by the `Register` routes of its mode (`localRoutes` for
332
+ * `lfetch`, `remoteRoutes` for the others): `useDoofpiFetch("hello", (client) => client.hello.read())`.
299
333
  * `key` must be unique per piece of data. `options.mode` picks the fetch mode like `useDoofpi({ mode })`.
334
+ * To type the client with other routes, annotate it: `(client: DoofpiClient<OtherRoutes>) => ...`.
300
335
  * Call inside `setup()`.
301
336
  */
302
- declare function useDoofpiFetch<T, R extends Routes = RegisteredRoutes>(key: string, handler: (client: DoofpiClient<R>) => Promise<T>, options?: UseAsyncDataOptions & {
337
+ declare function useDoofpiFetch<T, M extends FetchMode | undefined = undefined>(key: string, handler: (client: DoofpiClient<RoutesForMode<M>>) => Promise<T>, options?: UseAsyncDataOptions & {
338
+ mode?: M;
339
+ }): AsyncData<T>;
340
+ declare function useDoofpiFetch<T, R extends Routes>(key: string, handler: (client: DoofpiClient<R>) => Promise<T>, options?: UseAsyncDataOptions & {
303
341
  mode?: FetchMode;
304
342
  }): AsyncData<T>;
305
343
 
@@ -520,4 +558,4 @@ declare function navigateTo<T extends RouteLocationRaw>(to: T extends keyof Regi
520
558
  }): Promise<NavigationFailure | void | undefined>;
521
559
 
522
560
  export { AsyncDataError, ClientOnly, CookieCodec, CookieDefinition, CookieJar, CookieOptions, CreateErrorInput, FormErrors, FormSchema, HeadInput, ImageSource, MessageParamValue, Messages, NoofpiError, NoofpiErrorData, NoofpiImage, NoofpiLayout, NoofpiLink, NoofpiPage, RequestEntry, clearError, createDoofpiClient, createNoofpiApp, createRoutes, defineMessages, defineNoofpiPlugin, definePage, defineRouteMiddleware, filePathToRoute, navigateTo, showError, useAsyncData, useCookie, useCookies, useDoofpi, useDoofpiFetch, useDoofpiForm, useError, useHead, useI18n, useLayout, useLoaderData, useMiddleware, useState };
523
- export type { AsyncData, DoofpiClient, DoofpiClientConfig, DoofpiClientContext, DoofpiForm, I18n, LoaderContext, LoaderData, Locale, LocaleMessages, MessageArgs, MessageKey, NavigateToOptions, NoofpiPlugin, NoofpiPluginContext, PageDefinition, PageLoader, Register, RegisteredMessages, RegisteredPages, RegisteredRoutes, RouteMiddleware, RouteParamValue, SubmitResult, TranslateFunction, UseAsyncDataOptions, UseCookieCodecOptions, UseCookieOptions, UseDoofpiFormOptions, UseDoofpiOptions };
561
+ export type { AsyncData, DoofpiClient, DoofpiClientConfig, DoofpiClientContext, DoofpiForm, I18n, LoaderContext, LoaderData, Locale, LocaleMessages, MessageArgs, MessageKey, NavigateToOptions, NoofpiPlugin, NoofpiPluginContext, PageDefinition, PageLoader, Register, RegisteredLocalRoutes, RegisteredMessages, RegisteredPages, RegisteredRemoteRoutes, RegisteredRoutes, RouteMiddleware, RouteParamValue, RoutesForMode, SubmitResult, TranslateFunction, UseAsyncDataOptions, UseCookieCodecOptions, UseCookieOptions, UseDoofpiFormOptions, UseDoofpiOptions };
@@ -16,7 +16,7 @@ import {
16
16
  useI18n,
17
17
  useLoaderData,
18
18
  useState
19
- } from "../chunk-X34Y6ESE.js";
19
+ } from "../chunk-4YOOGEOF.js";
20
20
  import {
21
21
  defineRouteMiddleware,
22
22
  useMiddleware
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-WX2V6EYH.js";
4
4
  import {
5
5
  OUTPUT_DIR
6
- } from "./chunk-M7TMNIVH.js";
6
+ } from "./chunk-UF6YTIXD.js";
7
7
  import "./chunk-NX6BO7UB.js";
8
8
  import {
9
9
  createHandler
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "noofpi",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "A minimal Vue + Vite full-stack framework built around doofpi",
5
5
  "license": "MIT",
6
6
  "author": "Lior Cohen",