Skip to content

Commit 6e521b5

Browse files
AllieJonssonAlfred Jonsson
and
Alfred Jonsson
authoredMar 21, 2024
fix(swr): error type is correct even when it's the same type as success type (#1276)
Co-authored-by: Alfred Jonsson <alfred.jonsson@decerno.se>
1 parent 5fe09c3 commit 6e521b5

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed
 

‎packages/core/src/getters/res-req-types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { keyword } from 'esutils';
2+
import type { ValueIteratee } from 'lodash';
23
import uniqBy from 'lodash.uniqby';
4+
35
import {
46
MediaTypeObject,
57
ReferenceObject,
@@ -48,6 +50,7 @@ export const getResReqTypes = (
4850
name: string,
4951
context: ContextSpecs,
5052
defaultType = 'unknown',
53+
uniqueKey: ValueIteratee<ResReqTypesValue> = 'value',
5154
): ResReqTypesValue[] => {
5255
const typesArray = responsesOrRequests
5356
.filter(([_, res]) => Boolean(res))
@@ -213,7 +216,7 @@ export const getResReqTypes = (
213216

214217
return uniqBy(
215218
typesArray.flatMap((it) => it),
216-
'value',
219+
uniqueKey,
217220
);
218221
};
219222

‎packages/core/src/getters/response.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const getResponse = ({
3737
operationName,
3838
context,
3939
'void',
40+
(type) => type.key.startsWith('2') + type.value,
4041
);
4142

4243
const filteredTypes = contentType

‎tests/configs/swr.config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,15 @@ export default defineConfig({
167167
target: '../specifications/enums.yaml',
168168
},
169169
},
170+
errors: {
171+
output: {
172+
target: '../generated/swr/errors/endpoints.ts',
173+
schemas: '../generated/swr/errors/model',
174+
client: 'swr',
175+
mock: true,
176+
},
177+
input: {
178+
target: '../specifications/errors.yaml',
179+
},
180+
},
170181
});

‎tests/specifications/errors.yaml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
openapi: '3.0.0'
2+
info:
3+
version: 1.0.0
4+
title: Swagger Errors
5+
license:
6+
name: MIT
7+
paths:
8+
/error:
9+
get:
10+
summary: Get an Error Item
11+
operationId: createItems
12+
responses:
13+
'200':
14+
description: Success
15+
content:
16+
application/json:
17+
schema:
18+
$ref: '#/components/schemas/Item'
19+
'400':
20+
description: Error
21+
content:
22+
application/json:
23+
schema:
24+
$ref: '#/components/schemas/Item'
25+
default:
26+
description: Error
27+
content:
28+
application/json:
29+
schema:
30+
$ref: '#/components/schemas/Item'
31+
components:
32+
schemas:
33+
Item:
34+
type: object
35+
properties:
36+
prop:
37+
type: string

0 commit comments

Comments
 (0)
Please sign in to comment.