MCPcopy
hub / github.com/danielgtaylor/huma / NewAPI

Function NewAPI

api.go:460–576  ·  view source on GitHub ↗

NewAPI creates a new API with the given configuration and router adapter. You usually don't need to use this function directly, and can instead use the `New(...)` function provided by the adapter packages which call this function internally. When the API is created, this function will ensure a sche

(config Config, a Adapter)

Source from the content-addressed store, hash-verified

458// config := huma.DefaultConfig("Example API", "1.0.0")
459// api := huma.NewAPI(config, adapter)
460func NewAPI(config Config, a Adapter) API {
461 for i := 0; i < len(config.CreateHooks); i++ {
462 config = config.CreateHooks[i](config)
463 }
464
465 newAPI := &api{
466 adapter: a,
467 config: config,
468 formats: map[string]Format{},
469 transformers: config.Transformers,
470 }
471
472 if config.OpenAPI == nil {
473 config.OpenAPI = &OpenAPI{}
474 }
475
476 if config.OpenAPI.OpenAPI == "" {
477 config.OpenAPI.OpenAPI = "3.1.0"
478 }
479
480 if config.Components == nil {
481 config.Components = &Components{}
482 }
483
484 if config.Components.Schemas == nil {
485 config.Components.Schemas = NewMapRegistry("#/components/schemas/", DefaultSchemaNamer)
486 }
487
488 if mr, ok := config.Components.Schemas.(*mapRegistry); ok {
489 mr.config = config.registryConfig
490 }
491
492 if config.DefaultFormat == "" && !config.NoFormatFallback {
493 if config.Formats["application/json"].Marshal != nil {
494 config.DefaultFormat = "application/json"
495 }
496 }
497
498 if config.DefaultFormat != "" {
499 newAPI.formatKeys = append(newAPI.formatKeys, config.DefaultFormat)
500 }
501
502 for k, v := range config.Formats {
503 newAPI.formats[k] = v
504 newAPI.formatKeys = append(newAPI.formatKeys, k)
505 }
506
507 if config.OpenAPIPath != "" {
508 var specJSON []byte
509 a.Handle(&Operation{
510 Method: http.MethodGet,
511 Path: config.OpenAPIPath + ".json",
512 }, func(ctx Context) {
513 ctx.SetHeader("Content-Type", "application/openapi+json")
514 if specJSON == nil {
515 specJSON, _ = json.Marshal(newAPI.OpenAPI())
516 }
517 ctx.BodyWriter().Write(specJSON)

Callers

nothing calls this directly

Calls 13

OpenAPIMethod · 0.95
registerDocsRouteMethod · 0.95
NewMapRegistryFunction · 0.85
DowngradeMethod · 0.80
YAMLMethod · 0.80
DowngradeYAMLMethod · 0.80
HandleMethod · 0.65
SetHeaderMethod · 0.65
MarshalMethod · 0.65
BodyWriterMethod · 0.65
ParamMethod · 0.65
MapMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…