[object Object]

← back to Cli Printing Press

refactor(cli): reuse generated operation id params (#496)

3110d5ec64f3ee56786a5bedea4a063d21ea0fe7 · 2026-05-01 22:32:28 -0700 · Trevin Chow

Files touched

Diff

commit 3110d5ec64f3ee56786a5bedea4a063d21ea0fe7
Author: Trevin Chow <trevin@trevinchow.com>
Date:   Fri May 1 22:32:28 2026 -0700

    refactor(cli): reuse generated operation id params (#496)
---
 internal/spec/spec.go | 52 +++++++++++++++++++++++----------------------------
 1 file changed, 23 insertions(+), 29 deletions(-)

diff --git a/internal/spec/spec.go b/internal/spec/spec.go
index 253203d5..7705323c 100644
--- a/internal/spec/spec.go
+++ b/internal/spec/spec.go
@@ -948,7 +948,9 @@ func (s *APISpec) expandOperations() {
 		if r.Endpoints == nil {
 			r.Endpoints = make(map[string]Endpoint)
 		}
-		idParam := singularize(name) + "Id"
+		singularName := singularize(name)
+		idParam := singularName + "Id"
+		idPath := r.Path + "/{" + idParam + "}"
 		for _, op := range r.Operations {
 			// Skip if an explicit endpoint already exists with this name
 			if _, exists := r.Endpoints[op]; exists {
@@ -965,47 +967,29 @@ func (s *APISpec) expandOperations() {
 			case "get":
 				r.Endpoints["get"] = Endpoint{
 					Method:      "GET",
-					Path:        r.Path + "/{" + idParam + "}",
-					Description: "Get a " + singularize(name) + " by ID",
-					Params: []Param{{
-						Name:        idParam,
-						Type:        "string",
-						Required:    true,
-						Positional:  true,
-						Description: singularize(name) + " ID",
-					}},
+					Path:        idPath,
+					Description: "Get a " + singularName + " by ID",
+					Params:      operationIDParams(idParam, singularName),
 				}
 			case "create":
 				r.Endpoints["create"] = Endpoint{
 					Method:      "POST",
 					Path:        r.Path,
-					Description: "Create a new " + singularize(name),
+					Description: "Create a new " + singularName,
 				}
 			case "update":
 				r.Endpoints["update"] = Endpoint{
 					Method:      "PATCH",
-					Path:        r.Path + "/{" + idParam + "}",
-					Description: "Update a " + singularize(name),
-					Params: []Param{{
-						Name:        idParam,
-						Type:        "string",
-						Required:    true,
-						Positional:  true,
-						Description: singularize(name) + " ID",
-					}},
+					Path:        idPath,
+					Description: "Update a " + singularName,
+					Params:      operationIDParams(idParam, singularName),
 				}
 			case "delete":
 				r.Endpoints["delete"] = Endpoint{
 					Method:      "DELETE",
-					Path:        r.Path + "/{" + idParam + "}",
-					Description: "Delete a " + singularize(name),
-					Params: []Param{{
-						Name:        idParam,
-						Type:        "string",
-						Required:    true,
-						Positional:  true,
-						Description: singularize(name) + " ID",
-					}},
+					Path:        idPath,
+					Description: "Delete a " + singularName,
+					Params:      operationIDParams(idParam, singularName),
 				}
 			case "search":
 				r.Endpoints["search"] = Endpoint{
@@ -1025,6 +1009,16 @@ func (s *APISpec) expandOperations() {
 	}
 }
 
+func operationIDParams(idParam, singularName string) []Param {
+	return []Param{{
+		Name:        idParam,
+		Type:        "string",
+		Required:    true,
+		Positional:  true,
+		Description: singularName + " ID",
+	}}
+}
+
 // singularize returns a simple singular form of a plural noun.
 // Handles common patterns; irregular forms use a lookup table.
 func singularize(s string) string {

← e4773196 docs(cli): reorganize README, add badges and support section  ·  back to Cli Printing Press  ·  chore(main): release 3.4.2 (#497) f84853b7 →