MCPcopy
hub / github.com/nestjs/swagger / setup

Method setup

lib/swagger-module.ts:359–416  ·  view source on GitHub ↗
(
    path: string,
    app: INestApplication,
    documentOrFactory: OpenAPIObject | (() => OpenAPIObject),
    options?: SwaggerCustomOptions
  )

Source from the content-addressed store, hash-verified

357 }
358
359 public static setup(
360 path: string,
361 app: INestApplication,
362 documentOrFactory: OpenAPIObject | (() => OpenAPIObject),
363 options?: SwaggerCustomOptions
364 ) {
365 const globalPrefix = getGlobalPrefix(app);
366 const finalPath = validatePath(
367 options?.useGlobalPrefix && validateGlobalPrefix(globalPrefix)
368 ? `${globalPrefix}${validatePath(path)}`
369 : path
370 );
371 const urlLastSubdirectory = finalPath.split('/').slice(-1).pop() || '';
372 const validatedGlobalPrefix =
373 options?.useGlobalPrefix && validateGlobalPrefix(globalPrefix)
374 ? validatePath(globalPrefix)
375 : '';
376
377 const finalJSONDocumentPath = options?.jsonDocumentUrl
378 ? `${validatedGlobalPrefix}${validatePath(options.jsonDocumentUrl)}`
379 : `${finalPath}-json`;
380
381 const finalYAMLDocumentPath = options?.yamlDocumentUrl
382 ? `${validatedGlobalPrefix}${validatePath(options.yamlDocumentUrl)}`
383 : `${finalPath}-yaml`;
384
385 const ui = options?.ui ?? options?.swaggerUiEnabled ?? true;
386 const raw = options?.raw ?? true;
387
388 const httpAdapter = app.getHttpAdapter();
389
390 SwaggerModule.serveDocuments(
391 finalPath,
392 urlLastSubdirectory,
393 httpAdapter,
394 documentOrFactory,
395 {
396 ui,
397 raw,
398 jsonDocumentUrl: finalJSONDocumentPath,
399 yamlDocumentUrl: finalYAMLDocumentPath,
400 swaggerOptions: options || {}
401 }
402 );
403
404 if (ui) {
405 SwaggerModule.serveStatic(finalPath, app, options?.customSwaggerUiPath);
406 /**
407 * Covers assets fetched through a relative path when Swagger url ends with a slash '/'.
408 * @see https://github.com/nestjs/swagger/issues/1976
409 */
410 if (finalPath === `/${urlLastSubdirectory}`) {
411 return;
412 }
413 const serveStaticSlashEndingPath = `${finalPath}/${urlLastSubdirectory}`;
414 SwaggerModule.serveStatic(serveStaticSlashEndingPath, app);
415 }
416 }

Callers 3

bootstrapFunction · 0.80

Calls 5

getGlobalPrefixFunction · 0.90
validatePathFunction · 0.90
validateGlobalPrefixFunction · 0.90
serveDocumentsMethod · 0.80
serveStaticMethod · 0.80

Tested by

no test coverage detected