@orizm/cli 2.2.2 → 2.2.4
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/cli.cjs +420 -217
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +421 -218
- package/dist/cli.js.map +1 -1
- package/package.json +10 -12
package/dist/cli.js
CHANGED
|
@@ -14,7 +14,7 @@ async function loadConfig(configFile) {
|
|
|
14
14
|
bundle: true,
|
|
15
15
|
write: false,
|
|
16
16
|
format: "esm",
|
|
17
|
-
target: "
|
|
17
|
+
target: "node22",
|
|
18
18
|
platform: "node"
|
|
19
19
|
});
|
|
20
20
|
const moduleURL = `data:text/javascript,${encodeURIComponent(
|
|
@@ -28,9 +28,9 @@ async function loadConfig(configFile) {
|
|
|
28
28
|
import readline from "readline";
|
|
29
29
|
|
|
30
30
|
// src/lib/cli-symbol.ts
|
|
31
|
-
import
|
|
32
|
-
var success =
|
|
33
|
-
var error =
|
|
31
|
+
import chalk from "chalk";
|
|
32
|
+
var success = chalk.green("\u2713");
|
|
33
|
+
var error = chalk.red("\u2717");
|
|
34
34
|
|
|
35
35
|
// src/lib/cli-cursor.ts
|
|
36
36
|
function hide() {
|
|
@@ -97,11 +97,11 @@ function createSpinner() {
|
|
|
97
97
|
|
|
98
98
|
// src/lib/handle-error.ts
|
|
99
99
|
import * as v3 from "valibot";
|
|
100
|
-
import
|
|
100
|
+
import chalk3 from "chalk";
|
|
101
101
|
|
|
102
102
|
// src/lib/display-valibot-error.ts
|
|
103
103
|
import * as v2 from "valibot";
|
|
104
|
-
import
|
|
104
|
+
import chalk2 from "chalk";
|
|
105
105
|
function displayValibotError(error2) {
|
|
106
106
|
if (!v2.isValiError(error2)) {
|
|
107
107
|
return;
|
|
@@ -117,10 +117,10 @@ function buildValiErrorMessage(e, options) {
|
|
|
117
117
|
}).join("\n");
|
|
118
118
|
}
|
|
119
119
|
function getIssueMessage(message, color) {
|
|
120
|
-
return color ?
|
|
120
|
+
return color ? chalk2.red(message) : message;
|
|
121
121
|
}
|
|
122
122
|
function getLocationPath(path3, color) {
|
|
123
|
-
return color ?
|
|
123
|
+
return color ? chalk2.yellow(path3) : path3;
|
|
124
124
|
}
|
|
125
125
|
function getPath(issue) {
|
|
126
126
|
const issueDotPath = v2.getDotPath(issue);
|
|
@@ -164,7 +164,7 @@ function handleError(error2) {
|
|
|
164
164
|
}
|
|
165
165
|
console.error();
|
|
166
166
|
console.error(
|
|
167
|
-
|
|
167
|
+
chalk3.red(
|
|
168
168
|
"Something went wrong. Please check the error below for more details."
|
|
169
169
|
)
|
|
170
170
|
);
|
|
@@ -177,13 +177,6 @@ function handleError(error2) {
|
|
|
177
177
|
// src/cms/generate/generator.ts
|
|
178
178
|
import { mkdir, writeFile } from "fs/promises";
|
|
179
179
|
import path from "path";
|
|
180
|
-
import _Handlebars from "handlebars";
|
|
181
|
-
|
|
182
|
-
// src/cms/templates/types.ts.hbr
|
|
183
|
-
var types_ts_default = '// This file was automatically generated and should not be edited.\n\nimport { FormValidationRules, StorageItemType } from "@orizm/cms-sdk/common";\n\n// Utilities\ntype _ModuleRef<T> = T & _ModuleDescriptor;\ntype _ModuleDescriptor = {\n isModule: true;\n};\ntype _ArrayModuleRef<T> = T & _ArrayModuleDescriptor;\ntype _ArrayModuleDescriptor = {\n isModuleArray: true;\n};\n\n// Tables\n{{#each tables}}\nexport type {{toCodeTypeName this.name}} = {\n id: string;\n createdAt: string;\n updatedAt: string;\n {{#unless this.isSubmissions}}\n group: string | null;\n publishedAt: string | null;\n _draft: boolean | null;\n _published: boolean | null;\n scheduledPublishAt: string | null;\n scheduledUnpublishAt: string | null;\n {{else}}\n _formId: string;\n _content: unknown;\n _formSchema: unknown;\n _attachments: StorageItemType[];\n {{/unless}}\n {{#each this.columns}}\n {{this.name}}: {{typeName this}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};\n {{/each}}\n {{#if this.enableForm}}\n formSchema: FormValidationRules;\n {{/if}}\n};\n\nexport type {{toCodeTypeName this.name}}CreateInput = {\n {{#unless this.isSubmissions}}\n group?: string | null;\n publishedAt?: string | null;\n scheduledPublishAt?: string | null;\n scheduledUnpublishAt?: string | null;\n createdAt?: string;\n {{/unless}}\n {{#each this.columns}}\n {{this.name}}{{#unless this.required}}?{{/unless}}: {{typeName this "CreateInput"}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};\n {{/each}}\n {{#if this.enableForm}}\n formSchema: FormValidationRules;\n {{/if}}\n};\n\nexport type {{toCodeTypeName this.name}}UpdateInput = {\n {{#each this.columns}}\n {{#unless this.readOnly}}{{this.name}}?: {{typeName this "UpdateInput"}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};{{/unless}}\n {{/each}}\n {{#unless this.isSubmissions}}\n publishedAt?: string | null;\n scheduledPublishAt?: string | null;\n scheduledUnpublishAt?: string | null;\n createdAt?: string;\n {{/unless}}\n {{#if this.enableForm}}\n formSchema: FormValidationRules;\n {{/if}}\n};\n\n{{/each}}\n\n\n// Module types\n{{#each tableModules}}\nexport type {{toCodeTypeName this.name}} = {\n id: string;\n {{#each this.columns}}\n {{this.name}}: {{typeName this}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};\n {{/each}}\n};\n\nexport type {{toCodeTypeName this.name}}CreateInput = {\n {{#each this.columns}}\n {{this.name}}{{#unless this.required}}?{{/unless}}: {{typeName this "CreateInput"}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};\n {{/each}}\n};\n\nexport type {{toCodeTypeName this.name}}UpdateInput = {\n id?: string;\n {{#each this.columns}}\n {{this.name}}{{#unless this.required}}?{{/unless}}: {{typeName this}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};\n {{/each}}\n};\n\n{{/each}}\n\n\n// Descriptor types\n{{#each tables}}\nexport type {{toCodeTypeName this.name}}__Descriptor = {\n row: {{toCodeTypeName this.name}};\n create: {{toCodeTypeName this.name}}CreateInput;\n update: {{toCodeTypeName this.name}}UpdateInput;\n {{#if (filterRefColumn this.columns)}}\n refs: {\n {{#each (filterRefColumn this.columns)}}\n {{includeName this}}: {{{includeType this}}};\n {{/each}}\n }\n {{/if}}\n {{#unless (filterRefColumn this.columns)}}\n refs: object;\n {{/unless}}\n {{#if (filterUniqueIndexes this.indexes)}}\n unique: [\n {{#each (filterUniqueIndexes this.indexes)}}\n { {{#each (getIndexColumns ../this this)}}{{this.name}}:{{typeName this}}{{#unless this.required}}{{#if ../this.nullsNotDistinct}} | null{{/if}}{{/unless}},{{/each}} },\n {{/each}}\n ]\n {{/if}}\n {{#unless (filterUniqueIndexes this.indexes)}}\n unique: [];\n {{/unless}}\n {{#if this.enableDrafts}}\n enableDrafts: true;\n {{/if}}\n {{#if this.enableForm}}\n submissionDescriptor: {{toCodeTypeName this.name}}_Submission__Descriptor;\n {{else}}\n submissionDescriptor: never;\n {{/if}}\n};\n\n{{/each}}\n\n\n{{#each tableModules}}\nexport type {{toCodeTypeName this.name}}__Descriptor = {\n row: {{toCodeTypeName this.name}};\n {{#if (filterRefColumn this.columns)}}\n refs: {\n {{#each (filterRefColumn this.columns)}}\n {{includeName this}}: {{{includeType this}}};\n {{/each}}\n }\n {{/if}}\n {{#unless (filterRefColumn this.columns)}}\n refs: object;\n {{/unless}}\n unique:[]\n};\n{{/each}}\n\n\n// Webhook types\nexport type WebhookEvent = "content.create" | "content.update" | "content.delete";\n\ntype WebhookPayloadBase = {\n event: WebhookEvent;\n timestamp: string;\n project: string;\n};\n\n{{#if tables}}\nexport type WebhookPayload = WebhookPayloadBase & (\n {{#each tables}}\n | {\n table: "{{this.name}}";\n content: {{toCodeTypeName this.name}};\n old?: {{toCodeTypeName this.name}};\n }\n {{/each}}\n);\n{{else}}\nexport type WebhookPayload = WebhookPayloadBase;\n{{/if}}\n';
|
|
184
|
-
|
|
185
|
-
// src/cms/templates/client.ts.hbr
|
|
186
|
-
var client_ts_default = '// This file was automatically generated and should not be edited.\n\nimport { OrizmCMSClient } from "@orizm/cms-sdk";\n\nimport * as Models from "./types";\n\nexport class CMSClient {\n constructor(protected client: OrizmCMSClient) {\n this.tables = {\n {{#each tables}}\n {{toEndpointName this.name}}: this.client.getTableClient<Models.{{toCodeTypeName this.name}}__Descriptor>("{{toEndpointName this.name}}"),\n {{/each}}\n };\n\n this.buckets = {\n {{#each buckets}}\n {{toEndpointName this.name}}: this.client.getBucketClient("{{toEndpointName this.name}}"),\n {{/each}}\n };\n\n this.userManagement = this.client.userManagement;\n \n this.userinfo = this.client.userinfo;\n \n this.auth = this.client.auth;\n }\n\n readonly tables;\n readonly buckets;\n readonly userManagement;\n readonly userinfo;\n readonly auth;\n}\n';
|
|
187
180
|
|
|
188
181
|
// src/lib/format-ts-with-prettier.ts
|
|
189
182
|
import * as prettier from "prettier/standalone";
|
|
@@ -196,83 +189,150 @@ function formatTsWithPrettier(code) {
|
|
|
196
189
|
});
|
|
197
190
|
}
|
|
198
191
|
|
|
199
|
-
// src/
|
|
200
|
-
var Handlebars = _Handlebars.create();
|
|
201
|
-
Handlebars.registerHelper("toEndpointName", (str) => {
|
|
202
|
-
return str;
|
|
203
|
-
});
|
|
192
|
+
// src/lib/render-helpers.ts
|
|
204
193
|
function toCodeTypeName(str) {
|
|
205
194
|
str = str.charAt(0).toUpperCase() + str.slice(1);
|
|
206
195
|
return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
|
207
196
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
(column, _moduleSuffix) => {
|
|
212
|
-
const moduleSuffix = typeof _moduleSuffix === "string" ? _moduleSuffix : "";
|
|
213
|
-
if (column.type === "reference") {
|
|
197
|
+
function typeName(column, moduleSuffix = "") {
|
|
198
|
+
switch (column.type) {
|
|
199
|
+
case "reference":
|
|
214
200
|
return "string";
|
|
215
|
-
|
|
216
|
-
return toCodeTypeName(column.moduleName) + moduleSuffix;
|
|
217
|
-
|
|
218
|
-
return toCodeTypeName(column.moduleName) + moduleSuffix + "[]";
|
|
219
|
-
|
|
201
|
+
case "module":
|
|
202
|
+
return toCodeTypeName(column.moduleName ?? "") + moduleSuffix;
|
|
203
|
+
case "module-array":
|
|
204
|
+
return toCodeTypeName(column.moduleName ?? "") + moduleSuffix + "[]";
|
|
205
|
+
case "json":
|
|
220
206
|
return "unknown";
|
|
221
|
-
|
|
207
|
+
case "storage":
|
|
222
208
|
return "StorageItemType";
|
|
223
|
-
|
|
209
|
+
default:
|
|
224
210
|
return column.type;
|
|
225
|
-
}
|
|
226
211
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
"
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
)
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
"includeName
|
|
238
|
-
|
|
239
|
-
|
|
212
|
+
}
|
|
213
|
+
function nullableSuffix(column) {
|
|
214
|
+
return !column.required && column.type !== "module-array" ? " | null" : "";
|
|
215
|
+
}
|
|
216
|
+
function filterRefColumns(columns) {
|
|
217
|
+
return columns.filter(
|
|
218
|
+
(c) => c.type === "reference" && c.tableName !== void 0 && c.includeName !== void 0 && c.includeName !== "" || c.type === "module" && c.moduleName !== void 0 || c.type === "module-array" && c.moduleName !== void 0
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
function getIncludeName(column) {
|
|
222
|
+
return column.type === "reference" ? column.includeName || column.name : column.name;
|
|
223
|
+
}
|
|
224
|
+
function getIncludeType(column) {
|
|
225
|
+
if (column.type === "reference" && column.tableName) {
|
|
226
|
+
return toCodeTypeName(column.tableName) + "__Descriptor";
|
|
240
227
|
}
|
|
241
|
-
)
|
|
242
|
-
|
|
243
|
-
"includeType",
|
|
244
|
-
(column) => {
|
|
245
|
-
if (column.type === "reference")
|
|
246
|
-
return toCodeTypeName(column.tableName) + "__Descriptor";
|
|
247
|
-
if (column.type === "module")
|
|
248
|
-
return `_ModuleRef<${toCodeTypeName(column.moduleName)}__Descriptor>`;
|
|
249
|
-
if (column.type === "module-array")
|
|
250
|
-
return `_ArrayModuleRef<${toCodeTypeName(
|
|
251
|
-
column.moduleName
|
|
252
|
-
)}__Descriptor>`;
|
|
228
|
+
if (column.type === "module" && column.moduleName) {
|
|
229
|
+
return `_ModuleRef<${toCodeTypeName(column.moduleName)}__Descriptor>`;
|
|
253
230
|
}
|
|
254
|
-
)
|
|
255
|
-
|
|
256
|
-
"filterUniqueIndexes",
|
|
257
|
-
(indexes) => {
|
|
258
|
-
return (indexes ?? []).filter((i) => i.unique);
|
|
231
|
+
if (column.type === "module-array" && column.moduleName) {
|
|
232
|
+
return `_ArrayModuleRef<${toCodeTypeName(column.moduleName)}__Descriptor>`;
|
|
259
233
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
234
|
+
return "";
|
|
235
|
+
}
|
|
236
|
+
function renderColumnField(col, options) {
|
|
237
|
+
const optional2 = options?.optional === true ? "?" : "";
|
|
238
|
+
const suffix = options?.suffix ?? "";
|
|
239
|
+
return `${col.name}${optional2}: ${typeName(col, suffix)}${nullableSuffix(col)};`;
|
|
240
|
+
}
|
|
241
|
+
function renderTypeBlock(name, fields) {
|
|
242
|
+
return `export type ${name} = {
|
|
243
|
+
${fields.join("\n")}
|
|
244
|
+
};`;
|
|
245
|
+
}
|
|
246
|
+
function renderRefsFieldLines(columns) {
|
|
247
|
+
const refCols = filterRefColumns(columns);
|
|
248
|
+
if (refCols.length === 0) return ["refs: object;"];
|
|
249
|
+
return [
|
|
250
|
+
"refs: {",
|
|
251
|
+
...refCols.map((c) => `${getIncludeName(c)}: ${getIncludeType(c)};`),
|
|
252
|
+
"}"
|
|
253
|
+
];
|
|
254
|
+
}
|
|
255
|
+
function renderUniqueFieldLines(table) {
|
|
256
|
+
const uniqueIndexes = (table.indexes ?? []).filter((i) => i.unique);
|
|
257
|
+
if (uniqueIndexes.length === 0) return ["unique: [];"];
|
|
258
|
+
const entries = uniqueIndexes.map((index) => {
|
|
259
|
+
const cols = index.columns.map((colName) => {
|
|
260
|
+
const col = table.columns.find((c) => c.name === colName);
|
|
261
|
+
if (!col) throw new Error(`column ${colName} not found`);
|
|
262
|
+
const nullable = !col.required && index.nullsNotDistinct ? " | null" : "";
|
|
263
|
+
return `${col.name}:${typeName(col)}${nullable},`;
|
|
270
264
|
});
|
|
265
|
+
return `{ ${cols.join("")} },`;
|
|
266
|
+
});
|
|
267
|
+
return ["unique: [", ...entries, "]"];
|
|
268
|
+
}
|
|
269
|
+
function renderModuleRowTypeBlock(mod) {
|
|
270
|
+
return renderTypeBlock(toCodeTypeName(mod.name), [
|
|
271
|
+
"id: string;",
|
|
272
|
+
...mod.columns.map((col) => renderColumnField(col))
|
|
273
|
+
]);
|
|
274
|
+
}
|
|
275
|
+
function renderModuleDescriptorBlock(mod) {
|
|
276
|
+
const name = toCodeTypeName(mod.name);
|
|
277
|
+
return renderTypeBlock(`${name}__Descriptor`, [
|
|
278
|
+
`row: ${name};`,
|
|
279
|
+
...renderRefsFieldLines(mod.columns),
|
|
280
|
+
"unique: [];"
|
|
281
|
+
]);
|
|
282
|
+
}
|
|
283
|
+
function renderPreamble(importFrom) {
|
|
284
|
+
return `// This file was automatically generated and should not be edited.
|
|
285
|
+
|
|
286
|
+
import { FormValidationRules, StorageItemType } from "${importFrom}";
|
|
287
|
+
|
|
288
|
+
// Utilities
|
|
289
|
+
type _ModuleRef<T> = T & _ModuleDescriptor;
|
|
290
|
+
type _ModuleDescriptor = {
|
|
291
|
+
isModule: true;
|
|
292
|
+
};
|
|
293
|
+
type _ArrayModuleRef<T> = T & _ArrayModuleDescriptor;
|
|
294
|
+
type _ArrayModuleDescriptor = {
|
|
295
|
+
isModuleArray: true;
|
|
296
|
+
};`;
|
|
297
|
+
}
|
|
298
|
+
function renderSection(title, blocks) {
|
|
299
|
+
if (blocks.length === 0) return "";
|
|
300
|
+
return `// ${title}
|
|
301
|
+
${blocks.join("\n\n")}`;
|
|
302
|
+
}
|
|
303
|
+
function renderWebhookTypesSection(tables) {
|
|
304
|
+
const lines = [
|
|
305
|
+
"// Webhook types",
|
|
306
|
+
'export type WebhookEvent = "content.create" | "content.update" | "content.delete";',
|
|
307
|
+
"",
|
|
308
|
+
"type WebhookPayloadBase = {",
|
|
309
|
+
" event: WebhookEvent;",
|
|
310
|
+
" timestamp: string;",
|
|
311
|
+
" project: string;",
|
|
312
|
+
"};",
|
|
313
|
+
""
|
|
314
|
+
];
|
|
315
|
+
if (tables.length > 0) {
|
|
316
|
+
const unionMembers = tables.map((t) => {
|
|
317
|
+
const name = toCodeTypeName(t.name);
|
|
318
|
+
return `| {
|
|
319
|
+
table: "${t.name}";
|
|
320
|
+
content: ${name};
|
|
321
|
+
old?: ${name};
|
|
322
|
+
}`;
|
|
323
|
+
}).join("\n");
|
|
324
|
+
lines.push(
|
|
325
|
+
`export type WebhookPayload = WebhookPayloadBase & (
|
|
326
|
+
${unionMembers}
|
|
327
|
+
);`
|
|
328
|
+
);
|
|
329
|
+
} else {
|
|
330
|
+
lines.push("export type WebhookPayload = WebhookPayloadBase;");
|
|
271
331
|
}
|
|
272
|
-
);
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
332
|
+
return lines.join("\n");
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// src/cms/generate/generator.ts
|
|
276
336
|
async function generate(options) {
|
|
277
337
|
const config = options.config;
|
|
278
338
|
await mkdir(options.output, { recursive: true });
|
|
@@ -292,19 +352,143 @@ async function generate(options) {
|
|
|
292
352
|
async function renderTypes(config) {
|
|
293
353
|
const submissionTables = config.tables.filter((t) => t.enableForm).map((t) => ({
|
|
294
354
|
name: t.name + "__submission",
|
|
295
|
-
columns: t.formOptions?.submissionColumns
|
|
355
|
+
columns: t.formOptions?.submissionColumns ?? [],
|
|
296
356
|
isSubmissions: true
|
|
297
357
|
}));
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
358
|
+
const allTables = [...config.tables, ...submissionTables];
|
|
359
|
+
const modules = config.tableModules ?? [];
|
|
360
|
+
const source = [
|
|
361
|
+
renderPreamble("@orizm/cms-sdk/common"),
|
|
362
|
+
renderSection("Tables", allTables.map(renderTableTypes)),
|
|
363
|
+
renderSection("Module types", modules.map(renderModuleTypes)),
|
|
364
|
+
// CMS: モジュール descriptor 間は空行なし(スナップショット互換)
|
|
365
|
+
"// Descriptor types\n" + [
|
|
366
|
+
...allTables.map(renderTableDescriptor),
|
|
367
|
+
modules.map(renderModuleDescriptorBlock).join("\n")
|
|
368
|
+
].join("\n\n"),
|
|
369
|
+
renderWebhookTypesSection(allTables)
|
|
370
|
+
].filter(Boolean).join("\n\n\n");
|
|
371
|
+
return await formatTsWithPrettier(source);
|
|
372
|
+
}
|
|
373
|
+
function renderTableTypes(table) {
|
|
374
|
+
const cols = table.columns;
|
|
375
|
+
const name = toCodeTypeName(table.name);
|
|
376
|
+
const rowType = renderTypeBlock(name, [
|
|
377
|
+
"id: string;",
|
|
378
|
+
"createdAt: string;",
|
|
379
|
+
"updatedAt: string;",
|
|
380
|
+
...!table.isSubmissions ? [
|
|
381
|
+
"group: string | null;",
|
|
382
|
+
"publishedAt: string | null;",
|
|
383
|
+
"_draft: boolean | null;",
|
|
384
|
+
"_published: boolean | null;",
|
|
385
|
+
"scheduledPublishAt: string | null;",
|
|
386
|
+
"scheduledUnpublishAt: string | null;"
|
|
387
|
+
] : [
|
|
388
|
+
"_formId: string;",
|
|
389
|
+
"_content: unknown;",
|
|
390
|
+
"_formSchema: unknown;",
|
|
391
|
+
"_attachments: StorageItemType[];"
|
|
392
|
+
],
|
|
393
|
+
...cols.map((col) => renderColumnField(col)),
|
|
394
|
+
...table.enableForm ? ["formSchema: FormValidationRules;"] : []
|
|
395
|
+
]);
|
|
396
|
+
const createType = renderTypeBlock(`${name}CreateInput`, [
|
|
397
|
+
...!table.isSubmissions ? [
|
|
398
|
+
"group?: string | null;",
|
|
399
|
+
"publishedAt?: string | null;",
|
|
400
|
+
"scheduledPublishAt?: string | null;",
|
|
401
|
+
"scheduledUnpublishAt?: string | null;",
|
|
402
|
+
"createdAt?: string;"
|
|
403
|
+
] : [],
|
|
404
|
+
...cols.map(
|
|
405
|
+
(col) => renderColumnField(col, {
|
|
406
|
+
optional: !col.required,
|
|
407
|
+
suffix: "CreateInput"
|
|
408
|
+
})
|
|
409
|
+
),
|
|
410
|
+
...table.enableForm ? ["formSchema: FormValidationRules;"] : []
|
|
411
|
+
]);
|
|
412
|
+
const updateType = renderTypeBlock(`${name}UpdateInput`, [
|
|
413
|
+
...cols.filter((col) => !col.readOnly).map(
|
|
414
|
+
(col) => renderColumnField(col, { optional: true, suffix: "UpdateInput" })
|
|
415
|
+
),
|
|
416
|
+
...!table.isSubmissions ? [
|
|
417
|
+
"publishedAt?: string | null;",
|
|
418
|
+
"scheduledPublishAt?: string | null;",
|
|
419
|
+
"scheduledUnpublishAt?: string | null;",
|
|
420
|
+
"createdAt?: string;"
|
|
421
|
+
] : [],
|
|
422
|
+
...table.enableForm ? ["formSchema: FormValidationRules;"] : []
|
|
423
|
+
]);
|
|
424
|
+
return [rowType, createType, updateType].join("\n\n");
|
|
425
|
+
}
|
|
426
|
+
function renderModuleTypes(mod) {
|
|
427
|
+
const name = toCodeTypeName(mod.name);
|
|
428
|
+
const cols = mod.columns;
|
|
429
|
+
const rowType = renderModuleRowTypeBlock(mod);
|
|
430
|
+
const createType = renderTypeBlock(`${name}CreateInput`, [
|
|
431
|
+
...cols.map(
|
|
432
|
+
(col) => renderColumnField(col, {
|
|
433
|
+
optional: !col.required,
|
|
434
|
+
suffix: "CreateInput"
|
|
435
|
+
})
|
|
436
|
+
)
|
|
437
|
+
]);
|
|
438
|
+
const updateType = renderTypeBlock(`${name}UpdateInput`, [
|
|
439
|
+
"id?: string;",
|
|
440
|
+
...cols.map((col) => renderColumnField(col, { optional: !col.required }))
|
|
441
|
+
]);
|
|
442
|
+
return [rowType, createType, updateType].join("\n\n");
|
|
443
|
+
}
|
|
444
|
+
function renderTableDescriptor(table) {
|
|
445
|
+
const name = toCodeTypeName(table.name);
|
|
446
|
+
return renderTypeBlock(`${name}__Descriptor`, [
|
|
447
|
+
`row: ${name};`,
|
|
448
|
+
`create: ${name}CreateInput;`,
|
|
449
|
+
`update: ${name}UpdateInput;`,
|
|
450
|
+
...renderRefsFieldLines(table.columns),
|
|
451
|
+
...renderUniqueFieldLines(table),
|
|
452
|
+
...table.enableDrafts ? ["enableDrafts: true;"] : [],
|
|
453
|
+
...table.enableForm ? [`submissionDescriptor: ${name}_Submission__Descriptor;`] : ["submissionDescriptor: never;"]
|
|
454
|
+
]);
|
|
305
455
|
}
|
|
306
456
|
async function renderClient(config) {
|
|
307
|
-
|
|
457
|
+
const tableEntries = config.tables.map(
|
|
458
|
+
(t) => `${t.name}: this.client.getTableClient<Models.${toCodeTypeName(t.name)}__Descriptor>("${t.name}"),`
|
|
459
|
+
).join("\n");
|
|
460
|
+
const bucketEntries = (config.buckets ?? []).map((b) => `${b.name}: this.client.getBucketClient("${b.name}"),`).join("\n");
|
|
461
|
+
const source = `// This file was automatically generated and should not be edited.
|
|
462
|
+
|
|
463
|
+
import { OrizmCMSClient } from "@orizm/cms-sdk";
|
|
464
|
+
|
|
465
|
+
import * as Models from "./types";
|
|
466
|
+
|
|
467
|
+
export class CMSClient {
|
|
468
|
+
constructor(protected client: OrizmCMSClient) {
|
|
469
|
+
this.tables = {
|
|
470
|
+
${tableEntries}
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
this.buckets = {
|
|
474
|
+
${bucketEntries}
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
this.userManagement = this.client.userManagement;
|
|
478
|
+
|
|
479
|
+
this.userinfo = this.client.userinfo;
|
|
480
|
+
|
|
481
|
+
this.auth = this.client.auth;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
readonly tables;
|
|
485
|
+
readonly buckets;
|
|
486
|
+
readonly userManagement;
|
|
487
|
+
readonly userinfo;
|
|
488
|
+
readonly auth;
|
|
489
|
+
}
|
|
490
|
+
`;
|
|
491
|
+
return await formatTsWithPrettier(source);
|
|
308
492
|
}
|
|
309
493
|
|
|
310
494
|
// src/cms/generate/command.ts
|
|
@@ -336,7 +520,7 @@ function assertValidUrlOrigin(origin) {
|
|
|
336
520
|
}
|
|
337
521
|
}
|
|
338
522
|
|
|
339
|
-
// ../../node_modules/.pnpm/openapi-fetch@0.
|
|
523
|
+
// ../../node_modules/.pnpm/openapi-fetch@0.17.0/node_modules/openapi-fetch/dist/index.mjs
|
|
340
524
|
var PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
341
525
|
var supportsRequestInitExt = () => {
|
|
342
526
|
return typeof process === "object" && Number.parseInt(process?.versions?.node?.substring(0, 2)) >= 18 && process.versions.undici;
|
|
@@ -351,13 +535,14 @@ function createClient(clientOptions) {
|
|
|
351
535
|
fetch: baseFetch = globalThis.fetch,
|
|
352
536
|
querySerializer: globalQuerySerializer,
|
|
353
537
|
bodySerializer: globalBodySerializer,
|
|
538
|
+
pathSerializer: globalPathSerializer,
|
|
354
539
|
headers: baseHeaders,
|
|
355
540
|
requestInitExt = void 0,
|
|
356
541
|
...baseOptions
|
|
357
542
|
} = { ...clientOptions };
|
|
358
543
|
requestInitExt = supportsRequestInitExt() ? requestInitExt : void 0;
|
|
359
544
|
baseUrl = removeTrailingSlash(baseUrl);
|
|
360
|
-
const
|
|
545
|
+
const globalMiddlewares = [];
|
|
361
546
|
async function coreFetch(schemaPath, fetchOptions) {
|
|
362
547
|
const {
|
|
363
548
|
baseUrl: localBaseUrl,
|
|
@@ -368,11 +553,14 @@ function createClient(clientOptions) {
|
|
|
368
553
|
parseAs = "json",
|
|
369
554
|
querySerializer: requestQuerySerializer,
|
|
370
555
|
bodySerializer = globalBodySerializer ?? defaultBodySerializer,
|
|
556
|
+
pathSerializer: requestPathSerializer,
|
|
371
557
|
body,
|
|
558
|
+
middleware: requestMiddlewares = [],
|
|
372
559
|
...init
|
|
373
560
|
} = fetchOptions || {};
|
|
561
|
+
let finalBaseUrl = baseUrl;
|
|
374
562
|
if (localBaseUrl) {
|
|
375
|
-
|
|
563
|
+
finalBaseUrl = removeTrailingSlash(localBaseUrl) ?? baseUrl;
|
|
376
564
|
}
|
|
377
565
|
let querySerializer = typeof globalQuerySerializer === "function" ? globalQuerySerializer : createQuerySerializer(globalQuerySerializer);
|
|
378
566
|
if (requestQuerySerializer) {
|
|
@@ -381,40 +569,57 @@ function createClient(clientOptions) {
|
|
|
381
569
|
...requestQuerySerializer
|
|
382
570
|
});
|
|
383
571
|
}
|
|
384
|
-
const
|
|
385
|
-
const
|
|
572
|
+
const pathSerializer = requestPathSerializer || globalPathSerializer || defaultPathSerializer;
|
|
573
|
+
const serializedBody = body === void 0 ? void 0 : bodySerializer(
|
|
574
|
+
body,
|
|
575
|
+
// Note: we declare mergeHeaders() both here and below because it’s a bit of a chicken-or-egg situation:
|
|
576
|
+
// bodySerializer() needs all headers so we aren’t dropping ones set by the user, however,
|
|
577
|
+
// the result of this ALSO sets the lowest-priority content-type header. So we re-merge below,
|
|
578
|
+
// setting the content-type at the very beginning to be overwritten.
|
|
579
|
+
// Lastly, based on the way headers work, it’s not a simple “present-or-not” check becauase null intentionally un-sets headers.
|
|
580
|
+
mergeHeaders(baseHeaders, headers, params.header)
|
|
581
|
+
);
|
|
582
|
+
const finalHeaders = mergeHeaders(
|
|
386
583
|
// with no body, we should not to set Content-Type
|
|
387
584
|
serializedBody === void 0 || // if serialized body is FormData; browser will correctly set Content-Type & boundary expression
|
|
388
585
|
serializedBody instanceof FormData ? {} : {
|
|
389
586
|
"Content-Type": "application/json"
|
|
390
|
-
}
|
|
587
|
+
},
|
|
588
|
+
baseHeaders,
|
|
589
|
+
headers,
|
|
590
|
+
params.header
|
|
391
591
|
);
|
|
592
|
+
const finalMiddlewares = [...globalMiddlewares, ...requestMiddlewares];
|
|
392
593
|
const requestInit = {
|
|
393
594
|
redirect: "follow",
|
|
394
595
|
...baseOptions,
|
|
395
596
|
...init,
|
|
396
597
|
body: serializedBody,
|
|
397
|
-
headers:
|
|
598
|
+
headers: finalHeaders
|
|
398
599
|
};
|
|
399
600
|
let id;
|
|
400
601
|
let options;
|
|
401
|
-
let request = new
|
|
602
|
+
let request = new Request(
|
|
603
|
+
createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer, pathSerializer }),
|
|
604
|
+
requestInit
|
|
605
|
+
);
|
|
402
606
|
let response;
|
|
403
607
|
for (const key in init) {
|
|
404
608
|
if (!(key in request)) {
|
|
405
609
|
request[key] = init[key];
|
|
406
610
|
}
|
|
407
611
|
}
|
|
408
|
-
if (
|
|
612
|
+
if (finalMiddlewares.length) {
|
|
409
613
|
id = randomID();
|
|
410
614
|
options = Object.freeze({
|
|
411
|
-
baseUrl,
|
|
615
|
+
baseUrl: finalBaseUrl,
|
|
412
616
|
fetch,
|
|
413
617
|
parseAs,
|
|
414
618
|
querySerializer,
|
|
415
|
-
bodySerializer
|
|
619
|
+
bodySerializer,
|
|
620
|
+
pathSerializer
|
|
416
621
|
});
|
|
417
|
-
for (const m of
|
|
622
|
+
for (const m of finalMiddlewares) {
|
|
418
623
|
if (m && typeof m === "object" && typeof m.onRequest === "function") {
|
|
419
624
|
const result = await m.onRequest({
|
|
420
625
|
request,
|
|
@@ -424,7 +629,7 @@ function createClient(clientOptions) {
|
|
|
424
629
|
id
|
|
425
630
|
});
|
|
426
631
|
if (result) {
|
|
427
|
-
if (result instanceof
|
|
632
|
+
if (result instanceof Request) {
|
|
428
633
|
request = result;
|
|
429
634
|
} else if (result instanceof Response) {
|
|
430
635
|
response = result;
|
|
@@ -439,11 +644,11 @@ function createClient(clientOptions) {
|
|
|
439
644
|
if (!response) {
|
|
440
645
|
try {
|
|
441
646
|
response = await fetch(request, requestInitExt);
|
|
442
|
-
} catch (
|
|
443
|
-
let errorAfterMiddleware =
|
|
444
|
-
if (
|
|
445
|
-
for (let i =
|
|
446
|
-
const m =
|
|
647
|
+
} catch (error22) {
|
|
648
|
+
let errorAfterMiddleware = error22;
|
|
649
|
+
if (finalMiddlewares.length) {
|
|
650
|
+
for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
651
|
+
const m = finalMiddlewares[i];
|
|
447
652
|
if (m && typeof m === "object" && typeof m.onError === "function") {
|
|
448
653
|
const result = await m.onError({
|
|
449
654
|
request,
|
|
@@ -472,9 +677,9 @@ function createClient(clientOptions) {
|
|
|
472
677
|
throw errorAfterMiddleware;
|
|
473
678
|
}
|
|
474
679
|
}
|
|
475
|
-
if (
|
|
476
|
-
for (let i =
|
|
477
|
-
const m =
|
|
680
|
+
if (finalMiddlewares.length) {
|
|
681
|
+
for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
682
|
+
const m = finalMiddlewares[i];
|
|
478
683
|
if (m && typeof m === "object" && typeof m.onResponse === "function") {
|
|
479
684
|
const result = await m.onResponse({
|
|
480
685
|
request,
|
|
@@ -494,14 +699,22 @@ function createClient(clientOptions) {
|
|
|
494
699
|
}
|
|
495
700
|
}
|
|
496
701
|
}
|
|
497
|
-
|
|
702
|
+
const contentLength = response.headers.get("Content-Length");
|
|
703
|
+
if (response.status === 204 || request.method === "HEAD" || contentLength === "0" && !response.headers.get("Transfer-Encoding")?.includes("chunked")) {
|
|
498
704
|
return response.ok ? { data: void 0, response } : { error: void 0, response };
|
|
499
705
|
}
|
|
500
706
|
if (response.ok) {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
707
|
+
const getResponseData = async () => {
|
|
708
|
+
if (parseAs === "stream") {
|
|
709
|
+
return response.body;
|
|
710
|
+
}
|
|
711
|
+
if (parseAs === "json" && !contentLength) {
|
|
712
|
+
const raw = await response.text();
|
|
713
|
+
return raw ? JSON.parse(raw) : void 0;
|
|
714
|
+
}
|
|
715
|
+
return await response[parseAs]();
|
|
716
|
+
};
|
|
717
|
+
return { data: await getResponseData(), response };
|
|
505
718
|
}
|
|
506
719
|
let error2 = await response.text();
|
|
507
720
|
try {
|
|
@@ -555,15 +768,15 @@ function createClient(clientOptions) {
|
|
|
555
768
|
if (typeof m !== "object" || !("onRequest" in m || "onResponse" in m || "onError" in m)) {
|
|
556
769
|
throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse() or `onError()`");
|
|
557
770
|
}
|
|
558
|
-
|
|
771
|
+
globalMiddlewares.push(m);
|
|
559
772
|
}
|
|
560
773
|
},
|
|
561
774
|
/** Unregister middleware */
|
|
562
775
|
eject(...middleware) {
|
|
563
776
|
for (const m of middleware) {
|
|
564
|
-
const i =
|
|
777
|
+
const i = globalMiddlewares.indexOf(m);
|
|
565
778
|
if (i !== -1) {
|
|
566
|
-
|
|
779
|
+
globalMiddlewares.splice(i, 1);
|
|
567
780
|
}
|
|
568
781
|
}
|
|
569
782
|
}
|
|
@@ -729,16 +942,22 @@ function defaultPathSerializer(pathname, pathParams) {
|
|
|
729
942
|
}
|
|
730
943
|
return nextURL;
|
|
731
944
|
}
|
|
732
|
-
function defaultBodySerializer(body) {
|
|
945
|
+
function defaultBodySerializer(body, headers) {
|
|
733
946
|
if (body instanceof FormData) {
|
|
734
947
|
return body;
|
|
735
948
|
}
|
|
949
|
+
if (headers) {
|
|
950
|
+
const contentType = headers.get instanceof Function ? headers.get("Content-Type") ?? headers.get("content-type") : headers["Content-Type"] ?? headers["content-type"];
|
|
951
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
952
|
+
return new URLSearchParams(body).toString();
|
|
953
|
+
}
|
|
954
|
+
}
|
|
736
955
|
return JSON.stringify(body);
|
|
737
956
|
}
|
|
738
957
|
function createFinalURL(pathname, options) {
|
|
739
958
|
let finalURL = `${options.baseUrl}${pathname}`;
|
|
740
959
|
if (options.params?.path) {
|
|
741
|
-
finalURL =
|
|
960
|
+
finalURL = options.pathSerializer(finalURL, options.params.path);
|
|
742
961
|
}
|
|
743
962
|
let search = options.querySerializer(options.params.query ?? {});
|
|
744
963
|
if (search.startsWith("?")) {
|
|
@@ -926,16 +1145,6 @@ import { Command as Command3 } from "commander";
|
|
|
926
1145
|
// src/consumer/pull/generate.ts
|
|
927
1146
|
import { mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
|
|
928
1147
|
import path2 from "path";
|
|
929
|
-
import _Handlebars2 from "handlebars";
|
|
930
|
-
|
|
931
|
-
// src/consumer/templates/client.ts.hbr
|
|
932
|
-
var client_ts_default2 = '// This file was automatically generated and should not be edited.\n\nimport { ConsumerClientOptions, OrizmConsumerClientBase } from "@orizm/consumer-sdk";\nimport * as Models from "./types";\n\nexport class OrizmClient extends OrizmConsumerClientBase {\n constructor(options: ConsumerClientOptions) {\n super(options);\n }\n\n readonly tables = {\n {{#each tables}}\n {{toEndpointName this.name}}: this.getTableClient<Models.{{toCodeTypeName this.name}}__Descriptor>("{{toEndpointName this.name}}"),\n {{/each}}\n };\n}\n';
|
|
933
|
-
|
|
934
|
-
// src/consumer/templates/types.ts.hbr
|
|
935
|
-
var types_ts_default2 = '// This file was automatically generated and should not be edited.\n\nimport { FormValidationRules, StorageItemType } from "@orizm/consumer-sdk/common";\n\n// Utilities\ntype _ModuleRef<T> = T & _ModuleDescriptor;\ntype _ModuleDescriptor = {\n isModule: true;\n};\ntype _ArrayModuleRef<T> = T & _ArrayModuleDescriptor;\ntype _ArrayModuleDescriptor = {\n isModuleArray: true;\n};\n\n\n// Tables\n{{#each tables}}\nexport type {{toCodeTypeName this.name}} = {\n id: string;\n createdAt: string;\n updatedAt: string;\n group: string | null;\n publishedAt: string | null;\n _draft: boolean | null;\n _published: boolean | null;\n {{#each this.columns}}\n {{this.name}}: {{typeName this}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};\n {{/each}}\n {{#if this.enableForm}}\n formSchema: FormValidationRules;\n {{/if}}\n}\n\n{{/each}}\n\n{{#each tableModules}}\nexport type {{toCodeTypeName this.name}} = {\n id: string;\n {{#each this.columns}}\n {{this.name}}: {{typeName this}}{{#unless this.required}}{{#if_equal this.type "module-array"}}{{else}} | null{{/if_equal}}{{/unless}};\n {{/each}}\n};\n\n{{/each}}\n\n// Descriptor types\n{{#each tables}}\nexport type {{toCodeTypeName this.name}}__Descriptor = {\n row: {{toCodeTypeName this.name}};\n {{#if (filterRefColumn this.columns)}}\n refs: {\n {{#each (filterRefColumn this.columns)}}\n {{includeName this}}: {{{includeType this}}};\n {{/each}}\n }\n {{/if}}\n {{#unless (filterRefColumn this.columns)}}\n refs: object;\n {{/unless}}\n {{#if (filterUniqueIndexes this.indexes)}}\n unique: [\n {{#each (filterUniqueIndexes this.indexes)}}\n { {{#each (getIndexColumns ../this this)}}{{this.name}}:{{typeName this}}{{#unless this.required}}{{#if ../this.nullsNotDistinct}} | null{{/if}}{{/unless}},{{/each}} },\n {{/each}}\n ]\n {{/if}}\n {{#unless (filterUniqueIndexes this.indexes)}}\n unique: [];\n {{/unless}}\n {{#if this.enableDrafts}}\n enableDrafts: true;\n {{/if}}\n};\n\n{{/each}}\n\n\n{{#each tableModules}}\nexport type {{toCodeTypeName this.name}}__Descriptor = {\n row: {{toCodeTypeName this.name}};\n {{#if (filterRefColumn this.columns)}}\n refs: {\n {{#each (filterRefColumn this.columns)}}\n {{includeName this}}: {{{includeType this}}};\n {{/each}}\n }\n {{/if}}\n {{#unless (filterRefColumn this.columns)}}\n refs: object;\n {{/unless}}\n unique:[]\n};\n\n{{/each}}\n\n\n// Webhook types\nexport type WebhookEvent = "content.create" | "content.update" | "content.delete";\n\ntype WebhookPayloadBase = {\n event: WebhookEvent;\n timestamp: string;\n project: string;\n};\n\n{{#if tables}}\nexport type WebhookPayload = WebhookPayloadBase & (\n {{#each tables}}\n | {\n table: "{{this.name}}";\n content: {{toCodeTypeName this.name}};\n old?: {{toCodeTypeName this.name}};\n }\n {{/each}}\n);\n{{else}}\nexport type WebhookPayload = WebhookPayloadBase;\n{{/if}}\n';
|
|
936
|
-
|
|
937
|
-
// src/consumer/pull/generate.ts
|
|
938
|
-
var Handlebars2 = _Handlebars2.create();
|
|
939
1148
|
async function generate2(options) {
|
|
940
1149
|
await mkdir2(options.output, { recursive: true });
|
|
941
1150
|
const renderedTypes = await renderTypes2(options.definition);
|
|
@@ -945,80 +1154,63 @@ async function generate2(options) {
|
|
|
945
1154
|
const targetClientPath = path2.join(options.output, "client.ts");
|
|
946
1155
|
await writeFile2(targetClientPath, renderedClient, "utf-8");
|
|
947
1156
|
}
|
|
948
|
-
Handlebars2.registerHelper("toEndpointName", (str) => {
|
|
949
|
-
return str;
|
|
950
|
-
});
|
|
951
|
-
function toCodeTypeName2(str) {
|
|
952
|
-
str = str.charAt(0).toUpperCase() + str.slice(1);
|
|
953
|
-
return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
|
954
|
-
}
|
|
955
|
-
Handlebars2.registerHelper("toCodeTypeName", toCodeTypeName2);
|
|
956
|
-
Handlebars2.registerHelper("typeName", (column) => {
|
|
957
|
-
if (column.type === "reference") {
|
|
958
|
-
return "string";
|
|
959
|
-
} else if (column.type === "json") {
|
|
960
|
-
return "unknown";
|
|
961
|
-
} else if (column.type === "module") {
|
|
962
|
-
if (!column.moduleName) {
|
|
963
|
-
console.warn("moduleName is not defined", column.name);
|
|
964
|
-
}
|
|
965
|
-
return toCodeTypeName2(column.moduleName || "unknown");
|
|
966
|
-
} else if (column.type === "module-array") {
|
|
967
|
-
if (!column.moduleName) {
|
|
968
|
-
console.warn("moduleName is not defined", column.name);
|
|
969
|
-
}
|
|
970
|
-
return toCodeTypeName2(column.moduleName || "unknown") + "[]";
|
|
971
|
-
} else if (column.type === "storage") {
|
|
972
|
-
return "StorageItemType";
|
|
973
|
-
} else {
|
|
974
|
-
return column.type;
|
|
975
|
-
}
|
|
976
|
-
});
|
|
977
|
-
Handlebars2.registerHelper(
|
|
978
|
-
"filterRefColumn",
|
|
979
|
-
(columns) => {
|
|
980
|
-
return (columns ?? []).filter(
|
|
981
|
-
(c) => c.type === "reference" && c.tableName && c.includeName || c.type === "module" && c.moduleName || c.type === "module-array" && c.moduleName
|
|
982
|
-
);
|
|
983
|
-
}
|
|
984
|
-
);
|
|
985
|
-
Handlebars2.registerHelper("includeName", (column) => {
|
|
986
|
-
return column.type === "reference" ? column.includeName : column.name;
|
|
987
|
-
});
|
|
988
|
-
Handlebars2.registerHelper("includeType", (column) => {
|
|
989
|
-
if (column.type === "reference" && column.tableName)
|
|
990
|
-
return toCodeTypeName2(column.tableName) + "__Descriptor";
|
|
991
|
-
if (column.type === "module" && column.moduleName)
|
|
992
|
-
return `_ModuleRef<${toCodeTypeName2(column.moduleName)}__Descriptor>`;
|
|
993
|
-
if (column.type === "module-array" && column.moduleName)
|
|
994
|
-
return `_ArrayModuleRef<${toCodeTypeName2(column.moduleName)}__Descriptor>`;
|
|
995
|
-
});
|
|
996
|
-
Handlebars2.registerHelper(
|
|
997
|
-
"filterUniqueIndexes",
|
|
998
|
-
(indexes) => {
|
|
999
|
-
return (indexes ?? []).filter((i) => i.unique);
|
|
1000
|
-
}
|
|
1001
|
-
);
|
|
1002
|
-
Handlebars2.registerHelper(
|
|
1003
|
-
"getIndexColumns",
|
|
1004
|
-
(table, index) => {
|
|
1005
|
-
return index.columns.map((i) => {
|
|
1006
|
-
const column = table.columns.find((c) => c.name === i);
|
|
1007
|
-
if (!column) {
|
|
1008
|
-
throw new Error(`column ${i} not found`);
|
|
1009
|
-
}
|
|
1010
|
-
return column;
|
|
1011
|
-
});
|
|
1012
|
-
}
|
|
1013
|
-
);
|
|
1014
|
-
Handlebars2.registerHelper("if_equal", function(a, b, options) {
|
|
1015
|
-
return a === b ? options.fn(this) : options.inverse(this);
|
|
1016
|
-
});
|
|
1017
1157
|
async function renderTypes2(config) {
|
|
1018
|
-
|
|
1158
|
+
const tables = config.tables;
|
|
1159
|
+
const modules = config.tableModules ?? [];
|
|
1160
|
+
const source = [
|
|
1161
|
+
renderPreamble("@orizm/consumer-sdk/common"),
|
|
1162
|
+
renderSection("Tables", tables.map(renderTableRowType)),
|
|
1163
|
+
modules.map(renderModuleRowTypeBlock).join("\n\n"),
|
|
1164
|
+
renderSection("Descriptor types", [
|
|
1165
|
+
...tables.map(renderTableDescriptor2),
|
|
1166
|
+
...modules.map(renderModuleDescriptorBlock)
|
|
1167
|
+
]),
|
|
1168
|
+
renderWebhookTypesSection(tables)
|
|
1169
|
+
].filter(Boolean).join("\n\n\n");
|
|
1170
|
+
return await formatTsWithPrettier(source);
|
|
1171
|
+
}
|
|
1172
|
+
function renderTableRowType(table) {
|
|
1173
|
+
return renderTypeBlock(toCodeTypeName(table.name), [
|
|
1174
|
+
"id: string;",
|
|
1175
|
+
"createdAt: string;",
|
|
1176
|
+
"updatedAt: string;",
|
|
1177
|
+
"group: string | null;",
|
|
1178
|
+
"publishedAt: string | null;",
|
|
1179
|
+
"_draft: boolean | null;",
|
|
1180
|
+
"_published: boolean | null;",
|
|
1181
|
+
...table.columns.map((col) => renderColumnField(col)),
|
|
1182
|
+
...table.enableForm ? ["formSchema: FormValidationRules;"] : []
|
|
1183
|
+
]);
|
|
1184
|
+
}
|
|
1185
|
+
function renderTableDescriptor2(table) {
|
|
1186
|
+
const name = toCodeTypeName(table.name);
|
|
1187
|
+
return renderTypeBlock(`${name}__Descriptor`, [
|
|
1188
|
+
`row: ${name};`,
|
|
1189
|
+
...renderRefsFieldLines(table.columns),
|
|
1190
|
+
...renderUniqueFieldLines(table),
|
|
1191
|
+
...table.enableDrafts ? ["enableDrafts: true;"] : []
|
|
1192
|
+
]);
|
|
1019
1193
|
}
|
|
1020
1194
|
async function renderClient2(config) {
|
|
1021
|
-
|
|
1195
|
+
const tableEntries = config.tables.map(
|
|
1196
|
+
(t) => `${t.name}: this.getTableClient<Models.${toCodeTypeName(t.name)}__Descriptor>("${t.name}"),`
|
|
1197
|
+
).join("\n");
|
|
1198
|
+
const source = `// This file was automatically generated and should not be edited.
|
|
1199
|
+
|
|
1200
|
+
import { ConsumerClientOptions, OrizmConsumerClientBase } from "@orizm/consumer-sdk";
|
|
1201
|
+
import * as Models from "./types";
|
|
1202
|
+
|
|
1203
|
+
export class OrizmClient extends OrizmConsumerClientBase {
|
|
1204
|
+
constructor(options: ConsumerClientOptions) {
|
|
1205
|
+
super(options);
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
readonly tables = {
|
|
1209
|
+
${tableEntries}
|
|
1210
|
+
};
|
|
1211
|
+
}
|
|
1212
|
+
`;
|
|
1213
|
+
return await formatTsWithPrettier(source);
|
|
1022
1214
|
}
|
|
1023
1215
|
|
|
1024
1216
|
// src/consumer/generate/action.ts
|
|
@@ -1082,10 +1274,10 @@ var consumerPullCommand = new Command4("pull").description("pull orizm schema an
|
|
|
1082
1274
|
|
|
1083
1275
|
// src/push/command.ts
|
|
1084
1276
|
import { Command as Command5 } from "commander";
|
|
1085
|
-
import
|
|
1277
|
+
import chalk6 from "chalk";
|
|
1086
1278
|
|
|
1087
1279
|
// src/lib/developer-api.ts
|
|
1088
|
-
import
|
|
1280
|
+
import chalk4 from "chalk";
|
|
1089
1281
|
async function applyDefinition(env, projectDef) {
|
|
1090
1282
|
const client = createDeveloperAPIv2Client({
|
|
1091
1283
|
serverOrigin: env.ORIZM_BASE_URL,
|
|
@@ -1105,7 +1297,7 @@ async function applyDefinition(env, projectDef) {
|
|
|
1105
1297
|
);
|
|
1106
1298
|
}
|
|
1107
1299
|
if (response.error.type === "bad-request") {
|
|
1108
|
-
console.log(
|
|
1300
|
+
console.log(chalk4.red(response.error.detail));
|
|
1109
1301
|
process.exit(1);
|
|
1110
1302
|
}
|
|
1111
1303
|
throw new Error(
|
|
@@ -1143,7 +1335,7 @@ var getChangeEvents = async (env, projectDef) => {
|
|
|
1143
1335
|
};
|
|
1144
1336
|
|
|
1145
1337
|
// src/lib/display-change-events.ts
|
|
1146
|
-
import
|
|
1338
|
+
import chalk5 from "chalk";
|
|
1147
1339
|
|
|
1148
1340
|
// src/lib/cli-table.ts
|
|
1149
1341
|
var COLORS = {
|
|
@@ -1252,7 +1444,7 @@ function displayChangeEvents(changeEvents) {
|
|
|
1252
1444
|
} = changeEvents;
|
|
1253
1445
|
if (tableChangeEvents.length === 0 && bucketChangeEvents.length === 0 && authorityChangeEvents.length === 0) {
|
|
1254
1446
|
console.log();
|
|
1255
|
-
console.log(
|
|
1447
|
+
console.log(chalk5.green("No changes."));
|
|
1256
1448
|
return;
|
|
1257
1449
|
}
|
|
1258
1450
|
displayTableChangeEvents(tableChangeEvents);
|
|
@@ -1502,7 +1694,7 @@ function displayAuthorityChangeEvents(authorityChangeEvents) {
|
|
|
1502
1694
|
}
|
|
1503
1695
|
|
|
1504
1696
|
// src/lib/cli-input.ts
|
|
1505
|
-
import { createInterface } from "
|
|
1697
|
+
import { createInterface } from "readline/promises";
|
|
1506
1698
|
async function input(prompt) {
|
|
1507
1699
|
const rl = createInterface({
|
|
1508
1700
|
input: process.stdin,
|
|
@@ -1538,7 +1730,7 @@ var pushCommand = new Command5("push").description("apply schema to project.").o
|
|
|
1538
1730
|
if (!options.yes) {
|
|
1539
1731
|
const confirm = await input("Do you want to apply schema? (y/N): ");
|
|
1540
1732
|
if (confirm !== "y") {
|
|
1541
|
-
console.log(
|
|
1733
|
+
console.log(chalk6.red("Canceled."));
|
|
1542
1734
|
return;
|
|
1543
1735
|
}
|
|
1544
1736
|
}
|
|
@@ -1553,7 +1745,7 @@ var pushCommand = new Command5("push").description("apply schema to project.").o
|
|
|
1553
1745
|
|
|
1554
1746
|
// src/config/validate/command.ts
|
|
1555
1747
|
import { Command as Command6 } from "commander";
|
|
1556
|
-
import
|
|
1748
|
+
import chalk7 from "chalk";
|
|
1557
1749
|
import * as v5 from "valibot";
|
|
1558
1750
|
|
|
1559
1751
|
// src/config/validate/watcher.ts
|
|
@@ -1609,10 +1801,10 @@ var validateConfigCommand = new Command6("validate").description("validate orizm
|
|
|
1609
1801
|
console.log();
|
|
1610
1802
|
}
|
|
1611
1803
|
await loadConfig(options.config);
|
|
1612
|
-
console.log(
|
|
1804
|
+
console.log(chalk7.green("\u2705 Config is valid."));
|
|
1613
1805
|
} catch (error2) {
|
|
1614
1806
|
if (v5.isValiError(error2)) {
|
|
1615
|
-
console.error(
|
|
1807
|
+
console.error(chalk7.red("\u274C Config validation failed:"));
|
|
1616
1808
|
displayValibotError(error2);
|
|
1617
1809
|
return;
|
|
1618
1810
|
}
|
|
@@ -1632,16 +1824,27 @@ var validateConfigCommand = new Command6("validate").description("validate orizm
|
|
|
1632
1824
|
import { Command as Command7 } from "commander";
|
|
1633
1825
|
|
|
1634
1826
|
// src/config/generate-erd/generate.ts
|
|
1635
|
-
|
|
1827
|
+
function generate3(input2) {
|
|
1828
|
+
const tables = input2.tables.map((table) => {
|
|
1829
|
+
const columns = [
|
|
1830
|
+
" id string PK",
|
|
1831
|
+
...table.columns.map(
|
|
1832
|
+
(col) => ` ${col.name} ${col.type}${col.isForeignKey ? " FK" : ""}`
|
|
1833
|
+
)
|
|
1834
|
+
].join("\n");
|
|
1835
|
+
return ` ${table.name} {
|
|
1836
|
+
${columns}
|
|
1837
|
+
}`;
|
|
1838
|
+
}).join("\n");
|
|
1839
|
+
const relations = input2.relations.map((rel) => ` ${rel.from} ${rel.type} ${rel.to} : ${rel.comment}`).join("\n");
|
|
1840
|
+
return `generated by @orizm/cli
|
|
1636
1841
|
|
|
1637
|
-
|
|
1638
|
-
|
|
1842
|
+
\`\`\`mermaid
|
|
1843
|
+
erDiagram
|
|
1844
|
+
${tables}
|
|
1639
1845
|
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
function generate3(input2) {
|
|
1643
|
-
const template = Handlebars3.compile(mermaid_erd_default);
|
|
1644
|
-
return template(input2);
|
|
1846
|
+
${relations}
|
|
1847
|
+
\`\`\``;
|
|
1645
1848
|
}
|
|
1646
1849
|
|
|
1647
1850
|
// src/config/generate-erd/convert-to-input.ts
|