1- import { describe , expect , it , vi } from "vitest" ;
1+ import { describe , expect , it } from "vitest" ;
22import { set } from "lodash" ;
33import { OpenAPIObject } from "openapi3-ts" ;
44import { Config , generateFetchers } from "./generateFetchers" ;
5+ import { createWriteFileMock } from "../testUtils" ;
56
67const config : Config = {
78 filenamePrefix : "petstore" ,
@@ -59,7 +60,7 @@ describe("generateFetchers", () => {
5960 } ;
6061
6162 it ( "should generate fetchers" , async ( ) => {
62- const writeFile = vi . fn ( ) ;
63+ const writeFile = createWriteFileMock ( ) ;
6364
6465 await generateFetchers (
6566 {
@@ -73,37 +74,45 @@ describe("generateFetchers", () => {
7374
7475 expect ( writeFile . mock . calls [ 1 ] [ 0 ] ) . toBe ( "petstoreComponents.ts" ) ;
7576 expect ( writeFile . mock . calls [ 1 ] [ 1 ] ) . toMatchInlineSnapshot ( `
76- "/**
77- * Generated by @openapi-codegen
78- *
79- * @version 1.0.0
80- */
81- import type * as Fetcher from "./petstoreFetcher";
82- import { petstoreFetch } from "./petstoreFetcher";
83- import type * as Schemas from "./petstoreSchemas";
84- import type * as Responses from "./petstoreResponses";
85- import type { ServerErrorStatus } from "./petstoreUtils";
86-
87- export type ListPetsError = Fetcher.ErrorWrapper<{
88- status: 404;
89- payload: Responses.NotFoundError;
90- } | {
91- status: ServerErrorStatus;
92- payload: Schemas.Error;
93- }>;
94-
95- export type ListPetsResponse = Schemas.Pet[];
96-
97- /**
98- * Get all the pets
99- */
100- export const listPets = (signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
101- "
77+ "/**
78+ * Generated by @openapi-codegen
79+ *
80+ * @version 1.0.0
81+ */
82+ import type * as Fetcher from "./petstoreFetcher";
83+ import { petstoreFetch } from "./petstoreFetcher";
84+ import type * as Schemas from "./petstoreSchemas";
85+ import type * as Responses from "./petstoreResponses";
86+ import type { ServerErrorStatus } from "./petstoreUtils";
87+
88+ export type ListPetsError = Fetcher.ErrorWrapper<
89+ | {
90+ status: 404;
91+ payload: Responses.NotFoundError;
92+ }
93+ | {
94+ status: ServerErrorStatus;
95+ payload: Schemas.Error;
96+ }
97+ >;
98+
99+ export type ListPetsResponse = Schemas.Pet[];
100+
101+ /**
102+ * Get all the pets
103+ */
104+ export const listPets = (signal?: AbortSignal) =>
105+ petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
106+ url: "/pets",
107+ method: "get",
108+ signal,
109+ });
110+ "
102111 ` ) ;
103112 } ) ;
104113
105114 it ( "should generate fetchers without prefix" , async ( ) => {
106- const writeFile = vi . fn ( ) ;
115+ const writeFile = createWriteFileMock ( ) ;
107116
108117 await generateFetchers (
109118 {
@@ -117,37 +126,45 @@ describe("generateFetchers", () => {
117126
118127 expect ( writeFile . mock . calls [ 1 ] [ 0 ] ) . toBe ( "components.ts" ) ;
119128 expect ( writeFile . mock . calls [ 1 ] [ 1 ] ) . toMatchInlineSnapshot ( `
120- "/**
121- * Generated by @openapi-codegen
122- *
123- * @version 1.0.0
124- */
125- import type * as Fetcher from "./fetcher";
126- import { fetch } from "./fetcher";
127- import type * as Schemas from "./petstoreSchemas";
128- import type * as Responses from "./petstoreResponses";
129- import type { ServerErrorStatus } from "./utils";
130-
131- export type ListPetsError = Fetcher.ErrorWrapper<{
132- status: 404;
133- payload: Responses.NotFoundError;
134- } | {
135- status: ServerErrorStatus;
136- payload: Schemas.Error;
137- }>;
138-
139- export type ListPetsResponse = Schemas.Pet[];
140-
141- /**
142- * Get all the pets
143- */
144- export const listPets = (signal?: AbortSignal) => fetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
145- "
129+ "/**
130+ * Generated by @openapi-codegen
131+ *
132+ * @version 1.0.0
133+ */
134+ import type * as Fetcher from "./fetcher";
135+ import { fetch } from "./fetcher";
136+ import type * as Schemas from "./petstoreSchemas";
137+ import type * as Responses from "./petstoreResponses";
138+ import type { ServerErrorStatus } from "./utils";
139+
140+ export type ListPetsError = Fetcher.ErrorWrapper<
141+ | {
142+ status: 404;
143+ payload: Responses.NotFoundError;
144+ }
145+ | {
146+ status: ServerErrorStatus;
147+ payload: Schemas.Error;
148+ }
149+ >;
150+
151+ export type ListPetsResponse = Schemas.Pet[];
152+
153+ /**
154+ * Get all the pets
155+ */
156+ export const listPets = (signal?: AbortSignal) =>
157+ fetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
158+ url: "/pets",
159+ method: "get",
160+ signal,
161+ });
162+ "
146163 ` ) ;
147164 } ) ;
148165
149166 it ( "should generate fetcher with injected props" , async ( ) => {
150- const writeFile = vi . fn ( ) ;
167+ const writeFile = createWriteFileMock ( ) ;
151168
152169 await generateFetchers (
153170 {
@@ -168,39 +185,48 @@ describe("generateFetchers", () => {
168185
169186 expect ( writeFile . mock . calls [ 1 ] [ 0 ] ) . toBe ( "petstoreComponents.ts" ) ;
170187 expect ( writeFile . mock . calls [ 1 ] [ 1 ] ) . toMatchInlineSnapshot ( `
171- "/**
172- * Generated by @openapi-codegen
173- *
174- * @version 1.0.0
175- */
176- import type * as Fetcher from "./petstoreFetcher";
177- import { petstoreFetch, PetstoreFetcherExtraProps } from "./petstoreFetcher";
178- import type * as Schemas from "./petstoreSchemas";
179- import type * as Responses from "./petstoreResponses";
180- import type { ServerErrorStatus } from "./petstoreUtils";
181-
182- export type ListPetsError = Fetcher.ErrorWrapper<{
183- status: 404;
184- payload: Responses.NotFoundError;
185- } | {
186- status: ServerErrorStatus;
187- payload: Schemas.Error;
188- }>;
189-
190- export type ListPetsResponse = Schemas.Pet[];
191-
192- export type ListPetsVariables = PetstoreFetcherExtraProps;
193-
194- /**
195- * Get all the pets
196- */
197- export const listPets = (variables: ListPetsVariables, signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", ...variables, signal });
198- "
188+ "/**
189+ * Generated by @openapi-codegen
190+ *
191+ * @version 1.0.0
192+ */
193+ import type * as Fetcher from "./petstoreFetcher";
194+ import { petstoreFetch, PetstoreFetcherExtraProps } from "./petstoreFetcher";
195+ import type * as Schemas from "./petstoreSchemas";
196+ import type * as Responses from "./petstoreResponses";
197+ import type { ServerErrorStatus } from "./petstoreUtils";
198+
199+ export type ListPetsError = Fetcher.ErrorWrapper<
200+ | {
201+ status: 404;
202+ payload: Responses.NotFoundError;
203+ }
204+ | {
205+ status: ServerErrorStatus;
206+ payload: Schemas.Error;
207+ }
208+ >;
209+
210+ export type ListPetsResponse = Schemas.Pet[];
211+
212+ export type ListPetsVariables = PetstoreFetcherExtraProps;
213+
214+ /**
215+ * Get all the pets
216+ */
217+ export const listPets = (variables: ListPetsVariables, signal?: AbortSignal) =>
218+ petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
219+ url: "/pets",
220+ method: "get",
221+ ...variables,
222+ signal,
223+ });
224+ "
199225 ` ) ;
200226 } ) ;
201227
202228 it ( "should generate fetcher with operations by tag" , async ( ) => {
203- const writeFile = vi . fn ( ) ;
229+ const writeFile = createWriteFileMock ( ) ;
204230
205231 const openAPIDocumentWithTags = set (
206232 openAPIDocument ,
@@ -220,34 +246,42 @@ describe("generateFetchers", () => {
220246
221247 expect ( writeFile . mock . calls [ 1 ] [ 0 ] ) . toBe ( "petstoreComponents.ts" ) ;
222248 expect ( writeFile . mock . calls [ 1 ] [ 1 ] ) . toMatchInlineSnapshot ( `
223- "/**
224- * Generated by @openapi-codegen
225- *
226- * @version 1.0.0
227- */
228- import type * as Fetcher from "./petstoreFetcher";
229- import { petstoreFetch } from "./petstoreFetcher";
230- import type * as Schemas from "./petstoreSchemas";
231- import type * as Responses from "./petstoreResponses";
232- import type { ServerErrorStatus } from "./petstoreUtils";
233-
234- export type ListPetsError = Fetcher.ErrorWrapper<{
235- status: 404;
236- payload: Responses.NotFoundError;
237- } | {
238- status: ServerErrorStatus;
239- payload: Schemas.Error;
240- }>;
241-
242- export type ListPetsResponse = Schemas.Pet[];
243-
244- /**
245- * Get all the pets
246- */
247- export const listPets = (signal?: AbortSignal) => petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({ url: "/pets", method: "get", signal });
248-
249- export const operationsByTag = { "pets": { listPets } };
250- "
249+ "/**
250+ * Generated by @openapi-codegen
251+ *
252+ * @version 1.0.0
253+ */
254+ import type * as Fetcher from "./petstoreFetcher";
255+ import { petstoreFetch } from "./petstoreFetcher";
256+ import type * as Schemas from "./petstoreSchemas";
257+ import type * as Responses from "./petstoreResponses";
258+ import type { ServerErrorStatus } from "./petstoreUtils";
259+
260+ export type ListPetsError = Fetcher.ErrorWrapper<
261+ | {
262+ status: 404;
263+ payload: Responses.NotFoundError;
264+ }
265+ | {
266+ status: ServerErrorStatus;
267+ payload: Schemas.Error;
268+ }
269+ >;
270+
271+ export type ListPetsResponse = Schemas.Pet[];
272+
273+ /**
274+ * Get all the pets
275+ */
276+ export const listPets = (signal?: AbortSignal) =>
277+ petstoreFetch<ListPetsResponse, ListPetsError, undefined, {}, {}, {}>({
278+ url: "/pets",
279+ method: "get",
280+ signal,
281+ });
282+
283+ export const operationsByTag = { pets: { listPets } };
284+ "
251285 ` ) ;
252286 } ) ;
253287} ) ;
0 commit comments