@apso/cli 0.51.0 → 0.52.0

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.
@@ -20,7 +20,7 @@ class Generate extends base_command_1.default {
20
20
  const { flags } = await this.parse(Generate);
21
21
  const skipFormat = flags["skip-format"];
22
22
  const totalBuildStart = perf_hooks_1.performance.now();
23
- const { rootFolder, entities, relationshipMap, apiType, auth, emitEvents, http, coAuthor, language: configLanguage } = (0, lib_1.parseApsorc)();
23
+ const { rootFolder, entities, relationshipMap, apiType, auth, emitEvents, http, coAuthor, query, guards, language: configLanguage } = (0, lib_1.parseApsorc)();
24
24
  // Resolve language: flag > .apsorc > prompt
25
25
  let language;
26
26
  if (flags.language) {
@@ -180,6 +180,7 @@ class Generate extends base_command_1.default {
180
180
  apiType: lowerCaseApiType,
181
181
  relationshipMap,
182
182
  auth,
183
+ query,
183
184
  }));
184
185
  }
185
186
  // eslint-disable-next-line no-await-in-loop
@@ -209,7 +210,7 @@ class Generate extends base_command_1.default {
209
210
  await writeGenerated(file.path, file.content);
210
211
  }
211
212
  // Generate index module
212
- const indexFiles = await generator.generateIndexModule(entities, lowerCaseApiType, { emitEvents, http, auth });
213
+ const indexFiles = await generator.generateIndexModule(entities, lowerCaseApiType, { emitEvents, http, auth, guards });
213
214
  for (const file of indexFiles) {
214
215
  // eslint-disable-next-line no-await-in-loop
215
216
  await writeGenerated(file.path, file.content);
@@ -280,7 +280,7 @@ class McpServe extends base_command_1.default {
280
280
  isError: true,
281
281
  };
282
282
  }
283
- const { rootFolder, entities, relationshipMap, apiType, auth, language: configLanguage, } = (0, apsorc_parser_1.parseApsorc)();
283
+ const { rootFolder, entities, relationshipMap, apiType, auth, query, guards, language: configLanguage, } = (0, apsorc_parser_1.parseApsorc)();
284
284
  // Resolve language
285
285
  let lang;
286
286
  if (language) {
@@ -371,6 +371,7 @@ class McpServe extends base_command_1.default {
371
371
  apiType: lowerCaseApiType,
372
372
  relationshipMap,
373
373
  auth,
374
+ query,
374
375
  }),
