@@ -168,9 +168,32 @@ function resolveEnvelopeFromSchema(schema: any, spec: any): string {
168168 return `{ type: ${ typeName } ; data: ${ dataType } }` ;
169169}
170170
171+ /**
172+ * Extracts schema names from the openapi-typescript generated types string
173+ * and produces `export type Name = components["schemas"]["Name"];` aliases.
174+ */
175+ function buildSchemaExports ( generatedTypes : string ) : string {
176+ const schemaNames : string [ ] = [ ] ;
177+ // Match the schemas block inside `components { schemas: { ... } }`
178+ const schemasMatch = generatedTypes . match ( / s c h e m a s : \s * \{ ( [ \s \S ] * ?) \n { 4 } \} / ) ;
179+ if ( schemasMatch ) {
180+ // Each top-level schema key is at 8-space indent
181+ const re = / ^ { 8 } ( \w + ) \s * : / gm;
182+ let m ;
183+ while ( ( m = re . exec ( schemasMatch [ 1 ] ) ) !== null ) {
184+ schemaNames . push ( m [ 1 ] ) ;
185+ }
186+ }
187+ if ( schemaNames . length === 0 ) return "" ;
188+ return schemaNames
189+ . map ( ( name ) => ` export type ${ name } = components["schemas"]["${ name } "];` )
190+ . join ( "\n" ) ;
191+ }
192+
171193export function dtsTemplate ( generatedTypes : string , asyncapiSpec : object | null = null ) : string {
172194 const wsSection = buildWSChannelsType ( asyncapiSpec ) ;
173195 const wsImport = wsSection ? '\n import type { WSConnection } from "shiftapi/internal";\n import { WSError } from "shiftapi/internal";' : "" ;
196+ const schemaExports = buildSchemaExports ( generatedTypes ) ;
174197
175198 return `\
176199// Auto-generated by shiftapi. Do not edit.
@@ -201,15 +224,18 @@ ${indent(generatedTypes)}
201224${ wsSection }
202225 export const client: ReturnType<typeof createClient<paths>>;
203226 export { createClient };${ wsSection ? "\n export { WSError };" : "" }
204- }
227+ ${ schemaExports ? "\n" + schemaExports + "\n" : "" } }
205228` ;
206229}
207230
208- export function clientJsTemplate ( baseUrl : string ) : string {
231+ export function clientJsTemplate ( baseUrl : string , options ?: { hasWebSocket ?: boolean } ) : string {
232+ const hasWS = options ?. hasWebSocket ?? false ;
233+ const wsImport = hasWS ? ", WSError" : "" ;
234+ const wsExport = hasWS ? ", WSError" : "" ;
209235 return `\
210236// Auto-generated by shiftapi. Do not edit.
211237import createClient from "openapi-fetch";
212- import { createSSE, createWebSocket } from "shiftapi/internal";
238+ import { createSSE, createWebSocket${ wsImport } } from "shiftapi/internal";
213239
214240/** Pre-configured, fully-typed API client. */
215241export const client = createClient({
@@ -220,20 +246,24 @@ export const client = createClient({
220246export const sse = createSSE(${ JSON . stringify ( baseUrl ) } );
221247export const websocket = createWebSocket(${ JSON . stringify ( baseUrl ) } );
222248
223- export { createClient };
249+ export { createClient${ wsExport } };
224250` ;
225251}
226252
227253export function nextClientJsTemplate (
228254 port : number ,
229255 baseUrl : string ,
230256 devApiPrefix ?: string ,
257+ options ?: { hasWebSocket ?: boolean } ,
231258) : string {
259+ const hasWS = options ?. hasWebSocket ?? false ;
260+ const wsImport = hasWS ? ", WSError" : "" ;
261+ const wsExport = hasWS ? ", WSError" : "" ;
232262 if ( ! devApiPrefix ) {
233263 return `\
234264// Auto-generated by @shiftapi/next. Do not edit.
235265import createClient from "./openapi-fetch.js";
236- import { createSSE, createWebSocket } from "shiftapi/internal";
266+ import { createSSE, createWebSocket${ wsImport } } from "shiftapi/internal";
237267
238268const baseUrl =
239269 process.env.NEXT_PUBLIC_SHIFTAPI_BASE_URL || ${ JSON . stringify ( baseUrl ) } ;
@@ -247,15 +277,15 @@ export const client = createClient({
247277export const sse = createSSE(baseUrl);
248278export const websocket = createWebSocket(baseUrl);
249279
250- export { createClient };
280+ export { createClient${ wsExport } };
251281` ;
252282 }
253283
254284 const devServerUrl = `http://localhost:${ port } ` ;
255285 return `\
256286// Auto-generated by @shiftapi/next. Do not edit.
257287import createClient from "./openapi-fetch.js";
258- import { createSSE, createWebSocket } from "shiftapi/internal";
288+ import { createSSE, createWebSocket${ wsImport } } from "shiftapi/internal";
259289
260290const baseUrl =
261291 process.env.NEXT_PUBLIC_SHIFTAPI_BASE_URL ||
@@ -272,22 +302,26 @@ export const client = createClient({
272302export const sse = createSSE(baseUrl);
273303export const websocket = createWebSocket(baseUrl);
274304
275- export { createClient };
305+ export { createClient${ wsExport } };
276306` ;
277307}
278308
279309export function virtualModuleTemplate (
280310 baseUrl : string ,
281311 devApiPrefix ?: string ,
312+ options ?: { hasWebSocket ?: boolean } ,
282313) : string {
314+ const hasWS = options ?. hasWebSocket ?? false ;
315+ const wsImport = hasWS ? ", WSError" : "" ;
316+ const wsExport = hasWS ? ", WSError" : "" ;
283317 const baseUrlExpr = devApiPrefix
284318 ? `import.meta.env.VITE_SHIFTAPI_BASE_URL || (import.meta.env.DEV ? ${ JSON . stringify ( devApiPrefix ) } : ${ JSON . stringify ( baseUrl ) } )`
285319 : `import.meta.env.VITE_SHIFTAPI_BASE_URL || ${ JSON . stringify ( baseUrl ) } ` ;
286320
287321 return `\
288322// Auto-generated by @shiftapi/vite-plugin
289323import createClient from "openapi-fetch";
290- import { createSSE, createWebSocket } from "shiftapi/internal";
324+ import { createSSE, createWebSocket${ wsImport } } from "shiftapi/internal";
291325
292326const baseUrl = ${ baseUrlExpr } ;
293327
@@ -300,6 +334,6 @@ export const client = createClient({
300334export const sse = createSSE(baseUrl);
301335export const websocket = createWebSocket(baseUrl);
302336
303- export { createClient };
337+ export { createClient${ wsExport } };
304338` ;
305339}
0 commit comments