@hyper-fetch/cli 7.2.5 → 7.3.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.
@@ -1,10 +1,10 @@
1
1
  import DtsGenerator, { ExportedType } from "@anttiviljami/dtsgenerator/dist/core/dtsGenerator";
2
2
  import RefParser from "@apidevtools/json-schema-ref-parser";
3
3
  import { parseSchema } from "@anttiviljami/dtsgenerator/dist/core/type";
4
- import { find, chain, isEmpty } from "lodash";
4
+ import * as lodash from "lodash";
5
5
  import * as prettier from "prettier";
6
6
  import * as fs from "fs-extra";
7
- import * as path from "path";
7
+ import * as path from "node:path";
8
8
  import { createClient } from "@hyper-fetch/core";
9
9
 
10
10
  import { Document, Operation, GeneratedTypes } from "./openapi.types";
@@ -138,7 +138,7 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
138
138
  };
139
139
 
140
140
  static generateRequestInstanceType(
141
- { id, path: endpoint }: { id: string; path: string },
141
+ { id, path: endpoint, queryParamsRequired }: { id: string; path: string; queryParamsRequired?: boolean },
142
142
  types: Record<string, string>,
143
143
  ) {
144
144
  const Response = types[`${createTypeBaseName(id)}ResponseType`]
@@ -150,11 +150,19 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
150
150
  ? `${createTypeBaseName(id)}QueryParams`
151
151
  : undefined;
152
152
 
153
- return `Request<${Response}, ${Payload}, ${QueryParams}, ${LocalError}, "${endpoint}", Client>`;
153
+ const QueryParamsGeneric =
154
+ QueryParams && queryParamsRequired === false ? `${QueryParams} | undefined` : QueryParams;
155
+
156
+ return `Request<${Response}, ${Payload}, ${QueryParamsGeneric}, ${LocalError}, "${endpoint}", Client>`;
154
157
  }
155
158
 
156
159
  static generateHyperFetchRequest(
157
- { id, path: relPath, method }: { id: string; path: string; method: string },
160
+ {
161
+ id,
162
+ path: relPath,
163
+ method,
164
+ queryParamsRequired,
165
+ }: { id: string; path: string; method: string; queryParamsRequired?: boolean },
158
166
  types: Record<string, string>,
159
167
  ) {
160
168
  const Response = types[`${createTypeBaseName(id)}ResponseType`]
@@ -186,7 +194,8 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
186
194
  addToGenericType("error", LocalError);
187
195
  }
188
196
  if (QueryParams) {
189
- addToGenericType("query", QueryParams);
197
+ const key = queryParamsRequired === false ? "queryParams?" : "queryParams";
198
+ addToGenericType(key, QueryParams);
190
199
  }
191
200
 
