@conduit-client/generator-ts 3.5.0 → 3.6.2

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.
@@ -1,7 +1,6 @@
1
1
  import { Code } from '../files';
2
2
  import { CommandGenerator } from './base-command-generator';
3
3
  import type { ImportableTypeReference } from '../files';
4
- import { type Type } from '@conduit-client/model/v1';
5
4
  export declare const MEDIATYPE_APP_JSON = "application/json";
6
5
  /**
7
6
  * Extends CommandGenerator to provide functionality for generating command files specifically designed for HTTP operations.
@@ -51,17 +50,5 @@ export declare class HttpCommandGenerator extends CommandGenerator {
51
50
  * @returns {Code | undefined} A code snippet for the input payload parameter, if applicable.
52
51
  */
53
52
  generateInputPayloadParameter(): Code | undefined;
54
- /**
55
- * Encodes a parameter based on its type.
56
- * @param {string} name The name of the parameter to encode.
57
- * @param {Type} type The type descriptor of the parameter.
58
- * @returns {string | undefined} The encoded parameter value as a string, or undefined if the type is unsupported.
59
- */
60
- encodeParam(name: string, type: Type, required: boolean): string | undefined;
61
- /**
62
- * Processes the URL with replacements for URI parameters based on their values specified in the command model.
63
- * @returns {string} The fully constructed URL with all necessary URI parameter substitutions.
64
- */
65
- processUrl(): string;
66
53
  generateCoerceError(): Code;
67
54
  }
@@ -1,5 +1,6 @@
1
1
  import { type Code } from '../files';
2
2
  import { ICommandModel } from '../model/base-command-model';
3
+ import { type Type } from '@conduit-client/model/v1';
3
4
  /**
4
5
  * Generates a destructuring assignment code block for a given property name, ensuring that the variable names are valid TypeScript identifiers.
5
6
  * This function is particularly useful when dealing with property names that may not directly translate into valid TypeScript variable names,
@@ -15,3 +16,27 @@ export declare function buildPropertyDestructureDeclaration(propertyName: string
15
16
  variableName: string;
16
17
  };
17
18
  export declare function buildServiceProjection(model: ICommandModel): string[];
19
+ /**
20
+ * Encodes a parameter based on its type for use in URL template strings.
21
+ * @param name The name of the parameter variable to encode.
22
+ * @param type The type descriptor of the parameter.
23
+ * @param required Whether the parameter is required.
24
+ * @returns The encoded parameter value as a string expression, or undefined if the type is unsupported.
25
+ */
26
+ export declare function encodeParam(name: string, type: Type, required: boolean): string | undefined;
27
+ /**
28
+ * Processes the URL with replacements for URI parameters based on their values specified in the command model.
29
+ * Combines serverUrlPath and endpointUrlPath, then replaces URI parameter placeholders (e.g., {id}) with
30
+ * template string expressions that encode the parameter values.
31
+ * @param commandModel The command model containing URL paths and URI parameters.
32
+ * @returns The fully constructed URL template string with all necessary URI parameter substitutions.
33
+ */
34
+ export declare function processUrl(commandModel: ICommandModel): string;
35
+ /**
36
+ * Generates code for a URL getter method with URI parameter destructuring.
37
+ * Always destructures URI parameters from this.config to ensure consistency.
38
+ *
39
+ * @param commandModel The command model containing URL paths and URI parameters
40
+ * @returns Code block containing a getter method that returns the processed URL
41
+ */
42
+ export declare function generateUrlGetter(commandModel: ICommandModel): Code;
package/dist/v1/index.js CHANGED
@@ -2346,6 +2346,66 @@ function buildServiceProjection(model) {
2346
2346
  }
2347
2347
  return [name, overrides];
2348
2348
  }
