zitejs 0.9.114 → 0.9.115
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/backend/index.d.ts +13 -0
- package/dist/cjs/backend/index.js +63 -1
- package/dist/cjs/backend/index.test.d.ts +1 -0
- package/dist/cjs/backend/index.test.js +203 -0
- package/dist/cjs/runtime/index.js +108 -22
- package/dist/cjs/runtime/index.test.d.ts +1 -0
- package/dist/cjs/runtime/index.test.js +133 -0
- package/dist/cjs/sync/lib.js +14 -0
- package/dist/cjs/sync/sdkNames.d.ts +2 -1
- package/dist/cjs/sync/sdkNames.js +17 -1
- package/dist/cjs/sync/sdkNames.test.js +48 -0
- package/dist/esm/backend/index.d.ts +13 -0
- package/dist/esm/backend/index.js +63 -1
- package/dist/esm/backend/index.test.d.ts +1 -0
- package/dist/esm/backend/index.test.js +201 -0
- package/dist/esm/runtime/index.js +108 -22
- package/dist/esm/runtime/index.test.d.ts +1 -0
- package/dist/esm/runtime/index.test.js +131 -0
- package/dist/esm/sync/lib.js +14 -0
- package/dist/esm/sync/sdkNames.d.ts +2 -1
- package/dist/esm/sync/sdkNames.js +17 -1
- package/dist/esm/sync/sdkNames.test.js +48 -0
- package/package.json +1 -1
|
@@ -82,6 +82,12 @@ export interface EndpointConfig<TInput = unknown, TOutput = unknown, TStream ext
|
|
|
82
82
|
* `{ user: null }` for a webhook fire exactly as it does for a cron one.
|
|
83
83
|
*/
|
|
84
84
|
webhook?: TWebhook;
|
|
85
|
+
/**
|
|
86
|
+
* Set by {@link createEndpoint}, not by hand. The worker validates
|
|
87
|
+
* `inputSchema` for bundles built before this did, and skips ones that
|
|
88
|
+
* declare it — parsing twice would apply a schema's transforms twice.
|
|
89
|
+
*/
|
|
90
|
+
validatesInput?: boolean;
|
|
85
91
|
execute: (params: {
|
|
86
92
|
input: TInput;
|
|
87
93
|
context: TSchedule extends ZiteSchedule ? ZiteRequestContext | ZiteScheduledContext : TWebhook extends ZiteWebhook ? ZiteRequestContext | ZiteScheduledContext : ZiteRequestContext;
|
|
@@ -89,4 +95,11 @@ export interface EndpointConfig<TInput = unknown, TOutput = unknown, TStream ext
|
|
|
89
95
|
stream: ZiteStreamInterface;
|
|
90
96
|
} : {})) => Promise<TOutput> | TOutput;
|
|
91
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Create an endpoint definition, parsing `input` against `inputSchema` before
|
|
100
|
+
* `execute` runs. Nothing downstream parses it — the worker calls `execute` on
|
|
101
|
+
* whatever this returns — so while this was a bare `return config` a required
|
|
102
|
+
* field could be missing and the endpoint ran anyway. Parsing here is also what
|
|
103
|
+
* makes `TRawInput` -> `TInput` (defaults, transforms) true at runtime.
|
|
104
|
+
*/
|
|
92
105
|
export declare function createEndpoint<TInput = unknown, TOutput = unknown, TStream extends boolean = false, TSchedule extends ZiteSchedule | undefined = undefined, TWebhook extends ZiteWebhook | undefined = undefined, TRawInput = TInput>(config: EndpointConfig<TInput, TOutput, TStream, TSchedule, TWebhook, TRawInput>): EndpointConfig<TInput, TOutput, TStream, TSchedule, TWebhook, TRawInput>;
|
|
@@ -31,6 +31,68 @@ class ZiteError extends Error {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
exports.ZiteError = ZiteError;
|
|
34
|
+
/** Injected by the runner on a platform-fired run — see `isPlatformTriggeredInput`. */
|
|
35
|
+
const PLATFORM_TRIGGER_KEYS = ["__cron", "__webhook"];
|
|
36
|
+
function isPlatformTriggeredInput(input) {
|
|
37
|
+
if (typeof input !== "object" || input === null)
|
|
38
|
+
return false;
|
|
39
|
+
return PLATFORM_TRIGGER_KEYS.some((key) => Object.prototype.hasOwnProperty.call(input, key));
|
|
40
|
+
}
|
|
41
|
+
function isIssueBearingError(error) {
|
|
42
|
+
return (typeof error === "object" &&
|
|
43
|
+
error !== null &&
|
|
44
|
+
Array.isArray(error.issues));
|
|
45
|
+
}
|
|
46
|
+
function describeIssues(error) {
|
|
47
|
+
return error.issues
|
|
48
|
+
.map((issue) => {
|
|
49
|
+
const path = issue.path.map((part) => String(part)).join(".");
|
|
50
|
+
return path ? `${path}: ${issue.message}` : issue.message;
|
|
51
|
+
})
|
|
52
|
+
.join("; ");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create an endpoint definition, parsing `input` against `inputSchema` before
|
|
56
|
+
* `execute` runs. Nothing downstream parses it — the worker calls `execute` on
|
|
57
|
+
* whatever this returns — so while this was a bare `return config` a required
|
|
58
|
+
* field could be missing and the endpoint ran anyway. Parsing here is also what
|
|
59
|
+
* makes `TRawInput` -> `TInput` (defaults, transforms) true at runtime.
|
|
60
|
+
*/
|
|
34
61
|
function createEndpoint(config) {
|
|
35
|
-
|
|
62
|
+
const { inputSchema, execute } = config;
|
|
63
|
+
const firesWithoutARequest = Boolean(config.schedule || config.webhook);
|
|
64
|
+
// An `inputSchema` that isn't a validator stays inert, as it was.
|
|
65
|
+
if (!inputSchema || typeof inputSchema.parse !== "function")
|
|
66
|
+
return config;
|
|
67
|
+
return {
|
|
68
|
+
...config,
|
|
69
|
+
validatesInput: true,
|
|
70
|
+
// `async` so a schema failure rejects: `execute` may be declared sync, and
|
|
71
|
+
// a sync throw would escape the caller's `.catch()`.
|
|
72
|
+
execute: async (params) => {
|
|
73
|
+
// Cron and webhook fires carry no request body, so a schema written for
|
|
74
|
+
// request callers doesn't describe them.
|
|
75
|
+
if (isPlatformTriggeredInput(params.input))
|
|
76
|
+
return execute(params);
|
|
77
|
+
// Same, for a fire with no input object to hold the sentinel: the runner
|
|
78
|
+
// only merges `__cron` into a plain object.
|
|
79
|
+
if (firesWithoutARequest && params.input === undefined) {
|
|
80
|
+
return execute(params);
|
|
81
|
+
}
|
|
82
|
+
let parsed;
|
|
83
|
+
try {
|
|
84
|
+
parsed = inputSchema.parse(params.input);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new ZiteError({
|
|
88
|
+
code: "BAD_REQUEST",
|
|
89
|
+
message: isIssueBearingError(error)
|
|
90
|
+
? `Invalid input: ${describeIssues(error)}`
|
|
91
|
+
: `Invalid input: ${String(error?.message ?? error)}`,
|
|
92
|
+
userFacingMessage: "Some of the information sent with this request was missing or invalid.",
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return execute({ ...params, input: parsed });
|
|
96
|
+
},
|
|
97
|
+
};
|
|
36
98
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const index_js_1 = require("./index.js");
|
|
6
|
+
const index_js_2 = require("../runtime/index.js");
|
|
7
|
+
const requestContext = { user: { id: "u1", email: "a@b.c" } };
|
|
8
|
+
(0, vitest_1.afterEach)(() => {
|
|
9
|
+
vitest_1.vi.unstubAllGlobals();
|
|
10
|
+
});
|
|
11
|
+
(0, vitest_1.describe)("createEndpoint input validation", () => {
|
|
12
|
+
(0, vitest_1.it)("rejects input that does not satisfy inputSchema, without running execute", async () => {
|
|
13
|
+
const execute = vitest_1.vi.fn();
|
|
14
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
15
|
+
inputSchema: zod_1.z.object({ token: zod_1.z.string().min(10) }),
|
|
16
|
+
execute,
|
|
17
|
+
});
|
|
18
|
+
await (0, vitest_1.expect)(endpoint.execute({ input: {}, context: requestContext })).rejects.toBeInstanceOf(index_js_1.ZiteError);
|
|
19
|
+
(0, vitest_1.expect)(execute).not.toHaveBeenCalled();
|
|
20
|
+
});
|
|
21
|
+
(0, vitest_1.it)("surfaces the failure as BAD_REQUEST and names the offending field", async () => {
|
|
22
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
23
|
+
inputSchema: zod_1.z.object({ token: zod_1.z.string().min(10) }),
|
|
24
|
+
execute: async () => "unreachable",
|
|
25
|
+
});
|
|
26
|
+
await (0, vitest_1.expect)(endpoint.execute({ input: {}, context: requestContext })).rejects.toMatchObject({
|
|
27
|
+
code: "BAD_REQUEST",
|
|
28
|
+
message: vitest_1.expect.stringContaining("token"),
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
(0, vitest_1.it)("rejects a call with no input at all", async () => {
|
|
32
|
+
const execute = vitest_1.vi.fn();
|
|
33
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
34
|
+
inputSchema: zod_1.z.object({ token: zod_1.z.string().min(10) }),
|
|
35
|
+
execute,
|
|
36
|
+
});
|
|
37
|
+
await (0, vitest_1.expect)(endpoint.execute({
|
|
38
|
+
input: undefined,
|
|
39
|
+
context: requestContext,
|
|
40
|
+
})).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
41
|
+
(0, vitest_1.expect)(execute).not.toHaveBeenCalled();
|
|
42
|
+
});
|
|
43
|
+
(0, vitest_1.it)("hands execute the parsed value, so schema defaults finally apply", async () => {
|
|
44
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
45
|
+
inputSchema: zod_1.z.object({
|
|
46
|
+
token: zod_1.z.string(),
|
|
47
|
+
limit: zod_1.z.number().default(10),
|
|
48
|
+
}),
|
|
49
|
+
execute: async ({ input }) => input,
|
|
50
|
+
});
|
|
51
|
+
await (0, vitest_1.expect)(endpoint.execute({
|
|
52
|
+
input: { token: "abc" },
|
|
53
|
+
context: requestContext,
|
|
54
|
+
})).resolves.toEqual({ token: "abc", limit: 10 });
|
|
55
|
+
});
|
|
56
|
+
(0, vitest_1.it)("passes valid input through untouched", async () => {
|
|
57
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
58
|
+
inputSchema: zod_1.z.object({
|
|
59
|
+
token: zod_1.z.string().min(10),
|
|
60
|
+
action: zod_1.z.enum(["load", "accept"]),
|
|
61
|
+
}),
|
|
62
|
+
execute: async ({ input }) => input.action,
|
|
63
|
+
});
|
|
64
|
+
await (0, vitest_1.expect)(endpoint.execute({
|
|
65
|
+
input: { token: "b69779ae-eeea", action: "load" },
|
|
66
|
+
context: requestContext,
|
|
67
|
+
})).resolves.toBe("load");
|
|
68
|
+
});
|
|
69
|
+
(0, vitest_1.it)("declares that it validates, so the worker does not parse a second time", () => {
|
|
70
|
+
const withSchema = (0, index_js_1.createEndpoint)({
|
|
71
|
+
inputSchema: zod_1.z.object({ token: zod_1.z.string() }),
|
|
72
|
+
execute: async () => null,
|
|
73
|
+
});
|
|
74
|
+
const withoutSchema = (0, index_js_1.createEndpoint)({ execute: async () => null });
|
|
75
|
+
(0, vitest_1.expect)(withSchema.validatesInput).toBe(true);
|
|
76
|
+
(0, vitest_1.expect)(withoutSchema.validatesInput).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
(0, vitest_1.it)("leaves an endpoint without an inputSchema exactly as it was", async () => {
|
|
79
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
80
|
+
execute: async ({ input }) => input,
|
|
81
|
+
});
|
|
82
|
+
await (0, vitest_1.expect)(endpoint.execute({
|
|
83
|
+
input: { anything: true },
|
|
84
|
+
context: requestContext,
|
|
85
|
+
})).resolves.toEqual({ anything: true });
|
|
86
|
+
});
|
|
87
|
+
(0, vitest_1.it)("preserves the stream handle for streaming endpoints", async () => {
|
|
88
|
+
const stream = { write: async () => { }, forward: async () => "" };
|
|
89
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
90
|
+
stream: true,
|
|
91
|
+
inputSchema: zod_1.z.object({ prompt: zod_1.z.string() }),
|
|
92
|
+
execute: async (params) => params.stream === stream,
|
|
93
|
+
});
|
|
94
|
+
await (0, vitest_1.expect)(endpoint.execute({
|
|
95
|
+
input: { prompt: "hi" },
|
|
96
|
+
context: requestContext,
|
|
97
|
+
stream,
|
|
98
|
+
})).resolves.toBe(true);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
(0, vitest_1.describe)("platform-triggered runs", () => {
|
|
102
|
+
(0, vitest_1.it)("does not validate a cron fire, and leaves __cron readable", async () => {
|
|
103
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
104
|
+
inputSchema: zod_1.z.object({ token: zod_1.z.string().min(10) }),
|
|
105
|
+
schedule: {
|
|
106
|
+
scheduleType: "recurring",
|
|
107
|
+
schedule: { frequency: "hourly", interval: 6 },
|
|
108
|
+
timezone: "UTC",
|
|
109
|
+
},
|
|
110
|
+
execute: async ({ input }) => input,
|
|
111
|
+
});
|
|
112
|
+
await (0, vitest_1.expect)(endpoint.execute({
|
|
113
|
+
input: { __cron: { scheduleId: "s1" } },
|
|
114
|
+
context: { user: null },
|
|
115
|
+
})).resolves.toEqual({ __cron: { scheduleId: "s1" } });
|
|
116
|
+
});
|
|
117
|
+
(0, vitest_1.it)("does not validate a scheduled fire that carries no input object", async () => {
|
|
118
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
119
|
+
inputSchema: zod_1.z.string(),
|
|
120
|
+
schedule: {
|
|
121
|
+
scheduleType: "recurring",
|
|
122
|
+
schedule: { frequency: "hourly", interval: 6 },
|
|
123
|
+
timezone: "UTC",
|
|
124
|
+
},
|
|
125
|
+
execute: async ({ input }) => input ?? "(no input)",
|
|
126
|
+
});
|
|
127
|
+
await (0, vitest_1.expect)(endpoint.execute({ input: undefined, context: { user: null } })).resolves.toBe("(no input)");
|
|
128
|
+
});
|
|
129
|
+
(0, vitest_1.it)("still validates an ordinary endpoint called with no input", async () => {
|
|
130
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
131
|
+
inputSchema: zod_1.z.string(),
|
|
132
|
+
execute: async ({ input }) => input,
|
|
133
|
+
});
|
|
134
|
+
await (0, vitest_1.expect)(endpoint.execute({ input: undefined, context: requestContext })).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
135
|
+
});
|
|
136
|
+
(0, vitest_1.it)("does not validate a webhook fire", async () => {
|
|
137
|
+
const endpoint = (0, index_js_1.createEndpoint)({
|
|
138
|
+
inputSchema: zod_1.z.object({ token: zod_1.z.string().min(10) }),
|
|
139
|
+
webhook: {},
|
|
140
|
+
execute: async ({ input }) => input,
|
|
141
|
+
});
|
|
142
|
+
await (0, vitest_1.expect)(endpoint.execute({
|
|
143
|
+
input: { __webhook: { headers: {} }, id: 7 },
|
|
144
|
+
context: { user: null },
|
|
145
|
+
})).resolves.toEqual({ __webhook: { headers: {} }, id: 7 });
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
/**
|
|
149
|
+
* The failure this was written for: an endpoint that looks a customer up by a
|
|
150
|
+
* token, called with no token. The undefined was dropped on the way to the
|
|
151
|
+
* database and the unfiltered lookup returned the first row of the table.
|
|
152
|
+
* Either guard alone stops it; they fail at different layers, so both are here.
|
|
153
|
+
*/
|
|
154
|
+
(0, vitest_1.describe)("regression: token lookup on an endpoint called without a token", () => {
|
|
155
|
+
function buildEndpoint(onMatch) {
|
|
156
|
+
const clients = (0, index_js_2.createTableClient)("Clients");
|
|
157
|
+
return (0, index_js_1.createEndpoint)({
|
|
158
|
+
inputSchema: zod_1.z.object({
|
|
159
|
+
token: zod_1.z.string().min(10),
|
|
160
|
+
action: zod_1.z.enum(["load", "accept"]),
|
|
161
|
+
}),
|
|
162
|
+
execute: async ({ input }) => {
|
|
163
|
+
const row = await clients.findOne({ filters: { token: input.token } });
|
|
164
|
+
if (!row)
|
|
165
|
+
return "invalid_token";
|
|
166
|
+
onMatch(row.nom);
|
|
167
|
+
return "ok";
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
(0, vitest_1.it)("stops at the schema, so the database is never reached", async () => {
|
|
172
|
+
const dispatched = [];
|
|
173
|
+
vitest_1.vi.stubGlobal("__wrapSdkCall", async (_i, _c, m) => {
|
|
174
|
+
dispatched.push(m);
|
|
175
|
+
return { id: "1", token: "", nom: "an unrelated customer" };
|
|
176
|
+
});
|
|
177
|
+
const matched = [];
|
|
178
|
+
await (0, vitest_1.expect)(buildEndpoint((nom) => matched.push(nom)).execute({
|
|
179
|
+
input: {},
|
|
180
|
+
context: requestContext,
|
|
181
|
+
})).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
182
|
+
(0, vitest_1.expect)(dispatched).toEqual([]);
|
|
183
|
+
(0, vitest_1.expect)(matched).toEqual([]);
|
|
184
|
+
});
|
|
185
|
+
(0, vitest_1.it)("stops at the query too, if an undefined token reaches it another way", async () => {
|
|
186
|
+
const dispatched = [];
|
|
187
|
+
vitest_1.vi.stubGlobal("__wrapSdkCall", async (_i, _c, m) => {
|
|
188
|
+
dispatched.push(m);
|
|
189
|
+
return { id: "1", token: "", nom: "an unrelated customer" };
|
|
190
|
+
});
|
|
191
|
+
const warn = vitest_1.vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
192
|
+
const clients = (0, index_js_2.createTableClient)("Clients");
|
|
193
|
+
const token = undefined;
|
|
194
|
+
// Answers "no match" rather than throwing: `if (!row) return
|
|
195
|
+
// { status: 'invalid_token' }` is what the endpoint already does, so this
|
|
196
|
+
// makes the incident path correct without the app changing anything.
|
|
197
|
+
const row = await clients.findOne({ filters: { token } });
|
|
198
|
+
(0, vitest_1.expect)(row).toBeUndefined();
|
|
199
|
+
(0, vitest_1.expect)(dispatched).toEqual([]);
|
|
200
|
+
(0, vitest_1.expect)(warn).toHaveBeenCalled();
|
|
201
|
+
warn.mockRestore();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
@@ -8,6 +8,78 @@ exports.createAirtableClient = createAirtableClient;
|
|
|
8
8
|
exports.createEmailClient = createEmailClient;
|
|
9
9
|
const sdkCall_js_1 = require("../internal/sdkCall.js");
|
|
10
10
|
const DB_INTEGRATION_ID = "databases";
|
|
11
|
+
function isPlainObject(value) {
|
|
12
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Not thrown — the miss is the answer. Logged so the author can still find out
|
|
16
|
+
* their value was missing, since a silent null looks exactly like a legitimate
|
|
17
|
+
* no-match and would otherwise hide the bug that caused it.
|
|
18
|
+
*/
|
|
19
|
+
function warnEmptySelector(className, method, detail) {
|
|
20
|
+
console.warn(`${className}.${method}: ${detail} is undefined, so this query would have ` +
|
|
21
|
+
"run unnarrowed and returned an arbitrary record. Returning no match " +
|
|
22
|
+
"instead. Check the value before querying, or pass null to match " +
|
|
23
|
+
"records where the field is empty.");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Whether a selector was written with nothing in it.
|
|
27
|
+
*
|
|
28
|
+
* `JSON.stringify` drops an undefined value, so `{ filters: { token: undefined } }`
|
|
29
|
+
* reaches base-runner as `{ filters: {} }` — a valid request for no filter.
|
|
30
|
+
* `findOne` answers that with the first row of the table, and the caller reads
|
|
31
|
+
* it back as a match.
|
|
32
|
+
*
|
|
33
|
+
* `findOne` treats it as "matched nothing" instead, which is what asking for
|
|
34
|
+
* `token === undefined` should mean. Callers already handle the miss —
|
|
35
|
+
* `if (!row) return { status: 'invalid_token' }` — so the shape that caused the
|
|
36
|
+
* incident becomes correct on its own, with no app change and nothing to break.
|
|
37
|
+
*
|
|
38
|
+
* **`findOne` only.** On `findAll`, "no filter" is a coherent request, and
|
|
39
|
+
* `filters: { status: maybeUndefined }` meaning "don't narrow on status" is an
|
|
40
|
+
* idiom that type-checks today (`RecordFilters` is a `?:` map, and apps build
|
|
41
|
+
* without `exactOptionalPropertyTypes`). It is also what Prisma does with
|
|
42
|
+
* `undefined` in a `where`. Only `findOne` has no sane reading, because an
|
|
43
|
+
* unnarrowed single-row query returns an arbitrary record rather than everything.
|
|
44
|
+
*
|
|
45
|
+
* A key the caller never wrote is untouched: `findOne({ filters: {} })` still
|
|
46
|
+
* means "no filter" and still returns the first row. Only a key written with
|
|
47
|
+
* nothing in it counts. (Unknown filter keys have the same consequence — see
|
|
48
|
+
* `RecordFilters` — but those are `keyof T` and caught at build time.)
|
|
49
|
+
*/
|
|
50
|
+
function selectorMatchesNothing(className, method, params) {
|
|
51
|
+
if (!params)
|
|
52
|
+
return false;
|
|
53
|
+
const { filters } = params;
|
|
54
|
+
if (isPlainObject(filters)) {
|
|
55
|
+
for (const [key, value] of Object.entries(filters)) {
|
|
56
|
+
if (value === undefined) {
|
|
57
|
+
warnEmptySelector(className, method, `filter "${key}"`);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
// `{ field: { contains: undefined } }` collapses to `{ field: {} }` — no
|
|
61
|
+
// condition on that field at all.
|
|
62
|
+
if (isPlainObject(value)) {
|
|
63
|
+
for (const [operator, operand] of Object.entries(value)) {
|
|
64
|
+
if (operand === undefined) {
|
|
65
|
+
warnEmptySelector(className, method, `filter "${key}.${operator}"`);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// An undefined `id` leaves the query with no selector at all. Only counts
|
|
73
|
+
// when nothing else narrows it, so `findOne({ id: maybeId, filters: {...} })`
|
|
74
|
+
// still runs on the filters.
|
|
75
|
+
const wroteId = Object.prototype.hasOwnProperty.call(params, "id");
|
|
76
|
+
const hasFilters = isPlainObject(filters) && Object.keys(filters).length > 0;
|
|
77
|
+
if (wroteId && params.id === undefined && !hasFilters) {
|
|
78
|
+
warnEmptySelector(className, method, "`id`");
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
11
83
|
function getBaseId() {
|
|
12
84
|
try {
|
|
13
85
|
return (globalThis.__ZITE_EXECUTION_CONFIG__?.baseId ??
|
|
@@ -34,16 +106,22 @@ function resolveTableId(className) {
|
|
|
34
106
|
}
|
|
35
107
|
function createTableClient(className) {
|
|
36
108
|
return {
|
|
37
|
-
findAll: (params) =>
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
109
|
+
findAll: async (params) => {
|
|
110
|
+
return (0, sdkCall_js_1.getSdkCall)()(DB_INTEGRATION_ID, className, "findAll", {
|
|
111
|
+
baseId: getBaseId(),
|
|
112
|
+
tableId: resolveTableId(className),
|
|
113
|
+
...params,
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
findOne: async (params) => {
|
|
117
|
+
if (selectorMatchesNothing(className, "findOne", params))
|
|
118
|
+
return undefined;
|
|
119
|
+
return (0, sdkCall_js_1.getSdkCall)()(DB_INTEGRATION_ID, className, "findOne", {
|
|
120
|
+
baseId: getBaseId(),
|
|
121
|
+
tableId: resolveTableId(className),
|
|
122
|
+
...params,
|
|
123
|
+
});
|
|
124
|
+
},
|
|
47
125
|
create: (params) => (0, sdkCall_js_1.getSdkCall)()(DB_INTEGRATION_ID, className, "create", {
|
|
48
126
|
baseId: getBaseId(),
|
|
49
127
|
tableId: resolveTableId(className),
|
|
@@ -74,10 +152,12 @@ function createSqlClient() {
|
|
|
74
152
|
}
|
|
75
153
|
function createAuthClient() {
|
|
76
154
|
return {
|
|
77
|
-
findAllUsers: (options) =>
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
155
|
+
findAllUsers: async (options) => {
|
|
156
|
+
return (0, sdkCall_js_1.getSdkCall)()(DB_INTEGRATION_ID, "Auth", "findAllUsers", {
|
|
157
|
+
baseId: getBaseId(),
|
|
158
|
+
...options,
|
|
159
|
+
});
|
|
160
|
+
},
|
|
81
161
|
updateUserProfile: (userId, profile) => (0, sdkCall_js_1.getSdkCall)()(DB_INTEGRATION_ID, "Auth", "updateUserProfile", {
|
|
82
162
|
baseId: getBaseId(),
|
|
83
163
|
userId,
|
|
@@ -87,14 +167,20 @@ function createAuthClient() {
|
|
|
87
167
|
}
|
|
88
168
|
function createAirtableClient(integrationId, className, implicitParams) {
|
|
89
169
|
return {
|
|
90
|
-
findAll: (params) =>
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
170
|
+
findAll: async (params) => {
|
|
171
|
+
return (0, sdkCall_js_1.getSdkCall)()(integrationId, className, "findAll", {
|
|
172
|
+
...implicitParams,
|
|
173
|
+
...params,
|
|
174
|
+
});
|
|
175
|
+
},
|
|
176
|
+
findOne: async (params) => {
|
|
177
|
+
if (selectorMatchesNothing(className, "findOne", params))
|
|
178
|
+
return undefined;
|
|
179
|
+
return (0, sdkCall_js_1.getSdkCall)()(integrationId, className, "findOne", {
|
|
180
|
+
...implicitParams,
|
|
181
|
+
...params,
|
|
182
|
+
});
|
|
183
|
+
},
|
|
98
184
|
create: (params) => (0, sdkCall_js_1.getSdkCall)()(integrationId, className, "create", {
|
|
99
185
|
...implicitParams,
|
|
100
186
|
...params,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const index_js_1 = require("./index.js");
|
|
5
|
+
/** Captures what the runtime would have dispatched to the workflow-runner. */
|
|
6
|
+
function stubSdkCall(result = undefined) {
|
|
7
|
+
const calls = [];
|
|
8
|
+
vitest_1.vi.stubGlobal("__wrapSdkCall", async (_integrationId, _className, method, params) => {
|
|
9
|
+
calls.push({ method, params });
|
|
10
|
+
return result;
|
|
11
|
+
});
|
|
12
|
+
return calls;
|
|
13
|
+
}
|
|
14
|
+
(0, vitest_1.afterEach)(() => {
|
|
15
|
+
vitest_1.vi.unstubAllGlobals();
|
|
16
|
+
});
|
|
17
|
+
(0, vitest_1.describe)("the mechanism", () => {
|
|
18
|
+
(0, vitest_1.it)("drops an undefined filter value on serialization, leaving an unfiltered read", () => {
|
|
19
|
+
const token = undefined;
|
|
20
|
+
const wire = JSON.stringify({ filters: { tokenFraisAdmin: token } });
|
|
21
|
+
(0, vitest_1.expect)(wire).toBe('{"filters":{}}');
|
|
22
|
+
(0, vitest_1.expect)(JSON.parse(wire).filters).toEqual({});
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
(0, vitest_1.describe)("findOne", () => {
|
|
26
|
+
(0, vitest_1.it)("matches nothing when a filter value is undefined, without dispatching", async () => {
|
|
27
|
+
// The incident shape. `if (!row) return { status: 'invalid_token' }` is
|
|
28
|
+
// what callers already write, so answering "no match" makes that code
|
|
29
|
+
// correct on its own — where returning the first row made it lie.
|
|
30
|
+
const calls = stubSdkCall();
|
|
31
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
32
|
+
const token = undefined;
|
|
33
|
+
const row = await clients.findOne({
|
|
34
|
+
filters: { tokenFraisAdmin: token },
|
|
35
|
+
});
|
|
36
|
+
(0, vitest_1.expect)(row).toBeUndefined();
|
|
37
|
+
(0, vitest_1.expect)(calls).toHaveLength(0);
|
|
38
|
+
});
|
|
39
|
+
(0, vitest_1.it)("warns so a missing value is still discoverable in the run log", async () => {
|
|
40
|
+
// A silent miss looks exactly like a legitimate no-match, which would hide
|
|
41
|
+
// the bug that produced it.
|
|
42
|
+
const warn = vitest_1.vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
43
|
+
stubSdkCall();
|
|
44
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
45
|
+
await clients.findOne({ filters: { tokenFraisAdmin: undefined } });
|
|
46
|
+
(0, vitest_1.expect)(warn).toHaveBeenCalledWith(vitest_1.expect.stringContaining('filter "tokenFraisAdmin" is undefined'));
|
|
47
|
+
warn.mockRestore();
|
|
48
|
+
});
|
|
49
|
+
(0, vitest_1.it)("matches nothing on an undefined operand inside a filter condition", async () => {
|
|
50
|
+
const calls = stubSdkCall();
|
|
51
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
52
|
+
const row = await clients.findOne({
|
|
53
|
+
filters: { nom: { contains: undefined } },
|
|
54
|
+
});
|
|
55
|
+
(0, vitest_1.expect)(row).toBeUndefined();
|
|
56
|
+
(0, vitest_1.expect)(calls).toHaveLength(0);
|
|
57
|
+
});
|
|
58
|
+
(0, vitest_1.it)("matches nothing on an undefined id when nothing else narrows the query", async () => {
|
|
59
|
+
const calls = stubSdkCall();
|
|
60
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
61
|
+
const id = undefined;
|
|
62
|
+
const row = await clients.findOne({ id });
|
|
63
|
+
(0, vitest_1.expect)(row).toBeUndefined();
|
|
64
|
+
(0, vitest_1.expect)(calls).toHaveLength(0);
|
|
65
|
+
});
|
|
66
|
+
(0, vitest_1.it)("still allows an undefined id when filters narrow the query", async () => {
|
|
67
|
+
const calls = stubSdkCall();
|
|
68
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
69
|
+
await clients.findOne({ id: undefined, filters: { nom: "Henry" } });
|
|
70
|
+
(0, vitest_1.expect)(calls).toHaveLength(1);
|
|
71
|
+
});
|
|
72
|
+
(0, vitest_1.it)("still allows a deliberate unfiltered lookup", async () => {
|
|
73
|
+
const calls = stubSdkCall();
|
|
74
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
75
|
+
await clients.findOne({ filters: {} });
|
|
76
|
+
await clients.findOne({});
|
|
77
|
+
(0, vitest_1.expect)(calls).toHaveLength(2);
|
|
78
|
+
});
|
|
79
|
+
(0, vitest_1.it)("passes real values through untouched", async () => {
|
|
80
|
+
const calls = stubSdkCall();
|
|
81
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
82
|
+
await clients.findOne({ filters: { tokenFraisAdmin: "b69779ae-eeea" } });
|
|
83
|
+
(0, vitest_1.expect)(calls[0]?.params).toMatchObject({
|
|
84
|
+
filters: { tokenFraisAdmin: "b69779ae-eeea" },
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
(0, vitest_1.it)("allows null, which means the field is empty", async () => {
|
|
88
|
+
const calls = stubSdkCall();
|
|
89
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
90
|
+
await clients.findOne({ filters: { nom: { not: null } } });
|
|
91
|
+
(0, vitest_1.expect)(calls).toHaveLength(1);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
(0, vitest_1.describe)("findAll", () => {
|
|
95
|
+
(0, vitest_1.it)("treats an undefined filter value as 'do not narrow on this'", async () => {
|
|
96
|
+
// Deliberately NOT guarded, unlike findOne. On a list query "no filter" is
|
|
97
|
+
// a coherent request, `filters: { x: maybeUndefined }` type-checks today,
|
|
98
|
+
// and this is what Prisma does with undefined in a `where`. The dangerous
|
|
99
|
+
// reading is findOne's, where an unnarrowed query returns an arbitrary row
|
|
100
|
+
// that the caller treats as a match.
|
|
101
|
+
const calls = stubSdkCall({ records: [], hasMore: false });
|
|
102
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
103
|
+
await clients.findAll({ filters: { tokenFraisAdmin: undefined } });
|
|
104
|
+
(0, vitest_1.expect)(calls).toHaveLength(1);
|
|
105
|
+
});
|
|
106
|
+
(0, vitest_1.it)("still allows listing a whole table on purpose", async () => {
|
|
107
|
+
const calls = stubSdkCall({ records: [], hasMore: false });
|
|
108
|
+
const clients = (0, index_js_1.createTableClient)("Clients");
|
|
109
|
+
await clients.findAll();
|
|
110
|
+
await clients.findAll({ limit: 10 });
|
|
111
|
+
(0, vitest_1.expect)(calls).toHaveLength(2);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
(0, vitest_1.describe)("other clients on the same path", () => {
|
|
115
|
+
(0, vitest_1.it)("guards the Airtable client", async () => {
|
|
116
|
+
const calls = stubSdkCall();
|
|
117
|
+
const table = (0, index_js_1.createAirtableClient)("airtable", "Clients", {
|
|
118
|
+
baseId: "app1",
|
|
119
|
+
tableId: "tbl1",
|
|
120
|
+
});
|
|
121
|
+
const row = await table.findOne({
|
|
122
|
+
filters: { tokenFraisAdmin: undefined },
|
|
123
|
+
});
|
|
124
|
+
(0, vitest_1.expect)(row).toBeUndefined();
|
|
125
|
+
(0, vitest_1.expect)(calls).toHaveLength(0);
|
|
126
|
+
});
|
|
127
|
+
(0, vitest_1.it)("leaves auth user lookups alone, as a findAll", async () => {
|
|
128
|
+
const calls = stubSdkCall({ records: [], total: 0, hasMore: false });
|
|
129
|
+
const auth = (0, index_js_1.createAuthClient)();
|
|
130
|
+
await auth.findAllUsers({ filters: { email: undefined } });
|
|
131
|
+
(0, vitest_1.expect)(calls).toHaveLength(1);
|
|
132
|
+
});
|
|
133
|
+
});
|
package/dist/cjs/sync/lib.js
CHANGED
|
@@ -541,6 +541,7 @@ function generateDbTs(inputSchema) {
|
|
|
541
541
|
lines.push('// `not: null` means "is set". An empty `in: []` matches nothing; an', "// empty `notIn: []` applies no filter.");
|
|
542
542
|
lines.push("// sort is currently IGNORED here — it is stripped before the request.", "// (it does work on zite.auth.findAllUsers). Order in SQL instead.");
|
|
543
543
|
lines.push("// limit defaults to 500, max 2000; offset is a row count (a number)");
|
|
544
|
+
lines.push('// a filter value of `undefined` is REJECTED (it would query unfiltered', '// and match an arbitrary record). Check the value first.');
|
|
544
545
|
lines.push("// .findOne({ id?, filters?, fields? }) → T | undefined");
|
|
545
546
|
lines.push("// .create({ record }) → T");
|
|
546
547
|
lines.push("// .update({ id, record }) → { id: string, fields: Partial<T> }");
|
|
@@ -1157,6 +1158,13 @@ function generateAirtableTs(inputLock) {
|
|
|
1157
1158
|
"// Auto-generated by zitejs generate from zite.lock. Do not edit manually.",
|
|
1158
1159
|
"// Airtable SDK — uses createAirtableClient from zitejs/runtime.",
|
|
1159
1160
|
"//",
|
|
1161
|
+
"// Usage in endpoint files (src/api/*.ts) — one client is exported per table:",
|
|
1162
|
+
`// import { ${lock.tables[0]?.sdkName ?? "TableName"} } from 'zitejs/integrations';`,
|
|
1163
|
+
"//",
|
|
1164
|
+
"// Always use 'zitejs/integrations' — never a relative path like",
|
|
1165
|
+
"// '../../.zite/integrations/airtable'. The tsconfig aliases resolve zitejs/*",
|
|
1166
|
+
"// imports to the correct .zite/ files.",
|
|
1167
|
+
"//",
|
|
1160
1168
|
"// Each exported table client has these methods (all take a single params object):",
|
|
1161
1169
|
"// findAll({ offset?, limit?, filters? }) => { records: T[], offset: string | undefined, hasMore: boolean }",
|
|
1162
1170
|
"// findOne({ id?, filters? }) => T | undefined",
|
|
@@ -1374,6 +1382,12 @@ function generateEmailSdk(integrationId) {
|
|
|
1374
1382
|
"// Auto-generated by zitejs generate from zite.config.json. Do not edit manually.",
|
|
1375
1383
|
"// Email SDK — sends through the Zite email gateway via the runtime bridge.",
|
|
1376
1384
|
"//",
|
|
1385
|
+
"// Usage in endpoint files (src/api/*.ts):",
|
|
1386
|
+
"// import { Email } from 'zitejs/email';",
|
|
1387
|
+
"//",
|
|
1388
|
+
"// Always use 'zitejs/email' — never a relative path like '../../.zite/integrations/email'.",
|
|
1389
|
+
"// The tsconfig aliases resolve zitejs/* imports to the correct .zite/ files.",
|
|
1390
|
+
"//",
|
|
1377
1391
|
"// await Email.send({ to, subject, body: [{ type: 'text', content: '...' }] })",
|
|
1378
1392
|
"// => { success: boolean; messageId: string }",
|
|
1379
1393
|
"//",
|
|
@@ -25,7 +25,8 @@ export declare function toPascalCase(name: string): string;
|
|
|
25
25
|
export declare function toCamelCase(name: string): string;
|
|
26
26
|
/**
|
|
27
27
|
* camelCase for a FRESH sdkName, reading acronym runs as words: "VIP" ->
|
|
28
|
-
* "vip", "APIKey" -> "apiKey" (plain toCamelCase yields "vIP" / "aPIKey")
|
|
28
|
+
* "vip", "APIKey" -> "apiKey" (plain toCamelCase yields "vIP" / "aPIKey"), and
|
|
29
|
+
* folding a diacritic into its base letter: "Filières" -> "filieres".
|
|
29
30
|
*
|
|
30
31
|
* Deliberately not folded into toCamelCase: that also derives endpoint
|
|
31
32
|
* identifiers from existing filenames on every generate, where normalizing
|