11import type * as ServerTypes from "./gen/server.ts" ;
2- import * as server from "./gen/server.ts" ;
32import type { Request , Response } from "express" ;
43import { promises as fs } from "fs" ;
54import { join } from "path" ;
@@ -8,8 +7,24 @@ import { fileURLToPath } from "url";
87const __filename = fileURLToPath ( import . meta. url ) ;
98const __dirname = join ( __filename , ".." ) ;
109
11- const API : ServerTypes . Server < Request , Response > = {
12- getPetById : async ( { parameters, req } ) : ServerTypes . GetPetByIdResult => {
10+ // Service implementation for "pets" tag
11+ export const petsService : ServerTypes . ServerForPets < Request , Response > = {
12+ listPets : async ( ) : ServerTypes . ListPetsResult => {
13+ return {
14+ content : {
15+ 200 : {
16+ "application/json" : {
17+ pets : [
18+ { id : 1 , name : "dog" } ,
19+ { id : 2 , name : "cat" } ,
20+ ] ,
21+ } ,
22+ } ,
23+ } ,
24+ } ;
25+ } ,
26+
27+ getPetById : async ( { parameters } ) : ServerTypes . GetPetByIdResult => {
1328 if ( parameters . path . petId === 42 ) {
1429 return {
1530 content : {
@@ -56,45 +71,10 @@ const API: ServerTypes.Server<Request, Response> = {
5671 } ,
5772 } ;
5873 } ,
74+ } ;
5975
60- mixedContentTypes : async ( {
61- parameters,
62- requestBody,
63- contentType,
64- } ) : ServerTypes . MixedContentTypesResult => {
65- const { petId } = parameters . path ;
66- let status : "available" | "pending" | "sold" | undefined ;
67-
68- // Since each content type has different structures,
69- // use the request content type and requestBody discriminator to narrow the type in each case.
70-
71- if (
72- contentType === "application/json" &&
73- requestBody . mediaType === "application/json"
74- ) {
75- status = requestBody . content . jsonstatus ;
76- }
77-
78- if (
79- contentType == "application/xml" &&
80- requestBody . mediaType === "application/xml"
81- ) {
82- status = requestBody . content . xmlstatus ;
83- }
84-
85- return {
86- content : {
87- 200 : {
88- "application/json" : {
89- pet : { id : petId , name : "dog" , status } ,
90- } ,
91- } ,
92- } ,
93- } ;
94- } ,
95-
96- listPets : server . listPetsUnimplemented ,
97-
76+ // Service implementation for "media" tag
77+ export const mediaService : ServerTypes . ServerForMedia < Request , Response > = {
9878 getPetImage : async ( ) : ServerTypes . GetPetImageResult => {
9979 const image = await fs . readFile ( join ( __dirname , `./cat.jpeg` ) , {
10080 encoding : "base64" ,
@@ -121,4 +101,42 @@ const API: ServerTypes.Server<Request, Response> = {
121101 } ,
122102} ;
123103
124- export default API ;
104+ // Service implementation for untagged operations
105+ export const untaggedService : ServerTypes . ServerForUntagged < Request , Response > =
106+ {
107+ mixedContentTypes : async ( {
108+ parameters,
109+ requestBody,
110+ contentType,
111+ } ) : ServerTypes . MixedContentTypesResult => {
112+ const { petId } = parameters . path ;
113+ let status : "available" | "pending" | "sold" | undefined ;
114+
115+ // Since each content type has different structures,
116+ // use the request content type and requestBody discriminator to narrow the type in each case.
117+
118+ if (
119+ contentType === "application/json" &&
120+ requestBody . mediaType === "application/json"
121+ ) {
122+ status = requestBody . content . jsonstatus ;
123+ }
124+
125+ if (
126+ contentType == "application/xml" &&
127+ requestBody . mediaType === "application/xml"
128+ ) {
129+ status = requestBody . content . xmlstatus ;
130+ }
131+
132+ return {
133+ content : {
134+ 200 : {
135+ "application/json" : {
136+ pet : { id : petId , name : "dog" , status } ,
137+ } ,
138+ } ,
139+ } ,
140+ } ;
141+ } ,
142+ } ;
0 commit comments