Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320/** * Bundle Service Schemas * * Zod v4 schemas for request validation and response typing. * * @module */import { z } from 'zod'
// =============================================================================// Shared Helpers// =============================================================================
/** * Query-string boolean: accepts `""`, `"true"`, `"false"`, or absent. * * Transforms to `true` when value is `""` or `"true"`, `false` when `"false"`, * `undefined` when absent. This matches how query params behave in practice: * `?tsx` (empty string) and `?tsx=true` are both truthy. */const queryBool = () => z .enum(['', 'true', 'false']) .optional() .transform((v): boolean => v === '' || v === 'true')
/** * Tri-state query boolean: same as queryBool but preserves `undefined` * when absent (rather than coercing to `false`). Used for params where * "not specified" differs from "explicitly false". */const queryTriBool = () => z .enum(['', 'true', 'false']) .optional() .transform((v): boolean | undefined => v === '' || v === 'true' ? true : v === 'false' ? false : undefined )
// =============================================================================// Query Parameter Schemas// =============================================================================
/** * Schema for bundle query parameters (V4 DSL Spec v1). * * ## URL Structure * * ``` * /?v=1&q=<modules>&base=<default-bracket>&treeshake=<overrides> * ``` * * ### New in V4 * * - `v` — Spec version (always `1`). Canonical share links include this. * - `base` — Default bracket payload for unoverridden modules. * Missing = `auto|default` (namespace + default surface). * * ### Bracket Grammar * * ``` * bracket = emit_item ("|" emit_item)* * emit_item = [phase] clause [attributes] * phase = "defer" | "source" | "type" * clause = "bare" | "auto" | "default" | "*" | "* as X" | "{ ... }" | "id:X" | X * attributes = "with" "{" key:value, ... "}" * ``` * * @see GRAMMAR.md for full spec */export const BundleQuerySchema = z.object({ // ------------------------------------------------------------------------- // V4 DSL params // -------------------------------------------------------------------------
/** * Spec version. Always `1` for current grammar. * Share links SHOULD always include this to lock interpretation. */ v: z .string() .optional() .transform((v) => (v ? Number(v) : undefined)),
/** * Default bracket payload for modules without a treeshake override. * * Examples: * - `auto` — safe namespace only (one line per module) * - `auto|default` — namespace + default surface (two lines) * - `*|{default}` — old-school re-export all + default * - `type auto|type default` — type-only surfaces * * When missing, interpreted as `auto|default`. */ base: z.string().optional(),
// ------------------------------------------------------------------------- // Module & code params // -------------------------------------------------------------------------
/** Module specifier(s) — comma-separated */ q: z.string().optional(), query: z.string().optional(),
/** * Per-module bracket payload overrides. * * Positional: `[auto|default],,[{parse}with{type:json}]` * Sparse: `0:[auto|default];2:[{parse}with{type:json}]` */ treeshake: z.string().optional(),
/** LZ-compressed code (for large input) */ share: z.string().optional(),
/** Plain text code (for short input) */ text: z.string().optional(),
/** JSON5 build configuration */ config: z.string().optional(),
// ------------------------------------------------------------------------- // Build options // -------------------------------------------------------------------------
/** Enable JSX/TSX support */ tsx: queryBool(), jsx: queryBool(),
/** Enable polyfills */ polyfill: queryBool(),
/** Minify output */ minify: queryTriBool(),
/** Pretty (non-minified) output */ pretty: queryTriBool(),
/** Source map generation */ sourcemap: z .enum(['', 'true', 'false', 'inline', 'external', 'both']) .optional(),
/** Output format */ format: z.enum(['esm', 'cjs', 'iife']).optional(),
// ------------------------------------------------------------------------- // Output mode params // -------------------------------------------------------------------------
/** Enable metafile output */ metafile: queryBool(),
/** Enable analysis output */ analysis: z.string().optional(), analyze: z.string().optional(),
/** Badge generation */ badge: z.string().optional(), 'badge-style': z.string().optional(), 'badge-raster': queryBool(), png: queryBool(),
/** File output */ file: queryBool(),
/** Raw JSON output */ raw: queryBool(),
/** Cache control mode */ cache: z.enum(['use', 'bypass', 'refresh']).optional(),
/** Warnings output */ warnings: queryBool(), warning: queryBool(),})
export type BundleQuery = z.infer<typeof BundleQuerySchema>
// =============================================================================// Response Schemas// =============================================================================
/** Compression size information */export const CompressionSizeSchema = z.object({ type: z.string(), uncompressedSize: z.string(), compressedSize: z.string(), rawUncompressedSize: z.number(), rawCompressedSize: z.number(),})
/** Install size information */export const InstallSizeSchema = z.object({ total: z.number().optional(), packages: z .array( z.object({ name: z.string(), size: z.number(), }) ) .optional(),})
/** Module specification tuple: [specifier, mode] */export const ModuleSpecSchema = z.tuple([ z.string(), z.string(),])
/** Bundle result response */export const BundleResultSchema = z.object({ query: z.string(), rawQuery: z.string(), config: z.record(z.string(), z.unknown()), input: z.string(), version: z.string().optional(), versions: z.array(z.string()).optional(), modules: z.array(ModuleSpecSchema).optional(), size: CompressionSizeSchema, installSize: InstallSizeSchema.optional(), time: z.string(), rawTime: z.number(), fileId: z.string().optional(), fileUrl: z.string().optional(), fileHTMLUrl: z.string().optional(), warnings: z.array(z.string()).optional(), metafile: z.record(z.string(), z.unknown()).optional(), cached: z.boolean().optional(),})
export type BundleResultResponse = z.infer<typeof BundleResultSchema>
/** Error response */export const ErrorResponseSchema = z.object({ error: z.string(),})
export type ErrorResponse = z.infer<typeof ErrorResponseSchema>
// =============================================================================// Documentation Schema// =============================================================================
export const DocsSchema = z.object({ docs: z.string(), examples: z.array(z.string()), basics: z.array(z.string()),})
export type Docs = z.infer<typeof DocsSchema>
/** * API documentation content. */export const API_DOCS: Docs = { docs: '/?docs - Takes you to some docs for the API', examples: [ '/?v=1', '/?base=auto', '/?base=auto|default', '/?treeshake=[auto|default],,[{parse}with{type:json}]', '/?treeshake=0:[auto];2:[defer*asX]', '~~~', '/?tsx or /?jsx', '/?badge or /?badge=detailed or /?badge=minified', '/?badge-style=for-the-badge', '/?badge-raster', '/?file', '/?polyfill', '/?analysis or /?analyze=verbose', '/?metafile', '/?minify=false', '/?sourcemap=inline', '/?format=iife', '/?warnings', '/?raw', '~~~', '/?q=spring-easing,(import)@okikio/emitter,(import)@okikio/animate', '/?treeshake=[auto|default],[T],[{ animate }]', '/?text="export * as PR18 from \\"@okikio/animate\\";\\nexport { animate as animate2 } from \\"@okikio/animate\\";"', '/?share=MYewdgziA2CmB00QHMAUAiAwiG6CUQA', '/?config={"cdn":"skypack","compression":"brotli","esbuild":{"format":"cjs","minify":false,"treeShaking":false}}', ], basics: [ // V4 DSL params '/?v — Spec version (currently v=1). Canonical share links always include this.', '/?base — Default bracket payload for modules without a treeshake override. Missing = auto|default. Set base=auto for safe/minimal output (one line per module).', '~~~', // Treeshake grammar '/?treeshake — Per-module bracket overrides. Supports positional ([auto],,[{parse}]) and sparse (0:[auto];2:[{parse}]) formats.', ' Clauses: bare, auto, default, *, * as X, { named }, id:X, or an identifier', ' Phases: defer (namespace only), source (identifier only), type (all except bare)', ' Multi-emit: [auto|default] produces two statements (namespace + default surface)', ' Attributes: [{parse}with{type:json}] adds import attributes', ' Separators: spaces, +, _ between keywords all work. [*_as_X] = [* as X] = [*asX]', '~~~', // Existing params '/?q or /?query — Module specifier(s), comma-separated. Prefix (import) for import mode, bare = export.', '/?tsx or /?jsx — Support JSX and TSX', '/?badge — Generates a badge (badge=detailed for module names, badge=minified for minified size)', '/?badge-style — Badge styles from shields.io', '/?badge-raster — Badge as PNG image', '/?file — Resulting bundled code output', '/?polyfill — Polyfill Node built-ins', '/?analysis or /?analyze — Visual analysis via esbuild', '/?metafile — Esbuild bundle metafile for https://esbuild.github.io/analyze/', '/?minify — Esbuild minify (default true)', '/?sourcemap — Esbuild sourcemap (inline, external, both)', '/?format — Output format (esm, cjs, iife)', '/?warnings — Lists warnings for a bundle', '/?raw — Raw JSON result', '/?text — Input code as a string (short strings; use share for longer)', '/?share — LZ-compressed string version of input code', '/?config — JSON5 build configuration', '/?cache — Cache control: use (default), bypass, refresh', ],}