Skip to content

Commit 7131888

Browse files
committed
Add /user/{id}
1 parent ed0e7e9 commit 7131888

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

bin/generate.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
OpenAPIRegistry,
77
OpenApiGeneratorV31,
88
} from "@asteasolutions/zod-to-openapi";
9-
import { ItemSchema } from "./model.js";
9+
import { ItemSchema, UserSchema } from "./model.js";
1010

1111
extendZodWithOpenApi(z);
1212

@@ -34,6 +34,28 @@ registry.registerPath({
3434
},
3535
});
3636

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+
3759
const generator = new OpenApiGeneratorV31(registry.definitions);
3860

3961
fs.writeFileSync(

bin/model.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test, expect } from "vitest";
2-
import { ItemSchema } from "./model.js";
2+
import { ItemSchema, UserSchema } from "./model.js";
33

4-
test("Validate real item", async () => {
4+
test("Story", async () => {
55
const id = 40869877;
66
const response = await fetch(
77
`https://hacker-news.firebaseio.com/v0/item/${id}.json`
@@ -10,3 +10,13 @@ test("Validate real item", async () => {
1010
const result = ItemSchema.parse(json);
1111
expect(result.id).toBe(id);
1212
});
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+
});

bin/model.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,24 @@ export const ItemSchema = z.object({
5050
.optional()
5151
.describe("In the case of stories or polls, the total comment count."),
5252
});
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+
});

exports/api.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,44 @@ paths:
8787
description: In the case of stories or polls, the total comment count.
8888
required:
8989
- 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
90130
webhooks: {}

0 commit comments

Comments
 (0)