typedapi-client-helpers 0.3.1 → 0.3.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.
- package/CHANGELOG.md +0 -1
- package/README.md +5 -6
- package/dist/cjs/index.js +0 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/handleApiResponse.js +10 -6
- package/dist/cjs/utils/handleApiResponse.js.map +1 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/handleApiResponse.js +10 -6
- package/dist/esm/utils/handleApiResponse.js.map +1 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/interfaces/ApiResult.d.ts +1 -1
- package/dist/types/interfaces/ApiResult.d.ts.map +1 -1
- package/dist/types/types/ApiMethodOptions.d.ts +2 -2
- package/dist/types/types/ApiMethodOptions.d.ts.map +1 -1
- package/dist/types/utils/handleApiResponse.d.ts +1 -0
- package/dist/types/utils/handleApiResponse.d.ts.map +1 -1
- package/package.json +2 -1
- package/scripts/generate-api-minimal.mjs +30 -1
- package/scripts/openapi-generator/index.mjs +7 -26
- package/scripts/openapi-generator/method-template.mjs +14 -40
- package/scripts/openapi-generator/openapi-utils.mjs +411 -411
- package/scripts/openapi-generator/schema.mjs +0 -156
- package/dist/cjs/types/ApiMethodArguments.js +0 -3
- package/dist/cjs/types/ApiMethodArguments.js.map +0 -1
- package/dist/cjs/types/HttpResponse.js +0 -3
- package/dist/cjs/types/HttpResponse.js.map +0 -1
- package/dist/cjs/utils/extractArgsCallbacksAndParams.js +0 -51
- package/dist/cjs/utils/extractArgsCallbacksAndParams.js.map +0 -1
- package/dist/cjs/utils/extractArgsToastsAndParams.js +0 -49
- package/dist/cjs/utils/extractArgsToastsAndParams.js.map +0 -1
- package/dist/cjs/utils/wireMapping.js +0 -123
- package/dist/cjs/utils/wireMapping.js.map +0 -1
- package/dist/esm/types/ApiMethodArguments.js +0 -2
- package/dist/esm/types/ApiMethodArguments.js.map +0 -1
- package/dist/esm/types/HttpResponse.js +0 -2
- package/dist/esm/types/HttpResponse.js.map +0 -1
- package/dist/esm/utils/extractArgsCallbacksAndParams.js +0 -48
- package/dist/esm/utils/extractArgsCallbacksAndParams.js.map +0 -1
- package/dist/esm/utils/extractArgsToastsAndParams.js +0 -46
- package/dist/esm/utils/extractArgsToastsAndParams.js.map +0 -1
- package/dist/esm/utils/wireMapping.js +0 -119
- package/dist/esm/utils/wireMapping.js.map +0 -1
- package/dist/types/types/ApiMethodArguments.d.ts +0 -6
- package/dist/types/types/ApiMethodArguments.d.ts.map +0 -1
- package/dist/types/types/HttpResponse.d.ts +0 -2
- package/dist/types/types/HttpResponse.d.ts.map +0 -1
- package/dist/types/utils/extractArgsCallbacksAndParams.d.ts +0 -22
- package/dist/types/utils/extractArgsCallbacksAndParams.d.ts.map +0 -1
- package/dist/types/utils/extractArgsToastsAndParams.d.ts +0 -20
- package/dist/types/utils/extractArgsToastsAndParams.d.ts.map +0 -1
- package/dist/types/utils/wireMapping.d.ts +0 -30
- package/dist/types/utils/wireMapping.d.ts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -17,7 +17,6 @@ All notable changes to `typedapi-client-helpers` are documented in this file.
|
|
|
17
17
|
* Added configurable frontend method naming through `typedApiMethodNameStyle`.
|
|
18
18
|
* Added `typedApiPrefixMethodNamesWithController` for controller-prefixed action names.
|
|
19
19
|
* Added controller-name fallback detection from TypedApi metadata, OpenAPI tags, and route paths.
|
|
20
|
-
* Added `typedapi.manifest.json` containing generator, contract, source, and naming information.
|
|
21
20
|
* Added `--check`, `--strict`, `--offline`, and `--verbose` generator modes.
|
|
22
21
|
* Added configurable Swagger download timeouts.
|
|
23
22
|
* Added global and per-request timeout support.
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ A small Fetch runtime and OpenAPI 3 TypeScript generator designed to work with `
|
|
|
4
4
|
|
|
5
5
|
## Version 0.3 highlights
|
|
6
6
|
|
|
7
|
-
- Lowercases only the first character of generated TypeScript members
|
|
7
|
+
- Lowercases only the first character of generated TypeScript members.
|
|
8
8
|
- Sanitizes operation IDs and rejects duplicates before writing output.
|
|
9
9
|
- Can name frontend functions from either the unique OpenAPI operation ID or the original ASP.NET controller action name.
|
|
10
10
|
- Supports path, query, header, cookie, and request-body inputs in the same operation.
|
|
@@ -12,7 +12,7 @@ A small Fetch runtime and OpenAPI 3 TypeScript generator designed to work with `
|
|
|
12
12
|
- Supports `allOf` composition with local properties.
|
|
13
13
|
- Generates typed unions for documented non-success responses.
|
|
14
14
|
- Uses a structured `ApiHttpError` when an operation has no documented error schema.
|
|
15
|
-
-
|
|
15
|
+
- Generates API contracts and controller method files without creating a root API barrel file.
|
|
16
16
|
- Writes generated output transactionally, with a Windows-safe in-place fallback when directory renames are blocked by editors or file watchers.
|
|
17
17
|
- Adds `--check`, `--strict`, `--offline`, and `--verbose` CLI modes.
|
|
18
18
|
- Improves cancellation, timeouts, header merging, SSR safety, and malformed-response handling.
|
|
@@ -61,9 +61,8 @@ src/api/
|
|
|
61
61
|
├── generated/
|
|
62
62
|
│ ├── data-contracts.ts
|
|
63
63
|
│ └── http-client.ts
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
└── typedapi.manifest.json
|
|
64
|
+
└── methods/
|
|
65
|
+
└── Products.api.ts
|
|
67
66
|
```
|
|
68
67
|
|
|
69
68
|
The generator intentionally does not create `src/api/index.ts`. Import generated methods and contracts directly from their files:
|
|
@@ -224,7 +223,7 @@ configureApiClient({
|
|
|
224
223
|
setSecurityData("access-token");
|
|
225
224
|
```
|
|
226
225
|
|
|
227
|
-
The runtime also exports `createApiClient`, `request`, `abortRequest`, `mergeHeaders`, `toRequestHeaders`, `toCookieHeader`, `handleApiResponse`, `buildQuery`, `toFormData
|
|
226
|
+
The runtime also exports `createApiClient`, `request`, `abortRequest`, `mergeHeaders`, `toRequestHeaders`, `toCookieHeader`, `handleApiResponse`, `buildQuery`, `toFormData`.
|
|
228
227
|
|
|
229
228
|
## Backend pairing
|
|
230
229
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -21,12 +21,8 @@ __exportStar(require("./interfaces/SortType"), exports);
|
|
|
21
21
|
__exportStar(require("./types/ApiMethodTypes"), exports);
|
|
22
22
|
__exportStar(require("./types/ApiCallOptions"), exports);
|
|
23
23
|
__exportStar(require("./types/ApiMethodOptions"), exports);
|
|
24
|
-
__exportStar(require("./types/ApiMethodArguments"), exports);
|
|
25
24
|
__exportStar(require("./utils/buildQuery"), exports);
|
|
26
|
-
__exportStar(require("./utils/extractArgsToastsAndParams"), exports);
|
|
27
|
-
__exportStar(require("./utils/extractArgsCallbacksAndParams"), exports);
|
|
28
25
|
__exportStar(require("./utils/handleApiResponse"), exports);
|
|
29
26
|
__exportStar(require("./utils/SortTypeFromSortDirection"), exports);
|
|
30
27
|
__exportStar(require("./utils/toFormData"), exports);
|
|
31
|
-
__exportStar(require("./utils/wireMapping"), exports);
|
|
32
28
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAE7B,yDAAuC;AACvC,sDAAoC;AACpC,wDAAsC;AAEtC,yDAAuC;AACvC,yDAAuC;AACvC,2DAAyC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAE7B,yDAAuC;AACvC,sDAAoC;AACpC,wDAAsC;AAEtC,yDAAuC;AACvC,yDAAuC;AACvC,2DAAyC;AAEzC,qDAAmC;AACnC,4DAA0C;AAC1C,oEAAkD;AAClD,qDAAmC"}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createApiHttpError = createApiHttpError;
|
|
4
4
|
exports.handleApiResponse = handleApiResponse;
|
|
5
|
+
const defaultUnknownErrorMessage = "An unknown error occurred.";
|
|
5
6
|
class ResponseParseError extends Error {
|
|
6
7
|
constructor(status, rawBody, parseCause) {
|
|
7
8
|
super(`Could not parse JSON response with status ${status}.`);
|
|
@@ -48,7 +49,7 @@ function createApiHttpError(status, body) {
|
|
|
48
49
|
body: body,
|
|
49
50
|
};
|
|
50
51
|
}
|
|
51
|
-
function clientError(error) {
|
|
52
|
+
function clientError(error, fallbackErrorMessage) {
|
|
52
53
|
if (error instanceof ResponseParseError) {
|
|
53
54
|
return {
|
|
54
55
|
kind: "parse",
|
|
@@ -63,7 +64,7 @@ function clientError(error) {
|
|
|
63
64
|
if (name === "AbortError" || name === "TimeoutError") {
|
|
64
65
|
return { kind: "aborted", cause: error };
|
|
65
66
|
}
|
|
66
|
-
return
|
|
67
|
+
return fallbackErrorMessage ?? defaultUnknownErrorMessage;
|
|
67
68
|
}
|
|
68
69
|
async function handleApiResponse(call, options) {
|
|
69
70
|
let result;
|
|
@@ -85,7 +86,7 @@ async function handleApiResponse(call, options) {
|
|
|
85
86
|
ok: false,
|
|
86
87
|
status: response.status,
|
|
87
88
|
response: undefined,
|
|
88
|
-
error: clientError(error),
|
|
89
|
+
error: clientError(error, options?.fallbackErrorMessage),
|
|
89
90
|
};
|
|
90
91
|
}
|
|
91
92
|
}
|
|
@@ -93,9 +94,12 @@ async function handleApiResponse(call, options) {
|
|
|
93
94
|
if (isResponseLike(error)) {
|
|
94
95
|
try {
|
|
95
96
|
const rawErrorData = await readResponseBody(error.clone());
|
|
96
|
-
const
|
|
97
|
+
const transformedError = options?.transformError
|
|
97
98
|
? options.transformError(rawErrorData, error)
|
|
98
99
|
: rawErrorData;
|
|
100
|
+
const errorData = transformedError === undefined
|
|
101
|
+
? options?.fallbackErrorMessage ?? defaultUnknownErrorMessage
|
|
102
|
+
: transformedError;
|
|
99
103
|
result = {
|
|
100
104
|
ok: false,
|
|
101
105
|
status: error.status,
|
|
@@ -108,7 +112,7 @@ async function handleApiResponse(call, options) {
|
|
|
108
112
|
ok: false,
|
|
109
113
|
status: error.status,
|
|
110
114
|
response: undefined,
|
|
111
|
-
error: clientError(parseError),
|
|
115
|
+
error: clientError(parseError, options?.fallbackErrorMessage),
|
|
112
116
|
};
|
|
113
117
|
}
|
|
114
118
|
}
|
|
@@ -117,7 +121,7 @@ async function handleApiResponse(call, options) {
|
|
|
117
121
|
ok: false,
|
|
118
122
|
status: 0,
|
|
119
123
|
response: undefined,
|
|
120
|
-
error: clientError(error),
|
|
124
|
+
error: clientError(error, options?.fallbackErrorMessage),
|
|
121
125
|
};
|
|
122
126
|
}
|
|
123
127
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleApiResponse.js","sourceRoot":"","sources":["../../../src/utils/handleApiResponse.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"handleApiResponse.js","sourceRoot":"","sources":["../../../src/utils/handleApiResponse.ts"],"names":[],"mappings":";;AAgEA,gDAMC;AAwBD,8CAkEC;AA5JD,MAAM,0BAA0B,GAAG,4BAA4B,CAAC;AAUhE,MAAM,kBAAmB,SAAQ,KAAK;IACpC,YACW,MAAc,EACd,OAAe,EACf,UAAmB;QAE5B,KAAK,CAAC,6CAA6C,MAAM,GAAG,CAAC,CAAC;QAJrD,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAQ;QACf,eAAU,GAAV,UAAU,CAAS;QAG5B,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,KAAK,UAAU,gBAAgB,CAC7B,QAAkB;IAElB,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG;QAAE,OAAO,SAAS,CAAC;IAEzE,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,WAAW,EAAE,IAAI,EA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AC,CAAC;IAEtC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -5,12 +5,8 @@ export * from "./interfaces/SortType.js";
|
|
|
5
5
|
export * from "./types/ApiMethodTypes.js";
|
|
6
6
|
export * from "./types/ApiCallOptions.js";
|
|
7
7
|
export * from "./types/ApiMethodOptions.js";
|
|
8
|
-
export * from "./types/ApiMethodArguments.js";
|
|
9
8
|
export * from "./utils/buildQuery.js";
|
|
10
|
-
export * from "./utils/extractArgsToastsAndParams.js";
|
|
11
|
-
export * from "./utils/extractArgsCallbacksAndParams.js";
|
|
12
9
|
export * from "./utils/handleApiResponse.js";
|
|
13
10
|
export * from "./utils/SortTypeFromSortDirection.js";
|
|
14
11
|
export * from "./utils/toFormData.js";
|
|
15
|
-
export * from "./utils/wireMapping.js";
|
|
16
12
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAE7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAE7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AAEzC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mCAAmC,CAAC;AAClD,cAAc,oBAAoB,CAAC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const defaultUnknownErrorMessage = "An unknown error occurred.";
|
|
1
2
|
class ResponseParseError extends Error {
|
|
2
3
|
constructor(status, rawBody, parseCause) {
|
|
3
4
|
super(`Could not parse JSON response with status ${status}.`);
|
|
@@ -44,7 +45,7 @@ export function createApiHttpError(status, body) {
|
|
|
44
45
|
body: body,
|
|
45
46
|
};
|
|
46
47
|
}
|
|
47
|
-
function clientError(error) {
|
|
48
|
+
function clientError(error, fallbackErrorMessage) {
|
|
48
49
|
if (error instanceof ResponseParseError) {
|
|
49
50
|
return {
|
|
50
51
|
kind: "parse",
|
|
@@ -59,7 +60,7 @@ function clientError(error) {
|
|
|
59
60
|
if (name === "AbortError" || name === "TimeoutError") {
|
|
60
61
|
return { kind: "aborted", cause: error };
|
|
61
62
|
}
|
|
62
|
-
return
|
|
63
|
+
return fallbackErrorMessage ?? defaultUnknownErrorMessage;
|
|
63
64
|
}
|
|
64
65
|
export async function handleApiResponse(call, options) {
|
|
65
66
|
let result;
|
|
@@ -81,7 +82,7 @@ export async function handleApiResponse(call, options) {
|
|
|
81
82
|
ok: false,
|
|
82
83
|
status: response.status,
|
|
83
84
|
response: undefined,
|
|
84
|
-
error: clientError(error),
|
|
85
|
+
error: clientError(error, options?.fallbackErrorMessage),
|
|
85
86
|
};
|
|
86
87
|
}
|
|
87
88
|
}
|
|
@@ -89,9 +90,12 @@ export async function handleApiResponse(call, options) {
|
|
|
89
90
|
if (isResponseLike(error)) {
|
|
90
91
|
try {
|
|
91
92
|
const rawErrorData = await readResponseBody(error.clone());
|
|
92
|
-
const
|
|
93
|
+
const transformedError = options?.transformError
|
|
93
94
|
? options.transformError(rawErrorData, error)
|
|
94
95
|
: rawErrorData;
|
|
96
|
+
const errorData = transformedError === undefined
|
|
97
|
+
? options?.fallbackErrorMessage ?? defaultUnknownErrorMessage
|
|
98
|
+
: transformedError;
|
|
95
99
|
result = {
|
|
96
100
|
ok: false,
|
|
97
101
|
status: error.status,
|
|
@@ -104,7 +108,7 @@ export async function handleApiResponse(call, options) {
|
|
|
104
108
|
ok: false,
|
|
105
109
|
status: error.status,
|
|
106
110
|
response: undefined,
|
|
107
|
-
error: clientError(parseError),
|
|
111
|
+
error: clientError(parseError, options?.fallbackErrorMessage),
|
|
108
112
|
};
|
|
109
113
|
}
|
|
110
114
|
}
|
|
@@ -113,7 +117,7 @@ export async function handleApiResponse(call, options) {
|
|
|
113
117
|
ok: false,
|
|
114
118
|
status: 0,
|
|
115
119
|
response: undefined,
|
|
116
|
-
error: clientError(error),
|
|
120
|
+
error: clientError(error, options?.fallbackErrorMessage),
|
|
117
121
|
};
|
|
118
122
|
}
|
|
119
123
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleApiResponse.js","sourceRoot":"","sources":["../../../src/utils/handleApiResponse.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"handleApiResponse.js","sourceRoot":"","sources":["../../../src/utils/handleApiResponse.ts"],"names":[],"mappings":"AAIA,MAAM,0BAA0B,GAAG,4BAA4B,CAAC;AAUhE,MAAM,kBAAmB,SAAQ,KAAK;IACpC,YACW,MAAc,EACd,OAAe,EACf,UAAmB;QAE5B,KAAK,CAAC,6CAA6C,MAAM,GAAG,CAAC,CAAC;QAJrD,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAQ;QACf,eAAU,GAAV,UAAU,CAAS;QAG5B,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,KAAK,UAAU,gBAAgB,CAC7B,QAAkB;IAElB,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG;QAAE,OAAO,SAAS,CAAC;IAEzE,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;IAC9E,IACE,WAAW,CAAC,QAAQ,CAA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CAAC;AAChB,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -5,12 +5,8 @@ export * from "./interfaces/SortType";
|
|
|
5
5
|
export * from "./types/ApiMethodTypes";
|
|
6
6
|
export * from "./types/ApiCallOptions";
|
|
7
7
|
export * from "./types/ApiMethodOptions";
|
|
8
|
-
export * from "./types/ApiMethodArguments";
|
|
9
8
|
export * from "./utils/buildQuery";
|
|
10
|
-
export * from "./utils/extractArgsToastsAndParams";
|
|
11
|
-
export * from "./utils/extractArgsCallbacksAndParams";
|
|
12
9
|
export * from "./utils/handleApiResponse";
|
|
13
10
|
export * from "./utils/SortTypeFromSortDirection";
|
|
14
11
|
export * from "./utils/toFormData";
|
|
15
|
-
export * from "./utils/wireMapping";
|
|
16
12
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAE7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAE7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AAEzC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mCAAmC,CAAC;AAClD,cAAc,oBAAoB,CAAC"}
|
|
@@ -30,7 +30,7 @@ export type ApiResult<TResponse, TError = unknown> = {
|
|
|
30
30
|
ok: false;
|
|
31
31
|
status: number;
|
|
32
32
|
response?: undefined;
|
|
33
|
-
error: TError | ApiClientError;
|
|
33
|
+
error: TError | ApiClientError | string;
|
|
34
34
|
};
|
|
35
35
|
export type ApiSuccessResult<TResponse, TError = unknown> = Extract<ApiResult<TResponse, TError>, {
|
|
36
36
|
ok: true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiResult.d.ts","sourceRoot":"","sources":["../../../src/interfaces/ApiResult.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GACxB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,IAAI,GACJ,SAAS,GACT,gBAAgB,EAAE,GAClB;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,CAAA;CAAE,CAAC;AAExC,wEAAwE;AACxE,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,gBAAgB,CAAC;CACxB,CAAC;AAEF,0FAA0F;AAC1F,MAAM,MAAM,cAAc,GACtB;IACE,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CAChB,GACD;IACE,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CAChB,GACD;IACE,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CAChB,CAAC;AAEN,yEAAyE;AACzE,MAAM,MAAM,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,IAC7C;IACE,EAAE,EAAE,IAAI,CAAC;IACT,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,SAAS,CAAC;IACpB,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,EAAE,EAAE,KAAK,CAAC;IACV,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CA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AAO,IAAI,OAAO,CACjE,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,EAC5B;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,CACb,CAAC;AAEF,MAAM,MAAM,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,IAAI,OAAO,CAC/D,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,EAC5B;IAAE,EAAE,EAAE,KAAK,CAAA;CAAE,CACd,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ApiErrorHandler, ApiSuccessHandler } from "./ApiCallOptions";
|
|
2
|
-
import { RuntimeRequestParams } from "./
|
|
1
|
+
import type { ApiErrorHandler, ApiSuccessHandler } from "./ApiCallOptions";
|
|
2
|
+
import type { RuntimeRequestParams } from "./httpClientTypes";
|
|
3
3
|
export type ApiMethodOptions<TResponse, TError = unknown, TRequestParams = RuntimeRequestParams> = {
|
|
4
4
|
onSuccess?: ApiSuccessHandler<TResponse>;
|
|
5
5
|
onError?: ApiErrorHandler<TResponse, TError>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiMethodOptions.d.ts","sourceRoot":"","sources":["../../../src/types/ApiMethodOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"ApiMethodOptions.d.ts","sourceRoot":"","sources":["../../../src/types/ApiMethodOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC3E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,MAAM,MAAM,gBAAgB,CAC1B,SAAS,EACT,MAAM,GAAG,OAAO,EAChB,cAAc,GAAG,oBAAoB,IACnC;IACF,SAAS,CAAC,EAAE,iBAAiB,CAAC,SAAS,CAAC,CAAC;IACzC,OAAO,CAAC,EAAE,eAAe,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC7C,MAAM,CAAC,EAAE,cAAc,CAAC;CACzB,CAAC"}
|
|
@@ -6,6 +6,7 @@ export type HandleApiResponseOptions<TResponse, TError = unknown> = {
|
|
|
6
6
|
onError?: ApiErrorHandler<TResponse, TError>;
|
|
7
7
|
transformResponse?: (value: unknown) => TResponse;
|
|
8
8
|
transformError?: (value: unknown, response: Response) => TError;
|
|
9
|
+
fallbackErrorMessage?: string;
|
|
9
10
|
};
|
|
10
11
|
export declare function createApiHttpError(status: number, body: unknown): ApiHttpError;
|
|
11
12
|
export declare function handleApiResponse<TResponse, TError = unknown>(call: () => Promise<HttpResponse<TResponse, TError>>, options?: HandleApiResponseOptions<TResponse, TError>): Promise<ApiResult<TResponse, TError>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleApiResponse.d.ts","sourceRoot":"","sources":["../../../src/utils/handleApiResponse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,YAAY,EAAoB,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzG,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAClF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"handleApiResponse.d.ts","sourceRoot":"","sources":["../../../src/utils/handleApiResponse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,YAAY,EAAoB,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzG,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAClF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD,MAAM,MAAM,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,IAAI;IAClE,SAAS,CAAC,EAAE,iBAAiB,CAAC,SAAS,CAAC,CAAC;IACzC,OAAO,CAAC,EAAE,eAAe,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC7C,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,SAAS,CAAC;IAClD,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,KAAK,MAAM,CAAC;IAChE,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B,CAAC;AAoDF,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,YAAY,CAM9E;AAwBD,wBAAsB,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,EACjE,IAAI,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,EACpD,OAAO,CAAC,EAAE,wBAAwB,CAAC,SAAS,EAAE,MAAM,CAAC,GACpD,OAAO,CAAC,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CA+DvC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typedapi-client-helpers",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Reusable typed OpenAPI client helpers.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"typedApiDefaultFunctionsPath": "src/defaultApiFunctions",
|
|
67
67
|
"typedApiDefaultSuccessHandler": "handleGoodResult",
|
|
68
68
|
"typedApiDefaultErrorHandler": "handleErrors",
|
|
69
|
+
"typedApiDefaultErrorMessage": "unknownErrorMessage",
|
|
69
70
|
"typedApiDefaultResponseAsSuccess": false,
|
|
70
71
|
"typedApiGenerateUnionEnums": false,
|
|
71
72
|
"typedApiEnumNamesAsValues": false,
|
|
@@ -118,6 +118,13 @@ const defaultErrorHandlerName = getStringSetting(
|
|
|
118
118
|
"handleErrors",
|
|
119
119
|
);
|
|
120
120
|
|
|
121
|
+
const defaultErrorMessageName = getStringSetting(
|
|
122
|
+
process.env.TYPED_API_DEFAULT_ERROR_MESSAGE,
|
|
123
|
+
process.env.npm_config_typed_api_default_error_message,
|
|
124
|
+
config.typedApiDefaultErrorMessage,
|
|
125
|
+
"unknownErrorMessage",
|
|
126
|
+
);
|
|
127
|
+
|
|
121
128
|
const generatorOptions = {
|
|
122
129
|
cleanOutput: getBooleanSetting(
|
|
123
130
|
process.env.TYPED_API_CLEAN_OUTPUT,
|
|
@@ -408,6 +415,7 @@ function createDefaultFunctionsFileIfMissing(filePath) {
|
|
|
408
415
|
const resolvedFilePath = extension ? filePath : `${filePath}.ts`;
|
|
409
416
|
|
|
410
417
|
if (fs.existsSync(resolvedFilePath)) {
|
|
418
|
+
addDefaultErrorMessageIfMissing(resolvedFilePath);
|
|
411
419
|
return resolvedFilePath;
|
|
412
420
|
}
|
|
413
421
|
|
|
@@ -432,6 +440,8 @@ export function ${defaultErrorHandlerName}<T>(
|
|
|
432
440
|
): void | Promise<void> {
|
|
433
441
|
// Add your default error handling here.
|
|
434
442
|
}
|
|
443
|
+
|
|
444
|
+
export const ${defaultErrorMessageName} = "An unknown error occurred.";
|
|
435
445
|
`,
|
|
436
446
|
);
|
|
437
447
|
|
|
@@ -442,6 +452,22 @@ export function ${defaultErrorHandlerName}<T>(
|
|
|
442
452
|
return resolvedFilePath;
|
|
443
453
|
}
|
|
444
454
|
|
|
455
|
+
|
|
456
|
+
function addDefaultErrorMessageIfMissing(filePath) {
|
|
457
|
+
const source = fs.readFileSync(filePath, "utf8");
|
|
458
|
+
if (fileExportsName(source, defaultErrorMessageName)) return;
|
|
459
|
+
|
|
460
|
+
const separator = source.endsWith("\n") ? "\n" : "\n\n";
|
|
461
|
+
fs.appendFileSync(
|
|
462
|
+
filePath,
|
|
463
|
+
`${separator}export const ${defaultErrorMessageName} = "An unknown error occurred.";\n`,
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
console.log(
|
|
467
|
+
`Added default API error message to: ${path.relative(process.cwd(), filePath)}`,
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
445
471
|
function toRelativeImportPath(fromDirectory, filePath) {
|
|
446
472
|
let relativePath = path.relative(fromDirectory, filePath);
|
|
447
473
|
|
|
@@ -508,11 +534,13 @@ function resolveDefaultHandlers() {
|
|
|
508
534
|
const source = fs.readFileSync(defaultFunctionsFile, "utf8");
|
|
509
535
|
const hasSuccessHandler = fileExportsName(source, defaultSuccessHandlerName);
|
|
510
536
|
const hasErrorHandler = fileExportsName(source, defaultErrorHandlerName);
|
|
537
|
+
const hasErrorMessage = fileExportsName(source, defaultErrorMessageName);
|
|
511
538
|
|
|
512
|
-
if (!hasSuccessHandler || !hasErrorHandler) {
|
|
539
|
+
if (!hasSuccessHandler || !hasErrorHandler || !hasErrorMessage) {
|
|
513
540
|
const missingNames = [
|
|
514
541
|
...(!hasSuccessHandler ? [defaultSuccessHandlerName] : []),
|
|
515
542
|
...(!hasErrorHandler ? [defaultErrorHandlerName] : []),
|
|
543
|
+
...(!hasErrorMessage ? [defaultErrorMessageName] : []),
|
|
516
544
|
];
|
|
517
545
|
|
|
518
546
|
console.warn(
|
|
@@ -529,6 +557,7 @@ function resolveDefaultHandlers() {
|
|
|
529
557
|
path: toRelativeImportPath(methodsDir, defaultFunctionsFile),
|
|
530
558
|
success: defaultSuccessHandlerName,
|
|
531
559
|
error: defaultErrorHandlerName,
|
|
560
|
+
errorMessage: defaultErrorMessageName,
|
|
532
561
|
};
|
|
533
562
|
}
|
|
534
563
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
1
|
import fs from "node:fs";
|
|
3
2
|
import os from "node:os";
|
|
4
3
|
import path from "node:path";
|
|
@@ -11,7 +10,8 @@ import {
|
|
|
11
10
|
methodFileNameForController,
|
|
12
11
|
} from "./method-template.mjs";
|
|
13
12
|
|
|
14
|
-
const MANAGED_TOP_LEVEL = ["generated", "methods", "index.ts"
|
|
13
|
+
const MANAGED_TOP_LEVEL = ["generated", "methods", "index.ts"];
|
|
14
|
+
const LEGACY_MANAGED_TOP_LEVEL = ["typedapi.manifest.json"];
|
|
15
15
|
|
|
16
16
|
export async function generateApi(options) {
|
|
17
17
|
const inputPath = path.resolve(options.input);
|
|
@@ -73,25 +73,6 @@ export async function generateApi(options) {
|
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
const schemaHash = crypto.createHash("sha256").update(source).digest("hex");
|
|
77
|
-
files.set(
|
|
78
|
-
"typedapi.manifest.json",
|
|
79
|
-
`${JSON.stringify(
|
|
80
|
-
{
|
|
81
|
-
contractVersion: 1,
|
|
82
|
-
generator: options.runtimePackageName ?? "typedapi-client-helpers",
|
|
83
|
-
generatorVersion: options.generatorVersion ?? "unknown",
|
|
84
|
-
schemaSha256: schemaHash,
|
|
85
|
-
producer: openApi["x-typedapi"] ?? null,
|
|
86
|
-
operationCount: operations.length,
|
|
87
|
-
methodNameStyle: normalizedOptions.methodNameStyle,
|
|
88
|
-
prefixMethodNamesWithController:
|
|
89
|
-
normalizedOptions.prefixMethodNamesWithController,
|
|
90
|
-
},
|
|
91
|
-
null,
|
|
92
|
-
2,
|
|
93
|
-
)}\n`,
|
|
94
|
-
);
|
|
95
76
|
|
|
96
77
|
const changes = compareOutput(output, files);
|
|
97
78
|
if (options.check) {
|
|
@@ -135,7 +116,7 @@ function compareOutput(output, files) {
|
|
|
135
116
|
|
|
136
117
|
function listManagedFiles(output) {
|
|
137
118
|
const result = [];
|
|
138
|
-
for (const topLevel of MANAGED_TOP_LEVEL) {
|
|
119
|
+
for (const topLevel of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
139
120
|
const target = path.join(output, topLevel);
|
|
140
121
|
if (!fs.existsSync(target)) continue;
|
|
141
122
|
if (fs.statSync(target).isFile()) {
|
|
@@ -156,7 +137,7 @@ function walk(directory, callback) {
|
|
|
156
137
|
}
|
|
157
138
|
|
|
158
139
|
function removeManagedOutput(root) {
|
|
159
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
140
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
160
141
|
fs.rmSync(path.join(root, item), { recursive: true, force: true });
|
|
161
142
|
}
|
|
162
143
|
}
|
|
@@ -244,14 +225,14 @@ function writeManagedOutputInPlace(output, staging) {
|
|
|
244
225
|
const originallyPresent = new Set();
|
|
245
226
|
|
|
246
227
|
try {
|
|
247
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
228
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
248
229
|
const current = path.join(output, item);
|
|
249
230
|
if (!fs.existsSync(current)) continue;
|
|
250
231
|
originallyPresent.add(item);
|
|
251
232
|
syncPath(current, path.join(managedBackup, item));
|
|
252
233
|
}
|
|
253
234
|
|
|
254
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
235
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
255
236
|
const source = path.join(staging, item);
|
|
256
237
|
const target = path.join(output, item);
|
|
257
238
|
if (fs.existsSync(source)) syncPath(source, target);
|
|
@@ -259,7 +240,7 @@ function writeManagedOutputInPlace(output, staging) {
|
|
|
259
240
|
}
|
|
260
241
|
} catch (error) {
|
|
261
242
|
try {
|
|
262
|
-
for (const item of MANAGED_TOP_LEVEL) {
|
|
243
|
+
for (const item of [...MANAGED_TOP_LEVEL, ...LEGACY_MANAGED_TOP_LEVEL]) {
|
|
263
244
|
const target = path.join(output, item);
|
|
264
245
|
const saved = path.join(managedBackup, item);
|
|
265
246
|
if (originallyPresent.has(item)) syncPath(saved, target);
|
|
@@ -98,10 +98,6 @@ function contentTypeEnum(contentType) {
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
function toWireExpression(valueExpression, schemaKey) {
|
|
102
|
-
if (!schemaKey) return valueExpression;
|
|
103
|
-
return `toWireValue(${valueExpression}, typedApiWireSchemas[${JSON.stringify(schemaKey)}], typedApiWireSchemas)`;
|
|
104
|
-
}
|
|
105
101
|
|
|
106
102
|
function requestItems(operationInfo, operationTypes, options = {}) {
|
|
107
103
|
const inputName = options.inputName ?? "input";
|
|
@@ -118,17 +114,21 @@ function requestItems(operationInfo, operationTypes, options = {}) {
|
|
|
118
114
|
if (options.queryValueName) {
|
|
119
115
|
items.push(`query: ${options.queryValueName}`);
|
|
120
116
|
} else if (operationTypes.hasQueryParams) {
|
|
121
|
-
|
|
122
|
-
`query: ${
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
117
|
+
if (!operationTypes.hasPathParams && !operationTypes.hasHeaderParams && !operationTypes.hasCookieParams) {
|
|
118
|
+
items.push(`query: ${inputName}`);
|
|
119
|
+
} else {
|
|
120
|
+
items.push(
|
|
121
|
+
`query: ${parameterObjectExpression(
|
|
122
|
+
inputName,
|
|
123
|
+
operationTypes.parametersByLocation.query,
|
|
124
|
+
)}`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
if (operationTypes.bodyType) {
|
|
130
130
|
const bodyExpression = options.bodyValueName ?? "data";
|
|
131
|
-
items.push(`body: ${
|
|
131
|
+
items.push(`body: ${bodyExpression}`);
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
if (operationTypes.hasHeaderParams || operationTypes.hasCookieParams) {
|
|
@@ -167,25 +167,13 @@ function callbackOptions(operationTypes, defaultHandlers) {
|
|
|
167
167
|
if (defaultHandlers) {
|
|
168
168
|
items.push("onSuccess: onSuccess ?? typedApiDefaultSuccessHandler");
|
|
169
169
|
items.push("onError: onError ?? typedApiDefaultErrorHandler");
|
|
170
|
+
items.push("fallbackErrorMessage: typedApiDefaultErrorMessage");
|
|
170
171
|
} else {
|
|
171
172
|
items.push("onSuccess");
|
|
172
173
|
items.push("onError");
|
|
173
174
|
}
|
|
174
175
|
|
|
175
|
-
if (operationTypes.
|
|
176
|
-
items.push(
|
|
177
|
-
`transformResponse: (value) => fromWireValue(value, typedApiWireSchemas[${JSON.stringify(
|
|
178
|
-
operationTypes.responseWireSchemaKey,
|
|
179
|
-
)}], typedApiWireSchemas) as ${operationTypes.responseType}`,
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
if (operationTypes.errorWireSchemaKey) {
|
|
183
|
-
items.push(
|
|
184
|
-
`transformError: (value) => fromWireValue(value, typedApiWireSchemas[${JSON.stringify(
|
|
185
|
-
operationTypes.errorWireSchemaKey,
|
|
186
|
-
)}], typedApiWireSchemas) as ${operationTypes.errorType}`,
|
|
187
|
-
);
|
|
188
|
-
} else if (operationTypes.usesHttpErrorFallback) {
|
|
176
|
+
if (operationTypes.usesHttpErrorFallback) {
|
|
189
177
|
items.push(
|
|
190
178
|
"transformError: (value, response) => createApiHttpError(response.status, value)",
|
|
191
179
|
);
|
|
@@ -325,14 +313,12 @@ export function controllerNameForOperation(operationInfo, options = {}) {
|
|
|
325
313
|
|
|
326
314
|
export function generateMethodFile(openApi, controllerName, operations, options = {}) {
|
|
327
315
|
const dataContractImports = [];
|
|
328
|
-
const dataContractValueImports = [];
|
|
329
316
|
const helperTypeImports = ["ApiResult", "ApiMethodOptions"];
|
|
330
317
|
const helperValueImports = ["handleApiResponse"];
|
|
331
318
|
const methods = [];
|
|
332
319
|
let needsContentType = false;
|
|
333
320
|
let needsPaginationTypes = false;
|
|
334
321
|
let needsHeaderHelpers = false;
|
|
335
|
-
let needsWireMappings = false;
|
|
336
322
|
|
|
337
323
|
for (const operationInfo of operations) {
|
|
338
324
|
const operationTypes = getOperationTypes(openApi, operationInfo, options);
|
|
@@ -355,13 +341,6 @@ export function generateMethodFile(openApi, controllerName, operations, options
|
|
|
355
341
|
|
|
356
342
|
if (contentTypeEnum(operationTypes.contentType)) needsContentType = true;
|
|
357
343
|
if (operationTypes.hasHeaderParams || operationTypes.hasCookieParams) needsHeaderHelpers = true;
|
|
358
|
-
if (
|
|
359
|
-
operationTypes.bodyWireSchemaKey ||
|
|
360
|
-
operationTypes.responseWireSchemaKey ||
|
|
361
|
-
operationTypes.errorWireSchemaKey
|
|
362
|
-
) {
|
|
363
|
-
needsWireMappings = true;
|
|
364
|
-
}
|
|
365
344
|
if (
|
|
366
345
|
operationTypes.hasQueryParams &&
|
|
367
346
|
!operationTypes.hasPathParams && !operationTypes.bodyType &&
|
|
@@ -375,10 +354,6 @@ export function generateMethodFile(openApi, controllerName, operations, options
|
|
|
375
354
|
}
|
|
376
355
|
|
|
377
356
|
if (needsHeaderHelpers) helperValueImports.push("mergeHeaders", "toCookieHeader", "toRequestHeaders");
|
|
378
|
-
if (needsWireMappings) {
|
|
379
|
-
dataContractValueImports.push("typedApiWireSchemas");
|
|
380
|
-
helperValueImports.push("fromWireValue", "toWireValue");
|
|
381
|
-
}
|
|
382
357
|
if (needsPaginationTypes) {
|
|
383
358
|
helperValueImports.push("buildQuery");
|
|
384
359
|
helperTypeImports.push(
|
|
@@ -392,12 +367,11 @@ export function generateMethodFile(openApi, controllerName, operations, options
|
|
|
392
367
|
generatedFileHeader(),
|
|
393
368
|
createImport(["request", ...(needsContentType ? ["ContentType"] : [])], "../generated/http-client"),
|
|
394
369
|
createImport(["RequestParams"], "../generated/http-client", useTypeOnlyImports),
|
|
395
|
-
createImport(dataContractValueImports, "../generated/data-contracts"),
|
|
396
370
|
createImport(dataContractImports, "../generated/data-contracts", useTypeOnlyImports),
|
|
397
371
|
createImport(helperValueImports, options.runtimePackageName ?? "typedapi-client-helpers"),
|
|
398
372
|
createImport(helperTypeImports, options.runtimePackageName ?? "typedapi-client-helpers", useTypeOnlyImports),
|
|
399
373
|
defaultHandlers
|
|
400
|
-
? `import { ${defaultHandlers.success} as typedApiDefaultSuccessHandler, ${defaultHandlers.error} as typedApiDefaultErrorHandler } from ${JSON.stringify(defaultHandlers.path)};`
|
|
374
|
+
? `import { ${defaultHandlers.success} as typedApiDefaultSuccessHandler, ${defaultHandlers.error} as typedApiDefaultErrorHandler, ${defaultHandlers.errorMessage} as typedApiDefaultErrorMessage } from ${JSON.stringify(defaultHandlers.path)};`
|
|
401
375
|
: "",
|
|
402
376
|
].filter(Boolean);
|
|
403
377
|
|