Skip to content

Commit 7d4a9cc

Browse files
committed
test(openapi-generator): update tests and snapshots
1 parent 591da5b commit 7d4a9cc

File tree

9 files changed

+139
-173
lines changed

9 files changed

+139
-173
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { f } from '@metal-box/fetch'
2+
import { z } from 'zod'
3+
import * as Model from './models'
4+
5+
export const api = f.router('https://{environment}.example.com/v{version}', {
6+
products: {
7+
GET: f
8+
.builder()
9+
.def_json()
10+
.def_searchparams(
11+
z.object({
12+
category: z
13+
.enum(['electronics', 'clothing', 'books'])
14+
.optional(),
15+
priceRange: z.array(z.number()).optional(),
16+
page: z.number().int().min(1).optional(),
17+
limit: z.number().int().min(1).max(100).optional(),
18+
sort: z.object({}).optional(),
19+
}).parse
20+
)
21+
.def_response(async ({ json }) =>
22+
z.array(Model.Product).parse(await json())
23+
),
24+
POST: f
25+
.builder()
26+
.def_json()
27+
.def_body(Model.Product.parse)
28+
.def_response(async ({ json }) =>
29+
Model.Product.parse(await json())
30+
),
31+
},
32+
orders: {
33+
$orderId: {
34+
GET: f
35+
.builder()
36+
.def_json()
37+
.def_response(async ({ json }) =>
38+
Model.Order.parse(await json())
39+
),
40+
},
41+
},
42+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './api'
2+
export * from './models'

packages/openapi-generator/src/__tests__/__mocks__/gen/ecommerse-1/models.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ export const Product = z.object({
44
id: z.string().uuid(),
55
name: z.string(),
66
productType: z.string(),
7-
price: z.number(),
7+
price: z.number().min(0),
88
})
99

1010
export type ProductModel = z.infer<typeof Product>
1111

12-
export const ElectronicsProduct = z.any()
12+
export const ElectronicsProduct = Product.and(
13+
z.object({
14+
specs: z.object({}).optional(),
15+
})
16+
)
1317

1418
export type ElectronicsProductModel = z.infer<typeof ElectronicsProduct>
1519

16-
export const ClothingProduct = z.any()
20+
export const ClothingProduct = Product.and(
21+
z.object({
22+
size: z.enum(['S', 'M', 'L', 'XL']).optional(),
23+
color: z.string().optional(),
24+
})
25+
)
1726

1827
export type ClothingProductModel = z.infer<typeof ClothingProduct>
1928

@@ -22,13 +31,13 @@ export const Order = z.object({
2231
userId: z.string().optional(),
2332
products: z.array(Product).optional(),
2433
total: z.number().optional(),
25-
status: z.string().optional(),
34+
status: z.enum(['pending', 'shipped', 'delivered']).optional(),
2635
})
2736

2837
export type OrderModel = z.infer<typeof Order>
2938

3039
export const Error = z.object({
31-
code: z.number().optional(),
40+
code: z.number().int().optional(),
3241
message: z.string().optional(),
3342
})
3443

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { f } from '@metal-box/fetch'
2+
import { z } from 'zod'
3+
import * as Model from './models'
4+
5+
export const api = f.router(
6+
'https://{environment}.example-commerce.com/api/v2',
7+
{
8+
products: {
9+
GET: f
10+
.builder()
11+
.def_json()
12+
.def_searchparams(
13+
z.object({
14+
searchQuery: z.string().optional(),
15+
tags: z.array(z.string()).optional(),
16+
page: z.number().int().min(1).optional(),
17+
}).parse
18+
)
19+
.def_response(async ({ json }) =>
20+
Model.PaginatedProductResponse.parse(await json())
21+
),
22+
POST: f
23+
.builder()
24+
.def_json()
25+
.def_body(z.instanceof(FormData).parse)
26+
.def_response(async ({ json }) =>
27+
Model.Product.parse(await json())
28+
),
29+
$productId: {
30+
GET: f
31+
.builder()
32+
.def_json()
33+
.def_response(async ({ json }) =>
34+
Model.Product.parse(await json())
35+
),
36+
},
37+
},
38+
orders: {
39+
$orderId: {
40+
process: {
41+
POST: f.builder().def_json(),
42+
},
43+
},
44+
},
45+
}
46+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './api'
2+
export * from './models'

packages/openapi-generator/src/__tests__/__mocks__/gen/ecommerse-2/models.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,54 @@ import { z } from 'zod'
22

33
export const User = z.object({
44
id: z.string().uuid(),
5-
username: z.string(),
5+
username: z.string().regex(/^[a-zA-Z0-9_-]{3,16}$/),
66
email: z.string().email(),
77
profile: z
88
.object({
99
fullName: z.string().optional(),
1010
joinDate: z.string().datetime().optional(),
1111
})
1212
.optional(),
13-
legacyId: z.number().optional(),
13+
legacyId: z.number().int().optional(),
1414
})
1515

1616
export type UserModel = z.infer<typeof User>
1717

1818
export const ProductInput = z.object({
1919
name: z.string(),
2020
description: z.string().optional(),
21-
price: z.number(),
21+
price: z.number().min(0),
2222
})
2323

2424
export type ProductInputModel = z.infer<typeof ProductInput>
2525

26-
export const Product = z.any()
26+
export const Product = ProductInput.and(
27+
z.object({
28+
id: z.string().uuid().optional(),
29+
imageUrl: z.string().url().optional(),
30+
stock: z.number().int().optional(),
31+
})
32+
)
2733

2834
export type ProductModel = z.infer<typeof Product>
2935

3036
export const PaginatedResponse = z.object({
31-
page: z.number().optional(),
32-
pageSize: z.number().optional(),
33-
total: z.number().optional(),
37+
page: z.number().int().optional(),
38+
pageSize: z.number().int().optional(),
39+
total: z.number().int().optional(),
3440
items: z.array(z.any()).optional(),
3541
})
3642

3743
export type PaginatedResponseModel = z.infer<typeof PaginatedResponse>
3844

39-
export const PaginatedProductResponse = GenericPaginatedResponse
45+
export const PaginatedProductResponse = PaginatedResponse
4046

4147
export type PaginatedProductResponseModel = z.infer<
4248
typeof PaginatedProductResponse
4349
>
4450

45-
export const PaymentMethod = z.any()
46-
47-
export type PaymentMethodModel = z.infer<typeof PaymentMethod>
48-
4951
export const CreditCard = z.object({
50-
methodType: z.string(),
52+
methodType: z.enum(['card']),
5153
cardNumber: z.string(),
5254
expiry: z.string().optional(),
5355
cvv: z.string().optional(),
@@ -56,23 +58,30 @@ export const CreditCard = z.object({
5658
export type CreditCardModel = z.infer<typeof CreditCard>
5759

5860
export const PayPal = z.object({
59-
methodType: z.string(),
61+
methodType: z.enum(['paypal_account']),
6062
email: z.string().email(),
6163
})
6264

6365
export type PayPalModel = z.infer<typeof PayPal>
6466

67+
export const PaymentMethod = z.discriminatedUnion('methodType', [
68+
CreditCard,
69+
PayPal,
70+
])
71+
72+
export type PaymentMethodModel = z.infer<typeof PaymentMethod>
73+
6574
export const CallbackPayload = z.object({
6675
orderId: z.string().uuid().optional(),
67-
status: z.string().optional(),
76+
status: z.enum(['PROCESSED', 'FAILED']).optional(),
6877
detail: z.string().optional(),
6978
})
7079

7180
export type CallbackPayloadModel = z.infer<typeof CallbackPayload>
7281

7382
export const InventoryUpdatePayload = z.object({
7483
productId: z.string().uuid().optional(),
75-
newStockLevel: z.number().optional(),
84+
newStockLevel: z.number().int().optional(),
7685
timestamp: z.string().datetime().optional(),
7786
})
7887

packages/openapi-generator/src/__tests__/__mocks__/gen/simple/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { f } from '@metal-box/fetch'
22
import { z } from 'zod'
33
import * as Model from './models'
44

5-
export const api = f.router('https://api.example.com', {
5+
export const api = f.router('', {
66
books: {
77
GET: f
88
.builder()

packages/openapi-generator/src/__tests__/__mocks__/openapi/e-commerse2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@
400400
}
401401
},
402402
"PaginatedProductResponse": {
403-
"$ref": "generic_paginated_response",
403+
"$ref": "#/components/schemas/PaginatedResponse",
404404
"$defs": {
405405
"productItem": {
406406
"$dynamicAnchor": "item",

0 commit comments

Comments
 (0)