zitejs 0.9.26 → 0.9.28

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.
@@ -264,8 +264,14 @@ function createAliasPlugin(opts) {
264
264
  };
265
265
  }
266
266
  function generateEndpointWrapper(endpointName, usedImports, sdkExportKinds, sdkSource) {
267
+ // Always import the prebundled runtime to ensure globalThis.__wrapSdkCall
268
+ // is available for createTableClient/createSqlClient from zitejs/runtime.
269
+ const runtimeInit = `import { __wrapSdkCall, initRuntime } from '@zite/endpoints-runtime-sdk';
270
+ globalThis.__wrapSdkCall = __wrapSdkCall;
271
+ initRuntime();`;
267
272
  if (usedImports === null) {
268
273
  return `
274
+ ${runtimeInit}
269
275
  import * as sdk from '${sdkSource}';
270
276
  Object.assign(globalThis, sdk);
271
277
  import endpoint from './api/${endpointName}';
@@ -274,6 +280,7 @@ globalThis.__endpoint = endpoint;
274
280
  }
275
281
  if (usedImports.length === 0) {
276
282
  return `
283
+ ${runtimeInit}
277
284
  import endpoint from './api/${endpointName}';
278
285
  globalThis.__endpoint = endpoint;
279
286
  `;
@@ -299,6 +306,7 @@ globalThis.__endpoint = endpoint;
299
306
  ? `Object.assign(globalThis, { ${valueImports.join(', ')} });`
300
307
  : '';
301
308
  return `
309
+ ${runtimeInit}
302
310
  ${importStatements.join('\n')}
303
311
  ${globalAssign}
304
312
  import endpoint from './api/${endpointName}';
@@ -1,67 +1,46 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createCaller = void 0;
4
- exports.wrapSdkCall = wrapSdkCall;
5
4
  exports.createTableClient = createTableClient;
6
- const env_js_1 = require("../internal/env.js");
7
- const ENVIRONMENTS = {
8
- production: 'https://workflows.zite.com',
9
- staging: 'https://workflows.zitestaging.com',
10
- local: 'http://localhost:2506',
11
- };
12
- function getExecutionConfig() {
13
- return globalThis.__ZITE_EXECUTION_CONFIG__;
14
- }
15
- function getRunnerUrl() {
16
- const config = getExecutionConfig();
17
- if (config?.workflowRunnerUrl)
18
- return config.workflowRunnerUrl;
19
- const env = (0, env_js_1.getEnv)('ZITE_ENV', 'VITE_ZITE_ENV') ?? 'production';
20
- return (0, env_js_1.getEnv)('ZITE_RUNNER_URL', 'VITE_ZITE_RUNNER_URL') ?? ENVIRONMENTS[env] ?? ENVIRONMENTS.production;
21
- }
22
- function getToken() {
23
- const config = getExecutionConfig();
24
- if (config?.token)
25
- return config.token;
26
- return (0, env_js_1.getEnv)('ZITE_DB_TOKEN', 'VITE_ZITE_DB_TOKEN') ?? '';
27
- }
28
- async function wrapSdkCall(integrationId, className, methodName, params) {
29
- const mergedParams = { tableId: className, ...params };
30
- const res = await fetch(`${getRunnerUrl()}/sdk/execute`, {
31
- method: 'POST',
32
- headers: {
33
- Authorization: `Bearer ${getToken()}`,
34
- 'Content-Type': 'application/json',
35
- },
36
- body: JSON.stringify({ integrationId, methodName, params: mergedParams }),
37
- });
38
- if (!res.ok) {
39
- const text = await res.text().catch(() => '');
40
- throw new Error(`SDK call failed (${res.status}): ${text}`);
5
+ exports.createSqlClient = createSqlClient;
6
+ function getSdkCall() {
7
+ const fn = globalThis.__wrapSdkCall;
8
+ if (!fn) {
9
+ throw new Error('Zite SDK runtime not initialized. Endpoints must run inside the Zite worker.');
41
10
  }
42
- return res.json();
11
+ return fn;
43
12
  }
44
13
  function createTableClient(integrationId, className) {
45
14
  return {
46
- findAll: (options) => wrapSdkCall(integrationId, className, 'findAll', options),
47
- findOne: (recordId) => wrapSdkCall(integrationId, className, 'findOne', typeof recordId === 'string' ? { id: recordId } : recordId),
48
- create: (data) => wrapSdkCall(integrationId, className, 'create', data),
49
- update: (recordId, data) => {
50
- if (typeof recordId === 'object') {
51
- return wrapSdkCall(integrationId, className, 'update', recordId);
52
- }
53
- return wrapSdkCall(integrationId, className, 'update', {
54
- id: recordId,
55
- ...(data && 'record' in data ? data.record : data),
56
- });
57
- },
58
- delete: (recordId) => wrapSdkCall(integrationId, className, 'delete', {
59
- id: typeof recordId === 'string' ? recordId : recordId.id,
15
+ findAll: (options) => getSdkCall()(integrationId, className, 'findAll', {
16
+ tableId: className,
17
+ ...options,
18
+ }),
19
+ findOne: (recordId) => getSdkCall()(integrationId, className, 'findOne', {
20
+ tableId: className,
21
+ ...(typeof recordId === 'string' ? { id: recordId } : recordId),
60
22
  }),
61
- bulkCreate: (records) => wrapSdkCall(integrationId, className, 'bulkCreate', {
23
+ create: (data) => getSdkCall()(integrationId, className, 'create', {
24
+ tableId: className,
25
+ record: data,
26
+ }),
27
+ update: (id, data) => getSdkCall()(integrationId, className, 'update', {
28
+ tableId: className,
29
+ id,
30
+ record: data,
31
+ }),
32
+ delete: (id) => getSdkCall()(integrationId, className, 'delete', {
33
+ tableId: className,
34
+ id,
35
+ }),
36
+ bulkCreate: (records) => getSdkCall()(integrationId, className, 'bulkCreate', {
37
+ tableId: className,
62
38
  records,
63
39
  }),
64
40
  };
65
41
  }
42
+ function createSqlClient(integrationId) {
43
+ return (params) => getSdkCall()(integrationId, 'Db', 'executeSql', params);
44
+ }
66
45
  var index_js_1 = require("../caller/index.js");
67
46
  Object.defineProperty(exports, "createCaller", { enumerable: true, get: function () { return index_js_1.createCaller; } });
@@ -64,12 +64,11 @@ function generateSchema(base) {
64
64
  function generateDbTs(base, schema, integrationId = 'databases') {
65
65
  const lines = [];
66
66
  lines.push('// Auto-generated by zitejs sync. Do not edit manually.');
67
- lines.push('// createTableClient<T> returns a typed client with:');
68
- lines.push('// .findAll(options?) → { records: T[], total: number, hasMore: boolean }');
69
- lines.push('// .findOne(id | filters) → T');
70
- lines.push('// .create(data) → T | .update(id, data) → T | .delete(id) → { deleted: true }');
71
- lines.push('// .bulkCreate(records) → T[]');
72
- lines.push("import { createTableClient, wrapSdkCall } from 'zitejs/runtime';");
67
+ lines.push('// Table methods: .findAll(opts?) → { records: T[], total, hasMore }');
68
+ lines.push('// .findOne(id) → T | .create(data) → T | .update(id, data) → T');
69
+ lines.push('// .delete(id) → { deleted: true } | .bulkCreate(records) → T[]');
70
+ lines.push('// Raw SQL: zite.sql({ query: "SELECT..." }) → { rows: Record<string, unknown>[] }');
71
+ lines.push("import { createTableClient, createSqlClient } from 'zitejs/runtime';");
73
72
  lines.push('');
74
73
  for (const table of base.tables ?? []) {
75
74
  const className = toPascalCase(table.name);
@@ -92,8 +91,7 @@ function generateDbTs(base, schema, integrationId = 'databases') {
92
91
  const propName = toCamelCase(table.name);
93
92
  lines.push(` ${propName}: createTableClient<${className}RecordType>('${integrationId}', '${className}'),`);
94
93
  }
95
- lines.push(" executeSql: (params: { query?: string; sql?: string; params?: unknown[] }) => wrapSdkCall('databases', 'Db', 'executeSql', params) as Promise<{ rows: Record<string, unknown>[] }>,");
96
- lines.push(" db: { executeSql: (params: { query?: string; sql?: string; params?: unknown[] }) => wrapSdkCall('databases', 'Db', 'executeSql', params) as Promise<{ rows: Record<string, unknown>[] }> },");
94
+ lines.push(` sql: createSqlClient('${integrationId}'),`);
97
95
  lines.push('};');
98
96
  lines.push('');
99
97
  return lines.join('\n');
@@ -101,15 +99,14 @@ function generateDbTs(base, schema, integrationId = 'databases') {
101
99
  function generateDbDts(base, schema) {
102
100
  const lines = [
103
101
  '// Auto-generated by zitejs sync. Do not edit manually.',
104
- '// This file contains only type declarations for LLM context.',
105
- '// See db.ts for the full runtime.',
102
+ '// Type declarations for IDE and LLM context. See db.ts for runtime.',
106
103
  '',
107
104
  'interface TableClient<T> {',
108
105
  ' findAll(options?: { limit?: number; offset?: number; sort?: unknown[]; filter?: unknown; filters?: unknown }): Promise<{ records: T[]; total: number; hasMore: boolean }>;',
109
106
  ' findOne(recordId: string | { filters?: unknown; filter?: unknown }): Promise<T>;',
110
- ' create(data: Partial<T> | { record: Partial<T> }): Promise<T>;',
111
- ' update(recordId: string | { id: string; record?: Partial<T> }, data?: Partial<T> | { record: Partial<T> }): Promise<T>;',
112
- ' delete(recordId: string | { id: string }): Promise<{ deleted: true }>;',
107
+ ' create(data: Partial<T>): Promise<T>;',
108
+ ' update(id: string, data: Partial<T>): Promise<T>;',
109
+ ' delete(id: string): Promise<{ deleted: true }>;',
113
110
  ' bulkCreate(records: Partial<T>[]): Promise<T[]>;',
114
111
  '}',
115
112
  '',
@@ -135,7 +132,7 @@ function generateDbDts(base, schema) {
135
132
  const propName = toCamelCase(table.name);
136
133
  lines.push(` ${propName}: TableClient<${className}RecordType>;`);
137
134
  }
138
- lines.push(' executeSql: (params: { query?: string; sql?: string; params?: unknown[] }) => Promise<{ rows: Record<string, unknown>[] }>;');
135
+ lines.push(' sql: (params: { query?: string; sql?: string; params?: unknown[] }) => Promise<{ rows: Record<string, unknown>[] }>;');
139
136
  lines.push('};');
140
137
  lines.push('');
141
138
  return lines.join('\n');
@@ -228,8 +228,14 @@ function createAliasPlugin(opts) {
228
228
  };
229
229
  }
230
230
  function generateEndpointWrapper(endpointName, usedImports, sdkExportKinds, sdkSource) {
231
+ // Always import the prebundled runtime to ensure globalThis.__wrapSdkCall
232
+ // is available for createTableClient/createSqlClient from zitejs/runtime.
233
+ const runtimeInit = `import { __wrapSdkCall, initRuntime } from '@zite/endpoints-runtime-sdk';
234
+ globalThis.__wrapSdkCall = __wrapSdkCall;
235
+ initRuntime();`;
231
236
  if (usedImports === null) {
232
237
  return `
238
+ ${runtimeInit}
233
239
  import * as sdk from '${sdkSource}';
234
240
  Object.assign(globalThis, sdk);
235
241
  import endpoint from './api/${endpointName}';
@@ -238,6 +244,7 @@ globalThis.__endpoint = endpoint;
238
244
  }
239
245
  if (usedImports.length === 0) {
240
246
  return `
247
+ ${runtimeInit}
241
248
  import endpoint from './api/${endpointName}';
242
249
  globalThis.__endpoint = endpoint;
243
250
  `;
@@ -263,6 +270,7 @@ globalThis.__endpoint = endpoint;
263
270
  ? `Object.assign(globalThis, { ${valueImports.join(', ')} });`
264
271
  : '';
265
272
  return `
273
+ ${runtimeInit}
266
274
  ${importStatements.join('\n')}
267
275
  ${globalAssign}
268
276
  import endpoint from './api/${endpointName}';
@@ -1,4 +1,3 @@
1
- export declare function wrapSdkCall(integrationId: string, className: string, methodName: string, params?: unknown): Promise<unknown>;
2
1
  export interface TableClient<T> {
3
2
  findAll(options?: {
4
3
  limit?: number;
@@ -15,22 +14,20 @@ export interface TableClient<T> {
15
14
  filters?: unknown;
16
15
  filter?: unknown;
17
16
  }): Promise<T>;
18
- create(data: Partial<T> | {
19
- record: Partial<T>;
20
- }): Promise<T>;
21
- update(recordId: string | {
22
- id: string;
23
- record?: Partial<T>;
24
- }, data?: Partial<T> | {
25
- record: Partial<T>;
26
- }): Promise<T>;
27
- delete(recordId: string | {
28
- id: string;
29
- }): Promise<{
17
+ create(data: Partial<T>): Promise<T>;
18
+ update(id: string, data: Partial<T>): Promise<T>;
19
+ delete(id: string): Promise<{
30
20
  deleted: true;
31
21
  }>;
32
22
  bulkCreate(records: Partial<T>[]): Promise<T[]>;
33
23
  }
34
24
  export declare function createTableClient<T>(integrationId: string, className: string): TableClient<T>;
25
+ export declare function createSqlClient(integrationId: string): (params: {
26
+ query?: string;
27
+ sql?: string;
28
+ params?: unknown[];
29
+ }) => Promise<{
30
+ rows: Record<string, unknown>[];
31
+ }>;
35
32
  export { createCaller } from '../caller/index.js';
36
33
  export type { EndpointConfig } from '../caller/index.js';
@@ -1,61 +1,40 @@
1
- import { getEnv } from '../internal/env.js';
2
- const ENVIRONMENTS = {
3
- production: 'https://workflows.zite.com',
4
- staging: 'https://workflows.zitestaging.com',
5
- local: 'http://localhost:2506',
6
- };
7
- function getExecutionConfig() {
8
- return globalThis.__ZITE_EXECUTION_CONFIG__;
9
- }
10
- function getRunnerUrl() {
11
- const config = getExecutionConfig();
12
- if (config?.workflowRunnerUrl)
13
- return config.workflowRunnerUrl;
14
- const env = getEnv('ZITE_ENV', 'VITE_ZITE_ENV') ?? 'production';
15
- return getEnv('ZITE_RUNNER_URL', 'VITE_ZITE_RUNNER_URL') ?? ENVIRONMENTS[env] ?? ENVIRONMENTS.production;
16
- }
17
- function getToken() {
18
- const config = getExecutionConfig();
19
- if (config?.token)
20
- return config.token;
21
- return getEnv('ZITE_DB_TOKEN', 'VITE_ZITE_DB_TOKEN') ?? '';
22
- }
23
- export async function wrapSdkCall(integrationId, className, methodName, params) {
24
- const mergedParams = { tableId: className, ...params };
25
- const res = await fetch(`${getRunnerUrl()}/sdk/execute`, {
26
- method: 'POST',
27
- headers: {
28
- Authorization: `Bearer ${getToken()}`,
29
- 'Content-Type': 'application/json',
30
- },
31
- body: JSON.stringify({ integrationId, methodName, params: mergedParams }),
32
- });
33
- if (!res.ok) {
34
- const text = await res.text().catch(() => '');
35
- throw new Error(`SDK call failed (${res.status}): ${text}`);
1
+ function getSdkCall() {
2
+ const fn = globalThis.__wrapSdkCall;
3
+ if (!fn) {
4
+ throw new Error('Zite SDK runtime not initialized. Endpoints must run inside the Zite worker.');
36
5
  }
37
- return res.json();
6
+ return fn;
38
7
  }
39
8
  export function createTableClient(integrationId, className) {
40
9
  return {
41
- findAll: (options) => wrapSdkCall(integrationId, className, 'findAll', options),
42
- findOne: (recordId) => wrapSdkCall(integrationId, className, 'findOne', typeof recordId === 'string' ? { id: recordId } : recordId),
43
- create: (data) => wrapSdkCall(integrationId, className, 'create', data),
44
- update: (recordId, data) => {
45
- if (typeof recordId === 'object') {
46
- return wrapSdkCall(integrationId, className, 'update', recordId);
47
- }
48
- return wrapSdkCall(integrationId, className, 'update', {
49
- id: recordId,
50
- ...(data && 'record' in data ? data.record : data),
51
- });
52
- },
53
- delete: (recordId) => wrapSdkCall(integrationId, className, 'delete', {
54
- id: typeof recordId === 'string' ? recordId : recordId.id,
10
+ findAll: (options) => getSdkCall()(integrationId, className, 'findAll', {
11
+ tableId: className,
12
+ ...options,
13
+ }),
14
+ findOne: (recordId) => getSdkCall()(integrationId, className, 'findOne', {
15
+ tableId: className,
16
+ ...(typeof recordId === 'string' ? { id: recordId } : recordId),
55
17
  }),
56
- bulkCreate: (records) => wrapSdkCall(integrationId, className, 'bulkCreate', {
18
+ create: (data) => getSdkCall()(integrationId, className, 'create', {
19
+ tableId: className,
20
+ record: data,
21
+ }),
22
+ update: (id, data) => getSdkCall()(integrationId, className, 'update', {
23
+ tableId: className,
24
+ id,
25
+ record: data,
26
+ }),
27
+ delete: (id) => getSdkCall()(integrationId, className, 'delete', {
28
+ tableId: className,
29
+ id,
30
+ }),
31
+ bulkCreate: (records) => getSdkCall()(integrationId, className, 'bulkCreate', {
32
+ tableId: className,
57
33
  records,
58
34
  }),
59
35
  };
60
36
  }
37
+ export function createSqlClient(integrationId) {
38
+ return (params) => getSdkCall()(integrationId, 'Db', 'executeSql', params);
39
+ }
61
40
  export { createCaller } from '../caller/index.js';
@@ -53,12 +53,11 @@ export function generateSchema(base) {
53
53
  export function generateDbTs(base, schema, integrationId = 'databases') {
54
54
  const lines = [];
55
55
  lines.push('// Auto-generated by zitejs sync. Do not edit manually.');
56
- lines.push('// createTableClient<T> returns a typed client with:');
57
- lines.push('// .findAll(options?) → { records: T[], total: number, hasMore: boolean }');
58
- lines.push('// .findOne(id | filters) → T');
59
- lines.push('// .create(data) → T | .update(id, data) → T | .delete(id) → { deleted: true }');
60
- lines.push('// .bulkCreate(records) → T[]');
61
- lines.push("import { createTableClient, wrapSdkCall } from 'zitejs/runtime';");
56
+ lines.push('// Table methods: .findAll(opts?) → { records: T[], total, hasMore }');
57
+ lines.push('// .findOne(id) → T | .create(data) → T | .update(id, data) → T');
58
+ lines.push('// .delete(id) → { deleted: true } | .bulkCreate(records) → T[]');
59
+ lines.push('// Raw SQL: zite.sql({ query: "SELECT..." }) → { rows: Record<string, unknown>[] }');
60
+ lines.push("import { createTableClient, createSqlClient } from 'zitejs/runtime';");
62
61
  lines.push('');
63
62
  for (const table of base.tables ?? []) {
64
63
  const className = toPascalCase(table.name);
@@ -81,8 +80,7 @@ export function generateDbTs(base, schema, integrationId = 'databases') {
81
80
  const propName = toCamelCase(table.name);
82
81
  lines.push(` ${propName}: createTableClient<${className}RecordType>('${integrationId}', '${className}'),`);
83
82
  }
84
- lines.push(" executeSql: (params: { query?: string; sql?: string; params?: unknown[] }) => wrapSdkCall('databases', 'Db', 'executeSql', params) as Promise<{ rows: Record<string, unknown>[] }>,");
85
- lines.push(" db: { executeSql: (params: { query?: string; sql?: string; params?: unknown[] }) => wrapSdkCall('databases', 'Db', 'executeSql', params) as Promise<{ rows: Record<string, unknown>[] }> },");
83
+ lines.push(` sql: createSqlClient('${integrationId}'),`);
86
84
  lines.push('};');
87
85
  lines.push('');
88
86
  return lines.join('\n');
@@ -90,15 +88,14 @@ export function generateDbTs(base, schema, integrationId = 'databases') {
90
88
  export function generateDbDts(base, schema) {
91
89
  const lines = [
92
90
  '// Auto-generated by zitejs sync. Do not edit manually.',
93
- '// This file contains only type declarations for LLM context.',
94
- '// See db.ts for the full runtime.',
91
+ '// Type declarations for IDE and LLM context. See db.ts for runtime.',
95
92
  '',
96
93
  'interface TableClient<T> {',
97
94
  ' findAll(options?: { limit?: number; offset?: number; sort?: unknown[]; filter?: unknown; filters?: unknown }): Promise<{ records: T[]; total: number; hasMore: boolean }>;',
98
95
  ' findOne(recordId: string | { filters?: unknown; filter?: unknown }): Promise<T>;',
99
- ' create(data: Partial<T> | { record: Partial<T> }): Promise<T>;',
100
- ' update(recordId: string | { id: string; record?: Partial<T> }, data?: Partial<T> | { record: Partial<T> }): Promise<T>;',
101
- ' delete(recordId: string | { id: string }): Promise<{ deleted: true }>;',
96
+ ' create(data: Partial<T>): Promise<T>;',
97
+ ' update(id: string, data: Partial<T>): Promise<T>;',
98
+ ' delete(id: string): Promise<{ deleted: true }>;',
102
99
  ' bulkCreate(records: Partial<T>[]): Promise<T[]>;',
103
100
  '}',
104
101
  '',
@@ -124,7 +121,7 @@ export function generateDbDts(base, schema) {
124
121
  const propName = toCamelCase(table.name);
125
122
  lines.push(` ${propName}: TableClient<${className}RecordType>;`);
126
123
  }
127
- lines.push(' executeSql: (params: { query?: string; sql?: string; params?: unknown[] }) => Promise<{ rows: Record<string, unknown>[] }>;');
124
+ lines.push(' sql: (params: { query?: string; sql?: string; params?: unknown[] }) => Promise<{ rows: Record<string, unknown>[] }>;');
128
125
  lines.push('};');
129
126
  lines.push('');
130
127
  return lines.join('\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zitejs",
3
- "version": "0.9.26",
3
+ "version": "0.9.28",
4
4
  "description": "The Zite framework — build apps on Zite Database",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/runtime/index.js",