Skip to content

Commit c031cb6

Browse files
Replace media routes with store routes in examples/tags
- Removed media tagged routes (getPetImage, getPetWebpage) - Added store tagged routes (getInventory, placeOrder) - Updated api.ts to replace mediaService with storeService - Updated app.ts to use storeService instead of mediaService - Updated app.test.ts with store route tests - Removed unused cat.jpeg file - Added Order schema to openapi.yaml components Co-authored-by: jasonblanchard <1238532+jasonblanchard@users.noreply.github.com>
1 parent a2d7c4e commit c031cb6

File tree

7 files changed

+222
-122
lines changed

7 files changed

+222
-122
lines changed

examples/tags/api.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import type * as ServerTypes from "./gen/server.ts";
22
import type { Request, Response } from "express";
3-
import { promises as fs } from "fs";
4-
import { join } from "path";
5-
import { fileURLToPath } from "url";
6-
7-
const __filename = fileURLToPath(import.meta.url);
8-
const __dirname = join(__filename, "..");
93

104
// Service implementation for "pets" tag
115
export const petsService: ServerTypes.ServerForPets<Request, Response> = {
@@ -73,28 +67,38 @@ export const petsService: ServerTypes.ServerForPets<Request, Response> = {
7367
},
7468
};
7569

76-
// Service implementation for "media" tag
77-
export const mediaService: ServerTypes.ServerForMedia<Request, Response> = {
78-
getPetImage: async (): ServerTypes.GetPetImageResult => {
79-
const image = await fs.readFile(join(__dirname, `./cat.jpeg`), {
80-
encoding: "base64",
81-
});
82-
70+
// Service implementation for "store" tag
71+
export const storeService: ServerTypes.ServerForStore<Request, Response> = {
72+
getInventory: async (): ServerTypes.GetInventoryResult => {
8373
return {
8474
content: {
8575
200: {
86-
"image/jpeg": image,
76+
"application/json": {
77+
inventory: {
78+
available: 10,
79+
pending: 5,
80+
sold: 3,
81+
},
82+
},
8783
},
8884
},
8985
};
9086
},
9187

92-
getPetWebpage: async ({ parameters }): ServerTypes.GetPetWebpageResult => {
93-
const { petId } = parameters.path;
88+
placeOrder: async ({ requestBody }): ServerTypes.PlaceOrderResult => {
89+
const { petId, quantity } = requestBody.content;
90+
9491
return {
9592
content: {
9693
200: {
97-
"text/html": `<html><body><h1>Hello, pet ${petId}!</h1></body></html>`,
94+
"application/json": {
95+
order: {
96+
id: Math.floor(Math.random() * 1000),
97+
petId,
98+
quantity,
99+
status: "placed",
100+
},
101+
},
98102
},
99103
},
100104
};

examples/tags/app.test.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,35 @@ describe("listPets", async () => {
144144
});
145145
});
146146

147-
describe("getPetImage", async () => {
147+
describe("getInventory", async () => {
148148
it("returns 200", async () => {
149149
const response = await request(app)
150-
.get("/api/v3/pet/123/image")
151-
.set("Accept", "image/jpeg");
150+
.get("/api/v3/store/inventory")
151+
.set("Accept", "application/json");
152152

153153
assert.equal(response.status, 200);
154-
assert(response.body);
155-
assert.equal(response.headers["content-type"], "image/jpeg");
154+
assert.deepEqual(response.body, {
155+
inventory: {
156+
available: 10,
157+
pending: 5,
158+
sold: 3,
159+
},
160+
});
156161
});
157162
});
158163

159-
describe("getPetWebpage", async () => {
164+
describe("placeOrder", async () => {
160165
it("returns 200", async () => {
161166
const response = await request(app)
162-
.get("/api/v3/pet/123/webpage")
163-
.set("Accept", "text/html");
167+
.post("/api/v3/store/order")
168+
.set("Accept", "application/json")
169+
.send({ petId: 123, quantity: 2 });
164170

165171
assert.equal(response.status, 200);
166-
assert.equal(
167-
response.text,
168-
"<html><body><h1>Hello, pet 123!</h1></body></html>",
169-
);
172+
assert(response.body.order);
173+
assert.equal(response.body.order.petId, 123);
174+
assert.equal(response.body.order.quantity, 2);
175+
assert.equal(response.body.order.status, "placed");
176+
assert(typeof response.body.order.id === "number");
170177
});
171178
});

examples/tags/app.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from "express";
22
import type { Request, Response, NextFunction } from "express";
33
import { registerRouteHandlersByTag } from "./gen/server.ts";
44
import registerRoutes from "openapi-typescript-server-express";
5-
import { petsService, mediaService, untaggedService } from "./api.ts";
5+
import { petsService, storeService, untaggedService } from "./api.ts";
66
import OpenApiValidator from "express-openapi-validator";
77
import { NotImplementedError } from "openapi-typescript-server-runtime";
88
import xmlparser from "express-xml-bodyparser";
@@ -29,20 +29,10 @@ export default function makeApp() {
2929

3030
// Register routes by tag using registerRouteHandlersByTag
3131
const petsRoutes = registerRouteHandlersByTag("pets", petsService);
32-
const mediaRoutes = registerRouteHandlersByTag("media", mediaService);
32+
const storeRoutes = registerRouteHandlersByTag("store", storeService);
3333
const untaggedRoutes = registerRouteHandlersByTag(null, untaggedService);
3434

35-
registerRoutes(
36-
[...petsRoutes, ...mediaRoutes, ...untaggedRoutes],
37-
apiRouter,
38-
{
39-
serializers: {
40-
"image/jpeg": (content) => {
41-
return Buffer.from(content, "base64");
42-
},
43-
},
44-
},
45-
);
35+
registerRoutes([...petsRoutes, ...storeRoutes, ...untaggedRoutes], apiRouter);
4636

4737
app.use("/api/v3", apiRouter);
4838

examples/tags/cat.jpeg

-44.3 KB
Binary file not shown.

examples/tags/gen/schema.d.ts

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ export interface paths {
5353
patch?: never;
5454
trace?: never;
5555
};
56-
"/pet/{petId}/image": {
56+
"/store/inventory": {
5757
parameters: {
5858
query?: never;
5959
header?: never;
6060
path?: never;
6161
cookie?: never;
6262
};
63-
get: operations["getPetImage"];
63+
get: operations["getInventory"];
6464
put?: never;
6565
post?: never;
6666
delete?: never;
@@ -69,16 +69,16 @@ export interface paths {
6969
patch?: never;
7070
trace?: never;
7171
};
72-
"/pet/{petId}/webpage": {
72+
"/store/order": {
7373
parameters: {
7474
query?: never;
7575
header?: never;
7676
path?: never;
7777
cookie?: never;
7878
};
79-
get: operations["getPetWebpage"];
79+
get?: never;
8080
put?: never;
81-
post?: never;
81+
post: operations["placeOrder"];
8282
delete?: never;
8383
options?: never;
8484
head?: never;
@@ -119,6 +119,19 @@ export interface components {
119119
/** @enum {string} */
120120
status: "available" | "pending" | "sold";
121121
};
122+
Order: {
123+
/** Format: int64 */
124+
id: number;
125+
/** Format: int64 */
126+
petId: number;
127+
/** Format: int32 */
128+
quantity: number;
129+
/**
130+
* @description Order Status
131+
* @enum {string}
132+
*/
133+
status?: "placed" | "approved" | "delivered";
134+
};
122135
};
123136
responses: never;
124137
parameters: never;
@@ -281,14 +294,11 @@ export interface operations {
281294
};
282295
};
283296
};
284-
getPetImage: {
297+
getInventory: {
285298
parameters: {
286299
query?: never;
287300
header?: never;
288-
path: {
289-
/** @description ID of pet to return */
290-
petId: number;
291-
};
301+
path?: never;
292302
cookie?: never;
293303
};
294304
requestBody?: never;
@@ -299,30 +309,60 @@ export interface operations {
299309
[name: string]: unknown;
300310
};
301311
content: {
302-
"image/jpeg": string;
312+
"application/json": {
313+
inventory?: {
314+
[key: string]: number;
315+
};
316+
};
317+
};
318+
};
319+
/** @description unexpected error */
320+
default: {
321+
headers: {
322+
[name: string]: unknown;
323+
};
324+
content: {
325+
"application/json": components["schemas"]["ErrorResponse"];
303326
};
304327
};
305328
};
306329
};
307-
getPetWebpage: {
330+
placeOrder: {
308331
parameters: {
309332
query?: never;
310333
header?: never;
311-
path: {
312-
/** @description ID of pet to return */
313-
petId: number;
314-
};
334+
path?: never;
315335
cookie?: never;
316336
};
317-
requestBody?: never;
337+
requestBody: {
338+
content: {
339+
"application/json": {
340+
/** Format: int64 */
341+
petId: number;
342+
/** Format: int32 */
343+
quantity: number;
344+
};
345+
};
346+
};
318347
responses: {
319348
/** @description successful operation */
320349
200: {
321350
headers: {
322351
[name: string]: unknown;
323352
};
324353
content: {
325-
"text/html": string;
354+
"application/json": {
355+
order?: components["schemas"]["Order"];
356+
};
357+
};
358+
};
359+
/** @description unexpected error */
360+
default: {
361+
headers: {
362+
[name: string]: unknown;
363+
};
364+
content: {
365+
"application/json": components["schemas"]["ErrorResponse"];
326366
};
327367
};
328368
};

0 commit comments

Comments
 (0)