375
376
  generator.generateModule({
376
377
  entity,
@@ -401,7 +402,7 @@ class McpServe extends base_command_1.default {
401
402
  if (guardFiles.length > 0)
402
403
  (0, crud_deps_1.writeAccessManifest)(process.cwd(), this.config.version);
403
404
  // Generate index module
404
- const indexFiles = await generator.generateIndexModule(entities, lowerCaseApiType, { auth });
405
+ const indexFiles = await generator.generateIndexModule(entities, lowerCaseApiType, { auth, guards });
405
406
  for (const file of indexFiles) {
406
407
  const fullPath = path.join(autogenPath, file.path);
407
408
  // eslint-disable-next-line no-await-in-loop
@@ -1,7 +1,7 @@
1
1
  import { Entity } from "./types/entity";
2
2
  import { ApsorcRelationship, Relationship, RelationshipMap } from "./types/relationship";
3
3
  import { AuthConfig } from "./types/auth";
4
- import { TargetLanguage } from "./types/generator";
4
+ import { QueryConfig, TargetLanguage } from "./types/generator";
5
5
  export declare enum ApiType {
6
6
  Graphql = "graphql",
7
7
  Rest = "rest"
@@ -31,6 +31,14 @@ export type ApsorcType = {
31
31
  * trailer. Defaults to enabled.
32
32
  */
33
33
  coAuthor?: boolean;
34
+ /** TypeScript list route settings; see QueryConfig. */
35
+ query?: QueryConfig;
36
+ /**
37
+ * TypeScript only. False leaves the generated GuardsModule out of the
38
+ * autogen module list, for an app that registers its own global guards.
39
+ * Not allowed with `auth`, whose checks run in those guards.
40
+ */
41
+ guards?: boolean;
34
42
  };
35
43
  type ParsedApsorcData = {
36
44
  entities: Entity[];
@@ -49,6 +57,8 @@ export type ParsedApsorc = {
49
57
  http?: boolean;
50
58
  /** Opt-out for the Apso commit co-author hook (defaults to enabled). */
51
59
  coAuthor?: boolean;
60
+ query?: QueryConfig;
61
+ guards?: boolean;
52
62
  };
53
63
  export declare const parseApsorcV1: (apsorc: ApsorcType) => ParsedApsorcData;
54
64
  /**
@@ -94,6 +104,8 @@ export declare const dedupeIndexes: (entity: Entity) => Entity;
94
104
  */
95
105
  export declare const dedupeOneToOneUniques: (entity: Entity, relationships?: Relationship[]) => Entity;
96
106
  export declare const parseApsorcV2: (apsorc: ApsorcType) => ParsedApsorcData;
107
+ /** Checks the .apsorc `query` block; undefined when it is left out. */
108
+ export declare function parseQuery(raw: unknown): QueryConfig | undefined;
97
109
  export declare const parseApsorc: () => ParsedApsorc;
98
110
  /**
99
111
  * Finds the .apsorc configuration file path.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findConfigPath = exports.readApsorcFile = exports.parseApsorc = exports.parseApsorcV2 = exports.dedupeOneToOneUniques = exports.dedupeIndexes = exports.assertCodegenSafeNames = exports.ApsorcNamingError = exports.parseApsorcV1 = exports.ApiType = void 0;
3
+ exports.findConfigPath = exports.readApsorcFile = exports.parseApsorc = exports.parseQuery = exports.parseApsorcV2 = exports.dedupeOneToOneUniques = exports.dedupeIndexes = exports.assertCodegenSafeNames = exports.ApsorcNamingError = exports.parseApsorcV1 = exports.ApiType = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fs = tslib_1.__importStar(require("fs"));
6
6
  const path = tslib_1.__importStar(require("path"));
@@ -136,6 +136,12 @@ const parseRc = () => {
136
136
  const emitEvents = apsoConfig.emitEvents;
137
137
  const http = apsoConfig.http;
138
138
  const coAuthor = apsoConfig.coAuthor;
139
+ const query = parseQuery(apsoConfig.query);
140
+ const guards = apsoConfig.guards;
141
+ if (guards !== undefined && typeof guards !== "boolean")
142
+ throw new Error(`.apsorc "guards" must be true or false`);
143
+ if (guards === false && auth)
144
+ throw new Error(`.apsorc "guards": false leaves out the guards that enforce "auth"; remove one of them`);
139
145
  return {
140
146
  rootFolder,
141
147
  apiType,
@@ -147,8 +153,37 @@ const parseRc = () => {
147
153
  emitEvents,
148
154
  http,
149
155
  coAuthor,
156
+ query,
157
+ guards,
150
158
  };
151
159
  };
160
+ const DIALECTS = new Set(["postgrest", "nestjsx"]);
161
+ /** Checks the .apsorc `query` block; undefined when it is left out. */
162
+ function parseQuery(raw) {
163
+ if (raw === undefined)
164
+ return undefined;
165
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw))
166
+ throw new Error(`.apsorc "query" must be an object`);
167
+ const { dialects, limit, alwaysPaginate, ...rest } = raw;
168
+ const unknown = Object.keys(rest);
169
+ if (unknown.length > 0)
170
+ throw new Error(`.apsorc "query" has unknown setting(s) ${unknown.join(", ")}; use dialects, limit or alwaysPaginate`);
171
+ if (dialects !== undefined &&
172
+ (!Array.isArray(dialects) ||
173
+ dialects.length === 0 ||
174
+ dialects.some((d) => !DIALECTS.has(d))))
175
+ throw new Error(`.apsorc "query.dialects" must list one or both of "postgrest" and "nestjsx"`);
176
+ if (limit !== undefined && (!Number.isInteger(limit) || limit < 1 || limit > 100))
177
+ throw new Error(`.apsorc "query.limit" must be a whole number from 1 to 100`);
178
+ if (alwaysPaginate !== undefined && typeof alwaysPaginate !== "boolean")
179
+ throw new Error(`.apsorc "query.alwaysPaginate" must be true or false`);
180
+ return {
181
+ dialects: dialects,
182
+ limit: limit,
183
+ alwaysPaginate: alwaysPaginate,
184
+ };
185
+ }
186
+ exports.parseQuery = parseQuery;
152
187
  const parseApsorc = () => {
153
188
  const debug = process.env.DEBUG;
154
189
  const start = perf_hooks_1.performance.now();
@@ -169,6 +204,8 @@ const parseApsorc = () => {
169
204
  emitEvents: apsoConfig.emitEvents,
170
205
  http: apsoConfig.http,
171
206
  coAuthor: apsoConfig.coAuthor,
207
+ query: apsoConfig.query,
208
+ guards: apsoConfig.guards,
172
209
  ...(0, exports.parseApsorcV1)(apsoConfig),
173
210
  };
174
211
  if (debug) {
@@ -188,6 +225,8 @@ const parseApsorc = () => {
188
225
  emitEvents: apsoConfig.emitEvents,
189
226
  http: apsoConfig.http,
190
227
  coAuthor: apsoConfig.coAuthor,
228
+ query: apsoConfig.query,
229
+ guards: apsoConfig.guards,
191
230
  ...(() => {
192
231
  const relStart = perf_hooks_1.performance.now();
193
232
  const parsed = (0, exports.parseApsorcV2)(apsoConfig);
@@ -16,9 +16,10 @@ export declare class TypeScriptGenerator extends BaseGenerator {
16
16
  generateModule(options: EntityGenerationOptions): Promise<GeneratedFile[]>;
17
17
  generateMigration(_options: MigrationGenerationOptions): Promise<GeneratedFile[]>;
18
18
  generateEnums(entities: Entity[], apiType: string): Promise<GeneratedFile[]>;
19
- generateIndexModule(entities: Entity[], apiType: string, _opts?: {
19
+ generateIndexModule(entities: Entity[], apiType: string, opts?: {
20
20
  emitEvents?: boolean;
21
21
  auth?: AuthConfig;
22
+ guards?: boolean;
22
23
  }): Promise<GeneratedFile[]>;
23
24
  generateGuards(entities: Entity[], auth?: AuthConfig, relationshipMap?: RelationshipMap): Promise<GeneratedFile[]>;
24
25
  /**
@@ -125,7 +125,7 @@ class TypeScriptGenerator extends base_1.BaseGenerator {
125
125
  ];
126
126
  }
127
127
  async generateController(options) {
128
- var _a;
128
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
129
129
  const { entity, relationships, relationshipMap, auth } = options;
130
130
  const { name: entityName } = entity;
131
131
  // Auth entities are for service callers only, so no other entity's
@@ -183,6 +183,10 @@ class TypeScriptGenerator extends base_1.BaseGenerator {
183
183
  mcp,
184
184
  mcpName: (0, casing_1.snakeCase)(entityName),
185
185
  mcpPluralName: (0, casing_1.snakeCase)(pluralEntityName),
186
+ dialects: (_c = (_b = options.query) === null || _b === void 0 ? void 0 : _b.dialects) !== null && _c !== void 0 ? _c : ["postgrest"],
187
+ dialectList: ((_e = (_d = options.query) === null || _d === void 0 ? void 0 : _d.dialects) !== null && _e !== void 0 ? _e : ["postgrest"]).map((d) => `'${d}'`).join(", "),
188
+ limit: (_g = (_f = options.query) === null || _f === void 0 ? void 0 : _f.limit) !== null && _g !== void 0 ? _g : 25,
189
+ alwaysPaginate: (_j = (_h = options.query) === null || _h === void 0 ? void 0 : _h.alwaysPaginate) !== null && _j !== void 0 ? _j : false,
186
190
  };
187
191
  const controllerContent = await this.renderTemplate("./rest/controller-rest", data);
188
192
  const specContent = await this.renderTemplate("./rest/controller-rest-spec", data);
@@ -355,10 +359,10 @@ class TypeScriptGenerator extends base_1.BaseGenerator {
355
359
  },
356
360
  ];
357
361
  }
358
- async generateIndexModule(entities, apiType, _opts) {
362
+ async generateIndexModule(entities, apiType, opts) {
359
363
  // Listing GuardsModule is what turns the global guards on; every service
360
- // has at least the API key check.
361
- const content = await this.renderTemplate(`./${apiType}/index-module-${apiType}`, { entities });
364
+ // has at least the API key check. `.apsorc` "guards": false leaves it out.
365
+ const content = await this.renderTemplate(`./${apiType}/index-module-${apiType}`, { entities, guards: (opts === null || opts === void 0 ? void 0 : opts.guards) !== false });
362
366
  return [
363
367
  {
364
368
  path: "index.ts",
@@ -49,9 +49,13 @@ const skipScope = (req: any) => {
49
49
  replace: <%= it.entityName %>Update,
50
50
  },
51
51
  validation: false,
52
+ <% if (it.dialects.length === 1 && it.dialects[0] === 'postgrest') { %>
52
53
  // List queries are PostgREST syntax only (?status=eq.open&select=id,name&order=id.desc).
53
54
  // A nestjsx parameter (filter, join, sort, s, fields, page, per_page, cache) gets a 400.
54
- dialects: ['postgrest'],
55
+ <% } else { %>
56
+ // Query syntaxes accepted, from .apsorc "query.dialects".
57
+ <% } %>
58
+ dialects: [<%~ it.dialectList %>],
55
59
  params: {
56
60
  id: {
57
61
  field: 'id',
@@ -63,7 +67,10 @@ const skipScope = (req: any) => {
63
67
  // The default page: a list with no limit returns at most this many
64
68
  // rows. When more exist the answer is a 206 and its Content-Range
65
69
  // header has the total (0-24/42). The largest limit is 100.
66
- limit: 25,
70
+ limit: <%= it.limit %>,
71
+ <% if (it.alwaysPaginate) { %>
72
+ alwaysPaginate: true,
73
+ <% } %>
67
74
  <% if (it.associations.length) { %>
68
75
  join: {
69
76
  <% it.associations.forEach((assoc) => { %>
@@ -3,11 +3,15 @@
3
3
  <% it.entities.forEach((entity) => { %>
4
4
  import {<%= entity.name %>Module} from './<%= entity.name %>/<%= entity.name %>.module'
5
5
  <%}) %>
6
+ <% if (it.guards) { %>
6
7
  import { GuardsModule } from './guards/guards.module'
8
+ <% } %>
7
9
 
8
10
  export default [
9
11
  <% it.entities.forEach((entity) => { %>
10
12
  <%= entity.name %>Module,
11
13
  <%}) %>
14
+ <% if (it.guards) { %>
12
15
  GuardsModule,
16
+ <% } %>
13
17
  ]
@@ -1,6 +1,19 @@
1
1
  import { Entity } from "./entity";
2
2
  import { AuthConfig } from "./auth";
3
3
  import { RelationshipMap, Relationship } from "./relationship";
4
+ /**
5
+ * .apsorc `query` (TypeScript REST only): how the generated list routes read a
6
+ * request. Leave it out for the defaults. An app whose clients already send
7
+ * nestjsx queries keeps them working with `"dialects": ["nestjsx", "postgrest"]`.
8
+ */
9
+ export interface QueryConfig {
10
+ /** Query syntaxes the list routes accept. Default `["postgrest"]`. */
11
+ dialects?: Array<"postgrest" | "nestjsx">;
12
+ /** Rows a list returns when the request names no limit. Default 25. */
13
+ limit?: number;
14
+ /** Answer every list with a page object instead of an array. Default false. */
15
+ alwaysPaginate?: boolean;
16
+ }
4
17
  /**
5
18
  * Supported target languages for code generation
6
19
  */
@@ -158,6 +171,8 @@ export interface ControllerGenerationOptions extends EntityGenerationOptions {
158
171
  * caller's tenant
159
172
  */
160
173
  auth?: AuthConfig;
174
+ /** The .apsorc `query` settings for list routes */
175
+ query?: QueryConfig;
161
176
  }
162
177
  /**
163
178
  * Options for generating services
@@ -260,6 +275,7 @@ export interface LanguageGenerator {
260
275
  emitEvents?: boolean;
261
276
  http?: boolean;
262
277
  auth?: AuthConfig;
278
+ guards?: boolean;
263
279
  }): Promise<GeneratedFile[]>;
264
280
  /**
265
281
  * Generate guard files (auth, scope, etc.)
@@ -4,4 +4,4 @@ export { Index } from "./indices";
4
4
  export { JSONValue } from "./json";
5
5
  export { RelationshipType, Association, ApsorcRelationship, Relationship, RelationshipForTemplate, RelationshipMap, } from "./relationship";
6
6
  export { AuthProviderType, BaseAuthConfig, DbSessionAuthConfig, JwtConfig, JwtClaimsMapping, JwtAuthConfig, AuthConfig, AuthProviderConfig, AuthContext, DB_SESSION_AUTH_DEFAULTS, JWT_AUTH_DEFAULTS, isDbSessionAuth, BetterAuthConfig, assertSignInProvider, ResolvedJwtAuth, resolveJwtAuth, authProviders, resolveJwtProviders, } from "./auth";
7
- export { TargetLanguage, GeneratedFile, ValidationResult, PythonLanguageConfig, GoLanguageConfig, TypeScriptLanguageConfig, LanguageConfig, GeneratorConfig, EntityGenerationOptions, ControllerGenerationOptions, ServiceGenerationOptions, DtoGenerationOptions, MigrationGenerationOptions, LanguageGenerator, GeneratorFactory, GeneratorRegistry, } from "./generator";
7
+ export { TargetLanguage, QueryConfig, GeneratedFile, ValidationResult, PythonLanguageConfig, GoLanguageConfig, TypeScriptLanguageConfig, LanguageConfig, GeneratorConfig, EntityGenerationOptions, ControllerGenerationOptions, ServiceGenerationOptions, DtoGenerationOptions, MigrationGenerationOptions, LanguageGenerator, GeneratorFactory, GeneratorRegistry, } from "./generator";
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@apso/cli",
3
- "version": "0.51.0",
3
+ "version": "0.52.0",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@apso/cli",
9
- "version": "0.51.0",
9
+ "version": "0.52.0",
10
10
  "license": "Apache-2.0",
11
11
  "dependencies": {
12
12
  "@apso/schema-tools": "^0.2.2",
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.51.0",
2
+ "version": "0.52.0",
3
3
  "commands": {
4
4
  "config": {
5
5
  "id": "config",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apso/cli",
3
- "version": "0.51.0",
3
+ "version": "0.52.0",
4
4
  "mcpName": "io.github.apsoai/apso",
5
5
  "description": "Apso CLI",
6
6
  "author": "Apso by Mavric - @mavric",