@rexeus/typeweaver-types 0.8.0 → 0.9.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.
@@ -0,0 +1,29 @@
1
+ import type {
2
+ ResponseDefinition,
3
+ SpecDefinition,
4
+ } from "@rexeus/typeweaver-core";
5
+
6
+ type MatchedOperationDefinition<
7
+ TSpec extends SpecDefinition,
8
+ TResourceName extends keyof TSpec["resources"] & string,
9
+ > = NonNullable<TSpec["resources"][TResourceName]>["operations"][number];
10
+
11
+ type MatchedResponseDefinition<
12
+ TResponses extends readonly ResponseDefinition[],
13
+ > = TResponses[number];
14
+
15
+ export declare const getOperationDefinition: <
16
+ TSpec extends SpecDefinition,
17
+ TResourceName extends keyof TSpec["resources"] & string,
18
+ >(
19
+ spec: TSpec,
20
+ resourceName: TResourceName,
21
+ operationId: string
22
+ ) => MatchedOperationDefinition<TSpec, TResourceName>;
23
+
24
+ export declare const getResponseDefinition: <
25
+ TResponses extends readonly ResponseDefinition[],
26
+ >(
27
+ responses: TResponses,
28
+ responseName: string
29
+ ) => MatchedResponseDefinition<TResponses>;
@@ -0,0 +1,14 @@
1
+ export const getOperationDefinition = (spec, resourceName, operationId) => {
2
+ const operation = spec.resources[resourceName]?.operations.find((candidate) => candidate.operationId === operationId);
3
+ if (operation === undefined) {
4
+ throw new Error(`Missing operation definition '${String(resourceName)}.${String(operationId)}'.`);
5
+ }
6
+ return operation;
7
+ };
8
+ export const getResponseDefinition = (responses, responseName) => {
9
+ const response = responses.find((candidate) => candidate.name === responseName);
10
+ if (response === undefined) {
11
+ throw new Error(`Missing response definition '${String(responseName)}'.`);
12
+ }
13
+ return response;
14
+ };
package/dist/lib/index.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @generated by @rexeus/typeweaver
6
6
  */
7
7
 
8
- export * from "./assert";
8
+ export * from "./definitionLookup";
9
9
  export * from "./RequestValidator";
10
10
  export * from "./ResponseValidator";
11
11
  export * from "./Validator";
@@ -6,15 +6,17 @@
6
6
  * @generated by @rexeus/typeweaver
7
7
  */
8
8
 
9
- import definition from "<%= sourcePath %>";
9
+ import spec from "<%= specPath %>";
10
10
  import {
11
11
  type IHttpRequest,
12
12
  type SafeRequestValidationResult,
13
13
  RequestValidationError
14
14
  } from "@rexeus/typeweaver-core";
15
- import { RequestValidator } from "../lib/types";
15
+ import { getOperationDefinition, RequestValidator } from "../lib/types";
16
16
  import type { I<%= pascalCaseOperationId %>Request } from "<%= requestFile %>";
17
17
 
18
+ const definition = getOperationDefinition(spec, "<%= resourceName %>", "<%= operationId %>");
19
+
18
20
  export class <%= pascalCaseOperationId %>RequestValidator extends RequestValidator {
19
21
  public safeValidate(request: IHttpRequest): SafeRequestValidationResult<I<%= pascalCaseOperationId %>Request> {
20
22
  const error = new RequestValidationError();
@@ -41,17 +43,13 @@ export class <%= pascalCaseOperationId %>RequestValidator extends RequestValidat
41
43
 
42
44
  <% if(header) { %>
43
45
  if (definition.request.header) {
44
- <% if(header instanceof z.ZodObject) { %>
45
- const coercedHeader = this.coerceHeaderToSchema(request.header, definition.request.header.shape);
46
- <% } else { %>
47
- const coercedHeader = this.coerceHeaderToSchema(request.header, definition.request.header.unwrap().shape);
48
- <% } %>
46
+ const coercedHeader = this.coerceHeaderToSchema(request.header, this.getSchema(definition.request.header));
49
47
  const result = definition.request.header.safeParse(coercedHeader);
50
48
 
51
49
  if (!result.success) {
52
50
  error.addHeaderIssues(result.error.issues);
53
51
  } else {
54
- validatedRequest.header = result.data;
52
+ validatedRequest.header = result.data as IHttpRequest["header"];
55
53
  }
56
54
  }
57
55
  <% } %>
@@ -70,17 +68,13 @@ export class <%= pascalCaseOperationId %>RequestValidator extends RequestValidat
70
68
 
71
69
  <% if(query) { %>
72
70
  if (definition.request.query) {
73
- <% if(query instanceof z.ZodObject) { %>
74
- const coercedQuery = this.coerceQueryToSchema(request.query, definition.request.query.shape);
75
- <% } else { %>
76
- const coercedQuery = this.coerceQueryToSchema(request.query, definition.request.query.unwrap().shape);
77
- <% } %>
71
+ const coercedQuery = this.coerceQueryToSchema(request.query, this.getSchema(definition.request.query));
78
72
  const result = definition.request.query.safeParse(coercedQuery);
79
73
 
80
74
  if (!result.success) {
81
75
  error.addQueryIssues(result.error.issues);
82
76
  } else {
83
- validatedRequest.query = result.data;
77
+ validatedRequest.query = result.data as IHttpRequest["query"];
84
78
  }
85
79
  }
86
80
  <% } %>
@@ -107,4 +101,4 @@ export class <%= pascalCaseOperationId %>RequestValidator extends RequestValidat
107
101
 
108
102
  return result.data;
109
103
  }
110
- }
104
+ }
@@ -6,8 +6,10 @@
6
6
  * @generated by @rexeus/typeweaver
