interface OwWorldFileEntry { /** Relative to the world's folder, `/`-separated. */ path: string; data: Uint8Array; } type ElementType = 'ability' | 'character' | 'collective' | 'construct' | 'creature' | 'event' | 'family' | 'institution' | 'language' | 'law' | 'location' | 'map' | 'marker' | 'narrative' | 'object' | 'phenomenon' | 'pin' | 'relation' | 'species' | 'title' | 'trait' | 'zone'; /** * A world folder read as the Folder Format reads one (spec §3, §4): `world.json`, and every * `*.json` directly under `elements//` for the 22 types, plus the legacy * `spatial/{map,pin,zone,marker}/`. Identity is the `id` in the file, never its name; a file whose * body declares one of the 22 types is that type wherever it sits (the directory decides whether a * file is found, the body what it is). `elements/` wins over `spatial/` on a shared id, and within * one layout the first path in code point order wins (§4). Two ids are the same id when they are * written the same: one UUID in capitals in one file and in lower case in another is two elements. * Names are read as written: the extension is `.json` in lower case and a type folder is the type * word in lower case, so `M.JSON` is skipped by name and `elements/Character/` is no type folder. * A file that does not parse, or has no * non-empty string `id`, is skipped and counted, never fatal. So is a file this reader cannot * hold as written: one nested more than 200 deep, or holding a number too large for a double, * which would otherwise be served as `null`. Dot-prefixed folders and files are never read. * * Module-internal: not in the package's exports. */ /** * Why a file under a type folder was not served as an element: it is no JSON (`not_json`), it is * nested more than 200 deep (`too_deep`), it holds a number too large for a double, which no * answer could carry as written (`number_out_of_range`), it has no non-empty string `id` * (`no_id`), an earlier file holds its id, written the same (`duplicate`), or its extension is * `.json` in another spelling, such as `.JSON`, which the format does not read (`extension`). */ type OwFolderSkipReason = 'not_json' | 'too_deep' | 'number_out_of_range' | 'no_id' | 'duplicate' | 'extension'; interface OwFolderSkip { path: string; reason: OwFolderSkipReason; /** The id a `duplicate` carried (the element served under it came from an earlier file). */ id?: string; } /** A file under a type folder that is served as an element: what it became. */ interface OwFolderServed { path: string; /** The element's type: the one its body declares when that is one of the 22 slugs, else its folder's. */ type: ElementType; /** The id as the file writes it. */ id: string; /** The name as the file writes it, or null when the file holds no string name. */ name: string | null; } /** What one read of the entries found: handed to `onRead` once, when the first request reads them. */ interface OwFolderReadReport { /** `world.json` parsed with an id (false: every route answers 500, saying so). */ world: boolean; /** How many elements are served (the length of `served`). */ elements: number; /** Of those, how many came from the legacy `spatial/` layout. */ legacy: number; /** Each file served as an element, in the order they were read: `elements/` first, then by path in code point order. */ served: OwFolderServed[]; /** The files under a type folder that are not served, each with why, in the order they were read. */ skipped: OwFolderSkip[]; /** * A fixed name the folder holds only in another case (`Elements/` and no `elements/`, `World.JSON` and no * `world.json`), as written beside the name the format wants, or null. When set, no world is served and * every route answers 500 with this cause (folder spec 4.8: never read as a world with no elements). */ misnamed: { found: string; wanted: string; } | null; } /** * Whether the folder source reads the file at `path` (relative to the world's folder, `/`-separated, as * an entry's path is): `world.json`, a `*.json` directly under `elements//` for one of the 22 * types, or under the legacy `spatial/{map,pin,zone,marker}/`, never under a dot-prefixed folder, * dot-prefixed itself, or a swap file. It is the reader's own test, so a tool that watches a folder * asks it which new files to take in, rather than writing the rule a second time. A path that ends in * `/` names a folder, and counts when a file the reader reads can sit in it (`elements/`, * `elements/character/`, `spatial/pin/`): a folder moved in whole arrives as one change. Whether a file * that counts is served is the reader's next question (it may not parse, or have no id). Names are * matched exactly, case included, as the reader matches them. A path that is not a string, is * absolute, holds a backslash or an empty, `.` or `..` part is a TypeError: pass it as an entry's path. */ declare function countsPath(path: string): boolean; /** * keel's read answers, given from a folder snapshot: the world, a type's list, one element and the * search. Each is held to keel's own code (github.com/OnlyWorlds/keel, read at c6204cda): * `core/world_api.py` (the world's body), `elements/api.py` (the list's closed filter set, its * refusals and the one element's not-found), `elements/pagination.py` (the order and the cursor), * `elements/serialize.py` and `elements/resolve.py` (the body, `fields` and `expand`), * `elements/search.py` (the word rule) and `core/errors.py` (the envelope). The reader is the * world's owner: a folder is its holder's own view, so hidden elements are served with their mark. * * Module-internal: not in the package's exports. */ /** The one refusal's code: what a folder cannot answer, because it lives on the platform. */ declare const NOT_IN_A_FOLDER = "not_in_a_folder"; declare const NOT_IN_A_FOLDER_STATUS = 501; declare const NOT_IN_A_FOLDER_MESSAGE = "A world folder cannot answer this: it lives on the platform."; /** * `@onlyworlds/sdk/folder`: a world folder as the client's source. `folderFetch(entries)` is a * `fetch` that answers the client's reads from a folder's files, as the platform answers them for * the world's owner, so a tool that reads through `OwV2Client` reads a folder by changing one line: * * ```ts * import { OwV2Client } from '@onlyworlds/sdk'; * import { folderFetch, entriesFromDirectory } from '@onlyworlds/sdk/folder'; * * const client = new OwV2Client({ fetch: folderFetch(await entriesFromDirectory('./my-world')) }); * const page = await client.list('character'); * ``` * * `entries` is the list `readWorldFile` returns, so a `.owworld` file is a source too: * `folderFetch(readWorldFile(bytes).files)`. The source reads its entries once, on the first * request, and answers from that snapshot: when the folder changes, make a new fetch. * * What it answers: `GET /world/`, `GET /{type}/` (the platform's filters, order, cursor, `fields` * and `expand`), `GET /{type}/{id}/`, `GET /search` and `GET /health/`. Everything else refuses with * one answer, a 501 whose error code is `not_in_a_folder` (an `OwApiError` with that status and * code): every write, before anything is read; `/changes`, `/me`, members, files, proposals, the * draught, the account routes; and `as=` or `world=` on a read, which name a reader or a world on * the platform. Hidden elements are served with their `hidden: true`, since a folder is its * holder's own view. Leave the client's `world` option unset: it sends `world=`. * * The client needs no key with this fetch (nothing leaves the process); one given is never read. * `entriesFromDirectory` is for Node and loads `node:fs` only when called, so a browser imports * this subpath safely; there a tool builds the entries from its own files. A tool that watches a * folder asks `countsPath(path)` whether a new file is one the source reads, before reading again. */ interface OwFolderFetchOptions { /** Called once, when the first request reads the entries: each file served (its path, type, id and name), and each file skipped with why. */ onRead?: (report: OwFolderReadReport) => void; } /** * A `fetch` answering the client's reads from a world folder's files (`{path, data}`, relative to * the world's folder, `/`-separated): pass it as `new OwV2Client({ fetch: folderFetch(entries) })`. * Read once, on the first request. A write, or a route only the platform can answer, is refused * with a 501 `not_in_a_folder`. */ declare function folderFetch(entries: readonly OwWorldFileEntry[], opts?: OwFolderFetchOptions): typeof globalThis.fetch; /** * Node only: the files of the world folder at `path` that the folder source reads (`world.json`, * `elements/`, and the legacy `spatial/`), as the entries `folderFetch` takes, in code point order. * Dot-prefixed folders and files are never read, nor a browser's or editor's swap file; links are * not followed. `node:fs` is loaded only when this is called. */ declare function entriesFromDirectory(path: string): Promise; export { NOT_IN_A_FOLDER, NOT_IN_A_FOLDER_MESSAGE, NOT_IN_A_FOLDER_STATUS, countsPath, entriesFromDirectory, folderFetch }; export type { ElementType, OwFolderFetchOptions, OwFolderReadReport, OwFolderServed, OwFolderSkip, OwFolderSkipReason, OwWorldFileEntry };