Documentation
¶
Overview ¶
Package config handles parsing and loading yapi config files.
Package config handles project-level configuration for yapi.
Index ¶
- func FindProjectRoot(startDir string) (string, error)
- func FindUnknownKeys(raw map[string]any) []string
- type AssertionSet
- type ChainStep
- type ConfigV1
- func (c *ConfigV1) ExpandWithResolver(resolver vars.Resolver)
- func (c *ConfigV1) Merge(step ChainStep) ConfigV1
- func (c *ConfigV1) MergeWithDefaults(defaults ConfigV1) ConfigV1
- func (c *ConfigV1) ToDomain() (*domain.Request, error)
- func (c *ConfigV1) ToDomainWithResolver(resolver vars.Resolver) (*domain.Request, error)
- type Envelope
- type Environment
- type EnvironmentConfig
- type Expectation
- type ParseResult
- type ProjectConfigV1
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindProjectRoot ¶ added in v0.5.0
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 ¶
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"` // 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
ExpandWithResolver expands environment variables using a custom resolver
func (*ConfigV1) Merge ¶
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
MergeWithDefaults applies environment defaults to a file config. File values take precedence over environment defaults.
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).