7
7
  */
8
8
 
9
+ <% if (ownResponses.length > 0) { %>
9
10
  import { HttpStatusCode } from "<%= coreDir %>";
10
11
  import type { ITypedHttpResponse } from "<%= coreDir %>";
12
+ <% } %>
11
13
  <% for (const shared of sharedResponses) { %>
12
14
  import type { I<%= shared.name %>Response } from "<%= shared.path %>";
13
15
  <% } %>
@@ -6,13 +6,23 @@
6
6
  * @generated by @rexeus/typeweaver
7
7
  */
8
8
 
9
- import definition from "<%= sourcePath %>";
9
+ import spec from "<%= specPath %>";
10
10
  import {
11
+ getOperationDefinition,
12
+ getResponseDefinition,
11
13
  type ResponseEntry,
12
14
  ResponseValidator,
13
15
  } from "../lib/types";
14
16
  import type { <%= pascalCaseOperationId %>Response } from "<%= responseFile %>";
15
17
 
18
+ const definition = getOperationDefinition(spec, "<%= resourceName %>", "<%= operationId %>");
19
+ <% for (const response of [...ownResponses, ...sharedResponses]) { %>
20
+ const <%= response.definitionVariableName %> = getResponseDefinition(
21
+ definition.responses,
22
+ "<%= response.name %>"
23
+ );
24
+ <% } %>
25
+
16
26
  export class <%= pascalCaseOperationId %>ResponseValidator extends ResponseValidator<<%= pascalCaseOperationId %>Response> {
17
27
  protected override readonly expectedStatusCodes = [
18
28
  <% const sortedStatusCodes = allStatusCodes.map(statusCode => statusCode.statusCode).sort() %>
@@ -21,7 +31,7 @@ export class <%= pascalCaseOperationId %>ResponseValidator extends ResponseValid
21
31
 
22
32
  protected override readonly responseEntries: readonly ResponseEntry[] = [
23
33
  <% for (const response of [...ownResponses, ...sharedResponses]) { %>
24
- { name: "<%= response.name.replace(/Response$/, '') %>", statusCode: <%= response.statusCode %>, headerSchema: definition.responses[<%= response.index %>]?.header, bodySchema: definition.responses[<%= response.index %>]?.body },
34
+ { name: "<%= response.name %>", statusCode: <%= response.statusCode %>, headerSchema: <%= response.definitionVariableName %>.header, bodySchema: <%= response.definitionVariableName %>.body },
25
35
  <% } %>
26
36
  ];
27
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rexeus/typeweaver-types",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Generates request and response types plus validators aligned with your API contract. Powered by Typeweaver 🧵✨",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -47,18 +47,18 @@
47
47
  },
48
48
  "homepage": "https://github.com/rexeus/typeweaver#readme",
49
49
  "peerDependencies": {
50
- "@rexeus/typeweaver-core": "^0.8.0",
51
- "@rexeus/typeweaver-gen": "^0.8.0"
50
+ "@rexeus/typeweaver-core": "^0.9.0",
51
+ "@rexeus/typeweaver-gen": "^0.9.0"
52
52
  },
53
53
  "devDependencies": {
54
- "oxc-transform": "^0.115.0",
54
+ "oxc-transform": "^0.121.0",
55
55
  "test-utils": "file:../test-utils",
56
- "@rexeus/typeweaver-core": "^0.8.0",
57
- "@rexeus/typeweaver-gen": "^0.8.0"
56
+ "@rexeus/typeweaver-core": "^0.9.0",
57
+ "@rexeus/typeweaver-gen": "^0.9.0"
58
58
  },
59
59
  "dependencies": {
60
60
  "case": "^1.6.3",
61
- "@rexeus/typeweaver-zod-to-ts": "^0.8.0"
61
+ "@rexeus/typeweaver-zod-to-ts": "^0.9.0"
62
62
  },
63
63
  "scripts": {
64
64
  "typecheck": "tsc --noEmit -p tsconfig.typecheck.json",
@@ -1 +0,0 @@
1
- export declare function assert(value: unknown, message?: string): asserts value;
@@ -1,11 +0,0 @@
1
- /**
2
- * This file was automatically generated by typeweaver.
3
- * DO NOT EDIT. Instead, modify the source definition file and generate again.
4
- *
5
- * @generated by @rexeus/typeweaver
6
- */
7
- export function assert(value, message) {
8
- if (!value) {
9
- throw new Error(message || "Assertion failed");
10
- }
11
- }