@tylix/client 2.1.1 → 2.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tylix/client",
3
- "version": "2.1.1",
3
+ "version": "2.2.1",
4
4
  "description": "Browser client for Tylix apps: API requests, the auth session, live channels and the typed route client, with no framework dependency.",
5
5
  "license": "MIT",
6
6
  "author": "Mbiyrukzz",
@@ -15,14 +15,14 @@
15
15
  "typescript",
16
16
  "routes"
17
17
  ],
18
- "homepage": "https://github.com/Mbiyrukzz/tylix/tree/main/packages/client#readme",
18
+ "homepage": "https://github.com/tylixjs/tylix/tree/main/packages/client#readme",
19
19
  "repository": {
20
20
  "type": "git",
21
- "url": "git+https://github.com/Mbiyrukzz/tylix.git",
21
+ "url": "git+https://github.com/tylixjs/tylix.git",
22
22
  "directory": "packages/client"
23
23
  },
24
24
  "bugs": {
25
- "url": "https://github.com/Mbiyrukzz/tylix/issues"
25
+ "url": "https://github.com/tylixjs/tylix/issues"
26
26
  },
27
27
  "engines": {
28
28
  "node": ">=22.13"
@@ -0,0 +1,47 @@
1
+ // Error pages for hosted frontends (React, Vue, Svelte, Angular).
2
+ //
3
+ // When the server answers a browser navigation with an error status
4
+ // (abort(403), maintenance, a crash in production), it still sends the
5
+ // app's index.html, with the error in window.__TYLIX_ERROR__:
6
+ // { status, code, title, message, requestId, path }
7
+ // The app then shows its page from pages/errors/, picked the same way as
8
+ // app/pages/errors/ in .tyx apps: <status> first, then 4xx / 5xx.
9
+
10
+ /**
11
+ * The error the server sent with this page, or null. It belongs to the URL
12
+ * it was sent for: once the app navigates elsewhere, this is null again.
13
+ */
14
+ export function httpError(location = globalThis.location) {
15
+ const error = globalThis.__TYLIX_ERROR__
16
+ if (!error || typeof error !== 'object') return null
17
+ if (location && typeof error.path === 'string' && location.pathname !== error.path) return null
18
+ return error
19
+ }
20
+
21
+ /**
22
+ * Picks the module for `status` from a map of error page modules, as
23
+ * returned by Vite's import.meta.glob('./pages/errors/*.vue', { eager: true }):
24
+ * "<status>.<ext>", then "<N>xx.<ext>". Returns the module's default export
25
+ * (or the module itself), or null when there is none.
26
+ */
27
+ export function pickErrorPage(modules, status) {
28
+ const byName = new Map()
29
+ for (const [file, mod] of Object.entries(modules ?? {})) {
30
+ const name = file.split('/').pop().replace(/\.[^.]+$/, '').toLowerCase()
31
+ byName.set(name, mod && typeof mod === 'object' && 'default' in mod ? mod.default : mod)
32
+ }
33
+ const code = String(status)
34
+ return byName.get(code) ?? byName.get(`${code[0]}xx`) ?? null
35
+ }
36
+
37
+ /** A not-found error for client-side routes that match nothing. */
38
+ export function notFoundError(path = globalThis.location?.pathname ?? '/') {
39
+ return {
40
+ status: 404,
41
+ code: '404',
42
+ title: 'Page not found',
43
+ message: "The page you're looking for doesn't exist or has been moved.",
44
+ requestId: '',
45
+ path,
46
+ }
47
+ }
package/src/index.d.ts CHANGED
@@ -128,6 +128,7 @@ declare global {
128
128
  // Set by the server when it already knows the session at page load.
129
129
  // eslint-disable-next-line no-var
130
130
  var __TYLIX_SESSION__: { user: TylixUser | null } | undefined
131
+ var __TYLIX_ERROR__: HttpErrorInfo | undefined
131
132
  // Set when the server rendered the page (SSR): hydrate instead of render.
132
133
  // eslint-disable-next-line no-var
133
134
  var __TYLIX_SSR__: boolean | undefined
@@ -144,3 +145,25 @@ export type RouteTable = { [name: string]: [method: string, pattern: string] | R
144
145
  export function createRouteClient(api: Api, table: RouteTable): any
145
146
  /** "/api/posts/:id" + { id: 7 } + { page: 2 } -> "/api/posts/7?page=2". Throws when a param is missing. */
146
147
  export function buildPath(pattern: string, params?: Record<string, string | number>, query?: Record<string, unknown>): string
148
+
149
+ /** An error status the server chose for a browser navigation. */
150
+ export interface HttpErrorInfo {
151
+ status: number
152
+ code: string
153
+ title: string
154
+ message: string
155
+ requestId: string
156
+ path: string
157
+ }
158
+
159
+ /** The error the server sent with this page, or null (also after navigating away). */
160
+ export declare function httpError(location?: { pathname: string }): HttpErrorInfo | null
161
+
162
+ /**
163
+ * The page for `status` from import.meta.glob('./pages/errors/*', { eager: true }):
164
+ * "<status>" first, then "4xx" / "5xx". null when there is none.
165
+ */
166
+ export declare function pickErrorPage<T = unknown>(modules: Record<string, unknown>, status: number): T | null
167
+
168
+ /** A 404 error for client-side routes that match nothing. */
169
+ export declare function notFoundError(path?: string): HttpErrorInfo
package/src/index.js CHANGED
@@ -2,6 +2,7 @@ import { createApi, resultFromResponse } from './createApi.js'
2
2
  import { createAuth } from './createAuth.js'
3
3
  export { channel, lazyChannel } from './channel.js'
4
4
  export { createRouteClient, buildPath } from './routeClient.js'
5
+ export { httpError, pickErrorPage, notFoundError } from './httpError.js'
5
6
 
6
7
  export { createApi, createAuth, resultFromResponse }
7
8