Browse Source

update: schematics template for the resource api

pull/25761/head
sumeyye 1 month ago
parent
commit
98afff5c6c
  1. 44
      npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template
  2. 5
      npm/ng-packs/packages/schematics/src/commands/api/index.ts
  3. 5
      npm/ng-packs/packages/schematics/src/commands/api/schema.json

44
npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template

@ -9,10 +9,15 @@ export class <%= name %>Service {
apiName = '<%= apiName %>';<%
for (let {body, signature} of methods) { %>
<%
const isBlob = body.isBlobMethod() ;
const responseType = isBlob ? "Blob":body.responseType;
const isBlob = body.isBlobMethod();
const responseType = isBlob ? 'Blob' : body.responseType;
const httpResponseType = body.httpResponseType;
const acceptHeader = body.acceptHeader;
const resourceParameters = signature.parameters.filter(p => p.name !== 'config');
const resourceType = resourceParameters.length
? `{ ${resourceParameters.map(p => `${p.name}${p.optional}: ${p.type}`).join('; ')} }`
: '';
const resourceArgs = resourceParameters.map(p => `params.${p.name}`).join(', ');
%>
<%= camel(signature.name) %> = (<%= serializeParameters(signature.parameters) %>) =>
@ -32,5 +37,36 @@ export class <%= name %>Service {
if (body.body) { %>
body: <%= body.body %>,<% } %>
},
{ apiName: this.apiName,...config });<% } %>
}
{ apiName: this.apiName,...config });<%
if (resourceApi && body.method === 'GET') { %>
<%= camel(signature.name) %>Resource = (<%
if (resourceParameters.length) { %>
params: Signal<<%= resourceType %>>,<% } %>
config?: Partial<Rest.Config>
): ResourceRef<<%= responseType %> | undefined> => this.restService.requestResource(
computed(() => {<%
if (resourceParameters.length) { %>
const { <%= resourceParameters.map(p => p.name).join(', ') %> } = params();<% } %>
return ({
method: '<%= body.method %>',<%
if (httpResponseType && httpResponseType !== 'json') { %>
responseType: '<%= httpResponseType %>',<% } %><%
if (acceptHeader) { %>
headers: { Accept: '<%= acceptHeader %>' },<% } %>
url: <%= body.url %>,<%
if (body.dictParamVar && !body.params.length) { %>
params: <%= body.dictParamVar %>,<% } %><%
if (body.dictParamVar && body.params.length) { %>
params: { ...<%= body.dictParamVar %>, <%= body.params.join(', ') %> },<% } %><%
if (!body.dictParamVar && body.params.length) { %>
params: { <%= body.params.join(', ') %> },<% }
if (body.body) { %>
body: <%= body.body %>,<% } %>
});
}),
{ apiName: this.apiName, ...config },
);<% }
} %>
}

5
npm/ng-packs/packages/schematics/src/commands/api/index.ts

@ -56,6 +56,7 @@ export default function (schema: GenerateProxySchema) {
const types = data.types;
const modules = data.modules;
const serviceType = schema.serviceType || defaultEServiceType;
const resourceApi = schema.resourceApi ?? false;
if (!types || !modules) {
throw new SchematicsException(Exception.InvalidApiDefinition);
@ -82,6 +83,7 @@ export default function (schema: GenerateProxySchema) {
apiName,
controllers,
serviceImports,
resourceApi,
});
const modelImports: Record<string, string[]> = {};
@ -177,7 +179,7 @@ function createModelGenerator(params: ModelGeneratorParams) {
}
function createServiceGenerator(params: ServiceGeneratorParams) {
const { targetPath, controllers, serviceImports } = params;
const { targetPath, controllers, serviceImports, resourceApi } = params;
const mapControllerToService = createControllerToServiceMapper(params);
return chain(
@ -195,6 +197,7 @@ function createServiceGenerator(params: ServiceGeneratorParams) {
applyTemplates({
...cases,
serializeParameters,
resourceApi,
...service,
}),
move(normalize(targetPath)),

5
npm/ng-packs/packages/schematics/src/commands/api/schema.json

@ -79,6 +79,11 @@
}
]
}
},
"resourceApi": {
"description": "Generate Resource API helpers for read endpoints",
"type": "boolean",
"default": false
}
},

Loading…
Cancel
Save