192
201
  if (genericType) {
@@ -239,18 +248,22 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
239
248
  ) {
240
249
  const { operationId, method, path: relPath } = operation;
241
250
  const normalizedOperationId = normalizeOperationId(operationId);
242
- const pathParametersType = find(exportTypes, {
251
+ const pathParametersType = lodash.find(exportTypes, {
243
252
  schemaRef: `#/paths/${normalizedOperationId}/pathParameters`,
244
253
  })?.path;
245
- const queryParametersType = find(exportTypes, {
254
+ const queryParametersType = lodash.find(exportTypes, {
246
255
  schemaRef: `#/paths/${normalizedOperationId}/queryParameters`,
247
256
  })?.path;
248
- const requestBodyType = find(exportTypes, { schemaRef: `#/paths/${normalizedOperationId}/requestBody` })?.path;
249
- const responseTypePaths = chain(exportTypes)
257
+ const requestBodyType = lodash.find(exportTypes, {
258
+ schemaRef: `#/paths/${normalizedOperationId}/requestBody`,
259
+ })?.path;
260
+ const responseTypePaths = lodash
261
+ .chain(exportTypes)
250
262
  .filter(({ schemaRef }) => schemaRef.startsWith(`#/paths/${normalizedOperationId}/responses/2`))
251
263
  .map(({ path: responsePath }) => responsePath)
252
264
  .value();
253
- const errorTypePaths = chain(exportTypes)
265
+ const errorTypePaths = lodash
266
+ .chain(exportTypes)
254
267
  .filter(
255
268
  ({ schemaRef }) =>
256
269
  schemaRef.startsWith(`#/paths/${normalizedOperationId}/responses/4`) ||
@@ -259,8 +272,16 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
259
272
  .map(({ path: errorPath }) => errorPath)
260
273
  .value();
261
274
 
262
- const responseType = !isEmpty(responseTypePaths) ? responseTypePaths.join(" | ") : "any";
263
- const errorType = !isEmpty(errorTypePaths) ? errorTypePaths.join(" | ") : "undefined";
275
+ const responseType = !lodash.isEmpty(responseTypePaths) ? responseTypePaths.join(" | ") : "any";
276
+ const errorType = !lodash.isEmpty(errorTypePaths) ? errorTypePaths.join(" | ") : "undefined";
277
+ const queryParamsRequired = Array.isArray(operation.parameters)
278
+ ? operation.parameters.every((p) => {
279
+ if ("in" in p && p.in === "query" && p.required === false) {
280
+ return true;
281
+ }
282
+ return false;
283
+ })
284
+ : undefined;
264
285
 
265
286
  return {
266
287
  id: normalizedOperationId,
@@ -271,6 +292,7 @@ export const createSdk = <Client extends ClientInstance>(client: Client) => {
271
292
  responseType,
272
293
  path: adjustPathParamsFormat(relPath),
273
294
  method: method ? method.toUpperCase() : HttpMethod.GET,
295
+ queryParamsRequired,
274
296
  };
275
297
  }
276
298
 
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable no-param-reassign */
2
2
  import { Command } from "commander";
3
3
  import { z } from "zod";
4
- import path from "path";
5
- import fs from "fs-extra";
4
+ import * as path from "node:path";
5
+ import * as fs from "fs-extra";
6
6
  import { input, select, confirm } from "@inquirer/prompts";
7
7
 
8
8
  import { handleError } from "utils/handle-error";
@@ -1,8 +1,8 @@
1
1
  import { Command } from "commander";
2
2
  import { input, select } from "@inquirer/prompts";
3
3
  import { z } from "zod";
4
- import path from "path";
5
- import { existsSync, mkdir, readFileSync, writeFile } from "fs-extra";
4
+ import * as path from "node:path";
5
+ import * as fs from "fs-extra";
6
6
 
7
7
  import { handleError } from "../utils/handle-error";
8
8
  import { spinner } from "../utils/spinner";
@@ -103,16 +103,16 @@ export const init = new Command()
103
103
  {
104
104
  name: `Initialize API directory at ${relativePath}`,
105
105
  action: async () => {
106
- if (!existsSync(fullPath)) {
107
- await mkdir(fullPath, { recursive: true });
106
+ if (!fs.existsSync(fullPath)) {
107
+ await fs.mkdir(fullPath, { recursive: true });
108
108
  }
109
109
  },
110
110
  },
111
111
  {
112
112
  name: "Setup configuration",
113
113
  action: async (currentConfig) => {
114
- if (existsSync(configPath)) {
115
- const existingConfig = JSON.parse(readFileSync(configPath, "utf8"));
114
+ if (fs.existsSync(configPath)) {
115
+ const existingConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
116
116
  const { success, error, data } = configSchema.omit({ resolvedPaths: true }).safeParse({
117
117
  ...defaultConfig,
118
118
  ...existingConfig,
@@ -163,7 +163,7 @@ export const init = new Command()
163
163
 
164
164
  configSchema.omit({ resolvedPaths: true }).parse(finalConfig);
165
165
 
166
- await writeFile(configPath, JSON.stringify(finalConfig, null, 2));
166
+ await fs.writeFile(configPath, JSON.stringify(finalConfig, null, 2));
167
167
  },
168
168
  },
169
169
  ];
@@ -1,5 +1,5 @@
1
- import path from "path";
2
- import fs from "fs-extra";
1
+ import * as path from "node:path";
2
+ import * as fs from "fs-extra";
3
3
  import { loadConfig } from "tsconfig-paths";
4
4
 
5
5
  import { highlighter } from "utils/highlighter";
@@ -1,5 +1,5 @@
1
- import path from "path";
2
- import fs from "fs-extra";
1
+ import * as path from "node:path";
2
+ import * as fs from "fs-extra";
3
3
  import { z } from "zod";
4
4
 
5
5
  import { addOptionsSchema } from "commands/add";
@@ -1,5 +1,5 @@
1
- import path from "path";
2
- import fs from "fs-extra";
1
+ import * as path from "node:path";
2
+ import * as fs from "fs-extra";
3
3
  import { z } from "zod";
4
4
 
5
5
  import { generateOptionsSchema } from "commands/generate";
@@ -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
  }