@zenstackhq/server 3.5.0-beta.3 → 3.5.0-beta.5

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/api.d.cts CHANGED
@@ -1,6 +1,37 @@
1
+ import { QueryOptions } from '@zenstackhq/orm';
1
2
  import { SchemaDef } from '@zenstackhq/orm/schema';
3
+ import * as openapi_types from 'openapi-types';
4
+ import { OpenAPIV3_1 } from 'openapi-types';
2
5
  import { A as ApiHandler, L as LogConfig, R as RequestContext, a as Response } from './types-CU_PWKr9.cjs';
3
- import '@zenstackhq/orm';
6
+
7
+ type CommonHandlerOptions<Schema extends SchemaDef> = {
8
+ /** Query options that affect the behavior of the OpenAPI provider. */
9
+ queryOptions?: QueryOptions<Schema>;
10
+ };
11
+ type OpenApiSpecOptions = {
12
+ /** Spec title. Defaults to 'ZenStack Generated API' */
13
+ title?: string;
14
+ /** Spec version. Defaults to '1.0.0' */
15
+ version?: string;
16
+ /** Spec description. */
17
+ description?: string;
18
+ /** Spec summary. */
19
+ summary?: string;
20
+ /**
21
+ * When true, assumes that the schema includes access policies, and adds
22
+ * 403 responses to operations that can potentially be rejected.
23
+ */
24
+ respectAccessPolicies?: boolean;
25
+ };
26
+ /**
27
+ * Interface for generating OpenAPI specifications.
28
+ */
29
+ interface OpenApiSpecGenerator {
30
+ /**
31
+ * Generates an OpenAPI v3.1 specification document.
32
+ */
33
+ generateSpec(options?: OpenApiSpecOptions): Promise<OpenAPIV3_1.Document>;
34
+ }
4
35
 
5
36
  /**
6
37
  * Options for {@link RestApiHandler}
@@ -45,11 +76,19 @@ type RestApiHandlerOptions<Schema extends SchemaDef = SchemaDef> = {
45
76
  * Mapping from model names to unique field name to be used as resource's ID.
46
77
  */
47
78
  externalIdMapping?: Record<string, string>;
48
- };
79
+ /**
80
+ * When `true`, enables nested route handling for all to-many relations:
81
+ * `/:parentType/:parentId/:relationName` (collection) and
82
+ * `/:parentType/:parentId/:relationName/:childId` (single).
83
+ *
84
+ * Defaults to `false`.
85
+ */
86
+ nestedRoutes?: boolean;
87
+ } & CommonHandlerOptions<Schema>;
49
88
  /**
50
89
  * RESTful-style API request handler (compliant with JSON:API)
51
90
  */
52
- declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements ApiHandler<Schema> {
91
+ declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements ApiHandler<Schema>, OpenApiSpecGenerator {
53
92
  private readonly options;
54
93
  private serializers;
55
94
  private readonly errors;
@@ -64,22 +103,60 @@ declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Ap
64
103
  private modelNameMapping;
65
104
  private reverseModelNameMapping;
66
105
  private externalIdMapping;
106
+ private nestedRoutes;
67
107
  constructor(options: RestApiHandlerOptions<Schema>);
68
108
  private validateOptions;
69
109
  get schema(): Schema;
70
110
  get log(): LogConfig | undefined;
71
111
  private buildUrlPatternMap;
72
112
  private mapModelName;
113
+ /**
114
+ * Resolves child model type and reverse relation from a parent relation name.
115
+ * e.g. given parentType='user', parentRelation='posts', returns { childType:'post', reverseRelation:'author' }
116
+ */
117
+ private resolveNestedRelation;
118
+ private mergeFilters;
119
+ /**
120
+ * Builds a WHERE filter for the child model that constrains results to those belonging to the given parent.
121
+ * @param parentType lowercased parent model name
122
+ * @param parentId parent resource ID string
123
+ * @param parentRelation relation field name on the parent model (e.g. 'posts')
124
+ */
125
+ private buildNestedParentFilter;
73
126
  private matchUrlPattern;
74
127
  handleRequest({ client, method, path, query, requestBody }: RequestContext<Schema>): Promise<Response>;
75
128
  private handleGenericError;
76
129
  private processProcedureRequest;
77
130
  private makeProcBadInputErrorResponse;
78
131
  private makeProcGenericErrorResponse;
132
+ /**
133
+ * Builds the ORM `args` object (include, select) shared by single-read operations.
134
+ * Returns the args to pass to findUnique/findFirst and the resolved `include` list for serialization,
135
+ * or an error response if query params are invalid.
136
+ */
137
+ private buildSingleReadArgs;
79
138
  private processSingleRead;
80
139
  private processFetchRelated;
81
140
  private processReadRelationship;
82
141
  private processCollectionRead;
142
+ /**
143
+ * Builds link URL for a nested resource using parent type, parent ID, relation name, and optional child ID.
144
+ * Uses the parent model name mapping for the parent segment; the relation name is used as-is.
145
+ */
146
+ private makeNestedLinkUrl;
147
+ private processNestedSingleRead;
148
+ private processNestedCreate;
149
+ /**
150
+ * Builds the ORM `data` payload for a nested update, shared by both to-many (childId present)
151
+ * and to-one (childId absent) variants. Returns either `{ updateData }` or `{ error }`.
152
+ */
153
+ private buildNestedUpdatePayload;
154
+ /**
155
+ * Handles PATCH /:type/:id/:relationship/:childId (to-many) and
156
+ * PATCH /:type/:id/:relationship (to-one, childId undefined).
157
+ */
158
+ private processNestedUpdate;
159
+ private processNestedDelete;
83
160
  private buildPartialSelect;
84
161
  private addTotalCountToMeta;
85
162
  private makePaginator;
@@ -122,6 +199,7 @@ declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Ap
122
199
  private makeError;
123
200
  private makeUnsupportedModelError;
124
201
  private makeUnsupportedRelationshipError;
202
+ generateSpec(options?: OpenApiSpecOptions): Promise<openapi_types.OpenAPIV3_1.Document<{}>>;
125
203
  }
126
204
 
127
205
  /**
@@ -136,7 +214,7 @@ type RPCApiHandlerOptions<Schema extends SchemaDef = SchemaDef> = {
136
214
  * Logging configuration
137
215
  */
138
216
  log?: LogConfig;
139
- };
217
+ } & CommonHandlerOptions<Schema>;
140
218
  /**
141
219
  * RPC style API request handler that mirrors the ZenStackClient API
142
220
  */
