vite-plugin-openapi-codegen 1.2.0 → 1.2.1

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.mjs CHANGED
@@ -147,14 +147,20 @@ function createClientFunctionDeclaration(operation) {
147
147
  const requestProperties = [ts.factory.createSpreadAssignment(ts.factory.createIdentifier("requestOptions")), ts.factory.createPropertyAssignment(ts.factory.createIdentifier("method"), ts.factory.createStringLiteral(operation.methodUpper))];
148
148
  if (operation.queryChannel.present) requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("searchParams"), ts.factory.createCallExpression(ts.factory.createIdentifier("buildSearchParams"), void 0, [ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "query")])));
149
149
  if (operation.bodyChannel.present) {
150
- if (operation.bodyContentType === "json") requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("json"), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body")));
151
- else requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("body"), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body")));
150
+ const bodyPropertyName = operation.bodyContentType === "json" ? "json" : "body";
151
+ const bodyAssignment = ts.factory.createObjectLiteralExpression([ts.factory.createPropertyAssignment(ts.factory.createIdentifier(bodyPropertyName), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body"))], false);
152
+ if (operation.bodyContentType === "json") requestProperties.push(createBodyRequestProperty(operation.bodyChannel, bodyAssignment));
153
+ else requestProperties.push(createBodyRequestProperty(operation.bodyChannel, bodyAssignment));
152
154
  if (operation.bodyContentType === "binary") requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("contentType"), ts.factory.createStringLiteral("application/octet-stream")));
153
155
  }
154
156
  requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("signal"), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "signal")));
155
157
  const requestCall = ts.factory.createCallExpression(ts.factory.createIdentifier(operation.requestFunction), operation.responseTypeExpr ? [createTypeNodeFromText(operation.responseTypeExpr)] : void 0, [parseExpression(operation.pathInvocationExpr), ts.factory.createObjectLiteralExpression(requestProperties, true)]);
156
158
  return ts.factory.createFunctionDeclaration([createExportModifier()], void 0, ts.factory.createIdentifier(operation.funcName), void 0, [ts.factory.createParameterDeclaration(void 0, void 0, ts.factory.createIdentifier("options"), void 0, ts.factory.createTypeReferenceNode(operation.optionTypeName)), ts.factory.createParameterDeclaration(void 0, void 0, ts.factory.createIdentifier("requestOptions"), void 0, ts.factory.createTypeReferenceNode("RuntimeRequestOptions"), ts.factory.createObjectLiteralExpression())], createTypeNodeFromText(operation.returnTypeExpr), ts.factory.createBlock([ts.factory.createReturnStatement(requestCall)], true));
157
159
  }
