Skip to content

Commit

Permalink
feat: add intTotalElements option
Browse files Browse the repository at this point in the history
  • Loading branch information
shijistar committed Feb 2, 2025
1 parent 4e99eea commit 91396ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# swigger-api-hub

## [1.2.2](https://github.com/shijistar/swagger-api-hub/compare/1.2.1...1.2.2) (2025-01-16)

- Upgrade all dependencies to latest version
Expand Down
4 changes: 3 additions & 1 deletion cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export type ServiceConfig = GenerateApiParams & {
singleHttpClient?: never;
/** Change the default path of `http-client.ts` file, you can use your own http client */
httpClientFile?: string;
/** Auto create an instance for each api class */
/** Auto create an instance for each api class. Default is `true` */
createApiInstance?: boolean;
/** Whether to force convert `totalElements` to `number`(int32) type. Default is `false`. */
intTotalElements?: boolean;
/**
* Some custom data type mappings
*/
Expand Down
48 changes: 47 additions & 1 deletion templates/data-contracts.ejs
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
// @ts-nocheck

<%~ includeFile('@base/data-contracts.eta', it) %>
// &lt;%~ includeFile('@base/data-contracts.eta', it) %&gt;
<%
const { modelTypes, utils, config } = it;
const { formatDescription, require, _, Ts } = utils;
const buildGenerics = (contract) => {
if (!contract.genericArgs || !contract.genericArgs.length) return '';
return '<' + contract.genericArgs.map(({ name, default: defaultType, extends: extendsType }) => {
return [
name,
extendsType && `extends ${extendsType}`,
defaultType && `= ${defaultType}`,
].join('')
}).join(',') + '>'
}
const formatContent = (content) => {
if (config.intTotalElements) {
return content?.replace(/totalElements(\??)\:\s*(BigInt|string)([,;])/g, "totalElements$1: number$3");
}
return content;
}
const dataContractTemplates = {
enum: (contract) => {
return `enum ${contract.name} {\r\n${formatContent(contract.content)} \r\n }`;
},
interface: (contract) => {
return `interface ${contract.name}${buildGenerics(contract)} {\r\n${formatContent(contract.content)}}`;
},
type: (contract) => {
return `type ${contract.name}${buildGenerics(contract)} = ${formatContent(contract.content)}`;
},
}
%>
<% if (config.internalTemplateOptions.addUtilRequiredKeysType) { %>
type <%~ config.Ts.CodeGenKeyword.UtilRequiredKeys %><T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
<% } %>
<% for (const contract of modelTypes) { %>
<%~ includeFile('@base/data-contract-jsdoc.ejs', { ...it, data: { ...contract, ...contract.typeData } }) %>
<%~ contract.internal ? '' : 'export'%> <%~ (dataContractTemplates[contract.typeIdentifier] || dataContractTemplates.type)(contract) %>
<% } %>

0 comments on commit 91396ad

Please sign in to comment.