2349
+ function encodeParam(name, type, required) {
2350
+ const isScalarArrayType = (t) => {
2351
+ const canonicalizedType = canonicalizeType(t);
2352
+ if (canonicalizedType.type !== "array") {
2353
+ return false;
2354
+ }
2355
+ const canonicalizedItems = canonicalizeType(canonicalizedType.items);
2356
+ return isCanonicalStringType(canonicalizedItems) || isCanonicalNumberType(canonicalizedItems);
2357
+ };
2358
+ const canonicalType = canonicalizeType(type);
2359
+ return isCanonicalNumberType(canonicalType) ? `${name}` : isCanonicalStringType(canonicalType) ? required ? `encodeURIComponent(${name})` : `encodeURIComponent(${name} ? ${name} : '')` : isScalarArrayType(canonicalType) ? isCanonicalStringType(canonicalType.items) ? `${name}.map(item=>encodeURIComponent(item)).join(',')` : `${name}.join(',')` : void 0;
2360
+ }
2361
+ function processUrl(commandModel) {
2362
+ const uriParams = commandModel.uriParameters.reduce(
2363
+ (result, param) => {
2364
+ result[param.name] = param;
2365
+ return result;
2366
+ },
2367
+ {}
2368
+ );
2369
+ let url = [
2370
+ commandModel.serverUrlPath.replace(/\/$/, ""),
2371
+ commandModel.endpointUrlPath.replace(/^\//, "")
2372
+ ].join("/");
2373
+ url = url.replace(/\{(.+?)\}/g, (_, uriParameter) => {
2374
+ const uriParam = uriParams[uriParameter];
2375
+ if (!uriParam) {
2376
+ throw new Error(
2377
+ `unknown uri parameter ${uriParameter} in ${url} for ${commandModel.method} ${commandModel.endpointUrlPath} operation`
2378
+ );
2379
+ }
2380
+ const paramName = buildPropertyDestructureDeclaration(uriParameter).variableName;
2381
+ const value = encodeParam(paramName, uriParam.type, uriParam.required);
2382
+ return `\${${value}}`;
2383
+ });
2384
+ return url;
2385
+ }
2386
+ function generateUrlGetter(commandModel) {
2387
+ const processedUrl = processUrl(commandModel);
2388
+ const uriParams = commandModel.uriParameters;
2389
+ const hasUriParams = uriParams.length > 0;
2390
+ if (hasUriParams) {
2391
+ const paramDestructuring = uriParams.map((param) => {
2392
+ return buildPropertyDestructureDeclaration(param.name).code;
2393
+ });
2394
+ return code`
2395
+ get url() {
2396
+ const {
2397
+ ${paramDestructuring.join(",")}
2398
+ } = this.config;
2399
+ return \`${processedUrl}\`;
2400
+ }
2401
+ `;
2402
+ }
2403
+ return code`
2404
+ get url() {
2405
+ return \`${processedUrl}\`;
2406
+ }
2407
+ `;
2408
+ }
2349
2409
  const HEADER_CONTENT_TYPE = "Content-Type";
2350
2410
  const FETCH_SERVICE = {
2351
2411
  module: "@conduit-client/service-fetch-network/v1",
@@ -2420,7 +2480,8 @@ class HttpCommandGenerator extends CommandGenerator {
2420
2480
  * @returns {Code} A code snippet that defines the URL to be used in fetch operations.
2421
2481
  */
2422
2482
  generateUrl() {
2423
- return code`let url:Parameters<${FETCH_SERVICE}>[0]=\`${this.processUrl()}\`;`;
2483
+ const processedUrl = processUrl(this.commandModel);
2484
+ return code`let url:Parameters<${FETCH_SERVICE}>[0]=\`${processedUrl}\`;`;
2424
2485
  }
2425
2486
  /**
2426
2487
  * Generates headers necessary for the HTTP fetch based on the command model's requirements.
@@ -2514,57 +2575,6 @@ class HttpCommandGenerator extends CommandGenerator {
2514
2575
  }
2515
2576
  }
2516
2577
  }
2517
- /**
2518
- * Encodes a parameter based on its type.
2519
- * @param {string} name The name of the parameter to encode.
2520
- * @param {Type} type The type descriptor of the parameter.
2521
- * @returns {string | undefined} The encoded parameter value as a string, or undefined if the type is unsupported.
2522
- */
2523
- encodeParam(name, type, required) {
2524
- const isScalarArrayType = (t) => {
2525
- const canonicalizedType = canonicalizeType(t);
2526
- if (canonicalizedType.type !== "array") {
2527
- return false;
2528
- }
2529
- const canonicalizedItems = canonicalizeType(canonicalizedType.items);
2530
- return isCanonicalStringType(canonicalizedItems) || isCanonicalNumberType(canonicalizedItems);
2531
- };
2532
- const canonicalType = canonicalizeType(type);
2533
- return isCanonicalNumberType(canonicalType) ? `${name}` : isCanonicalStringType(canonicalType) ? required ? `encodeURIComponent(${name})` : `encodeURIComponent(${name} ? ${name} : '')` : isScalarArrayType(canonicalType) ? isCanonicalStringType(canonicalType.items) ? `${name}.map(item=>encodeURIComponent(item)).join(',')` : `${name}.join(',')` : void 0;
2534
- }
2535
- /**
2536
- * Processes the URL with replacements for URI parameters based on their values specified in the command model.
2537
- * @returns {string} The fully constructed URL with all necessary URI parameter substitutions.
2538
- */
2539
- processUrl() {
2540
- const uriParams = this.commandModel.uriParameters.reduce(
2541
- (result, param) => {
2542
- result[param.name] = param;
2543
- return result;
2544
- },
2545
- {}
2546
- );
2547
- let url = [
2548
- this.commandModel.serverUrlPath.replace(/\/$/, ""),
2549
- this.commandModel.endpointUrlPath.replace(/^\//, "")
2550
- ].join("/");
2551
- url = url.replace(/\{(.+?)\}/g, (_, uriParameter) => {
2552
- const uriParam = uriParams[uriParameter];
2553
- if (!uriParam) {
2554
- throw new Error(
2555
- `unknown uri parameter ${uriParameter} in ${url} for ${this.commandModel.method} ${this.commandModel.endpointUrlPath} operation`
2556
- );
2557
- }
2558
- const paramName = buildPropertyDestructureDeclaration(uriParameter).variableName;
2559
- const value = this.encodeParam(
2560
- paramName,
2561
- uriParam.type,
2562
- uriParam.required
2563
- );
2564
- return `\${${value}}`;
2565
- });
2566
- return url;
2567
- }
2568
2578
  generateCoerceError() {
2569
2579
  if (this.commandModel.errorStrategy.type === "fetchResponse") {
2570
2580
  return code`async coerceError(errorResponse: Response):Promise<${FETCH_RESPONSE$1}<unknown>>{
@@ -4112,14 +4122,10 @@ function GraphQLNormalizedCacheControlGeneratorMixin(Base, commandBaseClass) {
4112
4122
  }
4113
4123
  /**
4114
4124
  * Generate the URL getter for the GraphQL endpoint
4125
+ * Uses the same URL processing logic as HTTP commands to handle server URL, endpoint path, and URI parameters
4115
4126
  */
4116
4127
  generateUrl() {
4117
- const path2 = this.commandModel.endpointUrlPath;
4118
- return code`
4119
- get url() {
4120
- return '${path2}';
4121
- }
4122
- `;
4128
+ return generateUrlGetter(this.commandModel);
4123
4129
  }
4124
4130
  /**
4125
4131
  * Generate the endpoint property for GraphQL commands