File tree Expand file tree Collapse file tree 4 files changed +96
-3
lines changed Expand file tree Collapse file tree 4 files changed +96
-3
lines changed Original file line number Diff line number Diff line change 6
6
OpenAPIRegistry ,
7
7
OpenApiGeneratorV31 ,
8
8
} from "@asteasolutions/zod-to-openapi" ;
9
- import { ItemSchema } from "./model.js" ;
9
+ import { ItemSchema , UserSchema } from "./model.js" ;
10
10
11
11
extendZodWithOpenApi ( z ) ;
12
12
@@ -34,6 +34,28 @@ registry.registerPath({
34
34
} ,
35
35
} ) ;
36
36
37
+ registry . registerPath ( {
38
+ method : "get" ,
39
+ operationId : "getUser" ,
40
+ path : "/user/{id}.json" ,
41
+ summary : "Retrieve a user from the API." ,
42
+ request : {
43
+ params : z . object ( {
44
+ id : z . string ( ) ,
45
+ } ) ,
46
+ } ,
47
+ responses : {
48
+ 200 : {
49
+ description : "Success" ,
50
+ content : {
51
+ "application/json" : {
52
+ schema : UserSchema ,
53
+ } ,
54
+ } ,
55
+ } ,
56
+ } ,
57
+ } ) ;
58
+
37
59
const generator = new OpenApiGeneratorV31 ( registry . definitions ) ;
38
60
39
61
fs . writeFileSync (
Original file line number Diff line number Diff line change 1
1
import { test , expect } from "vitest" ;
2
- import { ItemSchema } from "./model.js" ;
2
+ import { ItemSchema , UserSchema } from "./model.js" ;
3
3
4
- test ( "Validate real item " , async ( ) => {
4
+ test ( "Story " , async ( ) => {
5
5
const id = 40869877 ;
6
6
const response = await fetch (
7
7
`https://hacker-news.firebaseio.com/v0/item/${ id } .json`
@@ -10,3 +10,13 @@ test("Validate real item", async () => {
10
10
const result = ItemSchema . parse ( json ) ;
11
11
expect ( result . id ) . toBe ( id ) ;
12
12
} ) ;
13
+
14
+ test ( "User" , async ( ) => {
15
+ const id = "dang" ;
16
+ const response = await fetch (
17
+ `https://hacker-news.firebaseio.com/v0/user/${ id } .json`
18
+ ) ;
19
+ const json = await response . json ( ) ;
20
+ const result = UserSchema . parse ( json ) ;
21
+ expect ( result . id ) . toBe ( id ) ;
22
+ } ) ;
Original file line number Diff line number Diff line change @@ -50,3 +50,24 @@ export const ItemSchema = z.object({
50
50
. optional ( )
51
51
. describe ( "In the case of stories or polls, the total comment count." ) ,
52
52
} ) ;
53
+
54
+ export const UserSchema = z . object ( {
55
+ id : z
56
+ . string ( )
57
+ . describe ( "The user's unique username. Case-sensitive. Required." ) ,
58
+ created : z
59
+ . number ( )
60
+ . int ( )
61
+ . describe (
62
+ "Creation date of the user, in [Unix Time](http://en.wikipedia.org/wiki/Unix_time)."
63
+ ) ,
64
+ karma : z . number ( ) . int ( ) . describe ( "The user's karma." ) ,
65
+ about : z
66
+ . string ( )
67
+ . optional ( )
68
+ . describe ( "The user's optional self-description. HTML." ) ,
69
+ submitted : z
70
+ . array ( z . number ( ) . int ( ) )
71
+ . optional ( )
72
+ . describe ( "List of the user's stories, polls and comments." ) ,
73
+ } ) ;
Original file line number Diff line number Diff line change @@ -87,4 +87,44 @@ paths:
87
87
description : In the case of stories or polls, the total comment count.
88
88
required :
89
89
- id
90
+ /user/{id}.json :
91
+ get :
92
+ operationId : getUser
93
+ summary : Retrieve a user from the API.
94
+ parameters :
95
+ - schema :
96
+ type : string
97
+ required : true
98
+ name : id
99
+ in : path
100
+ responses :
101
+ " 200 " :
102
+ description : Success
103
+ content :
104
+ application/json :
105
+ schema :
106
+ type : object
107
+ properties :
108
+ id :
109
+ type : string
110
+ description : The user's unique username. Case-sensitive. Required.
111
+ created :
112
+ type : integer
113
+ description : Creation date of the user, in [Unix
114
+ Time](http://en.wikipedia.org/wiki/Unix_time).
115
+ karma :
116
+ type : integer
117
+ description : The user's karma.
118
+ about :
119
+ type : string
120
+ description : The user's optional self-description. HTML.
121
+ submitted :
122
+ type : array
123
+ items :
124
+ type : integer
125
+ description : List of the user's stories, polls and comments.
126
+ required :
127
+ - id
128
+ - created
129
+ - karma
90
130
webhooks : {}
You can’t perform that action at this time.
0 commit comments