↑ Usage Guide Index | ← Core API OVERVIEW
Detects a spec’s type and routes it to the right loader. Think of AutoLoader as the “front door” for
SpecManager: you can hand it a URL or object and it will choose OpenAPI (or another supported type), fetch if necessary, and return a normalized spec record.
- Type inference: Inspect an object or fetched JSON for markers (e.g.,
openapi: "3.x") or an explicitx-typefield. - HTTP retrieval: If
sourceis a string, performGETby default, orPOSTwith a payload when requested. - Option forwarding: Pass through HTTP options (headers, timeout,
format, etc.) to the underlyingnet.httpcalls. - Normalization: Return
{ id, type, spec }suitable for registration inSpecManager. - Extensibility: Register custom detectors/handlers for additional spec families.
AutoLoader doesn’t call operations; it only loads & classifies specs so SpecManager can register them.
Creates an AutoLoader bound to a Net instance.
-
Parameters
net(Net) — Provides thehttpclient used to fetch remote specs.
Load a spec and infer its type.
-
Parameters
-
source(string | object) — URL string or already-parsed spec object. -
opts(object, optional)id(string) — Registry key suggestion. If omitted andsourceis a URL, uses the filename stem.type(string) — Force a specific type (e.g.,openapi). Skips inference.method("get" | "post") — HTTP method whensourceis a string. Default"get".payload(any) — Body to send whenmethod: "post".http(object) — Options forwarded tonet.http(headers,timeout,format, etc.).
-
-
Returns
Promise<{ id: string, type: string, spec: object }>
-
Behavior
- If
sourceis a string, fetch it vianet.http.get(source, { format: 'body', ...opts.http })orpostwhen requested. - Determine
type(see Type Detection). Respectopts.typeif provided. - Produce
{ id, type, spec }whereiddefaults toopts.id→ URL filename →'spec'.
- If
Extend AutoLoader with a new spec type.
-
Parameters
type(string) — Canonical type name.detector(function) —(input) => booleanreturns true if input matches this type.handler(function) —(input) => objectreturns a normalizedspecobject.
-
Returns
void
-
Notes
- Built-in detectors cover common OpenAPI shapes.
- Handlers may coerce or validate the input before returning.
AutoLoader checks, in order:
-
Forced type:
opts.type. -
Explicit field:
input["x-type"]if present. -
Signature heuristics:
- OpenAPI: string
input.openapibeginning with"3."or"2.". - Custom: your registered
detectorfunctions.
- OpenAPI: string
If no detector matches, AutoLoader throws E_SPEC_UNKNOWN_TYPE.
const a = new AutoLoader(net);
const { id, type, spec } = await a.load('/specs/pets.json', { id: 'pets' });
// type === 'openapi', spec is the parsed documentawait a.load('https://api.example.com/spec', {
method: 'post',
payload: { tenant: 'acme' },
http: { headers: { Authorization: 'Bearer …' } }
});await a.load('/specs/internal.json', {
type: 'openapi',
http: { timeout: 4000, format: 'body' }
});a.register('myproto',
input => typeof input === 'object' && input?.myproto === '1.0',
input => input // already normalized
);
const rec = await a.load('/specs/custom.json');
// rec.type may be 'myproto' if detector matchedAutoLoader is usually consumed by SpecManager.load():
// inside SpecManager
const { id, type, spec } = await autoloader.load(source, opts);
registry.set(id, { type, spec });This keeps SpecManager focused on operation dispatch, while AutoLoader handles format detection and fetch mechanics.
E_SPEC_UNKNOWN_TYPE— Could not determine type.E_SPEC_FETCH_FAILED— Network error or non-2xx status when retrieving the spec.E_SPEC_INVALID_SHAPE— Detector/handler rejected the input.
class AutoLoader {
constructor(net: Net);
async load(source: string | object, opts?: {
id?: string;
type?: string;
method?: 'get' | 'post';
payload?: any;
http?: object;
}): Promise<{ id: string; type: string; spec: object }>;
register(type: string, detector: (input: any) => boolean, handler: (input: any) => object): void;
}- CORE_API_SPEC_MANAGER.md — consumes AutoLoader results.
- HTTP: Requests & Responses — request options forwarded during fetch.