zitejs 0.10.0-start.2 → 0.10.0-start.21
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/dist/cjs/auth/actions.d.ts +28 -0
- package/dist/cjs/auth/actions.js +93 -0
- package/dist/cjs/auth/client.d.ts +357 -0
- package/dist/cjs/auth/client.js +66 -0
- package/dist/cjs/auth/index.d.ts +2 -249
- package/dist/cjs/auth/index.js +11 -108
- package/dist/cjs/auth/index.test.js +48 -0
- package/dist/cjs/auth/previewClaim.d.ts +7 -0
- package/dist/cjs/auth/previewClaim.js +16 -0
- package/dist/cjs/auth/profileUpdates.d.ts +9 -0
- package/dist/cjs/auth/profileUpdates.js +11 -0
- package/dist/cjs/backend/index.d.ts +121 -3
- package/dist/cjs/backend/index.test.js +58 -0
- package/dist/cjs/bundle/index.d.ts +8 -0
- package/dist/cjs/bundle/index.js +9 -8
- package/dist/cjs/check/index.d.ts +45 -1
- package/dist/cjs/check/index.js +198 -109
- package/dist/cjs/check/start/problem.d.ts +11 -0
- package/dist/cjs/check/start/problem.js +5 -0
- package/dist/cjs/check/start/routeTree.d.ts +9 -0
- package/dist/cjs/check/start/routeTree.js +111 -0
- package/dist/cjs/check/start/sourceRules.d.ts +8 -0
- package/dist/cjs/check/start/sourceRules.js +1159 -0
- package/dist/cjs/check/start/tsgo.d.ts +2 -0
- package/dist/cjs/check/start/tsgo.js +35 -0
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/client/index.d.ts +24 -0
- package/dist/cjs/dev/index.js +14 -4
- package/dist/cjs/integrations/rules.d.ts +13 -0
- package/dist/cjs/integrations/rules.js +31 -0
- package/dist/cjs/integrations/rules.test.js +1 -0
- package/dist/cjs/internal/appMode.d.ts +3 -0
- package/dist/cjs/internal/appMode.js +15 -0
- package/dist/cjs/runtime/index.d.ts +64 -25
- package/dist/cjs/runtime/sendEmailParamsTypes.test-d.d.ts +1 -0
- package/dist/cjs/runtime/sendEmailParamsTypes.test-d.js +33 -0
- package/dist/cjs/start/entries.d.ts +10 -4
- package/dist/cjs/start/entries.js +23 -6
- package/dist/cjs/steps/index.d.ts +78 -0
- package/dist/cjs/steps/index.js +507 -0
- package/dist/cjs/steps/index.test.d.ts +1 -0
- package/dist/cjs/steps/index.test.js +635 -0
- package/dist/cjs/sync/index.js +2 -1
- package/dist/cjs/sync/lib.d.ts +10 -3
- package/dist/cjs/sync/lib.js +44 -25
- package/dist/cjs/sync/lib.test.js +116 -36
- package/dist/cjs/types/fields.d.ts +8 -1
- package/dist/cjs/types/fields.js +7 -1
- package/dist/cjs/types/tables.d.ts +16 -0
- package/dist/cjs/upload/index.js +12 -2
- package/dist/cjs/upload/index.test.js +39 -0
- package/dist/cjs/vite/index.js +8 -3
- package/dist/cjs/vite/index.test.js +10 -2
- package/dist/esm/auth/actions.d.ts +28 -0
- package/dist/esm/auth/actions.js +55 -0
- package/dist/esm/auth/client.d.ts +357 -0
- package/dist/esm/auth/client.js +62 -0
- package/dist/esm/auth/index.d.ts +2 -249
- package/dist/esm/auth/index.js +2 -103
- package/dist/esm/auth/index.test.js +48 -0
- package/dist/esm/auth/previewClaim.d.ts +7 -0
- package/dist/esm/auth/previewClaim.js +12 -0
- package/dist/esm/auth/profileUpdates.d.ts +9 -0
- package/dist/esm/auth/profileUpdates.js +6 -0
- package/dist/esm/backend/index.d.ts +121 -3
- package/dist/esm/backend/index.test.js +58 -0
- package/dist/esm/bundle/index.d.ts +8 -0
- package/dist/esm/bundle/index.js +8 -8
- package/dist/esm/check/index.d.ts +45 -1
- package/dist/esm/check/index.js +198 -113
- package/dist/esm/check/start/problem.d.ts +11 -0
- package/dist/esm/check/start/problem.js +1 -0
- package/dist/esm/check/start/routeTree.d.ts +9 -0
- package/dist/esm/check/start/routeTree.js +70 -0
- package/dist/esm/check/start/sourceRules.d.ts +8 -0
- package/dist/esm/check/start/sourceRules.js +1156 -0
- package/dist/esm/check/start/tsgo.d.ts +2 -0
- package/dist/esm/check/start/tsgo.js +29 -0
- package/dist/esm/cli.js +1 -1
- package/dist/esm/client/index.d.ts +24 -0
- package/dist/esm/dev/index.js +16 -6
- package/dist/esm/integrations/rules.d.ts +13 -0
- package/dist/esm/integrations/rules.js +31 -0
- package/dist/esm/integrations/rules.test.js +1 -0
- package/dist/esm/internal/appMode.d.ts +3 -0
- package/dist/esm/internal/appMode.js +10 -0
- package/dist/esm/runtime/index.d.ts +64 -25
- package/dist/esm/runtime/sendEmailParamsTypes.test-d.d.ts +1 -0
- package/dist/esm/runtime/sendEmailParamsTypes.test-d.js +31 -0
- package/dist/esm/start/aiUsage.d.ts +23 -0
- package/dist/esm/start/aiUsage.js +146 -0
- package/dist/esm/start/airtable/arrayFields.d.ts +10 -0
- package/dist/esm/start/airtable/arrayFields.js +30 -0
- package/dist/esm/start/airtable/dispatch.d.ts +54 -0
- package/dist/esm/start/airtable/dispatch.js +483 -0
- package/dist/esm/start/airtable/formulas.d.ts +70 -0
- package/dist/esm/start/airtable/formulas.js +260 -0
- package/dist/esm/start/airtable/index.d.ts +8 -0
- package/dist/esm/start/airtable/index.js +54 -0
- package/dist/esm/start/authHooks.d.ts +7 -0
- package/dist/esm/start/authHooks.js +10 -0
- package/dist/esm/start/authHooksModule.d.ts +1 -0
- package/dist/esm/start/authHooksModule.js +3 -0
- package/dist/esm/start/clientErrorMessage.test.d.ts +1 -0
- package/dist/esm/start/clientErrorMessage.test.js +105 -0
- package/dist/esm/start/credentials.d.ts +2 -0
- package/dist/esm/start/credentials.js +2 -0
- package/dist/esm/start/data/baseRunner.d.ts +92 -0
- package/dist/esm/start/data/baseRunner.js +83 -0
- package/dist/esm/start/data/dispatch.d.ts +16 -0
- package/dist/esm/start/data/dispatch.js +198 -0
- package/dist/esm/start/data/index.d.ts +15 -0
- package/dist/esm/start/data/index.js +58 -0
- package/dist/esm/start/data/sqlNameMappings.d.ts +58 -0
- package/dist/esm/start/data/sqlNameMappings.js +92 -0
- package/dist/esm/start/data/transformations.d.ts +97 -0
- package/dist/esm/start/data/transformations.js +262 -0
- package/dist/esm/start/data/types.d.ts +91 -0
- package/dist/esm/start/data/types.js +14 -0
- package/dist/esm/start/describe.d.ts +42 -5
- package/dist/esm/start/describe.js +77 -23
- package/dist/esm/start/entries.d.ts +10 -4
- package/dist/esm/start/entries.js +21 -5
- package/dist/esm/start/errors.d.ts +63 -0
- package/dist/esm/start/errors.js +146 -0
- package/dist/esm/start/errors.test.d.ts +1 -0
- package/dist/esm/start/errors.test.js +23 -0
- package/dist/esm/start/factories.test-d.d.ts +2 -0
- package/dist/esm/start/factories.test-d.js +20 -0
- package/dist/esm/start/host.d.ts +2 -2
- package/dist/esm/start/index.d.ts +32 -27
- package/dist/esm/start/index.js +79 -47
- package/dist/esm/start/instance.d.ts +4 -4
- package/dist/esm/start/instance.js +54 -11
- package/dist/esm/start/outbound.d.ts +11 -0
- package/dist/esm/start/outbound.js +400 -0
- package/dist/esm/start/previewClaim.d.ts +7 -0
- package/dist/esm/start/previewClaim.js +23 -0
- package/dist/esm/start/problems.js +1 -1
- package/dist/esm/start/quiet.d.ts +2 -0
- package/dist/esm/start/quiet.js +27 -0
- package/dist/esm/start/redactSecrets.d.ts +15 -0
- package/dist/esm/start/redactSecrets.js +154 -0
- package/dist/esm/start/redactSecrets.test.d.ts +1 -0
- package/dist/esm/start/redactSecrets.test.js +151 -0
- package/dist/esm/start/registration.d.ts +13 -0
- package/dist/esm/start/registration.js +21 -0
- package/dist/esm/start/registrationHash.d.ts +22 -0
- package/dist/esm/start/registrationHash.js +470 -0
- package/dist/esm/start/router.d.ts +25 -4
- package/dist/esm/start/router.js +169 -11
- package/dist/esm/start/runLog.d.ts +7 -4
- package/dist/esm/start/runLog.js +64 -6
- package/dist/esm/start/scope.d.ts +4 -4
- package/dist/esm/start/server.d.ts +40 -36
- package/dist/esm/start/server.js +322 -49
- package/dist/esm/start/steps.d.ts +28 -0
- package/dist/esm/start/steps.js +97 -0
- package/dist/esm/start/types.d.ts +179 -6
- package/dist/esm/start/version.d.ts +1 -0
- package/dist/esm/start/version.js +2 -0
- package/dist/esm/start/vite.js +91 -14
- package/dist/esm/start/worker.d.ts +6 -0
- package/dist/esm/start/worker.js +12 -0
- package/dist/esm/start/workflow.d.ts +46 -12
- package/dist/esm/start/workflow.js +10 -3
- package/dist/esm/start/workflow.test-d.js +50 -8
- package/dist/esm/start/workflowModules.js +1 -1
- package/dist/esm/start/workflowRun.d.ts +26 -8
- package/dist/esm/start/workflowRun.js +39 -8
- package/dist/esm/steps/index.d.ts +78 -0
- package/dist/esm/steps/index.js +502 -0
- package/dist/esm/steps/index.test.d.ts +1 -0
- package/dist/esm/steps/index.test.js +633 -0
- package/dist/esm/sync/index.js +2 -1
- package/dist/esm/sync/lib.d.ts +10 -3
- package/dist/esm/sync/lib.js +44 -25
- package/dist/esm/sync/lib.test.js +116 -36
- package/dist/esm/types/fields.d.ts +8 -1
- package/dist/esm/types/fields.js +6 -0
- package/dist/esm/types/tables.d.ts +16 -0
- package/dist/esm/upload/index.js +12 -2
- package/dist/esm/upload/index.test.js +39 -0
- package/dist/esm/vite/index.js +8 -3
- package/dist/esm/vite/index.test.js +10 -2
- package/package.json +37 -16
- package/dist/cjs/start/problems.d.ts +0 -19
- package/dist/cjs/start/problems.js +0 -11
|
@@ -1,15 +1,30 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { dirname, join, relative } from "node:path";
|
|
3
|
+
import { listSourceDirs } from "../sourceRoots.js";
|
|
3
4
|
export const START_SERVER_ENTRY = ".zite/server.ts";
|
|
4
5
|
export const START_INSTANCE_ENTRY = ".zite/start.ts";
|
|
5
6
|
const APP_START_FILES = ["src/start.ts", "src/start.tsx"];
|
|
6
|
-
/**
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The server entry every Start app builds with. Platform-owned: regenerated,
|
|
9
|
+
* never edited. On Workers it's an entrypoint, so the host can hand each fetch
|
|
10
|
+
* its props; on Node, a plain fetch handler.
|
|
11
|
+
*/
|
|
12
|
+
export const renderStartServerEntry = (target = "cloudflare") => target === "cloudflare"
|
|
13
|
+
? `// Generated by zitejs for TanStack Start apps. The platform owns this file.
|
|
14
|
+
import handler from '@tanstack/react-start/server-entry';
|
|
15
|
+
import { createZiteWorker } from 'zitejs/start/worker';
|
|
16
|
+
import { getRouter } from '../src/router';
|
|
17
|
+
import { startInstance } from './start';
|
|
18
|
+
|
|
19
|
+
export default createZiteWorker({ handler, getRouter, startInstance });
|
|
20
|
+
`
|
|
21
|
+
: `// Generated by zitejs for TanStack Start apps. The platform owns this file.
|
|
8
22
|
import handler, { createServerEntry } from '@tanstack/react-start/server-entry';
|
|
9
23
|
import { createZiteFetch } from 'zitejs/start/server';
|
|
10
24
|
import { getRouter } from '../src/router';
|
|
25
|
+
import { startInstance } from './start';
|
|
11
26
|
|
|
12
|
-
export default createServerEntry({ fetch: createZiteFetch({ handler, getRouter }) });
|
|
27
|
+
export default createServerEntry({ fetch: createZiteFetch({ handler, getRouter, startInstance }) });
|
|
13
28
|
`;
|
|
14
29
|
/** The start instance: the platform's middleware, then the app's own `src/start.ts` when it has one. */
|
|
15
30
|
export const renderStartInstance = (hasAppStart) => hasAppStart
|
|
@@ -43,6 +58,7 @@ export const isStartAppDir = (appPath) => {
|
|
|
43
58
|
}
|
|
44
59
|
};
|
|
45
60
|
const isStartApp = (app) => isStartAppDir(app.path);
|
|
61
|
+
export const workspaceHasStartApps = () => listSourceDirs().some(isStartApp);
|
|
46
62
|
export function regenerateStartEntries(app) {
|
|
47
63
|
if (!isStartApp(app))
|
|
48
64
|
return;
|
|
@@ -57,7 +73,7 @@ export function regenerateStartEntries(app) {
|
|
|
57
73
|
}
|
|
58
74
|
const SOURCE_FILE = /\.[mc]?[jt]sx?$/;
|
|
59
75
|
const NOT_APP_CODE = /(\.(test|spec)\.[^.]+|\.d\.ts|routeTree\.gen\.ts)$/;
|
|
60
|
-
/** App modules that mention
|
|
76
|
+
/** App modules that mention createAutomation: what the workflow registry imports. Describe decides what each one really exports. */
|
|
61
77
|
export function workflowFiles(appDir) {
|
|
62
78
|
const found = [];
|
|
63
79
|
const walk = (dir) => {
|
|
@@ -67,7 +83,7 @@ export function workflowFiles(appDir) {
|
|
|
67
83
|
if (entry.name !== "node_modules")
|
|
68
84
|
walk(full);
|
|
69
85
|
}
|
|
70
|
-
else if (SOURCE_FILE.test(entry.name) && !NOT_APP_CODE.test(entry.name) && readFileSync(full, "utf-8").includes("
|
|
86
|
+
else if (SOURCE_FILE.test(entry.name) && !NOT_APP_CODE.test(entry.name) && readFileSync(full, "utf-8").includes("createAutomation")) {
|
|
71
87
|
found.push(relative(appDir, full).split("\\").join("/"));
|
|
72
88
|
}
|
|
73
89
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { HostMode } from "./types.js";
|
|
2
|
+
declare const STATUS: {
|
|
3
|
+
readonly BAD_REQUEST: 400;
|
|
4
|
+
readonly INVALID_INPUT: 400;
|
|
5
|
+
readonly UNAUTHORIZED: 401;
|
|
6
|
+
readonly FORBIDDEN: 403;
|
|
7
|
+
readonly NOT_FOUND: 404;
|
|
8
|
+
readonly CONFLICT: 409;
|
|
9
|
+
readonly RATE_LIMITED: 429;
|
|
10
|
+
readonly INTERNAL_ERROR: 500;
|
|
11
|
+
};
|
|
12
|
+
export type ZiteErrorCode = keyof typeof STATUS;
|
|
13
|
+
/** @internal The HTTP status each code answers with. */
|
|
14
|
+
export declare const ZITE_ERROR_STATUS: Readonly<Record<ZiteErrorCode, number>>;
|
|
15
|
+
export declare const isZiteErrorCode: (code: unknown) => code is ZiteErrorCode;
|
|
16
|
+
export type ZiteErrorOptions = {
|
|
17
|
+
code: ZiteErrorCode;
|
|
18
|
+
message: string;
|
|
19
|
+
userFacingMessage?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Fail with a status: `throw new ZiteError('FORBIDDEN', 'Only the owner can edit this')`. On the live app,
|
|
23
|
+
* `userFacingMessage` is what users read in place of `message`, which stays for logs and the editor.
|
|
24
|
+
*/
|
|
25
|
+
export declare class ZiteError extends Error {
|
|
26
|
+
readonly code: ZiteErrorCode;
|
|
27
|
+
readonly userFacingMessage?: string;
|
|
28
|
+
constructor(code: ZiteErrorCode, message?: string);
|
|
29
|
+
constructor(options: ZiteErrorOptions);
|
|
30
|
+
/** Classic's order, which code written for `zitejs/backend` uses. */
|
|
31
|
+
constructor(message: string, code?: ZiteErrorCode);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A ZiteError from any copy of zitejs as this one's. Integration SDKs and code written for classic throw
|
|
35
|
+
* `zitejs/backend`'s class, which `instanceof` doesn't recognise.
|
|
36
|
+
*/
|
|
37
|
+
export declare const asZiteError: (error: unknown) => ZiteError | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* What a failure may say to the app's users, as on classic. The message is the app author's, so it passes, with
|
|
40
|
+
* credentials redacted in place; one naming our infrastructure or billing is replaced whole. Preview is the editor,
|
|
41
|
+
* which reads it unchanged.
|
|
42
|
+
*/
|
|
43
|
+
export declare function toClientErrorMessage(errorMessage: string, mode: HostMode | undefined, userFacingMessage?: string): string;
|
|
44
|
+
/** The message a server function fails with when it needs a session; the platform's client middleware sends the user to sign in. */
|
|
45
|
+
export declare const SIGN_IN_REQUIRED = "ZITE_SIGN_IN_REQUIRED";
|
|
46
|
+
export declare const isSignInRequired: (error: unknown) => boolean;
|
|
47
|
+
/** @internal Signed out: the visitor's state, not a bug in the app's code. */
|
|
48
|
+
export declare const isSignInError: (error: unknown) => boolean;
|
|
49
|
+
/**
|
|
50
|
+
* @internal A thrown string or object as an INTERNAL_ERROR: only an Error goes through the zite-error adapter, which
|
|
51
|
+
* redacts it on its way to the browser. A Response, a redirect among them, and a notFound are TanStack's to handle.
|
|
52
|
+
*/
|
|
53
|
+
export declare const errorFromThrown: (value: unknown) => unknown;
|
|
54
|
+
/** @internal A result whose promises reject as `errorFromThrown` would throw: they cross to the browser as thrown. */
|
|
55
|
+
export declare const withThrownRejections: <T>(value: T, cross?: (error: unknown) => unknown) => T;
|
|
56
|
+
/** A thrown value as the browser receives it: its code, and only the message `toClientErrorMessage` allows. */
|
|
57
|
+
export declare const toClientError: (error: unknown) => ZiteError;
|
|
58
|
+
/**
|
|
59
|
+
* @internal An error as the browser would receive it, for a caller on the server that nothing serializes it for.
|
|
60
|
+
* Its message is already the one to show, so the adapter sends it as it is, and crossing it again changes nothing.
|
|
61
|
+
*/
|
|
62
|
+
export declare const asCrossed: (error: Error) => ZiteError;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { isNotFound } from "@tanstack/react-router";
|
|
2
|
+
import { currentHost } from "./host.js";
|
|
3
|
+
import { redactSecrets } from "./redactSecrets.js";
|
|
4
|
+
const STATUS = {
|
|
5
|
+
BAD_REQUEST: 400,
|
|
6
|
+
INVALID_INPUT: 400,
|
|
7
|
+
UNAUTHORIZED: 401,
|
|
8
|
+
FORBIDDEN: 403,
|
|
9
|
+
NOT_FOUND: 404,
|
|
10
|
+
CONFLICT: 409,
|
|
11
|
+
RATE_LIMITED: 429,
|
|
12
|
+
INTERNAL_ERROR: 500,
|
|
13
|
+
};
|
|
14
|
+
/** @internal The HTTP status each code answers with. */
|
|
15
|
+
export const ZITE_ERROR_STATUS = STATUS;
|
|
16
|
+
export const isZiteErrorCode = (code) => typeof code === "string" && Object.prototype.hasOwnProperty.call(STATUS, code);
|
|
17
|
+
/**
|
|
18
|
+
* Fail with a status: `throw new ZiteError('FORBIDDEN', 'Only the owner can edit this')`. On the live app,
|
|
19
|
+
* `userFacingMessage` is what users read in place of `message`, which stays for logs and the editor.
|
|
20
|
+
*/
|
|
21
|
+
export class ZiteError extends Error {
|
|
22
|
+
code;
|
|
23
|
+
userFacingMessage;
|
|
24
|
+
constructor(first, second) {
|
|
25
|
+
const options = typeof first === "object"
|
|
26
|
+
? first
|
|
27
|
+
: isZiteErrorCode(first)
|
|
28
|
+
? { code: first, message: second ?? first }
|
|
29
|
+
: { code: isZiteErrorCode(second) ? second : "INTERNAL_ERROR", message: first };
|
|
30
|
+
super(options.message);
|
|
31
|
+
this.name = "ZiteError";
|
|
32
|
+
this.code = options.code;
|
|
33
|
+
if (options.userFacingMessage)
|
|
34
|
+
this.userFacingMessage = options.userFacingMessage;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A ZiteError from any copy of zitejs as this one's. Integration SDKs and code written for classic throw
|
|
39
|
+
* `zitejs/backend`'s class, which `instanceof` doesn't recognise.
|
|
40
|
+
*/
|
|
41
|
+
export const asZiteError = (error) => {
|
|
42
|
+
if (error instanceof ZiteError)
|
|
43
|
+
return error;
|
|
44
|
+
if (!(error instanceof Error) || error.name !== "ZiteError")
|
|
45
|
+
return undefined;
|
|
46
|
+
const { code, userFacingMessage } = error;
|
|
47
|
+
if (!isZiteErrorCode(code))
|
|
48
|
+
return undefined;
|
|
49
|
+
return new ZiteError({
|
|
50
|
+
code,
|
|
51
|
+
message: error.message,
|
|
52
|
+
...(typeof userFacingMessage === "string" && { userFacingMessage }),
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
// Ported from restly's packages/endpoint-setup/src/endpointErrors.ts; keep the two in step. The fences are consumed
|
|
56
|
+
// characters rather than lookbehinds, which older Safari can't parse: this module ships to the browser.
|
|
57
|
+
const INTERNAL_SERVICE_NAMES = [
|
|
58
|
+
"zite-shard-restly-server",
|
|
59
|
+
"restly-server",
|
|
60
|
+
"workflow-runner",
|
|
61
|
+
"base-runner",
|
|
62
|
+
"app-runner",
|
|
63
|
+
"utility-service",
|
|
64
|
+
];
|
|
65
|
+
const PRIVATE_IPV4 = String.raw `\b(?:10|127)\.\d{1,3}\.\d{1,3}\.\d{1,3}\b|\b172\.(?:1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}\b|\b192\.168\.\d{1,3}\.\d{1,3}\b|\b169\.254\.\d{1,3}\.\d{1,3}\b`;
|
|
66
|
+
const PRIVATE_IPV6 = String.raw `(?:^|[^\w:])(?:::1|(?:f[cd][0-9a-f]{2}|fe[89ab][0-9a-f]):[0-9a-f:]+)(?![\w:])`;
|
|
67
|
+
const INTERNAL_HOSTNAME = String.raw `(?:^|[^\w.-])(?:${INTERNAL_SERVICE_NAMES.join("|")}|localhost)(?:-\w+)*(?::\d+)?(?![\w.-])`;
|
|
68
|
+
const UNAVAILABLE = "A service this app depends on is temporarily unavailable. Please try again.";
|
|
69
|
+
const AI_UNAVAILABLE = "AI features are temporarily unavailable. Please contact the app owner.";
|
|
70
|
+
const INFRASTRUCTURE_PATTERNS = [
|
|
71
|
+
{ pattern: new RegExp(`${PRIVATE_IPV4}|${PRIVATE_IPV6}|${INTERNAL_HOSTNAME}`, "i"), replacement: UNAVAILABLE },
|
|
72
|
+
{ pattern: /AI is paused: your organization is over its AI credit limit/i, replacement: AI_UNAVAILABLE },
|
|
73
|
+
{
|
|
74
|
+
pattern: /platform\.openai\.com\/(?:settings\/organization\/billing|docs\/guides\/error-codes)/i,
|
|
75
|
+
replacement: AI_UNAVAILABLE,
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
/**
|
|
79
|
+
* What a failure may say to the app's users, as on classic. The message is the app author's, so it passes, with
|
|
80
|
+
* credentials redacted in place; one naming our infrastructure or billing is replaced whole. Preview is the editor,
|
|
81
|
+
* which reads it unchanged.
|
|
82
|
+
*/
|
|
83
|
+
export function toClientErrorMessage(errorMessage, mode, userFacingMessage) {
|
|
84
|
+
if (mode === "preview")
|
|
85
|
+
return errorMessage;
|
|
86
|
+
if (userFacingMessage)
|
|
87
|
+
return userFacingMessage;
|
|
88
|
+
const redacted = redactSecrets(errorMessage);
|
|
89
|
+
for (const { pattern, replacement } of INFRASTRUCTURE_PATTERNS) {
|
|
90
|
+
if (pattern.test(redacted))
|
|
91
|
+
return replacement;
|
|
92
|
+
}
|
|
93
|
+
return redacted;
|
|
94
|
+
}
|
|
95
|
+
/** The message a server function fails with when it needs a session; the platform's client middleware sends the user to sign in. */
|
|
96
|
+
export const SIGN_IN_REQUIRED = "ZITE_SIGN_IN_REQUIRED";
|
|
97
|
+
export const isSignInRequired = (error) => error instanceof Error && error.message === SIGN_IN_REQUIRED;
|
|
98
|
+
/** @internal Signed out: the visitor's state, not a bug in the app's code. */
|
|
99
|
+
export const isSignInError = (error) => isSignInRequired(error) || asZiteError(error)?.code === "UNAUTHORIZED";
|
|
100
|
+
const thrownText = (value) => {
|
|
101
|
+
if (typeof value === "string")
|
|
102
|
+
return value;
|
|
103
|
+
try {
|
|
104
|
+
return JSON.stringify(value) ?? String(value);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return String(value);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* @internal A thrown string or object as an INTERNAL_ERROR: only an Error goes through the zite-error adapter, which
|
|
112
|
+
* redacts it on its way to the browser. A Response, a redirect among them, and a notFound are TanStack's to handle.
|
|
113
|
+
*/
|
|
114
|
+
export const errorFromThrown = (value) => value instanceof Error || value instanceof Response || isNotFound(value) ? value : new ZiteError("INTERNAL_ERROR", thrownText(value));
|
|
115
|
+
/** @internal A result whose promises reject as `errorFromThrown` would throw: they cross to the browser as thrown. */
|
|
116
|
+
export const withThrownRejections = (value, cross = (error) => error) => {
|
|
117
|
+
if (!value || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype)
|
|
118
|
+
return value;
|
|
119
|
+
const promised = Object.entries(value).filter(([, item]) => item instanceof Promise);
|
|
120
|
+
if (!promised.length)
|
|
121
|
+
return value;
|
|
122
|
+
const out = { ...value };
|
|
123
|
+
for (const [key, item] of promised) {
|
|
124
|
+
out[key] = item.catch((error) => {
|
|
125
|
+
throw cross(errorFromThrown(error));
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
};
|
|
130
|
+
/** A thrown value as the browser receives it: its code, and only the message `toClientErrorMessage` allows. */
|
|
131
|
+
export const toClientError = (error) => {
|
|
132
|
+
const zite = asZiteError(error);
|
|
133
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
134
|
+
return new ZiteError(zite?.code ?? "INTERNAL_ERROR", toClientErrorMessage(message, currentHost()?.mode, zite?.userFacingMessage));
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* @internal An error as the browser would receive it, for a caller on the server that nothing serializes it for.
|
|
138
|
+
* Its message is already the one to show, so the adapter sends it as it is, and crossing it again changes nothing.
|
|
139
|
+
*/
|
|
140
|
+
export const asCrossed = (error) => {
|
|
141
|
+
const { code, message } = toClientError(error);
|
|
142
|
+
const crossed = new ZiteError({ code, message, userFacingMessage: message });
|
|
143
|
+
if (currentHost()?.mode === "preview")
|
|
144
|
+
crossed.stack = error.stack;
|
|
145
|
+
return crossed;
|
|
146
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { asZiteError, ZiteError } from "./errors.js";
|
|
3
|
+
describe("ZiteError", () => {
|
|
4
|
+
it("takes Start's order, classic's order and classic's options", () => {
|
|
5
|
+
expect(new ZiteError("FORBIDDEN", "Owners only")).toMatchObject({ code: "FORBIDDEN", message: "Owners only" });
|
|
6
|
+
expect(new ZiteError("NOT_FOUND")).toMatchObject({ code: "NOT_FOUND", message: "NOT_FOUND" });
|
|
7
|
+
expect(new ZiteError("Owners only", "FORBIDDEN")).toMatchObject({ code: "FORBIDDEN", message: "Owners only" });
|
|
8
|
+
expect(new ZiteError("Something broke")).toMatchObject({ code: "INTERNAL_ERROR", message: "Something broke" });
|
|
9
|
+
expect(new ZiteError({ code: "CONFLICT", message: "Row 3 changed", userFacingMessage: "Someone else edited this." }))
|
|
10
|
+
.toMatchObject({ code: "CONFLICT", message: "Row 3 changed", userFacingMessage: "Someone else edited this." });
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
describe("asZiteError", () => {
|
|
14
|
+
it("recognises another copy's ZiteError by name and code", () => {
|
|
15
|
+
const classic = Object.assign(new Error("No such deal"), { name: "ZiteError", code: "NOT_FOUND", userFacingMessage: "Gone." });
|
|
16
|
+
expect(asZiteError(classic)).toMatchObject({ code: "NOT_FOUND", message: "No such deal", userFacingMessage: "Gone." });
|
|
17
|
+
});
|
|
18
|
+
it("refuses anything else", () => {
|
|
19
|
+
expect(asZiteError(new Error("plain"))).toBeUndefined();
|
|
20
|
+
expect(asZiteError(Object.assign(new Error("x"), { name: "ZiteError", code: "toString" }))).toBeUndefined();
|
|
21
|
+
expect(asZiteError({ name: "ZiteError", code: "NOT_FOUND", message: "not an Error" })).toBeUndefined();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { publicFn, role, signedInFn } from "./index.js";
|
|
3
|
+
describe("server function factories", () => {
|
|
4
|
+
it("take a method and a description", () => {
|
|
5
|
+
signedInFn({ method: "GET", description: "The signed-in user's orders" }).handler(async () => []);
|
|
6
|
+
publicFn({ method: "POST", description: "Joins the waitlist" }).handler(async () => null);
|
|
7
|
+
});
|
|
8
|
+
it("refuse options without a description", () => {
|
|
9
|
+
// @ts-expect-error the editor shows what each function does
|
|
10
|
+
signedInFn({ method: "GET" });
|
|
11
|
+
// @ts-expect-error the editor shows what each function does
|
|
12
|
+
publicFn({ method: "POST" });
|
|
13
|
+
});
|
|
14
|
+
it("carry the requirement through an app's own factory", () => {
|
|
15
|
+
const managerFn = signedInFn.middleware([role("manager")]);
|
|
16
|
+
managerFn({ method: "POST", description: "Reassigns a deal" }).handler(async () => null);
|
|
17
|
+
// @ts-expect-error the editor shows what each function does
|
|
18
|
+
managerFn({ method: "POST" });
|
|
19
|
+
});
|
|
20
|
+
});
|
package/dist/esm/start/host.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HostRequestState } from "./types.js";
|
|
2
2
|
export declare const HOST_STORE_KEY: unique symbol;
|
|
3
|
-
export declare const currentHost: () =>
|
|
3
|
+
export declare const currentHost: () => HostRequestState | undefined;
|
|
@@ -1,34 +1,24 @@
|
|
|
1
1
|
import type { User } from "./types.js";
|
|
2
|
-
export type {
|
|
2
|
+
export type { Role, User } from "./types.js";
|
|
3
|
+
export { isSignInRequired, SIGN_IN_REQUIRED, ZiteError, ZITE_ERROR_STATUS, type ZiteErrorCode, type ZiteErrorOptions, } from "./errors.js";
|
|
3
4
|
export { createZiteRouter, type ZiteRouterContext } from "./router.js";
|
|
4
|
-
export {
|
|
5
|
+
export { createAutomation, type StartProviderTrigger, type StartTrigger, type Automation, type AutomationArgs, type AutomationConfig, } from "./workflow.js";
|
|
5
6
|
export type { ZiteSchedule } from "../schedules/index.js";
|
|
6
|
-
export type { ZiteTrigger } from "../backend/index.js";
|
|
7
|
+
export type { ZiteTrigger, ZiteTriggerSubscribe, ZiteTriggerSubscribeInput, ZiteTriggerSubscribeResult, ZiteTriggerUnsubscribe, ZiteTriggerUnsubscribeInput, } from "../backend/index.js";
|
|
7
8
|
declare module "@tanstack/start-client-core" {
|
|
8
9
|
interface ServerFnOptions {
|
|
9
10
|
/** What this function does, in a sentence. Shown in the editor. */
|
|
10
|
-
description
|
|
11
|
+
description: string;
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
|
-
declare
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
readonly CONFLICT: 409;
|
|
19
|
-
readonly RATE_LIMITED: 429;
|
|
20
|
-
readonly INTERNAL_ERROR: 500;
|
|
21
|
-
};
|
|
22
|
-
export type ZiteErrorCode = keyof typeof STATUS;
|
|
23
|
-
/** Fail with a status: `throw new ZiteError('FORBIDDEN', 'Only the owner can edit this')`. */
|
|
24
|
-
export declare class ZiteError extends Error {
|
|
25
|
-
readonly code: ZiteErrorCode;
|
|
26
|
-
constructor(code: ZiteErrorCode, message?: string);
|
|
14
|
+
declare module "@tanstack/react-router" {
|
|
15
|
+
interface StaticDataRouteOption {
|
|
16
|
+
/** What an API route does, in a sentence. Shown in the editor. */
|
|
17
|
+
description?: string;
|
|
18
|
+
}
|
|
27
19
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export declare const SIGN_IN_REQUIRED = "ZITE_SIGN_IN_REQUIRED";
|
|
31
|
-
export declare const isSignInRequired: (error: unknown) => boolean;
|
|
20
|
+
/** By the role's id, as classic's does: a role's name can change. */
|
|
21
|
+
export declare const hasRole: (user: User, roleId: string) => boolean;
|
|
32
22
|
/**
|
|
33
23
|
* The platform's sign-in page, returning to `redirectTo` (default: the current
|
|
34
24
|
* page). For guards: `throw redirect({ href: loginHref(), reloadDocument: true })`.
|
|
@@ -36,13 +26,22 @@ export declare const isSignInRequired: (error: unknown) => boolean;
|
|
|
36
26
|
export declare function loginHref(redirectTo?: string): string;
|
|
37
27
|
/** What a zitejs middleware means, for describe. Keyed by identity, so composing or re-exporting keeps it. */
|
|
38
28
|
export type ZiteMiddlewareKind = {
|
|
39
|
-
kind: "signedIn" | "anyone" | "
|
|
29
|
+
kind: "signedIn" | "anyone" | "factory";
|
|
30
|
+
} | {
|
|
31
|
+
kind: "webhook";
|
|
32
|
+
paused?: true;
|
|
40
33
|
} | {
|
|
41
34
|
kind: "role";
|
|
42
35
|
role: string;
|
|
43
36
|
};
|
|
44
37
|
/** @internal */
|
|
45
38
|
export declare const ziteMiddlewareKind: (middleware: unknown) => ZiteMiddlewareKind | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* @internal TanStack answers a failed server function 200, with the error in the body. A nested call's failure is
|
|
41
|
+
* the caller's to handle, and so is one made while a page renders: its loader may catch it, and one that doesn't
|
|
42
|
+
* gets the error's status from the server entry. Redirects and notFound aren't Errors.
|
|
43
|
+
*/
|
|
44
|
+
export declare const setFailedStatus: (error: unknown) => void;
|
|
46
45
|
/** Only signed-in users. Handlers get `context.user: User`. */
|
|
47
46
|
export declare const signedIn: import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, {
|
|
48
47
|
user: User;
|
|
@@ -51,10 +50,16 @@ export declare const signedIn: import("@tanstack/react-start").RequestMiddleware
|
|
|
51
50
|
export declare const anyone: import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, {
|
|
52
51
|
user: User | null;
|
|
53
52
|
}>;
|
|
54
|
-
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Webhooks delivered by the platform, which already checked the URL token.
|
|
55
|
+
* `webhook.paused` keeps the URL but runs nothing: the platform answers the
|
|
56
|
+
* provider 200 and drops the delivery until it's switched back.
|
|
57
|
+
*/
|
|
58
|
+
export declare const webhook: import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, undefined> & {
|
|
59
|
+
paused: import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, undefined>;
|
|
60
|
+
};
|
|
61
|
+
/** Signed-in users with this role, by its id. */
|
|
62
|
+
export declare const role: (id: string) => import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, readonly [import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, {
|
|
58
63
|
user: User;
|
|
59
64
|
}>], undefined>;
|
|
60
65
|
declare const compiled: import("@tanstack/react-start").FunctionMiddlewareAfterClient<{}, unknown, undefined, undefined, undefined>;
|
package/dist/esm/start/index.js
CHANGED
|
@@ -1,32 +1,17 @@
|
|
|
1
1
|
import { createMiddleware, createServerFn } from "@tanstack/react-start";
|
|
2
2
|
import { setResponseStatus } from "@tanstack/react-start/server";
|
|
3
|
+
import { getStartContext } from "@tanstack/start-storage-context";
|
|
3
4
|
import { USAGE_TOKEN_QUERY_PARAM } from "../auth/constants.js";
|
|
5
|
+
import { asZiteError, errorFromThrown, SIGN_IN_REQUIRED, toClientErrorMessage, ZiteError, ZITE_ERROR_STATUS } from "./errors.js";
|
|
4
6
|
import { currentHost } from "./host.js";
|
|
7
|
+
import { PREVIEW_CLAIM_PARAM, previewClaim } from "./previewClaim.js";
|
|
8
|
+
import { recordRoute } from "./runLog.js";
|
|
5
9
|
import { getHost, inScope, runInScope } from "./scope.js";
|
|
10
|
+
export { isSignInRequired, SIGN_IN_REQUIRED, ZiteError, ZITE_ERROR_STATUS, } from "./errors.js";
|
|
6
11
|
export { createZiteRouter } from "./router.js";
|
|
7
|
-
export {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
UNAUTHORIZED: 401,
|
|
11
|
-
FORBIDDEN: 403,
|
|
12
|
-
NOT_FOUND: 404,
|
|
13
|
-
CONFLICT: 409,
|
|
14
|
-
RATE_LIMITED: 429,
|
|
15
|
-
INTERNAL_ERROR: 500,
|
|
16
|
-
};
|
|
17
|
-
/** Fail with a status: `throw new ZiteError('FORBIDDEN', 'Only the owner can edit this')`. */
|
|
18
|
-
export class ZiteError extends Error {
|
|
19
|
-
code;
|
|
20
|
-
constructor(code, message) {
|
|
21
|
-
super(message ?? code);
|
|
22
|
-
this.name = "ZiteError";
|
|
23
|
-
this.code = code;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export const hasRole = (user, role) => user.roles.includes(role);
|
|
27
|
-
/** The message a server function fails with when it needs a session; the platform's client middleware sends the user to sign in. */
|
|
28
|
-
export const SIGN_IN_REQUIRED = "ZITE_SIGN_IN_REQUIRED";
|
|
29
|
-
export const isSignInRequired = (error) => error instanceof Error && error.message === SIGN_IN_REQUIRED;
|
|
12
|
+
export { createAutomation, } from "./workflow.js";
|
|
13
|
+
/** By the role's id, as classic's does: a role's name can change. */
|
|
14
|
+
export const hasRole = (user, roleId) => user.roles.some(({ id }) => id === roleId);
|
|
30
15
|
/**
|
|
31
16
|
* The platform's sign-in page, returning to `redirectTo` (default: the current
|
|
32
17
|
* page). For guards: `throw redirect({ href: loginHref(), reloadDocument: true })`.
|
|
@@ -41,10 +26,13 @@ export function loginHref(redirectTo) {
|
|
|
41
26
|
params.set("redirectUrl", target.toString());
|
|
42
27
|
else if (!target && redirectTo)
|
|
43
28
|
params.set("redirectUrl", redirectTo);
|
|
44
|
-
// The editor preview's sign-in page needs the preview's token.
|
|
29
|
+
// The editor preview's sign-in page needs the preview's token, and its claim where the frame dropped the cookie.
|
|
45
30
|
const usageToken = current ? new URL(current).searchParams.get(USAGE_TOKEN_QUERY_PARAM) : null;
|
|
46
31
|
if (usageToken)
|
|
47
32
|
params.set(USAGE_TOKEN_QUERY_PARAM, usageToken);
|
|
33
|
+
const claim = previewClaim();
|
|
34
|
+
if (claim)
|
|
35
|
+
params.set(PREVIEW_CLAIM_PARAM, claim);
|
|
48
36
|
const query = params.toString();
|
|
49
37
|
return query ? `/auth/login?${query}` : "/auth/login";
|
|
50
38
|
}
|
|
@@ -63,49 +51,93 @@ const host = () => {
|
|
|
63
51
|
throw new ZiteError("UNAUTHORIZED", "Missing platform context");
|
|
64
52
|
return current;
|
|
65
53
|
};
|
|
66
|
-
//
|
|
67
|
-
|
|
54
|
+
// TanStack fails a validator with a plain Error whose message is the issues as JSON.
|
|
55
|
+
const validationIssues = (error) => {
|
|
56
|
+
if (!(error instanceof Error) || error.constructor !== Error)
|
|
57
|
+
return undefined;
|
|
58
|
+
try {
|
|
59
|
+
const issues = JSON.parse(error.message);
|
|
60
|
+
return Array.isArray(issues) && issues.length > 0 && issues.every((issue) => typeof issue?.message === "string")
|
|
61
|
+
? issues
|
|
62
|
+
: undefined;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const invalidInput = (issues) => {
|
|
69
|
+
if (getHost()?.mode !== "preview")
|
|
70
|
+
return new ZiteError("INVALID_INPUT", "Invalid input");
|
|
71
|
+
const describe = ({ message, path = [] }) => {
|
|
72
|
+
const at = path.map((segment) => String(typeof segment === "object" ? segment.key : segment)).join(".");
|
|
73
|
+
return at ? `${at}: ${message}` : message;
|
|
74
|
+
};
|
|
75
|
+
return new ZiteError("INVALID_INPUT", issues.map(describe).join("; "));
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* @internal TanStack answers a failed server function 200, with the error in the body. A nested call's failure is
|
|
79
|
+
* the caller's to handle, and so is one made while a page renders: its loader may catch it, and one that doesn't
|
|
80
|
+
* gets the error's status from the server entry. Redirects and notFound aren't Errors.
|
|
81
|
+
*/
|
|
82
|
+
export const setFailedStatus = (error) => {
|
|
83
|
+
if (!(error instanceof Error) || inScope() || getStartContext({ throwIfNotFound: false })?.handlerType !== "serverFn")
|
|
84
|
+
return;
|
|
85
|
+
setResponseStatus(ZITE_ERROR_STATUS[asZiteError(error)?.code ?? "INTERNAL_ERROR"]);
|
|
86
|
+
};
|
|
87
|
+
// Server functions report a status through TanStack's error envelope; server routes need a Response.
|
|
68
88
|
const withStatus = async (isServerFn, fn) => {
|
|
69
|
-
const nested = inScope();
|
|
70
89
|
try {
|
|
71
90
|
return await fn();
|
|
72
91
|
}
|
|
73
|
-
catch (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
92
|
+
catch (thrown) {
|
|
93
|
+
const caught = errorFromThrown(thrown);
|
|
94
|
+
const issues = validationIssues(caught);
|
|
95
|
+
const error = issues ? invalidInput(issues) : asZiteError(caught);
|
|
96
|
+
if (!isServerFn) {
|
|
97
|
+
if (!error)
|
|
98
|
+
throw caught;
|
|
99
|
+
const message = toClientErrorMessage(error.message, getHost()?.mode, error.userFacingMessage);
|
|
100
|
+
return new Response(message, { status: ZITE_ERROR_STATUS[error.code] });
|
|
101
|
+
}
|
|
102
|
+
setFailedStatus(error ?? caught);
|
|
103
|
+
throw error ?? caught;
|
|
81
104
|
}
|
|
82
105
|
};
|
|
106
|
+
// A server route's run is recorded around its access decision, so a refusal shows with its status.
|
|
107
|
+
const decide = (serverFnMeta, request, fn) => serverFnMeta ? withStatus(true, fn) : recordRoute(getHost(), request, () => withStatus(false, fn));
|
|
83
108
|
/** Only signed-in users. Handlers get `context.user: User`. */
|
|
84
|
-
export const signedIn = tag(createMiddleware().server(({ next, serverFnMeta }) =>
|
|
109
|
+
export const signedIn = tag(createMiddleware().server(({ next, serverFnMeta, request }) => decide(serverFnMeta, request, async () => {
|
|
85
110
|
const { user } = host();
|
|
86
111
|
if (!user)
|
|
87
112
|
throw new ZiteError("UNAUTHORIZED", SIGN_IN_REQUIRED);
|
|
88
113
|
return runInScope({ kind: "signedIn", user }, () => next({ context: { user } }));
|
|
89
114
|
})), { kind: "signedIn" });
|
|
90
115
|
/** Anyone, signed in or not. Handlers get `context.user: User | null`. */
|
|
91
|
-
export const anyone = tag(createMiddleware().server(({ next }) => {
|
|
116
|
+
export const anyone = tag(createMiddleware().server(({ next, serverFnMeta, request }) => decide(serverFnMeta, request, async () => {
|
|
92
117
|
const { user } = host();
|
|
93
118
|
return runInScope({ kind: "anyone", user }, () => next({ context: { user } }));
|
|
94
|
-
}), { kind: "anyone" });
|
|
95
|
-
|
|
96
|
-
export const webhook = tag(createMiddleware().server(({ next }) => {
|
|
119
|
+
})), { kind: "anyone" });
|
|
120
|
+
const webhookMiddleware = (paused) => tag(createMiddleware().server(({ next, request }) => {
|
|
97
121
|
if (host().invoke?.kind !== "webhook")
|
|
98
122
|
return new Response("Not found", { status: 404 });
|
|
99
|
-
return runInScope({ kind: "webhook", user: null }, () => next());
|
|
100
|
-
}), { kind: "webhook" });
|
|
101
|
-
/**
|
|
102
|
-
|
|
123
|
+
return recordRoute(getHost(), request, async () => runInScope({ kind: "webhook", user: null }, () => next()));
|
|
124
|
+
}), paused ? { kind: "webhook", paused: true } : { kind: "webhook" });
|
|
125
|
+
/**
|
|
126
|
+
* Webhooks delivered by the platform, which already checked the URL token.
|
|
127
|
+
* `webhook.paused` keeps the URL but runs nothing: the platform answers the
|
|
128
|
+
* provider 200 and drops the delivery until it's switched back.
|
|
129
|
+
*/
|
|
130
|
+
export const webhook = Object.assign(webhookMiddleware(false), { paused: webhookMiddleware(true) });
|
|
131
|
+
/** Signed-in users with this role, by its id. */
|
|
132
|
+
export const role = (id) => tag(createMiddleware()
|
|
103
133
|
.middleware([signedIn])
|
|
104
134
|
.server(({ next, context, serverFnMeta }) => withStatus(!!serverFnMeta, async () => {
|
|
105
|
-
if (!hasRole(context.user,
|
|
106
|
-
|
|
135
|
+
if (!hasRole(context.user, id)) {
|
|
136
|
+
// A role's id means nothing to the app's users; the editor still reads which one.
|
|
137
|
+
throw new ZiteError({ code: "FORBIDDEN", message: `Requires the ${id} role`, userFacingMessage: "You don't have access to this." });
|
|
138
|
+
}
|
|
107
139
|
return next();
|
|
108
|
-
})), { kind: "role", role:
|
|
140
|
+
})), { kind: "role", role: id });
|
|
109
141
|
// TanStack's compiler tags every server function it compiles with an id. A
|
|
110
142
|
// builder it didn't compile (an object property, one returned from a helper)
|
|
111
143
|
// has none and would run its handler wherever it was called, so it fails here.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { type AnySerializationAdapter } from "@tanstack/react-router";
|
|
2
2
|
import type { SSROption } from "@tanstack/router-core";
|
|
3
3
|
import type { AnyFunctionMiddleware, AnyRequestMiddleware, StartInstance } from "@tanstack/start-client-core";
|
|
4
|
-
import { ZiteError } from "./index.js";
|
|
5
4
|
/**
|
|
6
5
|
* The start instance every Start app builds with: the platform's middleware and
|
|
7
6
|
* adapters first, then whatever the app's own `src/start.ts` sets.
|
|
8
7
|
*/
|
|
9
|
-
export declare function createZiteStart<const TAdapters extends ReadonlyArray<AnySerializationAdapter> = [], TDefaultSsr extends SSROption = SSROption, const TRequest extends ReadonlyArray<AnyRequestMiddleware> = [], const TFunction extends ReadonlyArray<AnyFunctionMiddleware> = []>(app?: StartInstance<TAdapters, TDefaultSsr, TRequest, TFunction>): StartInstance<readonly [import("@tanstack/react-router").SerializationAdapter<
|
|
10
|
-
|
|
8
|
+
export declare function createZiteStart<const TAdapters extends ReadonlyArray<AnySerializationAdapter> = [], TDefaultSsr extends SSROption = SSROption, const TRequest extends ReadonlyArray<AnyRequestMiddleware> = [], const TFunction extends ReadonlyArray<AnyFunctionMiddleware> = []>(app?: StartInstance<TAdapters, TDefaultSsr, TRequest, TFunction>): StartInstance<readonly [import("@tanstack/react-router").SerializationAdapter<Error, {
|
|
9
|
+
stack?: string | undefined;
|
|
10
|
+
code: "BAD_REQUEST" | "UNAUTHORIZED" | "FORBIDDEN" | "NOT_FOUND" | "CONFLICT" | "RATE_LIMITED" | "INTERNAL_ERROR" | "INVALID_INPUT";
|
|
11
11
|
message: string;
|
|
12
|
-
}, never>, ...TAdapters], TDefaultSsr, readonly [import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, undefined>, ...TRequest], readonly [import("@tanstack/react-start").FunctionMiddlewareAfterClient<{}, unknown, undefined, undefined, undefined>, import("@tanstack/react-start").FunctionMiddlewareAfterServer<{}, unknown, undefined, undefined, undefined, undefined, undefined>, ...TFunction]>;
|
|
12
|
+
}, never>, ...TAdapters], TDefaultSsr, readonly [import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, undefined>, import("@tanstack/react-start").RequestMiddlewareAfterServer<{}, undefined, undefined>, ...TRequest], readonly [import("@tanstack/react-start").FunctionMiddlewareAfterClient<{}, unknown, undefined, undefined, undefined>, import("@tanstack/react-start").FunctionMiddlewareAfterServer<{}, unknown, undefined, undefined, undefined, undefined, undefined>, ...TFunction]>;
|