160
+ function createBodyRequestProperty(bodyChannel, bodyAssignment) {
161
+ if (bodyChannel.required) return bodyAssignment.properties[0];
162
+ return ts.factory.createSpreadAssignment(ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression(ts.factory.createBinaryExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body"), ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), ts.factory.createIdentifier("undefined")), ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createObjectLiteralExpression([], false), ts.factory.createToken(ts.SyntaxKind.ColonToken), bodyAssignment)));
163
+ }
158
164
  function createBuildSearchParamsFunction() {
159
165
  return ts.factory.createFunctionDeclaration(void 0, void 0, ts.factory.createIdentifier("buildSearchParams"), void 0, [ts.factory.createParameterDeclaration(void 0, void 0, ts.factory.createIdentifier("query"), void 0, createTypeNodeFromText("Record<string, unknown> | undefined"))], createTypeNodeFromText("URLSearchParams | undefined"), ts.factory.createBlock([
160
166
  ts.factory.createIfStatement(ts.factory.createBinaryExpression(ts.factory.createIdentifier("query"), ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), ts.factory.createIdentifier("undefined")), ts.factory.createReturnStatement(ts.factory.createIdentifier("undefined"))),
@@ -398,7 +404,7 @@ function createRequestBodyChannel(entry, context, body, kind) {
398
404
  };
399
405
  return {
400
406
  present: true,
401
- required: entry.operation.requestBody?.required !== false,
407
+ required: entry.operation.requestBody?.required === true,
402
408
  typeRef
403
409
  };
404
410
  }
package/dist/index.d.mts CHANGED
@@ -33,6 +33,7 @@ interface OpenAPIOperation {
33
33
  type OpenAPIPathItem = Partial<Record<HttpMethod, OpenAPIOperation>>;
34
34
  interface OpenAPISchema {
35
35
  properties?: Record<string, unknown>;
36
+ required?: string[];
36
37
  type?: string;
37
38
  }
38
39
  interface OpenAPISpec {
package/dist/index.mjs CHANGED
@@ -145,14 +145,20 @@ function createClientFunctionDeclaration(operation) {
145
145
  const requestProperties = [ts.factory.createSpreadAssignment(ts.factory.createIdentifier("requestOptions")), ts.factory.createPropertyAssignment(ts.factory.createIdentifier("method"), ts.factory.createStringLiteral(operation.methodUpper))];
146
146
  if (operation.queryChannel.present) requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("searchParams"), ts.factory.createCallExpression(ts.factory.createIdentifier("buildSearchParams"), void 0, [ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "query")])));
147
147
  if (operation.bodyChannel.present) {
148
- if (operation.bodyContentType === "json") requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("json"), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body")));
149
- else requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("body"), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body")));
148
+ const bodyPropertyName = operation.bodyContentType === "json" ? "json" : "body";
149
+ const bodyAssignment = ts.factory.createObjectLiteralExpression([ts.factory.createPropertyAssignment(ts.factory.createIdentifier(bodyPropertyName), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body"))], false);
150
+ if (operation.bodyContentType === "json") requestProperties.push(createBodyRequestProperty(operation.bodyChannel, bodyAssignment));
151
+ else requestProperties.push(createBodyRequestProperty(operation.bodyChannel, bodyAssignment));
150
152
  if (operation.bodyContentType === "binary") requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("contentType"), ts.factory.createStringLiteral("application/octet-stream")));
151
153
  }
152
154
  requestProperties.push(ts.factory.createPropertyAssignment(ts.factory.createIdentifier("signal"), ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "signal")));
153
155
  const requestCall = ts.factory.createCallExpression(ts.factory.createIdentifier(operation.requestFunction), operation.responseTypeExpr ? [createTypeNodeFromText(operation.responseTypeExpr)] : void 0, [parseExpression(operation.pathInvocationExpr), ts.factory.createObjectLiteralExpression(requestProperties, true)]);
154
156
  return ts.factory.createFunctionDeclaration([createExportModifier()], void 0, ts.factory.createIdentifier(operation.funcName), void 0, [ts.factory.createParameterDeclaration(void 0, void 0, ts.factory.createIdentifier("options"), void 0, ts.factory.createTypeReferenceNode(operation.optionTypeName)), ts.factory.createParameterDeclaration(void 0, void 0, ts.factory.createIdentifier("requestOptions"), void 0, ts.factory.createTypeReferenceNode("RuntimeRequestOptions"), ts.factory.createObjectLiteralExpression())], createTypeNodeFromText(operation.returnTypeExpr), ts.factory.createBlock([ts.factory.createReturnStatement(requestCall)], true));
155
157
  }
158
+ function createBodyRequestProperty(bodyChannel, bodyAssignment) {
159
+ if (bodyChannel.required) return bodyAssignment.properties[0];
160
+ return ts.factory.createSpreadAssignment(ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression(ts.factory.createBinaryExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("options"), "body"), ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), ts.factory.createIdentifier("undefined")), ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createObjectLiteralExpression([], false), ts.factory.createToken(ts.SyntaxKind.ColonToken), bodyAssignment)));
161
+ }
156
162
  function createBuildSearchParamsFunction() {
157
163
  return ts.factory.createFunctionDeclaration(void 0, void 0, ts.factory.createIdentifier("buildSearchParams"), void 0, [ts.factory.createParameterDeclaration(void 0, void 0, ts.factory.createIdentifier("query"), void 0, createTypeNodeFromText("Record<string, unknown> | undefined"))], createTypeNodeFromText("URLSearchParams | undefined"), ts.factory.createBlock([
158
164
  ts.factory.createIfStatement(ts.factory.createBinaryExpression(ts.factory.createIdentifier("query"), ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), ts.factory.createIdentifier("undefined")), ts.factory.createReturnStatement(ts.factory.createIdentifier("undefined"))),
@@ -396,7 +402,7 @@ function createRequestBodyChannel(entry, context, body, kind) {
396
402
  };
397
403
  return {
398
404
  present: true,
399
- required: entry.operation.requestBody?.required !== false,
405
+ required: entry.operation.requestBody?.required === true,
400
406
  typeRef
401
407
  };
402
408
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-openapi-codegen",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Vite plugin that generates typed API clients and route builders from OpenAPI specs",
5
5
  "keywords": [
6
6
  "api-client",