@metacensus/library-research 0.12.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react.d.ts +13 -2
- package/dist/react.js +13 -2
- package/dist/testing.d.ts +1 -0
- package/dist/types.d.ts +13 -0
- package/dist/vite.d.ts +10 -1
- package/dist/vite.js +29 -5
- package/package.json +1 -1
package/dist/react.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { DocumentError } from './documents.js';
|
|
2
2
|
import type { Corpus, Doc, Host } from './types.js';
|
|
3
|
+
/** The `Host` of the project's frame; throws outside `./host`'s `frame`. Stable for the life of the frame. */
|
|
3
4
|
export declare function useHost(): Host;
|
|
4
5
|
type Role<T = unknown> = readonly [role: string, parse: (json: unknown) => T];
|
|
5
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* One data role's JSON, parsed by `parse`. Suspends while it loads; a failed
|
|
8
|
+
* fetch or parse throws to the frame's error notice, naming the role. A 401 or
|
|
9
|
+
* 403 also calls `Host.sessionLost`. `parse` must be pure: it is read once per
|
|
10
|
+
* role list, not on every render.
|
|
11
|
+
*/
|
|
6
12
|
export declare function useData<T>(role: string, parse: (json: unknown) => T): T;
|
|
7
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* `useData` for several roles at once, fetched in parallel: a `[role, parse]`
|
|
15
|
+
* tuple each, the values returned in the same order and typed by their parsers.
|
|
16
|
+
* Each parser must be pure: it is read once per role list, not on every render.
|
|
17
|
+
*/
|
|
8
18
|
export declare function useDatas<const R extends readonly Role[]>(roles: R): {
|
|
9
19
|
-readonly [K in keyof R]: R[K] extends Role<infer T> ? T : never;
|
|
10
20
|
};
|
|
@@ -15,6 +25,7 @@ export declare function useDatas<const R extends readonly Role[]>(roles: R): {
|
|
|
15
25
|
* `{ json } | null`, is not `useData`'s raw JSON, under the same key prefix.
|
|
16
26
|
*/
|
|
17
27
|
export declare function usePrivate<T>(role: string, parse: (json: unknown) => T): Record<string, T>;
|
|
28
|
+
/** The `corpus` role, parsed by the library's own schema, through `useData`. */
|
|
18
29
|
export declare function useCorpus(): Corpus;
|
|
19
30
|
/**
|
|
20
31
|
* One document of the project, or `null` when none has that id. Suspends while
|
package/dist/react.js
CHANGED
|
@@ -3,17 +3,27 @@ import { useContext, useMemo, useState } from 'react';
|
|
|
3
3
|
import { FetchContext, HostContext } from './context.js';
|
|
4
4
|
import { DocumentError, documentId, documentPrefix } from './documents.js';
|
|
5
5
|
import * as schema from './schema.js';
|
|
6
|
+
/** The `Host` of the project's frame; throws outside `./host`'s `frame`. Stable for the life of the frame. */
|
|
6
7
|
export function useHost() {
|
|
7
8
|
const host = useContext(HostContext);
|
|
8
9
|
if (!host)
|
|
9
10
|
throw new Error('useHost outside the frame of ./host');
|
|
10
11
|
return host;
|
|
11
12
|
}
|
|
12
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* One data role's JSON, parsed by `parse`. Suspends while it loads; a failed
|
|
15
|
+
* fetch or parse throws to the frame's error notice, naming the role. A 401 or
|
|
16
|
+
* 403 also calls `Host.sessionLost`. `parse` must be pure: it is read once per
|
|
17
|
+
* role list, not on every render.
|
|
18
|
+
*/
|
|
13
19
|
export function useData(role, parse) {
|
|
14
20
|
return useDatas([[role, parse]])[0];
|
|
15
21
|
}
|
|
16
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* `useData` for several roles at once, fetched in parallel: a `[role, parse]`
|
|
24
|
+
* tuple each, the values returned in the same order and typed by their parsers.
|
|
25
|
+
* Each parser must be pure: it is read once per role list, not on every render.
|
|
26
|
+
*/
|
|
17
27
|
export function useDatas(roles) {
|
|
18
28
|
const host = useHost();
|
|
19
29
|
// The parsers and select functions are kept until the role list changes, so
|
|
@@ -108,6 +118,7 @@ export function usePrivate(role, parse) {
|
|
|
108
118
|
combine: (results) => Object.fromEntries(results.flatMap(({ data }, i) => data ? [[pages[i], data.value]] : [])),
|
|
109
119
|
});
|
|
110
120
|
}
|
|
121
|
+
/** The `corpus` role, parsed by the library's own schema, through `useData`. */
|
|
111
122
|
export function useCorpus() {
|
|
112
123
|
return useData('corpus', (json) => schema.Corpus.parse(json));
|
|
113
124
|
}
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ReactElement } from 'react';
|
|
2
2
|
import type { Routes } from './types.js';
|
|
3
|
+
/** What `testHost` is given besides the routes; every field is optional. */
|
|
3
4
|
export interface TestHostOptions {
|
|
4
5
|
/** Each data role's JSON value, parsed by the project's own schema. */
|
|
5
6
|
data?: Record<string, unknown>;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
2
|
import type { Corpus as CorpusSchema, kinds } from './schema.js';
|
|
3
|
+
/**
|
|
4
|
+
* What a host gives a project: `frame` provides it and `useHost` returns it.
|
|
5
|
+
* The methods are called during render, so each must be cheap and return the
|
|
6
|
+
* same URL for the same arguments.
|
|
7
|
+
*/
|
|
3
8
|
export interface Host {
|
|
9
|
+
/** The person the project is viewed as, who may differ from the person signed in. */
|
|
4
10
|
reader: Reader;
|
|
11
|
+
/** The URL of a data role's JSON, `role` being one the project declares; `useData` fetches it. */
|
|
5
12
|
data(role: string): string;
|
|
6
13
|
/** The viewed person's pages of a private role: page key to URL; empty when they hold none. */
|
|
7
14
|
private(role: string): Record<string, string>;
|
|
15
|
+
/** The URL of the paper stored under `sha`, as the corpus names it. */
|
|
8
16
|
paper(sha: string): string;
|
|
9
17
|
/**
|
|
10
18
|
* The URL of one document of this project: a GET reads it, a PUT writes it
|
|
@@ -26,10 +34,15 @@ export interface Doc<T> {
|
|
|
26
34
|
revision: number;
|
|
27
35
|
body: T;
|
|
28
36
|
}
|
|
37
|
+
/** The person a project is viewed as. */
|
|
29
38
|
export interface Reader {
|
|
39
|
+
/** The name the host signs them in as. */
|
|
30
40
|
username: string;
|
|
31
41
|
}
|
|
42
|
+
/** A kind of record the schema allows, one of `kinds`' keys. */
|
|
32
43
|
export type Kind = keyof typeof kinds;
|
|
44
|
+
/** The parsed `corpus` role: every paper by sha. */
|
|
33
45
|
export type Corpus = z.infer<typeof CorpusSchema>;
|
|
46
|
+
/** One paper of the corpus. */
|
|
34
47
|
export type Paper = Corpus[string];
|
|
35
48
|
export type { Mount, RouterOf, Routes } from './mount.js';
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { type Plugin } from 'vite';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Compiles each project, by slug and path from the Vite root, into the host.
|
|
4
|
+
* A build fails, naming the rule, unless the host's config keeps these:
|
|
5
|
+
* - an absolute `base`, not `./` or `''`, so a project's CSS `url()` paths resolve;
|
|
6
|
+
* - the default `build.minify`, which keeps the per-project canary the build checks for;
|
|
7
|
+
* - `build.cssCodeSplit`, so each project's CSS stays apart from the host's;
|
|
8
|
+
* - no `manualChunks`, which can mix host and project modules in one chunk;
|
|
9
|
+
* - no `publicDir` that holds or lies inside a project, which would copy it whole.
|
|
10
|
+
*/
|
|
3
11
|
export declare function research({ projects, }: {
|
|
4
12
|
projects: Record<string, string>;
|
|
5
13
|
}): Plugin[];
|
|
14
|
+
/** The Tailwind `content` globs that scan each project's `ui/`, skipping its `node_modules`; spread into the host's config. */
|
|
6
15
|
export declare function tailwindContent(projects: Record<string, string>): string[];
|
package/dist/vite.js
CHANGED
|
@@ -5,6 +5,7 @@ import { isCSSRequest, preprocessCSS, searchForWorkspaceRoot, } from 'vite';
|
|
|
5
5
|
import { project, read } from './data.js';
|
|
6
6
|
const prefix = 'projects';
|
|
7
7
|
const dataPath = new RegExp(`^${prefix}/([^/]+)/data/([^/]+)\\.json$`);
|
|
8
|
+
const privatePath = new RegExp(`^${prefix}/([^/]+)/private/([^/]+)/([^/]+)\\.json$`);
|
|
8
9
|
const virtual = 'virtual:research-projects';
|
|
9
10
|
const cssImport = /@(?:import|use|forward)\b/;
|
|
10
11
|
const cssComposes = /\bcomposes\s*:[^;}]*?\bfrom\s+(['"])([^'"]+)\1/g;
|
|
@@ -22,7 +23,15 @@ const declared = (file) => {
|
|
|
22
23
|
return undefined;
|
|
23
24
|
}
|
|
24
25
|
};
|
|
25
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Compiles each project, by slug and path from the Vite root, into the host.
|
|
28
|
+
* A build fails, naming the rule, unless the host's config keeps these:
|
|
29
|
+
* - an absolute `base`, not `./` or `''`, so a project's CSS `url()` paths resolve;
|
|
30
|
+
* - the default `build.minify`, which keeps the per-project canary the build checks for;
|
|
31
|
+
* - `build.cssCodeSplit`, so each project's CSS stays apart from the host's;
|
|
32
|
+
* - no `manualChunks`, which can mix host and project modules in one chunk;
|
|
33
|
+
* - no `publicDir` that holds or lies inside a project, which would copy it whole.
|
|
34
|
+
*/
|
|
26
35
|
export function research({ projects, }) {
|
|
27
36
|
let config;
|
|
28
37
|
let dirs = [];
|
|
@@ -254,12 +263,26 @@ export function research({ projects, }) {
|
|
|
254
263
|
configureServer(server) {
|
|
255
264
|
server.middlewares.use((req, res, next) => {
|
|
256
265
|
const path = req.url?.split('?')[0] ?? '';
|
|
257
|
-
const
|
|
258
|
-
path.slice(config.base.length)
|
|
259
|
-
|
|
260
|
-
const
|
|
266
|
+
const local = path.startsWith(config.base)
|
|
267
|
+
? path.slice(config.base.length)
|
|
268
|
+
: '';
|
|
269
|
+
const [, slug, role = ''] = local.match(dataPath) ?? [];
|
|
270
|
+
const [, pslug, page = '', prole = ''] = local.match(privatePath) ?? [];
|
|
271
|
+
const dir = dirs.find(([s]) => s === (slug ?? pslug))?.[1];
|
|
261
272
|
if (!dir)
|
|
262
273
|
return next();
|
|
274
|
+
if (pslug) {
|
|
275
|
+
// Gates nothing: the host's dev server decides who loads which page.
|
|
276
|
+
const { pages } = project(dir);
|
|
277
|
+
const held = Object.hasOwn(pages, prole) ? pages[prole]() : {};
|
|
278
|
+
const file = Object.hasOwn(held, page) ? held[page] : undefined;
|
|
279
|
+
if (!file) {
|
|
280
|
+
res.statusCode = 404;
|
|
281
|
+
return res.end(`${pslug} has no private role "${prole}" page "${page}"`);
|
|
282
|
+
}
|
|
283
|
+
res.setHeader('Content-Type', 'application/json');
|
|
284
|
+
return res.end(JSON.stringify(read(dir, file)));
|
|
285
|
+
}
|
|
263
286
|
const { roles } = project(dir);
|
|
264
287
|
if (!Object.hasOwn(roles, role)) {
|
|
265
288
|
res.statusCode = 404;
|
|
@@ -272,6 +295,7 @@ export function research({ projects, }) {
|
|
|
272
295
|
},
|
|
273
296
|
];
|
|
274
297
|
}
|
|
298
|
+
/** The Tailwind `content` globs that scan each project's `ui/`, skipping its `node_modules`; spread into the host's config. */
|
|
275
299
|
export function tailwindContent(projects) {
|
|
276
300
|
return Object.values(projects).flatMap((path) => [
|
|
277
301
|
`${path}/ui/**/*.{ts,tsx}`,
|