config

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package config handles parsing and loading yapi config files.

Package config handles project-level configuration for yapi.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindProjectRoot added in v0.5.0

func FindProjectRoot(startDir string) (string, error)

FindProjectRoot walks up the directory tree from startDir looking for yapi.config.yml. Returns the directory containing the config file, or an error if not found.

func FindUnknownKeys

func FindUnknownKeys(raw map[string]any) []string

FindUnknownKeys checks a raw map for keys not in knownV1Keys. Returns a sorted slice of unknown key names.

Types

type AssertionSet added in v0.5.0

type AssertionSet struct {
	Body    []string // Assertions on response body (default context)
	Headers []string // Assertions on response headers
}

AssertionSet represents assertions that can be either a flat list or grouped by context

func (*AssertionSet) UnmarshalYAML added in v0.5.0

func (a *AssertionSet) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements custom unmarshaling for AssertionSet to support both: - Flat array: assert: [...] (all treated as body assertions) - Grouped map: assert: { headers: [...], body: [...] }

type ChainStep

type ChainStep struct {
	Name     string           `yaml:"name"` // Required: unique step identifier
	ConfigV1 `yaml:",inline"` // All ConfigV1 fields available as overrides
}

ChainStep represents a single step in a request chain. It embeds ConfigV1 so all config fields are available as overrides.

type ConfigV1

type ConfigV1 struct {
	Yapi           string            `yaml:"yapi"` // The version tag
	URL            string            `yaml:"url"`
	Path           string            `yaml:"path,omitempty"`
	Method         string            `yaml:"method,omitempty"` // HTTP method (GET, POST, PUT, DELETE, etc.)
	ContentType    string            `yaml:"content_type,omitempty"`
	Headers        map[string]string `yaml:"headers,omitempty"`
	Body           map[string]any    `yaml:"body,omitempty"`
	JSON           string            `yaml:"json,omitempty"` // Raw JSON override
	Form           map[string]string `yaml:"form,omitempty"` // Form data (application/x-www-form-urlencoded or multipart/form-data)
	Query          map[string]string `yaml:"query,omitempty"`
	Graphql        string            `yaml:"graphql,omitempty"`   // GraphQL query/mutation
	Variables      map[string]any    `yaml:"variables,omitempty"` // GraphQL variables
	Service        string            `yaml:"service,omitempty"`   // gRPC
	RPC            string            `yaml:"rpc,omitempty"`       // gRPC
	Proto          string            `yaml:"proto,omitempty"`     // gRPC
	ProtoPath      string            `yaml:"proto_path,omitempty"`
	Data           string            `yaml:"data,omitempty"`     // TCP raw data
	Encoding       string            `yaml:"encoding,omitempty"` // text, hex, base64
	JQFilter       string            `yaml:"jq_filter,omitempty"`
	Insecure       bool              `yaml:"insecure,omitempty"`     // Skip TLS verification for HTTP/GraphQL; uses insecure transport for gRPC
	Plaintext      bool              `yaml:"plaintext,omitempty"`    // For gRPC
	ReadTimeout    int               `yaml:"read_timeout,omitempty"` // TCP read timeout in seconds
	IdleTimeout    int               `yaml:"idle_timeout,omitempty"` // TCP idle timeout in milliseconds (default 500)
	CloseAfterSend bool              `yaml:"close_after_send,omitempty"`

	// Flow control
	Delay   string `yaml:"delay,omitempty"`   // Wait before executing this step (e.g. "5s", "500ms")
	Timeout string `yaml:"timeout,omitempty"` // HTTP request timeout (e.g. "4s", "100ms", "1m")

	// Output
	OutputFile string `yaml:"output_file,omitempty"` // Save response to file (e.g. "./output.json", "./image.png")

	// Expect defines assertions to run after the request
	Expect Expectation `yaml:"expect,omitempty"`

	// Chain allows executing multiple dependent requests
	Chain []ChainStep `yaml:"chain,omitempty"`
}

ConfigV1 represents the v1 YAML schema

func (*ConfigV1) ExpandWithResolver added in v0.5.0

func (c *ConfigV1) ExpandWithResolver(resolver vars.Resolver)

ExpandWithResolver expands environment variables using a custom resolver

func (*ConfigV1) Merge

func (c *ConfigV1) Merge(step ChainStep) ConfigV1

Merge creates a full ConfigV1 by applying step overrides to the base config. Maps are deep copied to avoid polluting the shared base config between steps.