@@ -156,4 +234,4 @@ declare class RPCApiHandler<Schema extends SchemaDef = SchemaDef> implements Api
156
234
  private processRequestPayload;
157
235
  }
158
236
 
159
- export { RPCApiHandler, type RPCApiHandlerOptions, RestApiHandler, type RestApiHandlerOptions };
237
+ export { type OpenApiSpecGenerator, type OpenApiSpecOptions, RPCApiHandler, type RPCApiHandlerOptions, RestApiHandler, type RestApiHandlerOptions };
package/dist/api.d.ts CHANGED
@@ -1,6 +1,37 @@
1
+ import { QueryOptions } from '@zenstackhq/orm';
1
2
  import { SchemaDef } from '@zenstackhq/orm/schema';
3
+ import * as openapi_types from 'openapi-types';
4
+ import { OpenAPIV3_1 } from 'openapi-types';
2
5
  import { A as ApiHandler, L as LogConfig, R as RequestContext, a as Response } from './types-CU_PWKr9.js';
3
- import '@zenstackhq/orm';
6
+
7
+ type CommonHandlerOptions<Schema extends SchemaDef> = {
8
+ /** Query options that affect the behavior of the OpenAPI provider. */
9
+ queryOptions?: QueryOptions<Schema>;
10
+ };
11
+ type OpenApiSpecOptions = {
12
+ /** Spec title. Defaults to 'ZenStack Generated API' */
13
+ title?: string;
14
+ /** Spec version. Defaults to '1.0.0' */
15
+ version?: string;
16
+ /** Spec description. */
17
+ description?: string;
18
+ /** Spec summary. */
19
+ summary?: string;
20
+ /**
21
+ * When true, assumes that the schema includes access policies, and adds
22
+ * 403 responses to operations that can potentially be rejected.
23
+ */
24
+ respectAccessPolicies?: boolean;
25
+ };
26
+ /**
27
+ * Interface for generating OpenAPI specifications.
28
+ */
29
+ interface OpenApiSpecGenerator {
30
+ /**
31
+ * Generates an OpenAPI v3.1 specification document.
32
+ */
33
+ generateSpec(options?: OpenApiSpecOptions): Promise<OpenAPIV3_1.Document>;
34
+ }
4
35
 
5
36
  /**
6
37
  * Options for {@link RestApiHandler}
@@ -45,11 +76,19 @@ type RestApiHandlerOptions<Schema extends SchemaDef = SchemaDef> = {
45
76
  * Mapping from model names to unique field name to be used as resource's ID.
46
77
  */
47
78
  externalIdMapping?: Record<string, string>;
48
- };
79
+ /**
80
+ * When `true`, enables nested route handling for all to-many relations:
81
+ * `/:parentType/:parentId/:relationName` (collection) and
82
+ * `/:parentType/:parentId/:relationName/:childId` (single).
83
+ *
84
+ * Defaults to `false`.
85
+ */
86
+ nestedRoutes?: boolean;
87
+ } & CommonHandlerOptions<Schema>;
49
88
  /**
50
89
  * RESTful-style API request handler (compliant with JSON:API)
51
90
  */
52
- declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements ApiHandler<Schema> {
91
+ declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements ApiHandler<Schema>, OpenApiSpecGenerator {
53
92
  private readonly options;
54
93
  private serializers;
55
94
  private readonly errors;
@@ -64,22 +103,60 @@ declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Ap
64
103
  private modelNameMapping;
65
104
  private reverseModelNameMapping;
66
105
  private externalIdMapping;
106
+ private nestedRoutes;
67
107
  constructor(options: RestApiHandlerOptions<Schema>);
68
108
  private validateOptions;
69
109
  get schema(): Schema;
70
110
  get log(): LogConfig | undefined;
71
111
  private buildUrlPatternMap;
72
112
  private mapModelName;
113
+ /**
114
+ * Resolves child model type and reverse relation from a parent relation name.
115
+ * e.g. given parentType='user', parentRelation='posts', returns { childType:'post', reverseRelation:'author' }
116
+ */
117
+ private resolveNestedRelation;
118
+ private mergeFilters;
119
+ /**
120
+ * Builds a WHERE filter for the child model that constrains results to those belonging to the given parent.
121
+ * @param parentType lowercased parent model name
122
+ * @param parentId parent resource ID string
123
+ * @param parentRelation relation field name on the parent model (e.g. 'posts')
124
+ */
125
+ private buildNestedParentFilter;
73
126
  private matchUrlPattern;
74
127
  handleRequest({ client, method, path, query, requestBody }: RequestContext<Schema>): Promise<Response>;
75
128
  private handleGenericError;
76
129
  private processProcedureRequest;
77
130
  private makeProcBadInputErrorResponse;
78
131
  private makeProcGenericErrorResponse;
132
+ /**
133
+ * Builds the ORM `args` object (include, select) shared by single-read operations.
134
+ * Returns the args to pass to findUnique/findFirst and the resolved `include` list for serialization,
135
+ * or an error response if query params are invalid.
136
+ */
137
+ private buildSingleReadArgs;
79
138
  private processSingleRead;
80
139
  private processFetchRelated;
81
140
  private processReadRelationship;
82
141
  private processCollectionRead;
142
+ /**
143
+ * Builds link URL for a nested resource using parent type, parent ID, relation name, and optional child ID.
144
+ * Uses the parent model name mapping for the parent segment; the relation name is used as-is.
145
+ */
146
+ private makeNestedLinkUrl;
147
+ private processNestedSingleRead;
148
+ private processNestedCreate;
149
+ /**
150
+ * Builds the ORM `data` payload for a nested update, shared by both to-many (childId present)
151
+ * and to-one (childId absent) variants. Returns either `{ updateData }` or `{ error }`.
152
+ */
153
+ private buildNestedUpdatePayload;
154
+ /**
155
+ * Handles PATCH /:type/:id/:relationship/:childId (to-many) and
156
+ * PATCH /:type/:id/:relationship (to-one, childId undefined).
157
+ */
158
+ private processNestedUpdate;
159
+ private processNestedDelete;
83
160
  private buildPartialSelect;
84
161
  private addTotalCountToMeta;
85
162
  private makePaginator;
@@ -122,6 +199,7 @@ declare class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Ap
122
199
  private makeError;
123
200
  private makeUnsupportedModelError;
124
201
  private makeUnsupportedRelationshipError;
202
+ generateSpec(options?: OpenApiSpecOptions): Promise<openapi_types.OpenAPIV3_1.Document<{}>>;
125
203
  }
126
204
 
127
205
  /**
@@ -136,7 +214,7 @@ type RPCApiHandlerOptions<Schema extends SchemaDef = SchemaDef> = {
136
214
  * Logging configuration
137
215
  */
138
216
  log?: LogConfig;
139
- };
217
+ } & CommonHandlerOptions<Schema>;
140
218
  /**
141
219
  * RPC style API request handler that mirrors the ZenStackClient API
142
220
  */
@@ -156,4 +234,4 @@ declare class RPCApiHandler<Schema extends SchemaDef = SchemaDef> implements Api
156
234
  private processRequestPayload;
157
235
  }
158
236
 
159
- export { RPCApiHandler, type RPCApiHandlerOptions, RestApiHandler, type RestApiHandlerOptions };
237
+ export { type OpenApiSpecGenerator, type OpenApiSpecOptions, RPCApiHandler, type RPCApiHandlerOptions, RestApiHandler, type RestApiHandlerOptions };