@hyper-fetch/cli 7.2.6 → 7.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyper-fetch/cli",
3
- "version": "7.2.6",
3
+ "version": "7.4.0",
4
4
  "description": "Hyper Fetch cli for code generation and utilities",
5
5
  "author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
6
6
  "homepage": "https://hyperfetch.bettertyped.com/",
package/src/cli/index.ts CHANGED
@@ -5,6 +5,7 @@ import { select } from "@inquirer/prompts";
5
5
  import pkg from "../../package.json";
6
6
  import { generate } from "commands/generate";
7
7
  import { init } from "commands/init";
8
+ import { handleError } from "utils/handle-error";
8
9
 
9
10
  const program = new Command();
10
11
 
@@ -38,10 +39,12 @@ const main = async () => {
38
39
  description: cmd.description(),
39
40
  })),
40
41
  });
42
+ await program.parseAsync([process.argv[0], process.argv[1], chosenCommand]);
43
+ } else {
44
+ await program.parseAsync(process.argv);
41
45
  }
42
-
43
- await program.parseAsync([process.argv[0], process.argv[1], chosenCommand]);
44
46
  } catch (e) {
47
+ handleError(e);
45
48
  if (e instanceof Error) {
46
49
  if (e.message.includes("User force closed the prompt")) {
47
50
  process.exit(0);
@@ -117,20 +117,34 @@ export class OpenapiRequestGenerator {
117
117
  let currentLevel = schemaTree;
118
118
  // eslint-disable-next-line no-restricted-syntax
119
119
  for (const segment of segments) {
120
- const key = segment.startsWith(":") ? `$${segment.slice(1)}` : segment;
120
+ let key: string;
121
+ if (segment.startsWith(":")) {
122
+ // Parameter segment - keep the $ prefix
123
+ key = `$${segment.slice(1)}`;
124
+ } else if (segment.includes("-")) {
125
+ // Convert kebab-case to camelCase for path segments
126
+ key = segment.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
127
+ } else {
128
+ key = segment;
129
+ }
130
+
121
131
  if (!currentLevel[key]) {
122
132
  currentLevel[key] = {};
123
133
  }
124
134
  currentLevel = currentLevel[key];
125
135
  }
126
- currentLevel[method.toLowerCase()] = requestInstanceType;
136
+ // Prefix method names with $
137
+ currentLevel[`$${method.toLowerCase()}`] = requestInstanceType;
127
138
  });
128
139
 
129
140
  const sdkSchema = `export type SdkSchema<Client extends ClientInstance> = {\n${formatSchema(schemaTree)}\n}`;
130
141
 
131
142
  const createSdkFn = `
132
- export const createSdk = <Client extends ClientInstance>(client: Client) => {
133
- return coreCreateSdk<Client, SdkSchema<Client>>(client);
143
+
144
+ export type { Components };
145
+
146
+ export const createSdk = <Client extends ClientInstance>(client: Client, options?: Parameters<typeof coreCreateSdk>[1] | undefined) => {
147
+ return coreCreateSdk<Client, SdkSchema<Client>>(client, options);
134
148
  };
135
149
  `;
136
150
 
@@ -138,7 +152,7 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
138
152
  };
139
153
 
140
154
  static generateRequestInstanceType(
141
- { id, path: endpoint }: { id: string; path: string },
155
+ { id, path: endpoint, queryParamsRequired }: { id: string; path: string; queryParamsRequired?: boolean },
142
156
  types: Record<string, string>,
143
157
  ) {
144
158
  const Response = types[`${createTypeBaseName(id)}ResponseType`]
@@ -150,11 +164,18 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
150
164
  ? `${createTypeBaseName(id)}QueryParams`
151
165
  : undefined;
152
166
 
153
- return `Request<${Response}, ${Payload}, ${QueryParams}, ${LocalError}, "${endpoint}", Client>`;
167
+ const QueryParamsGeneric = QueryParams && !queryParamsRequired ? `${QueryParams} | undefined` : QueryParams;
168
+
169
+ return `Request<${Response}, ${Payload}, ${QueryParamsGeneric}, ${LocalError}, "${endpoint}", Client>`;
154
170
  }
155
171
 
156
172
  static generateHyperFetchRequest(
157
- { id, path: relPath, method }: { id: string; path: string; method: string },
173
+ {
174
+ id,
175
+ path: relPath,
176
+ method,
177
+ queryParamsRequired,
178
+ }: { id: string; path: string; method: string; queryParamsRequired?: boolean },
158
179
  types: Record<string, string>,
159
180
  ) {
160
181
  const Response = types[`${createTypeBaseName(id)}ResponseType`]
@@ -186,7 +207,8 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
186
207
  addToGenericType("error", LocalError);
187
208
  }
188
209
  if (QueryParams) {
189
- addToGenericType("query", QueryParams);
210
+ const key = !queryParamsRequired ? "queryParams?" : "queryParams";
211
+ addToGenericType(key, QueryParams);
190
212
  }
191
213
 
192
214
  if (genericType) {
@@ -265,6 +287,11 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
265
287
 
266
288
  const responseType = !lodash.isEmpty(responseTypePaths) ? responseTypePaths.join(" | ") : "any";
267
289
  const errorType = !lodash.isEmpty(errorTypePaths) ? errorTypePaths.join(" | ") : "undefined";
290
+ const queryParamsRequired = Array.isArray(operation.parameters)
291
+ ? operation.parameters.some((p) => {
292
+ return "in" in p && p.in === "query" && p.required === true;
293
+ })
294
+ : false;
268
295
 
269
296
  return {
270
297
  id: normalizedOperationId,
@@ -275,6 +302,7 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
275
302
  responseType,
276
303
  path: adjustPathParamsFormat(relPath),
277
304
  method: method ? method.toUpperCase() : HttpMethod.GET,
305
+ queryParamsRequired,
278
306
  };
279
307
  }
280
308
 
@@ -7,7 +7,7 @@ export function handleError(error: unknown) {
7
7
  logger.break();
8
8
  logger.error(`Something went wrong. Please check the error below for more details.`);
9
9
  logger.error(`If the problem persists, please open an issue on GitHub.`);
10
- logger.error("");
10
+ logger.break();
11
11
  if (typeof error === "string") {
12
12
  logger.error(error);
13
13
  logger.break();
@@ -30,6 +30,8 @@ export function handleError(error: unknown) {
30
30
  process.exit(1);
31
31
  }
32
32
 
33
+ logger.error(JSON.stringify({ error }));
34
+
33
35
  logger.break();
34
36
  process.exit(1);
35
37
  }