@bitstack/ng-query-codegen-openapi 0.0.38-alpha.2 → 0.0.38-alpha.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/lib/index.js CHANGED
@@ -619,6 +619,7 @@ var EndpointInfoExtractor = class {
619
619
  const { queryParams, pathParams, isVoidArg, hasRequestBody } = this.extractParameters(operationDefinition);
620
620
  const contentType = this.extractContentType(operation);
621
621
  const tags = Array.isArray(operation.tags) ? operation.tags : [];
622
+ const isBlobResponse = this.isBlobResponseEndpoint(operation);
622
623
  return {
623
624
  operationName: finalOperationName,
624
625
  argTypeName,
@@ -633,7 +634,8 @@ var EndpointInfoExtractor = class {
633
634
  summary,
634
635
  contentType,
635
636
  hasRequestBody,
636
- tags
637
+ tags,
638
+ isBlobResponse
637
639
  };
638
640
  }
639
641
  /**
@@ -667,6 +669,28 @@ var EndpointInfoExtractor = class {
667
669
  if (!parameters) return [];
668
670
  return Array.isArray(parameters) ? parameters : [parameters];
669
671
  }
672
+ /**
673
+ * 偵測是否為 Blob 下載端點
674
+ * 當 response content type 為 binary 類型或 schema format 為 binary 時回傳 true
675
+ */
676
+ isBlobResponseEndpoint(operation) {
677
+ if (!operation.responses) return false;
678
+ const successResponse = operation.responses["200"] || operation.responses["201"];
679
+ if (!successResponse?.content) return false;
680
+ const contentTypes = Object.keys(successResponse.content);
681
+ const textContentTypes = ["application/json", "text/", "*/*"];
682
+ const hasNonTextContentType = contentTypes.some(
683
+ (ct) => !textContentTypes.some((textCt) => ct === textCt || ct.startsWith(textCt))
684
+ );
685
+ if (hasNonTextContentType) return true;
686
+ for (const ct of contentTypes) {
687
+ const schema = successResponse.content[ct]?.schema;
688
+ if (schema && schema.type === "string" && schema.format === "binary") {
689
+ return true;
690
+ }
691
+ }
692
+ return false;
693
+ }
670
694
  /**
671
695
  * 提取操作的 content type
672
696
  * @param operation - 操作對象
@@ -707,10 +731,15 @@ function generateTypesFile(endpointInfos, _options, schemaInterfaces, operationD
707
731
  }
708
732
  });
709
733
  }
734
+ const hasBlobResponse = endpointInfos.some((info) => info.isBlobResponse);
710
735
  let importStatement = `/* eslint-disable */
711
- // [Warning] Generated automatically - do not edit manually
712
-
736
+ // [Warning] Generated automatically - do not edit manually
737
+
738
+ `;
739
+ if (hasBlobResponse) {
740
+ importStatement += `import { HttpResponse } from '@angular/common/http';
713
741
  `;
742
+ }
714
743
  const hasSchemaTypes = schemaInterfaces && Object.keys(schemaInterfaces).length > 0;
715
744
  if (hasSchemaTypes) {
716
745
  importStatement += `import * as Schema from "../schema";
@@ -731,11 +760,15 @@ function generateTypesFile(endpointInfos, _options, schemaInterfaces, operationD
731
760
  }
732
761
  }
733
762
  if (resTypeName) {
734
- const responseTypeContent = generateResponseTypeContent(endpoint, operationDefinitions, schemaTypeMap);
735
- if (responseTypeContent.trim() === "") {
736
- endpointTypes.push(`export type ${resTypeName} = void;`, ``);
763
+ if (endpoint.isBlobResponse) {
764
+ endpointTypes.push(`export type ${resTypeName} = HttpResponse<Blob>;`, ``);
737
765
  } else {
738
- endpointTypes.push(`export type ${resTypeName} = {`, responseTypeContent, `};`, ``);
766
+ const responseTypeContent = generateResponseTypeContent(endpoint, operationDefinitions, schemaTypeMap);
767
+ if (responseTypeContent.trim() === "") {
768
+ endpointTypes.push(`export type ${resTypeName} = void;`, ``);
769
+ } else {
770
+ endpointTypes.push(`export type ${resTypeName} = {`, responseTypeContent, `};`, ``);
771
+ }
739
772
  }
740
773
  }
741
774
  });
@@ -939,7 +972,7 @@ function generateRtkQueryFile(endpointInfos, options) {
939
972
  /**
940
973
  * ${info.summary}
941
974
  */
942
- ${info.operationName}Query(args: IRestFulEndpointsQueryReturn<${info.argTypeName}>, queryOptions?: QueryOptions): Observable<QueryState<${info.responseTypeName}>> {
975
+ ${info.operationName}Query(args: IRestFulEndpointsQueryReturn<${info.argTypeName}>, queryOptions?: BaseQueryOptions): Observable<QueryState<${info.responseTypeName}>> {
943
976
  if (queryOptions?.skip) {
944
977
  return this.ntkQuery.skipQuery<${info.responseTypeName}>();
945
978
  }
@@ -989,7 +1022,7 @@ function generateRtkQueryFile(endpointInfos, options) {
989
1022
  import {Injectable, inject} from '@angular/core';
990
1023
  import {Observable} from 'rxjs';
991
1024
  import {HttpErrorResponse} from '@angular/common/http';
992
- import {NtkQueryService, MutationState, QueryState, QueryOptions, MutationResponse, LazyQueryResult, LazyQueryTriggerOptions} from '@core/ntk-query';
1025
+ import {NtkQueryService, MutationState, QueryState, BaseQueryOptions, MutationResponse, LazyQueryResult, LazyQueryTriggerOptions} from '@core/ntk-query';
993
1026
  import {ECacheTagTypes} from '../tagTypes';
994
1027
  import {${apiServiceName}} from './enhanceEndpoints';
995
1028
  import {IRestFulEndpointsQueryReturn, RequestOptions} from '../common-types';
@@ -1050,10 +1083,13 @@ ${endpointInfos.map((info) => {
1050
1083
  const paramsLines = info.queryParams.map((param) => `${param.name}: args.variables.${param.name},`).join("\n");
1051
1084
  paramsSection = `params: {${paramsLines}},`;
1052
1085
  }
1086
+ const blobOptions = info.isBlobResponse ? `
1087
+ responseType: 'blob',
1088
+ observe: 'response',` : "";
1053
1089
  return `{
1054
1090
  ...args?.fetchOptions,
1055
1091
  headers: { 'Content-Type': '${info.contentType}', ...args?.fetchOptions?.headers },
1056
- ${paramsSection}${info.hasRequestBody ? `body: args.variables.body,` : ""}
1092
+ ${paramsSection}${info.hasRequestBody ? `body: args.variables.body,` : ""}${blobOptions}
1057
1093
  }`;
1058
1094
  };
1059
1095
  return `