func (*ConfigV1) MergeWithDefaults added in v0.5.0

func (c *ConfigV1) MergeWithDefaults(defaults ConfigV1) ConfigV1

MergeWithDefaults applies environment defaults to a file config. File values take precedence over environment defaults.

func (*ConfigV1) ToDomain

func (c *ConfigV1) ToDomain() (*domain.Request, error)

ToDomain converts V1 YAML to the Canonical Config

func (*ConfigV1) ToDomainWithResolver added in v0.5.0

func (c *ConfigV1) ToDomainWithResolver(resolver vars.Resolver) (*domain.Request, error)

ToDomainWithResolver converts V1 YAML using a custom resolver for variable expansion

type Envelope

type Envelope struct {
	Yapi string `yaml:"yapi"`
}

Envelope is used solely to peek at the version

type Environment added in v0.5.0

type Environment struct {
	Name     string            // Derived from map key
	ConfigV1 `yaml:",inline"`  // Inline all ConfigV1 fields (url, headers, method, etc.)
	Vars     map[string]string `yaml:"vars"`      // Environment-specific variables
	EnvFiles []string          `yaml:"env_files"` // Environment-specific .env files
}

Environment represents a single environment configuration. It embeds ConfigV1 to allow setting default values for any YAPI field at the environment level. These defaults are merged with individual file configs (file values take precedence).

type EnvironmentConfig added in v0.5.0

type EnvironmentConfig struct {
	Vars     map[string]string `yaml:"vars"`      // Direct variable definitions
	EnvFiles []string          `yaml:"env_files"` // Paths to .env files (relative to project root)
}

EnvironmentConfig holds variable definitions that can be shared or inherited.

type Expectation

type Expectation struct {
	Status any          `yaml:"status,omitempty"` // int or []int
	Assert AssertionSet `yaml:"assert,omitempty"` // JQ expressions that must evaluate to true
}

Expectation defines assertions for a chain step

type ParseResult

type ParseResult struct {
	Request  *domain.Request
	Warnings []string
	Chain    []ChainStep // Chain steps if this is a chain config
	Base     *ConfigV1   // Base config for chain merging
	Expect   Expectation // Expectations for single request validation
}

ParseResult holds the output of parsing a yapi config file.

func LoadFromString

func LoadFromString(data string) (*ParseResult, error)

LoadFromString parses a yapi config from raw YAML data.

func LoadFromStringWithOptions added in v0.5.0

func LoadFromStringWithOptions(data string, resolver vars.Resolver, defaults *ConfigV1) (*ParseResult, error)

LoadFromStringWithOptions parses a yapi config with optional resolver and environment defaults.

type ProjectConfigV1 added in v0.5.0

type ProjectConfigV1 struct {
	Yapi               string                 `yaml:"yapi"`                // Must be "v1"
	Kind               string                 `yaml:"kind"`                // Must be "project"
	DefaultEnvironment string                 `yaml:"default_environment"` // Default environment to use when --env not specified
	Defaults           EnvironmentConfig      `yaml:"defaults"`            // Default vars applied to all environments
	Environments       map[string]Environment `yaml:"environments"`        // Named environments (dev, staging, prod, etc.)
	// contains filtered or unexported fields
}

ProjectConfigV1 represents a yapi.config.yml file which defines environments for a project.

func LoadProject added in v0.5.0

func LoadProject(projectRoot string) (*ProjectConfigV1, error)

LoadProject loads and parses a yapi.config.yml file from the given directory. The projectRoot should be the directory containing the yapi.config.yml file.

func (*ProjectConfigV1) GetEnvironment added in v0.5.0

func (pc *ProjectConfigV1) GetEnvironment(name string) (*Environment, error)

GetEnvironment retrieves a specific environment by name, returning an error if not found.

func (*ProjectConfigV1) ListEnvironments added in v0.5.0

func (pc *ProjectConfigV1) ListEnvironments() []string

ListEnvironments returns a list of all environment names defined in the project.

func (*ProjectConfigV1) ResolveEnvFiles added in v0.5.0

func (pc *ProjectConfigV1) ResolveEnvFiles(projectRoot string, envName string) (map[string]string, error)

ResolveEnvFiles resolves all .env file paths relative to the project root and loads them. Returns a merged map of all variables (defaults first, then environment-specific). OS environment variables take precedence over all loaded vars. Results are cached to avoid repeated file I/O (important for LSP performance).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL